:root {
  color-scheme: light;
  --page: #f4f4f1;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6f6e69;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --field: #ffffff;
  --accent: #2a78d6;
  --accent-strong: #256abf;
  --on-accent: #ffffff;
  --principal: #2a78d6;
  --interest: #eb6834;
  --without: #8f8e86;
  --good: #006300;
  --bad: #b3261e;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #9d9c95;
    --grid: #2c2c2a;
    --axis: #45453f;
    --border: rgba(255, 255, 255, 0.1);
    --field: #121211;
    --accent: #3987e5;
    --accent-strong: #3987e5;
    --on-accent: #0b0b0b;
    --principal: #3987e5;
    --interest: #d95926;
    --without: #6b6a64;
    --good: #0ca30c;
    --bad: #f2867f;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
}

h1, h2, p { margin: 0; }
h1 { font-size: 22px; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 17px; font-weight: 650; }

.page {
  max-width: 1120px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  gap: 16px;
}

.output { display: grid; gap: 16px; min-width: 0; }

@media (min-width: 960px) {
  .page {
    grid-template-columns: 360px minmax(0, 1fr);
    align-items: start;
    gap: 20px;
    padding: 24px;
  }
  .top { grid-column: 1 / -1; }
  .loan { position: sticky; top: 20px; }
  .output { gap: 20px; }
}

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.lang {
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  min-height: 36px;
  cursor: pointer;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}

@media (min-width: 600px) {
  .card { padding: 20px; }
}

.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Form */

.loan { display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; }

.field { border: 0; margin: 0; padding: 0; min-width: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; }
.field > label, .field > legend { font-size: 14px; font-weight: 550; color: var(--ink-2); padding: 0; }
.field > legend { margin-bottom: 6px; }
.field.off { opacity: 0.45; }

.input {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--field);
  border: 1px solid var(--axis);
  border-radius: 8px;
  padding: 0 12px;
  min-height: 46px;
}
.input:focus-within { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.input input {
  flex: 1;
  width: 100%;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 18px;
  padding: 10px 0;
}
.input .affix { color: var(--muted); font-size: 17px; }
.input.invalid { border-color: var(--bad); }
.error { color: var(--bad); font-size: 13px; }
.hint { color: var(--muted); font-size: 13px; }

.inline-input { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; color: var(--ink); }
.inline-input input {
  width: 84px;
  min-height: 46px;
  background: var(--field);
  border: 1px solid var(--axis);
  border-radius: 8px;
  color: var(--ink);
  font: inherit;
  font-size: 18px;
  padding: 8px 10px;
  text-align: center;
}
.inline-input input:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

.chips { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.chip { position: relative; }
.chip input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.chip span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  border: 1px solid var(--axis);
  border-radius: 8px;
  background: var(--field);
  font-size: 16px;
  font-weight: 550;
}
.chip input:checked + span { background: var(--accent-strong); border-color: var(--accent-strong); color: var(--on-accent); }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.options { display: grid; gap: 8px; }
.option {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 46px;
  padding: 8px 12px;
  border: 1px solid var(--axis);
  border-radius: 8px;
  background: var(--field);
  cursor: pointer;
}
.option input { accent-color: var(--accent); width: 18px; height: 18px; margin: 0; flex: none; }
.option:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

input[type="number"]::-webkit-inner-spin-button { opacity: 1; }

/* Results */

.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.tile-label { font-size: 13px; color: var(--ink-2); }
.tile-value { font-size: 26px; font-weight: 650; letter-spacing: -0.01em; line-height: 1.2; margin-top: 2px; overflow-wrap: anywhere; }
.tile-value.good { color: var(--good); }
.tile-sub { font-size: 13px; color: var(--muted); margin-top: 2px; }

table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { padding: 9px 0; text-align: right; border-top: 1px solid var(--grid); vertical-align: top; }
th:first-child, td:first-child { text-align: left; padding-right: 8px; }
th + th, td + td { padding-left: 12px; }
thead th { border-top: 0; font-size: 13px; font-weight: 550; color: var(--ink-2); padding-top: 0; }
tbody th { font-weight: 450; color: var(--ink-2); white-space: nowrap; }
td { font-variant-numeric: tabular-nums; white-space: nowrap; }
.summary td { font-weight: 550; }
.summary td.good { color: var(--good); }
.table-wrap { overflow-x: auto; }

/* Month by month */

#months { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }

.scrub { display: grid; gap: 6px; }
.scrub-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 12px; font-weight: 600; }
.scrub-head .muted { font-weight: 450; font-size: 14px; }
input[type="range"] { width: 100%; margin: 6px 0; accent-color: var(--accent); min-height: 28px; }

.split { display: flex; gap: 2px; height: 14px; }
.split span { display: block; min-width: 0; }
.split-principal { background: var(--principal); border-radius: 4px 0 0 4px; }
.split-interest { background: var(--interest); border-radius: 0 4px 4px 0; }
.split .whole { border-radius: 4px; }

.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.key { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-2); }
.share { color: var(--muted); font-variant-numeric: tabular-nums; }
.value { font-size: 20px; font-weight: 650; margin-top: 2px; }
.facts { padding-top: 14px; border-top: 1px solid var(--grid); }
.change { font-size: 14px; min-height: 1.45em; }

.swatch { width: 10px; height: 10px; border-radius: 2px; flex: none; }
.swatch.principal { background: var(--principal); }
.swatch.interest { background: var(--interest); }

.chart { margin: 8px 0 0; min-width: 0; }
.chart figcaption { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 16px; margin-bottom: 6px; }
.chart-title { font-size: 14px; font-weight: 600; }
.legend { display: inline-flex; flex-wrap: wrap; gap: 4px 14px; }
.legend strong { color: var(--ink); font-weight: 600; margin-left: 2px; font-variant-numeric: tabular-nums; }
.line-key { width: 14px; height: 2px; border-radius: 1px; flex: none; }
.line-key.principal { background: var(--principal); }
.line-key.interest { background: var(--interest); }
.line-key.without { background: var(--without); }

.chart svg { display: block; width: 100%; height: auto; touch-action: pan-y; user-select: none; -webkit-user-select: none; cursor: crosshair; overflow: visible; }
.chart svg text { font-family: inherit; font-size: 12px; fill: var(--muted); }
.chart .gridline { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.principal { stroke: var(--principal); }
.chart .line.interest { stroke: var(--interest); }
.chart .line.without { stroke: var(--without); }
.chart .wash { fill: var(--principal); opacity: 0.1; stroke: none; }
.chart .cross-line { stroke: var(--ink-2); stroke-width: 1; opacity: 0.55; shape-rendering: crispEdges; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .dot.principal { fill: var(--principal); }
.chart .dot.interest { fill: var(--interest); }
.chart .dot.without { fill: var(--without); }
.chart svg text.direct { fill: var(--ink-2); paint-order: stroke; stroke: var(--surface); stroke-width: 4px; stroke-linejoin: round; }

.years summary { cursor: pointer; font-weight: 600; font-size: 15px; padding: 6px 0; }
.years table { margin-top: 6px; }
td .muted { display: block; font-size: 12px; font-weight: 450; white-space: normal; }

/* Other terms */

#terms { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; }
#terms table { margin-top: 8px; }
.term-button {
  font: inherit;
  white-space: nowrap;
  color: var(--accent);
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}
tr.current th, tr.current td { font-weight: 650; color: var(--ink); }
tr.current .term-button { color: var(--ink); text-decoration: none; cursor: default; }

.note { font-size: 13px; color: var(--muted); padding: 0 4px 12px; }

button:focus-visible, summary:focus-visible, input[type="range"]:focus-visible, .option input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (max-width: 400px) {
  table { font-size: 14px; }
  th + th, td + td { padding-left: 8px; }
  .tile-value { font-size: 24px; }
  .value { font-size: 18px; }
}

@media (max-width: 380px) {
  table { font-size: 13px; }
  th + th, td + td { padding-left: 6px; }
}

[hidden] { display: none !important; }
