:root {
  --bg: #0b0b0d;
  --surface: #151518;
  --surface-2: #1d1d21;
  --line: rgba(255, 255, 255, 0.09);
  --text: #f4f3f0;
  --muted: #8e8d93;
  --accent: #ff3b30;
  --accent-soft: rgba(255, 59, 48, 0.16);
  --glass: rgba(18, 18, 20, 0.55);
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);

  /* Prompteur, piloté par app.js */
  --p-font: 32px;
  --p-lines: 3;
  --p-width: 78%;
  --p-opacity: 0.55;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}
.screen.active { display: flex; }

/* ---------- Éditeur ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(var(--safe-top) + 14px) 20px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: 0.01em; font-size: 17px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }

.icon-btn {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--line);
}
.icon-btn:active { transform: scale(0.94); }

.editor {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 8px 20px 0;
  min-height: 0;
}
.editor-head { display: flex; align-items: baseline; justify-content: space-between; margin: 6px 0 10px; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); }
.stats { display: flex; gap: 6px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
#script {
  flex: 1;
  width: 100%;
  resize: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  padding: 18px;
  font: 400 17px/1.55 inherit;
  font-family: inherit;
  outline: none;
  min-height: 0;
}
#script:focus { border-color: rgba(255, 255, 255, 0.22); }
#script::placeholder { color: #5d5c62; }

.editor-meta {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 0;
}
.editor-meta .spacer { flex: 1; }
.sep { opacity: 0.5; }

.chip {
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font-size: 13px;
  font-weight: 500;
  display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap;
}
.chip.ghost { background: transparent; color: var(--muted); }
.chip.glass { background: var(--glass); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-color: rgba(255,255,255,0.12); }
.chip.static { cursor: default; color: rgba(255,255,255,0.75); }
.chip.big { padding: 14px 20px; font-size: 15px; }
.chip:active:not(.static) { transform: scale(0.96); }

.editor-footer { padding: 6px 20px calc(var(--safe-bottom) + 18px); }

.cta {
  width: 100%;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 17px 22px;
  border-radius: 16px;
  background: var(--text);
  color: #0b0b0d;
  font-weight: 600;
  font-size: 16px;
}
.cta.small { width: auto; padding: 12px 20px; font-size: 15px; }
.cta:active { transform: scale(0.98); }
.cta:disabled { opacity: 0.4; }

/* ---------- Studio ---------- */

#screen-studio { background: #000; }

#preview, #playback {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}
#preview-fx {
  display: none;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}
#screen-studio.fx #preview-fx { display: block; }
/* La vidéo reste lue (source des filtres) mais n'est plus affichée. */
#screen-studio.fx #preview { opacity: 0; }
#preview.mirror, #preview-fx.mirror { transform: scaleX(-1); }
#playback { object-fit: contain; }

.prompter {
  position: absolute;
  top: calc(var(--safe-top) + 10px);
  left: 50%;
  transform: translateX(-50%);
  width: var(--p-width);
  height: calc(var(--p-font) * 1.3 * var(--p-lines) + var(--p-font) * 0.9);
  overflow: hidden;
  border-radius: 20px;
  background: rgba(0, 0, 0, var(--p-opacity));
  backdrop-filter: blur(calc(var(--p-opacity) * 10px));
  -webkit-backdrop-filter: blur(calc(var(--p-opacity) * 10px));
  touch-action: none;
  cursor: grab;
  z-index: 2;
  transition: width 0.2s ease, height 0.2s ease;
}
.viewport {
  position: absolute;
  inset: 0;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 calc(var(--p-font) * 0.42), #000 72%, transparent 100%);
  mask-image: linear-gradient(to bottom, transparent 0, #000 calc(var(--p-font) * 0.42), #000 72%, transparent 100%);
}
.track {
  position: absolute;
  left: 0; right: 0; top: 0;
  padding: calc(var(--p-font) * 0.45) calc(var(--p-font) * 0.6);
  font-size: var(--p-font);
  line-height: 1.3;
  font-weight: 600;
  letter-spacing: -0.005em;
  color: #fff;
  text-shadow: 0 1px 8px rgba(0, 0, 0, 0.45);
  will-change: transform;
}
.track p { margin: 0; }
.track p.gap { height: calc(var(--p-font) * 0.65); }
.track .marker {
  display: flex; align-items: center; gap: 10px;
  margin: calc(var(--p-font) * 0.25) 0;
  font-size: calc(var(--p-font) * 0.42);
  text-transform: uppercase; letter-spacing: 0.2em;
  color: var(--accent);
}
.track .marker::before, .track .marker::after { content: ""; flex: 1; height: 1px; background: currentColor; opacity: 0.5; }
.track .end {
  margin-top: calc(var(--p-font) * 0.6);
  font-size: calc(var(--p-font) * 0.42);
  text-transform: uppercase; letter-spacing: 0.2em; color: var(--muted);
  text-align: center;
}
/* Repère de lecture : petite flèche sur la première ligne, au plus près de l’objectif */
.reading-mark {
  position: absolute;
  left: 6px;
  top: calc(var(--p-font) * 0.45 + var(--p-font) * 0.65 - 5px);
  width: 0; height: 0;
  border-top: 5px solid transparent; border-bottom: 5px solid transparent;
  border-left: 7px solid var(--accent);
  opacity: 0.9;
}

.paused-badge {
  position: absolute;
  left: 50%;
  top: calc(var(--safe-top) + 18px + var(--p-font) * 1.3 * var(--p-lines) + var(--p-font) * 0.9);
  transform: translateX(-50%);
  padding: 6px 12px; border-radius: 999px;
  background: var(--glass); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  font-size: 12px; color: rgba(255,255,255,0.8);
  opacity: 0; transition: opacity 0.25s;
  pointer-events: none;
  z-index: 2;
}
.paused-badge.show { opacity: 1; }

.countdown {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  font-size: 140px; font-weight: 700; color: #fff;
  text-shadow: 0 4px 30px rgba(0,0,0,0.5);
  pointer-events: none;
  z-index: 3;
}
.countdown span { animation: pop 1s ease-out both; }
@keyframes pop {
  0% { transform: scale(1.4); opacity: 0; }
  20% { transform: scale(1); opacity: 1; }
  80% { opacity: 1; }
  100% { transform: scale(0.85); opacity: 0; }
}

.studio-bottom {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding: 40px 16px calc(var(--safe-bottom) + 18px);
  background: linear-gradient(to top, rgba(0,0,0,0.55), transparent);
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  z-index: 2;
  transition: opacity 0.3s;
}
.studio-controls {
  display: flex; align-items: center; justify-content: center; gap: 14px;
  width: 100%;
}
.icon-btn.round { background: var(--glass); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-color: rgba(255,255,255,0.12); width: 48px; height: 48px; }
.studio-sub { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.rec-btn {
  width: 78px; height: 78px;
  border-radius: 50%;
  border: 4px solid rgba(255,255,255,0.92);
  display: grid; place-items: center;
  margin: 0 6px;
}
.rec-inner {
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--accent);
  transition: all 0.22s ease;
}
.rec-btn.recording .rec-inner { width: 30px; height: 30px; border-radius: 8px; }
.rec-btn:active .rec-inner { transform: scale(0.92); }

.rec-status {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; border-radius: 999px;
  background: var(--glass); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  font-variant-numeric: tabular-nums; font-size: 14px; font-weight: 600;
  visibility: hidden;
}
.rec-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0.25; } }

body.recording .rec-status { visibility: visible; }
body.recording [data-back],
body.recording [data-open-settings] { opacity: 0.25; pointer-events: none; }

.cam-error {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  padding: 30px; text-align: center;
  background: var(--bg);
  z-index: 4;
}
.cam-error[hidden] { display: none; }
.cam-error h2 { margin: 0; font-size: 20px; }
.cam-error p { margin: 0 0 8px; color: var(--muted); line-height: 1.5; max-width: 320px; }

.rotate-hint {
  position: absolute; inset: 0; z-index: 10;
  display: none; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  background: var(--bg); color: var(--muted);
}
.rotate-hint svg { width: 44px; height: 44px; }
@media (orientation: landscape) and (max-height: 520px) {
  .rotate-hint { display: flex; }
}

/* ---------- Revue ---------- */

#screen-review { background: #000; }
.review-bottom {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 50px 20px calc(var(--safe-bottom) + 16px);
  background: linear-gradient(to top, rgba(0,0,0,0.85) 40%, transparent);
  display: flex; flex-direction: column; gap: 12px; align-items: center;
}
.review-info { font-size: 13px; color: rgba(255,255,255,0.65); font-variant-numeric: tabular-nums; }
.review-actions { display: flex; gap: 10px; width: 100%; }
.review-actions .chip { flex: 0 0 auto; }
.review-actions .cta { flex: 1; }
.link-btn { color: var(--muted); font-size: 13px; text-decoration: underline; text-underline-offset: 3px; }

/* ---------- Feuille de réglages ---------- */

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0,0,0,0.45);
  opacity: 0; pointer-events: none; transition: opacity 0.25s;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  max-height: 82vh; overflow-y: auto;
  background: rgba(21, 21, 24, 0.94);
  backdrop-filter: blur(24px); -webkit-backdrop-filter: blur(24px);
  border-top: 1px solid var(--line);
  border-radius: 24px 24px 0 0;
  padding: 10px 22px calc(var(--safe-bottom) + 22px);
  transform: translateY(105%);
  transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}
body.sheet-open .sheet { transform: none; }
body.sheet-open .sheet-backdrop { opacity: 1; pointer-events: auto; }
/* Dans le studio, on garde le prompteur visible pendant le réglage */
body.sheet-open.in-studio .sheet-backdrop { background: transparent; }
body.in-studio .sheet { max-height: 58vh; }
@media (min-width: 640px) {
  .sheet { left: 50%; right: auto; width: 480px; margin-left: -240px; }
}
.sheet-handle { width: 40px; height: 5px; border-radius: 3px; background: rgba(255,255,255,0.2); margin: 0 auto 12px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-head h2 { font-size: 18px; margin: 0; }

.setting { display: block; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting[hidden] { display: none; }
#fx-settings .hint { margin-bottom: 4px; }
.setting:last-of-type { border-bottom: 0; }
.setting-row { display: flex; justify-content: space-between; font-size: 15px; margin-bottom: 10px; }
.setting-row output { color: var(--muted); font-variant-numeric: tabular-nums; }
.hint { font-size: 12px; color: var(--muted); margin: 8px 0 0; }
.hint.center { text-align: center; margin-top: 16px; }

input[type="range"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 28px; background: transparent; margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: rgba(255,255,255,0.15); }
input[type="range"]::-moz-range-track { height: 4px; border-radius: 2px; background: rgba(255,255,255,0.15); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 24px; height: 24px; border-radius: 50%;
  background: #fff; margin-top: -10px; box-shadow: 0 2px 8px rgba(0,0,0,0.4);
}
input[type="range"]::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: #fff; border: 0; }

.segmented { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.segmented button { flex: 1; padding: 9px 0; border-radius: 9px; font-size: 14px; color: var(--muted); }
.segmented.looks { flex-wrap: wrap; }
.segmented.looks button { flex: 1 0 30%; }
.segmented button.on { background: rgba(255,255,255,0.12); color: var(--text); font-weight: 600; }

.toggle { display: flex; align-items: center; justify-content: space-between; font-size: 15px; cursor: pointer; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.switch { width: 50px; height: 30px; border-radius: 999px; background: rgba(255,255,255,0.15); position: relative; transition: background 0.2s; flex: none; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform 0.2s; }
.toggle input:checked + .switch { background: #34c759; }
.toggle input:checked + .switch::after { transform: translateX(20px); }

/* ---------- Toast ---------- */

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 150px);
  transform: translate(-50%, 10px);
  padding: 10px 16px; border-radius: 12px;
  background: rgba(30, 30, 34, 0.95); color: var(--text);
  font-size: 14px; max-width: 86vw; text-align: center;
  opacity: 0; pointer-events: none; transition: all 0.25s; z-index: 30;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
