/* Mobile-first: base styles target a phone; @media (min-width) enhances for larger screens. */
:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #1c2733;
  --muted: #6b7280;
  --accent: #3a7bd5;
  --hot: #e23c3c;
  --ok: #2e9e5b;
  --tap: 48px;               /* minimum touch target */
}
* { box-sizing: border-box; }
/* The [hidden] attribute must win over the display rules below (e.g. video is
   display:block), otherwise JS toggling hidden=true leaves elements on screen. */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 16px;            /* >=16px avoids iOS input zoom */
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}

/* Sticky, thumb-friendly header */
header {
  position: sticky; top: 0; z-index: 500;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: #21323f; color: #fff;
}
header h1 { font-size: 1.05rem; margin: 0; line-height: 1.2; }
header nav a { color: #cfe0ff; text-decoration: none; font-weight: 600; font-size: .9rem; white-space: nowrap; }

main {
  max-width: 640px; margin: 0 auto;
  padding: 14px 14px calc(24px + env(safe-area-inset-bottom));
}
/* The admin dashboard has wide tables — give it more room on larger screens. */
main.wide { max-width: 1100px; }

.card {
  background: var(--card); border-radius: 14px; padding: 16px;
  margin-bottom: 14px; box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.card h2 { margin: 0 0 12px; font-size: 1rem; color: #37485a; }
.card h3 { margin: 14px 0 8px; font-size: .95rem; color: #37485a; }

.status { margin: 6px 0 0; font-size: .95rem; line-height: 1.4; }
.muted { color: var(--muted); font-size: .85rem; }

/* Capture-mode toggle: full-width segmented control */
.mode-toggle { display: flex; gap: 8px; margin-bottom: 12px; }
.chip {
  flex: 1; min-height: var(--tap);
  border: 1px solid #d0d7de; background: #fff; border-radius: 999px;
  padding: 0 12px; cursor: pointer; font-size: .95rem;
}
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Media fills the width on a phone */
video, img#photo-preview {
  width: 100%; aspect-ratio: 3 / 4; max-height: 70vh;
  background: #000; border-radius: 12px; object-fit: contain; display: block;
}
#playback, #photo-preview { aspect-ratio: auto; max-height: 50vh; }

.controls { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }

/* Camera adjustments (torch toggle + zoom), shown only if the device supports them */
.cam-adjust { display: flex; align-items: center; gap: 12px; margin-top: 10px; flex-wrap: wrap; }
#torch-btn { flex: 0 0 auto; }
.zoom-wrap { flex: 1 1 180px; display: flex; align-items: center; gap: 8px; font-size: .9rem; color: var(--muted); }
.zoom-wrap input[type="range"] { flex: 1; min-width: 100px; height: var(--tap); }
button {
  min-height: var(--tap);
  border: none; border-radius: 10px; padding: 0 18px; font-size: 1rem; font-weight: 600;
  background: var(--accent); color: #fff; cursor: pointer;
  touch-action: manipulation;
}
button:disabled { background: #b9c2cc; cursor: not-allowed; }
button.recording { background: var(--hot); }
button.secondary { background: #fff; color: var(--hot); border: 1px solid var(--hot); }
button.danger { background: var(--hot); color: #fff; }
#discard-btn { flex: 1 1 auto; }

/* Primary actions go full-width on mobile for easy thumb reach */
#start-camera, #record-btn, #photo-btn { flex: 1 1 auto; }
#upload-btn { width: 100%; background: var(--ok); font-size: 1.05rem; }
.timer { flex: 0 0 auto; font-variant-numeric: tabular-nums; color: var(--muted); min-width: 3.5em; text-align: right; }

/* Transient top-of-screen toast (upload success/failure) */
.toast {
  position: fixed; top: calc(10px + env(safe-area-inset-top)); left: 50%;
  transform: translateX(-50%) translateY(-16px);
  z-index: 1000; max-width: 92%; padding: 12px 18px; border-radius: 10px;
  color: #fff; font-weight: 600; font-size: .95rem; text-align: center;
  box-shadow: 0 4px 16px rgba(0,0,0,.28);
  opacity: 0; pointer-events: none; transition: opacity .25s ease, transform .25s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.success { background: var(--ok); }
.toast.error { background: var(--hot); }
.toast.info { background: #37485a; }

/* Admin form controls */
input[type="password"], input[type="text"] {
  width: 100%; min-height: var(--tap); margin-bottom: 10px;
  padding: 0 14px; font-size: 1rem; border: 1px solid #d0d7de; border-radius: 10px;
}
.check { display: flex; align-items: center; gap: 8px; margin: 10px 0; font-size: .95rem; }
.check input { width: 20px; height: 20px; }
code { background: #eef1f4; padding: 1px 5px; border-radius: 4px; font-size: .85em; }

/* Dual-knob time range slider (shared: admin + map) */
.time-filter { margin: 4px 0 14px; }
.time-filter .tf-labels {
  display: flex; justify-content: space-between; gap: 8px;
  font-size: .85rem; color: var(--muted); margin-bottom: 6px;
  font-variant-numeric: tabular-nums;
}
.range-slider { position: relative; height: 34px; width: 100%; }
.range-slider .rs-track {
  position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%);
  height: 6px; border-radius: 999px; background: #d0d7de;
}
.range-slider .rs-fill {
  position: absolute; top: 0; bottom: 0; border-radius: 999px; background: var(--accent);
}
.range-slider input[type="range"] {
  position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 0;
  background: none; pointer-events: none; -webkit-appearance: none; appearance: none;
}
.range-slider input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; pointer-events: auto;
  width: 22px; height: 22px; border-radius: 50%; cursor: pointer;
  background: #fff; border: 2px solid var(--accent); box-shadow: 0 1px 3px rgba(0,0,0,.3);
}
.range-slider input[type="range"]::-moz-range-thumb {
  pointer-events: auto; width: 22px; height: 22px; border-radius: 50%; cursor: pointer;
  background: #fff; border: 2px solid var(--accent); box-shadow: 0 1px 3px rgba(0,0,0,.3);
}
.range-slider input[type="range"]::-moz-range-track { background: none; }

/* Admin: report image preview gallery + lightbox */
.row-actions { display: flex; gap: 8px; align-items: center; white-space: nowrap; }
tr.gallery-detail > td { background: #f8fafc; }
.gallery {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px; padding: 4px 2px;
}
.thumb { margin: 0; }
.thumb img {
  width: 100%; aspect-ratio: 1 / 1; object-fit: cover;
  border-radius: 8px; background: #e5e7eb; cursor: zoom-in; display: block;
}
.thumb figcaption { font-size: .75rem; color: var(--muted); margin-top: 3px; text-align: center; white-space: normal; }
#lightbox {
  position: fixed; inset: 0; z-index: 1000; background: rgba(0,0,0,.85);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 20px;
}
#lightbox img { max-width: 95vw; max-height: 82vh; object-fit: contain; border-radius: 6px; }
#lightbox #lightbox-cap { color: #eee; font-size: .9rem; margin: 0; }
#lightbox-close {
  position: absolute; top: 14px; right: 16px; background: rgba(255,255,255,.15);
  color: #fff; border-radius: 999px; width: 44px; height: 44px; padding: 0; font-size: 1.1rem;
}

/* Workers table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid #eef1f4; vertical-align: top; white-space: nowrap; }
th { color: #6b7280; font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.wdot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; }
.wdot.on { background: #2e9e5b; box-shadow: 0 0 0 3px rgba(46,158,91,.2); }
.wdot.off { background: #b9c2cc; }

/* Map page */
#map { height: calc(100dvh - 130px); width: 100%; }
body:has(#map-filter:not([hidden])) #map { height: calc(100dvh - 210px); }
.map-filter-bar {
  padding: 8px 16px 4px; background: #fff; border-bottom: 1px solid #e5e7eb;
}
.map-filter-bar .tf-labels {
  display: flex; justify-content: space-between; gap: 8px;
  font-size: .85rem; color: var(--muted); margin-bottom: 2px;
}
.legend {
  display: flex; gap: 14px 18px; flex-wrap: wrap; align-items: center;
  padding: 10px 16px; background: #fff; border-bottom: 1px solid #e5e7eb; font-size: .85rem;
}
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }
/* Legend items double as category show/hide toggles */
.legend-toggle {
  background: none; border: none; padding: 4px 6px; margin: 0; cursor: pointer;
  font: inherit; color: var(--ink); min-height: auto; font-weight: 500;
  border-radius: 8px; display: inline-flex; align-items: center;
}
.legend-toggle:hover { background: #eef1f4; }
.legend-toggle.off { color: var(--muted); text-decoration: line-through; opacity: .55; }
.legend-toggle.off .dot { filter: grayscale(1); }
.dot.hot { background: var(--hot); }
.dot.clean { background: var(--accent); }
.dot.pending { background: #c9a227; }
/* Map: admin image-unlock control + popup thumbnail */
.img-unlock { display: inline-flex; align-items: center; gap: 6px; margin-left: auto; }
.img-unlock input[type="password"] {
  width: 130px; min-height: 34px; margin: 0; padding: 0 10px; font-size: .85rem;
  border: 1px solid #d0d7de; border-radius: 8px;
}
.img-unlock button { min-height: 34px; padding: 0 10px; font-size: .85rem; border-radius: 8px; }
.popup-media { margin-top: 6px; }
.popup-thumb { width: 240px; max-width: 72vw; border-radius: 6px; cursor: zoom-in; display: block; }

/* Popup action buttons (admin) */
.popup-actions { margin-top: 8px; }
.popup-btn {
  min-height: 34px; padding: 0 12px; font-size: .85rem; border-radius: 8px;
  border: 1px solid var(--accent); background: var(--accent); color: #fff; cursor: pointer;
}

/* Relocate banner pinned to the bottom of the map */
.relocate-bar {
  position: fixed; left: 50%; bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%); z-index: 1000; max-width: 94%;
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  padding: 10px 14px; border-radius: 10px; background: #1f2a37; color: #fff;
  box-shadow: 0 4px 16px rgba(0,0,0,.35); font-size: .9rem;
}
.relocate-bar[hidden] { display: none; }
.relocate-bar button { min-height: 36px; padding: 0 14px; border-radius: 8px; border: none; cursor: pointer; font-weight: 600; }
.reloc-primary { background: var(--ok); color: #fff; }
.reloc-secondary { background: transparent; color: #cbd5e1; border: 1px solid #47566a !important; }
.reloc-pin { font-size: 26px; line-height: 30px; text-align: center; cursor: grab; }
.reloc-pin:active { cursor: grabbing; }

/* Desktop enhancement */
@media (min-width: 700px) {
  header h1 { font-size: 1.2rem; }
  main { padding: 20px 16px; }
  .card { padding: 18px 20px; }
  #start-camera, #record-btn, #photo-btn { flex: 0 0 auto; }
  #upload-btn { width: auto; min-width: 200px; }
  video, img#photo-preview { aspect-ratio: 16 / 10; max-height: 420px; }
}
