:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --good: #16a34a;
  --warn: #f59e0b;
  --bad: #dc2626;
  --gauge-track: #e2e8f0;
  --needle: #0f172a;
  --input-bg: #f8fafc;
  --backdrop: rgba(15, 23, 42, 0.45);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 4px 16px rgba(15, 23, 42, 0.06);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --card: #151e2e;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --border: #243044;
    --accent: #3b82f6;
    --gauge-track: #243044;
    --needle: #f8fafc;
    --input-bg: #0f172a;
    --backdrop: rgba(0, 0, 0, 0.6);
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: calc(96px + var(--safe-bottom));
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

/* ---------- Top bar ---------- */
.topbar {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  padding: calc(8px + var(--safe-top)) 12px 4px;
}
.topbar h1 {
  margin: 0;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
}
.icon-btn {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  color: var(--muted);
}
.icon-btn:active { background: var(--border); }

main {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 16px;
}

/* ---------- Week navigation ---------- */
.week-nav {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  margin: 4px 0 12px;
}
.week-nav .icon-btn { color: var(--accent); }
.week-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px;
  border-radius: 12px;
}
.week-label:active { background: var(--border); }
#weekRange { font-size: 18px; font-weight: 600; }
.week-hint { font-size: 13px; color: var(--muted); }

/* ---------- Gauge ---------- */
.gauge-card, .list-card {
  background: var(--card);
  border-radius: 20px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
.gauge-card { padding: 16px 16px 18px; }

#gauge { display: block; width: 100%; max-width: 360px; margin: 0 auto; }
.gauge-letter {
  font-size: 22px;
  font-weight: 700;
  fill: var(--muted);
  text-anchor: middle;
}
.gauge-tick { stroke: var(--muted); stroke-linecap: round; }
.gauge-pump { fill: var(--muted); stroke: var(--muted); opacity: 0.55; }
.gauge-pump path:first-child { stroke: none; }
#needle {
  fill: var(--needle);
  transform-box: view-box;
  transform-origin: 150px 150px;
  transition: transform 0.9s cubic-bezier(0.34, 1.3, 0.64, 1);
}
.gauge-hub { fill: var(--needle); }

.gauge-readout { text-align: center; margin-top: 2px; }
.remaining-amount {
  font-size: 40px;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.remaining-amount.over { color: var(--bad); }
.remaining-label { font-size: 15px; color: var(--muted); margin-top: 2px; }

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin-top: 16px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}
.stat { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.stat + .stat { border-left: 1px solid var(--border); }
.stat-btn { border-radius: 0; }
.stat-btn .stat-value { color: var(--accent); }
.stat-btn:active { background: var(--bg); }
.stat-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.stat-value { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- Expense list ---------- */
.list-card { margin-top: 16px; padding: 14px 0 6px; }
.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 16px 8px;
}
.list-card h2 {
  margin: 0;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.expense-list { list-style: none; margin: 0; padding: 0; }
.expense-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 12px;
  width: 100%;
  text-align: left;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.expense-item:active { background: var(--bg); }
.expense-list li:first-child .expense-item { border-top: 0; }
.expense-cat { font-weight: 600; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.expense-amt { font-weight: 600; font-size: 16px; font-variant-numeric: tabular-nums; text-align: right; }
.expense-meta {
  grid-column: 1 / -1;
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.segmented {
  display: inline-grid;
  grid-auto-flow: column;
  padding: 2px;
  border-radius: 9px;
  background: var(--bg);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.segmented button {
  padding: 5px 10px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  white-space: nowrap;
}
.segmented button[aria-selected="true"] {
  background: var(--card);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.12);
}
.cat-item .expense-cat { display: flex; align-items: center; gap: 6px; }
.cat-item .chev {
  flex-shrink: 0;
  color: var(--muted);
  transition: transform 0.15s;
}
.cat-item.open .chev { transform: rotate(90deg); }
.cat-name { overflow: hidden; text-overflow: ellipsis; }
.share-bar {
  grid-column: 1 / -1;
  height: 4px;
  margin-top: 4px;
  border-radius: 2px;
  background: var(--gauge-track);
  overflow: hidden;
}
.share-bar span { display: block; height: 100%; border-radius: 2px; background: var(--accent); }
.sub-list { background: var(--bg); }
.sub-list .expense-item { padding-left: 36px; }
.sub-list li:first-child .expense-item { border-top: 1px solid var(--border); }
.sub-list .expense-cat, .sub-list .expense-amt { font-weight: 500; font-size: 15px; }
.empty-state { margin: 8px 16px 14px; color: var(--muted); font-size: 15px; }

/* ---------- Bottom bar ---------- */
.bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
  display: flex;
  justify-content: center;
}
.primary-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  max-width: 528px;
  height: 56px;
  border-radius: 16px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 18px;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(37, 99, 235, 0.35);
}
.primary-btn:active { transform: scale(0.98); }

/* ---------- Sheets ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
  animation: fade-in 0.2s ease;
}
.sheet-backdrop[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: 560px;
  max-height: calc(100dvh - var(--safe-top) - 24px);
  overflow-y: auto;
  background: var(--card);
  border-radius: 20px 20px 0 0;
  padding: 0 16px calc(24px + var(--safe-bottom));
  animation: slide-up 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet-header {
  position: sticky;
  top: 0;
  background: var(--card);
  display: grid;
  grid-template-columns: 80px 1fr 80px;
  align-items: center;
  padding: 14px 0 10px;
  margin-bottom: 4px;
  z-index: 1;
}
.sheet-header h2 { margin: 0; text-align: center; font-size: 17px; font-weight: 600; }
.text-btn { color: var(--accent); font-size: 17px; padding: 8px 0; text-align: left; }
.text-btn.strong { font-weight: 600; text-align: right; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes slide-up { from { transform: translateY(100%); } }

.field { display: block; position: relative; margin-top: 14px; }
.field > span {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin: 0 0 6px 4px;
}
.field input, .field textarea, .field select {
  width: 100%;
  /* 16px+ prevents iOS from zooming in on focus */
  font: inherit;
  font-size: 17px;
  color: var(--text);
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
.field input[type="date"] { min-height: 48px; }
.field select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%2364748b' stroke-width='2'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
}
.field input:focus, .field textarea:focus, .field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}
.field textarea { resize: none; }

.amount-wrap { position: relative; }
.amount-wrap .currency-symbol {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 17px;
  pointer-events: none;
}
.amount-wrap input { padding-left: 28px; font-variant-numeric: tabular-nums; }

.suggestions {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.suggestions[hidden] { display: none; }
.suggestions button {
  padding: 8px 14px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent);
  font-size: 15px;
  font-weight: 500;
}
.suggestions button:active { background: color-mix(in srgb, var(--accent) 25%, transparent); }
.suggestions mark { background: none; color: inherit; font-weight: 700; }

.form-error { color: var(--danger); font-size: 15px; margin: 12px 4px 0; }
.form-error[hidden] { display: none; }

.danger-btn, .secondary-btn {
  width: 100%;
  height: 50px;
  border-radius: 14px;
  font-size: 17px;
  font-weight: 600;
}
.danger-btn {
  margin-top: 24px;
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
}
.danger-btn[hidden] { display: none; }
.secondary-btn {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.settings-section { margin-top: 28px; border-top: 1px solid var(--border); padding-top: 16px; }
.settings-section h3 { margin: 0 0 4px 4px; font-size: 15px; }
.hint { margin: 0 4px 12px; font-size: 14px; color: var(--muted); }
/* ---------- 8-week report ---------- */
.report-sheet { height: calc(100dvh - var(--safe-top) - 24px); }
.report-tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 4px; }
.tile {
  background: var(--bg);
  border-radius: 14px;
  padding: 10px 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.tile-label { font-size: 12px; color: var(--muted); }
.tile-value { font-size: 20px; font-weight: 700; white-space: nowrap; }
.tile-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chart-block { margin-top: 18px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--muted); margin: 0 2px 8px; }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.key-under { background: var(--accent); }
.key-over { background: var(--bad); }
.key-budget { width: 14px; height: 2px; border-radius: 1px; background: var(--text); }

.chart-wrap { position: relative; }
#reportChart { display: block; width: 100%; height: 220px; overflow: visible; }
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-axis-text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-axis-text.current { fill: var(--text); font-weight: 600; }
.chart-bar-under { fill: var(--accent); }
.chart-bar-over { fill: var(--bad); }
.chart-budget { stroke: var(--text); stroke-width: 2; stroke-linecap: round; }
.chart-col { transition: opacity 0.15s; }
.chart-dim .chart-col { opacity: 0.35; }
.chart-dim .chart-col.active { opacity: 1; }
.chart-hit { fill: transparent; cursor: pointer; }

.chart-tip {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--card);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
  z-index: 2;
}
.chart-tip[hidden] { display: none; }
.chart-tip strong { display: block; font-size: 13px; }
.chart-tip .tip-row { font-variant-numeric: tabular-nums; }

.report-subhead {
  margin: 22px 4px 4px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.report-list { list-style: none; margin: 0; padding: 0; }
.report-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 12px;
  width: 100%;
  text-align: left;
  padding: 12px 4px;
  border-top: 1px solid var(--border);
}
.report-list li:first-child .report-row { border-top: 0; }
.report-row:active { background: var(--bg); }
.row-week { font-weight: 600; font-size: 15px; }
.row-week small { font-weight: 400; color: var(--muted); }
.row-amounts { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.row-diff { grid-row: 1 / span 2; grid-column: 2; align-self: center; text-align: right; font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.row-diff .status { display: block; font-size: 12px; font-weight: 500; color: var(--muted); }
.row-diff .status-icon { font-style: normal; margin-right: 3px; }
.row-diff.over .status-icon { color: var(--bad); }
.row-diff.under .status-icon { color: var(--good); }

.field-hint { margin: 8px 4px 0; font-size: 13px; }
.reset-btn { margin-top: 20px; }
.reset-btn[hidden] { display: none; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
