:root {
  --bg: #f3efe6;
  --panel: #fffdf8;
  --ink: #1d2926;
  --muted: #6c7772;
  --line: #ded8cc;
  --accent: #2f6b5f;
  --accent-strong: #225047;
  --accent-soft: #dfece7;
  --danger: #a33b32;
  --danger-soft: #f5e2df;
  --warn: #8a5a14;
  --warn-soft: #f6ead2;
  --shadow: 0 1px 2px rgba(29, 41, 38, 0.06), 0 10px 30px rgba(29, 41, 38, 0.05);
  --radius: 14px;
}

* { box-sizing: border-box; }

html { background: var(--bg); max-width: 100%; }

body {
  margin: 0;
  max-width: 100%;
  overflow-x: hidden;
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% -10%, #fbf7ed 0, transparent 55%),
    radial-gradient(1000px 500px at 110% 0%, #e7efe9 0, transparent 50%),
    var(--bg);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
}

[hidden] { display: none !important; }

h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(30px, 4vw, 46px); letter-spacing: -0.04em; line-height: 1.05; }
h2 { font-size: 17px; letter-spacing: -0.01em; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

a { color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.13em;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
}

/* Login */
.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(440px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: clamp(26px, 5vw, 44px);
}
.login-card h1 { margin: 8px 0 10px; }
.login-card .muted { margin-bottom: 28px; }
.login-card form { display: grid; gap: 10px; }
.login-card label { font-weight: 650; font-size: 13px; }

/* App shell */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 12px clamp(16px, 4vw, 40px);
  background: rgba(243, 239, 230, 0.88);
  border-bottom: 1px solid rgba(222, 216, 204, 0.8);
  backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: max-content; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 11px;
  background: var(--ink);
  color: #fffdf8;
  font-weight: 800;
  letter-spacing: -0.04em;
}
.brand strong { display: block; line-height: 1.1; }
.tabs { display: flex; align-items: center; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 12px;
  border-radius: 999px;
  white-space: nowrap;
  font-weight: 600;
  font-size: 13.5px;
}
.tab:hover { background: rgba(255, 253, 248, 0.8); color: var(--ink); }
.tab.active { background: var(--ink); color: #fffdf8; }

.page { width: min(1280px, 100%); margin: 0 auto; padding: clamp(18px, 4vw, 34px); min-width: 0; }
.view { display: grid; gap: 18px; min-width: 0; animation: rise 180ms ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.panel {
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}
.panel-head h2 { margin-bottom: 2px; }

/* Range + KPIs */
.rangebar {
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  justify-content: space-between;
  gap: 14px;
  padding: 14px;
  background: rgba(255, 253, 248, 0.7);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.range-fields, .presets { display: flex; flex-wrap: wrap; align-items: end; gap: 8px; min-width: 0; }
.range-fields label { display: grid; gap: 4px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.range-fields input { width: 150px; }

.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; min-width: 0; }
.kpi {
  min-width: 0;
  display: grid;
  gap: 2px;
  padding: 18px 20px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.kpi strong { font-size: clamp(25px, 3vw, 36px); letter-spacing: -0.04em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.kpi-label { font-size: 11px; font-weight: 750; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.kpi.accent { background: var(--ink); color: #fffdf8; border-color: var(--ink); }
.kpi.accent .kpi-label, .kpi.accent .muted { color: #b9c4bf; }
.kpi.accent .negative { color: #ffb4a8; }
.kpi.accent .positive { color: #b9e3c9; }

/* Overview layout */
.overview-layout { min-width: 0; display: grid; grid-template-columns: 230px minmax(0, 1fr); gap: 14px; align-items: start; }
.charts { min-width: 0; display: grid; grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr); gap: 14px; }
.chart-panel { padding: 18px; min-width: 0; }
.chart-panel figcaption { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 14px; margin-bottom: 8px; }
.filters { min-width: 0; padding: 16px; position: sticky; top: 82px; }
.filters h2 { font-size: 15px; }
.mini-actions { display: flex; gap: 8px; }
.link { border: 0; background: none; color: var(--accent); font-size: 12px; font-weight: 700; padding: 2px; }
.filter-list { display: grid; gap: 4px; max-height: 58vh; overflow: auto; }
.filter-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 8px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13.5px;
}
.filter-item:hover { background: #f4f1e9; }
.filter-item input { accent-color: var(--accent); width: 16px; height: 16px; flex: none; }
.filter-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filter-item .amount { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.filter-item.archived { opacity: 0.62; }
.swatch { width: 12px; height: 12px; flex: none; }
.swatch svg { display: block; }

/* Pie */
.pie-wrap { display: grid; grid-template-columns: minmax(150px, 220px) minmax(0, 1fr); gap: 12px; align-items: center; }
.pie-chart { width: 100%; aspect-ratio: 1; max-width: 220px; margin: 0 auto; }
.pie-chart svg { width: 100%; height: 100%; overflow: visible; }
.legend { display: grid; gap: 7px; align-content: center; }
.legend-row { display: grid; grid-template-columns: 12px minmax(0, 1fr) auto; align-items: center; gap: 8px; font-size: 12.5px; }
.legend-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-value { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.legend-pct { color: var(--ink); font-weight: 650; }

/* Bars */
.bar-chart { width: 100%; min-height: 190px; }
.bar-list { display: grid; gap: 10px; }
.bar-row { display: grid; gap: 5px; }
.bar-meta { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; font-size: 13px; }
.bar-name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-value { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.bar-track { height: 16px; background: #eee9df; border-radius: 4px; overflow: hidden; }
.bar-track svg { display: block; width: 100%; height: 16px; }
.budget-tick { stroke: var(--ink); stroke-width: 2; vector-effect: non-scaling-stroke; }
.chart-empty { display: grid; place-items: center; min-height: 190px; color: var(--muted); font-size: 13px; }
.pie-total-label { fill: var(--muted); font-size: 9px; text-transform: uppercase; letter-spacing: 0.1em; font-weight: 700; }
.pie-total-value { fill: var(--ink); font-size: 16px; font-weight: 750; letter-spacing: -0.02em; }

/* Table */
.table-panel { padding: 18px; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
td { padding: 10px; border-bottom: 1px solid #eee9df; vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.type-cell { display: flex; align-items: center; gap: 9px; min-width: 140px; }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}
.badge.ok { background: var(--accent-soft); color: var(--accent-strong); }
.badge.over { background: var(--danger-soft); color: var(--danger); }
.badge.neutral { background: #eeeae1; color: var(--muted); }
.badge.archived { background: var(--warn-soft); color: var(--warn); }
.remaining.negative { color: var(--danger); }
.remaining.positive { color: var(--accent-strong); }
.empty { padding: 24px 8px; color: var(--muted); text-align: center; }

/* Type chooser: buttons instead of a native select, so it is obvious and
   reliable on touch screens. */
.choice-field { border: 0; padding: 0; margin: 0; min-width: 0; }
.choice-field legend {
  padding: 0;
  margin-bottom: 6px;
  font-size: 12.5px;
  font-weight: 650;
  color: var(--muted);
}
.choice-list { display: flex; flex-wrap: wrap; gap: 8px; }
.choice-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 40px;
  padding: 8px 11px;
  border: 1px solid #cfc8bb;
  border-radius: 999px;
  background: #fffefa;
  font-size: 13px;
  font-weight: 650;
}
.choice-chip:hover { border-color: var(--accent); }
.choice-chip[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-strong);
}

/* Forms */
.form-panel { padding: clamp(18px, 3vw, 28px); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid label, .inline-form label, .receipt-actions label { display: grid; gap: 5px; font-size: 12.5px; font-weight: 650; color: var(--muted); }
.span-2 { grid-column: span 2; }
.form-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }
input, select, textarea {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  border: 1px solid #cfc8bb;
  background: #fffefa;
  border-radius: 9px;
  padding: 9px 10px;
  color: var(--ink);
  min-height: 40px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 3px solid rgba(47, 107, 95, 0.23);
  outline-offset: 1px;
  border-color: var(--accent);
}
input[type="color"] { padding: 4px; width: 54px; min-height: 40px; }
button.primary, button.quiet, .chip, .tab, .link {
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}
button.primary {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 9px;
  padding: 10px 16px;
  font-weight: 700;
}
button.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
button.primary:disabled { opacity: 0.45; cursor: not-allowed; }
button.quiet {
  border: 1px solid #cfc8bb;
  background: transparent;
  color: var(--ink);
  border-radius: 9px;
  padding: 8px 12px;
  font-weight: 600;
}
button.quiet:hover { background: #f4f1e9; }
.chip {
  border: 1px solid #cfc8bb;
  background: #fffefa;
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 12.5px;
  font-weight: 650;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.inline-form { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; margin-bottom: 12px; }
.inline-form label { flex: 1 1 150px; }
.inline-form button { flex: none; }
.inline-form input[type="color"] { min-height: 40px; }

/* Receipt */
.receipt-controls { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 14px; }
.file-label { display: grid; gap: 5px; font-size: 12.5px; font-weight: 650; color: var(--muted); min-width: min(100%, 320px); }
.receipt-actions { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; }
.receipt-actions label { min-width: 150px; }
.receipt-preview-wrap { margin-top: 14px; }
.receipt-preview {
  display: block;
  width: min(100%, 430px);
  max-height: 360px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f4f1e9;
}
#receipt-draft-wrap { margin-top: 18px; }
.draft-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.draft-toolbar strong { font-size: 14px; }
.draft-toolbar .muted { margin-left: 6px; }
button.link.danger { color: var(--danger); font-size: 12px; }
#receipt-draft-body input, #receipt-draft-body select { min-height: 36px; padding: 6px 8px; min-width: 110px; }
#receipt-draft-body td { padding: 7px; }
#receipt-draft-body .price-input { width: 100px; }
.price-cell { display: grid; gap: 2px; }
.original-price { color: var(--muted); font-size: 10.5px; white-space: nowrap; }

/* Categories */
#category-table-body input { min-height: 36px; padding: 6px 8px; }
#category-table-body .cat-name { min-width: 150px; }
#category-table-body .cat-budget { width: 130px; }
.row-actions { display: flex; gap: 7px; flex-wrap: wrap; }
.row-actions button { padding: 6px 9px; font-size: 12px; }

/* Export */
.callout {
  margin-top: 18px;
  padding: 13px 15px;
  background: #f7f4ec;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--muted);
  font-size: 13px;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fffdf8;
  border-radius: 10px;
  padding: 10px 16px;
  box-shadow: var(--shadow);
  z-index: 20;
  max-width: min(90vw, 620px);
  font-size: 13px;
}

.error { color: var(--danger); font-size: 13px; min-height: 1.2em; }
.positive { color: var(--accent-strong); }
.negative { color: var(--danger); }

@media (max-width: 980px) {
  .overview-layout { grid-template-columns: 1fr; }
  .filters { position: static; }
  .filter-list { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); max-height: none; }
  .charts { grid-template-columns: 1fr; }
}

/* Mobile-first interaction details: bottom navigation, camera input,
   card-like receipt rows and 16px inputs to avoid iOS zoom. */
@media (max-width: 720px) {
  .login-shell { min-height: 100dvh; padding: max(18px, env(safe-area-inset-top)) 16px max(18px, env(safe-area-inset-bottom)); }
  .login-card { border-radius: 18px; }
  .topbar {
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 14px 10px;
    /* A backdrop-filter creates a containing block for fixed descendants,
       which would pin the bottom navigation to the header. */
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: var(--bg);
  }
  .brand-mark { width: 32px; height: 32px; }
  .tabs {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    order: initial;
    width: auto;
    gap: 2px;
    padding: 5px 5px calc(5px + env(safe-area-inset-bottom));
    background: var(--panel);
    border-top: 1px solid var(--line);
    box-shadow: 0 -6px 20px rgba(29, 41, 38, 0.06);
    overflow: visible;
  }
  .tab {
    flex: 1 1 0;
    min-height: 50px;
    padding: 6px 2px;
    border-radius: 10px;
    font-size: 11.5px;
    display: grid;
    place-items: center;
  }
  .tab.active { background: var(--accent); color: #fff; }
  .page { padding: 12px 12px calc(86px + env(safe-area-inset-bottom)); }
  .view { gap: 12px; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 14px; }
  .kpi strong { font-size: clamp(20px, 6.5vw, 27px); }
  .kpi.accent { grid-column: 1 / -1; }
  .kpi .small { font-size: 11px; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: span 1; }
  .pie-wrap { grid-template-columns: 1fr; }
  .pie-chart { max-width: 190px; }
  .legend { grid-template-columns: 1fr; width: 100%; }
  .rangebar { align-items: stretch; padding: 12px; }
  .range-fields { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
  .range-fields label { flex: none; display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
  .range-fields input { width: 100%; }
  .range-fields #overview-refresh { grid-column: 1 / -1; }
  .presets { display: flex; width: 100%; overflow-x: auto; padding-bottom: 2px; }
  .presets .chip { flex: none; }
  .filters { padding: 13px; }
  .filter-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    overflow: visible;
    padding-bottom: 0;
    max-height: none;
  }
  .filter-item {
    min-width: 0;
    border: 1px solid var(--line);
    padding: 8px 10px;
    background: #fffefa;
  }
  .filter-item .amount { display: none; }
  #view-overview .table-scroll { overflow: visible; }
  #view-overview thead { display: none; }
  #view-overview table { display: block; }
  #view-overview tbody { display: block; }
  #view-overview tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5px 14px;
    padding: 12px 0;
    border-bottom: 1px solid var(--line);
  }
  #view-overview tr:last-child { border-bottom: 0; }
  #view-overview td { display: block; padding: 0; border: 0; }
  #view-overview td:first-child { grid-column: 1 / -1; margin-bottom: 3px; }
  #view-overview td.num {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    text-align: left;
  }
  #view-overview td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 10px;
    font-weight: 750;
    letter-spacing: 0.07em;
    text-transform: uppercase;
  }
  #view-overview td:first-child::before { content: none; }
  #view-overview td:last-child { display: flex; align-items: center; gap: 8px; }
  .receipt-controls, .receipt-actions { align-items: stretch; flex-direction: column; }
  .receipt-actions label { min-width: 0; }
  .receipt-actions button { width: 100%; }
  .file-label { min-width: 0; }
  .file-label input { padding: 12px; background: #f8f5ed; }
  .receipt-preview { width: 100%; max-height: 300px; }
  .inline-form { display: grid; grid-template-columns: 1fr 1fr; align-items: end; }
  .inline-form label { min-width: 0; }
  .inline-form label:first-child { grid-column: 1 / -1; }
  .inline-form button { grid-column: 1 / -1; width: 100%; }
  input, select, textarea { font-size: 16px; }
  button.primary, button.quiet { min-height: 44px; }
  .row-actions button { min-height: 36px; }
  #receipt-draft-wrap thead { display: none; }
  #receipt-draft-body tr {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 12px 0;
    border-bottom: 1px solid var(--line);
  }
  #receipt-draft-body td { display: block; padding: 0; border: 0; }
  #receipt-draft-body td::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 3px;
    color: var(--muted);
    font-size: 10px;
    font-weight: 750;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }
  #receipt-draft-body input, #receipt-draft-body select { width: 100%; min-width: 0; }
  #receipt-draft-body .price-input { width: 100%; }
  .table-panel { padding: 13px; }
  .form-panel { padding: 15px; }
  .chart-panel { padding: 14px; }
  .toast { bottom: calc(74px + env(safe-area-inset-bottom)); width: calc(100% - 24px); text-align: center; }
}
