:root {
  --bg: #07070a;
  --surface: #121219;
  --surface-2: #1c1c27;
  --glass: rgba(14, 14, 21, 0.8);
  --line: rgba(255, 255, 255, 0.09);
  --line-strong: rgba(255, 255, 255, 0.2);
  --text: #f3f3f7;
  --dim: #8d8d9e;
  --magenta: #ff2e88;
  --cyan: #00e5ff;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  color-scheme: dark;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--mono);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-font-smoothing: antialiased;
}
body { position: fixed; inset: 0; }

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; margin: 0; cursor: pointer; }
p { margin: 0; }
[hidden] { display: none !important; }

#app {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}

/* ── top bar ───────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding: 0 8px 0 16px;
}

.brand {
  font-size: 17px;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-shadow: -1.5px 0 var(--magenta), 1.5px 0 var(--cyan);
  animation: brand-glitch 6s linear infinite;
}
.brand span:last-child { color: var(--dim); }

@keyframes brand-glitch {
  0%, 90%, 100% { transform: none; text-shadow: -1.5px 0 var(--magenta), 1.5px 0 var(--cyan); }
  91% { transform: translateX(2px) skewX(-12deg); text-shadow: -5px 0 var(--magenta), 3px 0 var(--cyan); }
  93% { transform: translateX(-2px); text-shadow: 4px 0 var(--magenta), -4px 0 var(--cyan); }
  95% { transform: translateX(1px) skewX(6deg); text-shadow: -2px 1px var(--magenta), 2px -1px var(--cyan); }
}

.top-actions { display: flex; align-items: center; gap: 2px; }

.icon-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  transition: background 0.15s;
}
.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-btn svg .fill { fill: currentColor; stroke: none; }
.icon-btn:active { background: var(--surface-2); }
.icon-btn.small { width: 36px; height: 36px; border-radius: 10px; }
.icon-btn.small svg { width: 18px; height: 18px; }
.icon-btn.big {
  width: 54px;
  height: 54px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
}
.icon-btn.big svg { width: 26px; height: 26px; }
.icon-btn.big:active { transform: rotate(-12deg) scale(0.95); }

.pill {
  height: 30px;
  min-width: 52px;
  padding: 0 12px;
  margin-right: 4px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
}
.pill:active { background: var(--surface-2); }

/* ── stage / viewport ──────────────────── */

.stage {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  overflow: hidden;
}

.viewport {
  position: relative;
  background: #000;
  overflow: hidden;
  border-radius: 6px;
}
.viewport canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
}
.flash.go { animation: flash 0.35s ease-out; }
@keyframes flash { from { opacity: 0.85; } to { opacity: 0; } }

.badge {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 11px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid var(--line-strong);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.12em;
  white-space: nowrap;
  pointer-events: none;
}
.badge-live { pointer-events: auto; color: var(--magenta); }

.message {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
  background: var(--bg);
}
.message-title { font-size: 16px; font-weight: 700; letter-spacing: 0.06em; }
.message-body { max-width: 320px; color: var(--dim); font-size: 13px; line-height: 1.55; }
.message-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 8px; }

.btn {
  height: 46px;
  padding: 0 18px;
  border-radius: 13px;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
}
.btn:active { opacity: 0.75; }
.btn.primary { background: var(--text); color: #000; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; margin-top: 8px; }

/* ── effect panel ──────────────────────── */

.panel {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  max-width: 520px;
  max-height: min(55%, 380px);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(150%);
  backdrop-filter: blur(20px) saturate(150%);
  border: 1px solid var(--line);
  border-radius: 18px;
  overflow: hidden;
  animation: panel-in 0.18s ease-out;
}
@keyframes panel-in { from { opacity: 0; transform: translateY(12px); } }

.panel-head { display: flex; align-items: center; gap: 4px; padding: 8px 6px 2px 16px; }
.panel-title { flex: 1; font-size: 13px; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; }
.panel-head .switch { margin-right: 6px; }
.panel-body {
  overflow-y: auto;
  padding: 2px 16px 14px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.ctl { padding: 5px 0; }
.ctl > label, .ctl-row label {
  font-size: 11px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--dim);
}
.ctl-row { display: flex; justify-content: space-between; align-items: baseline; }
.ctl output { font-size: 12px; font-variant-numeric: tabular-nums; }

.ctl-seg > label { display: block; margin-bottom: 7px; }
.ctl-toggle { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; }

/* range slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 32px;
  margin: 0;
  background: transparent;
  --fill: linear-gradient(90deg, var(--magenta), var(--cyan)) no-repeat 0 0 / var(--pct, 50%) 100%;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: var(--fill), rgba(255, 255, 255, 0.13);
}
input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: var(--fill), rgba(255, 255, 255, 0.13);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  margin-top: -9px;
  border-radius: 5px;
  background: #fff;
  box-shadow: -2px 0 0 var(--magenta), 2px 0 0 var(--cyan);
}
input[type="range"]::-moz-range-thumb {
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 5px;
  background: #fff;
  box-shadow: -2px 0 0 var(--magenta), 2px 0 0 var(--cyan);
}

/* segmented options */
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  height: 34px;
  padding: 0 12px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid transparent;
  color: var(--dim);
  font-size: 12px;
  font-weight: 600;
}
.seg button.on { color: var(--text); border-color: var(--magenta); background: rgba(255, 46, 136, 0.16); }

/* switch */
.switch { position: relative; display: inline-block; flex: none; width: 44px; height: 26px; }
.switch input { position: absolute; inset: 0; z-index: 1; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: rgba(255, 255, 255, 0.15); transition: background 0.15s; }
.switch span::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}
.switch input:checked + span { background: var(--magenta); }
.switch input:checked + span::after { transform: translateX(18px); }

/* looks grid */
.preset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; padding-top: 6px; }
.preset {
  height: 46px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.preset:active { background: rgba(255, 46, 136, 0.22); }
.preset-random { color: var(--cyan); border-color: rgba(0, 229, 255, 0.45); }

/* ── effect chips ──────────────────────── */

.chips {
  display: flex;
  gap: 8px;
  padding: 10px 12px 6px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
.chips > :first-child { margin-left: auto; }
.chips > :last-child { margin-right: auto; }

.chip {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--dim);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.chip .dot { width: 7px; height: 7px; border-radius: 2px; border: 1.5px solid currentColor; transition: all 0.15s; }
.chip.on { color: var(--text); }
.chip.on .dot { background: var(--magenta); border-color: var(--magenta); box-shadow: 0 0 8px var(--magenta); }
.chip.selected { background: var(--surface-2); border-color: var(--text); }
.chip-presets { color: var(--cyan); border-color: rgba(0, 229, 255, 0.3); }

/* ── dock ──────────────────────────────── */

.dock {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas: ". mode ." "thumb shutter actions";
  align-items: center;
  row-gap: 6px;
  padding: 2px 20px 14px;
}

.mode-switch { grid-area: mode; display: flex; gap: 4px; justify-self: center; }
.mode-switch button {
  padding: 5px 11px;
  border-radius: 999px;
  color: var(--dim);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.mode-switch button.on { color: var(--text); background: rgba(255, 255, 255, 0.09); }

.thumb {
  grid-area: thumb;
  position: relative;
  justify-self: start;
  width: 54px;
  height: 54px;
  border-radius: 14px;
  background: var(--surface) center / cover no-repeat;
  border: 1px solid var(--line-strong);
}
.thumb:not(.has) { opacity: 0.4; pointer-events: none; }
.thumb.is-video::after {
  content: "▶";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 14px;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
}

.dock-actions { grid-area: actions; justify-self: end; display: flex; gap: 8px; }
.dock-actions .icon-btn.big { width: 48px; height: 48px; border-radius: 14px; }
.dock-actions .icon-btn.big svg { width: 24px; height: 24px; }
#btn-reset.idle { opacity: 0.35; }

.shutter {
  grid-area: shutter;
  position: relative;
  width: 78px;
  height: 78px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 4px solid #fff;
  box-shadow: -2px 0 0 var(--magenta), 2px 0 0 var(--cyan);
}
.shutter span {
  width: 62px;
  height: 62px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.12s, background 0.12s;
}
.shutter:active span { transform: scale(0.86); }
.shutter.busy span { transform: scale(0.7); background: var(--magenta); }
.shutter.video span { background: #ff3b30; transition: transform 0.12s, width 0.2s, height 0.2s, border-radius 0.2s; }
.shutter.video.recording span { width: 30px; height: 30px; border-radius: 7px; }

/* While recording, controls that would change the frame are locked. */
#app.recording .lock-when-rec { opacity: 0.3; pointer-events: none; }

.badge-rec {
  left: 10px;
  transform: none;
  display: flex;
  align-items: center;
  gap: 7px;
  background: rgba(255, 59, 48, 0.88);
  border-color: transparent;
  font-variant-numeric: tabular-nums;
}
.badge-rec i { width: 7px; height: 7px; border-radius: 50%; background: #fff; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* zoom / lens buttons over the bottom of the picture */
.zoom {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.42);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}
.zoom:empty { display: none; }
.zoom button {
  min-width: 34px;
  height: 34px;
  padding: 0 8px;
  border-radius: 999px;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.zoom button.on { color: var(--cyan); background: rgba(0, 0, 0, 0.55); min-width: 40px; }

/* ── review ────────────────────────────── */

.review {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: calc(var(--safe-t) + 16px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 16px) calc(var(--safe-l) + 16px);
  background: rgba(4, 4, 7, 0.96);
  animation: fade-in 0.18s ease-out;
}
.review-img { position: relative; flex: 1; min-height: 0; }
.review-img img, .review-img video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.review-img img {
  -webkit-touch-callout: default;
  -webkit-user-select: auto;
  user-select: auto;
}
.review-hint { text-align: center; font-size: 12px; line-height: 1.5; color: var(--dim); }
.review-actions { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; width: 100%; max-width: 480px; margin: 0 auto; }

@keyframes fade-in { from { opacity: 0; } }

/* ── settings sheet ────────────────────── */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55);
  animation: fade-in 0.15s ease-out;
}
.sheet-card {
  width: 100%;
  max-width: 520px;
  max-height: 88%;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-b);
  background: var(--surface);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 22px 22px 0 0;
  animation: sheet-up 0.22s ease-out;
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 10px 4px 20px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.sheet-body { display: flex; flex-direction: column; gap: 10px; padding: 6px 20px 20px; overflow-y: auto; }
.note { margin-top: 7px; font-size: 11px; line-height: 1.5; color: var(--dim); }
.tip {
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(0, 229, 255, 0.07);
  border: 1px solid rgba(0, 229, 255, 0.28);
  font-size: 12px;
  line-height: 1.55;
}
.tip strong { color: var(--cyan); letter-spacing: 0.04em; }
.tip p { margin-top: 4px; color: var(--dim); }

/* ── toast ─────────────────────────────── */

.toast {
  position: fixed;
  top: calc(var(--safe-t) + 60px);
  left: 50%;
  z-index: 40;
  max-width: calc(100% - 32px);
  padding: 9px 15px;
  transform: translateX(-50%);
  border-radius: 999px;
  background: rgba(20, 20, 28, 0.94);
  border: 1px solid var(--line-strong);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}
.toast.show { animation: toast-in 0.2s ease-out; }
.toast.has-action { display: flex; align-items: center; gap: 12px; padding: 5px 5px 5px 15px; pointer-events: auto; }
.toast-action {
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(0, 229, 255, 0.16);
  color: var(--cyan);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -8px); } }

/* The camera <video> must stay "visible" for iOS to keep decoding frames. */
#video { position: fixed; top: 0; left: 0; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* ── phone held sideways: controls move to the side ── */

@media (orientation: landscape) and (max-height: 600px) {
  #app {
    grid-template-columns: auto minmax(0, 1fr) auto auto;
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: "top stage chips dock";
  }

  .topbar {
    grid-area: top;
    flex-direction: column;
    justify-content: flex-start;
    gap: 12px;
    width: 58px;
    height: auto;
    padding: 12px 0;
  }
  .brand { writing-mode: vertical-rl; font-size: 14px; }
  .top-actions { flex-direction: column; gap: 2px; }
  .pill { min-width: 48px; padding: 0 6px; margin: 0 0 6px; }

  .stage { grid-area: stage; }

  .chips {
    grid-area: chips;
    flex-direction: column;
    width: 140px;
    padding: 10px 4px 10px 8px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .chips > :first-child, .chips > :last-child { margin: 0; }
  .chip { width: 100%; }

  .dock {
    grid-area: dock;
    grid-template-columns: auto;
    grid-template-rows: auto 1fr auto auto 1fr auto;
    grid-template-areas: "actions" "." "shutter" "mode" "." "thumb";
    justify-items: center;
    row-gap: 8px;
    padding: 10px 10px 10px 6px;
  }
  .dock-actions { justify-self: center; flex-direction: column; }
  .thumb { justify-self: center; }
  .mode-switch { flex-direction: column; gap: 2px; }

  .panel {
    top: 8px;
    bottom: 8px;
    left: auto;
    right: 8px;
    width: min(340px, calc(100% - 16px));
    max-height: none;
  }

  .toast { top: calc(var(--safe-t) + 12px); }

  .review { gap: 8px; padding-top: calc(var(--safe-t) + 10px); padding-bottom: calc(var(--safe-b) + 10px); }
  .review-actions { max-width: 420px; }
  .review-actions .btn { height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  .brand { animation: none; }
  .flash.go, .panel, .sheet-card, .review { animation: none; }
}
