/* ---------- tokens ---------- */
:root {
  --accent: #0d9488;
  --accent-dark: #0f766e;
  --accent-light: #e6f7f5;
  --danger: #dc2626;
  --danger-light: #fde8e8;
  --warn: #d97706;
  --warn-light: #fef3dc;
  --bg: #f7fafa;
  --surface: #ffffff;
  --surface-2: #f1f5f4;
  --text: #1a2420;
  --text-muted: #667370;
  --border: #e3e9e7;
  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 22px;
  --shadow: 0 1px 3px rgba(20, 30, 28, 0.08), 0 1px 2px rgba(20, 30, 28, 0.06);
  --shadow-lg: 0 8px 24px rgba(20, 30, 28, 0.14);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #2dd4bf;
    --accent-dark: #14b8a6;
    --accent-light: #0f2e2b;
    --danger-light: #3a1515;
    --warn-light: #3a2a0c;
    --bg: #0f1413;
    --surface: #1a2220;
    --surface-2: #212b29;
    --text: #eef2f1;
    --text-muted: #9bb0ac;
    --border: #2b3634;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 28px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

h1, h2, h3, h4, p { margin: 0; }
button { font-family: inherit; }
input, select, textarea { font-family: inherit; }

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

.app-root { min-height: 100vh; }

/* ---------- utility ---------- */
.hidden { display: none !important; }
.row { display: flex; align-items: center; }
.row-between { display: flex; align-items: center; justify-content: space-between; }
.gap-1 { gap: 6px; }
.gap-2 { gap: 12px; }
.muted { color: var(--text-muted); }
.text-sm { font-size: 13px; }
.spacer { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 14px; }

/* ---------- buttons & inputs ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform 0.05s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-block { width: 100%; }
.btn-sm { padding: 8px 12px; font-size: 14px; min-height: 36px; }
.btn[disabled] { opacity: 0.55; cursor: not-allowed; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; color: var(--text-muted); }
.input, select.input, textarea.input {
  font-size: 16px;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-height: 44px;
  width: 100%;
}
.input:focus, select.input:focus, textarea.input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
textarea.input { min-height: 72px; resize: vertical; }

.form-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }
@media (min-width: 560px) {
  .form-grid.cols-2 { grid-template-columns: 1fr 1fr; }
}

/* ---------- layout shell ---------- */
.shell { display: flex; flex-direction: column; min-height: 100vh; }
.shell-main { flex: 1; min-width: 0; }
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
}
.topbar {
  background: var(--bg);
  padding: calc(10px + env(safe-area-inset-top)) 16px 8px;
  display: flex; align-items: center; justify-content: space-between;
}
.brand { font-size: 20px; font-weight: 800; letter-spacing: -0.02em; color: var(--accent-dark); }
.main-content {
  flex: 1;
  padding: 0 16px calc(96px + env(safe-area-inset-bottom));
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-around;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.tab-btn {
  background: none; border: none; color: var(--text-muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; font-weight: 600; min-width: 56px; min-height: 48px;
  padding: 4px 2px; border-radius: 10px; cursor: pointer;
}
.tab-btn .ic { font-size: 20px; line-height: 1; }
.tab-btn.active { color: var(--accent-dark); }
.tab-btn-add {
  background: var(--accent); color: #fff;
  width: 56px; height: 56px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 26px; margin-top: -26px; box-shadow: var(--shadow-lg);
  border: 4px solid var(--bg);
}
.tab-btn-add .ic { font-size: 26px; }

@media (min-width: 900px) {
  .shell { flex-direction: row; }
  .tabbar {
    position: static; flex-direction: column; justify-content: flex-start;
    position: sticky; top: 0; flex-shrink: 0;
    width: 220px; height: 100vh; border-top: none; border-right: 1px solid var(--border);
    padding: 24px 14px; gap: 6px; align-items: stretch;
  }
  .tab-btn {
    flex-direction: row; justify-content: flex-start; font-size: 15px;
    padding: 12px 14px; width: 100%; gap: 12px;
  }
  .tab-btn-add {
    width: auto; height: auto; border-radius: var(--radius-sm); margin-top: 0;
    border: none; justify-content: flex-start; padding: 12px 14px; flex-direction: row;
  }
  .tab-btn-add .ic { font-size: 18px; }
  .main-content { padding: 0 32px 48px; max-width: 980px; }
  .topbar { padding: 24px 32px 10px; }
  .month-nav-inner { padding: 0 32px 12px; max-width: 980px; }
}

/* ---------- login ---------- */
.login-screen {
  min-height: 100vh; display: flex; flex-direction: column; align-items: center;
  justify-content: center; padding: 24px calc(24px + env(safe-area-inset-left));
  gap: 28px;
}
.login-brand { text-align: center; }
.login-brand h1 { font-size: 34px; font-weight: 800; color: var(--accent-dark); letter-spacing: -0.03em; }
.login-brand p { color: var(--text-muted); margin-top: 6px; }
.login-card {
  background: var(--surface); border-radius: var(--radius-lg); padding: 28px;
  width: 100%; max-width: 380px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 16px;
}
.login-error { color: var(--danger); font-size: 14px; min-height: 18px; }

/* ---------- cards ---------- */
.card {
  background: var(--surface); border-radius: var(--radius); padding: 18px;
  box-shadow: var(--shadow); border: 1px solid var(--border);
}
.card h3 { font-size: 15px; font-weight: 700; margin-bottom: 10px; }

.summary-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 640px) { .summary-grid { grid-template-columns: 1fr 1fr; } }

.summary-card .big { font-size: 24px; font-weight: 800; margin: 4px 0; }
.summary-card.warn-card { border-color: var(--warn); background: var(--warn-light); }
.summary-card .label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }

@media (min-width: 900px) {
  .dashboard-columns { display: grid; grid-template-columns: 1.4fr 1fr; gap: 20px; align-items: start; }
}

/* ---------- progress ---------- */
.progress-track {
  background: var(--surface-2); border-radius: 999px; height: 8px; overflow: hidden; width: 100%;
}
.progress-fill { height: 100%; border-radius: 999px; background: var(--accent); }
.progress-fill.warn { background: var(--warn); }
.progress-fill.over { background: var(--danger); }

/* ---------- month/person picker (sticky under the app header) ---------- */
.month-nav {
  background: var(--bg);
}
.month-nav[hidden] { display: none !important; }
.month-nav-inner {
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
  padding: 0 16px 10px;
}
.month-picker { display: flex; align-items: center; gap: 8px; min-width: 0; }
.month-picker .month-label { font-weight: 700; font-size: 15px; min-width: 132px; text-align: center; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 50%;
  width: 40px; height: 40px; display: flex; align-items: center; justify-content: center;
  font-size: 18px; cursor: pointer; color: var(--text); flex-shrink: 0;
}
.filter-bar {
  display: flex; flex-wrap: nowrap; gap: 8px; align-items: center;
  justify-content: space-between; margin-bottom: 0; min-height: 44px;
}
.filter-bar #person-filter {
  width: auto; min-width: 0; flex: 0 1 auto;
  min-height: 40px; padding: 8px 12px;
}
.month-nav.is-compact {
  box-shadow: 0 1px 0 var(--border);
}
.month-nav.is-compact .month-nav-inner { padding-bottom: 8px; }
.month-nav.is-compact .pace-hint {
  max-height: 0;
  opacity: 0;
  margin-top: 0;
  padding-top: 0;
  padding-bottom: 0;
  pointer-events: none;
}

/* ---------- category rows ---------- */
.category-row { display: flex; flex-direction: column; gap: 6px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.category-row:last-child { border-bottom: none; }
.category-row-top { display: flex; align-items: center; gap: 10px; }
.category-row .emoji { font-size: 20px; }
.category-row .name { font-weight: 600; flex: 1; }
.category-row .amounts { font-size: 13px; color: var(--text-muted); white-space: nowrap; }
.category-row .amounts.over { color: var(--danger); font-weight: 700; }
.category-row .amounts.warn { color: var(--warn); font-weight: 700; }
.badge {
  font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 999px;
  background: var(--warn-light); color: var(--warn); text-transform: uppercase; letter-spacing: 0.03em;
}
.badge.danger { background: var(--danger-light); color: var(--danger); }

.overspend-item { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.overspend-item:last-child { border-bottom: none; }
.overspend-item .over-amount { color: var(--danger); font-weight: 700; }

.merchant-item { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.merchant-item:last-child { border-bottom: none; }

.pace-hint {
  background: var(--accent-light); color: var(--accent-dark); border-radius: var(--radius-sm);
  padding: 6px 12px; font-size: 13px; font-weight: 600;
  margin-top: 8px;
  overflow: hidden;
  max-height: 4.5em;
  opacity: 1;
  transition: max-height 0.2s ease, opacity 0.15s ease, margin 0.2s ease, padding 0.2s ease;
}

/* ---------- chips (category select) ---------- */
.chip-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1.5px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 8px 14px; font-size: 14px; display: inline-flex; align-items: center; gap: 6px;
  cursor: pointer; min-height: 40px;
}
.chip.selected { border-color: var(--accent); background: var(--accent-light); color: var(--accent-dark); font-weight: 700; }

/* ---------- add / scan ---------- */
.add-choices { display: flex; flex-direction: column; gap: 14px; margin-top: 10px; }
.big-choice {
  background: var(--surface); border: 1.5px solid var(--border); border-radius: var(--radius-lg);
  padding: 26px; display: flex; flex-direction: column; align-items: center; gap: 10px;
  cursor: pointer; text-align: center; box-shadow: var(--shadow);
}
.big-choice .ic { font-size: 40px; }
.big-choice h3 { font-size: 17px; }
.big-choice p { color: var(--text-muted); font-size: 13px; }

.scan-preview { width: 100%; border-radius: var(--radius); overflow: hidden; background: var(--surface-2); }
.scan-preview img { width: 100%; display: block; }
.scan-status { display: flex; align-items: center; gap: 10px; justify-content: center; padding: 14px; font-weight: 600; color: var(--text-muted); }
.spinner {
  width: 18px; height: 18px; border-radius: 50%; border: 3px solid var(--border);
  border-top-color: var(--accent); animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.collapsible-header { display: flex; justify-content: space-between; align-items: center; cursor: pointer; padding: 6px 0; }
.line-item-row { display: grid; grid-template-columns: 1fr 70px 90px auto; gap: 8px; align-items: center; margin-bottom: 8px; }

/* ---------- history ---------- */
.day-group { margin-bottom: 18px; }
.day-group-label { font-size: 13px; font-weight: 700; color: var(--text-muted); margin: 18px 0 8px; text-transform: uppercase; letter-spacing: 0.03em; }
.txn-row {
  display: flex; align-items: center; gap: 12px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px 14px; margin-bottom: 8px;
  cursor: pointer; min-height: 44px;
}
.txn-row .emoji { font-size: 22px; width: 32px; text-align: center; flex-shrink: 0; }
.txn-row .info { flex: 1; min-width: 0; }
.txn-row .merchant { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.txn-row .sub { font-size: 12px; color: var(--text-muted); }
.txn-row .amount { font-weight: 700; white-space: nowrap; }
.txn-row .amount.refund { color: var(--accent-dark); }
.initials-badge {
  width: 24px; height: 24px; border-radius: 50%; background: var(--accent-light); color: var(--accent-dark);
  font-size: 10px; font-weight: 800; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.receipt-ic { font-size: 14px; flex-shrink: 0; }

.empty-state { text-align: center; color: var(--text-muted); padding: 48px 16px; }
.empty-state .ic { font-size: 40px; margin-bottom: 10px; }

/* ---------- modal / sheet ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(10, 16, 14, 0.5); z-index: 100;
  display: flex; align-items: flex-end; justify-content: center;
}
@media (min-width: 640px) { .modal-overlay { align-items: center; } }
.modal-sheet {
  background: var(--surface); border-radius: var(--radius-lg) var(--radius-lg) 0 0; width: 100%;
  max-width: 560px; max-height: 92vh; overflow-y: auto; padding: 20px 20px calc(24px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow-lg);
}
@media (min-width: 640px) { .modal-sheet { border-radius: var(--radius-lg); max-height: 88vh; } }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal-header h2 { font-size: 18px; }
.modal-close { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text-muted); min-width: 44px; min-height: 44px; }

.receipt-img-view { width: 100%; border-radius: var(--radius-sm); margin-bottom: 14px; }

/* ---------- settings ---------- */
.settings-section { margin-bottom: 22px; }
.settings-list-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border);
}
.settings-list-item:last-child { border-bottom: none; }
.settings-list-item .name { font-weight: 600; }
.settings-list-item .sub { font-size: 12px; color: var(--text-muted); }
.inactive-tag { font-size: 11px; color: var(--text-muted); background: var(--surface-2); padding: 2px 8px; border-radius: 999px; }
.cat-edit-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.cat-edit-row input[type=text] { width: 100%; }
.cat-edit-row .emoji-input { width: 48px; text-align: center; }
.cat-edit-row .limit-input { width: 90px; }
.coming-soon-card { border-style: dashed; }

.import-batch-row {
  display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border);
}
.import-batch-row:last-child { border-bottom: none; }
.import-batch-row .name { font-weight: 600; }

/* ---------- bank statement import review ---------- */
.inout-grid { margin-top: 0; margin-bottom: 12px; grid-template-columns: 1fr 1fr; }
.inout-grid .summary-card:last-child { grid-column: 1 / -1; }
@media (min-width: 640px) {
  .inout-grid { grid-template-columns: 1fr 1fr 1fr; }
  .inout-grid .summary-card:last-child { grid-column: auto; }
}

.import-totals-bar {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 14px;
}
@media (min-width: 560px) { .import-totals-bar { grid-template-columns: repeat(4, 1fr); } }
.imp-total {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 10px 12px; display: flex; flex-direction: column; gap: 2px;
}
.imp-total .label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }
.imp-total .val { font-size: 18px; font-weight: 800; }
.imp-total .val.income { color: var(--accent-dark); }
.imp-total .val.outgoing { color: var(--text); }

.imp-list { padding: 4px 14px; }
.imp-line {
  display: flex; align-items: flex-start; gap: 10px; padding: 12px 0; border-bottom: 1px solid var(--border);
}
.imp-line:last-child { border-bottom: none; }
.imp-line.excluded { opacity: 0.5; }
.imp-check { display: flex; align-items: center; min-height: 44px; }
.imp-check input { width: 22px; height: 22px; }
.imp-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.imp-row1 { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.imp-date { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.imp-merchant { font-weight: 600; flex: 1; min-width: 120px; overflow: hidden; text-overflow: ellipsis; }
.imp-amount { font-weight: 800; white-space: nowrap; }
.imp-amount.in { color: var(--accent-dark); }
.imp-amount.out { color: var(--text); }
.imp-row2 { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.imp-cat { width: auto; min-width: 180px; max-width: 100%; min-height: 40px; padding: 8px 12px; flex: 1; }
.imp-cat[disabled] { opacity: 0.5; }
.imp-transfer { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; min-height: 40px; }
.imp-transfer input { width: 18px; height: 18px; }
.imp-reason { margin-top: -2px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: calc(96px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 14px;
  z-index: 200; box-shadow: var(--shadow-lg); max-width: 90vw; text-align: center;
}
@media (min-width: 900px) { .toast { bottom: 24px; } }

.app-version { color: var(--text-muted); font-size: 12px; text-align: center; padding: 8px 0; }

/* ---------- QA tweaks (Molo) ---------- */
@media (max-width: 899px) {
  .tab-btn-add > span:not(.ic) { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
}
@media (max-width: 559px) {
  .line-item-row { grid-template-columns: 1fr 1fr auto; }
  .line-item-row > :first-child { grid-column: 1 / -1; }
  .line-item-row { padding-bottom: 8px; border-bottom: 1px solid var(--border); }
}
@media (min-width: 1200px) {
  .main-content { max-width: 1160px; }
  .month-nav-inner { max-width: 1160px; }
}
.tab-btn-add .ic { font-weight: 300; color: #fff; }
@media (max-width: 899px) { .tab-btn-add .ic { font-size: 34px; line-height: 1; margin-top: -3px; } }

@media (max-width: 559px) {
  .cat-edit-row { display: grid; grid-template-columns: 48px 120px 1fr auto; gap: 8px; align-items: center; }
  .cat-edit-row .emoji-input { grid-column: 1; grid-row: 1; }
  .cat-edit-row input[type=text] { grid-column: 2 / 4; grid-row: 1; }
  .cat-edit-row [data-archive] { grid-column: 4; grid-row: 1; }
  .cat-edit-row .limit-input { grid-column: 2; grid-row: 2; width: 100%; }
  .cat-edit-row label { grid-column: 3 / 5; grid-row: 2; }
  .settings-list-item { flex-wrap: wrap; }
}
