/* ===== tokens ===== */
:root {
  --ink: #232630;
  --paper: #f2f3f6;
  --surface: #ffffff;
  --surface-2: #eceef3;
  --line: #dfe1e8;
  --muted: #676b78;
  --accent: #574fd6;
  --accent-soft: #eceafb;
  --branch: #cf6446;
  --branch-soft: #f7e9e3;
  --asset: #2f8f77;
  --asset-soft: #e1f0ec;
  --ok: #2f9e6b;
  --err: #d21f3c;
  --warn: #a9791a;

  --font-display: "Kaisei Tokumin", "Hiragino Mincho ProN", "Yu Mincho", serif;
  --font-body: "Noto Sans JP", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  --fs: 13px;
  --fs-field: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink: #e7e8ec;
    --paper: #131418;
    --surface: #1c1e24;
    --surface-2: #24262f;
    --line: #32343e;
    --muted: #9aa0ad;
    --accent: #918af6;
    --accent-soft: #272552;
    --branch: #e28c72;
    --branch-soft: #3a2a24;
    --asset: #58c0a5;
    --asset-soft: #21322e;
    --ok: #57c491;
    --err: #f2647d;
    --warn: #d6a54a;
  }
}
:root[data-theme="dark"] {
  --ink: #e7e8ec;
  --paper: #131418;
  --surface: #1c1e24;
  --surface-2: #24262f;
  --line: #32343e;
  --muted: #9aa0ad;
  --accent: #918af6;
  --accent-soft: #272552;
  --branch: #e28c72;
  --branch-soft: #3a2a24;
  --asset: #58c0a5;
  --asset-soft: #21322e;
  --ok: #57c491;
  --err: #f2647d;
  --warn: #d6a54a;
}

* { box-sizing: border-box; margin: 0; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  font-family: var(--font-body);
  font-size: var(--fs);
  color: var(--ink);
  background: var(--paper);
  display: grid;
  grid-template-rows: auto auto 1fr;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ===== controls ===== */
button {
  font: inherit;
  cursor: pointer;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 6px 11px;
  min-height: 32px;
}
button:hover { border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button.t-ghost { background: transparent; }
button.t-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
button.t-primary:hover { background: color-mix(in srgb, var(--accent) 85%, #000); }
button.t-ghost.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

button.ic {
  min-height: 0;
  padding: 2px 6px;
  font-size: 12px;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  line-height: 1;
}
button.ic:hover { background: var(--surface-2); color: var(--ink); }

.chip-btn {
  min-height: 0;
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--accent);
}

input, select, textarea {
  font-family: var(--font-body);
  font-size: var(--fs-field);
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 6px 9px;
  max-width: 100%;
}
textarea { line-height: 1.65; resize: none; overflow: hidden; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
code { font-family: var(--font-mono); }

#banner {
  grid-row: 1;
  background: color-mix(in srgb, var(--warn) 18%, var(--surface));
  border-bottom: 1px solid color-mix(in srgb, var(--warn) 40%, var(--line));
  padding: 8px 14px;
  font-size: 12px;
}

/* ===== topbar ===== */
#topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.03em;
  margin-right: 6px;
}
#topbar .grow { flex: 1; }
#project-select { max-width: 190px; font-size: 13px; }
#save-state { font-size: 11px; color: var(--muted); min-width: 84px; text-align: right; }

/* ===== settings ===== */
#settings { border-bottom: 1px solid var(--line); background: var(--surface); }
.panel-h {
  width: 100%;
  text-align: left;
  border: 0;
  border-radius: 0;
  background: color-mix(in srgb, var(--surface-2) 60%, var(--surface));
  font-weight: 700;
  padding: 7px 14px;
  min-height: 0;
}
.panel-b {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 8px 16px;
  padding: 11px 14px;
  max-height: 30vh;
  overflow-y: auto;
}
.panel-b.collapsed { display: none; }
.panel-b label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 11px;
  color: var(--muted);
}
.panel-b input, .panel-b select { color: var(--ink); }

/* ===== stage ===== */
#stage {
  grid-row: 3;
  display: grid;
  grid-template-columns: 204px minmax(0, 1fr) var(--pv-w, 320px);
  min-height: 0;
  overflow: hidden;
}

/* 横向きゲーム: 右カラムのまま、枠を 16:9・カラム上部に置く（下は余らせない） */
#stage[data-orient="landscape"] #previewpane { align-self: start; max-height: 100%; }
#stage[data-orient="landscape"] .pv-wrap { display: block; padding: 10px; }
#stage[data-orient="landscape"] #preview { height: auto; aspect-ratio: 16 / 9; }

/* プレビューを畳んだ状態: フローティングの小さいピルに */
#stage.pv-collapsed { grid-template-columns: 204px minmax(0, 1fr) 0; }
#stage.pv-collapsed #previewpane {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 40;
  width: auto;
  height: auto;
  align-self: auto;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: 0 8px 24px -10px rgba(0, 0, 0, 0.3);
}
#stage.pv-collapsed .pv-wrap,
#stage.pv-collapsed .pv-resize,
#stage.pv-collapsed #pv-restart,
#stage.pv-collapsed #pv-out,
#stage.pv-collapsed #pv-label { display: none; }
#stage.pv-collapsed .pv-bar { background: transparent; border: 0; padding: 5px 8px; color: var(--ink); }
#stage.pv-collapsed .pv-bar .t-ghost { color: var(--accent); border-color: transparent; }

/* リサイズハンドル（左端・幅を変える） */
#previewpane { position: relative; }
.pv-resize {
  position: absolute;
  left: -3px;
  top: 0;
  width: 7px;
  height: 100%;
  z-index: 6;
  cursor: col-resize;
  background: transparent;
}
.pv-resize:hover, .pv-resize.dragging { background: var(--accent); opacity: 0.35; }

/* --- scene list --- */
#scenelist-host { overflow-y: auto; border-right: 1px solid var(--line); background: var(--paper); }
.scenelist { padding: 8px; display: flex; flex-direction: column; gap: 3px; }
.sl-h {
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
  padding: 4px 6px 6px;
}
.sl-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  position: relative;
  overflow: hidden;
}
.sl-item:hover { background: var(--surface); }
.sl-item.active { background: var(--surface); color: var(--ink); font-weight: 700; box-shadow: inset 0 0 0 1px var(--line); }
.sl-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; background: var(--muted); }
.sl-dot.ok { background: var(--ok); }
.sl-dot.warn { background: var(--warn); }
.sl-dot.err { background: var(--err); }
.sl-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; min-width: 0; }
.sl-end { font-size: 9px; font-weight: 700; color: var(--branch); letter-spacing: 0.05em; flex: none; }
.sl-tools { display: none; gap: 0; flex: none; }
.sl-item.active .sl-tools { display: flex; }
.sl-item.active .sl-tools .ic { padding: 2px 3px; }
.sl-add {
  margin-top: 4px;
  border: 1px dashed var(--line);
  background: transparent;
  color: var(--accent);
  font-size: 12px;
  border-radius: 8px;
}

/* --- card pane --- */
#cardpane { display: flex; flex-direction: column; min-height: 0; background: var(--paper); }
#cardpane-head {
  padding: 9px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
#active-scene-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
}
#cardstack-host { flex: 1; min-height: 0; overflow-y: auto; padding: 14px 16px 24px; }

.cardstack { display: flex; flex-direction: column; gap: 9px; max-width: 640px; }

.card {
  display: grid;
  grid-template-columns: 26px 1fr;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--surface);
  overflow: hidden;
}
.card-move {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 2px;
  background: var(--surface-2);
  border-right: 1px solid var(--line);
}
.card-move .ic { font-size: 9px; padding: 3px 2px; }
.card-main { padding: 9px 11px 11px; border-left: 3px solid var(--line); }
.card-say .card-main { border-left-color: var(--accent); }
.card-bg .card-main, .card-chara .card-main { border-left-color: var(--asset); }
.card-choice .card-main { border-left-color: var(--branch); }
.card-note .card-main { border-left-color: var(--muted); }
.card-interlude .card-main { border-left-color: #4b4b57; }
.card-interlude .card-kind { background: #2a2a32; color: #d9d9e2; }

.card-head { display: flex; align-items: center; margin-bottom: 7px; }
.card-kind {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  padding: 2px 8px;
  border-radius: 5px;
  background: var(--surface-2);
  color: var(--muted);
}
.card-say .card-kind { background: var(--accent-soft); color: var(--accent); }
.card-bg .card-kind, .card-chara .card-kind { background: var(--asset-soft); color: var(--asset); }
.card-choice .card-kind { background: var(--branch-soft); color: var(--branch); }
.card-del { margin-left: auto; }

.card-body { display: flex; flex-direction: column; gap: 7px; }
.card-body .f-who { width: 55%; min-width: 140px; }
.card-body .f-line, .card-body .f-note { width: 100%; }
.card-note .f-note { background: color-mix(in srgb, var(--surface-2) 70%, var(--surface)); }
.f-row-end { display: flex; justify-content: flex-end; }
.card-note-txt { font-size: 12px; color: var(--muted); }

.f-inline { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }

.card-cfg {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 7px 12px;
  margin-top: 4px;
  padding: 9px 10px;
  background: color-mix(in srgb, var(--surface-2) 55%, var(--surface));
  border-radius: 8px;
}
.card-cfg label { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--muted); }
.card-cfg input { color: var(--ink); font-size: 14px; }
.f-hint { font-size: 11px; color: var(--warn); }
.f-asset-box { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.f-asset { min-width: 160px; }
.f-url { flex: 1; min-width: 180px; font-size: 13px; }
.f-swatch {
  width: 44px; height: 28px;
  border-radius: 5px;
  border: 1px solid var(--line);
  background-size: cover;
  background-position: center;
  flex: none;
}
.f-chara-wrap { display: flex; flex-direction: column; gap: 6px; }
.pos-row { display: flex; align-items: center; gap: 4px; }
.pos-lbl { font-size: 11px; color: var(--muted); margin-right: 2px; }
.pos-btn {
  min-height: 0;
  padding: 3px 12px;
  font-size: 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
}
.pos-btn.on { background: var(--asset-soft); border-color: var(--asset); color: var(--asset); font-weight: 700; }

.opts { display: flex; flex-direction: column; gap: 6px; }
.opt { display: flex; align-items: center; gap: 6px; }
.opt-text { flex: 1; min-width: 0; }
.opt-arrow { color: var(--branch); font-weight: 700; }
.f-scene { max-width: 150px; font-size: 13px; }

.adder {
  margin-top: 4px;
  border: 1px dashed var(--line);
  border-radius: 11px;
  padding: 9px 11px;
  background: transparent;
}
.adder-h { font-size: 12px; font-weight: 700; color: var(--accent); }
.adder-row { margin-top: 7px; display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  min-height: 0;
  font-size: 12px;
  padding: 4px 11px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
}
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* --- issue bar --- */
#issuebar {
  border-top: 1px solid var(--line);
  padding: 8px 16px;
  max-height: 28vh;
  overflow-y: auto;
  background: var(--surface);
  font-size: 12px;
}
#issuebar.err { border-top-color: var(--err); }
#issuebar.warn { border-top-color: var(--warn); }
.ib-head { font-weight: 700; margin-bottom: 4px; }
#issuebar.err .ib-head { color: var(--err); }
#issuebar.warn .ib-head { color: var(--warn); }
.ib-item { padding: 4px 2px; cursor: pointer; border-radius: 5px; line-height: 1.5; }
.ib-item:hover { background: var(--surface-2); }
.ib-loc {
  font-size: 10px;
  border: 1px solid currentColor;
  border-radius: 4px;
  padding: 0 4px;
  opacity: 0.8;
}

/* --- preview --- */
#previewpane {
  border-left: 1px solid var(--line);
  background: #1b1c22;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.pv-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  background: #26272e;
  color: #cfd2e0;
  font-size: 11px;
}
.pv-bar .t-ghost { color: #cfd2e0; border-color: #3a3b45; }
#pv-label { color: #9aa6e6; }
.pv-wrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 12px; }
#preview { width: 100%; height: 100%; border: 0; border-radius: 12px; background: #000; }

/* ===== advanced mode ===== */
#advanced {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--paper);
  display: flex;
  flex-direction: column;
}
#assets-drawer { display: block; }
.adv-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  font-size: 13px;
}
#adv-text {
  flex: 1;
  min-height: 0;
  border: 0;
  border-radius: 0;
  padding: 16px;
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.7;
  overflow: auto;
  background: var(--surface);
}
#adv-issues { padding: 0 16px; max-height: 30vh; overflow-y: auto; }
.adv-err { color: var(--err); font-size: 12px; padding: 3px 0; }

/* ===== assets drawer ===== */
#assets-drawer { position: fixed; inset: 0; z-index: 70; }
.dr-scrim { position: absolute; inset: 0; background: rgba(10, 12, 20, 0.4); }
.dr-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: min(380px, 92vw);
  height: 100%;
  background: var(--surface);
  border-left: 1px solid var(--line);
  padding: 14px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: -20px 0 50px -25px rgba(0, 0, 0, 0.4);
}
.dr-head { display: flex; align-items: center; font-family: var(--font-display); font-weight: 700; font-size: 15px; }
.dr-head .ic { margin-left: auto; }
.dr-drop {
  border: 1.5px dashed var(--line);
  border-radius: 12px;
  padding: 22px 14px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  line-height: 2;
}
.dr-drop.over { border-color: var(--accent); background: var(--accent-soft); }
.dr-note { font-size: 11px; color: var(--muted); line-height: 1.7; }
.dr-empty { font-size: 12px; color: var(--muted); text-align: center; padding: 8px; }
#asset-list { display: flex; flex-direction: column; gap: 8px; }
.a-row { display: grid; grid-template-columns: 46px 1fr auto; gap: 10px; align-items: center; }
.a-thumb {
  width: 46px; height: 32px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  object-fit: cover;
  display: block;
}
.a-key { font-size: 13px; font-weight: 700; }
.a-note { font-size: 10px; color: var(--muted); margin-top: 2px; }

/* ===== toast ===== */
#toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translate(-50%, 12px);
  background: var(--ink);
  color: var(--paper);
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: 0.25s;
  z-index: 200;
  max-width: 90vw;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ===== responsive ===== */
@media (max-width: 1000px) {
  body { overflow: auto; }
  #stage {
    grid-template-columns: 1fr;
    overflow: visible;
  }
  #scenelist-host { border-right: 0; border-bottom: 1px solid var(--line); max-height: 30vh; }
  #cardpane { min-height: 60vh; }
  #previewpane { border-left: 0; border-top: 1px solid var(--line); min-height: 55vh; align-self: stretch; }
  #stage[data-orient="landscape"] #previewpane { min-height: 40vh; }
  #cardstack-host { overflow: visible; }
  .pv-resize { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
