:root {
  --bg: #edf1f7;
  --panel: #ffffff;
  --ink: #14233d;
  --muted: #6b7a99;
  --line: #e4eaf3;
  --brand: #0e9384;
  --brand-strong: #0a7a6e;
  --blue: #2563eb;
  --interest: #e5446d;
  --anuity: #6366f1;
  --diff: #f59e0b;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 35, 61, 0.05), 0 8px 24px rgba(20, 35, 61, 0.06);
  --input: #fbfcfe;
  --track: #eef2f8;
  --thead: #f4f7fb;
  --hover: #f2f7f6;
  --grid: #e8edf5;
  --axis: #8a97b1;
  --warn-bg: #fff4f5;
  --warn-border: #f9c8d1;
  --warn-ink: #b02243;
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Inter", Arial, sans-serif;
}

:root[data-theme="dark"] {
  --bg: #0f1724;
  --panel: #1a2436;
  --ink: #e7edf9;
  --muted: #8ea0bd;
  --line: #2a3650;
  --brand: #14b8a6;
  --brand-strong: #2dd4bf;
  --blue: #60a5fa;
  --interest: #fb7185;
  --anuity: #8b93ff;
  --diff: #fbbf24;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 10px 26px rgba(0, 0, 0, 0.35);
  --input: #111c2e;
  --track: #22304a;
  --thead: #1d2940;
  --hover: #1e2b43;
  --grid: #26344f;
  --axis: #7d8db0;
  --warn-bg: #36202a;
  --warn-border: #7a3d4e;
  --warn-ink: #ff9eb2;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(14, 147, 132, 0.08), transparent 60%),
    radial-gradient(1000px 500px at -10% 0%, rgba(37, 99, 235, 0.07), transparent 55%),
    var(--bg);
  color: var(--ink);
  min-height: 100vh;
  line-height: 1.45;
}
:root[data-theme="dark"] body {
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(20, 184, 166, 0.1), transparent 60%),
    radial-gradient(1000px 500px at -10% 0%, rgba(96, 165, 250, 0.08), transparent 55%),
    var(--bg);
}

body, .panel, .metric, .chart-card, input.money, input.num-inline,
.btn-ghost, .scheme-select, .comp-card, .presets button, #scheduleTable tbody tr {
  transition: background-color 0.25s ease, color 0.25s ease, border-color 0.25s ease;
}

/* ---------- header ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 18px clamp(16px, 4vw, 40px);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--blue));
  box-shadow: 0 6px 16px rgba(14, 147, 132, 0.35);
}
.topbar h1 { margin: 0; font-size: 20px; letter-spacing: -0.01em; }
.topbar-sub { margin: 0; color: var(--muted); font-size: 13px; }
.topbar-note {
  color: var(--muted);
  font-size: 12px;
  background: var(--track);
  padding: 6px 12px;
  border-radius: 999px;
}
.topbar-right { display: flex; align-items: center; gap: 10px; }

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  cursor: pointer;
  transition: all 0.2s ease;
}
.theme-toggle:hover { color: var(--brand-strong); border-color: var(--brand); }
.theme-toggle .icon-sun, .theme-toggle .icon-moon { display: none; }
.theme-toggle .icon-moon { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 330px 1fr;
  gap: 20px;
  max-width: 1240px;
  margin: 0 auto;
  padding: 8px clamp(16px, 4vw, 40px) 48px;
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- controls ---------- */
.controls {
  padding: 20px;
  position: sticky;
  top: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.controls h2 { margin: 0 0 2px; font-size: 16px; }

.field { display: flex; flex-direction: column; gap: 8px; }
.field-head { display: flex; align-items: center; justify-content: space-between; }
.field-head label { font-size: 13px; font-weight: 600; color: var(--muted); }
.field .hint, .field .val {
  font-size: 13px;
  font-weight: 700;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

input.money,
input.num-inline {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  background: var(--input);
  color: var(--ink);
  transition: border-color 0.15s, box-shadow 0.15s;
}
input.money:focus,
input.num-inline:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(14, 147, 132, 0.15);
}
.num-inline { margin-top: 2px; }

.slide-row input[type="range"] {
  width: 100%;
  accent-color: var(--brand);
  cursor: pointer;
}

.presets { display: flex; gap: 8px; }
.presets button {
  flex: 1;
  border: 1px solid var(--line);
  background: var(--input);
  color: var(--ink);
  border-radius: 8px;
  padding: 6px 4px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.presets button:hover { border-color: var(--brand); color: var(--brand); }

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  background: var(--track);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 4px;
}
.segmented button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  padding: 8px 4px;
  border-radius: 9px;
  cursor: pointer;
  transition: all 0.15s;
}
.segmented button.active {
  background: var(--panel);
  color: var(--brand-strong);
  box-shadow: 0 2px 6px rgba(20, 35, 61, 0.12);
}

.loan-box {
  background: linear-gradient(135deg, rgba(14, 147, 132, 0.08), rgba(37, 99, 235, 0.07));
  border: 1px dashed rgba(14, 147, 132, 0.4);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.loan-line {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 13px;
  color: var(--muted);
}
.loan-line strong {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.warning {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-ink);
  font-size: 13px;
}

/* ---------- dashboard ---------- */
.dashboard { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

.cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
}
.metric {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  position: relative;
  overflow: hidden;
}
.metric::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: var(--metric-color, var(--brand));
  opacity: 0.85;
}
.metric-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.metric-label[data-icon="d1"] { --metric-color: var(--blue); background: linear-gradient(transparent, transparent); }
.metric-label[data-icon="d2"] { --metric-color: var(--brand); }
.metric-label[data-icon="d3"] { --metric-color: var(--interest); }
.metric-label[data-icon="d4"] { --metric-color: var(--anuity); }
.metric-label[data-icon="d1"]::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--metric-color); }
.metric-label[data-icon="d2"]::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--metric-color); }
.metric-label[data-icon="d3"]::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--metric-color); }
.metric-label[data-icon="d4"]::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--metric-color); }
.metric-value {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 6px;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
}
.metric-sub { margin-top: 6px; font-size: 12px; color: var(--muted); }
.chip {
  display: inline-flex;
  align-items: baseline;
  padding: 2px 8px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.chip-annuity { color: #4f46e5; background: rgba(99, 102, 241, 0.12); }
.chip-diff { color: #b45309; background: rgba(245, 158, 11, 0.16); }
.saving { color: var(--brand-strong); font-weight: 700; }

/* ---------- charts ---------- */
.charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.chart-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
}
.chart-card.wide { grid-column: span 2; }
.chart-card h3 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 700;
}
.chart-body { width: 100%; position: relative; }
.chart-body svg line { stroke: var(--grid); }
.chart-body svg text { fill: var(--axis); }
.chart-body svg text[font-weight="700"] { fill: var(--ink); }
.donut-body { display: flex; align-items: center; justify-content: center; gap: 20px; flex-wrap: wrap; min-height: 190px; }
.compare-body { min-height: 190px; display: flex; align-items: stretch; gap: 12px; flex-wrap: wrap; }

.donut-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
}
.donut-center .dc-label { font-size: 11px; color: var(--muted); font-weight: 600; }
.donut-center .dc-value { font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; }
.donut-center .dc-note { font-size: 11px; color: var(--muted); }

.legend { display: flex; flex-direction: column; gap: 8px; min-width: 150px; font-size: 12px; }
.legend .lg-item { display: flex; align-items: center; gap: 8px; }
.legend .lg-dot { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }
.legend .lg-name { color: var(--muted); flex: 1; }
.legend .lg-value { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }

.legend-h { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 8px; font-size: 12px; }
.legend-h .lg-item { display: flex; align-items: center; gap: 6px; }
.legend-h .lg-name { color: var(--muted); }

.tip {
  position: absolute;
  pointer-events: none;
  background: rgba(20, 35, 61, 0.94);
  color: #fff;
  font-size: 12px;
  padding: 8px 10px;
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(20, 35, 61, 0.3);
  white-space: nowrap;
  z-index: 5;
  transform: translate(-50%, calc(100% + 10px));
  display: none;
}
.tip .tt-title { font-weight: 700; margin-bottom: 4px; color: #cfe6e2; }
.tip .tt-row { display: flex; align-items: center; gap: 6px; }
.tip .tt-dot { width: 8px; height: 8px; border-radius: 50%; }

/* ---------- compare cards ---------- */
.comp-card {
  flex: 1 1 180px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--input);
}
.comp-card.annuity { border-top: 3px solid var(--anuity); }
.comp-card.diff { border-top: 3px solid var(--diff); }
.comp-name { font-size: 13px; font-weight: 800; }
.comp-card .cc-row { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); gap: 8px; }
.comp-card .cc-row b { color: var(--ink); font-variant-numeric: tabular-nums; white-space: nowrap; }
.comp-line-type { font-size: 12px; font-weight: 700; margin: 2px 0 -2px; }
.best-note {
  grid-column: 1 / -1;
  flex-basis: 100%;
  text-align: center;
  font-size: 12.5px;
  color: var(--ink);
  background: linear-gradient(135deg, rgba(14, 147, 132, 0.12), rgba(245, 158, 11, 0.12));
  border: 1px solid rgba(14, 147, 132, 0.3);
  border-radius: 10px;
  padding: 8px 10px;
}
.best-note b { color: var(--brand-strong); }

/* ---------- table ---------- */
.table-card { grid-column: 1 / -1; }
.table-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.table-head h3 { margin: 0; }
.btn-ghost {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font-size: 12px;
  font-weight: 600;
  border-radius: 9px;
  padding: 7px 14px;
  cursor: pointer;
  transition: all 0.15s;
}
.btn-ghost:hover { border-color: var(--brand); color: var(--brand-strong); }
.btn-ghost:disabled { opacity: 0.4; cursor: default; }

.table-scroll { max-height: 420px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; min-width: 640px; }
thead th {
  position: sticky;
  top: 0;
  background: var(--thead);
  color: var(--muted);
  font-weight: 700;
  text-align: left;
  padding: 10px 12px;
  font-size: 12px;
  border-bottom: 1px solid var(--line);
  z-index: 1;
}
thead th.num, tbody td.num { text-align: right; }
tbody td { padding: 9px 12px; border-bottom: 1px solid var(--line); }
tbody tr:nth-child(even) { background: var(--input); }
tbody td:first-child { color: var(--muted); }
tbody tr:hover { background: var(--hover); }

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-top: 12px;
  font-size: 13px;
  color: var(--muted);
}
.pager b { color: var(--ink); }

/* ---------- footer ---------- */
.footer {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  padding: 0 16px 40px;
}

.placeholder {
  display: grid;
  place-items: center;
  min-height: 170px;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

/* ---------- donut pair (compare mode) ---------- */
.donut-pair {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: center;
}
.dp-title {
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 6px;
}
.donut-body .donut-pair .legend { min-width: 0; }
.dp-col { flex: 0 0 auto; }

/* ---------- table tools & scheme select ---------- */
.table-tools { display: flex; align-items: center; gap: 8px; }
.muted-sm { font-size: 12px; color: var(--muted); }
.scheme-select {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font-size: 12px;
  font-weight: 600;
  border-radius: 9px;
  padding: 7px 10px;
  cursor: pointer;
}

/* ---------- responsive ---------- */
@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; }
  .controls { position: static; }
  .cards { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 620px) {
  .cards { grid-template-columns: 1fr; }
  .charts { grid-template-columns: 1fr; }
  .chart-card.wide { grid-column: span 1; }
}