/* =====================================================================
   app.css — Ensemble Tools design system
   ---------------------------------------------------------------------
   ONE source of truth for tokens, shell, and shared components.
   Load this BEFORE each page's inline <style> so pages can still
   override locally. Replaces the ":root + retheme(1a)" block that was
   copy-pasted into all 11 pages.

   Layer order:  app.css  ->  page <style>  ->  mobile.css
   ===================================================================== */

/* ---------------------------------------------------------------------
   1. Tokens
   ------------------------------------------------------------------ */
:root {
  /* ink + surface */
  --ink:        #14161a;
  --ink-soft:   #3f4249;
  --muted:      #7f838b;
  --faint:      #a7abb2;
  --line:       #e8e9eb;
  --line-strong:#d7d9dc;
  --bg:         #f4f5f6;
  --card:       #ffffff;
  --accent:     #14161a;
  --accent-dim: #eef0f2;

  /* severity — used for meaning, never decoration */
  --ok:     #3a6b46;  --ok-bg:   #eef2ef;  --ok-line:   #cfdcd3;
  --warn:   #7c5a2f;  --warn-bg: #f7f2ea;  --warn-line: #e5d8c3;
  --err:    #9c3a34;  --err-bg:  #f7eded;  --err-line:  #e6cdcb;

  /* sidebar */
  --nav-bg:      #14161a;
  --nav-text:    #b6bac1;
  --nav-text-on: #ffffff;
  --nav-hover:   #22252b;
  --nav-line:    #282b32;
  --nav-w:       236px;
  --nav-w-min:   64px;

  /* type */
  --font-ui:      "Hanken Grotesk", -apple-system, "Segoe UI", Arial, sans-serif;
  --font-display: "Newsreader", Georgia, serif;
  --font-mono:    "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;

  /* spacing scale */
  --s1: 4px;  --s2: 8px;  --s3: 12px; --s4: 16px;
  --s5: 20px; --s6: 24px; --s7: 32px; --s8: 40px;

  /* shape + depth */
  --radius:    11px;
  --radius-sm: 8px;
  --radius-lg: 14px;
  --shadow:    0 1px 2px rgba(20,22,26,.05);
  --shadow-lg: 0 8px 32px rgba(20,22,26,.12);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--ink); }

:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
  border-radius: 6px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}

/* ---------------------------------------------------------------------
   2. App shell — sidebar + scrolling main column
   The shell is assembled by nav.js, so no page markup has to change.
   ------------------------------------------------------------------ */
/* nav.js sets .has-shell once the shell is mounted. Pages written before the
   sidebar existed still declare `body { padding: 22px 20px 80px }`, which
   would double up inside .app-main — this out-specifies that (0,1,1 beats
   0,0,1) without !important, and leaves shell-less pages like login alone. */
body.has-shell { margin: 0; padding: 0; }

.app-shell {
  display: flex;
  min-height: 100vh;
  align-items: stretch;
}

.app-main {
  flex: 1;
  min-width: 0;                 /* lets wide tables scroll instead of pushing */
  padding: var(--s6) var(--s7) 96px;
  margin-left: var(--nav-w);
  transition: margin-left .18s ease;
}
.app-shell.nav-collapsed .app-main { margin-left: var(--nav-w-min); }

/* Page content column. Wider than the old 1060px because a sidebar
   already eats the left gutter — otherwise dashboards feel cramped. */
.wrap,
.app-main > .wrap { max-width: 1180px; margin: 0 auto; }

/* ---------------------------------------------------------------------
   3. Sidebar
   ------------------------------------------------------------------ */
.app-nav {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: var(--nav-w);
  z-index: 80;
  display: flex;
  flex-direction: column;
  background: var(--nav-bg);
  font-family: var(--font-ui);
  transition: width .18s ease;
}
.app-shell.nav-collapsed .app-nav { width: var(--nav-w-min); }

.nav-head {
  display: flex;
  align-items: center;
  gap: var(--s2);
  padding: var(--s4) var(--s4) var(--s3);
  border-bottom: 1px solid var(--nav-line);
  min-height: 60px;
}
.nav-mark {
  display: grid;
  place-items: center;
  width: 28px; height: 28px;
  flex: none;
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 500;
}
.nav-wordmark {
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -.01em;
  color: #fff;
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
}

.nav-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--s3) var(--s2);
  scrollbar-width: thin;
  scrollbar-color: #383c44 transparent;
}
.nav-scroll::-webkit-scrollbar { width: 6px; }
.nav-scroll::-webkit-scrollbar-thumb { background: #383c44; border-radius: 3px; }

.nav-group-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: #6a6e77;
  padding: var(--s4) var(--s3) var(--s2);
  white-space: nowrap;
  overflow: hidden;
}
.nav-group-label:first-child { padding-top: var(--s1); }

.nav-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px var(--s3);
  margin-bottom: 1px;
  border-radius: var(--radius-sm);
  color: var(--nav-text);
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 500;
  line-height: 1.3;
  transition: background .12s, color .12s;
}
.nav-item:hover { background: var(--nav-hover); color: #fff; }
.nav-item.on {
  background: #fff;
  color: var(--ink);
  font-weight: 600;
}
.nav-item svg { flex: none; width: 16px; height: 16px; }
.nav-item .nav-label { flex: 1; min-width: 0; }

/* Count badge, e.g. open tickets */
.nav-count {
  flex: none;
  min-width: 19px;
  height: 19px;
  padding: 0 6px;
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 10.5px;
  font-weight: 500;
  display: grid;
  place-items: center;
}
.nav-item.on .nav-count { background: var(--ink); color: #fff; }

.nav-foot {
  border-top: 1px solid var(--nav-line);
  padding: var(--s3);
  display: flex;
  align-items: center;
  gap: var(--s2);
}
.nav-who {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-out {
  flex: none;
  display: grid;
  place-items: center;
  width: 32px; height: 32px;
  border-radius: var(--radius-sm);
  color: var(--nav-text);
  text-decoration: none;
}
.nav-out:hover { background: var(--nav-hover); color: #fff; }

.nav-toggle {
  display: grid;
  place-items: center;
  width: 32px; height: 32px;
  margin-left: auto;
  flex: none;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--nav-text);
  cursor: pointer;
}
.nav-toggle:hover { background: var(--nav-hover); color: #fff; }

/* --- collapsed state: icons only, labels revealed on hover --- */
.app-shell.nav-collapsed .nav-wordmark,
.app-shell.nav-collapsed .nav-group-label,
.app-shell.nav-collapsed .nav-label,
.app-shell.nav-collapsed .nav-who { display: none; }

.app-shell.nav-collapsed .nav-head { justify-content: center; padding-inline: 0; }
.app-shell.nav-collapsed .nav-toggle { margin: 0; position: absolute; top: 68px; right: 16px; }
.app-shell.nav-collapsed .nav-head .nav-toggle { display: none; }
.app-shell.nav-collapsed .nav-item { justify-content: center; padding-inline: 0; position: relative; }
.app-shell.nav-collapsed .nav-foot { justify-content: center; }
.app-shell.nav-collapsed .nav-group-label { padding: var(--s2) 0 0; }

.app-shell.nav-collapsed .nav-count {
  position: absolute;
  top: 3px; right: 8px;
  min-width: 15px; height: 15px;
  padding: 0 4px;
  font-size: 9.5px;
}

/* Tooltip for collapsed icons */
.app-shell.nav-collapsed .nav-item::after {
  content: attr(data-tip);
  position: absolute;
  left: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  font-size: 12.5px;
  font-weight: 500;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s;
  box-shadow: var(--shadow-lg);
  z-index: 5;
}
.app-shell.nav-collapsed .nav-item:hover::after { opacity: 1; }

/* --- mobile: sidebar becomes an off-canvas drawer --- */
.nav-topbar { display: none; }

@media (max-width: 900px) {
  .app-nav {
    transform: translateX(-102%);
    transition: transform .22s ease;
    box-shadow: var(--shadow-lg);
    width: min(280px, 84vw);
  }
  .app-nav.open { transform: translateX(0); }
  .app-main { margin-left: 0 !important; padding: var(--s3) var(--s4) 80px; }
  .app-shell.nav-collapsed .app-nav { width: min(280px, 84vw); }

  /* collapsed-mode rules must not apply on mobile */
  .app-shell.nav-collapsed .nav-wordmark,
  .app-shell.nav-collapsed .nav-group-label,
  .app-shell.nav-collapsed .nav-label,
  .app-shell.nav-collapsed .nav-who { display: revert; }
  .app-shell.nav-collapsed .nav-item { justify-content: flex-start; padding-inline: var(--s3); }
  .app-shell.nav-collapsed .nav-item::after { display: none; }
  .app-shell.nav-collapsed .nav-head { justify-content: flex-start; padding-inline: var(--s4); }
  .app-shell.nav-collapsed .nav-count { position: static; min-width: 19px; height: 19px; font-size: 10.5px; padding: 0 6px; }

  .nav-topbar {
    display: flex;
    align-items: center;
    gap: var(--s3);
    position: sticky;
    top: 0;
    z-index: 70;
    padding: 10px var(--s4);
    background: rgba(244,245,246,.92);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--line);
  }
  .nav-topbar .bar-mark {
    display: grid; place-items: center;
    width: 26px; height: 26px; flex: none;
    border-radius: 7px; background: var(--ink); color: #fff;
    font-family: var(--font-mono); font-size: 12px;
  }
  .nav-topbar .bar-page {
    flex: 1; min-width: 0;
    font-size: 14px; font-weight: 600; color: var(--ink);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .bar-burger {
    display: flex; flex-direction: column; justify-content: center; gap: 4.5px;
    width: 44px; height: 44px; padding: 0 11px; margin-right: -11px;
    flex: none; border: 0; background: none; cursor: pointer;
  }
  .bar-burger span { display: block; height: 2px; border-radius: 2px; background: var(--ink); }
  .bar-dot {
    min-width: 19px; height: 19px; padding: 0 6px; flex: none;
    border-radius: 10px; background: var(--ink); color: #fff;
    font-family: var(--font-mono); font-size: 10.5px;
    display: none; align-items: center; justify-content: center;
  }
  .nav-toggle { display: none; }
}

.nav-scrim {
  position: fixed;
  inset: 0;
  z-index: 79;
  background: rgba(20,22,26,.42);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
.nav-scrim.open { opacity: 1; pointer-events: auto; }

/* ---------------------------------------------------------------------
   4. Page header + section rhythm
   ------------------------------------------------------------------ */
.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--s4);
  flex-wrap: wrap;
  margin-bottom: var(--s6);
  padding-bottom: var(--s4);
  border-bottom: 1px solid var(--line);
}

h1 {
  font-family: var(--font-display);
  font-size: 27px;
  font-weight: 600;
  letter-spacing: -.02em;
  line-height: 1.15;
  margin: 0 0 var(--s1);
  color: var(--ink);
}
.sub { color: var(--muted); font-size: 13.5px; margin: 0; }

h2 {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
  margin: var(--s7) 0 var(--s3);
}
h2:first-child { margin-top: 0; }

/* Section header with an action on the right */
.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s3);
  margin: var(--s7) 0 var(--s3);
}
.section-head h2 { margin: 0; }
.section-head .link {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
}
.section-head .link:hover { color: var(--ink); text-decoration: underline; }

/* ---------------------------------------------------------------------
   5. Cards + stats
   ------------------------------------------------------------------ */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: var(--s3);
}
.cards.tight { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--s4);
  box-shadow: var(--shadow);
}

.label {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: var(--s2);
}

.stat {
  font-family: var(--font-mono);
  font-size: 28px;
  font-weight: 500;
  line-height: 1;
  letter-spacing: -.02em;
  color: var(--ink);
}
.stat.small { font-size: 17px; }
.stat.good  { color: var(--ok); }
.stat.bad   { color: var(--err); }
.stat.warn  { color: var(--warn); }

.card .dim,
.hint { font-size: 12px; color: var(--muted); margin-top: var(--s1); }

/* A card that is a link should not look like a link */
a.card-link { display: block; text-decoration: none; color: inherit; }

/* --- KPI card: severity is carried by a left rule, not by colour alone --- */
.kpi {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 2px solid var(--line-strong);
  border-radius: var(--radius);
  padding: var(--s4);
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: left;
  width: 100%;
  font: inherit;
  transition: border-color .12s, transform .12s, box-shadow .12s;
}
.kpi:hover { border-color: var(--line-strong); border-left-color: var(--ink); transform: translateY(-1px); box-shadow: 0 4px 14px rgba(20,22,26,.07); }
.kpi.sev-high { border-left-color: var(--err); }
.kpi.sev-med  { border-left-color: var(--warn); }
.kpi.sev-low  { border-left-color: var(--line-strong); }
.kpi.is-clear {
  border-left-color: var(--ok);
  cursor: default;
  opacity: .72;
}
.kpi.is-clear:hover { transform: none; box-shadow: var(--shadow); }
.kpi .n {
  font-family: var(--font-mono);
  font-size: 27px;
  font-weight: 500;
  line-height: 1;
  letter-spacing: -.02em;
}
.kpi.sev-high .n { color: var(--err); }
.kpi.sev-med  .n { color: var(--warn); }
.kpi.is-clear .n { color: var(--ok); }
.kpi .lab {
  font-size: 12.5px;
  color: var(--muted);
  margin-top: var(--s2);
  line-height: 1.35;
}
.kpi .tag {
  display: inline-block;
  margin-top: var(--s2);
  padding: 2px 7px;
  border-radius: 5px;
  background: var(--accent-dim);
  color: var(--ink-soft);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
}

/* ---------------------------------------------------------------------
   6. Buttons
   ------------------------------------------------------------------ */
button, .btn {
  font-family: var(--font-ui);
  font-size: 13.5px;
  font-weight: 600;
  padding: 9px 16px;
  border: 1px solid var(--ink);
  border-radius: var(--radius-sm);
  background: var(--ink);
  color: #fff;
  cursor: pointer;
  transition: opacity .12s, background .12s;
}
button:hover, .btn:hover { opacity: .88; }
button:disabled, .btn:disabled { opacity: .42; cursor: default; }

button.secondary, .btn.secondary {
  background: var(--card);
  color: var(--ink);
  border-color: var(--line-strong);
}
button.secondary:hover, .btn.secondary:hover { background: var(--accent-dim); opacity: 1; }

button.ghost {
  background: none;
  border-color: transparent;
  color: var(--muted);
}
button.ghost:hover { background: var(--accent-dim); color: var(--ink); opacity: 1; }

button.small, .btn.small { padding: 6px 11px; font-size: 12px; }

.toolbar {
  display: flex;
  align-items: center;
  gap: var(--s3);
  flex-wrap: wrap;
  margin-bottom: var(--s5);
}
.status { font-size: 12.5px; color: var(--muted); }

/* ---------------------------------------------------------------------
   7. Inputs
   ------------------------------------------------------------------ */
input[type=text], input[type=number], input[type=search],
input[type=password], select, textarea {
  font-family: var(--font-ui);
  font-size: 13.5px;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 8px 11px;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(20,22,26,.07);
}

/* ---------------------------------------------------------------------
   8. Tables
   ------------------------------------------------------------------ */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: 13px;
}
th {
  background: #1b1d22;
  color: #fff;
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  text-align: left;
  padding: 9px 12px;
}
td {
  padding: 9px 12px;
  text-align: left;
  border-top: 1px solid var(--line);
}
tbody tr:nth-child(even) { background: #fafafb; }
tbody tr:hover { background: var(--accent-dim); }
td.num, .t-ref, .t-age { font-family: var(--font-mono); }
.dim { color: var(--muted); }

.empty {
  color: var(--muted);
  font-size: 13.5px;
  padding: var(--s6);
  text-align: center;
}

/* ---------------------------------------------------------------------
   9. Alerts
   ------------------------------------------------------------------ */
.alert {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: 11px var(--s4);
  margin-bottom: var(--s2);
  border: 1px solid var(--line);
  border-left: 2px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--card);
  font-size: 13.5px;
  box-shadow: var(--shadow);
}
.alert.warn { border-left-color: var(--warn); }
.alert.bad  { border-left-color: var(--err); }
.alert svg  { flex: none; color: var(--muted); }
.alert .go {
  margin-left: auto;
  white-space: nowrap;
  font-weight: 600;
  font-size: 12.5px;
  color: var(--ink);
}

/* ---------------------------------------------------------------------
   10. Panels + chips
   ------------------------------------------------------------------ */
.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--s5);
  margin-bottom: var(--s4);
  box-shadow: var(--shadow);
  display: none;
}
.panel.open { display: block; }
.panel h2 { margin-top: 0; }

.bchip {
  display: inline-block;
  background: var(--accent-dim);
  color: var(--ink-soft);
  border-radius: 20px;
  padding: 3px 10px;
  font-size: 12px;
  font-weight: 600;
}
.bchip .n { font-family: var(--font-mono); color: var(--muted); }
.bybrand { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: var(--s3); }

/* ---------------------------------------------------------------------
   11. Utilities
   ------------------------------------------------------------------ */
.mono { font-family: var(--font-mono); }
.nowrap { white-space: nowrap; }
.actions { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; }
.spacer { flex: 1; }

@media (max-width: 620px) {
  h1 { font-size: 22px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(146px, 1fr)); gap: var(--s2); }
  .card, .kpi { padding: var(--s3); }
  .stat, .kpi .n { font-size: 22px; }
  .panel { padding: var(--s4); }
}
