/* Mobile-first, minimal. Cream/ink/in-out colour system (fix #5).
 * Light + dark via [data-theme]. No brand colour: ink is the accent.
 * Green (--in) and red (--out) appear only on money numbers; amber (--warn) on warnings. */
:root {
  --bg: #FAF6EE;
  --surface: #FFFDF8;
  --text: #1F1B16;
  --text-muted: #6B645A;
  --border: #E6DFD2;
  --accent: #1F1B16;
  --accent-soft: #F1EBDF;
  --in: #2E6B3F;
  --out: #A63D2F;
  --warn: #8A5A00;
  --warn-soft: #FDF0D5;
  --chart-grid: #E6DFD2;
  --on-accent: #FAF6EE;
  --radius: 12px;
  --tap: 48px;
}
html[data-theme="dark"] {
  --bg: #16140F;
  --surface: #201D18;
  --text: #EFE9DD;
  --text-muted: #A39B8E;
  --border: #34302A;
  --accent: #EFE9DD;
  --accent-soft: #2A2620;
  --in: #86C096;
  --out: #E8917F;
  --warn: #E0B45C;
  --warn-soft: #3A2F1C;
  --chart-grid: #34302A;
  --on-accent: #16140F;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px; line-height: 1.45;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
h2 { font-size: 1.25rem; margin: 0; }
h3 { font-size: 1rem; margin: 0 0 10px; }
p { margin: 8px 0; }

/* top bar */
#topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.tb-title { font-weight: 700; font-size: 1.1rem; }
.tb-actions { display: flex; gap: 6px; }
.icon-btn {
  position: relative; min-width: var(--tap); min-height: var(--tap);
  border: 0; background: transparent; color: var(--text); font-size: 1.3rem; border-radius: 10px;
}
.icon-btn:active { background: var(--accent-soft); }
.badge {
  position: absolute; top: 4px; right: 2px; min-width: 20px; height: 20px; padding: 0 5px;
  background: var(--out); color: var(--on-accent); font-size: 0.7rem; font-weight: 700;
  border-radius: 10px; line-height: 20px; text-align: center;
}

/* screens */
#screens { max-width: 720px; margin: 0 auto; padding: 12px 12px 24px; }
.screen { display: none; }
.screen.active { display: block; }

/* bottom nav */
#bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
#bottomnav button {
  flex: 1; border: 0; background: transparent; color: var(--text-muted);
  min-height: 64px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 0.72rem; gap: 2px;
}
#bottomnav button .ni { font-size: 1.25rem; }
#bottomnav button.active { color: var(--accent); font-weight: 700; }

/* cards */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin: 12px 0;
}
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.card-head h3 { margin: 0; }

/* quick add */
.quick-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin: 12px 0; }
.quick-btn {
  min-height: 96px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); font-size: 1rem; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
}
.quick-btn:active { background: var(--accent-soft); border-color: var(--accent); }
.qb-icon { font-size: 1.6rem; color: var(--accent); }
.alert-banner {
  width: 100%; text-align: left; border: 1px solid var(--warn); background: var(--accent-soft); color: var(--warn);
  border-radius: var(--radius); padding: 12px 14px; font-size: 0.95rem; margin: 4px 0 8px; min-height: var(--tap);
}

/* KPI */
.kpi-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 12px 0; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.kpi-label { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 4px; }
.kpi-value { font-size: 1.25rem; font-weight: 700; }
.kpi .neg, .neg { color: var(--out); }
.pos { color: var(--in); }
.kpi.warn .kpi-value { color: var(--warn); }

/* key-value rows */
.kv > div { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid var(--border); }
.kv > div:last-child { border-bottom: 0; }
.kv .indent { padding-left: 16px; }
.kv.total { font-weight: 700; font-size: 1.05rem; border-top: 2px solid var(--border); margin-top: 4px; }

/* tables */
.tbl { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.tbl th, .tbl td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; }
.tbl th { color: var(--text-muted); font-weight: 600; font-size: 0.8rem; }
.tbl .r { text-align: right; }

/* lists */
.list { display: flex; flex-direction: column; gap: 8px; }
.list-row {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; color: var(--text); font-size: 0.95rem; min-height: var(--tap);
}
.list-row:active { background: var(--accent-soft); }
.chev { margin-left: auto; color: var(--text-muted); font-size: 1.2rem; }
.lr-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.lr-top { display: flex; align-items: center; gap: 8px; }
.lr-date { font-size: 0.8rem; color: var(--text-muted); }
.lr-desc { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lr-sub { font-size: 0.82rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lr-amt { font-weight: 700; white-space: nowrap; }
.tag {
  font-size: 0.7rem; background: var(--accent-soft); color: var(--accent);
  border-radius: 6px; padding: 2px 7px; font-weight: 600; white-space: nowrap;
}
.tag.asset { background: var(--accent-soft); color: var(--warn); }
.pill { font-size: 0.72rem; font-weight: 700; border-radius: 20px; padding: 3px 10px; }
.pill.ok { background: var(--accent-soft); color: var(--in); }
.pill.low { background: var(--accent-soft); color: var(--warn); }
.pill.out { background: var(--accent-soft); color: var(--out); }
.pill.warn { background: var(--accent-soft); color: var(--warn); }

/* forms */
.field { display: block; margin: 10px 0; }
.field > span { display: block; font-size: 0.82rem; color: var(--text-muted); margin-bottom: 5px; font-weight: 600; }
input[type="text"], input[type="date"], input[type="month"], input[type="number"], input[type="search"], select, textarea {
  width: 100%; min-height: var(--tap); padding: 10px 12px; font-size: 1rem;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
textarea { font-family: monospace; font-size: 0.85rem; }
.check { display: flex; align-items: center; gap: 10px; min-height: var(--tap); font-weight: 600; }
.check input { width: 24px; height: 24px; }
.seg { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; margin: 10px 0; }
.seg button { flex: 1; border: 0; background: var(--surface); color: var(--text); min-height: var(--tap); font-size: 0.95rem; font-weight: 600; }
.seg button.on { background: var(--accent); color: var(--on-accent); }
.row { display: flex; gap: 10px; margin: 12px 0; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.btn {
  min-height: var(--tap); padding: 10px 18px; border-radius: 10px; font-size: 1rem; font-weight: 600;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.big { width: 100%; font-size: 1.1rem; }
.btn.danger { color: var(--out); border-color: var(--out); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 40px; padding: 6px 12px; font-size: 0.88rem; }
.btn:active { opacity: 0.85; }
.file { font-size: 0.9rem; }
.errors { background: var(--accent-soft); color: var(--out); border: 1px solid var(--out); border-radius: 10px; padding: 12px; margin: 10px 0; }
.warn-list { margin: 8px 0; padding-left: 20px; }
.warn-list li { margin: 6px 0; }
.form-head { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.history { list-style: none; margin: 0; padding: 0; }
.history li { padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.history li:last-child { border-bottom: 0; }

/* filters */
.filters { margin: 4px 0 8px; }
.filters input[type="search"] { margin-bottom: 8px; }
.filter-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 8px; }
.filter-row select { font-size: 0.85rem; min-height: 44px; }

/* dashboard */
.dash-head { margin: 4px 0 8px; }
.dash-head label { font-size: 0.85rem; font-weight: 600; color: var(--text-muted); display: block; }
.legend { display: flex; gap: 14px; margin-bottom: 6px; font-size: 0.82rem; color: var(--text-muted); flex-wrap: wrap; }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
canvas { width: 100%; display: block; }

/* checks */
.sev {
  width: 30px; height: 30px; border-radius: 50%; flex: none;
  display: flex; align-items: center; justify-content: center; font-weight: 800;
}
.sev.error { background: var(--accent-soft); color: var(--out); }
.sev.warning { background: var(--accent-soft); color: var(--warn); }
.bank-real { min-height: 40px; padding: 6px 8px; text-align: right; }

/* settings */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { background: var(--accent-soft); color: var(--text); border-radius: 16px; padding: 8px 14px; font-size: 0.9rem; }

/* modal + toast */
#modal-root { position: fixed; inset: 0; z-index: 50; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.45); }
.modal {
  position: absolute; left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom));
  max-height: 85vh; overflow-y: auto; background: var(--surface); color: var(--text);
  border-radius: 16px; padding: 18px; max-width: 560px; margin: 0 auto;
}
.modal-body { margin: 10px 0; }
#toast {
  position: fixed; left: 50%; transform: translateX(-50%) translateY(20px); bottom: 90px;
  background: var(--text); color: var(--bg); padding: 12px 20px; border-radius: 24px;
  font-size: 0.92rem; opacity: 0; transition: all 0.25s; z-index: 60; pointer-events: none;
  max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.muted { color: var(--text-muted); }
.small { font-size: 0.82rem; }

@media (min-width: 720px) {
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }
  .quick-grid { grid-template-columns: repeat(3, 1fr); }
}

/* fix #4: desktop layout at 1024px and wider. Phones (below 768px) are untouched. */
@media (min-width: 1024px) {
  body { padding-bottom: 0; padding-left: 216px; }
  #screens { max-width: none; padding: 20px 32px 48px; }
  #topbar { padding: 12px 32px; }
  /* left sidebar instead of the bottom bar */
  #bottomnav {
    top: 0; bottom: 0; left: 0; right: auto; width: 216px;
    flex-direction: column; justify-content: flex-start; align-items: stretch;
    border-top: 0; border-right: 1px solid var(--border);
    padding: 84px 0 0;
  }
  #bottomnav button {
    flex: none; flex-direction: row; justify-content: flex-start; align-items: center;
    min-height: 56px; padding: 0 24px; gap: 14px; font-size: 0.95rem;
  }
  #bottomnav button .ni { font-size: 1.2rem; }
  #modal-root { left: 216px; }
  #toast { left: calc(50% + 108px); }
  /* dashboard: KPIs in one row of 6, P&L and Cash side by side, charts full width */
  #screen-dashboard .kpi-grid { grid-template-columns: repeat(6, 1fr); }
  .dash-2col { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; align-items: start; }
  /* desktop ledger table */
  .ledger-table tr[onclick] { cursor: pointer; }
  .ledger-table tr[onclick]:hover { background: var(--accent-soft); }
}

/* Round 3: card-limit usage bars, warning cards, dash header */
.bar { height: 8px; background: var(--border); border-radius: 99px; overflow: hidden; margin: 2px 0 8px; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 99px; }
.bar-fill.warn { background: var(--warn); }
.card.warn { border-color: var(--warn); background: var(--warn-soft); }
.dash-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.dash-head .seg { margin: 0; flex: 0 0 auto; min-width: 160px; }
/* Round 3: muted second line under ledger amounts (computed NPR cost) */
.lr-subamt { margin-top: 2px; }
/* Round 4: sync pill in the top bar */
.sync-pill { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 6px 10px; font-size: 0.8rem; font-weight: 600; min-height: 36px; }
#sync-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: 0 0 auto; }
.sync-pill.st-ok #sync-dot { background: var(--pos); }
.sync-pill.st-busy #sync-dot { background: var(--warn); animation: pulse 1.2s infinite; }
.sync-pill.st-warn #sync-dot { background: var(--warn); }
.sync-pill.st-err #sync-dot { background: var(--neg); }
.sync-pill.st-off #sync-dot { background: var(--muted); }
@keyframes pulse { 50% { opacity: 0.35; } }
/* Round 4: login gate */
.login-wrap { max-width: 420px; margin: 8vh auto; padding: 0 16px; }
.err { color: var(--neg); font-size: 0.9rem; }
/* Round 4: checks header + conflict field picker */
.checks-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.checks-head h3 { margin: 0; }
.checks-head .seg { margin: 0; flex: 0 0 auto; min-width: 200px; }
.cf-pick { display: flex; gap: 6px; align-items: flex-start; cursor: pointer; }
