/* ---------- tokens ---------- */
:root {
  --bg: #0b1120;
  --bg-raised: #131c31;
  --bg-sunken: #070c17;
  --line: #22304d;
  --line-soft: #1a2439;
  --text: #eef2f9;
  --text-dim: #94a3b8;
  --text-faint: #64748b;
  --accent: #fbbf24;
  --accent-ink: #231a02;
  --done: #34d399;
  --done-bg: #10291f;
  --skip: #fb923c;
  --skip-bg: #2a1a0d;
  --danger: #f87171;
  --radius: 14px;
  --radius-sm: 10px;
  --tap: 56px;
  --gutter: 16px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --shadow: 0 6px 24px rgba(0, 0, 0, .45);
}

:root[data-theme="light"] {
  --bg: #f6f7fb;
  --bg-raised: #ffffff;
  --bg-sunken: #e9edf5;
  --line: #d3dae7;
  --line-soft: #e4e9f2;
  --text: #0f172a;
  --text-dim: #566179;
  --text-faint: #8a93a6;
  /* Kept distinct from --skip below, or the Finish button and an active
     skip button read as the same control. */
  --accent: #a16207;
  --accent-ink: #fffaf0;
  --done: #047857;
  --done-bg: #e7f6ef;
  --skip: #c2410c;
  --skip-bg: #fdf0e6;
  --danger: #b91c1c;
  --shadow: 0 6px 20px rgba(15, 23, 42, .12);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* A class that sets display would otherwise beat the UA rule for [hidden]. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior-y: contain;
}

body { min-height: 100dvh; }

h1, h2, h3 { margin: 0; line-height: 1.2; font-weight: 650; letter-spacing: -.01em; }
p { margin: 0 0 .75em; }
a { color: var(--accent); }

/* ---------- layout ---------- */
.screen { padding-bottom: calc(var(--safe-b) + 24px); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--bg);
  border-bottom: 1px solid var(--line-soft);
  padding: calc(var(--safe-t) + 10px) var(--gutter) 10px;
}

.topbar-row { display: flex; align-items: center; gap: 10px; min-height: 36px; }

.topbar-row h1 {
  font-size: 20px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-sub { color: var(--text-dim); font-size: 13px; margin-top: 2px; }

.body { padding: var(--gutter); max-width: 680px; margin: 0 auto; }
.body-flush { padding: 0; max-width: 680px; margin: 0 auto; }

.stack { display: grid; gap: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.row-wrap { display: flex; flex-wrap: wrap; gap: 8px; }
.spacer { flex: 1; }

/* ---------- buttons ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--bg-raised);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 0 16px;
  min-height: 48px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  text-decoration: none;
}

.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-dim); padding: 0 10px; }
.btn-ghost:hover { color: var(--text); }
.btn-danger { color: var(--danger); border-color: var(--line); background: transparent; }
.btn-block { width: 100%; }
.btn-sm { min-height: 38px; padding: 0 12px; font-size: 14px; border-radius: 8px; }
.btn-icon { min-height: 44px; min-width: 44px; padding: 0; }
.btn:disabled { opacity: .45; pointer-events: none; }

/* ---------- cards ---------- */
.card {
  background: var(--bg-raised);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 14px;
}

.card-link { display: block; text-decoration: none; color: inherit; }
.card-title { font-size: 17px; font-weight: 650; }
.card-meta { color: var(--text-dim); font-size: 13px; margin-top: 3px; }

.empty { text-align: center; padding: 40px 20px; color: var(--text-dim); }
.empty-mark { font-size: 44px; margin-bottom: 8px; }

/* ---------- forms ---------- */
label.field { display: block; }

.field-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
  display: block;
}

input[type="text"], input[type="number"], textarea, select {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px;
  min-height: 48px;
}

textarea { min-height: 140px; resize: vertical; line-height: 1.5; }

input:focus-visible, textarea:focus-visible, .btn:focus-visible, .stop:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hint { font-size: 13px; color: var(--text-faint); }

/* ---------- progress / timer ---------- */
.progress {
  height: 8px;
  background: var(--bg-sunken);
  border-radius: 99px;
  overflow: hidden;
  margin-top: 10px;
}

.progress > i {
  display: block;
  height: 100%;
  background: var(--done);
  border-radius: 99px;
  transition: width .22s ease;
}

.timer {
  font-variant-numeric: tabular-nums;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -.02em;
}

.timer.paused { color: var(--text-faint); }
.counts { font-variant-numeric: tabular-nums; color: var(--text-dim); font-size: 14px; font-weight: 600; }

/* ---------- stop list ---------- */
.street-head {
  position: sticky;
  top: var(--stickyTop, 0px);
  z-index: 10;
  background: var(--bg-sunken);
  border-top: 1px solid var(--line-soft);
  border-bottom: 1px solid var(--line-soft);
  padding: 7px var(--gutter);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-dim);
  display: flex;
  gap: 8px;
  align-items: center;
}

.street-head .street-count {
  color: var(--text-faint);
  letter-spacing: 0;
  text-transform: none;
  font-weight: 600;
}

.stop {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  min-height: var(--tap);
  padding: 10px var(--gutter);
  background: transparent;
  color: var(--text);
  border: 0;
  border-bottom: 1px solid var(--line-soft);
  font: inherit;
  cursor: pointer;
}

.stop-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.stop-label { font-size: 18px; font-weight: 600; }
.stop-note { font-size: 13px; color: var(--text-dim); margin-top: 1px; }

.stop-tick {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid var(--line);
  display: grid;
  place-items: center;
  font-size: 16px;
  font-weight: 700;
  color: transparent;
}

.stop-skipbtn {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid var(--line-soft);
  background: transparent;
  color: var(--text-faint);
  font-size: 17px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.stop[data-state="done"] { background: var(--done-bg); }

.stop[data-state="done"] .stop-label {
  text-decoration: line-through;
  text-decoration-thickness: 2px;
  color: var(--text-dim);
}

.stop[data-state="done"] .stop-tick { background: var(--done); border-color: var(--done); color: #04231a; }
.stop[data-state="skip"] { background: var(--skip-bg); }
.stop[data-state="skip"] .stop-label { color: var(--text-dim); }
/* A skipped house is not delivered, so the tick must not appear at all. */
.stop[data-state="skip"] .stop-tick { border-color: var(--skip); color: transparent; }
.stop[data-state="skip"] .stop-skipbtn { background: var(--skip); border-color: var(--skip); color: #2a1a0d; }

/* ---------- bottom action bar ---------- */
.actionbar {
  position: sticky;
  bottom: 0;
  z-index: 30;
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 10px var(--gutter) calc(var(--safe-b) + 10px);
  background: var(--bg);
  border-top: 1px solid var(--line-soft);
}

.actionbar .btn { flex: 1; }
.actionbar .btn-icon { flex: none; }

/* ---------- editor rows ---------- */
.edit-row {
  display: grid;
  grid-template-columns: 34px 1fr 40px;
  gap: 8px;
  align-items: start;
  padding: 8px 0;
  border-bottom: 1px solid var(--line-soft);
  background: var(--bg);
}

.edit-row.dragging { opacity: .9; box-shadow: var(--shadow); border-radius: var(--radius-sm); }
.edit-row.drop-target { border-top: 2px solid var(--accent); }

.grip {
  min-height: 48px;
  width: 34px;
  display: grid;
  place-items: center;
  color: var(--text-faint);
  cursor: grab;
  touch-action: none;
  font-size: 18px;
  border: 0;
  background: transparent;
  border-radius: 8px;
}

.edit-fields { display: grid; gap: 6px; min-width: 0; }
.edit-fields input { min-height: 44px; }
.edit-fields input.note { min-height: 38px; font-size: 14px; color: var(--text-dim); }

.edit-del {
  min-height: 48px;
  width: 40px;
  border: 0;
  background: transparent;
  color: var(--text-faint);
  font-size: 20px;
  cursor: pointer;
  border-radius: 8px;
}

.edit-del:hover { color: var(--danger); }

/* ---------- segmented control ---------- */
.seg {
  display: flex;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
}

.seg button {
  flex: 1;
  min-height: 40px;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  border-radius: 8px;
  cursor: pointer;
}

.seg button[aria-selected="true"] {
  background: var(--bg-raised);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
}

/* ---------- photo ---------- */
.thumb-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line-soft);
  background: var(--bg-sunken);
}

/* Anchor to the top: the start of the round sheet is the useful part. */
.thumb-wrap img { display: block; width: 100%; max-height: 230px; object-fit: cover; object-position: center top; }

.thumb-tag {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background: rgba(0, 0, 0, .72);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 99px;
}

.viewer { position: fixed; inset: 0; z-index: 100; background: #000; display: flex; flex-direction: column; }

.viewer-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(var(--safe-t) + 8px) 12px 8px;
  background: #0a0a0a;
  border-bottom: 1px solid #222;
  color: #bbb;
}

.viewer-bar .btn-ghost { color: #ddd; }
.viewer-hint { font-size: 12px; color: #777; }
.viewer-stage { flex: 1; overflow: auto; display: grid; place-items: center; }
.viewer-stage img { max-width: none; width: 100%; height: auto; }

/* ---------- OCR progress ---------- */
.ocr-box { display: grid; gap: 10px; padding: 18px; text-align: center; }
.bar { height: 10px; background: var(--bg-sunken); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .3s ease; }

/* ---------- misc ---------- */
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 92px);
  z-index: 200;
  background: var(--text);
  color: var(--bg);
  padding: 11px 18px;
  border-radius: 99px;
  font-weight: 600;
  font-size: 14px;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 32px);
  text-align: center;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-sunken);
  border: 1px solid var(--line-soft);
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 99px;
}

.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.stat {
  background: var(--bg-raised);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  padding: 12px;
  text-align: center;
}

.stat-v { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }

.stat-k {
  font-size: 11px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: .05em;
  margin-top: 2px;
}

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
hr.sep { border: 0; border-top: 1px solid var(--line-soft); margin: 18px 0; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
