:root {
  --ink: #f3e6c8;
  --ink-dim: #c9b48e;
  --ink-faint: #8f7c62;
  --accent: #ffb04a;
  --accent-2: #ff7a2a;
  --cold: #7fb8ff;
  --wood: #2a1a12;
  --wood-2: #3b2517;
  --wood-edge: #7a4a28;
  --wood-dark: #120a07;
  --night: #0b1230;
  --panel: rgba(24, 15, 10, 0.78);
  --font: "DotGothic16", "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", "Meiryo", system-ui, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--night);
  color: var(--ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}
canvas#scene {
  position: fixed; inset: 0;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}
img { image-rendering: auto; }
button { font-family: inherit; color: inherit; }
kbd {
  font-family: inherit; font-size: 11px;
  padding: 1px 5px; border: 1px solid var(--wood-edge);
  background: var(--wood-dark); color: var(--ink-dim);
}

/* ---------- 木の板パネル ---------- */
.panel {
  background: var(--panel);
  border: 2px solid var(--wood-edge);
  box-shadow: 0 0 0 2px var(--wood-dark), inset 0 0 0 2px rgba(0,0,0,.35), 4px 4px 0 rgba(0,0,0,.35);
  border-radius: 6px;
  -webkit-backdrop-filter: blur(1.5px); backdrop-filter: blur(1.5px);
}

/* ---------- ボタン ---------- */
.btn {
  display: inline-block; min-width: 200px;
  padding: 12px 22px; font-size: 18px; letter-spacing: .08em;
  background: linear-gradient(#4a2e1c, #2e1c11);
  border: 2px solid var(--wood-edge);
  box-shadow: 0 0 0 2px var(--wood-dark), inset 0 2px 0 rgba(255,210,150,.12), 3px 3px 0 rgba(0,0,0,.4);
  border-radius: 6px; cursor: pointer;
  transition: transform .08s, filter .2s;
}
.btn:hover { filter: brightness(1.15); }
.btn:active { transform: translate(1px, 2px); }
.btn:focus-visible, .act:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.btn.primary { color: #fff3dc; border-color: #c9772f; background: linear-gradient(#7a3e17, #4a230e); }
.btn.primary::before { content: "▶ "; color: var(--accent); }
.btn.ghost { background: rgba(10,8,20,.4); border-color: #5a6080; min-width: 140px; font-size: 15px; }

/* ---------- オーバーレイ ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  opacity: 0; pointer-events: none;
  transition: opacity .6s ease;
}
.overlay.show { opacity: 1; pointer-events: auto; }
#howto, #settings, #paused { background: rgba(4, 6, 18, .55); z-index: 30; }
.sheet { width: min(520px, 100%); max-height: 100%; overflow: auto; padding: 22px 22px 18px; }
.sheet h2 { margin: 0 0 14px; font-size: 22px; font-weight: normal; color: var(--accent); letter-spacing: .1em; }
.sheet.center { text-align: center; }
.pause-buttons { display: flex; flex-direction: column; align-items: center; gap: 12px; margin: 18px 0 8px; }
.pause-buttons .btn { min-width: 220px; }
.title-buttons .btn[hidden] { display: none; }
.sheet .btn { min-width: 140px; }
.sheet-buttons { display: flex; gap: 12px; justify-content: flex-end; flex-wrap: wrap; margin-top: 10px; }
.small { font-size: 13px; color: var(--ink-dim); line-height: 1.6; margin: 6px 0; }

/* タイトル */
#title { background: linear-gradient(rgba(5,8,25,.55), rgba(5,8,25,.05) 45%, rgba(5,8,25,.55)); align-items: flex-start; }
.title-box { text-align: center; margin-top: max(5vh, 20px); }
.title-flame {
  width: 48px; height: 56px; margin: 0 auto 6px;
  background: url(../assets/icon_fire.png) center/contain no-repeat;
  animation: sway 2.4s ease-in-out infinite;
}
@keyframes sway { 0%,100% { transform: scale(1) } 50% { transform: scale(1.06, .97) } }
#title h1 {
  margin: 0; font-weight: normal;
  font-size: clamp(56px, 13vw, 112px); letter-spacing: .12em; line-height: 1;
  color: #ffbe63;
  text-shadow: 0 4px 0 #8a3b12, 0 8px 0 #3a170a, 0 0 28px rgba(255,140,50,.45);
}
.subtitle { margin: 14px 0 6px; font-size: clamp(18px, 4.6vw, 26px); letter-spacing: .2em; color: var(--ink); text-shadow: 0 2px 0 #000; }
.tagline { margin: 0 0 26px; font-size: clamp(13px, 3.4vw, 16px); color: var(--ink-dim); text-shadow: 0 2px 0 #000; }
.title-buttons { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.note { margin-top: 18px; font-size: 12px; color: var(--ink-dim); text-shadow: 0 1px 0 #000; }

.howto-list { margin: 0 0 12px; padding-left: 1.1em; line-height: 1.7; font-size: 15px; }
.howto-list li { margin-bottom: 8px; }
.howto-list b { color: var(--accent); font-weight: normal; }
.keys { font-size: 13px; color: var(--ink-dim); line-height: 2; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; transition: opacity .8s ease; }
#hud.hidden { opacity: 0; }
#hud.hidden * { pointer-events: none !important; }
#hud > * { pointer-events: auto; }

.status {
  position: absolute;
  top: calc(12px + var(--safe-t)); left: calc(12px + var(--safe-l));
  width: 300px; padding: 10px 12px 8px;
}
.status-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 6px; font-size: 14px; }
.place { color: var(--ink-dim); }
.state { color: var(--accent); }
.clock { margin-left: auto; font-size: 18px; letter-spacing: .05em; font-variant-numeric: tabular-nums; }

.meter { display: grid; grid-template-columns: 30px 52px 1fr 30px; align-items: center; gap: 6px; height: 30px; }
.m-icon { width: 30px; height: 26px; object-fit: contain; }
.m-label { font-size: 14px; }
.m-val { font-size: 14px; text-align: right; font-variant-numeric: tabular-nums; color: var(--ink-dim); }
.bar {
  position: relative; height: 12px;
  background: #140c08; border: 2px solid #4a2f1d; border-radius: 3px; overflow: hidden;
}
.bar .fill {
  position: absolute; inset: 0 auto 0 0; width: 0%;
  background: var(--accent);
  transition: width .5s linear, background-color 1.5s;
}
.bar::after { /* 区切り線でドット絵風のセグメントに */
  content: ""; position: absolute; inset: 0;
  background: repeating-linear-gradient(90deg, transparent 0 calc(10% - 2px), #140c08 calc(10% - 2px) 10%);
  pointer-events: none;
}
.meter[data-k="ember"] .fill { background: linear-gradient(90deg, #b8380f, #ff8a2a 60%, #ffc35c); }
.meter[data-k="warmth"] .fill { background: #7fb8ff; }
.meter[data-k="fuel"] .fill { background: linear-gradient(90deg, #6e3f20, #b9773d); }
.bar .cap { position: absolute; top: -2px; bottom: -2px; width: 2px; background: #ffe6b0; opacity: .75; left: 50%; transition: left .5s linear; z-index: 2; }
.meter.low .m-label, .meter.low .m-val { color: #ff9a6a; }

.top-buttons {
  position: absolute; top: calc(12px + var(--safe-t)); right: calc(12px + var(--safe-r));
  display: flex; gap: 8px;
}
.icon-btn {
  width: 52px; height: 52px; padding: 0; border: 0; background: none; cursor: pointer;
  filter: drop-shadow(2px 3px 0 rgba(0,0,0,.45));
  transition: transform .08s, filter .2s;
}
.icon-btn img { width: 100%; height: 100%; object-fit: contain; display: block; }
.icon-btn:hover { filter: drop-shadow(2px 3px 0 rgba(0,0,0,.45)) brightness(1.15); }
.icon-btn:active { transform: translateY(2px); }
#btnWatch { width: 64px; }

.hint {
  position: absolute; left: 50%; bottom: calc(104px + var(--safe-b)); transform: translate(-50%, 6px);
  max-width: min(440px, calc(100% - 32px));
  padding: 8px 16px; font-size: 15px; text-align: center; line-height: 1.5;
  background: rgba(10, 8, 18, .7); border: 1px solid rgba(255,190,110,.35); border-radius: 6px;
  color: var(--ink); opacity: 0; transition: opacity .8s, transform .8s; pointer-events: none !important;
}
.hint.show { opacity: 1; transform: translate(-50%, 0); }

.actions {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(18px + var(--safe-b));
  display: flex; gap: 14px;
}
.act {
  position: relative; overflow: hidden;
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px;
  min-width: 250px; height: 70px; padding: 8px 14px;
  background: linear-gradient(#3e2617, #24150c);
  border: 2px solid var(--wood-edge); border-radius: 8px;
  box-shadow: 0 0 0 2px var(--wood-dark), inset 0 2px 0 rgba(255,210,150,.10), 4px 4px 0 rgba(0,0,0,.4);
  cursor: pointer; text-align: left;
  transition: transform .08s, filter .2s;
}
.act:hover { filter: brightness(1.12); }
.act:active { transform: translate(1px, 2px); }
.act-icon { width: 46px; height: 38px; object-fit: contain; }
.act-icon.wind { width: 38px; height: 38px; color: #bfe0ff; }
.act-text b { display: block; font-weight: normal; font-size: 19px; letter-spacing: .06em; white-space: nowrap; }
.act-text small { white-space: nowrap; }
.act-text small { display: block; font-size: 12px; color: var(--ink-dim); margin-top: 2px; }
.act-key { justify-self: end; }
.act .cd { position: absolute; left: 0; bottom: 0; height: 3px; width: 0; background: var(--accent); opacity: .8; }
.act.busy { filter: brightness(.8); }

.watch-tip {
  position: fixed; left: 50%; bottom: calc(22px + var(--safe-b)); transform: translateX(-50%);
  z-index: 11; font-size: 13px; color: var(--ink-dim); padding: 6px 12px;
  background: rgba(10,8,18,.45); border-radius: 6px;
  opacity: 0; transition: opacity 1.2s; pointer-events: none;
}
.watch-tip.show { opacity: 1; }

/* 設定 */
.row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px dashed rgba(201,180,142,.25); font-size: 16px; }
.row > span { flex: 0 0 9em; }
.row input[type=range] { flex: 1; accent-color: var(--accent); min-width: 0; }
.row output { width: 2.4em; text-align: right; color: var(--ink-dim); }
.row input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--accent); }
.row select {
  font-family: inherit; font-size: 15px; color: var(--ink);
  background: var(--wood-dark); border: 2px solid var(--wood-edge); border-radius: 4px; padding: 6px 10px;
}

/* 終了 */
#over { background: radial-gradient(ellipse at 50% 60%, rgba(5,8,25,.15), rgba(5,8,25,.65)); transition: opacity 3s ease; }
.over-box { text-align: center; }
.over-text { font-size: clamp(22px, 6vw, 34px); letter-spacing: .18em; margin: 0 0 18px; text-shadow: 0 2px 0 #000, 0 0 20px rgba(0,0,0,.6); }
.over-time { font-size: 17px; color: var(--ink-dim); margin: 0 0 6px; text-shadow: 0 2px 0 #000; }
.over-time span { color: var(--ink); font-size: 22px; margin-left: 6px; font-variant-numeric: tabular-nums; }
.over-best { font-size: 14px; color: var(--accent); min-height: 1.4em; margin: 0 0 26px; text-shadow: 0 2px 0 #000; }

/* ---------- 景色えらび ---------- */
.scene-block { margin-top: 18px; }
.scene-label { margin: 0 0 8px; font-size: 13px; letter-spacing: .15em; color: var(--ink-dim); text-shadow: 0 1px 0 #000; }
.scene-pick { display: grid; grid-template-columns: repeat(4, minmax(0, 104px)); gap: 8px; justify-content: center; }
.scene-opt {
  padding: 3px 3px 5px; border: 2px solid var(--wood-edge); border-radius: 6px; cursor: pointer;
  background: var(--wood-dark); box-shadow: 2px 2px 0 rgba(0,0,0,.4);
  display: flex; flex-direction: column; gap: 4px; align-items: stretch; min-width: 0;
  transition: transform .1s, border-color .2s, filter .2s;
}
.scene-opt img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 3px; display: block; image-rendering: pixelated; filter: brightness(.75); transition: filter .2s; }
.scene-opt span { font-size: 12px; color: var(--ink-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scene-opt:hover img { filter: brightness(.95); }
.scene-opt[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(255,176,74,.35), 2px 2px 0 rgba(0,0,0,.4); }
.scene-opt[aria-checked="true"] img { filter: none; }
.scene-opt[aria-checked="true"] span { color: var(--ink); }
.scene-opt:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.row.column { flex-direction: column; align-items: stretch; }
.row.column .scene-pick { justify-content: stretch; grid-template-columns: repeat(4, minmax(0, 1fr)); }
#fade { position: fixed; inset: 0; z-index: 50; background: #04060f; opacity: 0; pointer-events: none; transition: opacity .6s ease; }
#fade.on { opacity: 1; }

/* ---------- スマホ縦画面 ---------- */
@media (max-width: 640px) {
  .status { top: calc(70px + var(--safe-t)); left: calc(10px + var(--safe-l)); right: calc(10px + var(--safe-r)); width: auto; padding: 8px 10px 6px; }
  .status-head { font-size: 13px; margin-bottom: 4px; }
  .clock { font-size: 16px; }
  .meter { grid-template-columns: 24px 46px 1fr 28px; height: 24px; gap: 5px; }
  .m-icon { width: 24px; height: 20px; }
  .m-label, .m-val { font-size: 13px; }
  .bar { height: 10px; }
  .top-buttons { top: calc(10px + var(--safe-t)); right: calc(10px + var(--safe-r)); }
  .icon-btn { width: 48px; height: 48px; }
  #btnWatch { width: 58px; }
  .hint { top: auto; bottom: calc(108px + var(--safe-b)); font-size: 14px; }
  .actions { left: calc(10px + var(--safe-l)); right: calc(10px + var(--safe-r)); transform: none; gap: 10px; bottom: calc(14px + var(--safe-b)); }
  .act { flex: 1; min-width: 0; height: 74px; grid-template-columns: auto 1fr; padding: 8px 10px; gap: 8px; }
  .act-key { display: none; }
  .act-icon { width: 38px; height: 32px; }
  .act-icon.wind { width: 32px; height: 32px; }
  .act-text b { font-size: 17px; }
  .btn { min-width: 220px; }
}
/* 横向きで背が低い画面（スマホ横持ち） */
@media (max-height: 480px) and (orientation: landscape) {
  .status { top: calc(8px + var(--safe-t)); width: 260px; padding: 6px 10px 4px; }
  .meter { height: 22px; grid-template-columns: 22px 44px 1fr 26px; }
  .m-icon { width: 22px; height: 18px; }
  .bar { height: 9px; }
  .status-head { margin-bottom: 2px; }
  .icon-btn { width: 42px; height: 42px; }
  #btnWatch { width: 50px; }
  .act { height: 56px; min-width: 190px; }
  .act-text b { font-size: 16px; }
  .act-text small { display: none; }
  .actions { bottom: calc(8px + var(--safe-b)); }
  .title-box { margin-top: 0; }
  #title h1 { font-size: 48px; }
  .tagline { margin-bottom: 12px; }
  .title-buttons { flex-direction: row; flex-wrap: wrap; justify-content: center; }
  .scene-block { margin-top: 10px; }
  .scene-label { display: none; }
  .scene-pick { grid-template-columns: repeat(4, minmax(0, 84px)); }
  .btn { min-width: 0; padding: 9px 16px; font-size: 16px; }
  .hint { top: calc(10px + var(--safe-t)); bottom: auto; max-width: calc(100% - 560px); min-width: 220px; font-size: 13px; padding: 6px 10px; }
  .act-key { display: none; }
  .act { grid-template-columns: auto 1fr; min-width: 170px; }
}
@media (prefers-reduced-motion: reduce) {
  .title-flame { animation: none; }
}
.row.column > span { flex: none; }
