/* ============ Tokens ============ */
:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --accent: #4f6df5;
  --accent-contrast: #fff;
  --accent-soft: rgba(79, 109, 245, 0.12);
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #e2e5ea;
  --text: #1c2128;
  --text-muted: #6b7280;
  --success: #1a7f37;
  --success-soft: rgba(26, 127, 55, 0.12);
  --danger: #d1242f;
  --danger-soft: rgba(209, 36, 47, 0.12);
  --radius-s: 6px;
  --radius-m: 10px;
  --radius-l: 16px;
  --shadow-s: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-m: 0 4px 16px rgba(0, 0, 0, 0.10);
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;
  --sidebar-w: 220px;
  --tabbar-h: 60px;
  --content-max-w: 980px;   /* main content column */
  --name-w: 90px;            /* net-row name column */
  --name-val-w: 84px;        /* net-row value column */
  --input-narrow-w: 120px;   /* narrow text inputs (e.g. currency code) */
  --sheet-h: 92dvh;          /* add/edit sheet max height (mobile) */
  --sheet-max-h: 88dvh;      /* add/edit sheet max height (desktop) */
}

:root[data-theme="dark"] {
  --accent: #7c93ff;
  --accent-soft: rgba(124, 147, 255, 0.16);
  --bg: #12141a;
  --surface: #1b1e26;
  --surface-2: #232733;
  --border: #2d323e;
  --text: #e6e9ef;
  --text-muted: #98a1b0;
  --success: #3fb950;
  --success-soft: rgba(63, 185, 80, 0.16);
  --danger: #f85149;
  --danger-soft: rgba(248, 81, 73, 0.16);
  --shadow-s: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-m: 0 4px 16px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent: #7c93ff;
    --accent-soft: rgba(124, 147, 255, 0.16);
    --bg: #12141a;
    --surface: #1b1e26;
    --surface-2: #232733;
    --border: #2d323e;
    --text: #e6e9ef;
    --text-muted: #98a1b0;
    --success: #3fb950;
    --success-soft: rgba(63, 185, 80, 0.16);
    --danger: #f85149;
    --danger-soft: rgba(248, 81, 73, 0.16);
    --shadow-s: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-m: 0 4px 16px rgba(0, 0, 0, 0.4);
  }
}

/* ============ Base ============ */
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
/* Never let the document pan horizontally (iOS "sheet slides sideways" guard).
   `clip` — not `hidden` — so no scroll container is created and `position: sticky`
   (.topbar / .sidebar) keeps working. Supported in modern browsers; ignored elsewhere. */
html { overflow-x: clip; }
/* Subtle scrollbars (if any scroll appears) */
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
*::-webkit-scrollbar-track { background: transparent; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; }
h1, h2, h3 { margin: 0; }

/* ============ Layout ============ */
.app { display: flex; min-height: 100dvh; }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100dvh;
  padding: var(--sp-5) var(--sp-4);
  display: flex;
  flex-direction: column;
  gap: var(--sp-5);
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.brand { font-size: 20px; font-weight: 700; }
.nav { display: flex; flex-direction: column; gap: var(--sp-1); }
.nav a {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 10px 12px;
  border-radius: var(--radius-m);
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }

.icon {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}
.icon-sm { width: 16px; height: 16px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: none; }

.content {
  padding: var(--sp-5) var(--sp-6) var(--sp-6);
  max-width: var(--content-max-w);
  width: 100%;
  margin: 0 auto;
}
.content-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
}
.page-title { font-size: 22px; font-weight: 700; }

.view { display: none; }
.view.active { display: block; }

.tabbar { display: none; }

@media (max-width: 899px) {
  .sidebar { display: none; }
  .topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    position: sticky;
    top: 0;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    z-index: 20;
  }
  .content { padding: var(--sp-4) var(--sp-4) calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom)); }
  .content-head { display: none; }
  .tabbar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--tabbar-h);
    background: var(--surface);
    border-top: 1px solid var(--border);
    z-index: 30;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 11px;
  }
  .tabbar a.active { color: var(--accent); }
}

/* ============ Buttons ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: 9px 16px;
  border-radius: var(--radius-m);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn-ghost:hover { background: var(--accent-soft); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger); filter: brightness(1.08); }
.btn-icon { padding: 9px; }
.btn-block { width: 100%; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-s);
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

/* ============ Fields ============ */
.field { margin-bottom: var(--sp-4); }
.field > label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: var(--sp-2);
}
.input, select.input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}
.input:focus, select.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.input.search { margin-bottom: 0; }
.input.input-narrow { width: var(--input-narrow-w); }
.input:disabled { opacity: 0.55; }
/* Form controls must never be wider than their container (prevents horizontal overflow on narrow screens). */
input, select, textarea { max-width: 100%; }

/* ============ Chips & segmented ============ */
.chips { display: flex; flex-wrap: wrap; gap: var(--sp-2); }
.chip {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  user-select: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%; overflow: hidden; /* clip (not overflow) if a name/tag is longer than the row */
}
.chip:hover { background: var(--surface-2); }
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chip .chip-x { font-weight: 700; opacity: 0.7; }
.chip .chip-x:hover { opacity: 1; }

.seg {
  display: inline-flex;
  background: var(--surface-2);
  border-radius: var(--radius-m);
  padding: 3px;
  gap: 2px;
}
.seg button {
  border: 0;
  background: transparent;
  padding: 7px 14px;
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-s); font-weight: 600; }

/* ============ Badges, avatars, tags ============ */
.badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
  display: inline-block;
}
.badge-payment { background: var(--accent-soft); color: var(--accent); }
.badge-transfer { background: var(--success-soft); color: var(--success); }

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
  font-size: 14px;
}
.avatar-sm { width: 28px; height: 28px; font-size: 12px; }

.tag-chip {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  display: inline-block;
}

/* ============ Cards ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-l);
  padding: var(--sp-4) var(--sp-5);
  margin-bottom: var(--sp-4);
  box-shadow: var(--shadow-s);
}
.card h3 { font-size: 15px; font-weight: 700; margin-bottom: var(--sp-3); }

/* ============ Month header (transactions) ============ */
.month-head {
  margin-bottom: var(--sp-4);
}
.month-nav {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-bottom: var(--sp-3);
}
.month-nav select.input { width: auto; min-width: 170px; }
.month-stats {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-4);
  font-size: 14px;
  color: var(--text-muted);
}
.month-stats .stat b { color: var(--text); font-variant-numeric: tabular-nums; }
.month-stats .stat.settled b { color: var(--success); }

.month-net {
  margin-top: var(--sp-3);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.net-row { display: flex; align-items: center; gap: var(--sp-2); font-size: 13px; }
.net-row .net-name { width: var(--name-w); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-muted); }
.net-row .net-val { width: var(--name-val-w); text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.net-val.pos { color: var(--success); }
.net-val.neg { color: var(--danger); }
.net-val.zero { color: var(--text-muted); }

.bar-track {
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
  flex: 1;
}
.bar-fill { height: 100%; border-radius: 3px; background: var(--accent); }
.bar-fill.pos { background: var(--success); }
.bar-fill.neg { background: var(--danger); }

/* ============ Filters bar ============ */
.filters-bar {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
}
.filters-bar .chips { flex-wrap: wrap; }
.filters-bar .search { margin-top: 2px; }

/* ============ Transactions list ============ */
.tx-group { margin-bottom: var(--sp-2); }
.tx-group-head {
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
  padding: var(--sp-3) 8px var(--sp-2);
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  display: flex;
  gap: var(--sp-3);
}
.tx-row {
  display: flex;
  gap: var(--sp-3);
  padding: 12px 8px;
  border-bottom: 1px solid var(--border);
  align-items: center;
  background: var(--surface);
  border-radius: var(--radius-m);
}
.tx-row + .tx-row { margin-top: 2px; }
.tx-row:hover .row-actions { opacity: 1; }
.tx-main { flex: 1; min-width: 0; }
.tx-primary { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-secondary {
  font-size: 12.5px;
  color: var(--text-muted);
  margin-top: 2px;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-wrap: wrap;
}
.row-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.12s;
}
@media (max-width: 899px) {
  .row-actions { opacity: 1; }
}
.tx-amount {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  white-space: nowrap;
}
.tx-row.transfer .tx-amount { color: var(--success); }
.tx-row.transfer .tx-primary::before { content: "⇄ "; color: var(--success); }

/* ============ Owes view ============ */
.owes-section { margin-top: var(--sp-5); }
.owes-section h3 { font-size: 15px; font-weight: 700; margin-bottom: var(--sp-3); }
.balance-row {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-2) 8px;
}
.balance-row .name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.balance-row .bal { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bal.pos { color: var(--success); }
.bal.neg { color: var(--danger); }
.bal.zero { color: var(--text-muted); font-weight: 500; }
.balance-row .bar-track { max-width: 220px; }

.owes-list { display: flex; flex-direction: column; gap: var(--sp-2); }
.owes-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  font-size: 14px;
}
.owes-item .owes-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.owes-item .owes-amt { font-weight: 700; font-variant-numeric: tabular-nums; }

/* ============ Reports ============ */
.report-filters {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
}
.rf-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-3);
}
.rf-row label { font-size: 13px; font-weight: 600; color: var(--text-muted); display: inline-flex; align-items: center; gap: var(--sp-2); }
.rf-row input[type="month"] { width: auto; }

.report-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-4);
}
@media (max-width: 899px) {
  .report-grid { grid-template-columns: 1fr; }
}

.chart-svg { width: 100%; height: auto; display: block; }
.chart-svg text { font-family: var(--font); }
.chart-label { fill: var(--text-muted); font-size: 12px; }
.chart-value { fill: var(--text); font-size: 12px; font-weight: 600; }
.chart-axis { stroke: var(--border); stroke-width: 1; }

.donut-wrap { display: flex; align-items: center; gap: var(--sp-4); flex-wrap: wrap; }
.donut-legend { flex: 1; min-width: 140px; display: flex; flex-direction: column; gap: 6px; }
.legend-item { display: flex; align-items: center; gap: var(--sp-2); font-size: 13px; min-width: 0; }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
.legend-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-value { font-weight: 600; font-variant-numeric: tabular-nums; }
.legend-pct { color: var(--text-muted); font-size: 12px; width: 40px; text-align: right; }

.summary-list { display: flex; flex-direction: column; gap: var(--sp-2); font-size: 14px; }
.summary-row { display: flex; justify-content: space-between; gap: var(--sp-3); }
.summary-row .k { color: var(--text-muted); }
.summary-row .v { font-weight: 600; font-variant-numeric: tabular-nums; }

/* ============ Settings ============ */
.settings-row {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-2) 0;
}
.settings-row .name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.settings-row input.input { max-width: 220px; }

/* ============ Transaction form (sheet body) ============ */
.form-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 var(--sp-4); }
.form-grid .field { min-width: 0; } /* let grid tracks shrink below content min-width (no horizontal overflow) */
.form-grid .field-full { grid-column: 1 / -1; }

.split-rows { display: flex; flex-direction: column; gap: var(--sp-2); margin-bottom: var(--sp-3); }
.split-row {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2);
  background: var(--surface-2);
  border-radius: var(--radius-m);
}
.split-row .name { flex: 1; min-width: 0; font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.split-row input.input { width: 110px; padding: 8px 10px; text-align: right; font-variant-numeric: tabular-nums; }
.split-row .suffix { font-size: 13px; color: var(--text-muted); width: 18px; text-align: center; }
.split-row .amt { font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; color: var(--text-muted); min-width: 70px; text-align: right; }


.split-preview {
  font-size: 13px;
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: var(--radius-m);
  padding: var(--sp-3);
  margin-bottom: var(--sp-3);
  font-variant-numeric: tabular-nums;
}
.split-preview b { color: var(--text); }
.split-error {
  font-size: 13px;
  color: var(--danger);
  background: var(--danger-soft);
  border-radius: var(--radius-m);
  padding: var(--sp-3);
  margin-bottom: var(--sp-3);
}

/* ============ Sheet ============ */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; }
.sheet-wrap[hidden] { display: none; }
body.sheet-open { overflow: hidden; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.4); }
.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: auto; /* fit content; scroll only when taller than --sheet-h */
  max-height: var(--sheet-h);
  background: var(--surface);
  border-radius: var(--radius-l) var(--radius-l) 0 0;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-m);
  overflow: hidden;
}
@media (max-width: 899px) {
  .sheet { animation: sheetUp 0.22s ease; }
  @keyframes sheetUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
}
@media (min-width: 900px) {
  .sheet {
    left: 50%;
    right: auto;
    bottom: auto;
    top: 50%;
    transform: translate(-50%, -50%);
    width: min(560px, 92vw);
    height: auto;
    max-height: var(--sheet-max-h);
    border-radius: var(--radius-l);
    animation: sheetPop 0.16s ease;
  }
  @keyframes sheetPop { from { opacity: 0; transform: translate(-50%, -49%) scale(0.98); } to { opacity: 1; transform: translate(-50%, -50%) scale(1); } }
}
.sheet-handle {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: var(--sp-2) auto 0;
}
@media (min-width: 900px) { .sheet-handle { display: none; } }
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-3) var(--sp-5);
  border-bottom: 1px solid var(--border);
}
.sheet-head h2 { font-size: 17px; font-weight: 700; }
.sheet-body {
  padding: var(--sp-4) var(--sp-5) var(--sp-5);
  overflow-y: auto;
  overflow-x: hidden; /* vertical-only scroll: prevents the form panning sideways on touch (iOS) */
  overscroll-behavior: contain; /* no pull-to-refresh / scroll chaining inside sheet */
  flex: 1;
  min-height: 0; /* allow flex child to shrink so overflow kicks in */
}
.sheet-body .field:last-child { margin-bottom: 0; }
.sheet-foot {
  padding: var(--sp-3) var(--sp-5) calc(var(--sp-4) + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  display: flex;
  gap: var(--sp-3);
  justify-content: flex-end;
}

/* ============ Toasts ============ */
.toasts {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  align-items: center;
  pointer-events: none;
}
@media (max-width: 899px) {
  .toasts { bottom: calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom)); }
}
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: var(--radius-m);
  box-shadow: var(--shadow-m);
  font-size: 14px;
  animation: toastIn 0.18s ease;
  max-width: 90vw;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.out { opacity: 0; transition: opacity 0.2s; }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ============ Empty state & utilities ============ */
.empty {
  text-align: center;
  color: var(--text-muted);
  padding: var(--sp-6) 0;
  font-size: 14px;
}
.muted { color: var(--text-muted); font-size: 13px; margin: 0 0 var(--sp-2); }
.row { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }
