:root {
  color-scheme: light;
  --bg: #f4f3f0;
  --surface: #fcfcfb;
  --surface-2: #eceae5;
  --border: #dcdad4;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #74726d;
  --brand: #1d5b86;
  --brand-ink: #ffffff;
  --danger: #b42323;

  /* Status (fixed): good / warning / critical, paired with a word on every badge. */
  --dry: #0ca30c;
  --damp: #fab219;
  --wet: #d03b3b;
  --dry-bg: #e3f4e3;
  --damp-bg: #fdf0d3;
  --wet-bg: #f8e1e1;
  --dry-ink: #0a5c0a;
  --damp-ink: #6b4a00;
  --wet-ink: #8e1f1f;

  /* Categorical series for highlighted chart lines. */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --muted-line: #b5b3ad;
  --grid: #e4e2dd;

  --radius: 14px;
  --tap: 56px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #262624;
    --border: #383835;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9a9890;
    --brand: #3987e5;
    --danger: #e66767;
    --dry-bg: #15301a;
    --damp-bg: #3a2f10;
    --wet-bg: #3d1a1a;
    --dry-ink: #8fe08f;
    --damp-ink: #ffd27a;
    --wet-ink: #ffaaaa;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --muted-line: #5a5955;
    --grid: #2c2c2a;
  }
}
:root[data-theme='dark'] {
  color-scheme: dark;
  --bg: #111110;
  --surface: #1a1a19;
  --surface-2: #262624;
  --border: #383835;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #9a9890;
  --brand: #3987e5;
  --danger: #e66767;
  --dry-bg: #15301a;
  --damp-bg: #3a2f10;
  --wet-bg: #3d1a1a;
  --dry-ink: #8fe08f;
  --damp-ink: #ffd27a;
  --wet-ink: #ffaaaa;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --muted-line: #5a5955;
  --grid: #2c2c2a;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 17px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
a { color: var(--brand); }
h2 { font-size: 15px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-2); margin: 28px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
button, input, textarea, select { font: inherit; color: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--brand); color: var(--brand-ink);
}
.topbar h1 { flex: 1; margin: 0; font-size: 19px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { width: 44px; height: 44px; border: 0; background: transparent; color: inherit; font-size: 34px; line-height: 1; padding: 0 0 4px; border-radius: 10px; }
.back:active { background: rgb(255 255 255 / 0.15); }
.sync-pill {
  border: 0; border-radius: 999px; padding: 8px 12px; min-height: 36px;
  font-size: 14px; font-weight: 600; white-space: nowrap;
  background: rgb(255 255 255 / 0.18); color: inherit;
}
.sync-pill.warn { background: #fab219; color: #241a00; }
.sync-pill.off { background: rgb(0 0 0 / 0.25); }

main { max-width: 680px; margin: 0 auto; padding: 16px 16px calc(110px + env(safe-area-inset-bottom)); }

/* ---------- Buttons ---------- */
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px;
  border: 1.5px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font-weight: 600; text-decoration: none; text-align: center; cursor: pointer;
  user-select: none;
}
.btn:active:not(:disabled) { transform: scale(0.98); background: var(--surface-2); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:active:not(:disabled) { background: var(--brand); filter: brightness(0.92); }
.btn.big { min-height: 64px; font-size: 19px; width: 100%; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; width: 100%; margin-top: 12px; }
.btn.small { min-height: 40px; font-size: 14px; padding: 0 12px; margin: 0; }
.danger-text { color: var(--danger); width: 100%; margin-top: 12px; border: 0; }
.icon-btn { width: 44px; height: 44px; border: 0; border-radius: 10px; background: var(--surface-2); font-size: 16px; }
.icon-btn:disabled { opacity: 0.3; }
.stack { display: grid; gap: 10px; margin-top: 16px; }
.link-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.link-row .btn { flex: 1 1 auto; min-height: 48px; font-size: 15px; }

.bottom-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.bottom-bar .btn { max-width: 648px; margin: 0 auto; }

/* ---------- Cards, lists, rows ---------- */
.card {
  display: block; padding: 18px; margin-bottom: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  color: inherit; text-decoration: none;
}
.card-title { font-size: 21px; font-weight: 700; margin-bottom: 2px; }
.levels { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.pill { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 14px; font-weight: 600; }
.pill.wet { background: var(--wet-bg); color: var(--wet-ink); }
.pill.damp { background: var(--damp-bg); color: var(--damp-ink); }
.pill.dry { background: var(--dry-bg); color: var(--dry-ink); }
.pill.info { background: var(--surface-2); color: var(--text-2); }

.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  min-height: 64px; padding: 10px 14px;
  border: 0; border-bottom: 1px solid var(--border); background: transparent;
  color: inherit; text-decoration: none; text-align: left; cursor: pointer;
}
.row:last-child { border-bottom: 0; }
.row:active { background: var(--surface-2); }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; color: inherit; text-decoration: none; }
.row-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 14px; color: var(--muted); display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 2px; }
.row-end { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.chev { color: var(--muted); font-size: 24px; }
.num {
  flex: none; display: inline-grid; place-items: center;
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--surface-2); font-weight: 700;
}
.num.big { width: 52px; height: 52px; font-size: 24px; background: var(--brand); color: var(--brand-ink); }
.reorder { display: flex; gap: 6px; }
.empty-state { padding: 28px 16px; text-align: center; color: var(--muted); }
.notes { white-space: pre-wrap; color: var(--text-2); margin-top: 0; }
.tag { display: inline-block; font-size: 13px; padding: 1px 8px; border-radius: 6px; background: var(--surface-2); color: var(--text-2); }

.side { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; padding: 1px 6px; border-radius: 5px; border: 1px solid var(--border); color: var(--text-2); }
.side.outside { border-style: dashed; }

/* ---------- Value badges ---------- */
.badge { display: inline-flex; align-items: baseline; gap: 5px; padding: 3px 9px; border-radius: 8px; white-space: nowrap; }
.badge b { font-size: 16px; font-variant-numeric: tabular-nums; }
.badge i { font-style: normal; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.badge::before { content: ''; width: 9px; height: 9px; border-radius: 50%; align-self: center; }
.badge.dry { background: var(--dry-bg); color: var(--dry-ink); }
.badge.dry::before { background: var(--dry); }
.badge.damp { background: var(--damp-bg); color: var(--damp-ink); }
.badge.damp::before { background: var(--damp); }
.badge.wet { background: var(--wet-bg); color: var(--wet-ink); }
.badge.wet::before { background: var(--wet); }
.badge.none { color: var(--muted); }
.badge.none::before { display: none; }
.delta { font-size: 13px; font-weight: 600; color: var(--text-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.delta.up { color: var(--wet-ink); }
.delta.down { color: var(--dry-ink); }

/* ---------- Photos ---------- */
.photo-strip { display: flex; gap: 10px; overflow-x: auto; margin: 0 -16px; padding: 0 16px 6px; scroll-snap-type: x mandatory; }
.thumb { position: relative; flex: none; padding: 0; border: 0; border-radius: 12px; overflow: hidden; background: var(--surface-2); scroll-snap-align: start; cursor: zoom-in; }
.photo-strip .thumb img { display: block; height: 150px; width: auto; max-width: 260px; object-fit: cover; }
.thumb span { position: absolute; left: 0; right: 0; bottom: 0; padding: 4px 8px; font-size: 12px; color: #fff; background: rgb(0 0 0 / 0.55); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: left; }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.photo-card { display: grid; gap: 6px; }
.photo-card .thumb img { display: block; width: 100%; aspect-ratio: 3 / 4; object-fit: cover; }
.photo-card input { min-height: 44px; padding: 0 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.point-photo { position: relative; display: block; width: 100%; padding: 0; margin-top: 14px; border: 0; border-radius: var(--radius); overflow: hidden; background: #000; }
.point-photo img { display: block; width: 100%; max-height: 360px; object-fit: contain; }
.zoom-hint { position: absolute; right: 8px; bottom: 8px; padding: 3px 8px; border-radius: 6px; font-size: 12px; color: #fff; background: rgb(0 0 0 / 0.55); }
.point-head { display: flex; align-items: center; gap: 12px; }

#viewer { position: fixed; inset: 0; z-index: 50; background: #000; touch-action: none; }
.viewer-stage { position: absolute; inset: 0; display: grid; place-items: center; overflow: hidden; }
.viewer-stage img { max-width: 100%; max-height: 100%; transform-origin: center; user-select: none; -webkit-user-drag: none; }
.viewer-cap { position: absolute; left: 0; right: 0; bottom: 0; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); color: #fff; background: rgb(0 0 0 / 0.6); }
.viewer-close { position: absolute; top: calc(env(safe-area-inset-top) + 10px); right: 12px; width: 52px; height: 52px; border: 0; border-radius: 50%; font-size: 22px; color: #fff; background: rgb(255 255 255 / 0.2); }

/* ---------- Round ---------- */
.round { display: flex; flex-direction: column; gap: 10px; }
/* Entry screen fills the viewport exactly; the photo takes whatever height is spare. */
main:has(.keypad) { padding-bottom: 12px; }
.round:has(.keypad) { height: calc(100dvh - 60px - env(safe-area-inset-top) - 28px - env(safe-area-inset-bottom)); }
.round h2 { margin-top: 4px; }
.round-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 16px; }
.progress { display: flex; gap: 4px; }
.progress span { flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); }
.progress span.done { background: var(--brand); opacity: 0.55; }
.progress span.cur { background: var(--brand); opacity: 1; }
.round-photo {
  position: relative; flex: 1 1 0; min-height: 110px;
  display: grid; place-items: center; padding: 0; border: 0; border-radius: var(--radius); overflow: hidden; background: #000;
}
.round-photo img { width: 100%; height: 100%; object-fit: contain; }
.round-photo:disabled { background: var(--surface-2); }
.round-point { display: flex; align-items: center; gap: 12px; }
.round-point-text { min-width: 0; }
.entry { display: flex; align-items: baseline; justify-content: center; gap: 6px; min-height: 64px; }
.entry-val { font-size: 54px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.entry-val.placeholder { color: var(--border); }
.entry-unit { font-size: 26px; color: var(--muted); margin-right: 8px; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.keypad button {
  min-height: 60px; border: 0; border-radius: 14px;
  background: var(--surface); box-shadow: inset 0 0 0 1px var(--border);
  font-size: 28px; font-weight: 600; touch-action: manipulation; user-select: none;
}
.keypad button:active { background: var(--surface-2); transform: scale(0.97); }
.round-actions { display: grid; grid-template-columns: 1fr 1fr 1.6fr; gap: 8px; }
.round-actions .btn { min-height: 64px; font-size: 18px; padding: 0 8px; }
.sync-note { padding: 10px 14px; border-radius: 10px; font-size: 15px; margin: 4px 0 12px; }
.sync-note.busy { background: var(--damp-bg); color: var(--damp-ink); }
.sync-note.ok { background: var(--dry-bg); color: var(--dry-ink); }

@media (max-height: 700px) {
  .keypad button { min-height: 52px; font-size: 24px; }
  .entry-val { font-size: 44px; }
  .round-actions .btn { min-height: 56px; }
}

/* ---------- Chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 44px; padding: 0 14px; border-radius: 999px;
  border: 1.5px solid var(--border); background: var(--surface); font-weight: 600;
}
.chip.big { min-height: 56px; padding: 0 20px; font-size: 17px; }
.chip.on { border-color: var(--text-2); background: var(--surface-2); }
.chip .swatch { width: 12px; height: 12px; border-radius: 50%; }
.chip-x { border: 0; background: transparent; padding: 6px 2px 6px 6px; color: var(--muted); }

/* ---------- Forms ---------- */
.form { display: grid; gap: 16px; }
.form label, .form legend { display: grid; gap: 6px; font-weight: 600; }
.form input:not([type='radio']), .form textarea, .inline-form input {
  width: 100%; min-height: var(--tap); padding: 10px 14px;
  border: 1.5px solid var(--border); border-radius: 12px; background: var(--surface); font-size: 18px;
}
.form textarea { min-height: 90px; resize: vertical; }
.form fieldset { border: 0; padding: 0; margin: 0; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.inline-form { display: flex; gap: 8px; margin: 12px 0; }
.inline-form .btn { flex: none; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg legend { grid-column: 1 / -1; margin-bottom: 6px; }
.seg label { display: block; }
.seg input { position: absolute; opacity: 0; }
.seg span { display: grid; place-items: center; min-height: var(--tap); border: 1.5px solid var(--border); border-radius: 12px; background: var(--surface); }
.seg input:checked + span { border-color: var(--brand); background: var(--brand); color: var(--brand-ink); }
.seg input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 2px; }
.photo-pick { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; margin-top: 6px; }
.pick { position: relative; display: block !important; font-weight: 400 !important; font-size: 12px; }
.pick input { position: absolute; opacity: 0; }
.pick img, .pick-none { display: grid; place-items: center; width: 100%; aspect-ratio: 3 / 4; object-fit: cover; border-radius: 10px; border: 3px solid transparent; background: var(--surface-2); }
.pick input:checked ~ img, .pick input:checked ~ .pick-none { border-color: var(--brand); }
.pick input:focus-visible ~ img { outline: 2px solid var(--brand); }
.pick span:not(.pick-none) { display: block; margin-top: 2px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.legend { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; }

/* ---------- Tables ---------- */
.history { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.history th, .history td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); font-size: 15px; vertical-align: middle; }
.history th { font-size: 13px; color: var(--muted); font-weight: 600; }
.history td:last-child { text-align: right; width: 52px; }
.table-scroll { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.matrix { border-collapse: collapse; font-size: 14px; }
.matrix th, .matrix td { padding: 6px 6px; border-bottom: 1px solid var(--border); text-align: center; white-space: nowrap; }
.matrix tbody th { text-align: left; position: sticky; left: 0; background: var(--surface); font-weight: 600; }
.matrix thead th { color: var(--muted); font-size: 13px; }
.cell { display: inline-block; min-width: 40px; padding: 3px 4px; border-radius: 6px; font-variant-numeric: tabular-nums; font-weight: 600; }
.cell.dry { background: var(--dry-bg); color: var(--dry-ink); }
.cell.damp { background: var(--damp-bg); color: var(--damp-ink); }
.cell.wet { background: var(--wet-bg); color: var(--wet-ink); }

/* ---------- Chart ---------- */
.chart { position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 0 4px; min-height: 120px; }
.chart-svg { display: block; touch-action: pan-y; }
.chart .band { opacity: 0.1; }
.chart .band.dry { fill: var(--dry); }
.chart .band.damp { fill: var(--damp); opacity: 0.14; }
.chart .band.wet { fill: var(--wet); }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .marker { stroke: var(--text-2); stroke-width: 1; stroke-dasharray: 3 3; opacity: 0.6; }
.chart .marker-label { fill: var(--text-2); font-size: 10px; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.muted { stroke: var(--muted-line); stroke-width: 1.5; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .dot.muted { fill: var(--muted-line); }
.chart .end-label { fill: var(--muted); font-size: 11px; }
.chart .end-label.hl { fill: var(--text); font-weight: 700; }
.chart .crosshair { stroke: var(--text-2); stroke-width: 1; }
.chart .focus { stroke: var(--surface); stroke-width: 2; }
.chart .hit { fill: transparent; }
.chart .empty { padding: 28px; margin: 0; text-align: center; color: var(--muted); }
.tip {
  position: absolute; z-index: 2; pointer-events: none; min-width: 140px;
  padding: 8px 10px; border-radius: 10px; font-size: 13px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: 0 4px 16px rgb(0 0 0 / 0.15);
}
.tip-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-weight: 700; margin-bottom: 4px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 12px;
  background: #222; color: #fff; font-weight: 600; transition: 0.2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); }
