:root {
  color-scheme: light;
  --bg: #f7f2ea;
  --surface: #fffdfa;
  --surface-raised: #ffffff;
  --border: #e6dcca;
  --ink: #2b2318;
  --ink-secondary: #6b5f4d;
  --muted: #93876f;

  --accent: #b5652d;
  --accent-dark: #8f4f22;
  --accent-ink: #ffffff;

  /* status palette — reused for due-date urgency & payment state, never for decoration */
  --good: #0ca30c;
  --good-bg: #e5f6e0;
  --warning: #c98500;
  --warning-bg: #fdf0d4;
  --critical: #d03b3b;
  --critical-bg: #fbe6e4;

  /* single-hue sequential accent for magnitude charts (revenue) */
  --series-1: #2a78d6;
  --series-1-bg: #dfeafb;

  /* fixed-order categorical set for order-method breakdown (validated, adjacent-safe) */
  --cat-1: #2a78d6;
  --cat-2: #eb6834;
  --cat-3: #1baf7a;
  --cat-4: #eda100;
  --cat-5: #e87ba4;
  --cat-6: #008300;
  --cat-7: #4a3aa7;

  --radius-s: 8px;
  --radius-m: 14px;
  --radius-l: 20px;
  --tap: 48px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #17140f;
    --surface: #221e17;
    --surface-raised: #2a251c;
    --border: #3a3327;
    --ink: #f3ede1;
    --ink-secondary: #cabfa9;
    --muted: #8f8368;

    --accent: #d97b3f;
    --accent-dark: #e79256;
    --accent-ink: #1a1208;

    --good: #0ca30c;
    --good-bg: #163315;
    --warning: #e0a422;
    --warning-bg: #3a2c0e;
    --critical: #e66767;
    --critical-bg: #3a1918;

    --series-1: #3987e5;
    --series-1-bg: #16283f;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
}

h1, h2, h3 { margin: 0 0 8px; font-weight: 700; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding: max(14px, env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.15rem; color: var(--accent-dark); }

.app { padding: 16px; max-width: 640px; margin: 0 auto; }

/* Bottom nav */
.bottomnav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--surface-raised);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 20;
}
.navbtn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: none;
  background: none;
  color: var(--ink-secondary);
  font-size: 0.72rem;
  font-family: inherit;
  padding: 8px 0 6px;
  min-height: var(--tap);
}
.navbtn .navicon { font-size: 1.3rem; line-height: 1; }
.navbtn.active { color: var(--accent); font-weight: 700; }

/* Forms */
.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink-secondary);
  margin-bottom: 6px;
}
input[type="text"],
input[type="number"],
input[type="date"],
select,
textarea {
  width: 100%;
  font-size: 1rem;
  font-family: inherit;
  padding: 12px 14px;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  background: var(--surface);
  color: var(--ink);
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 12px 20px;
  border-radius: var(--radius-m);
  border: none;
  font-size: 1rem;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  width: 100%;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:active { background: var(--accent-dark); }
.btn-secondary { background: var(--surface-raised); color: var(--ink); border: 1px solid var(--border); }
.btn-danger { background: var(--critical-bg); color: var(--critical); border: 1px solid var(--critical); }
.btn-ghost { background: none; color: var(--ink-secondary); }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

/* Segmented control (payment status) */
.segmented { display: flex; border: 1px solid var(--border); border-radius: var(--radius-m); overflow: hidden; }
.segmented button {
  flex: 1;
  min-height: var(--tap);
  border: none;
  background: var(--surface);
  color: var(--ink-secondary);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  border-left: 1px solid var(--border);
}
.segmented button:first-child { border-left: none; }
.segmented button.active[data-val="Unpaid"] { background: var(--critical-bg); color: var(--critical); }
.segmented button.active[data-val="Partial"] { background: var(--warning-bg); color: var(--warning); }
.segmented button.active[data-val="Paid"] { background: var(--good-bg); color: var(--good); }

/* Line items */
.line-item {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  margin-bottom: 10px;
  position: relative;
}
.line-item .field { flex: 1; margin-bottom: 0; }
.line-item .qty-field { flex: 0 0 76px; }
.line-item .remove-btn {
  flex: 0 0 var(--tap);
  height: var(--tap);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--critical);
  border-radius: var(--radius-s);
  font-size: 1.1rem;
}

.autocomplete { position: relative; }
.autocomplete-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  max-height: 180px;
  overflow-y: auto;
  z-index: 30;
  box-shadow: 0 6px 20px rgba(0,0,0,0.12);
}
.autocomplete-list div {
  padding: 12px 14px;
  font-size: 0.95rem;
  border-bottom: 1px solid var(--border);
}
.autocomplete-list div:last-child { border-bottom: none; }
.autocomplete-list div:active { background: var(--bg); }

/* Filter chips */
.chiprow { display: flex; gap: 8px; margin-bottom: 14px; }
.chip {
  flex: 1;
  min-height: 40px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-secondary);
  font-family: inherit;
  font-weight: 600;
  font-size: 0.88rem;
}
.chip.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* Order cards */
.order-card {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-left: 5px solid var(--border);
  border-radius: var(--radius-m);
  padding: 14px 14px;
  margin-bottom: 10px;
  font-family: inherit;
  color: var(--ink);
}
.order-card.urgency-overdue { border-left-color: var(--critical); }
.order-card.urgency-soon { border-left-color: var(--warning); }
.order-card.urgency-paid-accent { border-left-color: var(--good); }

.order-card-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.order-card-name { font-weight: 700; font-size: 1.02rem; }
.order-card-price { font-weight: 700; color: var(--accent-dark); }
.order-card-meta { color: var(--ink-secondary); font-size: 0.85rem; margin-top: 4px; }
.order-card-badges { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
}
.badge-due-overdue { background: var(--critical-bg); color: var(--critical); }
.badge-due-soon { background: var(--warning-bg); color: var(--warning); }
.badge-pay-unpaid { background: var(--critical-bg); color: var(--critical); }
.badge-pay-partial { background: var(--warning-bg); color: var(--warning); }
.badge-pay-paid { background: var(--good-bg); color: var(--good); }
.badge-status { background: var(--border); color: var(--ink-secondary); }

.empty-state { text-align: center; color: var(--muted); padding: 40px 20px; }

/* Dashboard */
.tabrow { display: flex; gap: 6px; margin-bottom: 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); padding: 4px; }
.tabrow button {
  flex: 1;
  min-height: 42px;
  border: none;
  background: none;
  border-radius: var(--radius-s);
  font-family: inherit;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--ink-secondary);
}
.tabrow button.active { background: var(--accent); color: var(--accent-ink); }

.year-picker { display: flex; align-items: center; justify-content: center; gap: 16px; margin-bottom: 18px; }
.year-picker button {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface-raised);
  font-size: 1.1rem;
  color: var(--ink);
}
.year-picker .year-label { font-size: 1.1rem; font-weight: 700; min-width: 64px; text-align: center; }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 20px; }
.stat-tile {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  padding: 14px;
}
.stat-tile .stat-label { font-size: 0.78rem; color: var(--muted); font-weight: 600; }
.stat-tile .stat-value { font-size: 1.35rem; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat-tile.stat-good .stat-value { color: var(--good); }
.stat-tile.stat-critical .stat-value { color: var(--critical); }

.chart-block { background: var(--surface-raised); border: 1px solid var(--border); border-radius: var(--radius-m); padding: 16px; margin-bottom: 16px; }
.chart-block h3 { font-size: 0.92rem; color: var(--ink-secondary); }

.vbar-chart { display: flex; align-items: flex-end; gap: 4px; height: 140px; position: relative; }
.vbar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; cursor: pointer; }
.vbar-fill { width: 100%; max-width: 20px; background: var(--series-1); border-radius: 4px 4px 0 0; min-height: 2px; }
.vbar-label { font-size: 0.62rem; color: var(--muted); margin-top: 4px; }

.hbar-row { margin-bottom: 12px; }
.hbar-row:last-child { margin-bottom: 0; }
.hbar-labels { display: flex; justify-content: space-between; font-size: 0.85rem; margin-bottom: 4px; }
.hbar-labels .hbar-name { color: var(--ink); font-weight: 600; }
.hbar-labels .hbar-value { color: var(--ink-secondary); font-variant-numeric: tabular-nums; }
.hbar-track { background: var(--bg); border-radius: 4px; height: 10px; overflow: hidden; }
.hbar-fill { height: 100%; border-radius: 4px; min-width: 4px; }

.stack-bar { display: flex; height: 14px; border-radius: 4px; overflow: hidden; gap: 2px; margin: 10px 0; }
.stack-seg { height: 100%; }
.stack-legend { display: flex; gap: 16px; font-size: 0.82rem; }
.stack-legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

.tooltip-bubble {
  position: absolute;
  background: var(--ink);
  color: var(--bg);
  padding: 5px 9px;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  transform: translate(-50%, -110%);
  pointer-events: none;
  z-index: 5;
}

/* Modal (delete confirm) */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(20, 16, 8, 0.45);
  display: flex; align-items: flex-end;
  z-index: 50;
}
.modal-sheet {
  background: var(--surface-raised);
  width: 100%;
  border-radius: var(--radius-l) var(--radius-l) 0 0;
  padding: 22px 18px max(22px, env(safe-area-inset-bottom));
}
.modal-sheet p { color: var(--ink-secondary); margin: 0 0 18px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(76px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 60;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.section-title { font-size: 0.95rem; font-weight: 700; margin: 22px 0 10px; color: var(--ink-secondary); }
.section-title:first-child { margin-top: 0; }
