:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #1b1f24; --muted: #646d78; --line: #e3e6ea;
  --accent: #2557d6; --accent-ink: #ffffff; --good: #137a3f; --bad: #c02626; --warn: #9a6200;
  --good-bg: #e6f4ec; --bad-bg: #fdecec; --warn-bg: #fff4dc; --info-bg: #e8eefc;
  --radius: 10px; --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  color-scheme: light;
}
/* Theme: follows the phone unless More → Display sets Light or Dark (html[data-theme]). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216; --panel: #171b21; --text: #e6e9ed; --muted: #98a1ad; --line: #2a3038;
    --accent: #6b93ff; --accent-ink: #0b0e12; --good: #4cc27c; --bad: #ff6b6b; --warn: #f0b34a;
    --good-bg: #13291d; --bad-bg: #331616; --warn-bg: #33270f; --info-bg: #182240;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #0f1216; --panel: #171b21; --text: #e6e9ed; --muted: #98a1ad; --line: #2a3038;
    --accent: #6b93ff; --accent-ink: #0b0e12; --good: #4cc27c; --bad: #ff6b6b; --warn: #f0b34a;
    --good-bg: #13291d; --bad-bg: #331616; --warn-bg: #33270f; --info-bg: #182240;
    --shadow: none; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); }
h1 { font-size: 1.35rem; margin: 0 0 1rem; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
h3 { font-size: .95rem; margin: 1rem 0 .5rem; }
p { margin: .25rem 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85rem; word-break: break-all; }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.live-banner { background: var(--bad); color: #fff; text-align: center; font-weight: 600; padding: .35rem 1rem;
  padding-top: calc(.35rem + env(safe-area-inset-top, 0px)); }
header.top { display: flex; align-items: center; gap: .75rem; padding: .75rem 1rem; background: var(--panel);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
header.top .brand { font-weight: 700; }
header.top .spacer { flex: 1; }
.layout { display: flex; min-height: calc(100vh - 56px); }
nav.side { width: 200px; padding: 1rem .5rem; border-right: 1px solid var(--line); background: var(--panel); }
nav.side a, nav.tabs a { display: block; padding: .55rem .8rem; border-radius: 8px; color: var(--text); text-decoration: none; }
nav.side a.active { background: var(--info-bg); color: var(--accent); font-weight: 600; }
main { flex: 1; padding: 1.25rem; max-width: 1100px; width: 100%; }
nav.tabs { display: none; }
@media (max-width: 760px) {
  nav.side { display: none; }
  main { padding: 1rem 1rem calc(80px + env(safe-area-inset-bottom, 0px)); }
  nav.tabs { display: flex; position: fixed; bottom: 0; left: 0; right: 0; background: var(--panel);
    border-top: 1px solid var(--line); padding: .25rem .25rem calc(.25rem + env(safe-area-inset-bottom, 0px)); z-index: 5; }
  nav.tabs a { flex: 1; text-align: center; font-size: .75rem; padding: .5rem .1rem; }
  nav.tabs a.active { color: var(--accent); font-weight: 700; }
}

/* ---------- components ---------- */
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem;
  box-shadow: var(--shadow); margin-bottom: 1rem; min-width: 0; }
.card.grid { margin-bottom: 1rem; }
.stat { font-size: 1.5rem; font-weight: 700; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: .25rem .75rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.stack > * + * { margin-top: .75rem; }

.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 700;
  letter-spacing: .02em; background: var(--info-bg); color: var(--accent); }
.badge.good { background: var(--good-bg); color: var(--good); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.muted { background: var(--line); color: var(--muted); }
.pos { color: var(--good); }
.neg { color: var(--bad); }

.notice { border-radius: 8px; padding: .6rem .8rem; margin: .5rem 0; background: var(--info-bg); }
.notice.good { background: var(--good-bg); }
.notice.bad { background: var(--bad-bg); }
.notice.warn { background: var(--warn-bg); }

/* ---------- forms ---------- */
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .25rem; }
input, select, textarea { width: 100%; padding: .55rem .65rem; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font: inherit; }
textarea { min-height: 7rem; font-family: ui-monospace, Menlo, monospace; font-size: .8rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field { margin-bottom: .75rem; }
.fields { display: grid; gap: 0 .75rem; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.hint { font-size: .8rem; color: var(--muted); margin-top: .2rem; }
.check { display: flex; gap: .5rem; align-items: center; color: var(--text); font-size: .95rem; }
.check input { width: auto; }

button { font: inherit; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--text);
  padding: .5rem .9rem; cursor: pointer; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { background: var(--bad); color: #fff; border-color: var(--bad); font-weight: 600; }
button.link { border: none; background: none; color: var(--accent); padding: .2rem .3rem; }
button:disabled { opacity: .55; cursor: default; }
button.small { padding: .25rem .6rem; font-size: .85rem; }

/* ---------- tables & lists ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: .8rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--bg); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip { display: inline-flex; align-items: center; gap: .3rem; background: var(--bg); border: 1px solid var(--line);
  border-radius: 999px; padding: .15rem .3rem .15rem .65rem; font-size: .85rem; }
.chip button { border: none; background: none; padding: 0 .3rem; color: var(--muted); }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { padding: .35rem 0; border-bottom: 1px solid var(--line); }
ul.plain li:last-child { border-bottom: none; }

.progress { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; margin-top: .4rem; }
.progress > div { height: 100%; width: 35%; background: var(--accent); animation: slide 1.4s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(300%); } }
@media (prefers-reduced-motion: reduce) { .progress > div { animation: none; width: 100%; opacity: .5; } }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { width: 100%; max-width: 380px; }

/* ---------- toast ---------- */
#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + env(safe-area-inset-bottom, 0px));
  z-index: 20; display: flex; flex-direction: column; gap: .4rem; width: min(92vw, 460px); }
#toast div { background: var(--text); color: var(--bg); padding: .6rem .9rem; border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.2); }
#toast div.bad { background: var(--bad); color: #fff; }
@media (min-width: 761px) { #toast { bottom: 1.5rem; } }

.persp { display: grid; grid-template-columns: 8rem 6rem 1fr; gap: .4rem .75rem; align-items: start; }
@media (max-width: 760px) { .persp { grid-template-columns: 1fr auto; } .persp .ev { grid-column: 1 / -1; margin-bottom: .5rem; } }

.profile-select { width: auto; max-width: 14rem; padding: .3rem .5rem; font-size: .85rem; }

/* ---------- v1.5: finance dashboard patterns ---------- */
html { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
select { background: var(--bg); color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.skip-link { position: absolute; left: -999px; top: 0; z-index: 30; background: var(--panel); padding: .5rem 1rem; border-radius: 8px; }
.skip-link:focus { left: 1rem; top: 1rem; }
button:hover, a:hover { filter: brightness(1.06); }

/* lineage strip: where the numbers come from, as of when */
.lineage { display: flex; flex-wrap: wrap; gap: .35rem 1rem; font-size: .8rem; color: var(--muted);
  border-bottom: 1px solid var(--line); padding-bottom: .5rem; margin-bottom: 1rem; }
.lineage strong { color: var(--text); font-weight: 600; }

/* KPI tiles: summary on top of the tables, each drills to its detail */
.kpis { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 1rem; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem .9rem;
  display: flex; flex-direction: column; gap: .2rem; min-width: 0; color: var(--text); text-decoration: none; }
a.kpi:hover { border-color: var(--accent); }
.kpi .label { font-size: .78rem; color: var(--muted); }
.kpi .value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: .78rem; color: var(--muted); overflow-wrap: anywhere; }
.kpi .drill { font-size: .78rem; color: var(--accent); margin-top: auto; }
.meter { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin: .25rem 0; }
.meter > span { display: block; height: 100%; background: var(--accent); }
.meter.good > span { background: var(--good); }
.meter.bad > span { background: var(--bad); }

/* finance tables: sticky header, zebra, tabular numerals */
.table-wrap { max-height: 70vh; overflow: auto; }
thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; box-shadow: 0 1px 0 var(--line); }
tbody tr:nth-child(even) { background: color-mix(in srgb, var(--bg) 55%, transparent); }
tbody tr:hover { background: var(--info-bg); }
td.num, th.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
td.code { font-family: ui-monospace, Menlo, monospace; font-size: .8rem; color: var(--muted); }
tfoot td { font-weight: 700; border-top: 2px solid var(--line); }
.table-tools { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; margin-bottom: .4rem; }

/* confirmation panel: stays until dismissed (what happened, when, links) */
.confirm { border: 1px solid var(--good); background: var(--good-bg); border-radius: var(--radius); padding: .8rem 1rem; margin-bottom: 1rem; }
.confirm.bad { border-color: var(--bad); background: var(--bad-bg); }
.confirm h2 { margin-bottom: .3rem; }
.confirm ul { margin: .3rem 0 .5rem 1.1rem; padding: 0; }
.chip.active { border-color: var(--accent); color: var(--accent); font-weight: 600; }
a.chip { text-decoration: none; color: var(--text); padding: .3rem .8rem; }
.spacer-sm { width: .75rem; }
.alert { margin-bottom: .75rem; }
.alert p { margin: .25rem 0; }
.alert code { display: block; white-space: pre-wrap; overflow-wrap: anywhere; margin-top: .3rem; }


/* ---------- v2: mobile-first shell ---------- */
:root[data-level="simple"] .adv-only { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.grow { flex: 1; min-width: 0; }
a.button { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; border: 1px solid var(--line); border-radius: 8px;
  padding: .45rem .8rem; color: var(--text); background: var(--panel); }
a.button.small { padding: .3rem .65rem; font-size: .85rem; }
.toast-x { opacity: .7; }
#toast > div { cursor: pointer; }

/* Simple / Advanced segmented switch */
.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; padding: 2px; background: var(--bg); }
.segmented .seg { border: 0; background: transparent; border-radius: 999px; padding: .3rem .75rem; font-size: .8rem;
  color: var(--muted); min-height: 32px; }
.segmented .seg.on { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

/* Navigation: icons + labels */
nav.side a { display: flex; align-items: center; gap: .6rem; }
nav.side svg { flex: none; opacity: .85; }
@media (max-width: 760px) {
  header.top { padding: .5rem .75rem; padding-top: calc(.5rem + env(safe-area-inset-top, 0px)); gap: .5rem; flex-wrap: wrap; }
  header.top .brand { font-size: .95rem; }
  header.top .profile-select { min-height: 36px; padding: .2rem .5rem; font-size: 14px; max-width: 46vw; }
  .desktop-only { display: none !important; }
  nav.tabs a { display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 52px;
    justify-content: center; font-size: .7rem; border-radius: 10px; }
  nav.tabs a.active { background: var(--info-bg); }
  /* phones: 16px inputs stop the browser zooming in; 44px tap targets */
  input, select, textarea { font-size: 16px; }
  button, .button, select { min-height: 44px; }
  .segmented .seg { min-height: 36px; }
  main { padding-left: .75rem; padding-right: .75rem; }
  .card { padding: .85rem; border-radius: 12px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; }
  .kpi .value { font-size: 1.15rem; }

  /* tables become labelled cards */
  .table-wrap { max-height: none; overflow: visible; }
  .table-wrap table, .table-wrap tbody, .table-wrap tr, .table-wrap td { display: block; width: 100%; }
  .table-wrap thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .table-wrap tr { border: 1px solid var(--line); border-radius: 10px; padding: .5rem .75rem; margin-bottom: .5rem; background: var(--panel); }
  .table-wrap tbody tr:nth-child(even) { background: var(--panel); }
  .table-wrap td { display: flex; justify-content: space-between; gap: 1rem; border: 0; padding: .2rem 0; text-align: right; }
  .table-wrap td::before { content: attr(data-label); color: var(--muted); font-size: .8rem; text-align: left; flex: none; max-width: 45%; }
  .table-wrap td:first-child { font-size: 1rem; }
  .table-wrap td:empty, .table-wrap td.adv-only:empty { display: none; }
}

/* Stocks tab */
.stock-list { list-style: none; margin: 0; padding: 0; }
.stock { padding: .7rem 0; border-bottom: 1px solid var(--line); display: grid; gap: .3rem; }
.stock:last-child { border-bottom: 0; }
.stock-head, .stock-foot { display: flex; justify-content: space-between; align-items: center; gap: .75rem; }
.stock .num { font-variant-numeric: tabular-nums; }
a.verdict { text-decoration: none; color: var(--text); }

/* More tab */
.more-links { display: grid; }
.more-link { display: flex; align-items: center; gap: .75rem; padding: .85rem .25rem; color: var(--text);
  text-decoration: none; border-bottom: 1px solid var(--line); min-height: 48px; }
.more-link:last-child { border-bottom: 0; }
.more-link .chev { margin-left: auto; color: var(--muted); font-size: 1.3rem; }

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; animation: none !important; } }
