/* Sentinel - dark, monospace, minimal. No frameworks, no CDNs.
   Mobile-first; layout adapts at a single breakpoint. */

:root {
  --bg: #0d0f12;
  --panel: #15181d;
  --panel-2: #1c2026;
  --border: #2a2f37;
  --text: #e6e8eb;
  --text-dim: #8a8f98;
  --accent: #6ea8ff;
  --danger: #ff6b6b;
  --ok: #6ed28a;
  --ts: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

/* hidden attribute must beat any display:flex/grid we set below. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
}

/* Safe-area aware for notched / folding phones. */
body {
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

/* ============================================================
   Lock screen
   ============================================================ */
#lock {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 20px;
}
#lock-form {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 24px;
}
#lock-form h1 {
  margin: 0 0 4px;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.5px;
}
.lede {
  margin: 0 0 20px;
  color: var(--text-dim);
  font-size: 14px;
}
#lock-form label {
  display: block;
  margin: 14px 0 6px;
  font-size: 13px;
  color: var(--text-dim);
}
#lock-form input {
  width: 100%;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--ts);
  font-size: 13px;
  outline: none;
}
#lock-form input:focus {
  border-color: var(--accent);
}
#unlock-btn {
  width: 100%;
  margin-top: 20px;
  padding: 11px;
  background: var(--accent);
  color: #0d0f12;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
#unlock-btn:active { opacity: 0.85; }
.error {
  margin: 12px 0 0;
  color: var(--danger);
  font-size: 13px;
}
.hint {
  margin: 16px 0 0;
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1.5;
}

/* ============================================================
   Main app shell
   ============================================================ */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

#topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand {
  font-weight: 600;
  letter-spacing: 0.5px;
}
.status {
  flex: 1;
  color: var(--text-dim);
  font-family: var(--ts);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}
.actions button {
  padding: 6px 10px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}
.actions button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.actions button:not(:disabled):active {
  background: var(--border);
}
#delete-btn:not(:disabled):hover { border-color: var(--danger); }
#diff-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(110, 168, 255, 0.1);
}

/* Diff threshold preview bar - between topbar and viewer. */
#diff-bar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 8px 14px;
  flex-shrink: 0;
}
.diff-controls {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.diff-label {
  font-size: 12px;
  color: var(--text-dim);
  font-family: var(--ts);
}
.diff-label span {
  color: var(--text);
}
.diff-stats {
  font-family: var(--ts);
  font-size: 12px;
  color: var(--text-dim);
  flex: 1;
}
.diff-stats .trigger { color: var(--ok); }
.diff-stats .no-trigger { color: var(--danger); }

/* ============================================================
   Viewer
   ============================================================ */
#viewer {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #07090b;
  overflow: hidden;
  padding: 8px;
}
.viewer-empty {
  color: var(--text-dim);
  font-size: 14px;
}
#viewer-figure {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  max-height: 100%;
}
#viewer-img {
  max-width: 100%;
  max-height: calc(100dvh - 220px);
  object-fit: contain;
  border-radius: 4px;
  /* Camera is mounted upside down. Rotate the decoded JPEG in the
     viewer rather than re-encoding on the ESP - saves PSRAM and
     lets us re-orient without reflashing. */
  transform: rotate(180deg);
}
#viewer-caption {
  margin-top: 8px;
  font-family: var(--ts);
  font-size: 12px;
  color: var(--text-dim);
}

/* ============================================================
   Thumbnail strip
   ============================================================ */
#thumbs {
  flex-shrink: 0;
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 10px 12px 12px;
}
#thumbs-strip {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  scroll-snap-type: x proximity;
  padding-bottom: 4px;
}
#thumbs-strip::-webkit-scrollbar { height: 8px; }
#thumbs-strip::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
.thumb {
  position: relative;
  flex-shrink: 0;
  width: 128px;
  scroll-snap-align: start;
  cursor: pointer;
}
.thumb-canvas {
  display: block;
  width: 128px;
  height: 96px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  object-fit: cover;
}
.thumb.selected .thumb-canvas {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}
.thumb-check {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--accent);
}
.thumb-ts {
  display: block;
  margin-top: 4px;
  font-family: var(--ts);
  font-size: 11px;
  color: var(--text-dim);
  word-break: break-all;
}
.thumbs-empty {
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
  padding: 14px 0;
}

/* ============================================================
   Responsive - phones get a tighter topbar.
   ============================================================ */
@media (max-width: 600px) {
  body { font-size: 14px; }
  #topbar { flex-wrap: wrap; gap: 8px; padding: 8px 10px; }
  .brand { font-size: 14px; }
  .status { order: 3; flex-basis: 100%; }
  .actions button { padding: 6px 8px; font-size: 12px; }
  #viewer-img { max-height: calc(100dvh - 240px); }
  .thumb { width: 96px; }
  .thumb-canvas { width: 96px; height: 72px; }
}

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

/* ============================================================
   Settings drawer - slides in from the right.
   Layout: fixed header / scrolling body / fixed footer.
   The footer holds the calibration preview + actions so they're
   always visible while you scroll the sliders.
   ============================================================ */
#settings-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: 460px;
  max-width: 94vw;
  height: 100dvh;
  background: var(--panel);
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transform: translateX(0);
  transition: transform 0.18s ease-out;
}
#settings-drawer[hidden] {
  display: flex;
  transform: translateX(100%);
  pointer-events: none;
}
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.drawer-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}
#settings-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
}
#settings-close:hover { color: var(--text); }

/* Scrolling body - holds the fieldsets only. */
.drawer-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 16px 18px;
  margin: 0;  /* it's a <form> now, reset */
}

/* Fixed footer - full-width calibration image, then countdown +
   actions below it. */
.drawer-foot {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.cal-frame {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
#calibration-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  /* Camera is mounted upside down - flip to match the main viewer. */
  transform: rotate(180deg);
}
.cal-empty {
  color: var(--text-dim);
  font-size: 12px;
  padding: 12px;
  text-align: center;
}
.foot-right {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.poll-countdown {
  margin: 0;
  font-family: var(--ts);
  font-size: 11px;
  color: var(--text-dim);
}
.settings-status {
  margin: 0;
  font-size: 11px;
  font-family: var(--ts);
  color: var(--text-dim);
  word-break: break-word;
}
.settings-status.error { color: var(--danger); }
.settings-status.ok { color: var(--ok); }
.drawer-actions {
  display: flex;
  gap: 8px;
}
.drawer-actions button {
  flex: 1;
  padding: 9px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}
#settings-save {
  background: var(--accent);
  color: #0d0f12;
  border-color: var(--accent);
  font-weight: 600;
}
.drawer-actions button:active { opacity: 0.85; }

/* Fieldsets / controls (unchanged) */
fieldset {
  border: none;
  padding: 0 0 16px;
  margin: 0 0 8px;
  border-bottom: 1px solid var(--border);
}
fieldset:last-of-type { border-bottom: none; }
legend {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  padding: 0 0 8px;
}
fieldset label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-dim);
  margin: 10px 0 4px;
}
fieldset .val {
  font-family: var(--ts);
  color: var(--text);
  font-size: 12px;
}
fieldset input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
fieldset select {
  width: 100%;
  padding: 6px 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: var(--sans);
  font-size: 13px;
}
.check-row {
  display: flex !important;
  flex-direction: row-reverse !important;
  justify-content: flex-end !important;
  gap: 8px;
  cursor: pointer;
  margin: 6px 0 !important;
  color: var(--text) !important;
  font-size: 13px;
}
.check-row input { accent-color: var(--accent); }
