:root {
  --bg: #0b0f17;
  --bg-2: #131a26;
  --panel: #1a2230;
  --accent: #2ec4b6;
  --accent-2: #3a86ff;
  --text: #eef2f7;
  --muted: #8a97a8;
  --danger: #ff5d5d;
  --line: rgba(255, 255, 255, 0.08);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  height: 100%; height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  /* App-like: no text selection, long-press menus, or double-tap zoom delay. */
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  position: fixed; width: 100%;
}

/* Remove the 300ms tap delay / double-tap zoom on all controls. */
button, label, a, input, .filter-item, .page-thumb { touch-action: manipulation; }
img { -webkit-user-drag: none; user-drag: none; }

/* ============================ Screens ============================ */
.screen {
  position: fixed; inset: 0;
  height: 100dvh;            /* tracks the visual viewport (iOS URL bar) */
  display: none;
  flex-direction: column;
  overflow: hidden;
}
.screen.active { display: flex; }

/* ============================ Top bar ============================ */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  min-height: 56px;
  z-index: 5;
}
.topbar .title { font-weight: 600; font-size: 16px; }
.brand { font-weight: 800; font-size: 20px; letter-spacing: 0.5px; color: var(--text); }
.brand span { color: var(--accent); }

.text-btn {
  background: none; border: none; color: var(--accent);
  font-size: 15px; font-weight: 600; padding: 8px 6px; cursor: pointer;
}
.text-btn.danger { color: var(--danger); }
.icon-btn {
  background: rgba(255,255,255,0.08); border: none; color: var(--text);
  width: 40px; height: 40px; border-radius: 50%; font-size: 18px; cursor: pointer;
}
.icon-btn.ghost { background: transparent; }
.icon-btn.on { background: var(--accent); color: #04201d; }

/* ============================ Camera ============================ */
.camera-wrap, .stage {
  position: relative; flex: 1 1 auto;
  min-height: 0;            /* lets the canvas/video shrink instead of pushing the action bar off-screen */
  background: #000;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
#camera { width: 100%; height: 100%; object-fit: cover; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

.hint {
  position: absolute; top: max(16px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,0.55); color: #fff; padding: 8px 14px;
  border-radius: 20px; font-size: 13px; backdrop-filter: blur(4px);
  transition: opacity .3s; max-width: 90%; text-align: center; white-space: nowrap;
}
.no-cam {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; padding: 24px;
  color: var(--muted);
}
.no-cam p { margin: 6px 0; }
.no-cam .sub { font-size: 14px; }
.no-cam b { color: var(--text); }

/* ============================ Stage canvases ============================ */
#cropCanvas, #previewCanvas { max-width: 100%; max-height: 100%; touch-action: none; }
.magnifier {
  position: absolute; width: 120px; height: 120px; border-radius: 50%;
  border: 3px solid var(--accent); box-shadow: var(--shadow); pointer-events: none; z-index: 10;
}
.busy { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,0.35); }

/* ============================ Action bar ============================ */
.actionbar {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: space-around;
  gap: 12px; padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.actionbar.wide { justify-content: space-between; }

.shutter {
  width: 72px; height: 72px; border-radius: 50%;
  background: #fff; border: 4px solid rgba(255,255,255,0.35);
  cursor: pointer; box-shadow: 0 0 0 2px var(--bg) inset;
  transition: transform .08s;
}
.shutter:active { transform: scale(0.92); }

.round-btn {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  background: none; border: none; color: var(--text); cursor: pointer;
  font-size: 11px; min-width: 56px; position: relative;
}
.round-btn .ico { font-size: 22px; }
.round-btn .lbl { color: var(--muted); }

.primary-btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; border: none; border-radius: 12px;
  padding: 14px 26px; font-size: 16px; font-weight: 700; cursor: pointer;
  box-shadow: var(--shadow);
}
.primary-btn:active { filter: brightness(0.92); }

/* ============================ Filters strip ============================ */
.filters {
  flex: 0 0 auto;
  display: flex; gap: 12px; overflow-x: auto; padding: 14px 16px;
  background: var(--bg-2); border-top: 1px solid var(--line);
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.filters::-webkit-scrollbar { display: none; }
.filter-item {
  flex: 0 0 auto; text-align: center; cursor: pointer; opacity: 0.7;
  transition: opacity .15s;
}
.filter-item.active { opacity: 1; }
.filter-item canvas {
  width: 66px; height: 84px; border-radius: 8px; object-fit: cover;
  border: 2px solid transparent; background: #222; display: block;
}
.filter-item.active canvas { border-color: var(--accent); }
.filter-item span { display: block; font-size: 11px; margin-top: 5px; color: var(--muted); }
.filter-item.active span { color: var(--text); }

/* ============================ Sliders ============================ */
.sliders { flex: 0 0 auto; padding: 8px 16px 4px; background: var(--bg-2); }
.slider { display: flex; align-items: center; gap: 12px; font-size: 13px; color: var(--muted); margin: 6px 0; }
.slider span { width: 72px; flex: 0 0 auto; }
.slider output { width: 34px; text-align: right; color: var(--text); }
.slider input[type=range] {
  flex: 1; -webkit-appearance: none; height: 4px; border-radius: 4px;
  background: rgba(255,255,255,0.15);
}
.slider input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%;
  background: var(--accent); cursor: pointer; box-shadow: var(--shadow);
}
.slider input[type=range]::-moz-range-thumb {
  width: 20px; height: 20px; border-radius: 50%; border: none;
  background: var(--accent); cursor: pointer;
}

/* ============================ Pages tray ============================ */
.pages-tray {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--panel); border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0; box-shadow: var(--shadow);
  padding: 14px 16px; padding-bottom: max(14px, env(safe-area-inset-bottom));
  transform: translateY(0); transition: transform .25s;
  max-height: 72dvh; overflow-y: auto;
}
.tray-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.pages-list { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 8px; min-height: 110px; -webkit-overflow-scrolling: touch; }
.pages-list img { height: 100px; border-radius: 6px; border: 1px solid var(--line); }
.page-thumb { position: relative; flex: 0 0 auto; }
.page-thumb button {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%;
  background: var(--danger); color: #fff; border: none; font-size: 13px; cursor: pointer;
}
.tray-actions { display: flex; gap: 12px; align-items: center; margin-top: 10px; }

/* ============================ Loader / toast ============================ */
.loader {
  position: fixed; inset: 0; z-index: 100;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--bg); gap: 18px;
}
.loader.hidden { display: none; }
.spinner {
  width: 44px; height: 44px; border-radius: 50%;
  border: 4px solid rgba(255,255,255,0.15); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
.spinner.small { width: 28px; height: 28px; border-width: 3px; }
@keyframes spin { to { transform: rotate(360deg); } }
.loader-text { color: var(--muted); font-size: 14px; }

.toast {
  position: fixed; bottom: calc(24px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: #fff; color: #111; padding: 12px 20px; border-radius: 24px;
  font-size: 14px; font-weight: 600; z-index: 200; box-shadow: var(--shadow);
  max-width: 86%; text-align: center;
}
.toast.err { background: var(--danger); color: #fff; }

/* ===================== Landscape / short viewports ===================== */
/* Compact the chrome so the document stays visible and nothing is clipped. */
@media (max-height: 560px) {
  .topbar { min-height: 46px; padding: 8px 14px; padding-top: max(8px, env(safe-area-inset-top)); }
  .shutter { width: 56px; height: 56px; border-width: 3px; }
  .filters { padding: 8px 12px; gap: 8px; }
  .filter-item canvas { width: 50px; height: 64px; }
  .filter-item span { font-size: 10px; margin-top: 3px; }
  .sliders { padding: 4px 12px 0; }
  .slider { margin: 3px 0; }
  .slider span { width: 64px; }
  .actionbar { padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .actionbar.wide .round-btn .lbl { display: none; }
  .round-btn .ico { font-size: 20px; }
  .primary-btn { padding: 10px 20px; font-size: 15px; }
}

/* ===================== Narrow phones ===================== */
@media (max-width: 360px) {
  .filter-item canvas { width: 58px; height: 74px; }
  .primary-btn { padding: 13px 18px; font-size: 15px; }
  .round-btn { min-width: 48px; font-size: 10px; }
  .slider span { width: 64px; }
}

/* Respect reduced-motion preferences. */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
  .spinner { animation: spin 0.9s linear infinite; }
}
