/* grepcam — terminal-flavored viewfinder chrome over a live camera.
   One accent color (highlighter green). Near-black translucent surfaces.
   Personality lives in the mono type + prompt-style status text. */

:root {
  --bg: #0a0a0a;
  --surface: rgba(12, 12, 12, 0.72);
  --surface-solid: #101010;
  --border: rgba(255, 255, 255, 0.14);
  --fg: #e8e8e8;
  --fg-dim: #8a8a8a;
  --accent: #b8ff2f;          /* highlighter green — hits + focus */
  --accent-ink: #0a0a0a;
  --danger: #ff5c5c;
  --mono: "JetBrains Mono", "IBM Plex Mono", "SFMono-Regular", "SF Mono",
          "Cascadia Code", Menlo, Consolas, "DejaVu Sans Mono", monospace;
  --bar-h: 64px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--mono);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* --- Viewport / layers ---------------------------------------------------- */

.viewport {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background:
    repeating-linear-gradient(45deg, #0d0d0d 0 12px, #0a0a0a 12px 24px);
}

.layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.video,
.still {
  object-fit: cover;
  background: transparent;
}

.overlay {
  pointer-events: none;
  z-index: 2;
}

/* --- Status chip ---------------------------------------------------------- */

.status {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 12px;
  z-index: 3;
  padding: 6px 10px;
  font-size: 12px;
  letter-spacing: 0.02em;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  white-space: nowrap;
  max-width: calc(100% - 24px);
  overflow: hidden;
  text-overflow: ellipsis;
}

.status[data-kind="scanning"] { color: var(--accent); }
.status[data-kind="error"] { color: var(--danger); }

/* --- Top-right menu button ------------------------------------------------ */

.menu-btn {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: 12px;
  z-index: 5;
  width: 40px;
  height: 40px;
  font-size: 18px;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  cursor: pointer;
}
.menu-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- Text mode panel ------------------------------------------------------ */

.roll-text {
  position: absolute;
  inset: 0;
  z-index: 3;
  margin: 0;
  padding: calc(max(12px, env(safe-area-inset-top)) + 52px) 18px 24px;
  overflow-y: auto;
  background: rgba(8, 8, 8, 0.94);
  color: var(--fg);
  font-size: 15px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  -webkit-user-select: text;
  user-select: text;
}
.roll-text mark {
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 2px;
}
.roll-text .roll-text-copy {
  position: sticky;
  top: 0;
  float: right;
}
.roll-text:empty::before,
.roll-text .roll-text-empty { color: var(--fg-dim); }

/* --- Error / no-camera panel ---------------------------------------------- */

.error-panel {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(6, 6, 6, 0.86);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.error-card {
  width: min(420px, 100%);
  padding: 20px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.error-text {
  margin: 0 0 16px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--fg);
  white-space: pre-line;
}

.error-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

/* --- Debug panel (?debug=1) ----------------------------------------------- */

.debug {
  position: absolute;
  right: 12px;
  top: max(12px, env(safe-area-inset-top));
  z-index: 3;
  margin: 0;
  max-width: min(46vw, 320px);
  max-height: 40vh;
  overflow: auto;
  padding: 8px 10px;
  font-size: 11px;
  line-height: 1.45;
  color: var(--fg-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* --- Control bar ---------------------------------------------------------- */

/* Idle call-to-action */
.start-capture {
  position: absolute;
  z-index: 3;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 16px 28px;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--accent-ink);
  background: var(--accent);
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}
.start-capture:hover { filter: brightness(1.08); }
.start-capture:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

/* Search floats over the bottom of the video, above the bar. JS sets its
   `bottom` to sit above the keyboard when the field is focused. */
.search-dock {
  position: fixed;
  left: 12px;
  /* leave room on the right for the roll button when it's showing */
  right: calc(12px + var(--roll-space, 0px));
  width: auto;
  max-width: 460px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 6;
}

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-width: 0;
  padding: 0 12px;
  height: 46px;
  background: rgba(10, 10, 10, 0.82);
  border: 1px solid var(--border);
  border-radius: 10px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.search-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 4px 16px rgba(0, 0, 0, 0.4);
}

.prompt {
  color: var(--accent);
  font-weight: 700;
  user-select: none;
}

.search {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  border: 0;
  background: transparent;
  color: var(--fg);
  font: inherit;
  font-size: 16px; /* ≥16px so iOS doesn't zoom the page on focus */
  outline: none;
}

.search::placeholder { color: var(--fg-dim); }

.count {
  flex: 0 0 auto;
  color: var(--fg-dim);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  min-width: 1.5ch;
  text-align: right;
}

.count[data-hit="true"] { color: var(--accent); }

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  justify-content: space-between;
  overflow-x: auto;
}

/* --- Buttons -------------------------------------------------------------- */

.btn {
  appearance: none;
  height: 40px;
  padding: 0 14px;
  font: inherit;
  font-size: 13px;
  letter-spacing: 0.02em;
  color: var(--fg);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.12s ease, border-color 0.12s ease;
}

.btn:hover { background: rgba(255, 255, 255, 0.12); }
.btn:active { background: rgba(255, 255, 255, 0.18); }

.btn.primary {
  color: var(--accent-ink);
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}
.btn.primary:hover { filter: brightness(1.08); }

.btn.icon {
  width: 40px;
  padding: 0;
  font-size: 16px;
}

.btn[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent);
}

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

/* --- Roll (photo roll) ---------------------------------------------------- */

.roll-btn {
  position: fixed;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 6;
  width: 52px;
  height: 52px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: rgba(10, 10, 10, 0.82);
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}
.roll-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.roll-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.roll-count {
  position: absolute;
  top: -6px;
  left: -6px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

.roll-inner { max-height: 80vh; display: flex; flex-direction: column; }
.roll-head-actions { display: flex; align-items: center; gap: 8px; }

.roll-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
  overflow-y: auto;
  padding: 4px 2px;
  margin-top: 8px;
}

.roll-cell {
  position: relative;
  aspect-ratio: 4 / 3;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #000;
  cursor: pointer;
  padding: 0;
}
.roll-cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.roll-cell:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Dim scans with no match while a search is active */
.roll-cell.nomatch { opacity: 0.35; }

.roll-badge {
  position: absolute;
  top: 5px;
  right: 5px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

.roll-viewer {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.roll-pos {
  font-size: 12px;
  color: var(--fg-dim);
  font-variant-numeric: tabular-nums;
  min-width: 3ch;
  text-align: center;
}

/* --- Settings sheet ------------------------------------------------------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  align-items: end;
  background: rgba(6, 6, 6, 0.6);
}

.sheet-inner {
  background: var(--surface-solid);
  border-top: 1px solid var(--border);
  border-radius: 14px 14px 0 0;
  padding: 16px 16px max(16px, env(safe-area-inset-bottom));
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.sheet-title { color: var(--accent); font-size: 14px; }
.sheet-note { margin: 12px 0 0; color: var(--fg-dim); font-size: 12px; line-height: 1.5; }

.debug-block { margin-top: 14px; border-top: 1px solid rgba(255, 255, 255, 0.08); padding-top: 12px; }
.debug-title { color: var(--fg-dim); font-size: 12px; }
.debug-info {
  margin: 8px 0 0;
  max-height: 30vh;
  overflow: auto;
  font-size: 11px;
  line-height: 1.45;
  color: var(--fg-dim);
  white-space: pre-wrap;
  word-break: break-word;
}

.opt {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  font-size: 14px;
  cursor: pointer;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.opt:last-of-type { border-bottom: 0; }
.opt input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  flex: 0 0 auto;
  cursor: pointer;
}
.opt-hint { color: var(--fg-dim); }
.opt input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- Utility -------------------------------------------------------------- */

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

[hidden] { display: none !important; }

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