/* Tokens live on :root so body and every descendant inherit them — custom
   properties only cascade downward. Light is the default; dark values are
   declared under both the OS media query and the [data-theme="dark"] stamp so
   the toggle wins either way. */
:root {
  color-scheme: light;
  --plane:          #f9f9f7;
  --surface-1:      #fcfcfb;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --hairline:       rgba(11, 11, 11, 0.10);
  --gridline:       #e1e0d9;
  --meter-fill:     #2a78d6;   /* blue 450 */
  --meter-track:    #86b6ef;   /* blue 250 — clears 2:1 on the light surface */
  --accent:         #2a78d6;
  --accent-ink:     #ffffff;
  --good:           #006300;
  --field-bg:       #ffffff;
  --shadow:         0 1px 2px rgba(11, 11, 11, 0.04), 0 8px 24px rgba(11, 11, 11, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --plane:          #0d0d0d;
    --surface-1:      #1a1a19;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --hairline:       rgba(255, 255, 255, 0.10);
    --gridline:       #2c2c2a;
    --meter-fill:     #3987e5;
    --meter-track:    #184f95;
    --accent:         #3987e5;
    --accent-ink:     #06121f;
    --good:           #0ca30c;
    --field-bg:       #131312;
    --shadow:         none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --plane:          #0d0d0d;
  --surface-1:      #1a1a19;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --hairline:       rgba(255, 255, 255, 0.10);
  --gridline:       #2c2c2a;
  --meter-fill:     #3987e5;
  --meter-track:    #184f95;
  --accent:         #3987e5;
  --accent-ink:     #06121f;
  --good:           #0ca30c;
  --field-bg:       #131312;
  --shadow:         none;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--plane);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

/* The centred column. body already paints the plane behind it. */
.viz-root {
  background: var(--plane);
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 620px;
  margin: 0 auto;
  padding: 20px 16px 48px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 2px 2px;
}

.topbar h1 {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.sub {
  margin: 3px 0 0;
  font-size: 0.8125rem;
  color: var(--text-secondary);
}

.icon-btn {
  flex: none;
  width: 36px; height: 36px;
  display: grid;
  place-items: center;
  border: 1px solid var(--hairline);
  border-radius: 10px;
  background: var(--surface-1);
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.icon-btn:hover { color: var(--text-primary); border-color: var(--gridline); }

.icon-moon { display: none; }
:root[data-theme="dark"] .icon-sun { display: none; }
:root[data-theme="dark"] .icon-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .icon-sun { display: none; }
  :root:where(:not([data-theme="light"])) .icon-moon { display: block; }
}

/* ---------- cards ---------- */

.card {
  background: var(--surface-1);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 18px;
  box-shadow: var(--shadow);
}

.card h2 {
  margin: 0;
  font-size: 0.9375rem;
  font-weight: 600;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

/* ---------- hero + meter ---------- */

.hero-card { padding: 20px 18px 18px; }

.tile-label {
  margin: 0;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.01em;
}

.hero {
  margin: 6px 0 0;
  font-size: 3rem;
  line-height: 1.05;
  font-weight: 600;
  letter-spacing: -0.03em;
  /* Proportional figures — tabular-nums would loosen a display-size number. */
}

.hero.is-done { color: var(--good); }

.hero-note {
  margin: 7px 0 0;
  font-size: 0.8125rem;
  color: var(--text-secondary);
}

.meter {
  margin-top: 18px;
  height: 10px;
  border-radius: 999px;
  background: var(--meter-track);
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--meter-fill);
  transition: width 0.45s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.meter-scale {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.meter-pct {
  color: var(--text-secondary);
  font-weight: 600;
}

/* ---------- KPI tiles ---------- */

.kpis {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.tile { padding: 14px 15px; }

.tile-value {
  margin: 5px 0 0;
  font-size: 1.375rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.tile-sub {
  margin: 3px 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* ---------- form ---------- */

.form-card form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 14px;
}

.field-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }

.field label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.optional {
  color: var(--text-muted);
  font-weight: 400;
}

input[type="date"],
input[type="number"],
input[type="text"] {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  font: inherit;
  /* 16px exactly — anything smaller makes iOS Safari zoom the page on focus. */
  font-size: 1rem;
  padding: 10px 12px;
  color: var(--text-primary);
  background: var(--field-bg);
  border: 1px solid var(--gridline);
  border-radius: 10px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}

/* iOS Safari sizes date inputs to their native picker and ignores the grid
   column, so the field spills over the one beside it. Dropping the native
   appearance lets width:100% win; the tap-to-open picker still works. */
input[type="date"] {
  -webkit-appearance: none;
  appearance: none;
}

input[type="date"]::-webkit-date-and-time-value {
  text-align: left;
  margin: 0;
}

.input-affix { position: relative; }

.input-affix .affix {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1rem;
  color: var(--text-muted);
  pointer-events: none;
}

.input-affix input { padding-left: 28px; }

.btn-primary {
  margin-top: 2px;
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  padding: 11px 16px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  transition: filter 0.15s ease;
}

.btn-primary:hover { filter: brightness(1.07); }
.btn-primary:active { filter: brightness(0.95); }

.btn-primary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.form-msg {
  margin: 0;
  min-height: 1.1em;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.form-msg.is-error { color: #d03b3b; }
.form-msg.is-ok { color: var(--good); }

/* ---------- history ---------- */

.count-chip {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.table-scroll { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

th, td {
  text-align: left;
  padding: 10px 8px;
  border-bottom: 1px solid var(--gridline);
  white-space: nowrap;
}

th {
  font-size: 0.6875rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding-top: 0;
}

th:first-child, td:first-child { padding-left: 0; }
th:last-child, td:last-child { padding-right: 0; width: 1%; }

tbody tr:last-child td { border-bottom: none; }

.num { text-align: right; font-variant-numeric: tabular-nums; }

td.amount { font-weight: 600; }

td.balance { color: var(--text-secondary); }

.row-note {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 400;
  margin-top: 2px;
  max-width: 22ch;
  overflow: hidden;
  text-overflow: ellipsis;
}

.del-btn {
  width: 30px; height: 30px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease;
}

.del-btn:hover {
  color: #d03b3b;
  background: color-mix(in srgb, #d03b3b 10%, transparent);
}

.empty {
  margin: 2px 0 0;
  font-size: 0.875rem;
  color: var(--text-muted);
}

/* ---------- footer ---------- */

.foot {
  margin-top: 2px;
  padding: 0 2px;
}

.foot p {
  margin: 0;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Small phones (SE-sized): a date and an amount side by side get too tight
   even with the native appearance dropped, so stack them. */
@media (max-width: 380px) {
  .field-row { grid-template-columns: 1fr; }
}

/* ---------- wider screens ---------- */

@media (min-width: 560px) {
  .viz-root { padding: 28px 20px 56px; gap: 16px; }
  .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .hero { font-size: 3.5rem; }
  .card { padding: 20px; }
  .row-note { max-width: 32ch; }
}

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