/* ─────────────────────────────────────────────────────────────────────────
   Money — a ledger, not a dashboard.

   The visual language is a printed bank statement: warm paper, hairline rules,
   tabular figures that line up down a column, and colour used only where it
   carries meaning (money in, money out, a broken balance chain). Thai and Latin
   text sit side by side throughout, so the type stack leads with faces that
   have both.

   Layout note: grid tracks and flex items default to min-width:auto, so a wide
   child refuses to shrink and pushes the whole page sideways instead. Every
   track that can hold long content is therefore minmax(0, …) or min-width: 0.
   ───────────────────────────────────────────────────────────────────────── */

:root {
  --paper:      #faf8f4;
  --paper-2:    #ffffff;
  --paper-3:    #f2eee7;
  --ink:        #1d1a16;
  --ink-2:      #4a443c;
  --ink-3:      #6f685e;
  --rule:       #e4ded3;
  --rule-2:     #d3cabb;

  --in:         #2f7d55;
  --in-bg:      #e8f2eb;
  --out:        #b0483a;
  --out-bg:     #f8ebe8;
  --transfer:   #6f7d8c;
  --transfer-bg:#eceff2;
  --warn:       #a8721c;
  --warn-bg:    #faf1e0;
  --accent:     #2a5f6e;
  --accent-2:   #38788a;
  --on-accent:  #ffffff;   /* text that sits on --accent; flips in dark mode */

  --radius:     10px;
  --radius-sm:  6px;
  --shadow:     0 1px 2px rgba(29,26,22,.05), 0 4px 16px -6px rgba(29,26,22,.10);
  --sheet-shadow: 0 -8px 32px -8px rgba(29,26,22,.22);
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Sarabun",
          "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, monospace;

  --row-y:      .52rem;   /* vertical table padding — density control */
  --row-lh:     1.55;
  --sticky-top: 0px;      /* raised on mobile so headers clear the top bar */
  --tabbar-h:   0px;
}

/* The dark palette, declared twice on purpose.
   Once for "follow the system", and once for an explicit choice — CSS has no way
   to share a declaration block between two selectors, and light-dark() would need
   a 2024+ browser where an explicit block works anywhere the rest of the app does.
   The :not([data-theme="light"]) guard is what lets a light choice win over a
   dark operating system. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #17150f; --paper-2: #1f1c16; --paper-3: #262219;
    --ink: #f0ebe1; --ink-2: #c4bcae; --ink-3: #8e8578;
    --rule: #322d24; --rule-2: #443d31;
    --in: #6fc490; --in-bg: #1b2f23; --out: #e08878; --out-bg: #33201c;
    --transfer: #93a2b1; --transfer-bg: #232a30;
    --warn: #d7a44e; --warn-bg: #322a18;
    --accent: #7fc3d4; --accent-2: #a2dbe8; --on-accent: #16140f;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px -6px rgba(0,0,0,.5);
    --sheet-shadow: 0 -8px 32px -8px rgba(0,0,0,.6);
  }
}

:root[data-theme="dark"] {
    --paper: #17150f; --paper-2: #1f1c16; --paper-3: #262219;
    --ink: #f0ebe1; --ink-2: #c4bcae; --ink-3: #8e8578;
    --rule: #322d24; --rule-2: #443d31;
    --in: #6fc490; --in-bg: #1b2f23; --out: #e08878; --out-bg: #33201c;
    --transfer: #93a2b1; --transfer-bg: #232a30;
    --warn: #d7a44e; --warn-bg: #322a18;
    --accent: #7fc3d4; --accent-2: #a2dbe8; --on-accent: #16140f;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px -6px rgba(0,0,0,.5);
    --sheet-shadow: 0 -8px 32px -8px rgba(0,0,0,.6);
}

/* Form controls, scrollbars and the caret follow the chosen theme, not the
   system's, once a choice has been made. */
:root { color-scheme: light dark; }
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.55 var(--sans);
  color: var(--ink);
  background: var(--paper);
  font-variant-numeric: tabular-nums;
  overflow-x: hidden;                 /* last line of defence, not the fix */
}
a { color: var(--accent); text-decoration-color: color-mix(in srgb, var(--accent) 35%, transparent); text-underline-offset: 2px; }
a:hover { text-decoration-color: currentColor; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -.012em; line-height: 1.25; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: .92rem; }
p  { margin: 0 0 .8em; }
hr { border: 0; border-top: 1px solid var(--rule); margin: 1.4rem 0; }
small { font-size: .82em; }

:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 65%, transparent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* ── shell ─────────────────────────────────────────────────────────────── */
.shell { display: grid; grid-template-columns: minmax(0, 232px) minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  background: var(--paper-2);
  border-right: 1px solid var(--rule);
  padding: 1.15rem .8rem 2rem;
  display: flex; flex-direction: column; gap: 1.5rem;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  min-width: 0;
}
.brand { display: flex; align-items: baseline; gap: .5rem; padding: 0 .5rem; }
.brand b { font-size: 1.05rem; letter-spacing: -.02em; }
.brand span { font-size: .72rem; color: var(--ink-3); letter-spacing: .06em; text-transform: uppercase; }

.nav { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.nav a {
  display: flex; align-items: center; gap: .6rem;
  padding: .48rem .6rem; border-radius: var(--radius-sm);
  color: var(--ink-2); text-decoration: none; font-size: .9rem;
  min-width: 0;
}
.nav a:hover { background: var(--paper-3); color: var(--ink); }
.nav a.on { background: var(--paper-3); color: var(--ink); font-weight: 600; box-shadow: inset 2px 0 0 var(--accent); }
.nav .ico { width: 1.15rem; text-align: center; font-size: .95rem; opacity: .85; flex: none; }
.nav-group { padding: .35rem .6rem .15rem; font-size: .68rem; letter-spacing: .09em;
             text-transform: uppercase; color: var(--ink-3); font-weight: 600; }

.side-accounts { display: flex; flex-direction: column; gap: .1rem; margin-top: auto; min-width: 0; }
.side-account { display: flex; align-items: center; gap: .5rem; padding: .3rem .6rem;
                font-size: .8rem; color: var(--ink-2); text-decoration: none; border-radius: var(--radius-sm); min-width: 0; }
.side-account:hover { background: var(--paper-3); }
.side-account .dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.side-account .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.side-account .bal { font-size: .78rem; color: var(--ink-3); }

.main { padding: 1.6rem 2rem 4rem; max-width: 1180px; min-width: 0; }
.main.wide { max-width: 1460px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between;
             gap: 1rem; flex-wrap: wrap; margin-bottom: 1.4rem; }
.page-head .sub { color: var(--ink-3); font-size: .87rem; margin-top: .2rem; }

/* ── mobile chrome (hidden on the desktop layout) ──────────────────────── */
.mobile-top, .tabbar { display: none; }

/* ── cards & grid ──────────────────────────────────────────────────────── */
.card {
  background: var(--paper-2); border: 1px solid var(--rule);
  border-radius: var(--radius); padding: 1.1rem 1.2rem; min-width: 0;
}
.card + .card { margin-top: 1rem; }
.card-head { display: flex; align-items: baseline; justify-content: space-between;
             gap: .8rem; margin-bottom: .9rem; flex-wrap: wrap; }
.card-head h2 { font-size: .95rem; }
.card-head .hint { font-size: .78rem; color: var(--ink-3); }

.grid { display: grid; gap: 1rem; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g-2-1 { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); align-items: start; }

/* ── stat tiles ────────────────────────────────────────────────────────── */
.stat { min-width: 0; background: var(--paper-2); border: 1px solid var(--rule);
        border-radius: var(--radius); padding: .95rem 1.05rem; }
.stat .label { font-size: .72rem; letter-spacing: .07em; text-transform: uppercase;
               color: var(--ink-3); font-weight: 600; }
.stat .value { font-size: 1.5rem; font-weight: 600; letter-spacing: -.02em; margin-top: .3rem; line-height: 1.15; }
.stat .value .cur { font-size: .78em; color: var(--ink-3); font-weight: 500; margin-right: .1em; }
.stat .delta { font-size: .78rem; color: var(--ink-3); margin-top: .25rem; }
.stat.in .value  { color: var(--in); }
.stat.out .value { color: var(--out); }

/* ── numbers ───────────────────────────────────────────────────────────── */
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.amt-in   { color: var(--in); }
.amt-out  { color: var(--out); }
.amt-zero { color: var(--ink-3); }

/* ── tables ────────────────────────────────────────────────────────────────
   .table-wrap does NOT scroll by default. An overflow-x scroll container also
   becomes the scrollport for position:sticky, which silently stops column
   headers sticking to the viewport. Wide reference tables opt in with
   .scroll-x; the ledger uses fixed column widths so it never needs to.        */
.table-wrap { border: 1px solid var(--rule); border-radius: var(--radius);
              background: var(--paper-2); min-width: 0; }
.table-wrap.scroll-x { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: .875rem; }
th {
  text-align: left; font-weight: 600; font-size: .7rem; letter-spacing: .07em;
  text-transform: uppercase; color: var(--ink-3);
  padding: .6rem .7rem; background: var(--paper-2);
  position: sticky; top: var(--sticky-top); z-index: 2;
  /* a border does not paint on a sticky cell — inset shadow does */
  box-shadow: inset 0 -1px 0 var(--rule-2);
}
th:first-child { border-top-left-radius: var(--radius); }
th:last-child  { border-top-right-radius: var(--radius); }
td { padding: var(--row-y) .7rem; border-bottom: 1px solid var(--rule);
     vertical-align: top; line-height: var(--row-lh); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: color-mix(in srgb, var(--paper-3) 55%, transparent); }
tbody tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
tbody tr.is-focused { background: color-mix(in srgb, var(--accent) 8%, transparent); }
td.r, th.r { text-align: right; }
td.c, th.c { text-align: center; }
.t-date { color: var(--ink-2); white-space: nowrap; font-size: .84rem; }
.t-time { color: var(--ink-3); font-size: .78rem; }
.t-desc { min-width: 0; overflow-wrap: anywhere; }
.t-desc .primary { display: block; }
.t-desc .note {
  display: inline-block; margin-top: .15rem; font-size: .8rem; color: var(--warn);
  background: var(--warn-bg); padding: .04rem .38rem; border-radius: 4px;
}
.t-sub { color: var(--ink-3); font-size: .78rem; }
.t-more { display: none; }

/* Sortable headers */
th a.sort { color: inherit; text-decoration: none; display: inline-flex; align-items: center; gap: .25rem; }
th a.sort:hover { color: var(--ink); }
th a.sort .arrow { opacity: 0; font-size: .8em; }
th[aria-sort] a.sort { color: var(--ink); }
th[aria-sort] a.sort .arrow { opacity: 1; }

/* The ledger: fixed widths keep the figures in a column and remove any need
   for horizontal scrolling, which is what lets the header stick. */
#txnTable { table-layout: fixed; }
#txnTable th:nth-child(1), #txnTable td:nth-child(1) { width: 2.2rem; }
#txnTable th:nth-child(2), #txnTable td:nth-child(2) { width: 5.2rem; }
#txnTable th:nth-child(3), #txnTable td:nth-child(3) { width: 8.5rem; }
#txnTable th:nth-child(5), #txnTable td:nth-child(5) { width: 12.5rem; }
#txnTable th:nth-child(6), #txnTable td:nth-child(6) { width: 7.5rem; }
#txnTable th:nth-child(7), #txnTable td:nth-child(7) { width: 7.5rem; }

/* Density — comfortable by default; compact roughly doubles rows per screen by
   putting the time beside the date, keeping every row to a single line, and
   trimming the chrome around the chips. */
body.compact { --row-y: .16rem; --row-lh: 1.25; }
body.compact td { font-size: .83rem; }
body.compact .t-date br { display: none; }
body.compact .t-time { margin-left: .3rem; }
body.compact .t-desc .primary { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
body.compact .t-desc .note { display: inline; margin: 0 0 0 .3rem; padding: 0 .3rem; }
body.compact .t-desc .pill { display: none; }          /* badges move out of the way */
body.compact .t-desc .note + .pill { display: none; }
body.compact .cat-chip { padding: 0 .3rem; min-height: 0; }
body.compact .pill { padding: 0 .35rem; font-size: .7rem; }
body.compact .pick-hit { min-height: 0; }

/* ── pills, chips, badges ──────────────────────────────────────────────── */
.pill {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .1rem .45rem; border-radius: 20px; font-size: .74rem;
  background: var(--paper-3); color: var(--ink-2); white-space: nowrap;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis;
}
.pill.transfer { background: var(--transfer-bg); color: var(--transfer); }
.pill.warn     { background: var(--warn-bg); color: var(--warn); }
.pill.ok       { background: var(--in-bg); color: var(--in); }
.pill.bad      { background: var(--out-bg); color: var(--out); }
.pill .dot { width: 6px; height: 6px; border-radius: 50%; flex: none; }

.tag { display: inline-block; padding: .05rem .38rem; border-radius: 4px;
       background: var(--paper-3); color: var(--ink-2); font-size: .72rem; }

/* Category chip — one shared <select> is moved into place on click, instead of
   rendering a full category list into every row. */
.cat-chip {
  display: inline-flex; align-items: center; gap: .3rem; cursor: pointer;
  background: none; border: 1px solid transparent; color: var(--ink-2);
  border-radius: var(--radius-sm); padding: .16rem .4rem; font-size: .82rem;
  text-align: left; max-width: 100%; min-width: 0;
}
.cat-chip:hover { border-color: var(--rule-2); background: var(--paper-3); color: var(--ink); }
.cat-chip .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-chip.none { color: var(--ink-3); font-style: italic; }

/* The label, not the 24px box, is what gets tapped. */
.pick-hit { display: inline-flex; align-items: center; justify-content: center;
            min-width: 24px; min-height: 24px; cursor: pointer; margin: 0; }
#catPicker { position: absolute; z-index: 40; min-width: 12rem; font-size: .84rem; }
#catPicker[hidden] { display: none; }

/* ── forms & buttons ───────────────────────────────────────────────────── */
input, select, textarea, button { font: inherit; color: inherit; }
input[type=text], input[type=email], input[type=password], input[type=date],
input[type=number], input[type=search], input[type=time], select, textarea {
  background: var(--paper-2); border: 1px solid var(--rule-2);
  border-radius: var(--radius-sm); padding: .38rem .55rem; width: 100%;
  min-width: 0;
}
label { display: block; font-size: .78rem; color: var(--ink-2); margin-bottom: .22rem; font-weight: 500; }
.field { margin-bottom: .75rem; }
.row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 8rem; min-width: 0; }
.row > .narrow { flex: 0 0 auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .38rem; cursor: pointer;
  background: var(--paper-2); border: 1px solid var(--rule-2); color: var(--ink);
  padding: .38rem .8rem; border-radius: var(--radius-sm); font-size: .86rem;
  font-weight: 500; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--paper-3); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
.btn.danger { color: var(--out); border-color: color-mix(in srgb, var(--out) 40%, var(--rule-2)); }
.btn.danger:hover { background: var(--out-bg); }
.btn.sm { padding: .22rem .5rem; font-size: .78rem; }
.btn.on { background: var(--paper-3); border-color: var(--rule-2); font-weight: 600; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

.filters-toggle { display: none; }

/* Date range presets */
.presets { display: flex; gap: .3rem; flex-wrap: wrap; }
.presets .btn { font-size: .78rem; padding: .2rem .55rem; }

/* ── progress ──────────────────────────────────────────────────────────── */
.meter { height: 7px; border-radius: 4px; background: var(--paper-3); overflow: hidden; }
.meter > i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.meter.over > i { background: var(--out); }
.meter.near > i { background: var(--warn); }

/* ── notices ───────────────────────────────────────────────────────────── */
.notice { border: 1px solid var(--rule-2); border-left-width: 3px;
          border-radius: var(--radius-sm); padding: .7rem .9rem; font-size: .87rem; margin-bottom: 1rem; }
.notice.info { border-left-color: var(--accent); background: color-mix(in srgb, var(--accent) 6%, var(--paper-2)); }
.notice.good { border-left-color: var(--in);  background: var(--in-bg); }
.notice.warn { border-left-color: var(--warn); background: var(--warn-bg); }
.notice.bad  { border-left-color: var(--out); background: var(--out-bg); }
.notice h3 { margin-bottom: .25rem; }

.empty { text-align: center; padding: 2.6rem 1rem; color: var(--ink-3); }
.empty .big { font-size: 1.8rem; margin-bottom: .5rem; opacity: .5; }

/* ── charts ────────────────────────────────────────────────────────────── */
.chart { position: relative; height: 220px; width: 100%; min-width: 0; }
.chart.tall { height: 280px; }

/* ── category bars ─────────────────────────────────────────────────────── */
.catlist { display: flex; flex-direction: column; gap: .55rem; min-width: 0; }
.catrow { display: grid; grid-template-columns: 1.4rem minmax(0, 1fr) auto; gap: .55rem; align-items: center; }
.catrow .bar { height: 5px; border-radius: 3px; margin-top: .28rem; }
.catrow .nm { font-size: .86rem; }
.catrow .amt { font-size: .86rem; font-weight: 500; }
.catrow .pct { font-size: .74rem; color: var(--ink-3); }

/* Bulk action bar — follows you down a long ledger. Declared here rather than
   inline so the mobile override (which moves it above the tab bar) can win. */
#bulkBar { position: sticky; top: .6rem; z-index: 6; padding: .6rem .9rem;
           box-shadow: var(--shadow); }

/* ── results region (swapped in place when filters change) ─────────────── */
#results { position: relative; min-width: 0; }
#results.loading { opacity: .55; transition: opacity .12s; }
#results.loading::after {
  content: ''; position: absolute; inset: 0; cursor: progress;
}

/* ── theme switch ──────────────────────────────────────────────────────── */
.theme-switch { display: inline-flex; border: 1px solid var(--rule-2); border-radius: var(--radius-sm);
                overflow: hidden; background: var(--paper-2); }
.theme-switch button {
  border: 0; background: none; color: var(--ink-2); cursor: pointer;
  padding: .34rem .7rem; font-size: .82rem; display: inline-flex; align-items: center; gap: .3rem;
}
.theme-switch button + button { border-left: 1px solid var(--rule-2); }
.theme-switch button:hover { background: var(--paper-3); color: var(--ink); }
.theme-switch button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); font-weight: 600; }
.theme-switch .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.sidebar .theme-switch, .sheet .theme-switch { width: 100%; }
.sidebar .theme-switch button, .sheet .theme-switch button { flex: 1; justify-content: center; }
/* The sidebar is 232px wide — three labelled buttons do not fit, and a
   truncated one reads as broken. Icons carry it, with the name in the tooltip
   and for screen readers. */
.sidebar .theme-switch button { padding: .34rem .2rem; }
.sidebar .theme-switch .lbl { display: none; }

/* ── import drop zone ──────────────────────────────────────────────────── */
.dropzone { border-style: dashed; border-width: 2px; text-align: center;
            padding: 2.2rem 1.2rem; cursor: pointer; }
.dropzone .dz-icon { font-size: 1.8rem; opacity: .45; margin-bottom: .4rem; }
.dropzone .dz-copy { margin: .4rem auto 1rem; max-width: 34rem; }
.dropzone.is-over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 5%, var(--paper-2)); }

/* ── misc ──────────────────────────────────────────────────────────────── */
.muted { color: var(--ink-3); }
.mono { font-family: var(--mono); font-size: .82em; }
.nowrap { white-space: nowrap; }
.stack { display: flex; flex-direction: column; gap: 1rem; min-width: 0; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: .8rem; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.scroll-y { max-height: 26rem; overflow-y: auto; }

/* ── auth ──────────────────────────────────────────────────────────────── */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 2rem 1rem; }
.auth-card { width: 100%; max-width: 22rem; background: var(--paper-2);
             border: 1px solid var(--rule); border-radius: var(--radius);
             padding: 1.8rem; box-shadow: var(--shadow); }
.auth-card .brand { justify-content: center; margin-bottom: 1.4rem; padding: 0; }

/* ── dialogs & sheets ──────────────────────────────────────────────────── */
dialog { border: 1px solid var(--rule-2); border-radius: var(--radius); padding: 0;
         background: var(--paper-2); color: var(--ink); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(20, 18, 15, .38); }

.sheet { border: 0; border-radius: var(--radius) var(--radius) 0 0; margin: auto auto 0;
         width: 100%; max-width: 34rem; box-shadow: var(--sheet-shadow); }
.sheet[open] { animation: sheet-up .18s ease-out; }
@keyframes sheet-up { from { transform: translateY(14px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet .sheet-body { padding: 1rem 1.1rem calc(1.2rem + env(safe-area-inset-bottom)); }
.sheet .grab { width: 2.2rem; height: 4px; border-radius: 2px; background: var(--rule-2);
               margin: .6rem auto .2rem; }
.sheet-links { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .3rem; }
.sheet-links a { display: flex; align-items: center; gap: .55rem; padding: .7rem .6rem;
                 border-radius: var(--radius-sm); color: var(--ink-2); text-decoration: none; min-height: 44px; }
.sheet-links a:hover, .sheet-links a.on { background: var(--paper-3); color: var(--ink); }

/* Keyboard shortcut help */
.keys { width: 100%; }
.keys td { padding: .32rem .2rem; border-bottom: 1px solid var(--rule); }
kbd { font: 600 .78rem var(--mono); background: var(--paper-3); border: 1px solid var(--rule-2);
      border-bottom-width: 2px; border-radius: 4px; padding: .08rem .34rem; white-space: nowrap; }

/* ── responsive ────────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  :root { --sticky-top: 46px; --tabbar-h: 58px; }

  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { display: none; }

  /* Slim top bar: where you are, plus the one action that page needs. */
  .mobile-top {
    display: flex; align-items: center; justify-content: space-between; gap: .6rem;
    position: sticky; top: 0; z-index: 30;
    padding: .5rem .8rem; min-height: 46px;
    background: color-mix(in srgb, var(--paper) 88%, transparent);
    backdrop-filter: saturate(150%) blur(8px);
    border-bottom: 1px solid var(--rule);
  }
  .mobile-top .title { font-weight: 600; font-size: .95rem; min-width: 0;
                       overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .mobile-top .btn { min-height: 34px; }

  /* Bottom tab bar: five destinations within thumb reach. */
  .tabbar {
    display: grid; grid-template-columns: repeat(5, 1fr);
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: color-mix(in srgb, var(--paper-2) 94%, transparent);
    backdrop-filter: saturate(150%) blur(10px);
    border-top: 1px solid var(--rule);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar a, .tabbar button {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .12rem;
    padding: .4rem .2rem; min-height: 56px; background: none; border: 0;
    color: var(--ink-3); text-decoration: none; font-size: .66rem; cursor: pointer;
  }
  .tabbar .ico { font-size: 1.05rem; line-height: 1; }
  .tabbar .on { color: var(--accent); font-weight: 600; }

  .main { padding: 1rem .8rem calc(2rem + var(--tabbar-h) + env(safe-area-inset-bottom)); max-width: none; }

  /* The top bar already names the page — do not say it twice. */
  .hide-mobile, .page-head h1 { display: none; }
  .page-head { margin-bottom: .7rem; gap: .6rem; }
  .page-head .sub { margin-top: 0; }

  /* A filter panel that fills the screen before a single transaction is worse
     than no filter panel. Search stays out; everything else folds away. */
  .filters-toggle { display: block; width: 100%; margin-top: .5rem; }
  #filterForm .foldable { display: none; }
  #filterForm.open .foldable { display: block; }
  #filterForm.open .spread.foldable { display: flex; }

  /* Dropping a file is a desktop gesture; on a phone it is just a big button. */
  .dropzone { padding: 1.4rem 1rem; border-style: solid; border-width: 1px; }
  .dropzone .dz-icon { display: none; }
  .dropzone .dz-copy { font-size: .84rem; }
  .main.wide { max-width: none; }
  .page-head { margin-bottom: 1rem; }
  .page-head h1 { font-size: 1.2rem; }

  .g4, .g3, .g2, .g-2-1 { grid-template-columns: minmax(0, 1fr); }
  .g4.keep2, .g2.keep2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  /* Reference tables stay tables and scroll inside their own box. */
  .table-wrap { overflow-x: auto; }
  .card { padding: .9rem; }

  /* Anything you tap needs to be big enough to hit — 44px tall, 24px wide.
     That includes links sitting inside table cells and list rows, which are
     otherwise only as tall as their text. */
  .btn, .btn.sm, .presets .btn { min-height: 44px; padding: .5rem .9rem; }
  .btn.sm, .presets .btn { padding: .5rem .7rem; font-size: .82rem; }
  .tabbar a, .tabbar button, .sheet-links a { min-height: 44px; }
  input, select, textarea { min-height: 44px; font-size: 16px; }  /* 16px stops iOS zooming on focus */
  input[type=checkbox], input[type=radio] { min-height: 24px; width: 24px; height: 24px; }
  td a:not(.btn):not(.pill), .card-head .hint {
    display: inline-block; padding: .45rem 0; min-height: 44px;
  }
  /* Drill-down links in the category list: comfortably above the 24px
     minimum without stretching the list to twice its height. */
  .catrow .nm { display: inline-block; padding: .3rem 0; min-height: 30px; }
  .catrow { min-height: 44px; }

  /* ── the ledger becomes stacked rows ──────────────────────────────────
     Same markup as the desktop table, re-laid-out. One source of truth, so
     the partial re-render keeps working untouched.                        */
  .cards { overflow-x: visible; }
  .cards table { table-layout: auto; }
  .cards thead { display: none; }
  .cards table, .cards tbody, .cards tr, .cards td { display: block; width: auto; }
  .cards tr {
    display: grid;
    grid-template-columns: 1.9rem auto minmax(0, 1fr);
    grid-template-areas:
      "check date  account"
      "check desc  desc"
      "check cat   amount"
      "check more  more";
    column-gap: .5rem; row-gap: .1rem;
    padding: .55rem .7rem;
    border-bottom: 1px solid var(--rule);
    align-items: center;
  }
  .cards td { border: 0; padding: 0; min-width: 0; }
  .cards td:nth-child(1) { grid-area: check; align-self: stretch; }
  .cards td:nth-child(2) { grid-area: date; }
  .cards td:nth-child(3) { grid-area: account; justify-self: end; min-width: 0; }
  .cards td:nth-child(4) { grid-area: desc; font-size: .92rem; line-height: 1.35; }
  .cards td:nth-child(5) { grid-area: cat; }
  .cards td:nth-child(6) { grid-area: amount; text-align: right; font-size: 1rem; font-weight: 600; }
  .cards td:nth-child(7) { display: none; }          /* balance moves into the detail */
  .cards .t-date br { display: none; }
  .cards .t-time { margin-left: .35rem; }
  .cards td.t-more { grid-area: more; display: none; }
  .cards tr.open .t-more {
    display: block; margin-top: .35rem; padding-top: .35rem;
    border-top: 1px dashed var(--rule); font-size: .8rem; color: var(--ink-3);
  }
  .cards .pick { width: 24px; height: 24px; }
  .pick-hit { min-height: 44px; min-width: 32px; }
  /* The check column spans the whole card, so a full-size target sits inside
     it without stretching the row. */
  .cards td:nth-child(1) .pick-hit { min-height: 44px; align-items: flex-start; padding-top: 0; }
  /* Comfortably above the 24px accessibility floor and wide enough to hit
     easily; the whole card is tappable too, so it need not be 44px tall. */
  .cards .cat-chip { padding: .25rem .4rem; min-height: 34px; }

  /* Sticky filter/bulk bars must clear the fixed tab bar. */
  #bulkBar { position: fixed; left: .6rem; right: .6rem;
             bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + .5rem);
             top: auto; z-index: 25; box-shadow: var(--shadow); }
}

@media (min-width: 901px) {
  .sheet { max-width: 26rem; margin: auto; border-radius: var(--radius); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* ── print: the monthly report ─────────────────────────────────────────── */
@media print {
  :root { --paper: #fff; --paper-2: #fff; --paper-3: #f4f4f4;
          --ink: #000; --ink-2: #333; --ink-3: #666; --rule: #ddd; --rule-2: #bbb;
          --sticky-top: 0px; --tabbar-h: 0px; }
  .sidebar, .mobile-top, .tabbar, .no-print, .btn, dialog { display: none !important; }
  .shell { grid-template-columns: 1fr; }
  .main, .main.wide { padding: 0; max-width: none; }
  .card { border: 0; padding: 0; page-break-inside: avoid; }
  .card + .card { margin-top: 1.6rem; }
  .table-wrap { border: 0; overflow: visible; }
  th { position: static; box-shadow: none; border-bottom: 1px solid #bbb; }
  a { color: inherit; text-decoration: none; }
  @page { margin: 16mm 14mm; size: A4; }
}
