/* レイアウト：3Dの部屋を全面に敷き、UIは夜の部屋に浮かぶ小さな紙片やパネルとして四隅に置く */
:root {
  --night: #12152a;
  --night-2: #1d2244;
  --panel: rgba(16, 19, 38, 0.86);
  --line: rgba(246, 236, 217, 0.16);
  --ink: #f6ecd9;
  --muted: #b8b1c8;
  --gold: #f0bd52;
  --gold-ink: #2a1d05;
  --rose: #ff7a9c;
  --glow: #8affc8;
  --paper: #fbf5e6;
  --paper-ink: #2d2a3a;
  --display: 'Dela Gothic One', 'Hiragino Kaku Gothic StdN', 'Arial Black', sans-serif;
  --body: 'Zen Maru Gothic', 'Hiragino Maru Gothic ProN', 'Yu Gothic', 'Meiryo', sans-serif;
  --digital: 'DotGothic16', ui-monospace, 'Courier New', monospace;
  --hand: 'Klee One', 'Zen Maru Gothic', 'Hiragino Mincho ProN', serif;
  color-scheme: dark;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--night);
  color: var(--ink);
  font-family: var(--body);
  font-size: 15px;
  line-height: 1.7;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

button:focus-visible {
  outline: 2px solid var(--glow);
  outline-offset: 2px;
}

#app {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

#stage,
#stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ------------------------------------------------------------ HUD */
#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#hud > * {
  pointer-events: auto;
}

.hud-top {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 14px);
  left: 16px;
  right: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

.hud-top > * {
  pointer-events: auto;
}

.timer {
  font-family: var(--digital);
  font-size: 22px;
  letter-spacing: 0.08em;
  color: var(--glow);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 2px 12px;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 10px rgba(138, 255, 200, 0.5);
}

.hud-actions {
  display: flex;
  gap: 8px;
}

.chip {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 14px;
  font-weight: 700;
  backdrop-filter: blur(6px);
}

.chip:hover {
  border-color: var(--gold);
}

#btn-hint {
  color: var(--gold);
}

#crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 8px;
  height: 8px;
  margin: -4px 0 0 -4px;
  border-radius: 50%;
  background: rgba(246, 236, 217, 0.75);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4);
  pointer-events: none !important;
  transition: transform 0.15s, background 0.15s;
}

#crosshair.active {
  transform: scale(2.2);
  background: transparent;
  border: 1.5px solid var(--gold);
  box-shadow: none;
}

#target-label {
  position: absolute;
  left: 50%;
  top: calc(50% + 22px);
  transform: translateX(-50%);
  font-size: 14px;
  font-weight: 700;
  padding: 2px 10px;
  border-radius: 4px;
  background: rgba(16, 19, 38, 0.7);
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none !important;
}

#target-label.show {
  opacity: 1;
}

#target-label.far {
  color: var(--muted);
}

body[data-mode='drag'] #target-label,
body[data-mode='touch'] #target-label {
  top: auto;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 112px);
}

#resume {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, 40px);
  background: var(--panel);
  border: 1px solid var(--gold);
  border-radius: 999px;
  padding: 8px 20px;
  font-weight: 700;
  color: var(--gold);
}

#message {
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 104px);
  transform: translateX(-50%);
  width: min(600px, calc(100% - 32px));
  box-sizing: border-box;
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 3px solid var(--gold);
  border-radius: 4px 10px 10px 4px;
  padding: 12px 18px;
  font-size: 16px;
  line-height: 1.75;
  backdrop-filter: blur(8px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  /* 視点ドラッグやタップの邪魔をしない（時間がたつと自動で消える） */
  pointer-events: none !important;
}

#message.show {
  animation: rise 0.25s ease-out;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
}

#inventory {
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.slot {
  width: 74px;
  height: 74px;
  padding: 8px 4px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--gold);
  backdrop-filter: blur(6px);
}

.slot svg {
  width: 32px;
  height: 32px;
}

.slot span {
  font-size: 11px;
  line-height: 1.3;
  color: var(--ink);
  white-space: nowrap;
}

.slot:hover {
  border-color: var(--gold);
}

.slot.new {
  animation: pulse 1s ease-out;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(240, 189, 82, 0.9);
    transform: scale(1.12);
  }
  100% {
    box-shadow: 0 0 0 16px rgba(240, 189, 82, 0);
    transform: scale(1);
  }
}

#joystick {
  position: absolute;
  left: calc(env(safe-area-inset-left, 0px) + 22px);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 22px);
  width: 124px;
  height: 124px;
  border-radius: 50%;
  background: rgba(16, 19, 38, 0.45);
  border: 1.5px solid var(--line);
  touch-action: none;
}

#joystick .knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  margin: -27px 0 0 -27px;
  border-radius: 50%;
  background: rgba(246, 236, 217, 0.85);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}

#joystick.active {
  border-color: var(--gold);
}

/* スマホ：持ち物は右側に縦並び、メッセージはスティックの上 */
body[data-mode='touch'] #inventory {
  left: auto;
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  bottom: auto;
  top: calc(env(safe-area-inset-top, 0px) + 64px);
  transform: none;
  flex-direction: column;
}

body[data-mode='touch'] .slot {
  width: 62px;
  height: 62px;
}

body[data-mode='touch'] .slot svg {
  width: 26px;
  height: 26px;
}

body[data-mode='touch'] #message {
  bottom: calc(env(safe-area-inset-bottom, 0px) + 160px);
  font-size: 15px;
}

body[data-mode='touch'] .chip {
  padding: 6px 12px;
  font-size: 13px;
}

/* ------------------------------------------------------------ モーダル */
#modal {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(8, 9, 20, 0.6);
  backdrop-filter: blur(4px);
  z-index: 20;
}

.modal-card {
  position: relative;
  box-sizing: border-box;
  width: min(420px, 100%);
  max-height: calc(100% - 16px);
  overflow: auto;
  background: var(--night-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px 22px 22px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  animation: pop 0.2s ease-out;
  outline: none;
}

.modal-card.wide {
  width: min(760px, 100%);
}

@keyframes pop {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
}

#modal-title {
  margin: 0 36px 14px 0;
  font-family: var(--display);
  font-weight: 400;
  font-size: 20px;
  letter-spacing: 0.04em;
  color: var(--gold);
  text-wrap: balance;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: transparent;
  font-size: 22px;
  line-height: 1;
}

.modal-close:hover {
  border-color: var(--gold);
}

.text-modal p {
  margin: 0;
  font-size: 16px;
  white-space: pre-line;
}

.text-modal.item {
  display: flex;
  gap: 16px;
  align-items: center;
}

.item-icon {
  flex: none;
  width: 72px;
  height: 72px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: rgba(240, 189, 82, 0.12);
  color: var(--gold);
}

.item-icon svg {
  width: 48px;
  height: 48px;
}

.text-modal.hint p:first-child {
  font-size: 17px;
}

.pad-hint {
  margin: 12px 0 0;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
}

.paper {
  background: var(--paper);
  color: var(--paper-ink);
  font-family: var(--hand);
  font-weight: 600;
  font-size: 17px;
  line-height: 2;
  white-space: pre-line;
  padding: 18px 22px;
  border-radius: 4px;
  background-image: repeating-linear-gradient(transparent 0 33px, rgba(70, 90, 140, 0.18) 33px 34px);
  background-position: 0 14px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.06), 0 6px 18px rgba(0, 0, 0, 0.3);
  transform: rotate(-0.6deg);
}

.painting {
  margin: 0;
}

.painting img {
  display: block;
  width: 100%;
  border-radius: 4px;
  border: 10px solid #c79a46;
  box-sizing: border-box;
}

.painting figcaption {
  margin-top: 12px;
  font-size: 15px;
  color: var(--muted);
}

/* テンキー */
.keypad {
  display: grid;
  gap: 4px;
  justify-items: center;
}

.lcd {
  width: 100%;
  max-width: 260px;
  box-sizing: border-box;
  text-align: center;
  font-family: var(--digital);
  font-size: 40px;
  letter-spacing: 0.3em;
  padding: 6px 0 6px 0.3em;
  color: #5cff9d;
  background: #0b1f16;
  border-radius: 8px;
  border: 1px solid rgba(92, 255, 157, 0.25);
  text-shadow: 0 0 12px rgba(92, 255, 157, 0.6);
  font-variant-numeric: tabular-nums;
}

.lcd.err {
  color: #ff6b6b;
  border-color: rgba(255, 107, 107, 0.4);
  text-shadow: 0 0 12px rgba(255, 107, 107, 0.6);
}

.lcd.ok {
  color: var(--glow);
}

.keypad .pad-hint {
  margin: 4px 0 8px;
}

.keys {
  display: grid;
  grid-template-columns: repeat(3, 72px);
  gap: 10px;
}

.keys button {
  height: 58px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: linear-gradient(#e5e1d8, #c9c4b8);
  color: #23222a;
  font-size: 22px;
  font-weight: 700;
  box-shadow: 0 3px 0 #8d887d;
}

.keys button:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #8d887d;
}

.keys .ok-key {
  background: linear-gradient(#6fe0a0, #3fae70);
  color: #08240f;
  box-shadow: 0 3px 0 #23744a;
}

.keys .clear-key {
  background: linear-gradient(#f3b3b3, #d98686);
  box-shadow: 0 3px 0 #9c5555;
}

/* 金庫 */
.safe {
  display: grid;
  justify-items: center;
}

.safe.shake {
  animation: shake 0.4s;
}

@keyframes shake {
  20%,
  60% {
    transform: translateX(-8px);
  }
  40%,
  80% {
    transform: translateX(8px);
  }
}

.dials {
  display: flex;
  gap: 10px;
  padding: 14px;
  background: linear-gradient(#4b5260, #2c313b);
  border-radius: 12px;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.1);
}

.dial {
  display: grid;
  gap: 6px;
  justify-items: center;
}

.dial-btn {
  width: 56px;
  height: 40px;
  border-radius: 8px;
  border: none;
  background: rgba(255, 255, 255, 0.12);
  font-size: 16px;
}

.dial-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.dial-digit {
  width: 56px;
  height: 66px;
  display: grid;
  place-items: center;
  font-family: var(--digital);
  font-size: 44px;
  color: #5cff9d;
  background: #0b1f16;
  border-radius: 6px;
  text-shadow: 0 0 10px rgba(92, 255, 157, 0.55);
}

.safe .primary {
  margin-top: 14px;
}

/* 数字カード */
.card-list {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
}

.num-card {
  width: 76px;
  display: grid;
  justify-items: center;
  gap: 4px;
}

.num-card .num {
  width: 64px;
  height: 88px;
  display: grid;
  place-items: center;
  font-family: var(--display);
  font-size: 46px;
  color: #26262e;
  background: #fffaf0;
  border: 6px solid var(--card);
  border-radius: 6px;
}

.num-card .cap {
  font-size: 12px;
  color: var(--muted);
}

/* ------------------------------------------------------------ ボタン */
.primary {
  border: none;
  border-radius: 999px;
  padding: 12px 34px;
  background: var(--gold);
  color: var(--gold-ink);
  font-weight: 700;
  font-size: 17px;
  letter-spacing: 0.06em;
  box-shadow: 0 6px 24px rgba(240, 189, 82, 0.35);
  transition: transform 0.15s, box-shadow 0.15s;
}

.primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 30px rgba(240, 189, 82, 0.5);
}

.primary:disabled {
  opacity: 0.6;
  cursor: default;
}

/* ------------------------------------------------------------ タイトル・エンディング */
.screen {
  position: absolute;
  inset: 0;
  z-index: 10;
  overflow: auto;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  padding: calc(env(safe-area-inset-top, 0px) + 24px) 16px calc(env(safe-area-inset-bottom, 0px) + 24px);
}

#start {
  background:
    linear-gradient(90deg, rgba(14, 16, 34, 0.94) 0%, rgba(14, 16, 34, 0.82) 46%, rgba(14, 16, 34, 0.25) 100%);
}

.start-card {
  width: min(520px, 100%);
  margin: auto auto auto max(0px, 6vw);
}

.eyebrow {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.28em;
  color: var(--glow);
}

.logo {
  margin: 4px 0 10px;
  font-family: var(--display);
  font-weight: 400;
  font-size: clamp(56px, 13vw, 104px);
  line-height: 0.95;
  letter-spacing: 0.02em;
}

.logo span {
  color: var(--gold);
  text-shadow: 0 0 30px rgba(240, 189, 82, 0.45);
}

.lede {
  margin: 0 0 18px;
  font-size: 16px;
  color: var(--ink);
  max-width: 34em;
}

.howto {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}

.howto > div {
  border-top: 1px solid var(--line);
  padding-top: 8px;
  min-width: 0;
}

/* 今使っている端末の操作説明だけを出す */
body[data-mode='touch'] .howto-pc,
body:not([data-mode='touch']) .howto-touch {
  display: none;
}

body[data-mode] .howto {
  grid-template-columns: minmax(0, 1fr);
  max-width: 26em;
}

.howto h2 {
  margin: 0 0 4px;
  font-size: 12px;
  letter-spacing: 0.2em;
  color: var(--muted);
}

.howto dl {
  margin: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 10px;
  font-size: 14px;
}

.howto dt {
  color: var(--gold);
  font-weight: 700;
}

.howto dd {
  margin: 0;
}

kbd {
  display: inline-block;
  min-width: 1.4em;
  margin-right: 2px;
  padding: 0 4px;
  font-family: var(--digital);
  font-size: 12px;
  line-height: 1.6;
  text-align: center;
  color: var(--ink);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
}

.fineprint {
  margin: 14px 0 0;
  font-size: 13px;
  color: var(--muted);
  max-width: 34em;
}

.fineprint.error {
  color: #ff9a9a;
}

#end {
  background: radial-gradient(circle at 50% 30%, #3a2f5c 0%, #1a1d3a 55%, #0e1022 100%);
  justify-content: center;
}

#confetti {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.end-card {
  position: relative;
  width: min(560px, 100%);
  text-align: center;
  margin: auto;
}

.end-title {
  margin: 6px 0 4px;
  font-family: var(--display);
  font-weight: 400;
  font-size: clamp(44px, 11vw, 80px);
  line-height: 1.1;
  color: var(--ink);
}

.end-name {
  margin: 0 0 20px;
  font-family: var(--display);
  font-size: clamp(22px, 5.4vw, 34px);
  line-height: 1.3;
  color: var(--gold);
  text-shadow: 0 0 24px rgba(240, 189, 82, 0.4);
  text-wrap: balance;
}

.stats {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin: 0 0 18px;
}

.stats > div {
  min-width: 120px;
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(16, 19, 38, 0.5);
}

.stats dt {
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.1em;
}

.stats dd {
  margin: 0;
  font-family: var(--digital);
  font-size: 24px;
  color: var(--glow);
  font-variant-numeric: tabular-nums;
}

.end-msg {
  margin: 0 auto 22px;
  max-width: 28em;
  font-size: 16px;
}

#fade {
  position: absolute;
  inset: 0;
  background: #fff8e8;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.8s ease;
  z-index: 30;
}

#fade.on {
  opacity: 1;
}

@media (max-width: 560px) {
  #start {
    background: rgba(14, 16, 34, 0.86);
  }

  .start-card {
    margin: auto;
  }

  .howto {
    grid-template-columns: 1fr;
  }
}

/* 横向きのスマホなど、高さが足りない画面 */
@media (max-height: 520px) {
  .logo {
    font-size: 56px;
  }

  .lede {
    font-size: 14px;
    margin-bottom: 10px;
  }

  .howto {
    margin-bottom: 14px;
  }

  .howto dl {
    grid-template-columns: auto 1fr auto 1fr;
  }

  body[data-mode='touch'] #inventory {
    top: auto;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
    flex-direction: row;
  }

  body[data-mode='touch'] #message {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 88px);
    width: min(520px, calc(100% - 320px));
    min-width: 260px;
  }

  .end-title {
    font-size: 44px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
