/* Liquid Glass theme, hand-written (no CSS framework). Layering follows
   Apple's HIG Materials guidance: glass (translucency + backdrop blur +
   luminosity adaptation) is reserved for the FUNCTIONAL layer — sidebar,
   topbar, segmented controls — while content cards stay near-opaque
   standard material so data keeps full legibility. Light/dark are both
   deliberate palettes, swapped via html[data-theme] (set by app.js). */

:root, [data-theme=light] {
  color-scheme: light;
  --bg-0: #edf1f7;
  --wash-a: rgba(77, 124, 254, 0.10);
  --wash-b: rgba(52, 211, 177, 0.10);
  --wash-c: rgba(139, 124, 246, 0.07);

  --glass: rgba(255, 255, 255, 0.55);
  --glass-border: rgba(255, 255, 255, 0.72);
  --glass-shadow: 0 8px 32px rgba(31, 45, 80, 0.10);

  --surface: rgba(255, 255, 255, 0.80);
  --surface-solid: #ffffff;
  --card-border: rgba(255, 255, 255, 0.70);
  --card-shadow: 0 2px 14px rgba(31, 45, 80, 0.06);
  --raised: #ffffff;
  --raised-shadow: 0 3px 10px rgba(31, 45, 80, 0.12);

  --ink: #1b2130;
  --ink-2: #4c566b;
  --muted: #8a93a6;
  --grid: rgba(27, 33, 48, 0.07);
  --baseline: rgba(27, 33, 48, 0.18);
  --hover: rgba(77, 124, 254, 0.05);
  --inset: rgba(27, 33, 48, 0.035);

  --accent: #4d7cfe;
  --mint: #34d3b1;
  --series-1: #4d7cfe;
  --series-2: #34d3b1;
  --series-3: #8b7cf6;
  --series-4: #f5b93c;
  --pos: #2ec9a6;
  --neg: #ef5b6a;
  --good: #2ec9a6;
  --warning: #f5b93c;
  --critical: #ef5b6a;
  --good-text: #0f9f7d;
  --crit-text: #d64450;
  --warn-text: #b07908;
  --bar-pos-from: #9db9ff;
  --bar-pos-to: #4d7cfe;
  --bar-cap: #34d3b1;
  --bar-neg-from: #f9aab2;
  --bar-neg-to: #ef5b6a;
}
[data-theme=dark] {
  color-scheme: dark;
  --bg-0: #0f1218;
  --wash-a: rgba(77, 124, 254, 0.10);
  --wash-b: rgba(52, 211, 177, 0.07);
  --wash-c: rgba(139, 124, 246, 0.08);

  --glass: rgba(22, 26, 35, 0.55);
  --glass-border: rgba(255, 255, 255, 0.09);
  --glass-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);

  --surface: rgba(29, 34, 45, 0.82);
  --surface-solid: #1d222d;
  --card-border: rgba(255, 255, 255, 0.07);
  --card-shadow: 0 2px 14px rgba(0, 0, 0, 0.25);
  --raised: #262c3a;
  --raised-shadow: 0 3px 10px rgba(0, 0, 0, 0.40);

  --ink: #f2f5fa;
  --ink-2: #b6bfd0;
  --muted: #7d8798;
  --grid: rgba(255, 255, 255, 0.07);
  --baseline: rgba(255, 255, 255, 0.18);
  --hover: rgba(93, 139, 255, 0.08);
  --inset: rgba(255, 255, 255, 0.04);

  --accent: #5d8bff;
  --mint: #3adfbd;
  --series-1: #5d8bff;
  --series-2: #3adfbd;
  --series-3: #9d8ffb;
  --series-4: #f6c353;
  --pos: #3adfbd;
  --neg: #f0707d;
  --good: #3adfbd;
  --warning: #f6c353;
  --critical: #f0707d;
  --good-text: #3adfbd;
  --crit-text: #f0707d;
  --warn-text: #f6c353;
  --bar-pos-from: #33508f;
  --bar-pos-to: #5d8bff;
  --bar-cap: #3adfbd;
  --bar-neg-from: #8f4a53;
  --bar-neg-to: #f0707d;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  color: var(--ink);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", sans-serif;
  background:
    radial-gradient(900px 500px at 12% -6%, var(--wash-a), transparent 60%),
    radial-gradient(800px 520px at 88% 4%, var(--wash-b), transparent 60%),
    radial-gradient(700px 600px at 55% 110%, var(--wash-c), transparent 60%),
    var(--bg-0);
  background-attachment: fixed;
}

.glass {
  background: var(--glass);
  -webkit-backdrop-filter: blur(22px) saturate(1.7);
  backdrop-filter: blur(22px) saturate(1.7);
  border: 1px solid var(--glass-border);
  box-shadow: var(--glass-shadow);
}
/* HIG "reduce transparency" spirit: no blur support -> solid fallback */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .glass { background: var(--surface-solid); }
}

/* ---------------------------------------------------------------- shell */

.app-shell { display: flex; min-height: 100vh; padding: 16px; gap: 16px; }

.sidebar {
  width: 224px; flex: none;
  display: flex; flex-direction: column;
  border-radius: 24px;
  padding: 18px 12px 12px;
  position: sticky; top: 16px;
  height: calc(100vh - 32px); overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 2px 8px 16px; }
.brand-mark {
  width: 36px; height: 36px; flex: none;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 15px; color: #fff; border-radius: 12px;
  background: linear-gradient(135deg, #4d7cfe, #34d3b1);
  box-shadow: 0 3px 10px rgba(77, 124, 254, 0.35);
}
.brand-name { font-size: 15px; font-weight: 700; }

.side-nav { display: flex; flex-direction: column; gap: 4px; }
.side-nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: 14px;
  font-size: 13.5px; color: var(--ink-2); text-decoration: none;
  border: 1px solid transparent;
}
.side-nav a:hover { background: var(--hover); }
.side-nav a.active {
  background: var(--raised); color: var(--ink); font-weight: 600;
  border-color: var(--card-border);
  box-shadow: var(--raised-shadow);
}
/* gradient icon tiles — the nav's one vibrant accent per HIG color advice */
.nav-ico {
  width: 30px; height: 30px; flex: none; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center;
  color: #fff;
}
.nav-ico svg { width: 15px; height: 15px; }
.ico-1 { background: linear-gradient(135deg, #5d8bff, #7fb5ff); }
.ico-2 { background: linear-gradient(135deg, #2ec9a6, #63e0c3); }
.ico-3 { background: linear-gradient(135deg, #8b7cf6, #b0a5fb); }
.ico-4 { background: linear-gradient(135deg, #f5b93c, #fad277); }
.ico-5 { background: linear-gradient(135deg, #8a93a6, #b4bcca); }

.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 4px; }
.side-action {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 14px; border: none;
  background: none; color: var(--ink-2); font: inherit; font-size: 13px;
  cursor: pointer; text-align: left;
}
.side-action:hover { background: var(--hover); }
.side-action svg { width: 15px; height: 15px; flex: none; opacity: 0.8; }
.db-hint {
  padding: 10px 10px 2px; font-size: 10.5px; color: var(--muted);
  overflow-wrap: anywhere; border-top: 1px solid var(--grid);
}

.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 16px; }

.topbar {
  position: sticky; top: 16px; z-index: 5;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  border-radius: 20px;
  padding: 12px 20px;
}
.page-title { font-size: 19px; font-weight: 700; margin: 0; line-height: 1.3; }

.liveness {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 500; color: var(--ink-2);
  background: var(--inset); border-radius: 999px; padding: 4px 12px;
}
.liveness .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.liveness.live { color: var(--good-text); background: rgba(46, 201, 166, 0.12); }
.liveness.live .dot { background: var(--good); }

.topbar-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

/* segmented pill control */
.seg {
  display: inline-flex; gap: 2px; padding: 3px;
  background: var(--inset); border-radius: 12px;
}
.seg button {
  border: none; background: none; color: var(--ink-2);
  font: inherit; font-size: 12.5px; padding: 4px 12px;
  border-radius: 9px; cursor: pointer;
}
.seg button:hover { color: var(--ink); }
.seg button.on {
  background: var(--raised); color: var(--ink); font-weight: 600;
  box-shadow: var(--raised-shadow);
}

select.ctl {
  background: var(--inset); color: var(--ink);
  border: none; border-radius: 12px; padding: 6px 10px;
  font: inherit; font-size: 12.5px;
}
button.ctl {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--inset); color: var(--ink-2);
  border: none; border-radius: 12px; padding: 6px 12px;
  font: inherit; font-size: 12.5px; cursor: pointer;
}
button.ctl:hover { color: var(--ink); }
button.ctl svg { width: 13px; height: 13px; }
button.ctl.primary {
  background: linear-gradient(135deg, #4d7cfe, #6f9bff); color: #fff;
  box-shadow: 0 3px 10px rgba(77, 124, 254, 0.30);
}

.content { display: flex; flex-direction: column; min-width: 0; }

.error-banner {
  background: rgba(239, 91, 106, 0.12); border: 1px solid rgba(239, 91, 106, 0.45);
  color: var(--ink); border-radius: 16px; padding: 10px 16px;
  margin-bottom: 16px; font-size: 13px;
}

.section-gap { height: 16px; }

/* table pager (detail page) */
.pager {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 12px 18px; border-top: 1px solid var(--grid);
  font-size: 12.5px; color: var(--ink-2);
}
.pager-size { display: inline-flex; align-items: center; gap: 6px; }
.pager-pages { display: inline-flex; gap: 4px; align-items: center; }
.pg-btn {
  min-width: 30px; height: 30px; padding: 0 8px;
  border: 1px solid transparent; border-radius: 10px;
  background: var(--inset); color: var(--ink-2);
  font: inherit; font-size: 12.5px; cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.pg-btn:hover:not(:disabled) { color: var(--ink); }
.pg-btn:disabled { opacity: 0.4; cursor: default; }
.pg-btn.on {
  background: var(--raised); color: var(--accent); font-weight: 650;
  border-color: var(--accent);
  box-shadow: var(--raised-shadow);
}
.pg-gap { padding: 0 2px; color: var(--muted); }

@media (max-width: 900px) {
  .app-shell { flex-direction: column; padding: 10px; gap: 10px; }
  .sidebar {
    width: auto; height: auto; position: static;
    flex-direction: row; align-items: center; gap: 6px;
    padding: 10px 12px; border-radius: 18px;
  }
  .brand { padding: 0 6px 0 0; }
  .side-nav { flex-direction: row; overflow-x: auto; }
  .side-nav a { white-space: nowrap; padding: 6px 10px; }
  .sidebar-foot { display: none; }
  .topbar { top: 10px; }
}

/* ---------------------------------------------------------------- cards */

.cards { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(148px, 1fr)); }
/* cards keep their natural height — a short chart beside a tall table ends
   where its content ends instead of stretching around dead space */
.two-col { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); align-items: start; }
.card.wide { grid-column: 1 / -1; }

.card {
  background: var(--surface);
  border: 1px solid var(--card-border);
  border-radius: 20px;
  box-shadow: var(--card-shadow);
}
.card-head { padding: 14px 18px 0; }
.card-head .t { font-size: 14px; font-weight: 650; }
.card-head .s { font-size: 12px; color: var(--muted); margin-top: 1px; }
.card-body { padding: 12px 18px 16px; }
.card-body.flush { padding: 6px 0 0; }
.card > .card-body:first-child { padding-top: 16px; }
.sub { color: var(--muted); font-size: 12px; }

/* stat tile */
.tile .label { font-size: 12px; color: var(--ink-2); }
.tile .value {
  font-size: 24px; font-weight: 700; margin-top: 2px;
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.tile .delta { font-size: 12px; margin-top: 2px; color: var(--muted); }
.tile .value.pos { color: var(--good-text); }
.tile .value.neg { color: var(--crit-text); }

/* ring outcome census */
/* ---------------------------------------------------------------- tables */

table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--grid); }
th { color: var(--muted); font-weight: 500; font-size: 12px; white-space: nowrap; }
tbody:last-child tr:last-child > * { border-bottom: 0; }
/* flush cards run the table edge-to-edge; give edge cells breathing room */
.card-body.flush th:first-child, .card-body.flush td:first-child { padding-left: 18px; }
.card-body.flush th:last-child, .card-body.flush td:last-child { padding-right: 18px; }
.table-scroll { overflow-x: auto; }

td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
/* wrap ONLY where long pathy text lives — a blanket break-anywhere shreds
   narrow columns (timestamps, badges) into vertical slivers */
td.wrap { overflow-wrap: anywhere; }
td.nowrap { white-space: nowrap; }
td.err { color: var(--neg); }
td.err[title] { cursor: help; text-decoration: underline dotted; }

tr.expandable { cursor: pointer; }
tr.expandable:hover td { background: var(--hover); }
tr.expansion > td { background: var(--inset); padding: 12px 16px; }

.trade-link { color: var(--accent); cursor: pointer; }
.trade-link:hover { text-decoration: underline; }

td .pnl-pos { color: var(--good-text); }
td .pnl-neg { color: var(--crit-text); }
td.side-buy { color: var(--good-text); }
td.side-sell { color: var(--crit-text); }

/* status pill: soft tint + leading dot, variant by raw result string */
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 500; white-space: nowrap;
  padding: 2px 10px; border-radius: 999px;
  background: var(--inset); color: var(--ink-2);
}
.pill::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%;
  background: currentColor; opacity: 0.8;
}
.pill.complete, .pill.applied, .pill.effective {
  background: rgba(46, 201, 166, 0.14); color: var(--good-text);
}
.pill.residual { background: rgba(245, 185, 60, 0.16); color: var(--warn-text); }
.pill.failed, .pill.rejected { background: rgba(239, 91, 106, 0.13); color: var(--crit-text); }

/* config tree */
.cfg-table td:first-child { color: var(--ink-2); width: 40%; }
.cfg-table td:last-child { overflow-wrap: anywhere; }
.cfg-section { margin: 16px 0 6px; font-weight: 650; font-size: 13px; }

/* ---------------------------------------------------------------- charts */

.chart { position: relative; }
.chart svg { display: block; width: 100%; }
.no-data { color: var(--muted); font-size: 13px; padding: 24px 0; text-align: center; }
.legend {
  display: flex; gap: 14px; flex-wrap: wrap;
  font-size: 12px; color: var(--ink-2); margin: 2px 0 8px;
}
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.legend .swatch { width: 10px; height: 10px; border-radius: 3px; }
.legend .line-key { width: 14px; height: 2px; border-radius: 1px; }

#tooltip {
  position: fixed; z-index: 10; pointer-events: none; display: none;
  border-radius: 14px; padding: 8px 12px; font-size: 12px;
  max-width: 280px;
}
#tooltip .t-title { color: var(--muted); margin-bottom: 4px; }
#tooltip .t-row { display: flex; align-items: center; gap: 6px; }
#tooltip .t-key { width: 12px; height: 2px; border-radius: 1px; flex: none; }
#tooltip .t-val { font-weight: 650; font-variant-numeric: tabular-nums; }
#tooltip .t-name { color: var(--ink-2); }
