/* Piso design tokens. Passbook-ledger look: cool paper, peso-blue ink, rounded numerals for money.
   Colour never carries meaning alone: every status also has words. */
:root {
  color-scheme: light dark;
  --paper: #F2F4F8;
  --surface: #FFFFFF;
  --ink: #14213D;
  --ink-2: #4A5772;
  --line: #DCE1EB;
  --blue: #1F4E9C;
  --on-blue: #FFFFFF;
  --blue-soft: #E3ECFA;
  --good: #0B6B4A;
  --good-soft: #DDF3EA;
  --warn: #8F4005;
  --warn-soft: #FBEBD5;
  --bad: #A82A22;
  --bad-soft: #FBE1DE;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --num: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --r-s: 10px;
  --r-m: 14px;
  --r-l: 22px;
  --tab-h: 60px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0D1422;
    --surface: #172033;
    --ink: #E9EEF8;
    --ink-2: #A9B4CB;
    --line: #2A3650;
    --blue: #8DB4FF;
    --on-blue: #0D1422;
    --blue-soft: #1C2D50;
    --good: #5AD3A4;
    --good-soft: #123B30;
    --warn: #F2B764;
    --warn-soft: #44300F;
    --bad: #FF9288;
    --bad-soft: #4A1C19;
  }
}

*, *::before, *::after { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.45 var(--font);
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

h1 { margin: 0; font-size: 28px; line-height: 1.15; font-weight: 700; letter-spacing: -0.01em; }
h2 { margin: 28px 0 8px; font-size: 17px; line-height: 1.3; font-weight: 600; }
p { margin: 0 0 12px; }
small { font-size: 13px; }
a { color: var(--blue); }
.num { font-family: var(--num); font-variant-numeric: tabular-nums; }
.muted { color: var(--ink-2); }
.nowrap { white-space: nowrap; }
[hidden] { display: none !important; }

button, a, label, select, input { touch-action: manipulation; }
button { cursor: pointer; font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }

/* ---------- page frame ---------- */

#view {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 14px) 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 36px);
  outline: none;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
  margin-bottom: 14px;
}
.top-note { color: var(--ink-2); font-size: 14px; font-weight: 600; }
.back {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  margin-bottom: 2px;
  font-weight: 600;
  text-decoration: none;
}

/* ---------- tab bar ---------- */

.tabs {
  position: fixed;
  inset: auto 0 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  align-items: center;
  max-width: 480px;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tabs a {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 2px;
  min-height: 52px;
  color: var(--ink-2);
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
}
.tabs a[aria-current="page"] { color: var(--blue); }
.tabs svg, .tab-add svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tab-add {
  justify-self: center;
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  margin-top: -20px;
  border: 0;
  border-radius: 50%;
  background: var(--blue);
  color: var(--on-blue);
  box-shadow: 0 4px 14px rgb(20 33 61 / 0.28);
}
.tab-add svg { width: 28px; height: 28px; stroke-width: 2.2; }

/* ---------- home: the sum ---------- */

.sum {
  padding: 18px 18px 16px;
  border-radius: var(--r-l);
  background: var(--surface);
}
.sum dl { margin: 0; }
.sum div { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 3px 0; }
.sum dt { color: var(--ink-2); font-size: 15px; }
.sum dd { margin: 0; font: 600 20px/1.3 var(--num); font-variant-numeric: tabular-nums; white-space: nowrap; }
.sum .total { margin-top: 10px; padding-top: 12px; border-top: 2px solid var(--ink); }
.sum .total dt { color: var(--ink); font-weight: 600; }
.sum .total dd { font-size: 32px; font-weight: 700; letter-spacing: -0.01em; }
.sum .total.short dd { color: var(--bad); }
.sum-note { margin: 10px 0 0; font-size: 14px; font-weight: 600; }
.sum-note.short { color: var(--bad); }

.facts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 12px 0 0; }
.facts div { padding: 12px 14px; border-radius: var(--r-m); background: var(--surface); }
.facts dt { color: var(--ink-2); font-size: 13px; }
.facts dd { margin: 2px 0 0; font: 600 18px/1.3 var(--num); font-variant-numeric: tabular-nums; }

.notice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 56px;
  margin-top: 12px;
  padding: 12px 14px;
  border: 0;
  border-radius: var(--r-m);
  background: var(--blue-soft);
  color: var(--blue);
  font-weight: 600;
  text-align: left;
  text-decoration: none;
}
.notice.bad { background: var(--bad-soft); color: var(--bad); }
.notice.good { background: var(--good-soft); color: var(--good); }
.notice b { font-family: var(--num); font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- ledger and lists ---------- */

.ledger, .list {
  margin: 0;
  padding: 0;
  list-style: none;
  border-radius: var(--r-l);
  background: var(--surface);
  overflow: hidden;
}
.ledger > li + li, .list > li + li { border-top: 1px solid var(--line); }

.entry, .row {
  display: grid;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 60px;
  padding: 10px 14px;
  border: 0;
  background: none;
  text-align: left;
  text-decoration: none;
  color: inherit;
}
.entry { grid-template-columns: 44px minmax(0, 1fr) auto; }
.row { grid-template-columns: minmax(0, 1fr) auto; }
.entry b, .row b { display: block; font-weight: 600; overflow-wrap: anywhere; }
.entry small, .row small { display: block; color: var(--ink-2); }
.entry-side, .row-side { text-align: right; white-space: nowrap; }
.row-side b, .entry-side b { font-family: var(--num); font-variant-numeric: tabular-nums; }
.row-side .in { color: var(--good); }

.stub {
  display: grid;
  place-items: center;
  align-content: center;
  width: 44px;
  height: 48px;
  border-radius: var(--r-s);
  background: var(--paper);
  line-height: 1.1;
}
.stub b { font: 700 18px/1 var(--num); }
.stub span { color: var(--ink-2); font-size: 12px; font-weight: 600; }

.runs-out {
  padding: 7px 14px;
  border-top: 2px solid var(--bad);
  background: var(--bad-soft);
  color: var(--bad);
  font-size: 13px;
  font-weight: 700;
}
.short .entry-side small { color: var(--bad); font-weight: 600; }

.row-actions { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 14px 12px; }

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--blue-soft);
  color: var(--blue);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }

.empty { padding: 28px 18px; border-radius: var(--r-l); background: var(--surface); text-align: center; color: var(--ink-2); }
.empty b { display: block; margin-bottom: 4px; color: var(--ink); font-size: 17px; }
.empty .btn { margin-top: 12px; }

/* ---------- buttons ---------- */

.primary {
  display: block;
  width: 100%;
  min-height: 52px;
  border: 0;
  border-radius: var(--r-m);
  background: var(--blue);
  color: var(--on-blue);
  font-size: 17px;
  font-weight: 600;
}
.primary:disabled { background: var(--line); color: var(--ink-2); cursor: not-allowed; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--r-s);
  background: var(--surface);
  color: var(--ink);
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
}
.btn.strong { border-color: var(--blue); color: var(--blue); }
.ghost { min-height: 44px; padding: 0 8px; border: 0; background: none; color: var(--blue); font-size: 16px; font-weight: 600; }
.danger { width: 100%; min-height: 48px; margin-top: 8px; border: 0; background: none; color: var(--bad); font-size: 16px; font-weight: 600; }
.stack { display: grid; gap: 10px; margin-top: 14px; }

/* ---------- budget ---------- */

.month-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.month-nav button { width: 44px; height: 44px; border: 1px solid var(--line); border-radius: 50%; background: var(--surface); font-size: 20px; line-height: 1; }
.month-nav h1 { font-size: 22px; }

.rta { padding: 14px 16px; border-radius: var(--r-l); background: var(--blue); color: var(--on-blue); }
.rta b { display: block; font: 700 30px/1.2 var(--num); font-variant-numeric: tabular-nums; }
.rta.zero { background: var(--good-soft); color: var(--good); }
.rta.neg { background: var(--bad-soft); color: var(--bad); }

.cat { grid-template-columns: minmax(0, 1fr) auto; row-gap: 6px; }
.bar { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 2px; background: var(--blue); }
.bar.over i { background: var(--bad); }
.bar.good i { background: var(--good); }

/* ---------- sheet (full-height so the keyboard never covers the fields) ---------- */

#sheet {
  width: 100%;
  max-width: 480px;
  height: calc(100dvh - env(safe-area-inset-top) - 18px);
  max-height: none;
  margin: auto auto 0;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 28px);
  border: 0;
  border-radius: var(--r-l) var(--r-l) 0 0;
  background: var(--paper);
  color: var(--ink);
  overflow-y: auto;
  overscroll-behavior: contain;
}
#sheet::backdrop { background: rgb(8 14 28 / 0.55); }
.sheet-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 56px;
  background: var(--paper);
}
.sheet-head h2 { margin: 0; font-size: 20px; }

@media (prefers-reduced-motion: no-preference) {
  #sheet[open] { animation: rise 0.22s cubic-bezier(0.2, 0.8, 0.2, 1); }
  @keyframes rise { from { transform: translateY(36px); opacity: 0; } }
  #toast { transition: opacity 0.18s, transform 0.18s; }
}

/* ---------- forms ---------- */

.field { margin-bottom: 14px; }
.field > label, .label { display: block; margin-bottom: 6px; color: var(--ink-2); font-size: 14px; font-weight: 600; }
input, select, textarea {
  display: block;
  width: 100%;
  min-width: 0;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--r-s);
  background: var(--surface);
  color: var(--ink);
  font: 16px var(--font);
}
/* Own arrow so selects keep their 48px height in every browser. */
select {
  -webkit-appearance: none;
  appearance: none;
  padding-right: 38px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%237A869E' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 13px center;
  background-size: 12px 8px;
  text-overflow: ellipsis;
}
input[type="file"] { padding: 10px; }
input[aria-invalid="true"] { border-color: var(--bad); }
.hint { margin: 5px 0 0; color: var(--ink-2); font-size: 13px; }
.error { margin: 5px 0 0; color: var(--bad); font-size: 14px; font-weight: 600; }

.amount {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 14px;
  border: 1px solid var(--line);
  border-radius: var(--r-m);
  background: var(--surface);
}
.amount:focus-within { outline: 3px solid var(--blue); outline-offset: 2px; }
.amount span { color: var(--ink-2); font: 600 26px var(--num); }
.amount input { min-height: 60px; padding: 0; border: 0; background: none; font: 700 36px var(--num); outline: none; }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; margin-bottom: 12px; padding: 3px; border-radius: var(--r-s); background: var(--line); }
.seg label { position: relative; display: grid; place-items: center; min-height: 44px; border-radius: 8px; font-size: 15px; font-weight: 600; }
.seg label:has(input:checked) { background: var(--surface); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 14px; }
.chips label {
  position: relative;
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 15px;
  font-weight: 500;
}
.chips label:has(input:checked) { border-color: var(--blue); background: var(--blue); color: var(--on-blue); font-weight: 600; }
.seg input, .chips input { position: absolute; inset: 0; width: 100%; height: 100%; min-height: 0; margin: 0; padding: 0; opacity: 0; }
.seg label:has(input:focus-visible), .chips label:has(input:focus-visible) { outline: 3px solid var(--blue); outline-offset: 2px; }

/* Only the fields for the chosen entry type are shown. */
.if-expense, .if-income, .if-transfer, .if-not-transfer { display: none; }
form:has([name="type"][value="expense"]:checked) .if-expense,
form:has([name="type"][value="income"]:checked) .if-income,
form:has([name="type"][value="transfer"]:checked) .if-transfer,
form:has([name="type"][value="expense"]:checked) .if-not-transfer,
form:has([name="type"][value="income"]:checked) .if-not-transfer { display: block; }

details { margin: 14px 0; }
summary { min-height: 44px; display: flex; align-items: center; color: var(--blue); font-weight: 600; cursor: pointer; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- toast ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 22px);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: calc(100% - 32px);
  padding: 4px 14px;
  min-height: 44px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--paper);
  font-size: 15px;
  font-weight: 600;
  opacity: 0;
  transform: translate(-50%, 16px);
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { min-height: 44px; padding: 0 8px; border: 0; background: none; color: inherit; text-decoration: underline; }

/* ---------- setup, import, charts ---------- */

.steps { margin: 0; padding: 0; list-style: none; counter-reset: step; border-radius: var(--r-l); background: var(--surface); overflow: hidden; }
.steps li + li { border-top: 1px solid var(--line); }
.steps button, .steps a {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 56px;
  padding: 8px 14px;
  border: 0;
  background: none;
  color: inherit;
  text-align: left;
  text-decoration: none;
}
.steps button::before, .steps a::before {
  counter-increment: step;
  content: counter(step);
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--blue-soft);
  color: var(--blue);
  font: 700 14px var(--num);
}
.steps .done::before { content: "✓"; background: var(--good-soft); color: var(--good); }
.steps .done b { color: var(--ink-2); font-weight: 500; }

.guide { margin: 0; padding-left: 22px; }
.guide li { margin-bottom: 8px; }

table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left; }
td.num, th.num { text-align: right; }
.card { padding: 14px 16px; border-radius: var(--r-l); background: var(--surface); }
.card + .card { margin-top: 12px; }

.chart { display: block; width: 100%; height: auto; }
.chart text { fill: var(--ink); font: 13px var(--font); }
.chart .axis { fill: var(--ink-2); font-size: 12px; }
.chart .value { font-family: var(--num); font-variant-numeric: tabular-nums; font-weight: 600; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 8px 0 0; padding: 0; list-style: none; color: var(--ink-2); font-size: 13px; }
.legend i { display: inline-block; width: 10px; height: 10px; margin-right: 6px; border-radius: 3px; vertical-align: baseline; }

/* Chart marks. Series colours pass the colour-blind separation and contrast checks on each theme's surface. */
:root { --series-1: #1F4E9C; --series-2: #D9731A; }
@media (prefers-color-scheme: dark) { :root { --series-1: #5F8EE0; --series-2: #CC7B33; } }

.hbars { margin: 0; padding: 0; list-style: none; }
.hbars li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; padding: 7px 0; }
.hbars .num { font-weight: 600; }
.hbar { grid-column: 1 / -1; height: 8px; }
.hbar i { display: block; height: 8px; border-radius: 0 4px 4px 0; background: var(--series-1); }

.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .zero { stroke: var(--ink-2); stroke-width: 1; }
.chart .s1 { fill: var(--series-1); }
.chart .s2 { fill: var(--series-2); }
.chart .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.legend .s1 { background: var(--series-1); }
.legend .s2 { background: var(--series-2); }
