/* memluv-admin tokens + base styles. DESIGN.md is authoritative — every value
   here traces to it. Strict CSP: this file is the ONLY stylesheet; no inline
   styles anywhere. */

@font-face {
  font-family: "IBM Plex Sans";
  src: url("/static/fonts/ibm-plex-sans.var.woff2") format("woff2");
  font-weight: 400 500;
  font-display: swap;
}

:root {
  --page: #f3f0ea;
  --raised: #faf8f3;
  --bar: #eee6d8;
  --hairline: #e0dcd5;
  --ink: #161412;
  --secondary: #5f5e5a;
  --muted: #8a857c;
  --gold: #c9a84c;        /* borders/fills only — never text */
  --gold-strong: #a07830; /* large text + icons only; the focus ring */
  --amber: #8a5a00;       /* warning TEXT — passes 4.5:1 on parchment */
  --alert: #c0392b;
  --success: #2e7d32;
  /* Semantic surfaces (M7): every component colour routes through these so
     the dark palette below can restyle the whole app in one block. */
  --field: #ffffff;          /* inputs, buttons, panels on raised */
  --danger-bg: #fdf3f1;  --danger-border: #e3b9b3;
  --ok-bg: #eef3ec;      --ok-border: #c8dcc9;
  --warn-bg: #f7f2e4;    --warn-border: #d8cba8;
  --accent-bg: #f0e8d0;  --accent-ink: #7a5c12;
  --neutral-bg: #f0ede8;
  --hairline-strong: #b3ada2;
  --heat-1: #f5efdc; --heat-2: #eee2bb; --heat-3: #e6d49a; --heat-4: #dcc578;
  color-scheme: light dark;
}

/* ── Dark palette (M7): follows the OS — the 2am phone view is dark. Same
   parchment warmth, inverted. Every var above MUST be overridden here (the
   test suite enforces parity so a new colour can't ship light-only). ── */
@media (prefers-color-scheme: dark) {
  :root {
    --page: #171512;
    --raised: #201d19;
    --bar: #26221c;
    --hairline: #37322b;
    --ink: #eae6df;
    --secondary: #b3aca1;
    --muted: #948d81;
    --gold: #a8894a;
    --gold-strong: #d0a95e;
    --amber: #e0a83f;
    --alert: #e07162;
    --success: #7dbb82;
    --field: #26221c;
    --danger-bg: #392420;  --danger-border: #5e372f;
    --ok-bg: #1f2b20;      --ok-border: #35503a;
    --warn-bg: #322a18;    --warn-border: #55482a;
    --accent-bg: #3a3016;  --accent-ink: #d9b661;
    --neutral-bg: #2a2620;
    --hairline-strong: #4a443b;
    --heat-1: #322b18; --heat-2: #453a1c; --heat-3: #5c4c22; --heat-4: #756025;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

:focus-visible { outline: 2px solid var(--gold-strong); outline-offset: 2px; }

h1 { font-size: 22px; font-weight: 500; margin: 24px 0 16px; }

.sr-only, .skip-link {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
.skip-link:focus {
  position: fixed; top: 8px; left: 8px; width: auto; height: auto;
  clip: auto; background: var(--raised); padding: 8px 16px; z-index: 10;
  border: 1px solid var(--hairline); border-radius: 6px;
}

/* ── Top bar ── */
.topbar {
  display: flex; align-items: center; gap: 24px;
  background: var(--bar); border-bottom: 1px solid var(--hairline);
  padding: 8px 24px; min-height: 48px;
}
.wordmark { font-family: Georgia, "Times New Roman", serif; font-size: 17px; }
.wordmark em { font-style: normal; color: var(--gold-strong); }
.wordmark-lg { font-size: 20px; text-align: center; display: block; margin: 0 0 8px; }

.topbar nav { display: flex; gap: 4px; }
.topbar nav a, .topbar .nav-soon {
  padding: 4px 12px; border-radius: 6px; font-size: 14px;
  text-decoration: none; color: var(--secondary);
}
.topbar nav a.active { background: var(--page); color: var(--ink); font-weight: 500; }
.topbar .nav-soon { color: var(--muted); cursor: default; }

.admin-chip {
  margin-left: auto; display: flex; align-items: center; gap: 8px;
  font-size: 14px; color: var(--secondary);
}
.role-badge {
  background: var(--accent-bg); color: var(--accent-ink); border-radius: 4px;
  padding: 1px 8px; font-size: 13px;
}

/* ── Layout ── */
main { max-width: 1080px; margin: 0 auto; padding: 8px 24px 48px; }

.flash { padding: 8px 16px; border-radius: 6px; font-size: 14px; margin: 16px 0 0; }
.flash-error { background: var(--danger-bg); border: 1px solid var(--danger-border); color: var(--alert); }
.flash-ok { background: var(--ok-bg); border: 1px solid var(--ok-border); color: var(--success); }

/* ── Health strip (Overview anchor: one row, never wraps above KPIs) ── */
.health-strip {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 8px;
}
.tile {
  background: var(--raised); border: 1px solid var(--hairline);
  border-radius: 6px; padding: 8px 12px; font-size: 13px;
}
.tile-label { display: block; color: var(--secondary); }
.tile-value { display: block; font-weight: 500; }
.tile-hint { display: block; color: var(--secondary); font-size: 13px; }
.tile .dot::before { content: "●"; margin-right: 6px; }
.tile-green .dot::before { color: var(--success); }
.tile-amber .dot::before { color: var(--amber); }
.tile-amber { border-color: var(--warn-border); }
.tile-amber .tile-value { color: var(--amber); }
.tile-red { background: var(--danger-bg); border-color: var(--danger-border); }
.tile-red .dot::before, .tile-red .tile-value { color: var(--alert); }
.tile-wide { grid-column: 1 / -1; }   /* alarm tiles get the full row */
.tile-muted .dot::before { color: var(--muted); }
.tile-muted .tile-value { color: var(--muted); font-weight: 400; }

/* ── KPI row ── */
/* auto-fit like the health strip: M1 grew the row to 5 cards (DAU/MAU/TTM/
   new/cost) and it must reflow, not squeeze, if another lands. */
.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 8px; margin-top: 8px; }
.kpi {
  background: var(--raised); border: 1px solid var(--hairline);
  border-radius: 6px; padding: 12px;
}
.kpi-label { display: block; font-size: 13px; color: var(--secondary); }
.kpi-value { display: block; font-size: 20px; font-weight: 500;
  font-variant-numeric: tabular-nums; }
.kpi-hint { display: block; font-size: 13px; color: var(--muted); }
.kpi-link { text-decoration: none; color: inherit; display: block; }
.kpi-link:hover { border-color: var(--gold); }
.kpi-muted { grid-column: span 3; }
.kpi-billing { background: var(--neutral-bg); border-style: dashed; }
.kpi-billing .kpi-value { color: var(--muted); }

.freshness { font-size: 13px; color: var(--muted); margin-top: 16px; }

/* ── Billing segments (M1): Overview strip pills + grid badges ── */
.seg-strip {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  margin-top: 8px; padding: 10px 12px;
  background: var(--raised); border: 1px solid var(--hairline); border-radius: 6px;
}
.seg-title { font-size: 13px; color: var(--secondary); margin-right: 4px; }
.seg-pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 13px; padding: 3px 10px; border-radius: 999px;
  border: 1px solid var(--hairline); background: var(--page);
  color: var(--secondary); text-decoration: none;
}
.seg-pill b { font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }
.seg-pill:hover { border-color: var(--gold); }
.seg-badge {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px;
  border: 1px solid var(--hairline); background: var(--page); color: var(--secondary);
  white-space: nowrap;
}
.seg-subscriber { border-color: var(--ok-border); background: var(--ok-bg); color: var(--success); }
.seg-trial      { border-color: var(--warn-border); background: var(--warn-bg); color: var(--amber); }
.seg-gift       { border-color: var(--warn-border); background: var(--warn-bg); color: var(--amber); }
.seg-ending     { border-color: var(--warn-border); background: var(--warn-bg); color: var(--amber); }
.seg-unknown_status { border-color: var(--danger-border); background: var(--danger-bg);
  color: var(--alert); font-weight: 500; }
.seg-past_due   { border-color: var(--danger-border); background: var(--danger-bg); color: var(--alert); }
.seg-trial_ended { border-color: var(--warn-border); background: var(--warn-bg); color: var(--amber); font-style: italic; }
.seg-lapsed     { color: var(--muted); }
.seg-abandoned  { color: var(--muted); border-style: dashed; }
.seg-none       { color: var(--muted); border-style: dashed; }

/* ── Metric drill-down charts (M2, inline SVG — no chart lib) ── */
.metric-chart {
  margin: 8px 0 0; padding: 12px;
  background: var(--raised); border: 1px solid var(--hairline); border-radius: 6px;
}
.metric-chart svg { width: 100%; height: auto; display: block; }
.chart-grid { stroke: var(--hairline); stroke-width: 1; }
.chart-tick { font-size: 11px; fill: var(--muted); font-family: inherit; }
.chart-bar { fill: var(--gold); }
.chart-bar:hover { fill: var(--gold-strong); }
.metric-table { margin-top: 12px; }
.metric-table summary { cursor: pointer; font-size: 14px; color: var(--secondary); }
.metric-table table { margin-top: 8px; max-width: 420px; }

/* ── Admin section (M6b) ── */
.credentials-once {
  border: 2px solid var(--gold-strong); border-radius: 8px;
  background: var(--warn-bg); padding: 16px; margin: 16px 0;
}
.cred-list dt { font-size: 13px; color: var(--secondary); margin-top: 8px; }
.cred-list dd { margin: 2px 0 0; }
.cred-list code {
  display: inline-block; margin: 2px 6px 2px 0; padding: 2px 8px;
  background: var(--field); border: 1px solid var(--hairline); border-radius: 4px;
  font-size: 14px; user-select: all;
}
.badge-you {
  font-size: 11px; padding: 1px 6px; margin-left: 6px; border-radius: 999px;
  background: var(--bar); color: var(--secondary);
}
.dsr-delete { border-color: var(--danger-border); background: var(--danger-bg); color: var(--alert); }
.dsr-export { border-color: var(--ok-border); background: var(--ok-bg); color: var(--success); }
.due-overdue { color: var(--alert); font-weight: 600; }
.due-soon { color: var(--amber); font-weight: 500; }
.spend-table { margin-top: 12px; max-width: 640px; }

/* ── Trends (M5): prior-year overlay + dashed projection ── */
.trend-section { margin-top: 24px; }
.trend-prior { stroke: var(--secondary); stroke-width: 1.5; opacity: 0.55; }
.trend-proj { stroke: var(--gold-strong); stroke-width: 1.5;
  stroke-dasharray: 5 4; }
.trend-legend { margin-top: 4px; }
.legend-bar, .legend-prior, .legend-proj {
  display: inline-block; vertical-align: middle; width: 18px; height: 3px;
}
.legend-bar { background: var(--gold); height: 9px; }
.legend-prior { background: var(--secondary); opacity: 0.55; }
.legend-proj {
  background: repeating-linear-gradient(90deg, var(--gold-strong) 0 5px,
    transparent 5px 9px);
}

/* ── Cohorts (M4): heatmap buckets — background carries intensity, the
   number carries the meaning (never colour alone) ── */
.cohort-table td.cohort-cell { font-variant-numeric: tabular-nums; }
.ch-0 { color: var(--muted); }
.ch-1 { background: var(--heat-1); }
.ch-2 { background: var(--heat-2); }
.ch-3 { background: var(--heat-3); }
.ch-4 { background: var(--heat-4); }
.cohort-future { background: var(--page); }

/* ── Financials (M3) ── */
.stat-neg { color: var(--alert); }
.dist-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px; margin: 8px 0 12px; }

/* ── Audit log (M6a) ── */
.audit-action { font-size: 13px; color: var(--secondary); }
.audit-meta code {
  font-size: 12px; color: var(--muted); background: var(--page);
  padding: 1px 5px; border-radius: 4px; word-break: break-all;
}
.nowrap { white-space: nowrap; }
.billing-line { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }

/* Axis filter rows under the preset chips */
.chips-axis { margin-top: -6px; }
.chips-axis-label {
  font-size: 13px; color: var(--muted); min-width: 56px;
  align-self: center;
}

/* ── Login ── */
.login-wrap { display: flex; justify-content: center; padding: 48px 16px; }
.login-card {
  background: var(--raised); border: 1px solid var(--hairline);
  border-radius: 8px; padding: 32px; width: 360px; max-width: 100%;
}
.stack { display: flex; flex-direction: column; gap: 8px; }
.stack label { font-size: 14px; color: var(--secondary); margin-top: 8px; }
.stack input {
  font: inherit; padding: 8px 12px; border: 1px solid var(--hairline);
  border-radius: 6px; background: var(--field); color: var(--ink); min-height: 44px;
}
.otp-input {
  font-size: 22px; letter-spacing: 0.5em; text-align: center;
  font-variant-numeric: tabular-nums;
}
button.primary {
  font: inherit; font-weight: 500; margin-top: 16px; min-height: 44px;
  background: var(--ink); color: var(--page);
  border: none; border-radius: 6px; padding: 8px 16px; cursor: pointer;
}
button.linklike {
  font: inherit; font-size: 14px; background: none; border: none;
  color: var(--gold-strong); cursor: pointer; padding: 4px 0;
  text-decoration: underline;
}
button.linklike:disabled { color: var(--muted); text-decoration: none; cursor: default; }
.inline-form { display: inline; }
/* Billing panel trial control: its own line under the comp toggle, reading
   "[Grant free trial] for [N months]" — the select matches the button's
   footprint (Tai layout spec 2026-07-21). */
.trial-form { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.trial-form select {
  font: inherit; min-height: 40px; padding: 8px 12px; width: 150px;
  border: 1px solid var(--hairline-strong); border-radius: 6px;
  background: var(--raised); cursor: pointer;
}
.hint { font-size: 14px; color: var(--secondary); }
.hint a { color: var(--gold-strong); }

.error-title { font-size: 18px; font-weight: 500; margin: 8px 0; }
.error-steps { font-size: 14px; color: var(--secondary); padding-left: 20px; }

/* ── KPI sparklines ── */
.kpi .spark { width: 100%; height: 24px; margin-top: 6px; display: block; }
.kpi-wide { grid-column: span 3; }

/* ── Users grid ── */
h2.section-h { font-size: 16px; font-weight: 500; margin: 24px 0 8px; }
.crumb { font-size: 14px; margin: 8px 0; }
.crumb a, .col-name a, .sort-link, .pager a, .empty-row a { color: var(--gold-strong); }

.grid-search { display: flex; gap: 8px; margin: 16px 0 8px; }
.grid-search input[type="search"] {
  flex: 1; max-width: 360px; font: inherit; min-height: 40px;
  padding: 8px 12px; border: 1px solid var(--hairline); border-radius: 6px;
  background: var(--field);
}
.grid-search button {
  font: inherit; min-height: 40px; padding: 8px 16px; cursor: pointer;
  background: var(--raised); border: 1px solid var(--hairline); border-radius: 6px;
}

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip {
  font-size: 13px; padding: 6px 12px; border-radius: 14px; min-height: 32px;
  border: 1px solid var(--hairline); color: var(--secondary);
  text-decoration: none; display: inline-flex; align-items: center;
}
.chip-on { background: var(--accent-bg); color: var(--accent-ink); border-color: var(--warn-border); font-weight: 500; }
.chip-clear { border-style: dashed; color: var(--muted); }

.grid-scroll { overflow-x: auto; }
table.grid {
  width: 100%; border-collapse: collapse; font-size: 13px;
  font-variant-numeric: tabular-nums;
}
table.grid th {
  text-align: left; font-weight: 500; color: var(--secondary);
  padding: 8px; border-bottom: 1px solid var(--hairline); white-space: nowrap;
}
table.grid td { padding: 8px; border-bottom: 1px solid var(--hairline); }
/* Users grid: rows are ALWAYS one line (Tai 2026-07-21 — wrapping made
   column widths jump between sorts). Long names ellipsize instead of
   wrapping (full name in the title tooltip); the grid-scroll wrapper
   absorbs any extra width, so the page never scrolls sideways. */
table.grid-oneline th, table.grid-oneline td { white-space: nowrap; }
table.grid-oneline td.col-name {
  max-width: 240px; overflow: hidden; text-overflow: ellipsis;
}
table.grid th.num, table.grid td.num { text-align: right; }
table.grid tbody tr:hover { background: var(--raised); cursor: pointer; }
table.grid .col-name { position: sticky; left: 0; background: var(--page); }
table.grid tbody tr:hover .col-name { background: var(--raised); }
.sort-link { text-decoration: none; }
.sort-active { color: var(--ink); }
.row-inactive td { color: var(--muted); }
.row-inactive .col-name a { color: var(--muted); }

.badge-inactive, .badge-ok, .la-badge {
  font-size: 12px; padding: 1px 8px; border-radius: 4px; margin-left: 6px;
  white-space: nowrap;
}
.badge-inactive { background: var(--neutral-bg); color: var(--muted); }
.badge-ok { background: var(--ok-bg); color: var(--success); }
.la-badge { margin-left: 0; }
.la-green { background: var(--ok-bg); color: var(--success); }
.la-amber { background: var(--warn-bg); color: var(--amber); }
.la-red { background: var(--danger-bg); color: var(--alert); }
.la-never { background: var(--neutral-bg); color: var(--muted); }

.grid-foot {
  display: flex; justify-content: space-between; align-items: center;
  flex-wrap: wrap; gap: 8px; margin-top: 12px;
}
.pager { font-size: 13px; color: var(--secondary); }
.pager a { text-decoration: none; padding: 2px 8px; }
.pager-off { color: var(--muted); padding: 2px 8px; }
.empty-row { font-size: 14px; color: var(--secondary); margin-top: 24px; }

/* ── User detail ── */
.identity-main { display: flex; align-items: center; gap: 12px; }
.identity-meta {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 8px 24px; margin: 12px 0 0;
}
.identity-meta div { margin: 0; }
.identity-meta dt { font-size: 13px; color: var(--secondary); }
.identity-meta dd { margin: 2px 0 0; font-size: 14px; }

.tabs { display: flex; gap: 4px; margin: 20px 0 16px;
  border-bottom: 1px solid var(--hairline); }
.tab { padding: 8px 14px; font-size: 14px; }
.tab-on { font-weight: 500; border-bottom: 2px solid var(--gold-strong); }
.tab-soon { color: var(--muted); cursor: default; }

.stat-row {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px; margin-bottom: 16px;
}
.stat { background: var(--raised); border: 1px solid var(--hairline);
  border-radius: 6px; padding: 10px 12px; }
.stat-label { display: block; font-size: 13px; color: var(--secondary); }
.stat-value { display: block; font-size: 18px; font-weight: 500;
  font-variant-numeric: tabular-nums; }

.danger-zone { margin-top: 32px; border-top: 1px solid var(--hairline);
  padding-top: 16px; }

/* ── Story + Media & People tabs (3b.2) ── */
.prose {
  white-space: pre-wrap; font-size: 16px; line-height: 1.6;
  max-width: 68ch; background: var(--raised); border: 1px solid var(--hairline);
  border-radius: 8px; padding: 16px 20px;
}
.small { font-size: 13px; }
.memory-list { list-style: none; padding: 0; margin: 8px 0 0; }
.memory-list li {
  display: flex; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--hairline);
  font-size: 14px;
}
.memory-date { color: var(--muted); flex: 0 0 84px; font-variant-numeric: tabular-nums; }
.memory-text { white-space: pre-wrap; }
section[aria-label] { margin-bottom: 8px; }

/* ── User ops (3c): demographics form + account status ── */
.ops-section { margin-top: 24px; }
.demo-form { display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.demo-field { display: flex; flex-direction: column; gap: 4px; }
.demo-field label { font-size: 13px; color: var(--secondary); }
.demo-field input {
  font: inherit; min-height: 40px; padding: 6px 10px; width: 140px;
  border: 1px solid var(--hairline); border-radius: 6px; background: var(--field);
}
.src-badge { font-size: 12px; padding: 1px 6px; border-radius: 4px; align-self: flex-start; }
.src-admin { background: var(--accent-bg); color: var(--accent-ink); }
.src-inferred { background: var(--neutral-bg); color: var(--muted); }
button.ops-btn {
  font: inherit; min-height: 40px; padding: 8px 16px; cursor: pointer;
  background: var(--raised); border: 1px solid var(--hairline-strong); border-radius: 6px;
}
button.danger-btn {
  font: inherit; min-height: 40px; padding: 8px 16px; cursor: pointer;
  background: var(--field); color: var(--alert); border: 1px solid var(--danger-border);
  border-radius: 6px;
}
button.ops-btn-secondary { background: var(--field); }
.demo-field select {
  font: inherit; min-height: 40px; padding: 6px 10px; width: 140px;
  border: 1px solid var(--hairline); border-radius: 6px; background: var(--field);
}
/* checkboxes must not inherit the 140px text-input sizing */
.demo-field input[type="checkbox"] { width: auto; min-height: auto; padding: 0; }
.consent-field { gap: 6px; }
.consent-field label {
  display: flex; align-items: center; gap: 6px; color: var(--secondary);
}
.consent-field input:not([type="checkbox"]) { width: 240px; }
.invite-panel {
  margin: 8px 0 16px; border: 1px solid var(--hairline); border-radius: 8px;
  background: var(--raised); padding: 4px 16px;
}
.invite-panel summary { cursor: pointer; padding: 12px 0; font-weight: 500; }
.invite-panel[open] summary {
  border-bottom: 1px solid var(--hairline); margin-bottom: 12px;
}
.invite-panel .demo-form { padding-bottom: 12px; }
.danger-delete {
  border: 1px solid var(--danger-border); border-radius: 8px; background: var(--danger-bg);
  padding: 16px; margin-top: 24px;
}
.delete-form {
  display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-top: 8px;
}
.delete-form label {
  display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--secondary);
}
.delete-form input {
  font: inherit; min-height: 40px; padding: 6px 10px; width: 200px;
  border: 1px solid var(--danger-border); border-radius: 6px; background: var(--field);
}

/* ── Responsive (DESIGN.md: the 2am phone view is the Overview) ── */
@media (max-width: 640px) {
  main { padding: 8px 16px 32px; }
  .topbar { padding: 8px 16px; gap: 12px; flex-wrap: wrap; }
  .health-strip, .kpi-row { grid-template-columns: 1fr; }
  .kpi-muted, .kpi-wide { grid-column: auto; }
  .login-wrap { padding: 16px; }
  .login-card { padding: 24px 16px; }
  button.linklike { min-height: 44px; }  /* touch target on phones */
  /* Grid keeps its columns but scrolls, name column pinned. */
  table.grid { font-size: 12px; }
  .grid-search input[type="search"] { max-width: none; }
  .stat-row { grid-template-columns: 1fr 1fr; }
}
