:root {
  --ground: #FBF7F4;
  --card: #FFFFFF;
  --ink: #2B2140;
  --muted: #716A85;
  --line: #ECE3EA;
  --line-strong: #DFD4E6;
  --primary: #7C3AED;
  --primary-deep: #5B21B6;
  --heat2: #FF9D73;
  --heat1: #FFE8D9;
  --red: #B42318;
  --accent: #7C3AED;      /* links and focus ring */
  --money: #D64530;       /* saved amounts */
  --gold: #F5A524;        /* streaks and today */
  --on-dark: #FFFFFF;     /* text on the dark button */
  --btn-dark: #5B21B6;
  color-scheme: light;
  --display: 'Baloo 2', system-ui, sans-serif;
  --body: Inter, system-ui, -apple-system, 'Segoe UI', sans-serif;
  --tabs-h: 68px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme=light]) {
    --ground: #17121F;
    --card: #221B2E;
    --ink: #F2EDF7;
    --muted: #AC9FC0;
    --line: #332945;
    --line-strong: #443659;
    --primary: #9F7AFF;
    --primary-deep: #7C3AED;
    --heat2: #E0744D;
    --heat1: #44241B;
    --accent: #B795FF; --money: #FF8A70; --gold: #F0B24C;
    --red: #FF8A7A;
    --on-dark: #1C1427;
    --btn-dark: #F2EDF7;
    color-scheme: dark;
  }
}
:root[data-theme=dark] {
  --ground: #17121F;
  --card: #221B2E;
  --ink: #F2EDF7;
  --muted: #AC9FC0;
  --line: #332945;
  --line-strong: #443659;
  --primary: #9F7AFF;
  --primary-deep: #7C3AED;
  --heat2: #E0744D;
  --heat1: #44241B;
  --accent: #B795FF; --money: #FF8A70; --gold: #F0B24C;
  --red: #FF8A7A;
  --on-dark: #1C1427;
  --btn-dark: #F2EDF7;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--body);
  font-size: 15px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
}

h1, h2 { margin: 0; }
h1 { font-family: var(--display); font-size: 26px; font-weight: 600; }
h2 { font-size: 16px; font-weight: 600; }
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.brand { font-family: var(--display); font-size: 20px; font-weight: 700; color: var(--ink); text-decoration: none; margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--red); font-size: 14px; margin: 0; }
.hint { color: var(--muted); font-size: 13px; margin: -8px 0 0; }

/* Buttons */
.btn {
  min-height: 44px; padding: 0 18px; border-radius: 12px; border: 1px solid transparent;
  font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.dark { background: var(--btn-dark); color: var(--on-dark); }
.btn.ghost { background: var(--card); border-color: var(--line-strong); }
.btn.danger { background: var(--card); border-color: var(--red); color: var(--red); }
.btn:disabled { opacity: .6; cursor: default; }
.link { background: none; border: 0; padding: 12px 0; color: var(--accent); font-weight: 500; cursor: pointer; text-decoration: underline; }

/* Form fields */
label, legend { font-size: 13px; font-weight: 600; padding: 0; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=search], select {
  width: 100%; height: 48px; padding: 0 14px; background: var(--card);
  border: 1px solid var(--line-strong); border-radius: 12px; font-size: 16px;
}
fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 8px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 500; font-size: 15px; }
.check input { width: 22px; height: 22px; accent-color: var(--primary); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 44px; padding: 0 16px; border-radius: 22px; background: var(--card);
  border: 1px solid var(--line-strong); font-size: 14px; font-weight: 500; cursor: pointer;
}
.chip[aria-pressed=true] { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }

/* Log in */
.auth { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.auth-card { width: 100%; max-width: 380px; display: grid; gap: 16px; }
.auth-card form { display: grid; gap: 10px; }
.auth-card .btn { margin-top: 8px; }
.auth-card .link { justify-self: start; }

/* App shell */
.top { background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.top-inner { max-width: 1120px; margin: 0 auto; padding: 10px 20px; display: flex; align-items: center; gap: 24px; }
.top-inner .btn { margin-left: auto; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; display: flex;
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabs a {
  flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  font-size: 11px; font-weight: 500; color: var(--muted); text-decoration: none;
}
.tabs a[aria-current=page] { color: var(--accent); font-weight: 600; }
.tabs svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.view {
  max-width: 1120px; margin: 0 auto; display: grid; gap: 16px;
  padding: 20px 20px calc(var(--tabs-h) + env(safe-area-inset-bottom) + 24px);
}
.view-head { display: grid; gap: 2px; }

@media (min-width: 800px) {
  .tabs { position: static; height: auto; padding: 0; border: 0; background: none; gap: 4px; }
  .tabs a { flex: none; flex-direction: row; padding: 12px 14px; font-size: 14px; }
  .tabs svg { display: none; }
  .view { padding: 32px 24px 48px; gap: 24px; }
  h1 { font-size: 32px; }
}

/* Cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 16px; display: grid; gap: 10px; align-content: start; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.grid-2 { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }

.stats { display: grid; gap: 12px; grid-template-columns: repeat(2, 1fr); }
@media (min-width: 800px) { .stats { grid-template-columns: repeat(4, 1fr); gap: 16px; } }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 14px 16px; display: grid; gap: 2px; min-width: 0; }
.stat span { font-size: 13px; color: var(--muted); }
.stat strong { font-family: var(--display); font-size: clamp(20px, 5.5vw, 30px); font-weight: 700; overflow-wrap: anywhere; }
.stat.hero { background: linear-gradient(135deg, #6D28D9 0%, #B04FD6 55%, #FF8559 118%); border-color: transparent; color: #fff; }
.stat.hero span { color: #EDEBFF; }

.progress { height: 10px; background: var(--line); border-radius: 5px; overflow: hidden; }
.progress div { height: 100%; background: linear-gradient(90deg, #FF6B54, var(--primary)); border-radius: 5px; }

/* 14-day chart */
.chart { height: 160px; display: flex; align-items: flex-end; gap: 6px; }
.chart .col { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 6px; font-size: 11px; color: var(--muted); }
.chart .bar { width: 100%; max-width: 36px; background: linear-gradient(180deg, #FF8A6B, #E85C3F); border-radius: 6px 6px 0 0; }
.chart .bar.zero { background: var(--line-strong); }
.chart .col.today { color: var(--ink); font-weight: 600; }
.chart .col.today .bar { background: linear-gradient(180deg, #F7B84B, #E9940C); }
.chart.months .col { font-size: 12px; }

/* Entry lists */
.list { list-style: none; margin: 0; padding: 0; }
.list li + li { border-top: 1px solid var(--line); }
.row {
  width: 100%; min-height: 56px; padding: 8px 0; background: none; border: 0; cursor: pointer; text-align: left;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
}
.row .what { display: grid; gap: 2px; min-width: 0; }
.row .what b { font-size: 14px; font-weight: 500; overflow-wrap: anywhere; }
.row .amt { font-weight: 600; color: var(--money); white-space: nowrap; }
.empty { color: var(--muted); margin: 0; padding: 8px 0; }

/* Calendar */
.cal-head { display: flex; justify-content: space-between; align-items: center; }
.cal-head b { font-size: 16px; font-weight: 600; }
.icon-btn { width: 44px; height: 44px; border: 0; background: none; border-radius: 22px; cursor: pointer; display: grid; place-items: center; }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.cal .wd { font-size: 12px; color: var(--muted); text-align: center; }
.cal .d { color: var(--ink); height: 44px; border-radius: 10px; border: 2px solid transparent; background: none; font-size: 14px; font-weight: 500; cursor: pointer; }
.cal .d.future { color: var(--muted); }
.cal .d.l1 { background: var(--heat1); }
.cal .d.l2 { background: var(--heat2); color: #3A1608; }
.cal .d.l3 { background: var(--primary); color: #fff; }
.cal .d[aria-pressed=true] { border-color: var(--gold); }
.legend { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.legend i { width: 16px; height: 16px; border-radius: 4px; }

/* Stacked forms on Goal and Settings */
.stack { display: grid; gap: 10px; max-width: 420px; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 6px; }

/* Dialog */
dialog {
  width: min(440px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  border: 0; border-radius: 20px; padding: 20px; background: var(--ground); color: var(--ink);
}
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop { background-color: transparent; overflow: hidden; overscroll-behavior: contain; }
dialog form { display: grid; gap: 10px; }
dialog h2 { font-family: var(--display); font-size: 20px; margin-bottom: 4px; }
.amount-row { display: flex; align-items: baseline; gap: 8px; }
.amount-row .currency { font-family: var(--display); font-size: 24px; font-weight: 600; color: var(--muted); }
.amount-row input {
  flex: 1; min-width: 0; height: 60px; padding: 0; border: 0; border-bottom: 2px solid var(--primary); border-radius: 0;
  background: none; font-family: var(--display); font-size: 44px; font-weight: 700;
}
.dialog-actions { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.dialog-actions .spacer { flex: 1; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 20; margin: 0;
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
  background: var(--btn-dark); color: var(--on-dark); padding: 12px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100vw - 32px);
}

/* Full version additions */
.field { display: grid; gap: 10px; }
.banner { margin: 0; padding: 10px 20px; background: var(--heat1); color: var(--ink); font-size: 14px; text-align: center; }
.stack-lg { display: grid; gap: 16px; align-content: start; }
.goal-mini { display: grid; gap: 6px; }
.goal-mini + .goal-mini { border-top: 1px solid var(--line); padding-top: 12px; }
.pending { font-style: italic; }

.filters { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: end; }
.filters .field { gap: 6px; }

.catbars { display: grid; gap: 12px; }
.catbar { display: grid; gap: 4px; }
.catbar .line { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; }
.catbar .line b { font-weight: 500; overflow-wrap: anywhere; }

.rows { display: grid; }
.rows > div { display: flex; align-items: center; gap: 10px; padding: 8px 0; }
.rows > div + div { border-top: 1px solid var(--line); }
.rows .grow { flex: 1; min-width: 0; }
.btn.small { min-height: 44px; padding: 0 14px; }
.file-btn { position: relative; display: inline-flex; align-items: center; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
select { appearance: auto; }

/* ---------- Animation ---------- */

/* Pop-ups: fade in and out for everyone; the slide is added below only when motion is welcome. */
dialog {
  opacity: 0;
  transition-property: opacity, transform, display, overlay;
  transition-duration: .22s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}
dialog::backdrop {
  transition-property: background-color, display, overlay;
  transition-duration: .22s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}
dialog[open]:where(:not([data-closing])) { opacity: 1; }
dialog[open]:where(:not([data-closing]))::backdrop { background-color: rgba(10, 16, 13, .55); }
@starting-style {
  dialog[open]:where(:not([data-closing])) { opacity: 0; }
  dialog[open]:where(:not([data-closing]))::backdrop { background-color: transparent; }
}

@media (prefers-reduced-motion: reduce) {
  dialog, dialog::backdrop { transition-duration: .1s; }
}

@media (prefers-reduced-motion: no-preference) {
  dialog { transform: translateY(28px); }
  dialog[open]:where(:not([data-closing])) { transform: none; }
  @starting-style {
    dialog[open]:where(:not([data-closing])) { transform: translateY(28px); }
  }

  /* Switching screens */
  .view.enter { animation: view-in .2s ease-out; }
  @keyframes view-in { from { opacity: 0; transform: translateY(8px); } }

  /* Charts and progress lines grow from nothing when a screen opens or a saving is added */
  .view.enter .bar { transform-origin: bottom; animation: grow-y .5s cubic-bezier(.2, .8, .2, 1) backwards; }
  .view.enter .progress div { transform-origin: left; animation: grow-x .6s cubic-bezier(.2, .8, .2, 1) backwards; }
  .view.enter .col:nth-child(n+5) .bar { animation-delay: .04s; }
  .view.enter .col:nth-child(n+9) .bar { animation-delay: .08s; }
  .view.enter .col:nth-child(n+12) .bar { animation-delay: .12s; }
  @keyframes grow-y { from { transform: scaleY(0); } }
  @keyframes grow-x { from { transform: scaleX(0); } }

  /* Small touches */
  .btn, .chip, .cal .d, .icon-btn { transition: transform .08s ease-out, background-color .15s, border-color .15s; }
  .btn:active, .chip:active, .cal .d:active, .icon-btn:active { transform: scale(.96); }
  .toast:not([hidden]) { animation: toast-in .22s ease-out; }
  @keyframes toast-in { from { opacity: 0; transform: translate(-50%, 14px); } }
  .banner:not([hidden]) { animation: banner-in .25s ease-out; }
  @keyframes banner-in { from { opacity: 0; transform: translateY(-8px); } }
}

/* Celebration pieces when a goal or the monthly target is reached */
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
.confetti i { position: absolute; top: 38%; left: 50%; width: 9px; height: 14px; border-radius: 2px; }

/* Scan image row in the Add saving pop-up */
.scan { display: flex; align-items: center; gap: 12px; }
.scan .muted { flex: 1; min-width: 0; }

/* Choose items pop-up */
.pick-list { max-height: 38vh; overflow: auto; overscroll-behavior: contain; border: 1px solid var(--line); border-radius: 12px; padding: 0 12px; background: var(--card); }
.pick-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.pick-row + .pick-row { border-top: 1px solid var(--line); }
.pick-row .check { flex: 1; min-width: 0; gap: 12px; font-weight: 400; }
.pick-row .grow { flex: 1; min-width: 0; display: grid; gap: 2px; font-size: 14px; overflow-wrap: anywhere; }
.pick-row .amt { font-weight: 600; white-space: nowrap; }
.pick-row input { flex: none; }
.pick-row select { flex: none; width: auto; height: 44px; padding: 0 6px; font-size: 14px; border-radius: 10px; }
.pick-summary { margin: 0; font-weight: 600; }
.dialog-actions.wrap { flex-wrap: wrap; }

/* Floating add button: on phones the top bar is gone and this is how you add a saving */
.fab {
  position: fixed; right: 16px; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px); z-index: 6;
  width: 58px; height: 58px; border-radius: 29px; border: 0; cursor: pointer;
  background: linear-gradient(135deg, #6D28D9, #B04FD6 55%, #FF8559 125%);
  color: #FFFFFF; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 10px 24px -8px rgba(109, 40, 217, .55);
}
.fab svg { width: 26px; height: 26px; }
@media (max-width: 799px) {
  .top { position: static; background: none; border: 0; }
  .top-inner { padding: 0; }
  .top-inner .brand, .top-inner > .btn { display: none; }
  .view { padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 96px); }
}
@media (min-width: 800px) {
  .fab { display: none; }
}
@media (prefers-reduced-motion: no-preference) {
  .fab { transition: transform .08s ease-out; }
  .fab:active { transform: scale(.94); }
}

/* On a phone the three choices read better stacked, with the main one on top */
@media (max-width: 460px) {
  .dialog-actions.stacked { flex-direction: column-reverse; align-items: stretch; }
  .dialog-actions.stacked .spacer { display: none; }
}
