/* ── Design tokens ────────────────────────────────────────── */
:root {
  /* Brand / primary */
  --primary:              #2563eb;
  --primary-dark:         #1d4ed8;
  --primary-fg:           #ffffff;

  /* Sidebar */
  --sidebar-bg:           #1e2532;
  --sidebar-width:        220px;
  --sidebar-link-hover:   rgba(255,255,255,0.08);
  --sidebar-link-active:  #2563eb;
  --sidebar-fg:           #c8d6f0;
  --sidebar-fg-muted:     #7a8aaa;

  /* Page surface */
  --page-bg:              #f0f2f5;
  --card-bg:              #ffffff;
  --card-shadow:          0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --card-radius:          8px;

  /* Table */
  --table-row-alt:        #f8f9fb;
  --table-border:         #e5e7eb;
  --table-header-bg:      #f1f3f6;

  /* Borders / dividers */
  --border:               #e5e7eb;
  --border-focus:         #2563eb;

  /* Text */
  --text-primary:         #111827;
  --text-secondary:       #4b5563;
  --text-muted:           #9ca3af;

  /* State surfaces */
  --color-inactive-surface: #f3f4f6;
  --color-inactive-border:  #e5e7eb;

  /* Severity (retain existing) */
  --severity-low:         #f59e0b;
  --severity-low-fg:      #1a1208;
  --severity-medium:      #ea580c;
  --severity-medium-fg:   #ffffff;
  --severity-high:        #dc2626;
  --severity-high-fg:     #ffffff;
  --severity-critical:    #7f1d1d;
  --severity-critical-fg: #ffffff;

  /* Success */
  --success:              #16a34a;
  --success-border:       #16a34a;
  --success-surface:      #f0fdf4;

  /* Legacy aliases — keep for any templates referencing old names */
  --color-primary:        #2563eb;
  --color-primary-dark:   #1d4ed8;
  --color-nav-bg:         #1e2532;
  --color-text-muted:     #9ca3af;
}

/* ── Reset & base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--page-bg);
}

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

h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: 1rem; color: var(--text-primary); }
h2 { font-size: 1.1rem; font-weight: 600; margin-bottom: 0.5rem; color: var(--text-secondary); }
h3 { font-size: 1rem; font-weight: 600; margin-bottom: 0.4rem; }

/* ── App shell — sidebar + content ───────────────────────── */
.app-shell {
  display: flex;
  min-height: 100vh;
}

/* ── Sidebar ──────────────────────────────────────────────── */
.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  z-index: 100;
  overflow-y: auto;
}

.sidebar-brand {
  padding: 1.25rem 1rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}

.sidebar-brand-name {
  display: block;
  font-size: 1.05rem;
  font-weight: 700;
  color: #ffffff;
  letter-spacing: 0.02em;
}

.sidebar-brand-sub {
  display: block;
  font-size: 0.78rem;
  color: var(--sidebar-fg-muted);
  margin-top: 2px;
}

/* ── Sidebar nav links ────────────────────────────────────── */
.sidebar-nav {
  flex: 1;
  padding: 0.5rem 0;
}

.sidebar-link {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 0 1rem;
  color: var(--sidebar-fg);
  font-size: 0.92rem;
  transition: background 0.12s, color 0.12s;
  text-decoration: none;
  width: 100%;
}

.sidebar-link:hover {
  background: var(--sidebar-link-hover);
  color: #ffffff;
  text-decoration: none;
}

.sidebar-link-active {
  background: var(--sidebar-link-active) !important;
  color: #ffffff !important;
  font-weight: 600;
}

/* ── Sidebar identity block ───────────────────────────────── */
.sidebar-identity {
  padding: 0.75rem 1rem;
  border-top: 1px solid rgba(255,255,255,0.08);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  flex-shrink: 0;
}

.sidebar-user-name {
  font-size: 0.88rem;
  font-weight: 600;
  color: #ffffff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-role-label {
  font-size: 0.78rem;
  color: var(--sidebar-fg-muted);
}

.sidebar-logout {
  display: inline-block;
  margin-top: 0.4rem;
  font-size: 0.82rem;
  color: var(--sidebar-fg);
  padding: 0.25rem 0;
}
.sidebar-logout:hover { color: #ffffff; text-decoration: none; }

/* ── Page body ────────────────────────────────────────────── */
.page-body {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-height: 100vh;
  background: var(--page-bg);
  display: flex;
  flex-direction: column;
}

/* Unauthenticated pages (login, password reset) have no sidebar — remove the
   left margin so the page-body fills the full viewport width. */
.page-body--full {
  margin-left: 0;
}

main {
  flex: 1;
  padding: 1.5rem 1.75rem;
}

/* ── Cards ────────────────────────────────────────────────── */
.card {
  background: var(--card-bg);
  border-radius: var(--card-radius);
  box-shadow: var(--card-shadow);
  padding: 1.5rem;
  margin-bottom: 1.25rem;
}

.card-header {
  margin-bottom: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}

/* ── Tables ───────────────────────────────────────────────── */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  background: var(--card-bg);
}

thead th {
  background: var(--table-header-bg);
  color: var(--text-secondary);
  font-size: 0.82rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.65rem 0.9rem;
  border-bottom: 2px solid var(--table-border);
  text-align: left;
  white-space: nowrap;
}

tbody tr:nth-child(even) { background: var(--table-row-alt); }
tbody tr:hover { background: #eef1f6; }

tbody td {
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--table-border);
  color: var(--text-primary);
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }

.mono { font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace; font-size: 0.85rem; }

/* ── Buttons ──────────────────────────────────────────────── */
.button-primary, button[type="submit"], input[type="submit"] {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 1.25rem;
  background: var(--primary);
  color: var(--primary-fg);
  border: none;
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.12s;
}

.button-primary:hover, button[type="submit"]:hover, input[type="submit"]:hover {
  background: var(--primary-dark);
  color: var(--primary-fg);
  text-decoration: none;
}

.button-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 1.25rem;
  background: transparent;
  color: var(--primary);
  border: 1.5px solid var(--primary);
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.12s, color 0.12s;
}

.button-secondary:hover {
  background: var(--primary);
  color: #ffffff;
  text-decoration: none;
}

.button-danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 1.25rem;
  background: var(--severity-high);
  color: #ffffff;
  border: none;
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.button-danger:hover { background: #b91c1c; color: #ffffff; text-decoration: none; }

/* ── Forms ────────────────────────────────────────────────── */
label {
  display: block;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.3rem;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="search"],
input[type="date"],
input[type="file"],
select,
textarea {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0.5rem 0.75rem;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  font-size: 0.95rem;
  color: var(--text-primary);
  background: var(--card-bg);
  transition: border-color 0.12s;
  font-family: inherit;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}

textarea { min-height: 80px; resize: vertical; }

.field { margin-bottom: 1rem; }
.field-error { font-size: 0.82rem; color: var(--severity-high); margin-top: 0.25rem; }

/* Touch-friendly input alias */
.touch-input { min-height: 48px; }

/* ── Severity badges ──────────────────────────────────────── */
.severity-badge,
.severity-low,
.severity-medium,
.severity-high,
.severity-critical,
.severity-borderline,
.severity-moderate,
.severity-severe {
  display: inline-block;
  border-radius: 9999px;
  padding: 3px 10px;
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.severity-low, .severity-borderline {
  background: var(--severity-low);
  color: var(--severity-low-fg);
}
.severity-medium, .severity-moderate {
  background: var(--severity-medium);
  color: var(--severity-medium-fg);
}
.severity-high, .severity-severe {
  background: var(--severity-high);
  color: var(--severity-high-fg);
}
.severity-critical {
  background: var(--severity-critical);
  color: var(--severity-critical-fg);
}

/* ── Message banners ──────────────────────────────────────── */
.error-banner, .warning-banner {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
  border-radius: 6px;
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.92rem;
}

.info-banner, .success-banner {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
  border-radius: 6px;
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.92rem;
}

/* ── Session banner (legacy — kept for compat) ────────────── */
.session-banner {
  background: var(--color-inactive-surface);
  border-bottom: 1px solid var(--color-inactive-border);
  color: var(--text-muted);
  font-size: 0.82rem;
  text-align: center;
  padding: 4px 1rem;
}

/* ── Pagination ───────────────────────────────────────────── */
.pagination {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1rem;
  font-size: 0.9rem;
}

.pagination a, .pagination span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  min-width: 36px;
  padding: 0 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--primary);
  text-decoration: none;
}

.pagination a:hover { background: var(--primary); color: #ffffff; text-decoration: none; }
.pagination .current { background: var(--primary); color: #ffffff; border-color: var(--primary); font-weight: 700; }

/* ── Filter bar ───────────────────────────────────────────── */
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
  background: var(--card-bg);
  border-radius: var(--card-radius);
  box-shadow: var(--card-shadow);
  padding: 1rem 1.25rem;
  margin-bottom: 1.25rem;
}

.filter-bar .field { margin-bottom: 0; }
.filter-bar label { white-space: nowrap; }

/* ── Stat strip / summary cards ──────────────────────────── */
.stat-strip {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}

.stat-card {
  flex: 1;
  min-width: 140px;
  background: var(--card-bg);
  border-radius: var(--card-radius);
  box-shadow: var(--card-shadow);
  padding: 1rem 1.25rem;
}

.stat-card-label {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.stat-card-value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.2;
}

/* ── Chart containers ─────────────────────────────────────── */
.chart-container {
  background: var(--card-bg);
  border-radius: var(--card-radius);
  box-shadow: var(--card-shadow);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}

.chart-container h2 { margin-bottom: 0.75rem; }

/* ── Misc utility ─────────────────────────────────────────── */
.text-muted { color: var(--text-muted); font-size: 0.88rem; }
.text-success { color: var(--success); }
.text-danger  { color: var(--severity-high); }

.badge-corrected {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 9999px;
  background: #fef3c7;
  color: #92400e;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.badge-correction {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 9999px;
  background: #dbeafe;
  color: #1e40af;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ── Definition lists (confirm screen) ───────────────────── */
dl.confirm-fields {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.4rem 1.25rem;
  font-size: 0.95rem;
}

dl.confirm-fields dt {
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
}

dl.confirm-fields dd { color: var(--text-primary); }

/* ── Success surface (confirm card) ──────────────────────── */
.success-surface {
  border: 1.5px solid var(--success-border) !important;
  background: var(--success-surface) !important;
}

/* ── Typography helpers ───────────────────────────────────── */
.typography-display {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 0.75rem;
}

/* ── Responsive layout ───────────────────────────────────── */
:root {
  --topbar-height: 52px;
  /* Tablet sidebar width — coupled to .page-body margin-left in the tablet
     media query below. Change both together via this single variable. */
  --sidebar-width-tablet: 200px;
}

/* Mobile top bar — hidden by default on all viewports */
.mobile-topbar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 200;
  background: var(--sidebar-bg);
  height: var(--topbar-height);
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
}

/* Hamburger button */
.hamburger-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
  min-height: 44px;
  min-width: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Sidebar overlay backdrop */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 150;
}

/* ── Mobile (< 768px) ─────────────────────────────────────── */
@media (max-width: 767px) {
  .mobile-topbar { display: flex; }
  /* The backdrop only exists at the mobile breakpoint where the hamburger
     drawer lives — scoping it here prevents a stuck overlay if the viewport
     is resized to tablet/desktop while the drawer is open. */
  .sidebar-overlay.is-open { display: block; }
  /* Lock background scroll while the drawer is open. */
  body.nav-open { overflow: hidden; }
  .sidebar {
    transform: translateX(-100%);
    /* visibility: hidden removes the closed drawer from tab order and the
       accessibility tree without affecting the CSS-driven slide animation.
       A transition-delay equal to the animation duration ensures visibility
       stays visible during the slide-out and only hides AFTER the animation
       completes — preventing an immediate invisible snap on close. */
    visibility: hidden;
    transition: transform 0.22s ease, visibility 0s 0.22s;
    top: var(--topbar-height);
    height: calc(100vh - var(--topbar-height));
    z-index: 160;
  }
  /* On open: show immediately (no delay) and slide in. */
  .sidebar.is-open {
    transform: translateX(0);
    visibility: visible;
    transition: transform 0.22s ease, visibility 0s;
  }
  .page-body { margin-left: 0; padding-top: var(--topbar-height); }
  main { padding: 1rem; }
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }
  .filter-bar { flex-direction: column; }
  .filter-bar .field { width: 100%; }
  .stat-strip { flex-direction: column; }
  .chart-container canvas { max-width: 100%; height: auto !important; }
  select,
  input[type="text"],
  input[type="date"],
  input[type="file"],
  input[type="search"],
  input[type="password"],
  input[type="email"],
  textarea {
    min-height: 48px;
    width: 100%;
    font-size: 1rem;
  }
  button[type="submit"],
  .button-primary { width: 100%; min-height: 48px; }
  .card { padding: 1rem; }
}

/* ── Tablet (768px – 1024px) ──────────────────────────────── */
/* Upper bound is 1024.98px (not 1024px) so fractional/zoomed logical widths
   in the 1024–1025px band are not left uncovered by either media query. */
@media (min-width: 768px) and (max-width: 1024.98px) {
  .mobile-topbar { display: none; }
  .sidebar { transform: none; width: var(--sidebar-width-tablet); visibility: visible; }
  .page-body { margin-left: var(--sidebar-width-tablet); }
  /* Unauthenticated pages have no sidebar — keep the page-body full-width at
     the tablet breakpoint too. The base .page-body--full rule has equal
     specificity to the tablet .page-body rule above, so without this
     in-media-query override the tablet margin-left would re-apply a phantom
     left margin on the (sidebar-less) login page. */
  .page-body--full { margin-left: 0; }
}

/* ── Desktop (≥ 1025px) ───────────────────────────────────── */
@media (min-width: 1025px) {
  .mobile-topbar { display: none; }
  .sidebar { transform: none; position: fixed; visibility: visible; }
  /* Defensive override: if a future rule ever adds margin-left to .page-body at
     this breakpoint, unauthenticated pages (login, password reset) would gain a
     phantom left margin. Mirror the tablet pattern to prevent that regression. */
  .page-body--full { margin-left: 0; }
}
