:root {
  --bg: #0d0d10;
  --fg: #ece8e1;
  --muted: #9a958c;
  --accent: #e8b46a;
  --box: rgba(14, 14, 18, .92);
  --line: rgba(255, 255, 255, .12);
  --wall-a: #6d6a63;
  --wall-b: #4f4c46;
  --sil: #17161a;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: #000; color: var(--fg); overflow: hidden;
  font-family: "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif; }
/* ゲーム画面はいつも 9:16（画面に収まる最大の大きさ。余白は黒） */
body { display: flex; align-items: center; justify-content: center; height: 100vh; height: 100dvh; }
#app { position: relative; flex: none; background: var(--bg); overflow: hidden;
  width: min(100vw, calc(100dvh * 9 / 16)); height: min(100dvh, calc(100vw * 16 / 9)); }
@supports not (height: 100dvh) { #app { width: min(100vw, calc(100vh * 9 / 16)); height: min(100vh, calc(100vw * 16 / 9)); } }

/* ---------- 開発用パネル（S キー） ---------- */
body.dev #app { width: min(calc(100vw - 340px), calc(100dvh * 9 / 16)); height: min(100dvh, calc((100vw - 340px) * 16 / 9)); }
#devpanel { display: none; }
body.dev #devpanel { display: block; flex: none; width: 340px; height: 100dvh; overflow-y: auto; padding: 10px 12px 40px;
  background: #15151b; border-left: 1px solid var(--line); font-size: 12px; line-height: 1.5; }
@media (max-width: 700px) {   /* 狭い画面では、ゲームの上に重ねる */
  body.dev #app { width: min(100vw, calc(100dvh * 9 / 16)); height: min(100dvh, calc(100vw * 16 / 9)); }
  body.dev #devpanel { position: fixed; right: 0; top: 0; bottom: 0; width: min(340px, 90vw); z-index: 50; background: rgba(21, 21, 27, .95); }
}
#devpanel header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; font-size: 14px; }
#devpanel .dp-hint { color: var(--muted); font-size: 11px; }
#devpanel section { border-top: 1px solid var(--line); padding: 8px 0; }
#devpanel h3 { margin: 0 0 6px; font-size: 12px; color: var(--accent); }
#devpanel button { background: rgba(255,255,255,.07); border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; cursor: pointer; }
#devpanel input, #devpanel select { font: inherit; color: inherit; background: #0d0d12; border: 1px solid var(--line); border-radius: 4px; padding: 1px 4px; }
#devpanel input[type=number] { width: 64px; }
#devpanel .dp-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin: 4px 0; }
#devpanel .dp-small { color: var(--muted); font-size: 11px; }
#devpanel .dp-info { white-space: pre-wrap; color: #cfc9be; }
#devpanel .dp-slider { display: grid; grid-template-columns: 6em 1fr 2.4em; align-items: center; gap: 6px; }
#devpanel .dp-slider input { width: 100%; padding: 0; }
#devpanel .dp-slider output { text-align: right; font-variant-numeric: tabular-nums; }
#devpanel .dp-check { display: flex; align-items: center; gap: 4px; }
#devpanel .dp-inv { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 10px; }
#devpanel .dp-inv label { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
#devpanel .dp-flags { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 8px; max-height: 260px; overflow-y: auto; }
.screen { position: absolute; inset: 0; display: flex; flex-direction: column; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }

/* ---------- タイトル ---------- */
#title { align-items: center; justify-content: center;
  /* タイトル画像（img/end/end_title.webp）。文字が読めるよう、上下を暗くする */
  background:
    linear-gradient(180deg, rgba(5,5,8,.55) 0%, rgba(5,5,8,.15) 30%, rgba(5,5,8,.35) 55%, rgba(5,5,8,.88) 100%),
    url(img/end/end_title.webp) center / cover no-repeat,
    radial-gradient(ellipse at 50% 40%, #3a3326 0%, #121114 60%, #050506 100%); }
#title h1, #title .title-sub { text-shadow: 0 2px 12px rgba(0,0,0,.85), 0 0 2px rgba(0,0,0,.9); }
.title-box { width: 100%; padding: 0 28px; text-align: center; }
.title-sub { color: var(--muted); letter-spacing: .2em; font-size: 13px; margin: 0; }
h1 { font-size: 56px; letter-spacing: .3em; margin: 8px 0 40px; font-weight: 300;
  text-shadow: 0 0 24px rgba(232, 180, 106, .35); }
.name-label { display: block; color: var(--muted); font-size: 13px; margin-bottom: 24px; }
.name-label input { display: block; width: 100%; margin-top: 8px; padding: 12px; font-size: 18px;
  text-align: center; background: rgba(255,255,255,.06); color: var(--fg);
  border: 1px solid var(--line); border-radius: 8px; }
.btn-main { display: block; width: 100%; margin: 10px 0; padding: 14px; font-size: 17px;
  background: rgba(232, 180, 106, .15); border: 1px solid rgba(232, 180, 106, .5); border-radius: 8px; cursor: pointer; }
.btn-main:active { background: rgba(232, 180, 106, .3); }
.age-gate { display: block; margin: 0 0 6px; font-size: 14px; cursor: pointer; }
.age-gate input { width: 18px; height: 18px; vertical-align: middle; margin-right: 6px; }
.age-note { color: var(--muted); font-size: 11px; margin: 0 0 12px; }
.btn-main:disabled { opacity: .35; cursor: default; }
.ending-list { color: var(--muted); font-size: 12px; min-height: 1em; }
.proto-note { color: #5d5a55; font-size: 11px; margin-top: 30px; }

/* ---------- HUD ---------- */
#hud { display: flex; align-items: center; gap: 10px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  font-size: 13px; background: #0a0a0c; border-bottom: 1px solid var(--line); z-index: 5; }
#clock { font-variant-numeric: tabular-nums; font-size: 15px; }
#elapsed { color: var(--muted); flex: 1; }
#battery { color: var(--muted); font-variant-numeric: tabular-nums; }
#btnVoice, #btnDebug { background: none; border: 1px solid var(--line); border-radius: 6px; padding: 2px 8px; cursor: pointer; }

/* ---------- ステージ（エレベーター内） ---------- */
#stage { position: relative; flex: 1; min-height: 0; overflow: hidden;
  background: linear-gradient(#2c2b28, #1d1c1a); }
#stage.shake { animation: shake .6s; }
#stage.shake-big { animation: shake 1.4s; }
@keyframes shake {
  10%, 50%, 90% { transform: translate(-6px, 3px); }
  30%, 70% { transform: translate(6px, -4px); }
  20%, 60% { transform: translate(3px, 5px); }
  40%, 80% { transform: translate(-4px, -3px); }
}
/* 3D のかご（Three.js）。使えないときは1枚絵の背景 */
#bg3d { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#stage.three-off { background: #1d1c1a url(img/elevator.webp) center 30% / cover no-repeat; }
#stage.three-off #bg3d { display: none; }
#stage.three-on.shake, #stage.three-on.shake-big { animation: none; }   /* 3D ではカメラが揺れる */
#stage.three-on #dark { display: none; }                                /* 暗さは 3D の光で表す */

/* 立ち絵（3D が使えないときだけ、キャンバスをそのまま置く） */
#heroine { position: absolute; left: 50%; bottom: 0; height: 96%; aspect-ratio: 576 / 1032; transform: translateX(-50%);
  transition: left .6s ease, height .6s ease; z-index: 1; pointer-events: none; }
#heroine canvas { width: 100%; height: 100%; transform-origin: 50% 100%; }
#stage.three-on #heroine { display: none; }
#heroine.pose-stand { height: 98%; }
#heroine.pose-sit { height: 80%; }
#heroine.pose-squat { height: 72%; }
#heroine.close { left: 30%; height: 104%; bottom: -12%; }

#dark { position: absolute; inset: 0; pointer-events: none; z-index: 2; opacity: 0; transition: opacity 1.2s, background .8s;
  background: #000; }
#stage.three-off.lights-flicker { animation: flicker .9s; }
@keyframes flicker { 0%, 30%, 60%, 100% { filter: none; } 15%, 45%, 75% { filter: brightness(.2); } }

/* 佳奈の様子（ステージの上に、半透明の帯で読みやすく。足もとの影を隠さないように） */
/* 視野が狭くなる（体力が極端に低い・トイレの限界）。--v（0〜1）の強さで、まわりが暗くぼやける */
#vignette { position: absolute; inset: 0; z-index: 2; pointer-events: none; opacity: 0; transition: opacity 1.5s;
  --v: 0; --r: calc(78% - var(--v) * 46%);
  background: radial-gradient(ellipse 60% 55% at 50% 45%, rgba(0,0,0,0) var(--r), rgba(10,0,0,.55) calc(var(--r) + 14%), rgba(0,0,0,.94) calc(var(--r) + 34%)); }
#vignette.on { opacity: 1; }   /* 視界が狭いだけ（脈打つ動きはなし） */
#stage.tunnel #bg3d { filter: blur(calc(var(--v) * 1.8px)); }
#status { position: absolute; left: 8px; right: 8px; top: 8px; z-index: 3; font-size: 14px; line-height: 1.6;
  color: #fff7ea; font-weight: 500; letter-spacing: .02em; padding: 6px 12px; border-radius: 10px;
  background: rgba(12, 12, 18, .62); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  border: 1px solid rgba(255, 255, 255, .12); text-shadow: 0 1px 2px rgba(0, 0, 0, .8); }
#status:empty { display: none; }

/* ---------- エンディング（紙芝居）：木の枠の中で、絵を横に引き抜いて入れ替える ---------- */
#cg { position: absolute; inset: 0; z-index: 30; background: #2a1d12; overflow: hidden;
  border: 10px solid #6b4a2b; border-radius: 6px; box-shadow: inset 0 0 0 3px #3c2716, inset 0 0 30px rgba(0,0,0,.6); }
#cg .card { position: absolute; inset: 0; background: #000 center / cover no-repeat; transition: transform 1.1s cubic-bezier(.6,.05,.3,1); }
#cg .card.enter { transform: translateX(105%); }
#cg .card.leave { transform: translateX(-105%); }
#game.cg-on #msg, #game.cg-on #choices { position: absolute; left: 0; right: 0; z-index: 31; }
#game.cg-on #msg { bottom: 0; background: rgba(10, 8, 6, .78); border-top: 1px solid rgba(255,255,255,.12); }
#game.cg-on #hud { visibility: hidden; }

/* ---------- テキスト ---------- */
#msg { position: relative; min-height: 132px; padding: 12px 16px 26px; background: var(--box);
  border-top: 1px solid var(--line); cursor: pointer; user-select: none; }
#who { font-size: 13px; color: var(--accent); min-height: 1.2em; margin-bottom: 4px; }
#text { font-size: 16px; line-height: 1.75; white-space: pre-wrap; }
#text.narr { color: #d8d3ca; }
#text.inner { color: #aab6c8; font-style: italic; }
#next { position: absolute; right: 14px; bottom: 6px; font-size: 12px; color: var(--muted); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }

#choices { display: flex; flex-direction: column; gap: 6px; padding: 8px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: #0b0b0d; max-height: 64%; overflow-y: auto; min-height: 0; }
/* 選択肢が出ているあいだは、ステージを縮めて選択肢を全部見せる */
#stage { min-height: 120px; }
#game.choosing #msg { min-height: 0; padding-bottom: 10px; }
#choices:empty { display: none; }
#choices.grid { display: grid; grid-template-columns: 1fr 1fr; }
#choices button { padding: 10px 12px; text-align: left; font-size: 15px; line-height: 1.4; cursor: pointer;
  background: rgba(255,255,255,.05); border: 1px solid var(--line); border-radius: 8px; }
#choices.grid button { text-align: center; }
#choices button:active { background: rgba(232, 180, 106, .25); }
#choices button:disabled { opacity: .35; }
#choices button small { display: block; color: var(--muted); font-size: 12px; }
#choices button.back { color: var(--muted); }

/* ---------- デバッグ ---------- */
#debug { position: absolute; top: 44px; right: 6px; left: 6px; z-index: 20; padding: 10px; font-size: 12px;
  background: rgba(0, 0, 0, .9); border: 1px solid var(--line); border-radius: 8px; }
#debugBody { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 14px; }
.dbg-row { display: grid; grid-template-columns: 5.5em 1fr 2.2em; align-items: center; gap: 4px; }
.dbg-bar { height: 6px; background: #333; border-radius: 3px; overflow: hidden; }
.dbg-bar i { display: block; height: 100%; background: var(--accent); }
.dbg-h { grid-column: 1 / -1; color: var(--accent); margin-top: 4px; }
.dbg-col { color: var(--accent); margin-top: 4px; }
.dbg-info { grid-column: 1 / -1; color: var(--muted); }
.debug-btns { display: flex; gap: 6px; margin-top: 8px; }
.debug-btns button { flex: 1; padding: 6px; background: #222; border: 1px solid #444; border-radius: 6px; }

/* ---------- エンディング ---------- */
.ending-card { text-align: center; padding: 6px 0; }
.ending-card b { display: block; font-size: 22px; color: var(--accent); letter-spacing: .2em; }
