/* ===== Design system: theme from tenant (hex → HSL in layout) ===== */
:root {
  --theme-h: 221;
  --theme-s: 91%;
  --theme-l: 60%;
  --theme: hsl(var(--theme-h), var(--theme-s), var(--theme-l));
  --theme-foreground: hsl(var(--theme-h), var(--theme-s), 98%);
  --theme-hover: hsl(var(--theme-h), var(--theme-s), 50%);
  --theme-muted: hsl(var(--theme-h), var(--theme-s), 94%);
  --theme-ring: hsl(var(--theme-h), var(--theme-s), 90% / 0.5);
  --sidebar-bg: hsl(224, 28%, 12%);
  --sidebar-fg: hsl(220, 14%, 88%);
  --sidebar-fg-dim: hsl(220, 10%, 55%);
  --sidebar-border: hsl(220, 18%, 20%);
  --sidebar-hover: hsl(220, 18%, 18%);
  --page-bg: hsl(220, 20%, 97%);
  --card-bg: #fff;
  --border: hsl(220, 16%, 90%);
  --border-strong: hsl(220, 14%, 82%);
  --text: hsl(222, 22%, 16%);
  --text-muted: hsl(220, 12%, 45%);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px hsl(220 18% 40% / 0.06);
  --shadow: 0 4px 12px hsl(220 18% 40% / 0.08);
  --shadow-lg: 0 12px 40px hsl(220 18% 40% / 0.12);
  --font-sans: "DM Sans", system-ui, -apple-system, sans-serif;
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --transition: 0.2s ease;
}

body[data-theme-color] { /* overridden inline from tenant theme_color */ }

/* ===== Base ===== */
*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: var(--page-bg);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--theme);
  text-decoration: none;
  transition: color var(--transition);
}
a:hover { color: var(--theme-hover); text-decoration: underline; }

/* ===== App shell ===== */
.app-shell {
  display: flex;
  min-height: 100vh;
  position: relative;
}

/* ===== Sidebar ===== */
.app-sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  color: var(--sidebar-fg);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--sidebar-border);
  z-index: 100;
  transition: transform var(--transition), box-shadow var(--transition);
}

.sidebar-brand {
  padding: var(--space-5) var(--space-6);
  font-weight: 700;
  font-size: 1.125rem;
  letter-spacing: -0.02em;
  border-bottom: 1px solid var(--sidebar-border);
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-nav {
  padding: var(--space-3) var(--space-2);
  flex: 1;
  overflow-y: auto;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  margin: 2px var(--space-2) 2px 0;
  border-radius: var(--radius-sm);
  color: var(--sidebar-fg);
  text-decoration: none;
  transition: background var(--transition), color var(--transition);
  font-weight: 500;
  font-size: 0.9375rem;
}

.sidebar-nav a:hover {
  background: var(--sidebar-hover);
  color: #fff;
  text-decoration: none;
}

.sidebar-nav a.active {
  background: var(--theme);
  color: var(--theme-foreground);
}
.sidebar-nav a.active:hover {
  background: var(--theme-hover);
  color: var(--theme-foreground);
  text-decoration: none;
}

.sidebar-close {
  display: none;
  position: absolute;
  top: var(--space-4);
  right: var(--space-4);
  width: 40px;
  height: 40px;
  align-items: center;
  justify-content: center;
  border: none;
  background: var(--sidebar-hover);
  color: var(--sidebar-fg);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
}

/* ===== Main + topbar ===== */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 100vh;
}

.app-topbar {
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: var(--space-3) var(--space-6);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow-sm);
}

.topbar-menu-btn {
  display: none;
  width: 44px;
  height: 44px;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 1.35rem;
}
.topbar-menu-btn:hover { background: var(--border); }

.app-topbar .breadcrumb {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  font-size: 0.875rem;
  color: var(--text-muted);
}

.app-topbar .breadcrumb a {
  color: var(--text-muted);
}
.app-topbar .breadcrumb a:hover {
  color: var(--theme);
  text-decoration: none;
}

.app-topbar .user-menu {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}
.user-menu .user-name {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
}
.user-menu .btn { font-size: 0.875rem; }

/* ===== Content ===== */
.app-content {
  flex: 1;
  padding: var(--space-6) var(--space-8);
}

/* ===== Flash ===== */
.flash-messages {
  padding: 0 var(--space-8);
}

.flash-messages .notice,
.flash-messages .alert {
  padding: var(--space-4) var(--space-5);
  margin: var(--space-4) 0 0;
  border-radius: var(--radius-sm);
  font-size: 0.9375rem;
  font-weight: 500;
  border: 1px solid transparent;
}

.flash-messages .notice {
  background: hsl(152, 70%, 96%);
  color: hsl(152, 45%, 28%);
  border-color: hsl(152, 55%, 88%);
}

.flash-messages .alert {
  background: hsl(0, 85%, 97%);
  color: hsl(0, 55%, 32%);
  border-color: hsl(0, 65%, 88%);
}

/* ===== Page header ===== */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-6);
  gap: var(--space-4);
  flex-wrap: wrap;
}

.page-header h1,
.page-title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

.page-title {
  margin-bottom: var(--space-5);
}

.page-actions {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* ===== Cards ===== */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  padding: var(--space-6);
  margin-bottom: var(--space-6);
  transition: box-shadow var(--transition);
}
.card:hover { box-shadow: var(--shadow-lg); }

.card-title {
  margin: 0 0 var(--space-4);
  font-size: 1.0625rem;
  font-weight: 600;
}

/* Report / link cards (index) */
.report-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: var(--space-4);
}

.report-card {
  display: block;
  text-decoration: none;
  color: inherit;
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: var(--space-5);
  transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
}
.report-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
  border-color: var(--theme);
  text-decoration: none;
  color: inherit;
}
.report-card .report-card-icon {
  font-size: 2rem;
  margin-bottom: var(--space-3);
  opacity: 0.9;
}
.report-card strong { font-size: 1rem; }
.report-card p {
  margin: var(--space-2) 0 0;
  font-size: 0.875rem;
  color: var(--text-muted);
  line-height: 1.45;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  font-size: 0.9375rem;
  font-weight: 500;
  font-family: inherit;
  line-height: 1.4;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--transition), color var(--transition), transform 0.1s ease;
}
.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--theme);
  color: var(--theme-foreground);
  box-shadow: 0 1px 2px hsl(var(--theme-h) var(--theme-s) 40% / 0.3);
}
.btn-primary:hover {
  background: var(--theme-hover);
  color: var(--theme-foreground);
  text-decoration: none;
  box-shadow: 0 2px 6px hsl(var(--theme-h) var(--theme-s) 40% / 0.35);
}

.btn-secondary {
  background: var(--card-bg);
  color: var(--text);
  border: 1px solid var(--border-strong);
}
.btn-secondary:hover {
  background: hsl(220, 14%, 95%);
  border-color: var(--text-muted);
  text-decoration: none;
  color: var(--text);
}

.btn-danger {
  background: hsl(0, 65%, 52%);
  color: #fff;
}
.btn-danger:hover {
  background: hsl(0, 65%, 45%);
  color: #fff;
  text-decoration: none;
}

.btn-sm {
  padding: var(--space-1) var(--space-3);
  font-size: 0.8125rem;
}

/* ===== Forms ===== */
.form-errors {
  background: hsl(0, 80%, 97%);
  border: 1px solid hsl(0, 60%, 88%);
  color: hsl(0, 50%, 28%);
  padding: var(--space-4) var(--space-5);
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-4);
}
.form-errors ul { margin: var(--space-2) 0 0 1.25rem; padding: 0; }

.form-group { margin-bottom: var(--space-4); }

.form-group label {
  display: block;
  margin-bottom: var(--space-1);
  font-weight: 500;
  font-size: 0.875rem;
  color: var(--text);
}

.form-control {
  width: 100%;
  max-width: 420px;
  padding: var(--space-2) var(--space-3);
  font-size: 0.9375rem;
  font-family: inherit;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--card-bg);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.form-control:focus {
  outline: none;
  border-color: var(--theme);
  box-shadow: 0 0 0 3px var(--theme-ring);
}

.form-control::placeholder { color: var(--text-muted); }

input[type="color"].form-control {
  height: 44px;
  padding: 4px;
  cursor: pointer;
}

select.form-control {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23666' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2.5rem;
}

textarea.form-control {
  resize: vertical;
  min-height: 88px;
}

.form-control-sm {
  padding: var(--space-1) var(--space-2);
  font-size: 0.875rem;
  max-width: none;
}

.form-hint {
  font-size: 0.875rem;
  color: var(--text-muted);
}

/* Form grids */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-4);
}
.form-grid .form-group { margin-bottom: 0; }

.form-grid-2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-4);
}
.form-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-4);
}

/* ===== Tables ===== */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

table.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9375rem;
}

table.table th,
table.table td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  border-bottom: 1px solid var(--border);
}

table.table th {
  font-weight: 600;
  font-size: 0.8125rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  background: hsl(220, 18%, 98%);
}

table.table tbody tr {
  transition: background var(--transition);
}
table.table tbody tr:hover {
  background: hsl(220, 20%, 98%);
}
table.table tbody tr:last-child td { border-bottom: none; }

.table-compact th,
.table-compact td {
  padding: var(--space-2) var(--space-3);
  font-size: 0.875rem;
}

.text-end { text-align: right; }

.table-actions {
  display: flex;
  gap: var(--space-1);
  flex-wrap: wrap;
}
.table-actions .btn { font-size: 0.8125rem; padding: var(--space-1) var(--space-2); }

/* ===== Badges ===== */
.badge {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge-success {
  background: hsl(152, 55%, 92%);
  color: hsl(152, 45%, 28%);
}

.badge-primary {
  background: var(--theme-muted);
  color: var(--theme);
}

.badge-secondary {
  background: hsl(220, 14%, 92%);
  color: var(--text-muted);
}

.badge-danger {
  background: hsl(0, 75%, 95%);
  color: hsl(0, 55%, 38%);
}

.badge-default {
  background: hsl(220, 14%, 92%);
  color: var(--text-muted);
}

/* Legacy badge class → primary */
.badge:not([class*="badge-"]) {
  background: var(--theme-muted);
  color: var(--theme);
}

/* ===== Filter bar ===== */
.filter-bar {
  display: flex;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
  flex-wrap: wrap;
  align-items: flex-end;
}
.filter-bar .form-group { margin-bottom: 0; flex: 1; min-width: 160px; }

.form-inline {
  display: flex;
  gap: var(--space-3);
  align-items: flex-end;
  flex-wrap: wrap;
}
.form-inline .form-group { margin-bottom: 0; flex: 1; min-width: 140px; }
.form-inline .form-group label { margin-bottom: var(--space-1); }

/* ===== Pagination ===== */
.pagination {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  margin: var(--space-4) 0;
  padding: 0;
  list-style: none;
}

.pagination a,
.pagination span {
  display: inline-block;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
  transition: all var(--transition);
}

.pagination a:hover {
  background: var(--theme-muted);
  border-color: var(--theme);
  color: var(--theme);
  text-decoration: none;
}

.pagination .current {
  background: var(--theme);
  color: var(--theme-foreground);
  border-color: var(--theme);
}

.pagination .disabled span { color: var(--text-muted); cursor: not-allowed; }

/* ===== Empty state ===== */
.empty-state {
  text-align: center;
  padding: var(--space-8) var(--space-4);
  color: var(--text-muted);
}

.empty-state-icon {
  font-size: 3rem;
  margin-bottom: var(--space-4);
  opacity: 0.5;
}

.empty-state .btn { margin-top: var(--space-3); }

/* ===== Settings grid ===== */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: var(--space-6);
}

.settings-section {
  background: hsl(220, 18%, 99%);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: var(--space-5);
}

.settings-section h3 {
  margin: 0 0 var(--space-4);
  font-size: 1rem;
  font-weight: 600;
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--border);
}

/* ===== Auth & standalone ===== */
.auth-page,
.standalone-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
}

.auth-card,
.standalone-page .card {
  width: 100%;
  max-width: 400px;
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  padding: var(--space-8);
}

.auth-card h1,
.standalone-page h1 {
  margin: 0 0 var(--space-6);
  font-size: 1.5rem;
  font-weight: 700;
  text-align: center;
}

.auth-card .form-group { margin-bottom: var(--space-5); }
.auth-card .actions { margin-top: var(--space-6); }
.auth-card .actions .btn { width: 100%; padding: var(--space-3); }
.auth-card .links { margin-top: var(--space-4); text-align: center; font-size: 0.9rem; color: var(--text-muted); }

/* ===== Dashboard ===== */
.dashboard-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.stat-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition);
}
.stat-card:hover { box-shadow: var(--shadow); }

.stat-card-label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: var(--space-2);
}

.stat-card-value {
  font-size: 1.375rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}
.stat-card-value--success { color: hsl(152, 45%, 32%); }
.stat-card-value--warning { color: hsl(35, 90%, 42%); }

.dashboard-charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-6);
}

.dashboard-chart-card {
  min-height: 320px;
}

.dashboard-chart-card--donut {
  min-height: 320px;
}

.dashboard-chart-title {
  font-size: 1rem;
  font-weight: 600;
  margin: 0 0 var(--space-4);
  color: var(--text);
}

.dashboard-chart-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 260px;
  color: var(--text-muted);
  font-size: 0.9375rem;
}

@media (max-width: 900px) {
  .dashboard-charts { grid-template-columns: 1fr; }
  .dashboard-stats { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 480px) {
  .dashboard-stats { grid-template-columns: 1fr; }
}

/* ===== Overlay (mobile sidebar) ===== */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: hsl(220 18% 20% / 0.5);
  z-index: 90;
  opacity: 0;
  transition: opacity var(--transition);
  pointer-events: none;
}
.sidebar-overlay.is-open {
  opacity: 1;
  pointer-events: auto;
}

/* ===== Responsive ===== */
@media (max-width: 1024px) {
  .app-content { padding: var(--space-4) var(--space-5); }
  .form-grid-2 { grid-template-columns: 1fr; }
  .form-grid-3 { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .topbar-menu-btn { display: flex; }
  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    transform: translateX(-100%);
    box-shadow: none;
  }
  .app-shell.sidebar-open .app-sidebar {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }
  .sidebar-close { display: flex; }
  .sidebar-overlay { display: block; }
  .app-shell.sidebar-open .sidebar-overlay.is-open { display: block; }

  .app-topbar { padding: var(--space-3) var(--space-4); }
  .app-content { padding: var(--space-4); }
  .flash-messages { padding: 0 var(--space-4); }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    margin-bottom: var(--space-4);
  }
  .page-header h1 { font-size: 1.25rem; }
  .page-actions { width: 100%; }

  .card { padding: var(--space-4); }
  .report-cards { grid-template-columns: 1fr; }
  .user-menu .user-name { display: none; }
}

@media (max-width: 480px) {
  .form-control { max-width: none; }
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar .form-group { min-width: 0; }
  .form-inline { flex-direction: column; }
  .form-inline .form-group { min-width: 0; }
  .table-wrap { margin-left: -var(--space-4); margin-right: -var(--space-4); border-radius: 0; border-left: none; border-right: none; }
}
