:root {
      color-scheme: dark;
      --page: #0d1215;
      --panel: #171f24;
      --panel-2: #202a30;
      --ink: #f4f7f8;
      --muted: #b5c0c6;
      --line: #3a464d;
      --accent: #ff5c2b;
      --accent-strong: #ff7c55;
      --accent-soft: #422319;
      --good: #55c78e;
      --danger: #ff8a80;
      --stage: #05080a;
      --stage-ui: rgba(12, 16, 18, .92);
      --focus: #39c7d8;
      --shadow: 0 20px 56px rgba(0, 0, 0, .38);
    }
    @media (prefers-color-scheme: light) {
      :root {
        color-scheme: light;
        --page: #20282d;
        --panel: #29343a;
        --panel-2: #333f46;
        --ink: #ffffff;
        --muted: #d1d9dd;
        --line: #526068;
        --accent: #ff6636;
        --accent-strong: #ff8967;
        --accent-soft: #4c2b20;
        --focus: #62d2df;
      }
    }
    * { box-sizing: border-box; }
    html, body { min-height: 100%; }
    body {
      margin: 0;
      background: var(--page);
      color: var(--ink);
      font-family: "IBM Sans", sans-serif;
      -webkit-font-smoothing: antialiased;
    }
    button, textarea, input, select { font: inherit; }
    button, input, select, textarea { color: inherit; }
    button { touch-action: manipulation; }
    :focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
    .app {
      width: min(1440px, 100%);
      min-height: 100vh;
      margin: 0 auto;
      display: grid;
      grid-template-columns: minmax(320px, 440px) minmax(0, 1fr);
      gap: 24px;
      padding: 24px;
    }
    .setup {
      background: var(--panel);
      border: 1px solid var(--line);
      box-shadow: var(--shadow);
      min-width: 0;
      align-self: start;
      overflow: hidden;
    }
    .setup-head { padding: 24px 24px 18px; border-bottom: 1px solid var(--line); }
    .setup-head h1 { margin: 0 0 5px; font-size: clamp(1.5rem, 3vw, 2rem); line-height: 1.1; letter-spacing: -.035em; }
    .setup-head p { margin: 0; color: var(--muted); font-size: .95rem; }
    .setup-body { padding: 20px 24px 24px; display: grid; gap: 22px; }
    .section-title { margin: 0 0 9px; font-size: .79rem; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
    .script-wrap { position: relative; }
    textarea {
      display: block;
      width: 100%;
      min-height: 230px;
      resize: vertical;
      border: 1px solid var(--line);
      background: var(--panel-2);
      border-radius: 6px;
      padding: 14px;
      line-height: 1.5;
      font-size: 1rem;
    }
    textarea::placeholder { color: var(--muted); opacity: .82; }
    .script-meta { display: flex; justify-content: space-between; gap: 12px; margin-top: 8px; color: var(--muted); font-size: .82rem; }
    .field { margin-bottom: 12px; }
    .field label { display: block; margin-bottom: 6px; }
    input[type="text"] {
      display: block;
      width: 100%;
      min-height: 42px;
      border: 1px solid var(--line);
      background: var(--panel-2);
      border-radius: 6px;
      padding: 9px 12px;
      line-height: 1.4;
    }
    input[type="text"]::placeholder { color: var(--muted); opacity: .82; }
    #qPoint { min-height: 96px; }
    .generate-status { min-height: 1.3em; margin: 10px 0 0; color: var(--muted); font-size: .85rem; }
    .control-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 14px; }
    .control { min-width: 0; }
    .control.wide { grid-column: 1 / -1; }
    .label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 7px; }
    label { font-size: .86rem; font-weight: 650; }
    output { font-family: "IBM Plex Mono", monospace; font-size: .78rem; color: var(--muted); }
    input[type="range"] { width: 100%; accent-color: var(--accent); }
    select {
      width: 100%;
      min-height: 42px;
      border: 1px solid var(--line);
      border-radius: 5px;
      background: var(--panel-2);
      padding: 0 36px 0 11px;
    }
    .toggle-row { display: flex; flex-wrap: wrap; gap: 9px; }
    .toggle {
      border: 1px solid var(--line);
      background: var(--panel-2);
      border-radius: 999px;
      padding: 9px 12px;
      font-size: .84rem;
      font-weight: 650;
      cursor: pointer;
    }
    .toggle[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-strong); }
    .setup-action { display: flex; justify-content: flex-end; }
    .setup-action .primary { width: 100%; }
    .privacy-note { display: flex; gap: 9px; color: var(--muted); font-size: .82rem; line-height: 1.45; }
    .privacy-note svg { flex: 0 0 auto; margin-top: 2px; }

    .app[data-view="setup"] { grid-template-columns: minmax(0, 640px); justify-content: center; }
    .app[data-view="setup"] .setup { width: min(640px, 100%); margin: auto; }
    .app[data-view="setup"] .workspace { display: none; }
    .app[data-view="prompter"] { grid-template-columns: 1fr; }
    .app[data-view="prompter"] .setup { display: none; }
    .app[data-view="prompter"] .workspace { min-height: calc(100vh - 48px); }
    .workspace { min-width: 0; display: grid; place-items: center; }
    .stage-shell { width: min(100%, 720px); }
    .stage {
      position: relative;
      width: min(100%, calc((100vh - 48px) * var(--recording-ratio, .5625)));
      aspect-ratio: var(--recording-aspect, 9 / 16);
      margin: auto;
      overflow: hidden;
      background: var(--stage);
      box-shadow: var(--shadow);
      color: white;
      isolation: isolate;
    }
    video.camera-source {
      position: absolute;
      width: 1px;
      height: 1px;
      opacity: 0;
      pointer-events: none;
    }
    canvas.camera {
      position: absolute;
      z-index: 0;
      inset: 0;
      width: 100%;
      height: 100%;
      background: #11191e;
      transform: translateZ(0);
      -webkit-transform: translateZ(0);
    }
    canvas.camera.mirrored {
      transform: translateZ(0) scaleX(-1);
      -webkit-transform: translateZ(0) scaleX(-1);
    }
    .camera-empty {
      position: absolute;
      z-index: 8;
      top: 58px;
      left: 50%;
      width: min(86%, 330px);
      transform: translateX(-50%);
      padding: 12px 14px;
      text-align: center;
      background: rgba(20, 28, 33, .92);
      border: 1px solid rgba(255,255,255,.2);
      border-radius: 8px;
      box-shadow: 0 8px 30px rgba(0,0,0,.45);
    }
    .camera-empty svg { display: none; }
    .camera-empty h2 { margin: 0 0 3px; font-size: 1rem; }
    .camera-empty p { margin: 0 auto 9px; max-width: 27rem; color: #c6d0d5; line-height: 1.35; font-size: .78rem; }
    .camera-empty .primary { padding: 9px 13px; }
    .primary, .secondary, .icon-button {
      border: 0;
      cursor: pointer;
      font-weight: 700;
    }
    .primary { background: var(--accent); color: #fff; padding: 12px 17px; border-radius: 5px; }
    .primary:hover { background: var(--accent-strong); }
    .secondary { background: #263139; color: white; padding: 11px 14px; border-radius: 5px; }
    .primary:disabled, .secondary:disabled, .icon-button:disabled { opacity: .42; cursor: not-allowed; }
    .topline {
      position: absolute;
      z-index: 5;
      top: 0;
      left: 0;
      right: 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: max(13px, env(safe-area-inset-top)) 14px 12px;
      background: linear-gradient(to bottom, rgba(0,0,0,.72), transparent);
      pointer-events: none;
    }
    .status { display: inline-flex; align-items: center; gap: 8px; font-family: "IBM Plex Mono", monospace; font-size: .76rem; font-weight: 600; }
    .dot { width: 8px; height: 8px; border-radius: 50%; background: #7d8a91; }
    .status.live .dot { background: #ff3b30; box-shadow: 0 0 0 5px rgba(255, 59, 48, .18); }
    .status.paused .dot { background: #ffb340; box-shadow: 0 0 0 5px rgba(255, 179, 64, .18); }
    .time { font-family: "IBM Plex Mono", monospace; font-size: .8rem; font-weight: 600; font-variant-numeric: tabular-nums; }
    .guide {
      position: absolute;
      z-index: 6;
      left: 8%;
      right: 8%;
      top: 25%;
      bottom: 18%;
      pointer-events: auto;
      cursor: pointer;
      overflow: hidden;
      transform: translateZ(1px);
      -webkit-transform: translateZ(1px);
      -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 12%, #000 82%, transparent 100%);
      mask-image: linear-gradient(to bottom, transparent 0, #000 12%, #000 82%, transparent 100%);
    }
    .guide:before {
      content: "";
      position: absolute;
      z-index: 1;
      left: 0;
      right: 0;
      top: 42%;
      border-top: 2px solid rgba(255, 112, 64, .78);
      pointer-events: none;
    }
    .prompt-scroll {
      position: relative;
      z-index: 2;
      height: 100%;
      overflow: hidden;
      padding: 0;
      scrollbar-width: none;
      transform: translateZ(0);
      -webkit-transform: translateZ(0);
    }
    .prompt-scroll::-webkit-scrollbar { display: none; }
    .prompt-track {
      position: relative;
      z-index: 2;
      min-height: 100%;
      will-change: transform;
      transform: translate3d(0, 0, 0);
      -webkit-transform: translate3d(0, 0, 0);
    }
    .prompt-copy {
      position: relative;
      margin: 0 auto;
      width: 92%;
      color: #fff;
      font-weight: 700;
      font-size: 42px;
      line-height: 1.35;
      text-align: center;
      white-space: pre-wrap;
      text-wrap: balance;
      text-shadow: 0 2px 6px rgba(0,0,0,.98), 0 0 22px rgba(0,0,0,.88);
      transform: translateZ(0);
      -webkit-transform: translateZ(0);
    }
    .guide.backplate {
      background: rgba(0, 0, 0, .55);
      border-radius: 8px;
    }
    .guide.flipped .prompt-copy { transform: scaleX(-1); }
    .countdown {
      position: absolute;
      z-index: 10;
      inset: 0;
      display: none;
      place-items: center;
      background: rgba(0, 0, 0, .45);
      font-family: "IBM Plex Mono", monospace;
      font-size: clamp(7rem, 28vw, 13rem);
      font-weight: 600;
      text-shadow: 0 6px 28px rgba(0,0,0,.65);
    }
    .countdown.show { display: grid; }
    .stage-controls {
      position: absolute;
      z-index: 7;
      left: 12px;
      right: 12px;
      bottom: max(12px, env(safe-area-inset-bottom));
      display: grid;
      grid-template-columns: 44px 1fr 44px;
      align-items: center;
      gap: 10px;
      background: var(--stage-ui);
      border: 1px solid rgba(255,255,255,.14);
      border-radius: 10px;
      padding: 9px;
      backdrop-filter: blur(12px);
    }
    .icon-button {
      min-width: 44px;
      min-height: 44px;
      display: inline-grid;
      place-items: center;
      background: transparent;
      color: white;
      border-radius: 6px;
    }
    .icon-button:hover { background: rgba(255,255,255,.12); }
    .transport { display: flex; justify-content: center; align-items: center; gap: 12px; }
    .record {
      position: relative;
      width: 54px;
      height: 54px;
      border-radius: 50%;
      border: 3px solid white;
      background: transparent;
      cursor: pointer;
    }
    .record:before { content: ""; position: absolute; inset: 5px; border-radius: 50%; background: #ff3b30; transition: .18s ease; }
    .record.recording:before { inset: 13px; border-radius: 4px; }
    .play-scroll, .pause-record {
      min-width: 46px;
      min-height: 46px;
      display: grid;
      place-items: center;
      border: 0;
      border-radius: 50%;
      cursor: pointer;
    }
    .play-scroll { background: #fff; color: #111; }
    .pause-record { background: #263139; color: #fff; border: 1px solid rgba(255,255,255,.24); }
    .pause-record:not(:disabled):hover { background: #34414a; }
    .pause-record:disabled { opacity: .36; cursor: not-allowed; }
    .pause-record.paused { background: #ffb340; color: #17120a; border-color: #ffb340; }
    .hint { margin: 10px 4px 0; text-align: center; color: var(--muted); font-size: .82rem; }
    .prompter-tools { display: flex; flex-wrap: wrap; align-items: end; justify-content: center; gap: 10px; margin-top: 12px; }
    .prompter-tools .secondary { border: 1px solid var(--line); }
    .camera-tool { width: min(260px, 100%); }
    .camera-tool label, .orientation-tool > span { display: block; margin-bottom: 6px; color: var(--muted); font-size: .78rem; font-weight: 650; }
    .camera-tool select { min-height: 41px; }
    .orientation-tool { min-width: 220px; }
    .orientation-switch {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 3px;
      padding: 3px;
      border: 1px solid var(--line);
      border-radius: 6px;
      background: var(--panel);
    }
    .orientation-switch button {
      min-height: 35px;
      border: 0;
      border-radius: 4px;
      background: transparent;
      color: var(--muted);
      font-weight: 700;
      cursor: pointer;
    }
    .orientation-switch button[aria-pressed="true"] { background: var(--accent); color: #fff; }
    .error {
      display: none;
      margin: 14px 0 0;
      padding: 11px 12px;
      border: 1px solid color-mix(in srgb, var(--danger) 40%, var(--line));
      background: color-mix(in srgb, var(--danger) 10%, var(--panel));
      color: var(--danger);
      border-radius: 5px;
      font-size: .86rem;
      line-height: 1.4;
    }
    .error.show { display: block; }
    dialog {
      width: min(560px, calc(100% - 28px));
      border: 1px solid var(--line);
      border-radius: 8px;
      padding: 0;
      color: var(--ink);
      background: var(--panel);
      box-shadow: 0 28px 90px rgba(0,0,0,.42);
    }
    dialog::backdrop { background: rgba(0,0,0,.72); }
    .review { padding: 22px; }
    .review h2 { margin: 0 0 6px; font-size: 1.4rem; }
    .review p { margin: 0 0 16px; color: var(--muted); }
    .review video { display: block; width: 100%; max-height: 58vh; background: black; border-radius: 5px; }
    .review-actions { display: flex; flex-wrap: wrap; gap: 9px; margin-top: 16px; }
    .review-actions .primary, .review-actions .secondary { flex: 1 1 150px; text-align: center; text-decoration: none; }
    .review .secondary { background: var(--panel-2); color: var(--ink); border: 1px solid var(--line); }
    .download-status { min-height: 1.3em; margin: 10px 0 0 !important; font-size: .82rem; }

    /* ---- Paywall: script saving ---- */
    .script-actions { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
    .save-status { min-height: 1.2em; font-size: .82rem; color: var(--muted); }
    .library { margin-top: 14px; }
    .library-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
    .library-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 5px; background: var(--panel-2); }
    .library-load { flex: 1; background: none; border: 0; padding: 2px 0; color: var(--ink); font: inherit; font-weight: 600; text-align: left; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .library-load:hover { color: var(--accent); }
    .library-delete { background: none; border: 0; color: var(--danger); font: inherit; font-size: .82rem; cursor: pointer; padding: 4px 6px; }
    .library-delete:hover { text-decoration: underline; }
    .library-empty { color: var(--muted); font-size: .86rem; padding: 6px 2px; }
    .paywall { padding: 22px; }
    .paywall h2 { margin: 0 0 6px; font-size: 1.4rem; }
    .paywall p { margin: 0 0 14px; color: var(--muted); }
    .paywall .price { color: var(--ink); font-size: 1.02rem; }
    .paywall .price strong { font-size: 1.25rem; }
    .paywall .primary { width: 100%; }
    .paywall-status { min-height: 1.3em; font-size: .84rem; }
    .code-row { margin: 16px 0; padding-top: 16px; border-top: 1px solid var(--line); }
    .code-row label { display: block; font-size: .86rem; font-weight: 600; margin-bottom: 8px; }
    .code-input-row { display: flex; gap: 8px; }
    .code-input-row input { flex: 1; min-width: 0; }
    .text-button { display: block; width: 100%; margin-top: 6px; background: none; border: 0; color: var(--muted); font: inherit; font-size: .9rem; padding: 10px; cursor: pointer; }
    .text-button:hover { color: var(--ink); text-decoration: underline; }

    @media (max-width: 900px) {
      .app { display: flex; flex-direction: column; padding: 0; gap: 0; }
      .workspace { order: -1; display: block; }
      .stage-shell { width: 100%; }
      .stage { width: min(100%, calc(100svh * var(--recording-ratio, .5625))); box-shadow: none; }
      .setup { border-width: 1px 0 0; box-shadow: none; }
      .setup-head, .setup-body { padding-left: 18px; padding-right: 18px; }
      .hint { padding: 0 16px; margin-bottom: 12px; }
    }
    @media (max-width: 430px) {
      .control-grid { grid-template-columns: 1fr; }
      .control.wide { grid-column: auto; }
      .prompt-copy { font-size: 38px; }
    }
    @media (orientation: landscape) and (max-height: 560px) {
      .app { display: grid; grid-template-columns: minmax(260px, 38vw) 1fr; padding: 10px; gap: 10px; }
      .workspace { order: initial; }
      .stage { width: min(100%, calc((100vh - 20px) * var(--recording-ratio, .5625))); height: auto; aspect-ratio: var(--recording-aspect, 9 / 16); }
      .setup { max-height: calc(100vh - 20px); overflow: auto; border-width: 1px; }
      .setup-head { padding: 16px; }
      .setup-body { padding: 16px; }
      textarea { min-height: 150px; }
    }
    @media (prefers-reduced-motion: reduce) {
      *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
    }
