:root {
  --bg: #0b0b0f;
  --panel: #16161c;
  --panel2: #1e1e26;
  --panel3: #262632;
  --line: rgba(255, 255, 255, 0.1);
  --txt: #eceaf2;
  --dim: rgba(255, 255, 255, 0.55);
  --dimmer: rgba(255, 255, 255, 0.3);
  --accent: #d9a441;
  --danger: #e2637a;
  --mincho: "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", "Noto Serif JP", serif;
  --gothic: "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", sans-serif;
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  /* spacing (8dpグリッド。docs/design-system.md 参照。全アプリ共通スケール) */
  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px; --space-5: 20px; --space-6: 24px;
  /* shape */
  --radius-xs: 4px; --radius-sm: 8px; --radius-md: 12px; --radius-lg: 16px; --radius-full: 999px;
  /* state layer (hover/focus。pointer:fine環境向け) */
  --state-hover: color-mix(in srgb, var(--accent) 8%, transparent);
  --state-focus: var(--accent);
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--txt);
  font-family: var(--mincho);
  overflow: hidden;
}
body.font-gothic { font-family: var(--gothic); }

#app {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
}
@media (min-width: 760px) {
  #app { max-width: 640px; }
}

.toolbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.toolbar h1 { font-size: 13px; font-weight: 700; letter-spacing: 0.4px; }
.toolbar .spacer { flex: 1; }
.toolbar button {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--dim);
  font-size: 15px;
  width: 38px;
  height: 38px;
  cursor: pointer;
}
.toolbar button.on { color: var(--accent); border-color: var(--accent); }

.view { flex: 1; overflow-y: auto; padding: 16px; }
.view.hidden { display: none; }

textarea#ks-input {
  width: 100%;
  min-height: 40vh;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--txt);
  font-family: var(--mono);
  font-size: 12px;
  padding: 10px;
  resize: vertical;
}
.loader-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.loader-actions button, .loader-actions label {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--txt);
  padding: 9px 14px;
  font-size: 13px;
  cursor: pointer;
}
.loader-actions button.primary { background: var(--accent); color: #17140a; border-color: var(--accent); font-weight: 700; }
.loader-actions input[type="file"] { display: none; }

.save-row { display: flex; gap: 8px; margin-top: 14px; }
.save-row input[type="text"] {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--txt);
  padding: 9px 10px;
  font-size: 13px;
}
.save-row button {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--txt);
  padding: 9px 14px;
  font-size: 13px;
  cursor: pointer;
}

.sync-status { font-size: 12px; color: var(--dim); margin-top: 8px; min-height: 1.4em; white-space: pre-wrap; }
.sync-status.error { color: #ff6b6b; }

.saved-wrap { margin-top: 18px; }
.saved-header { font-size: 11px; color: var(--dim); margin-bottom: 8px; letter-spacing: 0.5px; }
.saved-list { display: flex; flex-direction: column; gap: 6px; }
.saved-list .empty { color: var(--dimmer); font-size: 12px; }
.asset-item {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--dim);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
}
.saved-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
}
.saved-item .info { flex: 1; min-width: 0; cursor: pointer; }
.saved-item .name { font-size: 13px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.saved-item .meta { font-family: var(--mono); font-size: 9px; color: var(--dimmer); margin-top: 2px; }
.saved-item button {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--dim);
  font-size: 11px;
  padding: 6px 8px;
  cursor: pointer;
}

.jump-row { display: flex; gap: 6px; }
.jump-row input[type="text"] {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--txt);
  padding: 6px 8px;
  font-size: 12px;
  font-family: var(--mono);
}
.jump-row button {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--txt);
  padding: 6px 10px;
  font-size: 11px;
  cursor: pointer;
}

#game-view { position: relative; display: flex; flex-direction: column; height: 100%; padding: 0; }
#game-view.hidden { display: none; }

.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  overflow: hidden;
  flex-shrink: 0;
}
.stage-cam {
  position: absolute;
  inset: 0;
  transform-origin: 50% 50%;
  transition: transform 0.6s ease;
}
.stage-bg {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 6px 8px;
  font-family: var(--mono);
  font-size: 10px;
  color: rgba(255, 255, 255, 0.55);
  transition: background 0.25s ease;
}
.stage-charas {
  position: absolute;
  inset: 0;
}
.chara-silhouette {
  position: absolute;
  bottom: 0;
  transform: translateX(-50%);
  width: 30%;
  max-width: 150px;
  aspect-ratio: 3 / 5;
  border-radius: 10px 10px 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  opacity: 0.92;
  transition: left 0.5s ease, background 0.2s ease;
}
.chara-name {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, -140%);
  font-size: 11px;
  color: #fff;
  background: rgba(0, 0, 0, 0.45);
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}
.chara-face { font-size: 18px; font-weight: 700; color: #fff; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5); }
.chara-img { display: none; width: 100%; height: 100%; object-fit: contain; object-position: bottom center; }
.chara-flash { animation: flash 0.35s ease; }
@keyframes flash {
  0% { filter: brightness(2.4); }
  100% { filter: brightness(1); }
}

.tap-layer { position: absolute; inset: 0; z-index: 1; }

.message-window {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 14px 16px 22px;
  border-top: 1px solid var(--line);
  background: linear-gradient(180deg, var(--panel), var(--bg));
}
.nameplate {
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
  min-height: 1em;
}
.message-text {
  flex: 1;
  font-size: 16px;
  line-height: 1.9;
  overflow-y: auto;
  white-space: pre-wrap;
}
.wait-indicator {
  align-self: flex-end;
  width: 9px;
  height: 9px;
  margin-top: 8px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0;
}
.wait-indicator.show { opacity: 1; animation: bob 1s ease-in-out infinite; }
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

.overlay-panel {
  position: absolute;
  top: 52px;
  right: 8px;
  left: 8px;
  max-height: 40vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 11px;
  z-index: 50;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
.overlay-panel.hidden { display: none; }
.overlay-panel .row { padding: 3px 0; color: var(--dim); border-bottom: 1px solid var(--line); }
#settings-panel label { display: block; font-size: 11px; color: var(--dim); margin-bottom: 6px; }
#settings-panel input[type="range"] { width: 100%; }
#settings-panel .row { margin-bottom: 12px; border-bottom: none; }
.help-panel {
  font-size: 12px;
  line-height: 1.7;
}
.help-panel h4 { color: var(--accent); font-size: 12px; margin: 12px 0 4px; }
.help-panel h4:first-child { margin-top: 0; }
.help-panel p { color: var(--txt); margin-bottom: 4px; }
.help-panel ul { padding-left: 18px; margin-bottom: 4px; }
.help-panel li { margin-bottom: 3px; }
.help-panel code {
  font-family: var(--mono);
  background: var(--panel2);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 11px;
}

#settings-panel select {
  width: 100%;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--txt);
  font-family: inherit;
  font-size: 12px;
  padding: 7px 8px;
}

.inspect-tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.inspect-tab {
  flex: 1;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--dim);
  font-family: inherit;
  font-size: 11px;
  padding: 7px 4px;
  cursor: pointer;
}
.inspect-tab.on { color: var(--accent); border-color: var(--accent); background: var(--panel3, var(--panel2)); }

#inspect-body .scope { color: var(--accent); font-weight: 700; margin: 6px 0 2px; }
#inspect-body .var-row { display: flex; justify-content: space-between; padding: 2px 0; }
#inspect-body .var-row.changed { animation: flash 0.6s ease; color: var(--accent); }
#inspect-body .empty { color: var(--dimmer); }
#inspect-body .row { padding: 3px 0; color: var(--dim); border-bottom: 1px solid var(--line); }

#inspect-body .trace-line { line-height: 1.6; word-break: break-all; }
#inspect-body .choice-tag { color: var(--accent); }
#inspect-body .back-btn {
  margin-top: 10px;
  width: 100%;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--txt);
  padding: 8px;
  font-size: 11px;
  cursor: pointer;
}
#inspect-body .back-btn:disabled { opacity: 0.4; cursor: default; }

.settings-section-title {
  font-size: 10px;
  color: var(--dim);
  letter-spacing: 0.8px;
  text-transform: uppercase;
  margin: 16px 0 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--line);
}
.settings-section-title:first-child { margin-top: 0; }

.advanced-section { margin-top: 18px; }
.advanced-section summary {
  font-size: 11px;
  color: var(--dim);
  letter-spacing: 0.5px;
  cursor: pointer;
  padding: 4px 0;
}
.advanced-section[open] summary { color: var(--accent); margin-bottom: 4px; }

.choice-panel {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  background: rgba(6, 6, 10, 0.72);
  backdrop-filter: blur(2px);
}
.choice-panel.hidden { display: none; }
.choice-btn {
  background: var(--panel2);
  border: 1px solid var(--line2, var(--line));
  border-radius: 10px;
  color: var(--txt);
  padding: 13px 16px;
  font-size: 14px;
  font-family: inherit;
  text-align: center;
  cursor: pointer;
}
.choice-btn:active { background: var(--accent); color: #17140a; }

/* 演出系(Phase3): quake / fade / bgm / se */
@keyframes shake-screen {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-6px, 3px); }
  40% { transform: translate(5px, -4px); }
  60% { transform: translate(-4px, -2px); }
  80% { transform: translate(6px, 3px); }
}
#game-view.shake { animation: shake-screen 0.08s linear infinite; }

.fade-overlay {
  position: absolute;
  inset: 0;
  z-index: 8;
  background: #000;
  opacity: 0;
  pointer-events: none;
  transition: opacity linear;
}

.bgm-indicator, .se-toast {
  position: absolute;
  left: 8px;
  bottom: 8px;
  z-index: 5;
  font-family: var(--mono);
  font-size: 9px;
  color: rgba(255, 255, 255, 0.75);
  background: rgba(0, 0, 0, 0.4);
  padding: 3px 8px;
  border-radius: 999px;
}
.se-toast { bottom: 28px; color: var(--accent); opacity: 0; transition: opacity 0.3s ease; }
.se-toast.show { opacity: 1; }
.bgm-indicator.hidden, .se-toast.hidden { display: none; }

.tempo-bar {
  display: flex;
  gap: 10px;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--dimmer);
  margin-top: 8px;
  flex-shrink: 0;
}
.tempo-bar #tempo-chars.over { color: var(--danger); font-weight: 700; }

/* ==============================
   画面レイアウト切替(設定パネルから選択)
   ① portrait-fixed: 標準(デフォルト、上記までのスタイルそのまま)
   ② portrait-full : 縦持ち・立ち絵フル画面+メッセージウィンドウは下部オーバーレイ
   ③ landscape     : 横持ち・#app自体を横長にし、内部は①と同じ(stage 16:9 + message-window)構成を流用
   ============================== */

/* ② 縦持ち・立ち絵フル画面版 */
body[data-layout="portrait-full"] .stage {
  position: absolute;
  inset: 0;
  aspect-ratio: auto;
}
body[data-layout="portrait-full"] .message-window {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 42%;
  border-top: none;
  background: linear-gradient(180deg, rgba(11, 11, 15, 0) 0%, rgba(11, 11, 15, 0.9) 26%, rgba(11, 11, 15, 0.97) 100%);
}
body[data-layout="portrait-full"] .chara-silhouette {
  width: 42%;
  max-width: 220px;
  height: 88%;
  aspect-ratio: auto;
}

/* ③ 横持ち: #appを横長にする(中身のstage 16:9 + message-windowの構成は①と共通のまま流用)。
   高さは中身に合わせた可変(auto)にし、無理に固定比率へ押し込まない(stageのflex-shrink:0とぶつかって
   はみ出すのを防ぐため)。実機で画面が狭い場合に備えて#app自体にも念のためスクロールを許可する。 */
body[data-layout="landscape"] {
  display: flex;
  align-items: center;
  justify-content: center;
}
body[data-layout="landscape"] #app {
  max-width: none;
  width: min(640px, 90vw);
  height: auto;
  max-height: 96vh;
  overflow-y: auto;
  margin: 0;
  border-radius: 14px;
}
body[data-layout="landscape"] .message-window {
  flex: 0 0 auto;
  max-height: 30vh;
}

/* 簡易編集モード(v2): 行番号ガター+簡易シンタックスハイライト。
   透明文字のtextareaを、色付きの<pre>の上に重ねてキャレットだけ見せる古典的な手法。 */
#edit-view {
  padding: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#edit-view.hidden {
  display: none;
}
.edit-wrap {
  flex: 1;
  display: flex;
  overflow: hidden;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
}
.edit-gutter {
  width: 34px;
  flex-shrink: 0;
  background: var(--panel);
  color: var(--dimmer);
  text-align: right;
  padding: 10px 6px 10px 0;
  white-space: pre;
  overflow: hidden;
  user-select: none;
  border-right: 1px solid var(--line);
}
.edit-highlight-scroll {
  position: relative;
  flex: 1;
  overflow: hidden;
  background: var(--bg);
}
.edit-highlight,
.edit-textarea {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 10px;
  font: inherit;
  line-height: inherit;
  white-space: pre;
  overflow: auto;
}
.edit-highlight {
  color: var(--txt);
  pointer-events: none;
}
.edit-textarea {
  background: transparent;
  color: transparent;
  caret-color: var(--txt);
  border: none;
  outline: none;
  resize: none;
}
.tok-comment { color: var(--dimmer); }
.tok-label { color: var(--accent); font-weight: 700; }
.tok-name { color: #7fd1ff; }
.tok-tag { color: #7ee787; }
.edit-actions {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}
.edit-actions button {
  flex: 1;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--txt);
  font-size: 13px;
  cursor: pointer;
}
.edit-actions button.primary { background: var(--accent); color: #17140a; font-weight: 700; border-color: var(--accent); }
