/* ==========================================================================
   House Sale Decision Simulator — /housesale
   Design tokens follow the validated house dataviz palette (light + dark).
   Strategy series colors are fixed by entity, never re-assigned:
     1 keepForever   blue     2 sellNow       aqua
     3 wallThenSell  yellow   4 rentThenSell  violet
   ========================================================================== */

:root {
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid-line: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, 0.10);
  --good: #006300;
  --bad: #d03b3b;
  --series-1: #2a78d6; /* blue    — keep & rent forever */
  --series-2: #1baf7a; /* aqua    — sell now */
  --series-3: #eda100; /* yellow  — build wall, then sell */
  --series-4: #4a3aa7; /* violet  — rent N years, then sell */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 10px -2px rgba(0, 0, 0, 0.07);
  --radius: 10px;
  --font-sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
}

[data-theme="dark"] {
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --surface-2: #242423;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid-line: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, 0.16);
  --good: #0ca30c;
  --bad: #e66767;
  --series-1: #3987e5;
  --series-2: #199e70;
  --series-3: #c98500;
  --series-4: #9085e9;
  --shadow-sm: none;
  --shadow-md: none;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { color-scheme: light; }
html[data-theme="dark"] { color-scheme: dark; }

body {
  background: var(--page);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ------------------------------------------------------------- header */

.app-header {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  align-items: center;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
}

.app-title h1 { font-size: 20px; font-weight: 700; letter-spacing: -0.01em; }
.app-title p { font-size: 13px; color: var(--text-muted); }

.header-actions {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.scenario-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

select.scenario-select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-width: 160px;
  cursor: pointer;
}

.btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  transition: background 0.15s ease;
}
.btn:hover { background: var(--accent-soft); }
.btn.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.btn-primary:hover { filter: brightness(1.08); }
.btn.icon { padding: 7px 9px; }

/* ------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 0;
  align-items: start;
}

.main { padding: 20px 24px 60px; min-width: 0; }

section.panel { margin-bottom: 28px; }

.section-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.section-head h2 { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; }
.section-head .hint { font-size: 12.5px; color: var(--text-muted); }

/* --------------------------------------------------------- assumptions */

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface-1);
  padding: 16px;
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
}

.sidebar h2 { font-size: 14px; font-weight: 700; margin: 4px 4px 10px; }
.sidebar .sidebar-note { font-size: 12px; color: var(--text-muted); margin: 0 4px 12px; }

details.group {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-1);
  margin-bottom: 8px;
  overflow: hidden;
}
details.group > summary {
  cursor: pointer;
  list-style: none;
  padding: 10px 14px;
  font-weight: 600;
  font-size: 13.5px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  user-select: none;
}
details.group > summary::-webkit-details-marker { display: none; }
details.group > summary::after { content: "+"; color: var(--text-muted); font-weight: 500; }
details.group[open] > summary::after { content: "–"; }
details.group[open] > summary { border-bottom: 1px solid var(--border); }

.fields { padding: 10px 14px 14px; display: grid; gap: 10px; }

.field { display: grid; gap: 4px; }
.field label { font-size: 12.5px; color: var(--text-secondary); font-weight: 500; }
.field .control { display: flex; align-items: center; gap: 8px; }
.field input[type="number"] {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  font-variant-numeric: tabular-nums;
}
.field input[type="number"]:focus,
select:focus, .btn:focus-visible, input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.field .unit { font-size: 12px; color: var(--text-muted); min-width: 34px; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.field .help { font-size: 11.5px; color: var(--text-muted); }

.field.toggle { grid-template-columns: 1fr auto; align-items: center; }
.switch { position: relative; width: 38px; height: 22px; flex: none; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.switch .track {
  position: absolute; inset: 0; border-radius: 11px;
  background: var(--baseline); transition: background 0.15s ease;
  /* Let clicks fall through to the checkbox underneath. */
  pointer-events: none;
}
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); }

/* --------------------------------------------------- assumptions strip */

.assumption-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.assumption-chips .chip {
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  white-space: nowrap;
}
.assumption-chips .chip strong { color: var(--text-primary); font-weight: 600; }

/* ---------------------------------------------------------------- KPIs */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
}

.kpi {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}
.kpi .kpi-label { font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; }
.kpi .kpi-value { font-size: 24px; font-weight: 650; letter-spacing: -0.02em; line-height: 1.2; }
.kpi .kpi-sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; display: flex; align-items: center; gap: 6px; }
.kpi .kpi-sub .delta-up { color: var(--good); font-weight: 600; }
.kpi .kpi-sub .delta-down { color: var(--bad); font-weight: 600; }
.kpi .series-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; display: inline-block; }
.kpi.kpi-hero .kpi-value { font-size: 34px; }

/* ------------------------------------------------------ comparison table */

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-1); }
table.compare { border-collapse: collapse; width: 100%; font-size: 13px; min-width: 640px; }
table.compare th, table.compare td { padding: 9px 12px; text-align: right; white-space: nowrap; }
table.compare th { font-weight: 600; color: var(--text-secondary); border-bottom: 1px solid var(--border); }
table.compare td { font-variant-numeric: tabular-nums; border-bottom: 1px solid var(--grid-line); }
table.compare tr:last-child td { border-bottom: none; }
table.compare th:first-child, table.compare td:first-child { text-align: left; }
table.compare td.best { color: var(--good); font-weight: 650; }
.strat-cell { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text-primary); }
.strat-cell .series-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }

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

.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  gap: 14px;
}

.chart-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 8px;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}
.chart-card h3 { font-size: 13.5px; font-weight: 650; margin-bottom: 2px; }
.chart-card .sub { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.chart-card.wide { grid-column: 1 / -1; }

.chart-legend { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 0 4px; }
.legend-chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: none; border: 1px solid var(--border); border-radius: 999px;
  padding: 3px 10px; font-size: 12px; color: var(--text-secondary);
  cursor: pointer; opacity: 0.45; transition: opacity 0.15s ease;
}
.legend-chip.is-on { opacity: 1; }
.legend-chip .legend-key { width: 14px; height: 3px; border-radius: 2px; }

.chart-plot { position: relative; }
.chart-plot:focus-visible { outline: 2px solid var(--accent); border-radius: 6px; }
.chart-plot svg { display: block; width: 100%; height: auto; }

.grid-line { stroke: var(--grid-line); stroke-width: 1; }
.grid-zero { stroke: var(--baseline); stroke-width: 1; }
.tick-label { fill: var(--text-muted); font-size: 11px; font-family: var(--font-sans); font-variant-numeric: tabular-nums; }
.crosshair { stroke: var(--baseline); stroke-width: 1; }
.crosshair-dot { stroke: var(--surface-1); stroke-width: 2; }

.chart-tooltip {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-md);
  padding: 8px 10px;
  font-size: 12px;
  min-width: 150px;
}
.chart-tooltip .tt-head { color: var(--text-muted); margin-bottom: 4px; font-size: 11.5px; }
.chart-tooltip .tt-row { display: flex; align-items: center; gap: 7px; padding: 1.5px 0; }
.chart-tooltip .tt-key { width: 12px; height: 3px; border-radius: 2px; flex: none; }
.chart-tooltip .tt-row strong { font-variant-numeric: tabular-nums; font-weight: 650; }
.chart-tooltip .tt-name { color: var(--text-secondary); }

/* ------------------------------------------------------------ Monte Carlo */

.mc-controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.mc-progress { flex: 1 1 160px; height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; min-width: 120px; }
.mc-progress > div { height: 100%; width: 0%; background: var(--accent); transition: width 0.1s linear; }
.mc-note { font-size: 12px; color: var(--text-muted); }

/* ------------------------------------------------------------- break-even */

.breakeven-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.be-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
}
.be-card .be-q { font-size: 12.5px; color: var(--text-secondary); margin-bottom: 8px; min-height: 3em; }
.be-card .be-value { font-size: 22px; font-weight: 650; letter-spacing: -0.01em; }
.be-card .be-current { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.be-gauge { position: relative; height: 22px; margin-top: 10px; }
.be-gauge .track { position: absolute; top: 9px; left: 0; right: 0; height: 4px; border-radius: 2px; background: var(--surface-2); }
.be-gauge .marker { position: absolute; top: 4px; width: 2px; height: 14px; border-radius: 1px; background: var(--text-muted); }
.be-gauge .marker.current { background: var(--accent); width: 4px; border-radius: 2px; }
.be-gauge .lab { position: absolute; top: 22px; font-size: 10.5px; color: var(--text-muted); transform: translateX(-50%); white-space: nowrap; }
.be-card .be-verdict { font-size: 12px; margin-top: 18px; color: var(--text-secondary); }
.be-card .be-verdict strong { font-weight: 650; }

/* -------------------------------------------------------------- timeline */

.timeline { position: relative; padding-left: 22px; }
.timeline::before { content: ""; position: absolute; left: 7px; top: 6px; bottom: 6px; width: 2px; background: var(--grid-line); border-radius: 1px; }
.tl-item { position: relative; padding: 6px 0 6px 8px; }
.tl-item::before {
  content: ""; position: absolute; left: -21px; top: 13px;
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--surface-1);
}
.tl-item[data-kind="sale"]::before { background: var(--series-2); }
.tl-item[data-kind="wall"]::before { background: var(--series-3); }
.tl-item[data-kind="mortgage"]::before { background: var(--series-1); }
.tl-item[data-kind="invest"]::before { background: var(--series-4); }
.tl-item[data-kind="tax"]::before { background: var(--text-muted); }
.tl-date { font-size: 11.5px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.tl-title { font-weight: 600; font-size: 13.5px; }
.tl-detail { font-size: 12.5px; color: var(--text-secondary); }

/* --------------------------------------------------------------- summary */

.decision-summary {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}
.decision-summary p { margin-bottom: 10px; font-size: 14px; color: var(--text-primary); }
.decision-summary p:last-child { margin-bottom: 0; }

.disclaimer { font-size: 11.5px; color: var(--text-muted); margin-top: 22px; }

/* ----------------------------------------------------------------- modal */

dialog.shortcuts {
  margin: auto; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-1); color: var(--text-primary);
  padding: 20px 24px; max-width: 420px; width: calc(100vw - 48px);
}
dialog.shortcuts::backdrop { background: rgba(0, 0, 0, 0.4); }
dialog.shortcuts h3 { margin-bottom: 10px; font-size: 15px; }
dialog.shortcuts table { width: 100%; font-size: 13px; border-collapse: collapse; }
dialog.shortcuts td { padding: 5px 0; color: var(--text-secondary); }
dialog.shortcuts td:first-child { width: 90px; }
dialog.shortcuts kbd {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 5px; padding: 1px 7px; font-family: inherit; font-size: 12px;
}

.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--text-primary); color: var(--page);
  border-radius: 8px; padding: 9px 16px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: opacity 0.25s ease; z-index: 50;
}
.toast.show { opacity: 1; }

/* ------------------------------------------------------------- responsive */

@media (max-width: 1000px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; max-height: none; border-right: none; border-bottom: 1px solid var(--border); }
  .kpi .kpi-value { font-size: 20px; }
  .kpi.kpi-hero .kpi-value { font-size: 28px; }
}

@media (max-width: 560px) {
  .app-header { padding: 14px 16px; }
  .main { padding: 16px 14px 48px; }
  .charts-grid { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}

/* ----------------------------------------------------------------- print */

@media print {
  body { background: #fff; }
  .sidebar, .header-actions, .mc-controls, .btn, .app-header .scenario-bar { display: none !important; }
  .layout { grid-template-columns: 1fr; }
  .chart-card, .kpi, .be-card, .decision-summary { box-shadow: none; break-inside: avoid; }
  section.panel { break-inside: avoid-page; }
}
