:root {
  --bg: #f7f6f3; --panel: #fff; --ink: #1d1d1f; --muted: #6e6e73; --line: #e3e1dc; --grid: #ecebe7;
  --accent: #c2410c; --accent-ink: #fff; --good: #15803d; --bad: #b91c1c; --warn: #b45309;
  --row-hover: #fbfaf7; --focus: #2563eb;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #161616; --panel: #1f1f1f; --ink: #ececec; --muted: #9a9a9f; --line: #333; --grid: #2a2a2a;
    --accent: #f97316; --accent-ink: #111; --good: #4ade80; --bad: #f87171; --warn: #fbbf24; --row-hover: #242424; --focus: #60a5fa; }
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif; background: var(--bg); color: var(--ink);
  padding: max(20px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(48px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left)); }
html { -webkit-text-size-adjust: 100%; }
input, select, button { font: inherit; color: inherit; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }

.top { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; }
.event { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.event label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.event input, .event select { border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 5px 8px; }
#ev-budget { width: 110px; }
.ev-name { font-size: 22px !important; font-weight: 650; border-color: transparent !important; background: transparent !important; padding: 2px 4px !important; width: 260px; }
.ev-name:hover, .ev-name:focus { border-color: var(--line) !important; }
.actions { display: flex; gap: 8px; }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 7px; padding: 7px 12px; cursor: pointer; text-decoration: none; color: inherit; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn:disabled { opacity: .5; cursor: default; }
.sync-status { min-height: 20px; margin: 8px 0 14px; color: var(--muted); font-size: 13px; }
.sync-status.ok { color: var(--good); } .sync-status.err { color: var(--bad); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
.tile span { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.tile b { font-size: 22px; font-variant-numeric: tabular-nums; }
.tile small { color: var(--muted); font-size: 12px; }
.over { color: var(--bad) !important; } .under { color: var(--good); }
.meter { height: 6px; background: var(--grid); border-radius: 3px; margin: 12px 0 20px; overflow: hidden; }
#meter-fill { height: 100%; background: var(--accent); transition: width .2s; }
#meter-fill.over { background: var(--bad); }

.toolbar { display: flex; gap: 8px; margin-bottom: 8px; }
.toolbar input, .toolbar select { border: 1px solid var(--line); background: var(--panel); border-radius: 7px; padding: 6px 10px; }
.toolbar input { flex: 0 1 240px; }
.toolbar .btn { margin-left: auto; }

.sheet-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.sheet { border-collapse: collapse; width: 100%; min-width: 960px; }
.sheet th { position: sticky; top: 0; background: var(--panel); text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 8px 6px; border-bottom: 1px solid var(--line); }
.sheet td { border-bottom: 1px solid var(--grid); border-right: 1px solid var(--grid); padding: 0; height: 34px; }
.sheet td:last-child, .sheet th:last-child { border-right: 0; }
.sheet tbody tr:hover { background: var(--row-hover); }
.sheet input, .sheet select { width: 100%; height: 34px; border: 0; background: transparent; padding: 0 8px; outline: none; }
.sheet .num input { text-align: right; }
.sheet input:focus, .sheet select:focus { box-shadow: inset 0 0 0 2px var(--focus); border-radius: 2px; }
.sheet .c-n { width: 34px; text-align: center; color: var(--muted); font-size: 12px; }
.sheet .c-task { width: 90px; padding: 0 8px; font-size: 12px; white-space: nowrap; }
.sheet .c-task a { color: var(--good); text-decoration: none; }
.sheet .c-x { width: 32px; text-align: center; }
.del { border: 0; background: none; color: var(--muted); font-size: 18px; cursor: pointer; opacity: 0; }
@media (hover: none) { .del { opacity: 1; } }
tr:hover .del, .del:focus { opacity: 1; } .del:hover { color: var(--bad); }
.status[data-s="Paid"] { color: var(--good); font-weight: 600; }
.status[data-s="Booked"] { color: var(--warn); font-weight: 600; }
tr.paid [data-f="item"] { color: var(--muted); }
tr.overdue td:first-child { box-shadow: inset 3px 0 0 var(--bad); }
tr.overdue [data-f="due_date"] { color: var(--bad); font-weight: 600; }
.sheet tfoot td { font-weight: 650; padding: 0 8px; border-bottom: 0; font-variant-numeric: tabular-nums; }
.empty td { text-align: center; color: var(--muted); padding: 28px; height: auto; }

.by-cat { margin-top: 24px; max-width: 640px; }
.by-cat h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 10px; }
.cat { display: grid; grid-template-columns: 120px 1fr 90px 44px; gap: 10px; align-items: center; padding: 4px 0; }
.bar { height: 10px; background: var(--grid); border-radius: 5px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); border-radius: 5px; }
/* column widths */
.sheet td:nth-child(3) { min-width: 140px; }
.sheet td:nth-child(8) { min-width: 104px; }
.sheet td:nth-child(5) { width: 110px; }
/* sign-in */
body.login { display: grid; place-items: center; min-height: 100vh; min-height: 100dvh; padding: 0; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px; width: min(340px, 90vw); display: flex; flex-direction: column; gap: 12px; }
.login-card h1 { margin: 0; font-size: 20px; } .login-card p { margin: 0; }
.login-card input { border: 1px solid var(--line); background: var(--bg); border-radius: 7px; padding: 9px 10px; }

/* ---------- phones: the sheet becomes a list of cards ---------- */
@media (max-width: 720px) {
  body { padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(88px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
  body.login { padding: 12px; }
  /* >=16px stops iOS Safari zooming on focus; 44px is the minimum comfortable tap target */
  input, select, textarea { font-size: 16px; }
  button, .btn, select, input:not([type="checkbox"]):not([type="radio"]) { min-height: 44px; }
  .btn { display: inline-flex; align-items: center; justify-content: center; padding: 0 16px; touch-action: manipulation; }
  .login-card { padding: 22px; }

  /* header */
  .top { flex-direction: column; align-items: stretch; gap: 12px; }
  .event { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
  .ev-name { grid-column: 1 / -1; width: 100%; min-height: 44px; }
  .event label { flex-direction: column; align-items: stretch; gap: 4px; min-width: 0; }
  .event label:first-of-type { grid-column: 1 / -1; }
  .event input, .event select { width: 100%; min-width: 0; padding: 0 10px; border-radius: 8px; }
  #ev-budget { width: 100%; }
  .actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .actions #sync { grid-column: 1 / -1; order: -1; }
  .sync-status { margin: 10px 0 12px; font-size: 14px; }

  /* summary tiles */
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tile { padding: 10px 12px; }
  .tile b { font-size: 18px; overflow-wrap: break-word; }
  .meter { margin: 12px 0 16px; }

  /* toolbar */
  .toolbar { flex-wrap: wrap; margin-bottom: 12px; }
  .toolbar input { flex: 1 1 100%; border-radius: 8px; }
  .toolbar select { flex: 1 1 0; min-width: 0; border-radius: 8px; }

  /* table -> cards */
  .sheet-wrap { overflow: visible; background: none; border: 0; border-radius: 0; }
  .sheet, .sheet tbody, .sheet tfoot, .sheet tr, .sheet td { display: block; }
  .sheet { min-width: 0; width: 100%; }
  .sheet thead { display: none; }

  .sheet tbody tr:not(.empty) {
    position: relative; display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px 8px;
    padding: 12px; margin-bottom: 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  }
  .sheet tbody tr:not(.empty):hover { background: var(--panel); }
  .sheet tbody tr.overdue { box-shadow: inset 4px 0 0 var(--bad); }
  .sheet tbody tr.overdue > td:first-child { box-shadow: none; }
  .sheet tbody tr:not(.empty) > td, .sheet tfoot td {
    width: auto; min-width: 0; height: auto; padding: 0; border: 0; text-align: left; grid-column: span 3;
  }
  .sheet td[data-label]::before {
    content: attr(data-label); display: block; margin-bottom: 4px; font-size: 11px; line-height: 14px; font-weight: 600;
    color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
  }
  /* item row: number badge left, delete right */
  .sheet tbody tr:not(.empty) > td:nth-child(2) { grid-column: 1 / -1; padding: 0 40px 0 22px; }
  .sheet tbody tr:not(.empty) > td:nth-child(2) input { font-size: 17px; font-weight: 600; }
  .sheet tbody tr:not(.empty) > td.c-n { position: absolute; top: 12px; left: 8px; width: 28px; line-height: 44px; }
  .sheet tbody tr:not(.empty) > td.c-x { position: absolute; top: 12px; right: 4px; width: 44px; }
  .sheet tbody tr:not(.empty) > td.c-task { order: 1; padding: 0; font-size: 14px; line-height: 44px; white-space: normal; }
  .sheet tbody tr:not(.empty) > td.c-task::before { line-height: 14px; }
  .sheet tbody tr:not(.empty) > td:nth-child(9) { order: 2; grid-column: 1 / -1; }
  .sheet tbody tr:not(.empty) > td.num { text-align: left; }
  .del { width: 44px; height: 44px; font-size: 24px; opacity: 1; }

  .sheet tbody tr:not(.empty) input, .sheet tbody tr:not(.empty) select {
    display: block; width: 100%; height: 44px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
  }
  .sheet tbody tr:not(.empty) .num input { text-align: left; }
  .sheet tbody tr:not(.empty) input:focus, .sheet tbody tr:not(.empty) select:focus { border-color: var(--focus); box-shadow: 0 0 0 2px var(--focus); border-radius: 8px; }
  .sheet tbody tr:not(.empty) input[type="date"] { min-width: 0; text-align: left; }
  .sheet tbody tr:not(.empty) > td:nth-child(2) input { border-color: transparent; background: transparent; padding-left: 4px; }
  .sheet tbody tr:not(.empty) > td:nth-child(2) input:focus { border-color: var(--focus); background: var(--bg); }
  .empty td { padding: 24px 12px; border: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }

  /* totals: pinned to the bottom while scrolling the list */
  .sheet tfoot { position: sticky; bottom: max(8px, env(safe-area-inset-bottom)); z-index: 3; }
  .sheet tfoot tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px; padding: 10px;
    background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 4px 16px rgba(0, 0, 0, .18);
  }
  .sheet tfoot td { grid-column: auto; font-size: 12px; font-weight: 600; overflow-wrap: break-word; line-height: 1.3; }
  .sheet tfoot td:nth-child(1), .sheet tfoot td:nth-child(4) { display: none; }
  .sheet tfoot td { font-size: 15px; }
  .sheet tfoot td:nth-child(3) { text-align: right; }

  /* by category: bar drops under the name/amount line */
  .by-cat { margin-top: 20px; max-width: none; }
  .cat { grid-template-columns: minmax(0, 1fr) auto 44px; grid-template-areas: "name amt pct" "bar bar bar"; gap: 4px 10px; padding: 8px 0; }
  .cat .cat-name { grid-area: name; overflow-wrap: anywhere; }
  .cat .bar { grid-area: bar; }
  .cat > :nth-child(3) { grid-area: amt; }
  .cat > :nth-child(4) { grid-area: pct; }
}

/* ---- add-expense (mobile) ---- */
.qa { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin: 0 0 12px; }
.qa-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.qa-close, .qa-fab, .qa-backdrop { display: none; }
.qa-grid { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr auto; gap: 8px 10px; align-items: end; }
.qa-more .qa-grid { grid-template-columns: repeat(4, 1fr); }
.qa label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); min-width: 0; }
.qa input, .qa select { width: 100%; min-width: 0; min-height: 36px; border: 1px solid var(--line); background: var(--bg); border-radius: 7px; padding: 0 10px; color: var(--ink); }
.qa input:focus, .qa select:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 2px var(--focus); }
.qa-submit { min-height: 36px; }
.qa-more { margin-top: 8px; }
.qa-more summary { cursor: pointer; color: var(--muted); font-size: 13px; padding: 4px 0; }
.qa-more .qa-grid { margin-top: 6px; }
.qa-msg { margin: 8px 0 0; min-height: 20px; font-size: 13px; color: var(--muted); }
.qa-msg.ok { color: var(--good); } .qa-msg.err { color: var(--bad); }
.qa-undo { border: 0; background: none; padding: 0 4px; color: var(--focus); font-weight: 600; text-decoration: underline; cursor: pointer; }

@media (max-width: 720px) {
  .qa {
    position: fixed; left: 0; right: 0; bottom: var(--qa-kb, 0px); z-index: 50; margin: 0;
    max-height: calc(var(--qa-vh, 100dvh) - 12px); overflow-y: auto; overscroll-behavior: contain;
    border-radius: 16px 16px 0 0; border-width: 1px 0 0; box-shadow: 0 -8px 32px rgba(0, 0, 0, .28);
    padding: 12px max(14px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
    transform: translateY(calc(100% + 48px)); visibility: hidden; transition: transform .2s ease, visibility 0s .2s;
  }
  .qa.open { transform: none; visibility: visible; transition: transform .2s ease; }
  .qa-close { display: inline-flex; border: 0; background: none; color: var(--focus); font-weight: 600; padding: 0 4px; cursor: pointer; }
  .qa-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .35); }
  body.qa-open .qa-backdrop { display: block; }
  .qa-fab {
    display: grid; place-items: center; position: fixed; z-index: 30; right: max(16px, env(safe-area-inset-right));
    bottom: calc(88px + env(safe-area-inset-bottom)); width: 56px; height: 56px; padding: 0; border: 0; border-radius: 50%;
    background: var(--accent); color: var(--accent-ink); font-size: 32px; line-height: 1; box-shadow: 0 4px 14px rgba(0, 0, 0, .3); touch-action: manipulation;
  }
  body.qa-open .qa-fab { display: none; }
  .qa-grid { grid-template-columns: 1fr 1fr; gap: 8px 10px; }
  .qa-more .qa-grid { grid-template-columns: 1fr 1fr; }
  .qa .f-item, .qa .wide { grid-column: 1 / -1; }
  .qa .qa-grid input, .qa .qa-grid select, .qa .qa-submit { min-height: 48px; font-size: 16px; } /* .qa-grid prefix: outrank the global 44px input rule */
  .qa-more summary { padding: 10px 0; font-size: 14px; }
  .qa-msg { font-size: 14px; }
  .qa-undo { min-height: 44px; padding: 0 8px; }
}
