:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #eceee9;
  --text: #1b1e1c;
  --muted: #5f665f;
  --border: #d5d9d2;
  --accent: #2f7d4f;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --single: #3ddc84;
  --cluster: #ffb020;
  --manual: #38bdf8;
  --viewer-bg: #0e1011;
  --radius: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121413;
    --surface: #1b1e1c;
    --surface-2: #252925;
    --text: #e8ebe7;
    --muted: #9aa39a;
    --border: #333833;
    --accent: #4caf73;
    --accent-text: #0b1a10;
    --danger: #f2867d;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

/* Quick repeated taps (shutter, + Add, Undo) must not zoom the page. */
html, button, label, input, summary, a, .btn { touch-action: manipulation; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

[hidden] { display: none !important; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar h1 { font-size: 18px; margin: 0; letter-spacing: 0.01em; }

main { max-width: 1200px; margin: 0 auto; padding: 16px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn.big { min-height: 56px; font-size: 18px; width: 100%; }
.btn.danger { color: var(--danger); }
.btn:focus-visible, .seg-btn:focus-visible, input:focus-visible, summary:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent);
  outline-offset: 2px;
}

/* Start screen */
.start { max-width: 520px; margin: 8vh auto 24px; }
.drop {
  display: grid;
  gap: 12px;
  padding: 24px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: center;
}
.drop.dragging { border-color: var(--accent); background: var(--surface-2); }
.drop-hint { margin: 0; color: var(--muted); font-size: 14px; }
.tips { margin: 20px 0 0; padding-left: 20px; color: var(--muted); }
.tips li { margin: 4px 0; }

/* Result layout */
.result { display: grid; gap: 16px; }
@media (min-width: 900px) {
  .result { grid-template-columns: minmax(0, 1fr) 340px; align-items: start; }
}

.viewer-wrap { display: grid; gap: 8px; min-width: 0; }

.viewer {
  position: relative;
  height: min(62vh, 640px);
  min-height: 320px;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--viewer-bg);
  touch-action: none;
  scroll-margin-top: 72px;
}
.viewer canvas { display: block; width: 100%; height: 100%; }
.viewer.mode-add, .viewer.mode-calibrate { cursor: crosshair; }
.viewer.mode-remove { cursor: not-allowed; }
.viewer.mode-view { cursor: grab; }

.busy {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(10, 12, 11, 0.6);
  color: #fff;
  font-weight: 600;
}
.spinner {
  width: 36px;
  height: 36px;
  border: 4px solid rgba(255, 255, 255, 0.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

.mode-hint {
  position: absolute;
  left: 50%;
  top: 10px;
  transform: translateX(-50%);
  max-width: calc(100% - 20px);
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(10, 12, 11, 0.75);
  color: #fff;
  font-size: 14px;
  pointer-events: none;
  text-align: center;
}

.count-badge {
  position: absolute;
  left: 10px;
  bottom: 10px;
  padding: 4px 10px;
  border-radius: 8px;
  background: rgba(10, 12, 11, 0.75);
  color: #fff;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.count-badge:empty, .viewer.mode-area .count-badge { display: none; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.seg {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.seg-btn {
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.seg-btn.active { background: var(--accent); color: var(--accent-text); }

/* Side panel */
.panel { display: grid; gap: 16px; }

.count-card {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.count-label { color: var(--muted); font-size: 14px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.count { font-size: 56px; font-weight: 750; line-height: 1.05; font-variant-numeric: tabular-nums; }
.count-detail { color: var(--muted); font-size: 14px; }
.note {
  margin-top: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--cluster) 18%, transparent);
  font-size: 14px;
}

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--muted); font-size: 14px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 12px; height: 12px; border-radius: 50%; border: 2px solid rgba(0, 0, 0, 0.6); display: inline-block; }
.dot.single { background: var(--single); }
.dot.cluster { background: var(--cluster); }
.dot.manual { background: var(--manual); }

.save label, .field .label, .field label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.save .row input { flex: 1 1 160px; min-width: 0; }
input[type="text"] {
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
.saved-msg { min-height: 1.4em; margin-top: 6px; color: var(--accent); font-size: 14px; font-weight: 600; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.settings {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 0 16px;
}
.settings summary { cursor: pointer; padding: 12px 0; font-weight: 600; }
.settings[open] { padding-bottom: 12px; }
.field { margin: 8px 0 16px; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.field output { color: var(--muted); font-weight: 600; margin-left: 4px; }
.value { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 4em; }
.help { margin: 6px 0 0; color: var(--muted); font-size: 13px; }
.help.warn { color: var(--danger); }

.area-bar {
  padding: 12px 16px;
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface);
}

/* History */
.history { margin-top: 32px; }
.history-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.history h2 { font-size: 18px; margin: 0; }
.muted { color: var(--muted); }
.history-list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 8px; }
.history-item {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.history-item img { width: 64px; height: 64px; object-fit: cover; border-radius: 6px; background: var(--viewer-bg); }
.history-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-item .meta { color: var(--muted); font-size: 13px; }
.history-item .n { font-size: 24px; font-weight: 750; font-variant-numeric: tabular-nums; text-align: right; }
.history-item .del { min-height: 32px; padding: 0 8px; font-size: 13px; }

/* Photos of the current sample */
.shots { display: flex; gap: 10px; align-items: center; }
.shot-list {
  position: relative;
  display: flex;
  gap: 8px;
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  padding: 8px 4px 4px;
  list-style: none;
  overflow-x: auto;
}
.shot-list li { position: relative; flex: none; }
.shot {
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 3px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.shot img { display: block; width: 56px; height: 56px; object-fit: cover; border-radius: 6px; background: var(--viewer-bg); }
.shot-n { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.shot.active { border-color: var(--accent); }
.shot.outlier .shot-n { color: var(--danger); }
.shot.pending img { opacity: 0.5; }
.shot-del {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 28px;
  height: 28px;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--text);
  color: var(--bg);
  font: 700 16px/1 system-ui, sans-serif;
  cursor: pointer;
}
.shot-add { display: flex; flex-direction: column; align-items: stretch; gap: 4px; flex: none; }
@media (hover: hover) and (pointer: fine) { .add-files { display: none; } }

/* Live camera */
.camera {
  position: fixed;
  inset: 0;
  z-index: 50;
  touch-action: none; /* no pinch or double-tap zoom on the camera screen */
  display: flex;
  flex-direction: column;
  background: #000;
  color: #fff;
}
.camera video { flex: 1 1 auto; min-height: 0; width: 100%; object-fit: contain; background: #000; }
.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.on { animation: flash 0.25s ease-out; }
@keyframes flash { from { opacity: 0.7; } to { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .flash.on { animation-duration: 0.12s; } }
.camera-msg {
  position: absolute;
  left: 16px;
  right: 16px;
  top: calc(16px + env(safe-area-inset-top));
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(20, 20, 20, 0.85);
  text-align: center;
}
.camera-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.85);
}
.camera-status { font-size: 14px; line-height: 1.3; font-variant-numeric: tabular-nums; }
.camera-status strong { display: block; font-size: 20px; }
.shutter {
  touch-action: none;
  width: 76px;
  height: 76px;
  border: 5px solid #fff;
  border-radius: 50%;
  background: #fff;
  box-shadow: inset 0 0 0 4px #000;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.shutter:active { transform: scale(0.94); }
.shutter:disabled { opacity: 0.4; }
.camera-done { justify-self: end; background: transparent; color: #fff; border-color: rgba(255, 255, 255, 0.5); }
.camera-done:hover { background: rgba(255, 255, 255, 0.12); }
