:root {
  --bg: #0b0b0f;
  --panel: #15151c;
  --panel-2: #1d1d26;
  --frame: #000;
  --fg: #f4f4f6;
  --muted: rgba(244, 244, 246, 0.6);
  --faint: rgba(244, 244, 246, 0.38);
  --glass: rgba(10, 10, 14, 0.55);
  --glass-strong: rgba(14, 14, 20, 0.88);
  --line: rgba(255, 255, 255, 0.1);
  --line-strong: rgba(255, 255, 255, 0.18);
  /* accent: viewer credits, own card, submit button only */
  --accent: #ff7a45;
  --accent-soft: rgba(255, 122, 69, 0.14);
  /* yellow: world-state conflicts only */
  --warn: #f5c84b;
  --warn-soft: rgba(245, 200, 75, 0.12);
  /* red: failures only */
  --bad: #ff5c5c;
  --bad-soft: rgba(255, 92, 92, 0.12);
  --radius: 12px;
  --tb-h: 52px;
  --compose-h: 64px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--fg); }
body {
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: 0.45; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------------ top bar */

#topbar {
  position: sticky; top: 0; z-index: 8;
  height: var(--tb-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 16px;
  background: rgba(11, 11, 15, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.story {
  flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-weight: 600; font-size: 15px;
  transition: color 600ms ease;
}
.story.changed { color: #fff; text-shadow: 0 0 12px rgba(255, 255, 255, 0.35); }
.tb-btns { flex: none; display: flex; gap: 6px; }
.pill {
  font-size: 13px; padding: 5px 12px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: var(--panel);
  white-space: nowrap;
}
.pill:hover { background: var(--panel-2); }
.pill.name { max-width: 120px; overflow: hidden; text-overflow: ellipsis; }

/* ------------------------------------------------------------------ layout */

#layout { display: block; padding-bottom: calc(var(--compose-h) + env(safe-area-inset-bottom)); }
#playerCol { display: flex; justify-content: center; }

/* 9:16 player: phone width 100%, height ≤ 62vh (shrinks width, centred) */
#stage {
  position: relative;
  width: min(100vw, calc(62vh * 9 / 16));
  aspect-ratio: 9 / 16;
  background: var(--frame);
  overflow: hidden;
}

#side { padding: 12px 16px 16px; }

@media (min-width: 900px) {
  html, body { height: 100%; }
  body { height: 100vh; height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }
  #topbar { flex: none; position: relative; padding: 0 24px; }
  #layout {
    flex: 1; min-height: 0;
    display: flex; justify-content: center; gap: 16px;
    padding: 16px;
  }
  #playerCol { flex: none; min-width: 0; }
  #stage {
    /* height = viewport − top bar − 32px, unless the column is too narrow for 9:16 at that height */
    width: auto;
    height: min(calc(100vh - var(--tb-h) - 32px), calc((100vw - 360px - 48px) * 16 / 9));
    height: min(calc(100dvh - var(--tb-h) - 32px), calc((100vw - 360px - 48px) * 16 / 9));
    border-radius: 14px;
    box-shadow: 0 0 0 1px var(--line), 0 24px 70px rgba(0, 0, 0, 0.55);
  }
  #side {
    flex: none; width: 360px; min-height: 0;
    display: flex; flex-direction: column; gap: 12px;
    padding: 0;
  }
  #queue {
    flex: 1; min-height: 0; overflow-y: auto;
    background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
    padding: 12px;
  }
  #compose { flex: none; }
}

/* ------------------------------------------------------------------ player */

video {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; background: var(--frame);
  opacity: 0; transition: opacity 120ms linear;
}
video.active { opacity: 1; }

.ov-top {
  position: absolute; top: 10px; right: 10px; left: 10px;
  display: flex; flex-direction: column; align-items: flex-end; gap: 6px;
  pointer-events: none;
}
.badge-row { display: flex; align-items: center; gap: 6px; max-width: 100%; pointer-events: auto; }
.badge {
  min-width: 0; max-width: 100%;
  font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px;
  background: var(--glass); border: 1px solid var(--line-strong);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}
.badge .live-dot { display: inline-block; width: 6px; height: 6px; margin-right: 6px; border-radius: 50%; background: #fff; vertical-align: 2px; }
.badge.user .live-dot, .badge.live .live-dot { animation: pulse 1.6s ease-in-out infinite; }
.badge .who { color: var(--accent); }
.badge.loop, .badge.vod { color: var(--fg); background: var(--glass-strong); }
@keyframes pulse { 50% { opacity: 0.3; } }
.back-live {
  flex: none; font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: var(--fg); color: var(--bg);
}
.credit-line {
  max-width: 100%;
  font-size: 12px; padding: 3px 9px; border-radius: 8px;
  background: var(--glass); border: 1px solid var(--line);
  text-align: right; overflow: hidden; text-overflow: ellipsis;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}
.credit-line .who { color: var(--accent); font-weight: 600; }
.flag-line {
  max-width: 100%;
  font-size: 12px; padding: 3px 9px; border-radius: 8px;
  color: var(--warn); background: rgba(20, 16, 4, 0.72); border: 1px solid rgba(245, 200, 75, 0.45);
  text-align: right;
}

.status, .flash {
  position: absolute; left: 50%; top: 45%; transform: translate(-50%, -50%);
  max-width: 86%;
  padding: 8px 14px; border-radius: 999px; background: var(--glass-strong); border: 1px solid var(--line);
  font-size: 13px; pointer-events: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.flash { top: 30%; font-weight: 600; font-size: 14px; border-color: var(--accent); }
.sound-hint {
  position: absolute; left: 50%; top: 56%; transform: translate(-50%, -50%);
  padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--glass-strong); font-size: 13px;
  white-space: nowrap;
}

.subtitle-wrap {
  position: absolute; left: 10px; right: 68px; bottom: 14px;
  text-align: center; pointer-events: none;
}
.subtitle {
  font-size: 15px; font-weight: 600; line-height: 1.75;
  padding: 3px 8px; border-radius: 6px;
  background: rgba(0, 0, 0, 0.62);
  -webkit-box-decoration-break: clone; box-decoration-break: clone;
}

.rating {
  position: absolute; right: 10px; bottom: 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.rating button {
  min-width: 48px; padding: 5px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 600;
  background: var(--glass); border: 1px solid var(--line-strong);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.rating button:hover { background: var(--glass-strong); }
.rating button.on { background: var(--fg); color: var(--bg); border-color: var(--fg); }

/* ------------------------------------------------------------------ queue */

.q-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; font-weight: 600; }
.link { background: none; border: 0; padding: 0; color: var(--muted); font-size: 13px; text-decoration: underline; text-underline-offset: 3px; }
.cards { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.q-card {
  padding: 9px 11px; border-radius: var(--radius);
  background: var(--panel-2); border: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 3px;
}
.q-top { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.q-status {
  flex: none; font-weight: 600; padding: 1px 7px; border-radius: 6px;
  background: rgba(255, 255, 255, 0.08); color: var(--muted);
}
.q-eta { flex: 1; min-width: 0; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.q-mine { flex: none; font-size: 11px; font-weight: 700; color: var(--accent); }
.q-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.q-names { font-size: 12px; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.q-flags { font-size: 12px; color: var(--warn); }
.q-fail { font-size: 12px; color: var(--bad); font-weight: 600; }
.q-card.st-playing { border-color: var(--line-strong); background: #23232e; }
.q-card.st-playing .q-status { background: var(--fg); color: var(--bg); }
.q-card.st-ready .q-status { color: var(--fg); background: rgba(255, 255, 255, 0.14); }
.q-card.st-failed { border-color: rgba(255, 92, 92, 0.6); background: var(--bad-soft); }
.q-card.st-failed .q-status { color: var(--bad); background: rgba(255, 92, 92, 0.16); }
.q-card.mine { border-color: var(--accent); background: linear-gradient(0deg, var(--accent-soft), var(--accent-soft)), var(--panel-2); }
.q-card.mine.st-failed { border-color: var(--bad); }
.q-empty { color: var(--faint); font-size: 13px; padding: 10px 2px; }

@media (max-width: 899px) {
  #queue:not(.expanded) .q-card.extra { display: none; }
}
@media (min-width: 900px) {
  #queueMore { display: none !important; }
}

/* ------------------------------------------------------------------ compose + preview */

#compose {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 7;
  padding: 8px 12px max(8px, env(safe-area-inset-bottom));
  background: rgba(11, 11, 15, 0.94);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
@media (min-width: 900px) {
  #compose { position: static; padding: 0; background: none; border: 0; backdrop-filter: none; -webkit-backdrop-filter: none; }
}
.inputbar {
  display: flex; gap: 8px; align-items: center;
  padding: 6px 6px 6px 12px; border-radius: 14px;
  background: var(--panel); border: 1px solid var(--line-strong);
}
.inputbar:focus-within { border-color: rgba(255, 255, 255, 0.32); }
.inputbar input { flex: 1; min-width: 0; border: 0; outline: none; background: transparent; padding: 8px 0; font-size: 16px; }
.char-count { flex: none; font-size: 11px; color: var(--faint); font-variant-numeric: tabular-nums; }
.char-count.full { color: var(--fg); }
.send { flex: none; border: 0; border-radius: 10px; padding: 8px 14px; background: var(--accent); color: #fff; font-weight: 600; }

.preview {
  display: flex; flex-direction: column; gap: 6px;
  padding: 12px; border-radius: 14px;
  background: var(--panel); border: 1px solid var(--line-strong);
}
.pv-state { font-size: 12px; color: var(--muted); font-weight: 600; }
.pv-state.busy::after { content: ""; display: inline-block; width: 10px; height: 10px; margin-left: 6px; vertical-align: -1px; border: 2px solid var(--muted); border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.pv-title { font-size: 16px; font-weight: 700; }
.pv-text { font-size: 14px; }
.pv-text.muted { color: var(--muted); }
.pv-flags { font-size: 12px; color: var(--warn); background: var(--warn-soft); border: 1px solid rgba(245, 200, 75, 0.35); border-radius: 8px; padding: 5px 8px; }
.pv-actions { display: flex; gap: 8px; margin-top: 4px; }
.pv-actions button { flex: 1; }

.primary { border: 0; border-radius: 10px; padding: 9px 14px; background: var(--fg); color: var(--bg); font-weight: 600; }
.primary.accent { background: var(--accent); color: #fff; }
.ghost { border: 1px solid var(--line-strong); border-radius: 10px; padding: 9px 12px; background: transparent; white-space: nowrap; }
.ghost:hover:not(:disabled) { background: var(--panel-2); }

/* ------------------------------------------------------------------ toast, modal, sheets */

.toast {
  position: fixed; left: 50%; bottom: calc(var(--compose-h) + 24px); transform: translateX(-50%); z-index: 30;
  max-width: min(86vw, 420px); padding: 9px 16px; border-radius: 10px;
  background: var(--glass-strong); border: 1px solid var(--line-strong);
  font-size: 13px; text-align: center; pointer-events: none;
}
body.previewing .toast { bottom: 200px; }

.modal, .sheet { position: fixed; inset: 0; z-index: 20; display: flex; background: rgba(0, 0, 0, 0.6); }
.modal { align-items: center; justify-content: center; padding: 16px; }
.modal-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 10px; padding: 20px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line-strong); }
.modal-card h2 { margin: 0; font-size: 18px; }
.modal-card input { padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line-strong); background: rgba(255, 255, 255, 0.05); outline: none; font-size: 16px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions .primary { flex: 1; }
.muted { color: var(--muted); margin: 0; font-size: 13px; }

.sheet { align-items: flex-end; justify-content: center; }
.sheet-card {
  width: 100%; max-height: 82vh; overflow-y: auto;
  padding: 14px 16px max(16px, env(safe-area-inset-bottom));
  border-radius: 18px 18px 0 0; background: var(--panel); border-top: 1px solid var(--line-strong);
}
@media (min-width: 900px) {
  .sheet { align-items: center; padding: 24px; }
  .sheet-card { max-width: 820px; max-height: 84vh; border-radius: 18px; border: 1px solid var(--line-strong); padding: 16px 20px 20px; }
}
.sheet-head { position: sticky; top: -14px; z-index: 1; display: flex; align-items: center; justify-content: space-between; margin: -14px -16px 8px; padding: 14px 16px 8px; background: var(--panel); }
.sheet-head h2 { margin: 0; font-size: 17px; }
.close { background: none; border: 0; padding: 0 4px; font-size: 24px; line-height: 1; color: var(--muted); }

/* cast */
.cast-meta { display: flex; flex-direction: column; gap: 2px; margin-bottom: 12px; padding: 8px 10px; border-radius: 10px; background: var(--panel-2); }
.cast-meta:empty { display: none; }
.cast-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
@media (min-width: 640px) { .cast-list { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
.cast-card { padding: 12px; border-radius: 14px; background: var(--panel-2); border: 1px solid var(--line); display: flex; flex-direction: column; gap: 4px; }
.cast-card.dead { opacity: 0.55; }
.cast-head { display: flex; gap: 10px; align-items: center; margin-bottom: 4px; }
.avatar { flex: none; width: 52px; height: 52px; border-radius: 50%; object-fit: cover; background: #333; }
.avatar.letter { display: grid; place-items: center; font-size: 22px; font-weight: 700; color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4); }
.cast-who { min-width: 0; }
.cast-name { font-size: 16px; font-weight: 700; display: flex; align-items: center; gap: 6px; }
.tag { font-size: 11px; font-weight: 600; padding: 0 6px; border-radius: 6px; border: 1px solid var(--line-strong); color: var(--muted); }
.cast-pers { font-size: 12px; color: var(--muted); }
.cast-row { display: flex; gap: 10px; font-size: 13px; padding: 2px 6px; margin: 0 -6px; border-radius: 6px; transition: background 400ms ease; }
.cast-k { flex: none; width: 2.6em; color: var(--faint); }
.cast-v { min-width: 0; overflow-wrap: anywhere; }
.cast-rels { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.rel { display: flex; gap: 8px; align-items: baseline; padding: 0 4px; margin: 0 -4px; border-radius: 6px; }
.rel-to { font-weight: 600; }
.rel-label { color: var(--fg); }
.rel-trust { margin-left: auto; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.secrets { margin: 0; padding-left: 1.1em; min-width: 0; }
.secrets li { overflow-wrap: anywhere; }
.cast-row.changed, .rel.changed { background: rgba(255, 255, 255, 0.13); }

/* episodes */
.ep-older { display: block; margin: 0 auto 8px; }
.ep-newest { margin: 10px auto 0; }
.ep-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
@media (min-width: 640px) { .ep-list { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
.ep-item { min-width: 0; }
.ep-btn {
  width: 100%; display: flex; gap: 10px; align-items: stretch; text-align: left;
  padding: 8px; border-radius: 12px; background: var(--panel-2); border: 1px solid var(--line);
}
.ep-btn:hover { border-color: var(--line-strong); background: #23232e; }
.ep-item.playing .ep-btn { border-color: var(--fg); }
.ep-thumb { flex: none; width: 54px; aspect-ratio: 9 / 16; border-radius: 6px; overflow: hidden; background: #000; }
.ep-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ep-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; justify-content: center; }
.ep-n { font-size: 12px; color: var(--muted); }
.ep-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.ep-credit { font-size: 12px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ep-credit.who { color: var(--accent); }
.ep-flag { font-size: 12px; color: var(--warn); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ------------------------------------------------------------------ narrow phones */

@media (max-width: 420px) {
  :root { --tb-h: 48px; }
  #topbar { padding: 0 10px; gap: 8px; }
  .story { font-size: 13px; }
  .pill { padding: 4px 9px; font-size: 12px; }
  .pill.name { max-width: 84px; }
}

/* ------------------------------------------------------------------ embed (director preview iframe) */

body.embed { height: 100vh; overflow: hidden; }
body.embed #topbar, body.embed #side, body.embed .rating, body.embed .sound-hint { display: none !important; }
body.embed #layout { padding: 0; display: block; height: 100vh; }
body.embed #playerCol { height: 100%; }
body.embed #stage { width: auto; height: 100vh; max-width: 100vw; border-radius: 0; box-shadow: none; }
