/* 迷いの森 — 奈良時代風・縦長固定 */
:root {
  --ink: #1a120c;
  --paper: #e8dcc8;
  --vermilion: #b33a2b;
  --vermilion-deep: #7a2418;
  --indigo: #2a3a5c;
  --moss: #3d5a3a;
  --moss-dark: #1e2e1c;
  --gold: #c9a227;
  --wood: #5c4030;
  --glow: #2a2218;
  --wood-line: #3a2a1a;
  --void: #0c100c;
  --muted: #8a7a68;
  --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);
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
  touch-action: none;
}

::selection { background: transparent; color: inherit; }

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  height: -webkit-fill-available;
  overflow: hidden;
  position: fixed;
  inset: 0;
  overscroll-behavior: none;
  background: var(--ink);
  color: var(--paper);
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
}

#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding:
    calc(8px + var(--safe-t))
    calc(10px + var(--safe-r))
    calc(8px + var(--safe-b))
    calc(10px + var(--safe-l));
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
  background:
    radial-gradient(ellipse at 50% 0%, var(--glow) 0%, var(--ink) 55%),
    var(--ink);
  transition: background 0.35s ease;
}

/* HUD */
.hud { display: flex; flex-direction: column; gap: 4px; z-index: 2; }
.hud[hidden] { display: none !important; }
.hud-row { display: flex; justify-content: space-between; align-items: center; }
.seal {
  font-size: 0.95rem;
  letter-spacing: 0.15em;
  color: var(--gold);
  border: 1px solid var(--gold);
  padding: 3px 10px;
  background: rgba(201, 162, 39, 0.08);
}
.icon-btn {
  min-width: 48px;
  min-height: 48px;
  border: 1px solid var(--muted);
  background: rgba(0,0,0,0.35);
  color: var(--paper);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, opacity 0.08s ease;
}
.icon-btn:active, .icon-btn.is-pressed { transform: scale(0.92); opacity: 0.88; }

.race-board {
  display: grid;
  grid-template-columns: 1fr 1.4fr 1fr;
  align-items: center;
  gap: 6px;
  background: rgba(0,0,0,0.4);
  border: 1px solid #3a2e22;
  padding: 6px 8px;
}
.score { display: flex; flex-direction: column; align-items: center; line-height: 1.1; gap: 2px; }
.score-icon {
  width: 24px;
  height: 32px;
  object-fit: contain;
  object-position: bottom center;
  pointer-events: none;
}
.score .lbl { font-size: 0.7rem; color: var(--muted); letter-spacing: 0.2em; }
.score span:last-child { font-size: 1.45rem; font-variant-numeric: tabular-nums; }
.score.me span:last-child { color: #d4c4a0; }
.score.foe span:last-child { color: #9bb0d0; }
.gap-wrap { text-align: center; }
.gap-label { font-size: 0.65rem; color: var(--muted); letter-spacing: 0.3em; }
.gap-track {
  height: 8px;
  background: #2a2218;
  border: 1px solid #4a3a28;
  margin: 4px 0;
  overflow: hidden;
  position: relative;
}
.gap-fill {
  position: absolute;
  top: 0; bottom: 0; left: 50%;
  width: 50%;
  background: linear-gradient(90deg, var(--moss), #6a8a5a);
  transform-origin: left center;
  transition: transform 0.15s ease, background 0.2s ease, left 0.15s ease;
}
.gap-fill.behind {
  background: linear-gradient(90deg, var(--vermilion-deep), var(--vermilion));
}
.gap-text { font-size: 0.8rem; font-variant-numeric: tabular-nums; color: var(--gold); }
.hud-sub { font-size: 0.72rem; color: var(--muted); letter-spacing: 0.08em; text-align: center; }

/* Stage / Canvas */
.stage {
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px 0;
}
#game {
  display: block;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1 / 1;
  background: var(--void);
  border: 2px solid var(--wood-line);
  box-shadow: 0 0 0 1px var(--ink), inset 0 0 40px rgba(0, 0, 0, 0.45);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* Controls */
.controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: end;
  gap: 4px 8px;
  padding-top: 2px;
  z-index: 2;
}
.controls[hidden] { display: none !important; }

.ofuda-btn {
  grid-column: 3;
  justify-self: end;
  align-self: center;
  min-height: 56px;
  min-width: 72px;
  padding: 0 12px;
  border: 2px solid var(--vermilion);
  background: linear-gradient(180deg, #4a2018, #2a1210);
  color: #f0d0c0;
  font: inherit;
  font-size: 0.95rem;
  letter-spacing: 0.28em;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, opacity 0.08s ease;
}
.ofuda-btn:active, .ofuda-btn.is-pressed { transform: scale(0.92); opacity: 0.88; }
.ofuda-btn.hidden { display: none; }
.ofuda-btn:disabled { opacity: 0.4; border-color: var(--muted); color: var(--muted); }

.dpad {
  grid-column: 1;
  justify-self: start;
  position: relative;
  width: 168px;
  height: 168px;
}
.d-btn {
  position: absolute;
  width: 56px;
  height: 56px;
  border: 1px solid #5a4030;
  background: linear-gradient(145deg, #3a2a20, #1e1510);
  color: var(--paper);
  font: inherit;
  font-size: 0.85rem;
  letter-spacing: 0.1em;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, opacity 0.08s ease;
  box-shadow: 0 3px 0 #0a0806;
}
.d-btn:active, .d-btn.is-pressed {
  transform: scale(0.92) translateY(2px);
  opacity: 0.88;
  box-shadow: none;
}
#btnUp { top: 0; left: 50%; margin-left: -28px; }
#btnDown { bottom: 0; left: 50%; margin-left: -28px; }
#btnLeft { top: 50%; left: 0; margin-top: -28px; }
#btnRight { top: 50%; right: 0; margin-top: -28px; }
.d-center {
  position: absolute;
  top: 50%; left: 50%;
  width: 36px; height: 36px;
  margin: -18px 0 0 -18px;
  border-radius: 50%;
  border: 1px solid #3a2a20;
  background: #15100c;
  pointer-events: none;
}

.text-btn {
  grid-column: 1 / -1;
  justify-self: center;
  min-height: 44px;
  padding: 0 14px;
  border: none;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.75rem;
  letter-spacing: 0.15em;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, opacity 0.08s ease;
}
.text-btn:active, .text-btn.is-pressed { transform: scale(0.92); opacity: 0.8; }

/* Overlay / scroll UI */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  padding: calc(12px + var(--safe-t)) 14px calc(12px + var(--safe-b));
  background: rgba(8, 6, 4, 0.92);
  transition: opacity 0.25s ease;
}
.overlay.hidden { opacity: 0; pointer-events: none; }

.scroll {
  width: min(100%, 380px);
  max-height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(180deg, #2a1e14 0%, #1a120c 100%);
  border: 2px solid var(--vermilion-deep);
  box-shadow: 0 0 0 1px var(--gold), 0 12px 40px rgba(0,0,0,0.6);
  padding: 22px 18px 16px;
  text-align: center;
  position: relative;
}
.scroll-edge {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 6px;
  background: repeating-linear-gradient(
    90deg,
    var(--vermilion) 0 12px,
    var(--vermilion-deep) 12px 24px
  );
}
.scroll h1 {
  font-size: 1.45rem;
  letter-spacing: 0.28em;
  color: var(--gold);
  margin-bottom: 10px;
  font-weight: normal;
  flex: 0 0 auto;
}
.journey {
  margin: -2px 0 10px;
  padding: 7px 8px 9px;
  flex: 0 0 auto;
  border-top: 1px solid rgba(201, 162, 39, 0.35);
  border-bottom: 1px solid rgba(201, 162, 39, 0.35);
  background:
    linear-gradient(90deg, rgba(232, 220, 200, 0.03), rgba(201, 162, 39, 0.08), rgba(232, 220, 200, 0.03));
}
.journey.hidden { display: none; }
.journey-caption {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 7px;
  font-size: 0.62rem;
  letter-spacing: 0.18em;
  color: var(--muted);
}
.journey-caption strong {
  overflow: hidden;
  color: var(--paper);
  font-size: 0.72rem;
  font-weight: normal;
  letter-spacing: 0.08em;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.journey-route {
  position: relative;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  align-items: center;
  gap: 3px;
}
.journey-route::before {
  content: "";
  position: absolute;
  top: 50%;
  right: 4%;
  left: 4%;
  height: 2px;
  background: #4a3828;
  transform: translateY(-50%);
}
.journey-seal {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  margin: auto;
  border: 1px solid #65513c;
  border-radius: 50%;
  background: #1a120c;
  color: #786a58;
  font-size: 0.62rem;
  line-height: 1;
}
.journey-seal.done {
  border-color: var(--vermilion);
  background: var(--vermilion-deep);
  color: #ffe7d7;
  box-shadow: 0 0 0 1px rgba(179, 58, 43, 0.28);
}
.journey-seal.current {
  border-color: var(--gold);
  color: var(--gold);
  box-shadow: 0 0 0 2px rgba(201, 162, 39, 0.18);
  animation: current-seal 1.5s ease-in-out infinite;
}
.journey-seal.done.current {
  color: #fff2cf;
  box-shadow: 0 0 0 2px rgba(201, 162, 39, 0.32);
}
@keyframes current-seal {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}
.ov-portraits {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 12px;
  margin: 4px 0 10px;
  height: 108px;
  flex: 0 0 auto;
}
.ov-portraits.solo .ov-vs,
.ov-portraits.solo #ovFoe {
  display: none;
}
.ov-portrait {
  width: 80px;
  height: 108px;
  object-fit: contain;
  object-position: bottom center;
  flex: 0 0 auto;
  filter: drop-shadow(0 4px 10px rgba(0,0,0,0.55));
  pointer-events: none;
  background: transparent;
}
.ov-portrait:not([src]),
.ov-portrait[src=""],
.score-icon:not([src]),
.score-icon[src=""] {
  visibility: hidden;
}
.ov-vs {
  align-self: center;
  font-size: 0.85rem;
  letter-spacing: 0.3em;
  color: var(--gold);
  padding-bottom: 28px;
}
.ov-body {
  font-size: 0.9rem;
  line-height: 1.65;
  text-align: left;
  white-space: pre-wrap;
  min-height: 0;
  flex: 1 1 auto;
  overflow: hidden;
  color: var(--paper);
  margin-bottom: 14px;
}
.primary-btn {
  flex: 0 0 auto;
  min-height: 52px;
  min-width: 70%;
  padding: 0 20px;
  border: 2px solid var(--gold);
  background: linear-gradient(180deg, var(--vermilion), var(--vermilion-deep));
  color: #fff8e8;
  font: inherit;
  font-size: 1.05rem;
  letter-spacing: 0.2em;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, opacity 0.08s ease;
}
.primary-btn:active, .primary-btn.is-pressed { transform: scale(0.92); opacity: 0.9; }
.hint {
  margin-top: 10px;
  font-size: 0.7rem;
  color: var(--muted);
  letter-spacing: 0.08em;
  flex: 0 0 auto;
}

.shake { animation: shake 0.28s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

@media (max-height: 700px) {
  .dpad { width: 156px; height: 156px; }
  .d-btn { width: 52px; height: 52px; }
  #btnUp, #btnDown { margin-left: -26px; }
  #btnLeft, #btnRight { margin-top: -26px; }
  .ofuda-btn { min-height: 52px; }
  .scroll { padding-top: 16px; padding-bottom: 12px; }
  .scroll h1 { margin-bottom: 6px; font-size: 1.2rem; }
  .journey { margin-bottom: 6px; padding-top: 4px; padding-bottom: 6px; }
  .ov-portraits { height: 84px; margin-bottom: 6px; }
  .ov-portrait { width: 62px; height: 84px; }
  .ov-body { font-size: 0.8rem; line-height: 1.45; margin-bottom: 10px; }
  .primary-btn { min-height: 48px; }
}

@media (max-height: 620px) {
  .dpad { width: 144px; height: 144px; }
  .d-btn { width: 48px; height: 48px; }
  #btnUp, #btnDown { margin-left: -24px; }
  #btnLeft, #btnRight { margin-top: -24px; }
  .ov-portraits { height: 64px; }
  .ov-portrait { width: 48px; height: 64px; }
}
