/* Household Budget — the look.
   Colours are tokens on :root so light and dark mode only swap values. */

:root {
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --surface-2: #efebe1;
  --ink: #1f2a24;
  --ink-soft: #5b6560;
  --line: #ddd6c7;
  --accent: #2f6b4f;
  --accent-ink: #ffffff;
  --accent-soft: #dcebe2;
  --good: #2f7a4e;
  --good-soft: #def0e3;
  --warn: #a8640d;
  --warn-soft: #f8e9cf;
  --bad: #b23b2e;
  --bad-soft: #f7dcd6;
  --t-color: #3d6fa8;  /* Tõnis */
  --k-color: #a2477a;  /* Kristiina */
  --w-color: #6b6f2f;  /* wallet */
  /* Corner shapes, used everywhere: rectangles with slightly rounded corners. */
  --radius: 12px;      /* panels, cards, dialogs */
  --radius-sm: 9px;    /* boxes inside panels */
  --r-control: 10px;   /* buttons, inputs, chips */
  --r-tag: 6px;        /* small tags and status labels */
  --shadow: 0 1px 2px rgb(31 42 36 / 6%), 0 4px 14px rgb(31 42 36 / 6%);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --num: var(--font);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151a17;
    --surface: #1d2420;
    --surface-2: #26302a;
    --ink: #e9ece6;
    --ink-soft: #a3ada6;
    --line: #34403a;
    --accent: #6fbf93;
    --accent-ink: #10231a;
    --accent-soft: #24392d;
    --good: #7fd19f;
    --good-soft: #20372a;
    --warn: #e7b062;
    --warn-soft: #3a2f1c;
    --bad: #f08a7c;
    --bad-soft: #3d2420;
    --t-color: #8ab4e8;
    --k-color: #e391be;
    --w-color: #c4c97a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}

.skip-link { position: absolute; left: -999px; }
.skip-link:focus { left: 8px; top: 8px; z-index: 50; background: var(--surface); padding: 8px 12px; border-radius: 8px; }

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

button, input, select { font: inherit; color: inherit; }

button { cursor: pointer; }

/* Interactive areas: no text selection or long-press menu on touch. */
button, .tabs a, .row-btn, .seg label {
  touch-action: manipulation; /* react to taps straight away, never wait for a double tap */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

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

.topbar {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}

.topbar-inner {
  max-width: 760px; margin: 0 auto;
  padding: 10px 16px;
  display: flex; align-items: center; justify-content: center; gap: 12px;
  min-height: 61px;
}

.month-nav { display: flex; align-items: center; gap: 4px; min-width: 0; }

.month-title {
  border: 0; background: none; padding: 6px 8px; border-radius: var(--r-control);
  font-size: 1.15rem; font-weight: 700; letter-spacing: -0.01em; white-space: nowrap;
}
.month-title:hover { background: var(--surface-2); }

.icon-btn {
  width: 40px; height: 40px; border-radius: var(--r-control);
  border: 1px solid var(--line); background: var(--surface);
  font-size: 1.4rem; line-height: 1; display: grid; place-items: center;
}
.icon-btn:hover { background: var(--surface-2); }

.step {
  display: inline-grid; place-items: center; width: 22px; height: 22px; margin-right: 6px;
  border-radius: var(--r-tag); background: var(--accent-soft); color: var(--accent);
  font-size: 0.78rem; font-weight: 800; vertical-align: 2px;
}
.sync-alert {
  display: block; width: calc(100% - 32px); max-width: 728px; margin: 10px auto 0; padding: 8px 12px;
  border: 0; border-radius: var(--r-control); background: var(--warn-soft); color: var(--warn);
  font-weight: 600; font-size: 0.88rem; text-align: left;
}
.sync-alert[hidden] { display: none; }
.chip-version { opacity: 0.8; }
.sync-chip {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: var(--r-control); padding: 6px 12px; font-size: 0.82rem; color: var(--ink-soft);
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
.sync-chip::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--ink-soft); }
.sync-chip[data-kind="ok"]::before { background: var(--good); }
.sync-chip[data-kind="syncing"]::before { background: var(--warn); animation: pulse 1s infinite; }
.sync-chip[data-kind="error"]::before { background: var(--bad); }
.sync-chip[data-kind="offline"]::before { background: var(--warn); }
@keyframes pulse { 50% { opacity: 0.3; } }

/* Tabs: bottom bar on phones, under the header on wide screens. */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 9px 4px 10px; color: var(--ink-soft); text-decoration: none; font-size: 0.78rem; font-weight: 600;
}
.tab-icon { font-size: 1.2rem; line-height: 1; }
.tabs a { position: relative; }
.tab-badge {
  position: absolute; top: 4px; left: calc(50% + 8px); min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: var(--r-tag); background: var(--warn); color: var(--surface); font-size: 0.7rem; font-weight: 800;
  display: grid; place-items: center; line-height: 1;
}
.tab-badge[hidden] { display: none; }
@media (min-width: 800px) { .tab-badge { position: static; } }
.tabs a[aria-current="page"] { color: var(--accent); }

@media (min-width: 800px) {
  body { padding-bottom: 32px; }
  .tabs {
    position: static; max-width: 760px; margin: 0 auto; padding: 0 16px;
    display: flex; gap: 4px; background: none; border: 0;
  }
  .tabs a { flex-direction: row; gap: 8px; padding: 8px 14px 10px; font-size: 0.92rem; border-bottom: 3px solid transparent; }
  .tabs a[aria-current="page"] { border-bottom-color: var(--accent); }
}

.banner {
  max-width: 760px; margin: 12px auto 0; padding: 10px 14px;
  background: var(--accent-soft); border-radius: var(--radius-sm);
  width: calc(100% - 32px);
}

/* ---------- layout ---------- */

/* touch-action: pan-y = the browser handles up/down scrolling; sideways swipes are ours. */
main { max-width: 760px; margin: 0 auto; padding: 16px; outline: none; touch-action: pan-y; }
html, body { overflow-x: hidden; }
body { min-height: 100dvh; }

.stack > * + * { margin-top: 14px; }

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

.card h2 { margin: 0 0 10px; font-size: 1rem; letter-spacing: 0.01em; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }

.muted { color: var(--ink-soft); }
.small { font-size: 0.86rem; }
.num, .amount, .sumtable td:last-child, input.money { font-variant-numeric: tabular-nums; }

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

.btn {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: var(--r-control); padding: 10px 16px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); }
.btn.small-btn { min-height: 36px; padding: 6px 12px; font-size: 0.88rem; }
.btn kbd { font-size: 0.72rem; opacity: 0.75; color: inherit; background: transparent; border-color: currentColor; }
@media (hover: none) { .btn kbd { display: none; } } /* no keyboard hints on touch phones */

.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }
.btn-row.grow > .btn { flex: 1 1 140px; }

.link-btn { border: 0; background: none; color: var(--accent); font-weight: 700; padding: 0; text-decoration: underline; }

kbd {
  font-family: var(--num); font-size: 0.8rem;
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px;
  padding: 1px 6px; background: var(--surface-2);
}

/* ---------- wallet hero ---------- */

.hero-top { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.page-top { display: flex; justify-content: flex-end; }
.hero-top .sync-chip { font-size: 0.75rem; padding: 4px 9px; }
.version-tag { font-size: 0.72rem; color: var(--ink-soft); background: var(--surface-2); border-radius: var(--r-tag); padding: 2px 8px; white-space: nowrap; }
.hero .label { font-size: 0.85rem; color: var(--ink-soft); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.hero .big { font-size: clamp(2.2rem, 9vw, 3rem); font-weight: 750; letter-spacing: -0.03em; line-height: 1.1; margin: 4px 0 2px; }
.hero .big.negative { color: var(--bad); }

.meter { position: relative; height: 8px; background: var(--surface-2); border-radius: var(--r-tag); margin: 18px 0 8px; overflow: visible; }
/* Calm when all is well; orange/red stay strong so being over plan stands out. */
.meter-fill { position: absolute; inset: 0 auto 0 0; border-radius: var(--r-tag); background: color-mix(in srgb, var(--good) 45%, var(--surface-2)); max-width: 100%; }
.meter-fill.warn { background: var(--warn); }
.meter-fill.bad { background: var(--bad); }
.meter-mark { position: absolute; top: -4px; bottom: -4px; width: 2px; border-radius: 1px; background: var(--ink-soft); }
.meter-legend { display: flex; justify-content: space-between; font-size: 0.8rem; color: var(--ink-soft); }

.hero-bottom { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; flex-wrap: nowrap; }
.hero-bottom .pace-pill { flex: 0 1 auto; min-width: 0; }
.quiet-btn {
  margin-left: auto; flex: none; white-space: nowrap; border: 0; background: none; padding: 6px 2px; min-height: 36px;
  color: var(--ink-soft); font-size: 0.85rem; font-weight: 600;
}
.quiet-btn:hover { color: var(--ink); text-decoration: underline; }
.pace-pill {
  display: inline-flex; flex-direction: column; gap: 0;
  padding: 6px 12px; border-radius: var(--r-control); font-weight: 650; margin-top: 12px;
}
.pace-pill .pace-sub { font-weight: 500; font-size: 0.85rem; opacity: 0.85; }
.pace-pill.good { background: var(--good-soft); color: var(--good); }
.pace-pill.warn { background: var(--warn-soft); color: var(--warn); }
.pace-pill.bad { background: var(--bad-soft); color: var(--bad); }

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 560px) { .stats { grid-template-columns: repeat(3, 1fr); } }
.stat { background: var(--surface-2); border-radius: var(--radius-sm); padding: 10px 12px; }
.stat .k { font-size: 0.78rem; color: var(--ink-soft); }
.stat .v { font-size: 1.15rem; font-weight: 700; }

/* ---------- lists ---------- */

.list { list-style: none; margin: 0; padding: 0; }
.list li + li { border-top: 1px solid var(--line); }

.row {
  display: flex; align-items: center; gap: 10px; padding: 10px 2px; min-height: 52px;
}
.row-btn {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px;
  border: 0; background: none; text-align: left; padding: 4px 2px; border-radius: var(--r-tag);
}
.row-btn:hover { background: var(--surface-2); }
.row .grow { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.row .grow br { display: none; }
/* Titles wrap onto a second line instead of running under the amount. */
.row .title { font-weight: 600; overflow-wrap: anywhere; line-height: 1.3; }
.row .sub { font-size: 0.82rem; color: var(--ink-soft); }
.row .amount { flex: none; margin-left: auto; padding-left: 8px; text-align: right; font-weight: 650; white-space: nowrap; }
.row.is-paid .title { text-decoration: line-through; color: var(--ink-soft); }

.check {
  width: 26px; height: 26px; flex: none; accent-color: var(--accent); cursor: pointer;
}

.badge {
  display: inline-grid; place-items: center; flex: none;
  min-width: 30px; height: 24px; padding: 0 6px; border-radius: var(--r-tag);
  font-size: 0.72rem; font-weight: 800; letter-spacing: 0.04em;
  background: var(--surface-2); color: var(--ink-soft);
}
.badge.t { background: color-mix(in srgb, var(--t-color) 18%, transparent); color: var(--t-color); }
.badge.k { background: color-mix(in srgb, var(--k-color) 18%, transparent); color: var(--k-color); }
.badge.wallet { background: color-mix(in srgb, var(--w-color) 20%, transparent); color: var(--w-color); }

.empty { padding: 14px 4px; color: var(--ink-soft); }

/* ---------- money summary table ---------- */

.sumtable { width: 100%; border-collapse: collapse; }
.sumtable td { padding: 7px 0; border-top: 1px solid var(--line); }
.sumtable tr:first-child td { border-top: 0; }
.sumtable td:last-child { text-align: right; font-family: var(--num); font-variant-numeric: tabular-nums; white-space: nowrap; }
.sumtable tr.total td { font-weight: 750; border-top: 2px solid var(--ink); }

.revolut-btn { display: flex; width: 100%; margin-top: 10px; text-decoration: none; }
.deposits { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.deposit {
  border-radius: var(--radius-sm); padding: 12px; background: var(--surface-2);
  border: 1px solid transparent; text-align: left; display: flex; flex-direction: column; gap: 2px;
  min-width: 0; color: var(--ink);
}
.deposit:active { transform: scale(0.98); }
.deposit .status {
  align-self: flex-start; margin-top: 8px; padding: 3px 10px; border-radius: var(--r-tag);
  font-size: 0.8rem; font-weight: 700; background: var(--warn-soft); color: var(--warn);
}
.deposit.is-done { border-color: var(--good); }
.deposit.is-done .status { background: var(--good-soft); color: var(--good); }
.deposit.is-done .v { color: var(--ink-soft); }
.sort-row { flex-wrap: wrap; }
.sort-row .choice { display: flex; gap: 6px; width: 100%; }
.chip-btn {
  flex: 1; min-height: 40px; border-radius: var(--r-control); border: 1px solid var(--line);
  background: var(--surface); font-weight: 650; font-size: 0.9rem;
}
.chip-btn.common { border-color: var(--accent); color: var(--accent); }
.chip-btn.suggested { outline: 2px solid currentColor; outline-offset: -1px; }
.chip-btn.common.suggested { background: var(--accent); color: var(--accent-ink); }
.suggest { font-size: 0.75rem; font-weight: 700; padding: 1px 6px; border-radius: var(--r-tag); background: var(--surface-2); }
.suggest.common { color: var(--accent); }
.sorted-away summary { cursor: pointer; list-style-position: inside; }
.sorted-away[open] summary { margin-bottom: 6px; }
.howto summary { cursor: pointer; font-weight: 600; color: var(--accent); }
.howto ol { padding-left: 20px; margin: 8px 0; }
.import-revolut { display: flex; width: 100%; text-decoration: none; margin-top: 4px; }
#form-dialog:has(.import-revolut) .dialog-actions .btn.primary { background: var(--surface); border-color: var(--line); color: var(--ink); }
.import-steps { padding-left: 20px; margin: 12px 0 8px; line-height: 1.5; }
.withdraw-card .big { font-size: clamp(2rem, 8vw, 2.6rem); }
.pending-line { display: block; margin-top: 10px; font-size: 0.88rem; font-weight: 600; color: var(--accent); text-decoration: none; }
.notice {
  display: block; padding: 10px 14px; border-radius: var(--radius-sm);
  background: var(--warn-soft); color: var(--warn); font-weight: 600; font-size: 0.9rem; text-decoration: none;
}
.lock-card.is-locked { border-color: var(--good); }
.notice-btn { width: 100%; border: 0; text-align: left; font: inherit; font-weight: 600; font-size: 0.9rem; cursor: pointer; }
.close-card { border-color: var(--accent); }
.closed-card { border-color: var(--good); }
.chip-btn[aria-disabled="true"] { opacity: 0.45; border-style: dashed; }
.votes { list-style: none; padding: 0; margin: 8px 0 12px; display: grid; gap: 4px; font-size: 0.9rem; }
.vote::before { content: '○ '; color: var(--ink-soft); }
.vote.approved::before { content: '● '; color: var(--good); }
.vote.stale::before, .vote.unlock::before, .vote.reopen::before { content: '● '; color: var(--warn); }
.lock-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row-btn.is-static { cursor: default; }
.row-btn.is-static:hover { background: none; }
input[readonly] { background: var(--surface-2); color: var(--ink-soft); border-style: dashed; }
.private-line {
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
  margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); font-weight: 700;
}
.private-line .num { font-size: 1.15rem; }
.deposit.t { border-left: 5px solid var(--t-color); }
.deposit.k { border-left: 5px solid var(--k-color); }
.deposit .who { font-weight: 700; }
.deposit .v { font-size: 1.45rem; font-weight: 750; letter-spacing: -0.02em; }

.inline-field { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.inline-field label { font-weight: 600; flex: 1 1 160px; }
.inline-field input { width: 150px; }

/* ---------- forms and dialog ---------- */

input[type="text"], input[type="date"], input[type="month"], input[type="number"], input[type="url"], input[type="password"], input[inputmode], select {
  width: 100%; min-height: 46px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: var(--r-control); background: var(--surface);
}
input.money { font-family: var(--num); text-align: right; }

dialog {
  border: 0; border-radius: var(--radius); padding: 0; width: min(460px, calc(100% - 24px));
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgb(0 0 0 / 30%);
  /* Never taller than the visible area (which shrinks when the keyboard opens); scroll inside instead. */
  max-height: calc(100dvh - 24px); overflow-y: auto; overscroll-behavior: contain;
}
/* Phones: forms sit near the top, so the field you type in stays above the keyboard. */
@media (max-width: 799px) {
  dialog { margin: 8px auto auto; max-height: calc(100dvh - 16px); }
  dialog form { padding-top: 14px; }
  dialog h2 { margin-bottom: 10px; }
  .field { margin-bottom: 10px; }
}
dialog::backdrop { background: rgb(15 20 17 / 45%); }
dialog form { padding: 18px 18px 0; }
dialog h2 { margin: 0 0 14px; font-size: 1.15rem; }
.field { margin-bottom: 14px; }
.field[hidden] { display: none; }
.field > label, .field > .label { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 6px; }
.field .hint { font-size: 0.8rem; color: var(--ink-soft); margin-top: 4px; }
.form-error { color: var(--bad); font-size: 0.88rem; min-height: 1.2em; margin: 0 0 8px; }
.dialog-actions {
  display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin: 6px -18px 0; padding: 12px 18px 18px;
  /* Pinned to the bottom of the form: always visible, even when the keyboard leaves little room. */
  position: sticky; bottom: 0; background: var(--surface); border-top: 1px solid transparent;
}
dialog.is-scrolling .dialog-actions { border-top-color: var(--line); }
.dialog-actions .spacer { flex: 1; }

/* Segmented choice (e.g. who pays). */
.seg { display: flex; border: 1px solid var(--line); border-radius: var(--r-control); padding: 3px; gap: 3px; flex-wrap: wrap; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label {
  flex: 1; text-align: center; padding: 8px 10px; border-radius: calc(var(--r-control) - 3px); cursor: pointer;
  font-weight: 600; font-size: 0.9rem; color: var(--ink-soft); min-width: 0;
}
.seg input:checked + label { background: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + label { outline: 3px solid var(--accent); outline-offset: 1px; }

.check-field { display: flex; align-items: center; gap: 10px; font-weight: 600; }

/* ---------- misc ---------- */

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--surface-2); color: var(--ink); border: 1px solid var(--line);
  padding: 12px 14px; border-radius: var(--r-control); box-shadow: 0 6px 24px rgb(0 0 0 / 25%);
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 60;
  display: flex; gap: 14px; align-items: center; justify-content: space-between;
  width: max-content; max-width: min(520px, calc(100% - 32px));
  font-size: 0.92rem; line-height: 1.35; overflow-wrap: anywhere;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); pointer-events: auto; }
.toast-buttons { flex: none; display: flex; gap: 14px; }
.toast button { flex: none; border: 0; background: none; color: var(--accent); font-weight: 700; text-decoration: underline; padding: 4px 0; }
@media (min-width: 800px) { .toast { bottom: 24px; } }

.shortcut-list { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; align-items: center; margin: 0; }
.shortcut-list dt { white-space: nowrap; }
.shortcut-list dd { margin: 0; }

.prose p { margin: 0 0 8px; }
.prose ul { margin: 0 0 8px; padding-left: 20px; }

/* Screen change after a swipe: slide in from the side it came from. */
main.slide-from-right { animation: slide-from-right 0.18s ease-out; }
main.slide-from-left { animation: slide-from-left 0.18s ease-out; }
@keyframes slide-from-right { from { transform: translateX(24px); opacity: 0.4; } }
@keyframes slide-from-left { from { transform: translateX(-24px); opacity: 0.4; } }

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