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

:root {
  --sidebar-w: 220px;

  --bg:        #F1F5F9;
  --surface:   #FFFFFF;
  --border:    #E2E8F0;
  --border-md: #CBD5E1;
  --border-lt: var(--border);

  --text-1:    #0F172A;
  --text-2:    #475569;
  --text-3:    #94A3B8;

  --sidebar-bg:     #0F172A;
  --sidebar-text:   #94A3B8;
  --sidebar-active: #FFFFFF;
  --sidebar-hover:  #1E293B;
  --sidebar-accent: #6366F1;

  --accent:    #6366F1;
  --accent-dk: #4F46E5;
  --success:   #10B981;
  --warning:   #F59E0B;
  --danger:    #EF4444;

  --radius-sm: 6px;
  --radius:    10px;
  --radius-lg: 14px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
  --shadow:    0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,.07), 0 2px 4px -1px rgba(0,0,0,.05);

  /* Secondary surfaces & status tints (themed — see [data-theme="dark"]) */
  --surface-2:   #F8FAFC;
  --code-bg:     #F1F5F9;
  --code-text:   #334155;
  --neutral-bg:  #F1F5F9;
  --neutral-text:#64748B;
  --link:        #2563EB;
  --accent-soft: #EEF2FF;
  --ok-bg:   #ECFDF5; --ok-text:   #065F46; --ok-border:   #A7F3D0;
  --err-bg:  #FEF2F2; --err-text:  #991B1B; --err-border:  #FECACA;
  --warn-bg: #FFFBEB; --warn-text: #92400E; --warn-border: #FDE68A; --warn-strong: #B45309;
  --info-bg: #EFF6FF; --info-text: #1D4ED8; --info-border: #BFDBFE;
  color-scheme: light;

  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: var(--text-1);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ── Dark theme ───────────────────────────────────────────────────
   Set on <html data-theme="dark"> by the theme switcher (base.html). */
:root[data-theme="dark"] {
  --bg:        #0B1120;
  --surface:   #131B2C;
  --surface-2: #1A2336;
  --border:    #243049;
  --border-md: #334155;

  --text-1:    #E2E8F0;
  --text-2:    #A3B1C6;
  --text-3:    #718096;

  --sidebar-bg:     #070C17;
  --sidebar-hover:  #1A2336;

  --accent:    #818CF8;
  --accent-dk: #6366F1;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
  --shadow:    0 1px 3px rgba(0,0,0,.5), 0 1px 2px rgba(0,0,0,.3);
  --shadow-md: 0 4px 10px -1px rgba(0,0,0,.55), 0 2px 4px -1px rgba(0,0,0,.35);

  --code-bg:     #1E293B;
  --code-text:   #CBD5E1;
  --neutral-bg:  #1E293B;
  --neutral-text:#94A3B8;
  --link:        #60A5FA;
  --accent-soft: rgba(129,140,248,.16);
  --ok-bg:   rgba(16,185,129,.14);  --ok-text:   #6EE7B7; --ok-border:   rgba(16,185,129,.35);
  --err-bg:  rgba(239,68,68,.14);   --err-text:  #FCA5A5; --err-border:  rgba(239,68,68,.35);
  --warn-bg: rgba(245,158,11,.13);  --warn-text: #FCD34D; --warn-border: rgba(245,158,11,.35); --warn-strong: #FBBF24;
  --info-bg: rgba(59,130,246,.15);  --info-text: #93C5FD; --info-border: rgba(59,130,246,.35);
  color-scheme: dark;
}
/* Form controls without their own colors (browser defaults are light). */
:root[data-theme="dark"] select,
:root[data-theme="dark"] textarea,
:root[data-theme="dark"] input:not([type=checkbox]):not([type=radio]):not([type=range]) {
  background-color: var(--surface); color: var(--text-1); border-color: var(--border-md);
}

/* ── Shell layout ─────────────────────────────────────────────── */
.shell { display: flex; min-height: 100vh; }

/* ── Sidebar ──────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  position: fixed;
  inset: 0 auto 0 0;
  overflow-y: auto;
  z-index: 30;
  transition: transform .2s ease;
}

/* ── Mobile nav: hamburger + off-canvas sidebar ──────────────────── */
.sidebar-toggle {
  display: none;
  align-items: center; justify-content: center;
  width: 34px; height: 34px; flex-shrink: 0;
  background: transparent; border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer; color: var(--text-2);
}
.sidebar-toggle svg { width: 18px; height: 18px; }
.sidebar-toggle:hover { background: var(--bg); color: var(--text-1); }

.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(15,23,42,.5);
  z-index: 25;
}
.sidebar-backdrop.open { display: block; }

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 18px 18px;
  border-bottom: 1px solid #1E293B;
  text-decoration: none;
}
.sidebar-brand .brand-icon {
  width: 30px; height: 30px;
  background: var(--accent);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; color: #fff; font-weight: 700; flex-shrink: 0;
}
.sidebar-brand .brand-name {
  font-size: 13px; font-weight: 600;
  color: var(--sidebar-active);
  line-height: 1.3;
}
.sidebar-brand .brand-sub {
  font-size: 11px; color: var(--sidebar-text);
  display: block;
}

.sidebar-nav { padding: 12px 10px; flex: 1; }
.nav-section { margin-bottom: 20px; }
.nav-label {
  font-size: 10px; font-weight: 600; letter-spacing: .08em;
  text-transform: uppercase; color: #334155;
  padding: 0 8px; margin-bottom: 4px;
}
.nav-item {
  display: flex; align-items: center; gap: 9px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--sidebar-text); text-decoration: none;
  font-size: 13px; font-weight: 500;
  transition: background .12s, color .12s;
  margin-bottom: 1px;
}
.nav-item:hover { background: var(--sidebar-hover); color: var(--sidebar-active); }
.nav-item.active { background: #1E293B; color: var(--sidebar-active); }
.nav-item svg { width: 16px; height: 16px; flex-shrink: 0; opacity: .7; }
.nav-item.active svg, .nav-item:hover svg { opacity: 1; }
button.nav-item {   /* Sign out is a form button styled as a nav link */
  width: 100%; background: none; border: 0; cursor: pointer;
  font-family: inherit; text-align: left;
}

.sidebar-footer {
  padding: 12px 10px;
  border-top: 1px solid #1E293B;
}

/* ── Main ─────────────────────────────────────────────────────── */
.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-width: 0; /* let children shrink instead of overflowing on narrow screens */
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: space-between;
  position: sticky; top: 0; z-index: 5;
}
.topbar-title { font-size: 15px; font-weight: 600; color: var(--text-1); }
.topbar-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: flex-end; }

.content { padding: 28px; flex: 1; max-width: 1100px; min-width: 0; }

/* ── Mobile layout ────────────────────────────────────────────── */
@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-toggle { display: inline-flex; }

  .main { margin-left: 0; }

  .topbar {
    padding: 10px 14px;
    height: auto; min-height: 56px;
    flex-wrap: wrap;
  }
  .topbar-title { flex: 1 1 auto; }

  .content { padding: 16px; }

  .login-shell { padding: 16px; }

  .filter-input { width: 100% !important; }

  .form-grid input[type=text], .form-grid input[type=password] { min-width: 0; flex: 1 1 160px; }

  .grid-2col { grid-template-columns: 1fr !important; }
}

/* ── Flash ────────────────────────────────────────────────────── */
.flash {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 14px; border-radius: var(--radius-sm);
  margin-bottom: 20px; font-size: 13px; font-weight: 500;
}
.flash::before { font-size: 15px; }
.flash.success { background: var(--ok-bg); color: var(--ok-text); border: 1px solid var(--ok-border); }
.flash.success::before { content: "✓"; }
.flash.error   { background: var(--err-bg); color: var(--err-text); border: 1px solid var(--err-border); }
.flash.error::before { content: "✕"; }

/* ── Cards ────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 8px;
}
.card-header h2 { font-size: 13px; font-weight: 600; color: var(--text-1); }
.card-body { padding: 18px; }
.card + .card { margin-top: 16px; }
/* In a grid the gap does the spacing — a top margin would push every tile
   but the first out of line. */
.card-grid > .card + .card { margin-top: 0; }

/* ── Stat cards ───────────────────────────────────────────────── */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px 20px;
}
.stat-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); margin-bottom: 6px; }
.stat-value { font-size: 26px; font-weight: 700; color: var(--text-1); line-height: 1; }
.stat-sub { font-size: 12px; color: var(--text-3); margin-top: 4px; }
/* Long values (e.g. a hostname) wrap instead of pushing the copy button out of the card */
.stat-card { min-width: 0; }
.stat-card .stat-value { min-width: 0; overflow-wrap: anywhere; line-height: 1.25; }
.stat-card .btn-copy { flex-shrink: 0; }

/* Health meter bar — used by the Server Health tiles (fill colour set in JS) */
.meter { position: relative; height: 6px; border-radius: 4px; background: var(--border); overflow: hidden; margin-top: 12px; }
.meter > span { display: block; height: 100%; border-radius: 4px; background: var(--success); transition: width .4s ease, background .4s ease; }
/* Configured-alert-threshold marker (set via JS: left = threshold %) */
.meter > .meter-mark { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--text-2); opacity: .55; border-radius: 1px; }

/* ── Tables ───────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead tr { border-bottom: 1px solid var(--border); }
th {
  text-align: left; padding: 10px 16px;
  font-size: 11px; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase; color: var(--text-3);
  white-space: nowrap;
}
td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--surface-2); }

/* ── Badges ───────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 99px;
  font-size: 11px; font-weight: 600; letter-spacing: .03em;
  white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
.badge-active  { background: var(--ok-bg); color: var(--ok-text); } .badge-active::before  { background: #10B981; }
.badge-built   { background: var(--ok-bg); color: var(--ok-text); } .badge-built::before   { background: #10B981; }
.badge-signed  { background: var(--info-bg); color: var(--info-text); } .badge-signed::before  { background: #3B82F6; }
.badge-pending { background: var(--warn-bg); color: var(--warn-text); } .badge-pending::before { background: #F59E0B; animation: pulse 1.5s infinite; }
.badge-failed  { background: var(--err-bg); color: var(--err-text); } .badge-failed::before  { background: #EF4444; }
.badge-offline { background: var(--neutral-bg); color: var(--neutral-text); } .badge-offline::before { background: #94A3B8; }
.badge-unknown { background: var(--neutral-bg); color: var(--text-2); } .badge-unknown::before { background: #94A3B8; }

@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:.4; } }

/* ── Buttons ──────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 500; cursor: pointer;
  border: 1px solid transparent; text-decoration: none;
  transition: background .12s, box-shadow .12s, opacity .12s;
  line-height: 1.4; white-space: nowrap;
}
.btn:active { opacity: .85; }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 1px 2px rgba(99,102,241,.25); }
.btn-primary:hover { background: var(--accent-dk); }
.btn-ghost { background: transparent; color: var(--text-2); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg); color: var(--text-1); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #DC2626; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn svg { width: 14px; height: 14px; }

/* ── Forms ────────────────────────────────────────────────────── */
.form-grid { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 5px; }
label { font-size: 12px; font-weight: 500; color: var(--text-2); }
input[type=text], input[type=password] {
  padding: 7px 11px; border: 1px solid var(--border-md);
  border-radius: var(--radius-sm); font-size: 13px;
  color: var(--text-1); background: var(--surface);
  transition: border-color .12s, box-shadow .12s;
  min-width: 200px; outline: none;
}
input[type=text]:focus, input[type=password]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99,102,241,.12);
}
.hint { font-size: 11px; color: var(--text-3); margin-top: 2px; }

/* ── Empty state ──────────────────────────────────────────────── */
.empty {
  text-align: center; padding: 40px 20px;
  color: var(--text-3); font-size: 13px;
}
.empty-icon { font-size: 32px; margin-bottom: 8px; line-height: 1; }
.empty p { margin-top: 4px; }

/* ── Misc helpers ─────────────────────────────────────────────── */
code {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: .85em; background: var(--code-bg);
  padding: 2px 6px; border-radius: 4px; color: var(--code-text);
}
.text-muted { color: var(--text-3); }
.text-sm    { font-size: 12px; }
.mono       { font-family: monospace; font-size: .9em; }
.mt         { margin-top: 16px; }
.section    { margin-bottom: 28px; }
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px; flex-wrap: wrap; gap: 8px;
}
.section-head h2 { font-size: 14px; font-weight: 600; }
.page-head { margin-bottom: 22px; }
.page-head h1 { font-size: 20px; font-weight: 700; }
.page-head p  { color: var(--text-2); margin-top: 4px; font-size: 13px; }

/* ── Login ────────────────────────────────────────────────────── */
.login-shell {
  min-height: 100vh; display: flex; align-items: center;
  justify-content: center; background: var(--bg);
}
.login-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-md);
  padding: 40px 36px; width: 100%; max-width: 360px;
}
.login-logo {
  display: flex; align-items: center; gap: 10px; margin-bottom: 28px;
}
.login-logo .brand-icon {
  width: 36px; height: 36px; background: var(--accent);
  border-radius: 10px; display: flex; align-items: center;
  justify-content: center; font-size: 16px; color: #fff; font-weight: 700;
}
.login-logo .brand-name { font-size: 15px; font-weight: 700; color: var(--text-1); }
.login-logo .brand-sub  { font-size: 12px; color: var(--text-3); display: block; }
.login-card h2 { font-size: 17px; font-weight: 700; margin-bottom: 6px; }
.login-card p  { font-size: 13px; color: var(--text-2); margin-bottom: 24px; }
.login-field   { margin-bottom: 14px; }
.login-field label { display: block; margin-bottom: 6px; }
.login-field input { width: 100%; }
.login-card .btn { width: 100%; justify-content: center; padding: 9px; margin-top: 6px; }

/* ── Wide device tables ───────────────────────────────────────── */
/* Pages with many-column tables use the full window width instead of
   the 1100px reading width, and pack cells tighter. */
.content.content-wide { max-width: none; }
.device-table th, .device-table td { padding: 10px 10px; }
.device-table th:first-child, .device-table td:first-child { padding-left: 16px; }
.device-table th:last-child,  .device-table td:last-child  { padding-right: 16px; }
.device-table td:first-child, .device-table td:last-child  { white-space: nowrap; width: 1px; }
.device-table .btn-sm { padding: 5px 8px; }
/* Least important column goes first on narrower screens (it's also in the Edit dialog) */
@media (max-width: 1500px) {
  .device-table .col-registered { display: none; }
}

/* ── Sortable table headers ───────────────────────────────────── */
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text-1); }
.sort-icon { font-style: normal; color: var(--text-3); margin-left: 3px; font-size: 11px; }

/* ── Copy button ──────────────────────────────────────────────── */
.btn-copy {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 8px; border-radius: var(--radius-sm);
  font-size: 12px; font-weight: 500; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-3); transition: color .12s, border-color .12s, background .12s;
  line-height: 1; white-space: nowrap;
}
.btn-copy:hover { color: var(--text-1); border-color: var(--border-md); background: var(--bg); }
.btn-copy svg { width: 13px; height: 13px; flex-shrink: 0; }

/* ── Key-value detail ─────────────────────────────────────────── */
.kv-grid { display: grid; grid-template-columns: auto 1fr; gap: 0; }
.kv-row { display: contents; }
.kv-row .kv-key,
.kv-row .kv-val {
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  font-size: 13px; align-items: center; display: flex;
}
.kv-row:last-child .kv-key,
.kv-row:last-child .kv-val { border-bottom: none; }
.kv-key { font-weight: 500; color: var(--text-2); white-space: nowrap; background: var(--surface-2); min-width: 140px; }
.kv-val { color: var(--text-1); word-break: break-all; }

/* ── Modal backdrops (shared by users.html's .modal-backdrop and the
   ad-hoc inline-styled backdrops in devices/groups/group_detail) ──── */
.modal-backdrop { padding: 16px; }

.filter-input {
  padding: 7px 11px; border: 1px solid var(--border-md);
  border-radius: var(--radius-sm); font-size: 13px;
  outline: none; background: var(--surface); color: var(--text-1);
}

/* ── Pubkey highlight ─────────────────────────────────────────── */
.pubkey-box {
  background: #0F172A; color: #A5F3FC;
  font-family: monospace; font-size: 13px;
  padding: 14px 16px; border-radius: var(--radius-sm);
  word-break: break-all; line-height: 1.6;
  border: 1px solid #1E293B;
}

/* Dark theme: keep primary buttons at the deeper indigo so white text stays readable. */
:root[data-theme="dark"] .btn-primary { background: var(--accent-dk); }

/* ── Theme switcher (sidebar footer) ─────────────────────────────── */
.theme-switch { display: flex; gap: 2px; margin: 4px 12px 8px; padding: 3px; background: #0B1222; border: 1px solid #1E293B; border-radius: 8px; }
.theme-switch button {
  flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  padding: 5px 0; border: 0; border-radius: 6px; background: transparent;
  color: var(--sidebar-text); font: inherit; font-size: 11px; font-weight: 500; cursor: pointer;
}
.theme-switch button:hover { color: #E2E8F0; }
.theme-switch button[aria-pressed="true"] { background: #1E293B; color: #fff; }
.theme-switch svg { width: 13px; height: 13px; }
:root[data-theme="dark"] .btn-primary:hover { background: #4F46E5; }

/* ── Data-viz palette (reports) ──────────────────────────────────────
   Categorical slots in fixed order — validated (CVD + normal-vision
   separation, adjacent pairs) on this card surface in both themes. Light
   aqua/yellow/magenta are under 3:1 on white, so every colored mark is
   labeled in its table. Status tones are fixed and never used as a series. */
:root {
  --viz-1: #2a78d6; --viz-2: #eb6834; --viz-3: #1baf7a; --viz-4: #eda100;
  --viz-5: #e87ba4; --viz-6: #008300; --viz-7: #6250d6; --viz-8: #e34948;
  --viz-neutral: #A8B3C2;
  --status-good: #0ca30c; --status-warning: #fab219; --status-serious: #ec835a;
  --status-critical: #d03b3b; --status-neutral: #A8B3C2; --status-info: #2a78d6;
}
:root[data-theme="dark"] {
  --viz-1: #3987e5; --viz-2: #d95926; --viz-3: #199e70; --viz-4: #c98500;
  --viz-5: #d55181; --viz-6: #008300; --viz-7: #9085e9; --viz-8: #e66767;
  --viz-neutral: #4B5870; --status-neutral: #4B5870; --status-info: #3987e5;
}
