:root {
  --bg: #eef1f5;
  --card: #ffffff;
  --text: #1b1f24;
  --muted: #5b6573;
  --line: #cfd6df;
  --accent: #1f4e79;
  --accent-text: #ffffff;
  --accent-soft: #dde8f3;
  --err: #b42318;
  --err-bg: #fdecea;
  --warn: #8a5300;
  --warn-bg: #fff4dc;
  --ok-bg: #e8f3ec;
  --tap: 48px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161b;
    --card: #1c2229;
    --text: #e8ecf1;
    --muted: #9aa5b3;
    --line: #34404d;
    --accent: #5b9bd5;
    --accent-text: #0b1520;
    --accent-soft: #23384d;
    --err: #ff8a80;
    --err-bg: #3a1f1f;
    --warn: #ffcc80;
    --warn-bg: #3a2e18;
    --ok-bg: #1d3226;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 16px; padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--accent); color: var(--accent-text);
}
.top h1 { font-size: 18px; margin: 0; }
.top .ghost { color: var(--accent-text); border-color: color-mix(in srgb, var(--accent-text) 50%, transparent); min-height: 40px; }
#app { max-width: 640px; margin: 0 auto; padding: 12px 12px calc(130px + env(safe-area-inset-bottom)); }
.card {
  background: var(--card); border-radius: 12px; padding: 14px;
  margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,.08);
}
h2 { font-size: 17px; margin: 0 0 10px; display: flex; align-items: baseline; gap: 8px; }
h2.mt { margin-top: 16px; }
h3 { font-size: 15px; margin: 14px 0 6px; }
.h-sum { font-size: 14px; font-weight: 500; color: var(--muted); margin-left: auto; text-align: right; }
label, .field-label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 10px; }
.field { margin-bottom: 12px; }
.field-label { margin-bottom: 6px; }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  display: block; width: 100%; min-width: 0; margin-top: 4px;
  min-height: var(--tap); padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--text); font-size: 17px;
}
input[type="date"] { -webkit-appearance: none; appearance: none; padding-right: 6px; }
input::placeholder, textarea::placeholder { color: color-mix(in srgb, var(--muted) 70%, transparent); }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
textarea { resize: vertical; }
input.invalid { border-color: var(--err); background: var(--err-bg); }
.row2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 8px; }
.row3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0 8px; }
/* Keep inputs on one line even when a label wraps to two lines. */
.row2 > label, .row3 > label { display: flex; flex-direction: column; justify-content: flex-end; }
.row2 > label > .bal { min-height: 1.3em; }
button {
  min-height: var(--tap); padding: 8px 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); cursor: pointer; touch-action: manipulation;
}
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--err); }
button.wide { width: 100%; }
button.icon { min-width: var(--tap); padding: 0 10px; font-size: 20px; line-height: 1; }
button:disabled { opacity: .5; }
.hint { font-size: 14px; color: var(--muted); margin: -4px 0 10px; }
.hint.center { text-align: center; margin: 8px 0 0; }
.hint.warn { color: var(--warn); background: var(--warn-bg); padding: 8px 10px; border-radius: 8px; margin-top: 0; }
.hint button { min-height: 36px; padding: 2px 10px; margin-left: 6px; font-size: 14px; }

/* Chips: saved drivers, cars, routes, period presets, notes */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 10px; }
.chips:empty { display: none; }
.chip {
  min-height: 44px; padding: 6px 14px; border-radius: 22px;
  border: 1px solid var(--line); background: var(--card); font-size: 15px; text-align: left; line-height: 1.2;
}
.chip small { display: block; font-size: 12px; color: var(--muted); }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.chip[aria-pressed="true"] small { color: inherit; opacity: .85; }
.chip.add { border-style: dashed; color: var(--accent); }
.chip.sm { min-height: 40px; font-size: 14px; padding: 4px 12px; }
.add-inline { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 4px; }
.add-inline input { margin-top: 0; flex: 1 1 140px; }
.add-inline button { flex: 0 0 auto; }

/* Expenses */
.cat-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; }
.cat-btn {
  min-height: 52px; padding: 6px 10px; text-align: left; font-size: 15px; line-height: 1.2;
  background: var(--accent-soft); border-color: transparent; display: flex; flex-direction: column; justify-content: center;
}
.cat-btn b { font-weight: 600; }
.cat-btn small { font-size: 12px; color: var(--muted); }
.cat-btn:last-child:nth-child(odd) { grid-column: 1 / -1; }
.exp { margin-top: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 12px; }
.exp-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.exp-head b { flex: 1; font-size: 15px; }
.exp-head button { min-height: 40px; min-width: 44px; padding: 0; color: var(--err); border-color: transparent; font-size: 18px; }
.exp input { margin-top: 0; }
.exp .amt { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; margin-bottom: 8px; }
.exp .amt input { font-size: 20px; font-weight: 600; }
.exp .meta { display: grid; grid-template-columns: minmax(0, 9.5em) minmax(0, 1fr); gap: 8px; margin-bottom: 8px; }
.exp .chips { margin: 6px 0 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; min-height: var(--tap); padding: 0 8px; font-size: 14px; min-width: 42px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); font-weight: 600; }

/* Calculated values */
.bal { display: block; font-size: 13px; margin-top: 3px; }
.calc { padding: 10px 12px; border-radius: 10px; background: var(--ok-bg); font-size: 16px; margin-bottom: 8px; }
.calc b { font-variant-numeric: tabular-nums; }
.calc.err { background: var(--err-bg); color: var(--err); }
.calc:empty { display: none; }
.cost-line { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; align-items: end; margin-bottom: 8px; }
.cost-line label { margin: 0; }
.auto { min-height: var(--tap); display: flex; align-items: center; padding: 0 12px; border-radius: 10px; background: var(--bg); font-variant-numeric: tabular-nums; margin-top: 4px; color: var(--text); font-size: 17px; }

/* Errors */
.errors { padding: 10px 12px; border-radius: 10px; background: var(--err-bg); color: var(--err); font-size: 15px; margin-bottom: 10px; }
.errors ul { margin: 0; padding-left: 18px; }

/* Bottom bar */
.bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--card); border-top: 1px solid var(--line);
  box-shadow: 0 -2px 10px rgba(0,0,0,.08);
  padding: 6px 12px calc(8px + env(safe-area-inset-bottom));
}
.bar > * { max-width: 616px; margin-left: auto; margin-right: auto; }
.bar-status {
  display: block; width: 100%; min-height: 36px; padding: 2px 4px; border: 0; background: transparent;
  text-align: left; font-size: 15px; font-variant-numeric: tabular-nums; line-height: 1.3;
}
.bar-status .e { color: var(--err); font-weight: 600; }
.bar-status .w { color: var(--warn); }
.bar-btns { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; }
.bar-btns .primary { min-height: 52px; font-size: 17px; }
.bar-btns button:not(.primary) { min-height: 52px; }

/* Dialog */
dialog {
  width: min(560px, calc(100vw - 16px)); max-height: calc(100vh - 32px);
  border: 0; border-radius: 12px; padding: 14px; background: var(--card); color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.dlg-head h2 { margin: 0; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.list li span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.list li button { min-height: 40px; min-width: 44px; padding: 0 8px; color: var(--err); }
.list .empty { color: var(--muted); font-size: 14px; border: 0; }

.toast {
  position: fixed; left: 50%; bottom: calc(130px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  width: max-content; max-width: calc(100vw - 32px); padding: 10px 14px; border-radius: 10px;
  background: #222; color: #fff; font-size: 15px; z-index: 20; box-shadow: 0 2px 8px rgba(0,0,0,.3);
}

/* Install card */
.install { display: grid; grid-template-columns: 48px minmax(0, 1fr); gap: 10px 12px; align-items: center; border: 2px solid var(--accent); }
.install img { border-radius: 12px; }
.install-text { font-size: 15px; line-height: 1.35; }
.install-text > b { display: block; font-size: 16px; margin-bottom: 2px; }
.ios-share { width: 20px; height: 20px; vertical-align: -4px; color: var(--accent); }
.install-btns { grid-column: 1 / -1; display: flex; gap: 8px; }
.install-btns button { flex: 1; }
