:root {
  --bg: #14151a;
  --card: #1d1f27;
  --card-2: #23252f;
  --text: #f2f3f7;
  --text-dim: #8b8d9a;
  --accent: #5ee0a3;
  --accent-dim: #3a8f6c;
  --on-accent: #0d1410;
  --over: #ff6b6b;
  --border: #2c2e39;
  --scrim: rgba(0, 0, 0, 0.55);
  --radius: 18px;
  color-scheme: dark;
}

/* Light palette: applied when the OS asks for light and no explicit choice
   was made, or when the user picked light directly. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f5f8;
    --card: #ffffff;
    --card-2: #eceef3;
    --text: #16181d;
    --text-dim: #6b6f7d;
    --accent: #10a06a;
    --accent-dim: #0c7d52;
    --on-accent: #ffffff;
    --over: #d93f3f;
    --border: #e1e4ea;
    --scrim: rgba(20, 21, 26, 0.32);
    color-scheme: light;
  }
}

:root[data-theme="light"] {
  --bg: #f4f5f8;
  --card: #ffffff;
  --card-2: #eceef3;
  --text: #16181d;
  --text-dim: #6b6f7d;
  --accent: #10a06a;
  --accent-dim: #0c7d52;
  --on-accent: #ffffff;
  --over: #d93f3f;
  --border: #e1e4ea;
  --scrim: rgba(20, 21, 26, 0.32);
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  padding-bottom: env(safe-area-inset-bottom);
}

button { font-family: inherit; cursor: pointer; }

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 14px) 18px 110px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.icon-btn {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.icon-btn:active { background: var(--card-2); }
.icon-btn svg { width: 18px; height: 18px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.daynav {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  width: 40px;
  height: 40px;
  border-radius: 12px;
  font-size: 20px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.daynav:active { background: var(--card-2); }
.daynav:disabled { opacity: 0.35; }

.dayinfo { text-align: center; }
.daylabel { font-size: 17px; font-weight: 600; }
.datelabel { font-size: 13px; color: var(--text-dim); margin-top: 2px; }

.budget-card {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 0 4px;
}

.ring {
  width: 210px;
  height: 210px;
  transform: rotate(-90deg);
}

.ring-track {
  fill: none;
  stroke: var(--card-2);
  stroke-width: 10;
}

.ring-fill {
  fill: none;
  stroke: var(--accent);
  stroke-width: 10;
  stroke-linecap: round;
  stroke-dasharray: 326.7256;
  stroke-dashoffset: 326.7256;
  transition: stroke-dashoffset 0.4s ease, stroke 0.4s ease;
}

.budget-center {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.remaining-value {
  font-size: 44px;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.remaining-label {
  font-size: 13px;
  color: var(--text-dim);
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.stat {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 8px;
  text-align: center;
}

.stat-value {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat-key {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 2px;
}

.meals-section { flex: 1; }

.meals-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.meals-header h2 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
  padding: 4px;
}

.meals-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.meal-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 11px 14px;
}

.meal-main { min-width: 0; }

.meal-name {
  display: block;
  font-size: 15px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meal-detail {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}

.meal-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.meal-cal {
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
}

.meal-delete {
  background: var(--card-2);
  border: none;
  color: var(--text-dim);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  font-size: 15px;
  line-height: 1;
}

.meal-delete:active { color: var(--over); }

.empty-state {
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  padding: 24px 0;
  display: none;
}

/* Bottom dock */
.dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 14px 18px calc(16px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 62%, transparent);
  display: flex;
  justify-content: center;
  pointer-events: none;
}

.add-cta {
  pointer-events: auto;
  width: 100%;
  max-width: 444px;
  background: var(--accent);
  color: var(--on-accent);
  border: none;
  border-radius: 16px;
  padding: 15px;
  font-size: 16px;
  font-weight: 700;
}

.add-cta:active { background: var(--accent-dim); }
.add-cta .plus { font-size: 19px; font-weight: 700; margin-right: 2px; }

/* Add sheet */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--scrim);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sheet.hidden { display: none; }

.sheet-card {
  width: 100%;
  max-width: 480px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 22px 22px 0 0;
  padding: 8px 18px calc(18px + env(safe-area-inset-bottom));
  max-height: 92vh;
  overflow-y: auto;
  animation: sheet-up 0.22s ease;
}

@keyframes sheet-up {
  from { transform: translateY(18px); opacity: 0.6; }
  to { transform: translateY(0); opacity: 1; }
}

.sheet-grab {
  width: 38px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 4px auto 14px;
}

.seg {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  background: var(--card-2);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 14px;
}

.seg-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  border-radius: 9px;
  padding: 9px 4px;
  font-size: 13px;
  font-weight: 600;
}

.seg-btn.active {
  background: var(--card);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.18);
}

.sheet-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.field {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 14px;
  padding: 13px 14px;
  font-size: 16px;
  font-family: inherit;
}

.field:focus { outline: none; border-color: var(--accent-dim); }
.field::placeholder { color: var(--text-dim); }

.field.big {
  font-size: 24px;
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
  padding: 14px;
}

.mode-fields { display: none; flex-direction: column; gap: 10px; }
.mode-fields.active { display: flex; }

.field-row { display: flex; gap: 10px; }
.field-wrap { flex: 1; min-width: 0; display: block; }

.field-label {
  display: block;
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0 0 5px 3px;
}

.chips { display: flex; flex-wrap: wrap; gap: 7px; }

.chip {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 13px;
  font-size: 13px;
  font-weight: 500;
}

.chip:active { background: var(--card-2); }
.chip .chip-cal { color: var(--text-dim); margin-left: 6px; font-variant-numeric: tabular-nums; }

.total-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 2px 4px;
}

.total-label {
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.total-value {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.sheet-actions { display: flex; gap: 8px; margin-top: 4px; }

.recents { margin-top: 18px; }
.recents.hidden { display: none; }

.recents-title {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 9px;
}

/* Budget modal */
.modal {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 30;
}

.modal.hidden { display: none; }

.modal-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  width: 100%;
  max-width: 320px;
}

.modal-card h3 {
  margin: 0 0 14px;
  font-size: 16px;
}

.modal-card input {
  width: 100%;
  background: var(--card-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 18px;
  text-align: center;
  font-family: inherit;
}

.modal-card input:focus { outline: none; border-color: var(--accent-dim); }

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.modal-btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  padding: 13px;
  font-size: 15px;
  font-weight: 600;
}

.modal-btn.ghost {
  background: var(--card-2);
  color: var(--text);
}

.modal-btn.primary {
  background: var(--accent);
  color: var(--on-accent);
}

.modal-btn.primary:disabled { opacity: 0.45; }

/* Snackbar */
.snackbar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(88px + env(safe-area-inset-bottom));
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 11px 14px;
  font-size: 14px;
  max-width: min(440px, calc(100vw - 36px));
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

.snackbar.hidden { display: none; }

.snackbar span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.snack-undo {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 700;
  padding: 2px 4px;
  flex-shrink: 0;
}

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

/* categories + history */
.cats { margin-bottom: 10px; }
.chip.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.cat-head { display: flex; justify-content: space-between; list-style: none; margin: 14px 4px 4px; font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--text-dim); }
.appbar { gap: 4px; }
.appbar .brand { margin-right: auto; }
.hist-chart { width: 100%; height: auto; margin: 6px 0 10px; }
.hist-chart text { fill: var(--text-dim); font-size: 9px; text-anchor: middle; }
.hist-chart .bar { fill: var(--accent); }
.hist-chart .bar.over { fill: var(--over); }
.hist-chart .budget-line { stroke: var(--text-dim); stroke-dasharray: 3 3; stroke-width: 1; }
.hist-stats { margin: 0 0 6px; }

/* budget calculator */
.scrolly { max-height: calc(100dvh - 48px); overflow-y: auto; }
.modal-card .seg { margin-bottom: 10px; }
.pane.hidden { display: none; }
.calc-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.calc-f { display: block; text-align: left; }
.calc-f.full { margin-bottom: 4px; }
.calc-f span, .calc-sub { display: block; font-size: 12px; color: var(--text-dim); margin: 0 0 4px 2px; }
.calc-sub { margin-top: 10px; }
.calc-f input, .calc-f select { font-size: 16px; padding: 10px 12px; }
.calc-f select { width: 100%; background: var(--card-2); border: 1px solid var(--border); color: var(--text); border-radius: 12px; font-family: inherit; }
.calc-result { margin-top: 12px; padding: 12px; background: var(--card-2); border-radius: 12px; font-size: 13px; line-height: 1.5; color: var(--text-dim); }
.calc-result strong { color: var(--text); font-size: 20px; display: block; }
.calc-result .warn { color: var(--over); }

/* backup + legal */
.foot { text-align: center; padding: 18px 0 8px; }
.modal-btn.block { display: block; width: 100%; margin-bottom: 8px; }
.priv-note { font-size: 13px; line-height: 1.5; color: var(--text-dim); margin: 0 0 14px; }
.priv-note strong { color: var(--text); }
.priv-note.small { font-size: 12px; margin: 10px 0; }
.backup-info { font-size: 12px; color: var(--text-dim); min-height: 16px; }
.backup-info.err { color: var(--over); }
.legal-links { font-size: 13px; text-align: center; margin-top: 4px; }
.legal-links a { color: var(--accent); }
.legal-co { font-size: 11px; color: var(--text-dim); text-align: center; margin-top: 4px; }
a.link-btn { text-decoration: none; display: inline-block; }
.foot .dot { color: var(--text-dim); margin: 0 6px; }
