:root {
  --bg: #1b1d3a;
  --panel: #262950;
  --panel-2: #31356a;
  --text: #f7f5ff;
  --muted: #b9b6dd;
  --accent: #ffd23f;
  --accent-text: #1b1d3a;
  --coral: #ff5d73;
  --teal: #2ec4b6;
  --radius: 14px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.35 "Trebuchet MS", ui-rounded, "Segoe UI", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.15; }

/* ---------- shared buttons ---------- */
.chip-button {
  display: inline-flex; align-items: center; justify-content: center; gap: .35em;
  min-height: 44px; padding: .45em .9em;
  border: 0; border-radius: 999px;
  background: var(--panel-2); color: var(--text);
  font-weight: 700; text-decoration: none; white-space: nowrap;
}
.chip-button:hover { filter: brightness(1.15); }
.chip-button:disabled { opacity: .45; cursor: default; filter: none; }
.chip-button.accent { background: var(--accent); color: var(--accent-text); }
.chip-button.danger { background: #5a2440; }
.chip-button.toggle[aria-pressed="true"], .icon-button.toggle[aria-pressed="true"] { background: var(--teal); color: #08201e; }
.icon-button {
  position: relative; width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 12px; background: var(--panel-2); font-size: 1.3rem;
}
.icon-button:disabled { opacity: .4; cursor: default; }
.mini-button { width: 34px; height: 34px; border: 0; border-radius: 10px; background: var(--panel-2); font-size: 1.05rem; }
.link-button { border: 0; background: none; padding: 0; color: var(--muted); text-decoration: underline; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; }
.hint { margin: 0; color: var(--muted); font-size: .9rem; }
.badge {
  position: absolute; top: -4px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 10px; background: var(--coral); color: #fff; font-size: .75rem; font-weight: 700; line-height: 20px;
}

/* ---------- home ---------- */
.home { max-width: 1100px; margin: 0 auto; padding: max(16px, env(safe-area-inset-top)) 16px 32px; }
.home-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.home-head > div { flex: 1; min-width: 180px; }
.home-logo { width: 64px; height: 64px; }
.home-head h1 { font-size: clamp(1.6rem, 4vw, 2.4rem); }
.home-note { color: var(--muted); max-width: 70ch; }
.resume {
  display: flex; align-items: center; gap: 16px; width: 100%; margin: 6px 0 18px; padding: 12px;
  border: 0; border-radius: var(--radius); background: var(--accent); color: var(--accent-text); text-align: left;
  font-size: 1.2rem; font-weight: 700;
}
.resume img { width: 120px; aspect-ratio: 16/9; object-fit: cover; border-radius: 10px; background: #0003; }
.film-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 16px; }
.film-card {
  position: relative; display: flex; flex-direction: column; gap: 6px; padding: 10px;
  border: 0; border-radius: var(--radius); background: var(--panel); text-align: left;
}
.film-card img, .film-card .no-cover {
  width: 100%; aspect-ratio: 16/9; object-fit: cover; border-radius: 10px; background: #0004;
  display: grid; place-items: center; font-size: 2rem;
}
.film-card strong { font-size: 1.05rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.film-card small { color: var(--muted); }
.film-card .card-menu { position: absolute; top: 14px; right: 14px; display: flex; gap: 6px; }
.film-card .card-menu button { width: 36px; height: 36px; border: 0; border-radius: 10px; background: #000a; }
.new-film { align-items: center; justify-content: center; min-height: 160px; border: 3px dashed var(--panel-2); background: transparent; }
.big-plus { font-size: 3rem; color: var(--accent); line-height: 1; }

/* ---------- editor ---------- */
.editor {
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh; height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px; background: var(--panel); overflow-x: auto; scrollbar-width: none; }
.film-name {
  min-width: 90px; width: 14em; max-width: 30vw; padding: .45em .7em;
  border: 2px solid transparent; border-radius: 10px; background: #0003; font-weight: 700;
}
.film-name:focus { border-color: var(--accent); outline: none; }
.save-status { font-size: .8rem; color: var(--muted); white-space: nowrap; }
.save-status.saving { color: var(--accent); }
.save-status.offline { color: var(--coral); }
.spacer { flex: 1; }
.speed { display: flex; align-items: center; gap: 4px; padding: 0 2px; white-space: nowrap; }
.speed small { color: var(--muted); }
.icon-button.play { background: var(--teal); }

.workspace { display: grid; grid-template-columns: auto minmax(0, 1fr) 270px; min-height: 0; }
.tools { display: flex; flex-direction: column; gap: 6px; padding: 8px; background: var(--panel); overflow-y: auto; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 2px; width: 66px; padding: 8px 4px;
  border: 0; border-radius: 12px; background: transparent; font-size: 1.6rem;
}
.tool span { font-size: .72rem; font-weight: 700; color: var(--muted); }
.tool.active { background: var(--accent); }
.tool.active span { color: var(--accent-text); }

.stage { position: relative; display: grid; place-items: center; min-width: 0; min-height: 0; padding: 8px; background: #0d0e1f; overflow: hidden; }
#view { display: block; background: #000; border-radius: 6px; touch-action: none; box-shadow: 0 6px 30px #0008; }
.stage.tool-pen #view, .stage.tool-text #view, .stage.tool-sticker #view { cursor: crosshair; }
.stage.tool-move #view { cursor: grab; }
.stage.tool-eraser #view { cursor: cell; }
.countdown {
  position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none;
  font-size: min(30vw, 30vh); font-weight: 900; color: #fff; text-shadow: 0 6px 30px #000;
}
/* The camera is drawn onto the canvas; the video stays in the page (not display:none) so iOS keeps it running. */
.camera-feed { position: absolute; width: 2px; height: 2px; opacity: 0; pointer-events: none; }
.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.go { animation: flash .35s ease-out; }
@keyframes flash { from { opacity: .85; } to { opacity: 0; } }
.stage-message {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); max-width: 80%; margin: 0; padding: 14px 18px;
  border-radius: var(--radius); background: #000c; text-align: center; pointer-events: none;
}

.side { display: flex; flex-direction: column; gap: 10px; padding: 10px; background: var(--panel); overflow-y: auto; }
.panel-section { display: flex; flex-direction: column; gap: 10px; }
.panel-section h2 { font-size: 1.2rem; }
.panel-section h3 { font-size: .95rem; color: var(--muted); }
.panel-section .chip-button { min-height: 40px; padding: .35em .75em; font-size: .92rem; }
.capture {
  align-self: center; width: 104px; height: 104px; padding: 10px; border: 6px solid #fff; border-radius: 50%; background: transparent;
}
.capture span { display: block; width: 100%; height: 100%; border-radius: 50%; background: var(--coral); transition: transform .1s; }
.capture:active span { transform: scale(.85); }
.capture:disabled { opacity: .4; }
#liveHint { text-align: center; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch { width: 36px; height: 36px; border: 3px solid #0006; border-radius: 50%; }
.swatch.active { border-color: #fff; box-shadow: 0 0 0 3px var(--accent); }
.size-button { width: 48px; height: 44px; border: 0; border-radius: 10px; background: var(--panel-2); display: grid; place-items: center; }
.size-button.active { background: var(--accent); }
.size-button i { display: block; border-radius: 50%; background: currentColor; }
.size-button.active i { background: var(--accent-text); }
.selection { padding: 10px; border-radius: var(--radius); background: var(--panel-2); }
.selection .chip-button { background: var(--panel); }
.selection .chip-button.danger { background: #5a2440; }
.range-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button { width: 36px; height: 36px; border: 0; border-radius: 10px; background: var(--panel); font-size: 1.2rem; font-weight: 700; }
.stepper b { min-width: 3.2em; text-align: center; }

.timeline {
  display: flex; gap: 6px; padding: 8px; background: var(--panel); overflow-x: auto; overflow-y: hidden;
  scroll-behavior: smooth; min-height: 96px;
}
.tile {
  position: relative; flex: none; display: grid; place-items: center; width: 112px; padding: 0;
  border: 3px solid transparent; border-radius: 10px; background: #0005; overflow: hidden;
}
.tile canvas { display: block; width: 100%; height: auto; }
.tile .num { position: absolute; left: 4px; bottom: 3px; padding: 0 5px; border-radius: 6px; background: #000a; font-size: .75rem; font-weight: 700; }
.tile.current { border-color: var(--accent); }
.tile.in-range::after { content: ""; position: absolute; left: 0; right: 0; top: 0; height: 6px; background: var(--teal); }
.tile.camera-tile, .tile.add-tile { font-size: 1.8rem; aspect-ratio: 16/9; background: var(--panel-2); }
.tile.camera-tile.current { background: var(--coral); }

/* Phones held upright: everything stacks */
@media (orientation: portrait) and (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr) auto; }
  .tools { flex-direction: row; overflow-x: auto; justify-content: space-between; }
  .tool { width: auto; flex: 1; min-width: 54px; font-size: 1.35rem; }
  .side { max-height: 36vh; }
  .capture { width: 84px; height: 84px; }
  .film-name { flex: 1; width: auto; max-width: none; }
  .topbar { flex-wrap: wrap; justify-content: center; }
  .spacer { display: none; }
}
/* Short landscape phones */
@media (max-height: 520px) {
  .tool { width: 56px; padding: 5px 2px; font-size: 1.3rem; }
  .tool span { display: none; }
  .timeline { min-height: 72px; padding: 5px 8px; }
  .tile { width: 84px; }
  .workspace { grid-template-columns: auto minmax(0, 1fr) 230px; }
  .capture { width: 80px; height: 80px; }
  .topbar { padding: 5px 8px; }
}

/* ---------- dialogs ---------- */
.dialog {
  width: min(460px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); padding: 0;
  border: 0; border-radius: 20px; background: var(--panel); color: var(--text); box-shadow: 0 20px 60px #000a;
}
.dialog.wide { width: min(720px, calc(100vw - 32px)); }
.dialog::backdrop { background: #0009; }
.dialog-body { display: flex; flex-direction: column; gap: 14px; padding: 20px; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; }
.dialog textarea, .dialog input:not([type]) {
  width: 100%; padding: .6em .8em; border: 2px solid var(--panel-2); border-radius: 12px; background: #0003; font-size: 1.3rem;
}
.dialog textarea:focus, .dialog input:focus { border-color: var(--accent); outline: none; }
.sticker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(54px, 1fr)); gap: 6px; }
.sticker-grid button { height: 54px; border: 0; border-radius: 12px; background: var(--panel-2); font-size: 1.9rem; }
.link-part { display: flex; flex-direction: column; gap: 8px; padding: 12px; border-radius: var(--radius); background: var(--panel-2); }
.link-part p { margin: 0; color: var(--muted); }
.link-code { font-size: 2.6rem; font-weight: 900; letter-spacing: .18em; text-align: center; color: var(--accent) !important; }
.code-entry { display: flex; gap: 8px; }
.code-entry input { text-transform: uppercase; letter-spacing: .15em; font-weight: 700; }
.check { display: flex; align-items: center; gap: 10px; }
.check input { width: 22px; height: 22px; }
progress { width: 100%; height: 18px; accent-color: var(--accent); }
#videoResult, .video-item video { width: 100%; max-height: 50vh; border-radius: 12px; background: #000; }
.video-list { display: grid; gap: 14px; }
.video-item { display: flex; flex-direction: column; gap: 8px; padding: 10px; border-radius: var(--radius); background: var(--panel-2); }
.video-item .button-row { align-items: center; }
.video-item small { flex: 1; color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 10;
  max-width: calc(100vw - 32px); padding: 12px 18px; border-radius: 999px; background: #000d; font-weight: 700; text-align: center;
}
