:root {
  --bg: #12151c;
  --panel: #1c2230;
  --line: #2e3850;
  --text: #dfe6f2;
  --sub: #8b96ad;
  --accent: #4fa3ff;
  --warn: #ffb74d;
  --error: #ff8a80;
  --ok: #69db7c;
  --hud-h: 56px;
  --top-h: 48px;
  --bar: #3b4763;        /* スクロールバーのつまみ */
  --bar-hover: #4f5f85;
}
* { box-sizing: border-box; }

/* ---- スクロールバー(全体の配色に合わせる) ---- */
/* Firefox */
* { scrollbar-width: thin; scrollbar-color: var(--bar) transparent; }
/* Chrome / Edge / Safari。つまみは背景を透かした余白付きで細く見せる */
::-webkit-scrollbar { width: 11px; height: 11px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bar); background-clip: padding-box; border: 3px solid transparent; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--bar-hover); background-clip: padding-box; }
::-webkit-scrollbar-thumb:active { background: var(--accent); background-clip: padding-box; }
::-webkit-scrollbar-corner { background: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 14px;
  overflow: hidden;
}
button {
  background: var(--accent);
  color: #0b1220;
  border: none;
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
button.secondary { background: #37415a; color: var(--text); }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
button.danger { background: #5a3737; color: var(--text); }
button:disabled { opacity: 0.4; cursor: default; }
button.active { outline: 2px solid var(--accent); outline-offset: 1px; }
select, input[type="number"], input[type="text"] {
  background: #0b0e14;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 13px;
}
input[type="range"] { accent-color: var(--accent); }
label.check { display: inline-flex; align-items: center; gap: 6px; color: var(--sub); font-size: 13px; cursor: pointer; }
.hidden { display: none !important; }
.sub { color: var(--sub); }
.mono { font-variant-numeric: tabular-nums; font-family: Consolas, "Courier New", monospace; }
.error { color: var(--error); }
.warn { color: var(--warn); }
.ok { color: var(--ok); }

/* ---- 共通ヘッダー ---- */
.topbar {
  height: var(--top-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 15px; margin: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }
.topbar .status { font-size: 12px; color: var(--sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }

/* ---- ビュー切替 ---- */
.view { position: absolute; inset: var(--top-h) 0 0 0; display: flex; flex-direction: column; }

/* ---- カタログ ---- */
#catalogView { overflow: auto; padding: 16px; display: block; }
#catalogView .lead { color: var(--sub); margin: 0 0 14px; overflow-wrap: anywhere; }
.work { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; margin-bottom: 14px; min-width: 0; max-width: 100%; overflow: hidden; overflow-wrap: anywhere; }
.work h2 { font-size: 16px; margin: 0 0 4px; color: var(--accent); }
.work .credits { font-size: 12px; color: var(--sub); margin-bottom: 4px; }
.work .desc { font-size: 12px; color: var(--sub); margin: 0 0 10px; }
.work .episodes { margin-top: 8px; }
.episodes { display: flex; flex-wrap: wrap; gap: 12px; }
.episode {
  flex: 0 1 150px;
  width: 150px;
  min-width: 0;
  white-space: normal; /* button 既定の nowrap を戻し、カード内の文言を折り返す */
  overflow-wrap: anywhere;
  background: #0b0e14;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  cursor: pointer;
  text-align: left;
  color: var(--text);
  font-weight: 400;
}
.episode:hover { border-color: var(--accent); }
.episode img { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; border-radius: 4px; background: #000; display: block; }
.episode .t { margin-top: 6px; font-size: 13px; font-weight: 600; }
.episode .m { font-size: 11px; color: var(--sub); }
.episode .badge { display: inline-block; font-size: 10px; padding: 1px 6px; border-radius: 10px; background: var(--accent); color: #0b1220; margin-left: 4px; }

/* ---- ビューア ---- */
#viewerView { }
.stage {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #07090d;
  user-select: none;
  touch-action: pan-y;
}
.page {
  position: relative;
  height: 100%;
  max-width: 100%;
  aspect-ratio: 3 / 4;
  overflow: hidden;
  background: #000;
}
.page img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}
.overlay { position: absolute; inset: 0; }
.overlay.off { display: none; }
.mask {
  position: absolute;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.65);
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-radius: 2px;
  transition: left 250ms ease, top 250ms ease, width 250ms ease, height 250ms ease;
  pointer-events: none;
}
.mask .tag {
  position: absolute;
  left: 0;
  top: 0;
  transform: translateY(-100%);
  background: var(--accent);
  color: #0b1220;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 3px 3px 0 0;
  white-space: nowrap;
}
.mask.pulse { animation: pulse 400ms ease-out 1; }
@keyframes pulse { 0% { outline-color: #fff; outline-width: 4px; } 100% { outline-color: var(--accent); outline-width: 2px; } }
/* ---- 合図(作中の吹き出し風) ---- */
/* 形は cue.js が文字の大きさを測って SVG で描く。ここは文字・配置・出入りの動き */
.cue {
  position: absolute;
  transform: translate(calc(-50% + var(--dx, 0px)), calc(-50% + var(--dy, 0px))) scale(0.7) rotate(-4deg);
  opacity: 0;
  z-index: 5;
  pointer-events: none;
  font-family: "Yu Gothic Medium", "Yu Gothic", "YuGothic", "Hiragino Sans", "Noto Sans JP", "Meiryo", sans-serif;
  font-weight: 700;
  color: #111;
  filter: drop-shadow(0 3px 3px rgba(0, 0, 0, 0.45));
  white-space: nowrap;
  width: max-content;
  transition: transform 260ms cubic-bezier(0.2, 1.5, 0.4, 1), opacity 160ms ease;
}
.cue.in { opacity: 1; transform: translate(calc(-50% + var(--dx, 0px)), calc(-50% + var(--dy, 0px))) scale(1) rotate(0deg); }
.cue.in .cue-body { animation: cue-float 2.4s ease-in-out 300ms infinite; }
.cue.in .cue-shape { animation: cue-float 2.4s ease-in-out 300ms infinite; }
.cue.out { opacity: 0; transform: translate(calc(-50% + var(--dx, 0px)), calc(-50% + var(--dy, 0px))) scale(0.92); }
@keyframes cue-float { 0%, 100% { translate: 0 0; } 50% { translate: 0 -2px; } }
.cue-shape { position: absolute; overflow: visible; pointer-events: none; }
.cue-shape .shape-fill { fill: #fff; }
.cue-shape .shape-stroke { fill: none; stroke: #111; stroke-width: 2.6; stroke-linejoin: round; stroke-linecap: round; }
.cue-shape .shape-fill.shape-stroke { fill: #fff; }
.cue-shape .shape-stroke.thin { stroke-width: 1; opacity: 0.85; }
.cue-shape .burst { stroke-width: 3; stroke-linejoin: miter; }
.cue-body { position: relative; display: flex; align-items: center; justify-content: center; gap: 0.3em; padding: 0.55em 0.8em; font-size: var(--cue-fs, 18px); }
.cue-text { font-size: var(--cue-fs, 18px); letter-spacing: 0.06em; line-height: 1.3; white-space: pre; text-align: center; }
/* 吹き出し(縦書き) */
.cue-bubble .cue-body { flex-direction: column; padding: 0.8em 0.65em; }
.cue-bubble .cue-text { writing-mode: vertical-rl; text-orientation: upright; letter-spacing: 0.14em; line-height: 1.15; }
/* 叫び: 太い書き文字 */
.cue-burst .cue-body { padding: 0.9em 1.1em; }
.cue-burst .cue-text { font-weight: 900; letter-spacing: 0.1em; font-size: calc(var(--cue-fs, 18px) * 1.1); }
/* ナレーション枠 */
.cue-caption .cue-body { padding: 0.45em 0.8em; }
.cue-caption .cue-text { font-weight: 600; letter-spacing: 0.1em; }
/* 石の絵 */
.cue-icon { width: calc(var(--cue-fs, 18px) * 1.8); height: calc(var(--cue-fs, 18px) * 1.8); flex: 0 0 auto; overflow: visible; }
.cue-icon.icon-shake { animation: cue-shake 0.9s ease-in-out infinite; }
.cue-icon.icon-up { animation: cue-up 1.4s ease-in-out infinite; }
.cue-icon.icon-move { animation: cue-move 1.2s ease-in-out infinite; }
.cue-icon.icon-tilt { animation: cue-tilt 1.6s ease-in-out infinite; }
.cue-icon.icon-swing { animation: cue-swingicon 1.3s ease-in-out infinite; transform-origin: 50% 85%; }
.cue-icon.icon-brake { animation: cue-brakeicon 1.5s ease-in-out infinite; }
@keyframes cue-shake { 0%, 100% { transform: rotate(-6deg); } 50% { transform: rotate(6deg); } }
@keyframes cue-up { 0%, 100% { transform: translateY(2px); } 50% { transform: translateY(-3px); } }
@keyframes cue-move { 0%, 100% { transform: translateX(-2px); } 50% { transform: translateX(3px); } }
@keyframes cue-tilt { 0%, 100% { transform: rotate(0deg); } 50% { transform: rotate(14deg); } }
@keyframes cue-swingicon { 0%, 100% { transform: rotate(-13deg); } 50% { transform: rotate(13deg); } }
@keyframes cue-brakeicon { 0%, 100% { transform: translateX(2px); } 55% { transform: translateX(-3px); } }
/* エディタ: ドラッグ可 */
.cue.draggable { pointer-events: auto; cursor: grab; }
.cue.dragging { cursor: grabbing; }
.cue.dragging .cue-body, .cue.dragging .cue-shape { animation: none; }
.pageno {
  position: absolute;
  right: 10px;
  bottom: 8px;
  font-size: 12px;
  color: var(--sub);
  background: rgba(0, 0, 0, 0.5);
  padding: 2px 8px;
  border-radius: 10px;
  pointer-events: none;
}
.nav-hit { position: absolute; top: 0; bottom: 0; width: 50%; }
.nav-hit.left { left: 0; }
.nav-hit.right { right: 0; }
.loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--sub); pointer-events: none; }

/* ---- サムネイル ---- */
.thumbs {
  position: absolute;
  inset: 0;
  background: rgba(7, 9, 13, 0.96);
  overflow: auto;
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 10px;
  align-content: start;
}
.thumbs button { background: #0b0e14; border: 1px solid var(--line); padding: 4px; border-radius: 6px; color: var(--sub); font-weight: 400; }
.thumbs button.current { border-color: var(--accent); }
.thumbs img { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; display: block; background: #000; }

/* ---- HUD / 操作バー ---- */
.hud {
  min-height: var(--hud-h);
  background: var(--panel);
  border-top: 1px solid var(--line);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
}
.hud .group { display: inline-flex; align-items: center; gap: 6px; }
.hud .info { flex: 1 1 260px; min-width: 200px; font-size: 12px; line-height: 1.4; }
.hud .info b { color: var(--text); }
.hud .info .id { color: var(--accent); font-weight: 700; margin-right: 6px; }
.hud .info .step { color: var(--sub); }
.progress { height: 4px; background: #0b0e14; border-radius: 2px; overflow: hidden; margin-top: 3px; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width 100ms linear; }
.hud .state { font-size: 11px; padding: 2px 8px; border-radius: 10px; background: #0b0e14; color: var(--sub); }
.hud .state.playing { color: var(--ok); }
.hud .state.paused { color: var(--warn); }
.hud .state.waitingInteraction { color: var(--accent); }
.hud output { min-width: 3em; text-align: right; font-size: 12px; color: var(--sub); }

/* ---- 設定パネル / 警告パネル ---- */
.drawer {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: min(360px, 100%);
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow: auto;
  padding: 14px;
  z-index: 5;
}
.drawer h3 { margin: 0 0 10px; font-size: 14px; color: var(--accent); }
.drawer .row { display: flex; align-items: center; gap: 8px; margin: 8px 0; font-size: 13px; }
.drawer .row label { color: var(--sub); flex: 1; }
.drawer ul { margin: 6px 0; padding-left: 18px; font-size: 12px; }
.drawer li { margin: 3px 0; }
.drawer textarea { width: 100%; height: 160px; background: #0b0e14; color: var(--text); border: 1px solid var(--line); border-radius: 6px; font-size: 11px; }

/* ---- 編集モード ---- */
.overlay.edit .mask { display: none; }
.rect {
  position: absolute;
  border: 1.5px solid rgba(79, 163, 255, 0.85);
  background: rgba(79, 163, 255, 0.10);
  cursor: move;
}
.rect .lbl {
  position: absolute;
  left: 0;
  top: 0;
  font-size: 11px;
  font-weight: 700;
  background: rgba(79, 163, 255, 0.85);
  color: #0b1220;
  padding: 0 5px;
  pointer-events: none;
}
.rect.selected { border-color: #ffb74d; background: rgba(255, 183, 77, 0.14); z-index: 2; }
.rect.selected .lbl { background: #ffb74d; }
.rect .h { position: absolute; width: 12px; height: 12px; background: #ffb74d; border: 1px solid #0b1220; border-radius: 2px; display: none; }
.rect.selected .h { display: block; }
.h.nw { left: -6px; top: -6px; cursor: nwse-resize; }
.h.n  { left: calc(50% - 6px); top: -6px; cursor: ns-resize; }
.h.ne { right: -6px; top: -6px; cursor: nesw-resize; }
.h.e  { right: -6px; top: calc(50% - 6px); cursor: ew-resize; }
.h.se { right: -6px; bottom: -6px; cursor: nwse-resize; }
.h.s  { left: calc(50% - 6px); bottom: -6px; cursor: ns-resize; }
.h.sw { left: -6px; bottom: -6px; cursor: nesw-resize; }
.h.w  { left: -6px; top: calc(50% - 6px); cursor: ew-resize; }
.overlay.edit { cursor: crosshair; }
.editlist { list-style: none; padding: 0; margin: 0; font-size: 12px; }
.editlist li { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 4px; cursor: pointer; }
.editlist li.selected { background: #0b0e14; outline: 1px solid var(--warn); }
.editlist li .id { color: var(--accent); font-weight: 700; min-width: 3em; }
.editlist li .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.editlist li .todo { color: var(--warn); }
.editlist li button { padding: 1px 6px; font-size: 11px; }

@media (max-width: 600px) {
  .topbar h1 { font-size: 13px; }
  .hud .info { flex-basis: 100%; }
  .topbar .status { display: none; }
}

/* ---- 送信ログ ---- */
.loglist { list-style: none; margin: 0; padding: 0; font-size: 11px; font-family: Consolas, "Courier New", monospace; }
.loglist li { display: grid; grid-template-columns: 5.5em 3.5em 1fr; gap: 6px; padding: 2px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.loglist li .t { color: var(--sub); text-align: right; }
.loglist li .p { color: var(--accent); font-weight: 700; }
.loglist li.dev { color: var(--sub); }
.loglist li.se { color: var(--warn); }
.loglist li.ind { color: var(--ok); }

/* ---- 縦読み(webtoon) ---- */
.scroller {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
}
.strip { width: min(100%, 720px); margin: 0 auto; background: #000; }
.strip-item { position: relative; width: 100%; overflow: hidden; }
.strip-item .overlay { position: absolute; inset: 0; }
.strip-item .dimmer { position: absolute; inset: 0; pointer-events: none; }
.strip-item img { display: block; width: 100%; height: auto; }
