:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --grid: #e5e9f0;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --accent-soft: #bfdbfe;
  --accent-contrast: #ffffff;
  --danger: #dc2626;
  --over: #f97316;
  --ok-bg: #ecfdf5; --ok-fg: #065f46;
  --warn-bg: #fff7ed; --warn-fg: #9a3412;
  --info-bg: #eff6ff; --info-fg: #1e3a8a;
  --bad-bg: #fef2f2; --bad-fg: #991b1b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120; --surface: #111827; --surface-2: #0f172a; --text: #e5e7eb; --muted: #94a3b8;
    --border: #1f2937; --grid: #1f2a3a; --accent: #60a5fa; --accent-hover: #93c5fd; --accent-soft: #1e3a5f;
    --accent-contrast: #0b1120; --danger: #f87171; --over: #fb923c;
    --ok-bg: #052e22; --ok-fg: #6ee7b7; --warn-bg: #2b1606; --warn-fg: #fdba74;
    --info-bg: #0c1a3a; --info-fg: #93c5fd; --bad-bg: #2a0b0b; --bad-fg: #fca5a5;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120; --surface: #111827; --surface-2: #0f172a; --text: #e5e7eb; --muted: #94a3b8;
  --border: #1f2937; --grid: #1f2a3a; --accent: #60a5fa; --accent-hover: #93c5fd; --accent-soft: #1e3a5f;
  --accent-contrast: #0b1120; --danger: #f87171; --over: #fb923c;
  --ok-bg: #052e22; --ok-fg: #6ee7b7; --warn-bg: #2b1606; --warn-fg: #fdba74;
  --info-bg: #0c1a3a; --info-fg: #93c5fd; --bad-bg: #2a0b0b; --bad-fg: #fca5a5;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 Inter, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  -webkit-font-smoothing: antialiased; min-height: 100vh; display: flex; flex-direction: column;
}
body.is-loading main { visibility: hidden; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.25; }
h1 { font-size: clamp(1.6rem, 3vw, 2.2rem); }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

/* ---------- header ---------- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { max-width: 1240px; margin: 0 auto; padding: .75rem 16px; display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.brand { display: flex; align-items: center; gap: .65rem; }
.brand-mark { width: 34px; height: 34px; padding: 6px; border-radius: 10px; background: var(--accent); fill: var(--accent-contrast); }
.brand-name { font-weight: 700; }
.brand-sub { font-size: .8rem; color: var(--muted); }
.user-box { display: flex; align-items: center; gap: .75rem; }
.user-meta { display: flex; flex-direction: column; align-items: flex-end; font-size: .9rem; line-height: 1.2; }
.user-meta .muted { font-size: .8rem; }

/* ---------- layout ---------- */
.container { max-width: 1240px; width: 100%; margin: 0 auto; padding: 24px 16px 40px; flex: 1; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 16px; min-width: 0; }
.app { display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 16px; align-items: start; }
.form-card { position: sticky; top: 84px; }
.results-col { min-width: 0; }
@media (max-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .form-card { position: static; }
}

/* ---------- auth ---------- */
.auth { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 420px); gap: 32px; align-items: center; padding-top: 3vh; }
.auth-intro p { color: var(--muted); font-size: 1.05rem; }
.feature-list { list-style: none; padding: 0; margin: 1rem 0 0; display: grid; gap: .5rem; }
.feature-list li { padding-left: 1.6rem; position: relative; }
.feature-list li::before { content: "✓"; position: absolute; left: 0; color: var(--accent); font-weight: 700; }
@media (max-width: 860px) { .auth { grid-template-columns: minmax(0, 1fr); } }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 4px; margin-bottom: 1rem; }
.tab { border: 0; background: transparent; color: var(--muted); padding: .55rem; border-radius: 7px; font: inherit; font-weight: 600; cursor: pointer; }
.tab[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- forms ---------- */
.form { display: grid; gap: .8rem; }
label { display: grid; gap: .3rem; font-size: .875rem; font-weight: 600; color: var(--text); }
input, select {
  font: inherit; font-weight: 400; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 9px; padding: .6rem .7rem; width: 100%; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.row-btns { display: flex; gap: .5rem; flex-wrap: wrap; }
.hint { font-size: .8rem; color: var(--muted); margin: -.45rem 0 0; }
.error { color: var(--bad-fg); background: var(--bad-bg); border-radius: 9px; padding: .55rem .75rem; font-size: .9rem; margin: 0; }
.btn { font: inherit; font-weight: 600; border-radius: 9px; padding: .65rem 1rem; cursor: pointer; border: 1px solid transparent; transition: background .15s; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn-sm { padding: .35rem .7rem; font-size: .85rem; }
.btn.is-busy { position: relative; color: transparent; }
.btn.is-busy::after { content: ""; position: absolute; inset: 0; margin: auto; width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--accent-contrast); border-right-color: transparent; animation: spin .7s linear infinite; }
.btn-ghost.is-busy::after { border-color: var(--text); border-right-color: transparent; }
@keyframes spin { to { transform: rotate(360deg); } }
.formula { margin-top: 1.1rem; padding-top: 1rem; border-top: 1px dashed var(--border); display: grid; gap: .35rem; }
.formula-title { font-size: .8rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.formula code { font-size: .78rem; background: var(--surface-2); border-radius: 6px; padding: .35rem .5rem; overflow-wrap: anywhere; }

/* ---------- results ---------- */
.empty { text-align: center; color: var(--muted); padding: 48px 20px; }
.res-head { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin: .5rem 0 1rem; }
.kpi { background: var(--surface-2); border: 1px solid var(--border); border-radius: 11px; padding: .7rem .8rem; min-width: 0; }
.kpi-label { font-size: .75rem; color: var(--muted); line-height: 1.25; min-height: 1.9em; }
.kpi-value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kpi-main { background: var(--accent); border-color: var(--accent); }
.kpi-main .kpi-label, .kpi-main .kpi-value { color: var(--accent-contrast); }
.note { border-radius: 10px; padding: .7rem .9rem; margin: 0; font-size: .92rem; }
.note-ok { background: var(--ok-bg); color: var(--ok-fg); }
.note-warn { background: var(--warn-bg); color: var(--warn-fg); }
.note-info { background: var(--info-bg); color: var(--info-fg); }
.note-bad { background: var(--bad-bg); color: var(--bad-fg); }
.chart-box { position: relative; height: 320px; width: 100%; }
@media (max-width: 600px) { .chart-box { height: 260px; } .row-2 { grid-template-columns: 1fr; } }
.details { margin-top: 1rem; }
.details summary { cursor: pointer; font-weight: 600; color: var(--accent); }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .875rem; margin-top: .5rem; font-variant-numeric: tabular-nums; }
.table th, .table td { text-align: left; padding: .45rem .6rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.table tr.is-median td { font-weight: 700; background: var(--info-bg); }
.table-hover tbody tr { cursor: pointer; }
.table-hover tbody tr:hover, .table-hover tbody tr:focus { background: var(--surface-2); outline: none; }

.footer { text-align: center; color: var(--muted); font-size: .8rem; padding: 16px; border-top: 1px solid var(--border); }
@media (max-width: 600px) {
  .brand-sub, .user-meta .muted { display: none; }
  .kpi-value { font-size: 1.2rem; }
}
