/* 一筆ダンジョン 3D版 UI — 机に広げた和紙と墨の立体ダンジョン
   役割色: 朱=危険のみ / 金=出口と巻物の軸蓋 / 群青=主人公のみ。UIは和紙・墨・木だけで組む。
   外部読み込みなし（画像はすべてCSSグラデーションとインラインSVG）。 */

:root {
  --washi: #EFE6D2;
  --washi-2: #E8DEC8;
  --washi-hi: #F7F3EA;
  --sumi: #1E1B1A;
  --sumi-soft: rgba(30, 27, 26, .62);
  --sumi-faint: rgba(30, 27, 26, .22);
  --wood: #3B2A20;
  --wood-hi: #5A4232;
  --gold: #F2B636;
  --kasa: #D9A441;
  --night-1: #171A24;
  --night-2: #0E1016;
  --font-ui: "Hiragino Sans", "Yu Gothic", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  --font-brush: "Hiragino Mincho ProN", "Yu Mincho", "AppleMyungjo", "Nanum Myeongjo", serif;
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  /* 筆で引いたような不揃いの角 */
  --brush-radius: 14px 6px 12px 5px / 6px 12px 5px 14px;
  --kasa-full: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 28 28'%3E%3Cpath d='M2.5 19.5 14 7l11.5 12.5Q14 23.5 2.5 19.5Z' fill='none' stroke='%23EFE6D2' stroke-width='4.5' stroke-linejoin='round'/%3E%3Cpath d='M2.5 19.5 14 7l11.5 12.5Q14 23.5 2.5 19.5Z' fill='%23D9A441' stroke='%231E1B1A' stroke-width='1.6' stroke-linejoin='round'/%3E%3Cpath d='M14 7 9.5 20.6M14 7l4.5 13.6' stroke='%231E1B1A' stroke-opacity='.35' stroke-width='1' fill='none'/%3E%3Ccircle cx='14' cy='6.6' r='1.7' fill='%231E1B1A'/%3E%3C/svg%3E");
  --kasa-lost: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 28 28'%3E%3Cpath d='M2.5 19.5 14 7l11.5 12.5Q14 23.5 2.5 19.5Z' fill='none' stroke='%231E1B1A' stroke-width='3.6' stroke-linejoin='round'/%3E%3Cpath d='M2.5 19.5 14 7l11.5 12.5Q14 23.5 2.5 19.5Z' fill='none' stroke='%23EFE6D2' stroke-width='1.5' stroke-linejoin='round' stroke-dasharray='3 2.2'/%3E%3C/svg%3E");
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--night-2);
  color: var(--sumi);
  font-family: var(--font-ui);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
:lang(ko) { word-break: keep-all; }

/* ───────── 画面と盤面 ───────── */

#game {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  isolation: isolate;
  background: linear-gradient(180deg, var(--night-1) 0%, var(--night-2) 100%);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#stage {
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
}

#gl, #canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  outline: none;
}
#gl { z-index: 0; pointer-events: none; }
#canvas { z-index: 1; touch-action: none; cursor: crosshair; }

/* ───────── HUD（上部の8%に収める。盤面への入力は素通し） ───────── */

header {
  position: absolute;
  z-index: 3;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: calc(var(--sat) + 6px) calc(var(--sar) + 10px) 0 calc(var(--sal) + 12px);
  pointer-events: none;
}
header > * { flex: 0 0 auto; }

/* 階数: 墨の文字に和紙の縁、下に一筆の払い */
.mark {
  display: flex;
  align-items: baseline;
  gap: 4px;
  font-size: 0; /* 包みの中の飾り文字は出さず、階数だけを見せる */
}
#floor {
  position: relative;
  display: inline-block;
  padding: 0 4px 5px 2px;
  font-family: var(--font-ui);
  font-size: 28px;
  font-weight: 800;
  line-height: 1;
  letter-spacing: .02em;
  font-variant-numeric: tabular-nums;
  color: var(--sumi);
  -webkit-text-stroke: 5px var(--washi);
  paint-order: stroke fill;
  text-shadow: 0 2px 0 rgba(14, 16, 22, .35);
  transform: rotate(-2deg);
  transform-origin: left bottom;
}
#floor::after {
  /* 墨の払い: 入りが太く、右へ細く抜ける */
  content: "";
  position: absolute;
  left: 0;
  right: -10px;
  bottom: -3px;
  height: 7px;
  background: linear-gradient(90deg, var(--washi) 0%, var(--washi) 55%, rgba(239, 230, 210, .55) 100%);
  clip-path: polygon(0 35%, 6% 0, 38% 18%, 72% 38%, 100% 52%, 70% 72%, 34% 92%, 5% 100%);
  filter: drop-shadow(0 1px 0 rgba(14, 16, 22, .4));
}

/* 任意の題字（親が #title を置いた場合だけ。狭い画面では出さない） */
#title {
  margin-left: 6px;
  font-family: var(--font-brush);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: .14em;
  color: var(--washi);
  opacity: .82;
  white-space: nowrap;
  text-shadow: 0 1px 0 rgba(14, 16, 22, .6);
}
@media (max-width: 479px) { #title { display: none; } }

/* 笠: 残りは塗り、失った分は輪郭だけ */
#hearts {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 2px;
  min-height: 28px;
  /* 子要素を置かず文字で渡された場合の見え方 */
  font-size: 22px;
  line-height: 1;
  letter-spacing: 2px;
  color: var(--kasa);
  -webkit-text-stroke: 3px var(--sumi);
  paint-order: stroke fill;
}
#hearts > * {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  font-size: 0;
  background: var(--kasa-full) center / contain no-repeat;
  transform-origin: 50% 30%;
  transition: opacity .2s ease;
}
#hearts > .lost,
#hearts > .off,
#hearts > .empty,
#hearts > [data-lost="true"],
#hearts > [data-state="lost"] {
  background-image: var(--kasa-lost);
  opacity: .3;
}
/* 朱に触れた区間を描いた瞬間: 笠が1つ震える */
#hearts > .shake,
#hearts.shake > :not(.lost):not(.off):not(.empty) {
  animation: kasa-shake .32s linear 2;
}
/* 被弾: 笠が落ちて輪郭になる */
#hearts > .drop {
  animation: kasa-drop .46s cubic-bezier(.5, 0, .9, .5) both;
}
@keyframes kasa-shake {
  0%, 100% { transform: rotate(0); }
  20% { transform: rotate(-13deg) translateY(-1px); }
  50% { transform: rotate(11deg); }
  80% { transform: rotate(-6deg) translateY(1px); }
}
@keyframes kasa-drop {
  0% { background-image: var(--kasa-full); opacity: 1; transform: translateY(0) rotate(0); }
  70% { background-image: var(--kasa-full); opacity: .9; transform: translateY(16px) rotate(38deg); }
  71% { background-image: var(--kasa-lost); opacity: 0; transform: translateY(0) rotate(0); }
  100% { background-image: var(--kasa-lost); opacity: .3; transform: translateY(0) rotate(0); }
}

/* 右上の道具: 消音と言語。各36pxの和紙の丸札 */
.tools {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: 2px;
}
#sound, #language {
  pointer-events: auto;
  height: 36px;
  margin: 0;
  border: 1.5px solid var(--sumi);
  color: var(--sumi);
  background:
    radial-gradient(120% 90% at 30% 20%, var(--washi-hi) 0%, transparent 60%),
    var(--washi);
  box-shadow: 0 2px 0 rgba(14, 16, 22, .45);
  font-family: var(--font-ui);
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition: transform .08s ease, box-shadow .08s ease, opacity .15s ease;
}
#sound {
  position: relative;
  width: 36px;
  padding: 0;
  border-radius: 50% 46% 52% 48% / 48% 52% 46% 50%;
  font-size: 18px;
  overflow: hidden;
}
#sound::after {
  /* 消音: 墨の斜め一太刀 */
  content: "";
  position: absolute;
  left: 4px;
  right: 4px;
  top: 50%;
  height: 3px;
  margin-top: -1.5px;
  background: var(--sumi);
  border-radius: 3px 1px 1px 3px;
  transform: rotate(-42deg) scaleX(0);
  transform-origin: 0 50%;
  transition: transform .14s ease-out;
}
#sound.muted, #sound.off, #sound[data-muted="true"] { color: var(--sumi-soft); }
#sound.muted::after, #sound.off::after, #sound[data-muted="true"]::after {
  transform: rotate(-42deg) scaleX(1);
}
#language {
  min-width: 36px;
  max-width: 84px;
  padding: 0 9px;
  border-radius: 18px 15px 18px 14px / 16px 18px 15px 18px;
  font-size: 13px;
  text-align: center;
  text-align-last: center;
  text-overflow: ellipsis;
}
@media (pointer: coarse) { #language { font-size: 16px; } } /* iOSの自動拡大を避ける */
#sound:active, #language:active {
  transform: translateY(2px);
  box-shadow: 0 0 0 rgba(14, 16, 22, .45);
}
#sound:focus-visible, #language:focus-visible {
  outline: 2px solid var(--washi-hi);
  outline-offset: 2px;
}
header > #sound { margin-left: 2px; }

/* ───────── 誘導と状態（空なら出さない。入力は素通し） ───────── */

#hint {
  position: absolute;
  z-index: 2;
  left: 50%;
  bottom: calc(var(--sab) + 46px);
  transform: translateX(-50%);
  max-width: min(86%, 420px);
  font-family: var(--font-brush);
  font-weight: 700;
  font-size: 15px;
  line-height: 1.4;
  letter-spacing: .08em;
  text-align: center;
  color: var(--washi);
  text-shadow: 0 1px 0 rgba(14, 16, 22, .8), 0 0 10px rgba(14, 16, 22, .7);
  pointer-events: none;
  animation: hint-breathe 2.2s ease-in-out infinite;
}
@keyframes hint-breathe {
  0%, 100% { opacity: .5; }
  50% { opacity: .95; }
}

footer {
  position: absolute;
  z-index: 3;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  padding: 0 calc(var(--sar) + 12px) calc(var(--sab) + 10px) calc(var(--sal) + 12px);
  pointer-events: none;
}
#status {
  /* 細い和紙の短冊に墨の一行 */
  display: inline-block;
  max-width: min(88%, 440px);
  padding: 5px 16px 6px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: .04em;
  text-align: center;
  color: var(--sumi);
  background:
    repeating-linear-gradient(96deg, rgba(59, 42, 32, .05) 0 1px, transparent 1px 6px),
    var(--washi);
  clip-path: polygon(1.5% 8%, 99% 0, 100% 88%, 97.5% 100%, 0 94%);
  animation: slip-in .18s ease-out both;
}
@keyframes slip-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}
#hint:empty, #status:empty, #saveHint:empty, #resultSub:empty, #resultLabel:empty { display: none; }

footer button {
  pointer-events: auto;
  min-height: 36px;
  padding: 0 14px;
  border: 1.5px solid var(--sumi);
  border-radius: var(--brush-radius);
  background: var(--washi);
  color: var(--sumi);
  font: 700 13px/1 var(--font-ui);
  cursor: pointer;
}

/* ───────── 結果: 暗くした盤の上に掛かる巻物の札 ───────── */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding:
    calc(var(--sat) + 26px) calc(var(--sar) + 14px)
    calc(var(--sab) + 26px) calc(var(--sal) + 14px);
  background:
    radial-gradient(90% 60% at 50% 45%, rgba(14, 16, 22, .58) 0%, rgba(14, 16, 22, .86) 100%);
  animation: dusk .28s ease-out both;
}
@keyframes dusk { from { opacity: 0; } to { opacity: 1; } }

.result-card {
  position: relative;
  flex: 0 0 auto;
  width: 86%;
  max-width: 430px;
  margin: auto; /* 収まれば中央、はみ出せば上からスクロール */
  padding: 22px 18px 20px;
  text-align: center;
  color: var(--sumi);
  background:
    radial-gradient(60% 40% at 22% 12%, rgba(247, 243, 234, .85) 0%, transparent 70%),
    radial-gradient(50% 35% at 85% 92%, rgba(59, 42, 32, .07) 0%, transparent 70%),
    repeating-linear-gradient(97deg, rgba(59, 42, 32, .04) 0 1px, transparent 1px 7px),
    repeating-linear-gradient(4deg, rgba(59, 42, 32, .025) 0 1px, transparent 1px 11px),
    linear-gradient(180deg, var(--washi) 0%, var(--washi-2) 100%);
  box-shadow:
    inset 0 0 0 1px rgba(59, 42, 32, .16),
    inset 0 10px 14px -10px rgba(59, 42, 32, .35),
    inset 0 -10px 14px -10px rgba(59, 42, 32, .35),
    0 18px 40px rgba(0, 0, 0, .5);
  -webkit-user-select: none;
  user-select: none;
  animation: unroll .42s cubic-bezier(.2, .8, .25, 1) both;
}
/* 巻物の軸: 木に金の蓋 */
.result-card::before,
.result-card::after {
  content: "";
  position: absolute;
  left: -10px;
  right: -10px;
  height: 14px;
  border-radius: 7px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, .28) 0%, transparent 38%, rgba(0, 0, 0, .34) 100%),
    linear-gradient(90deg,
      var(--gold) 0, var(--gold) 12px, var(--wood) 12px,
      var(--wood-hi) 50%,
      var(--wood) calc(100% - 12px), var(--gold) calc(100% - 12px), var(--gold) 100%);
  box-shadow: 0 3px 6px rgba(0, 0, 0, .4);
}
.result-card::before { top: -9px; }
.result-card::after { bottom: -9px; }
@keyframes unroll {
  from { clip-path: inset(48% -14px 48% -14px); transform: scale(.985); }
  to { clip-path: inset(-40px -14px -40px -14px); transform: none; }
}

#resultLabel, .result-card .small {
  font-family: var(--font-brush);
  font-weight: 700;
  font-size: 14px;
  line-height: 1.3;
  letter-spacing: .3em;
  text-indent: .3em;
  color: var(--sumi-soft);
}

#resultFloor {
  position: relative;
  display: inline-block;
  max-width: 100%;
  margin: 4px 0 14px;
  padding: 0 6px;
  font-family: var(--font-brush);
  font-weight: 800;
  font-size: min(96px, 16vw);
  line-height: 1.08;
  letter-spacing: .01em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--sumi);
  animation: ink-settle .5s .16s ease-out both;
}
#resultFloor::after {
  /* 階数の下を走る一筆 */
  content: "";
  position: absolute;
  left: -4%;
  right: -8%;
  bottom: -9px;
  height: 9px;
  background: linear-gradient(90deg, var(--sumi) 0%, var(--sumi) 62%, rgba(30, 27, 26, .5) 88%, rgba(30, 27, 26, .12) 100%);
  clip-path: polygon(0 40%, 4% 4%, 30% 14%, 66% 34%, 100% 50%, 68% 70%, 32% 92%, 3% 100%);
  transform-origin: 0 50%;
  animation: one-stroke .38s .34s cubic-bezier(.3, .7, .2, 1) both;
}
@keyframes ink-settle {
  from { opacity: 0; filter: blur(5px); transform: translateY(4px); }
  to { opacity: 1; filter: blur(0); transform: none; }
}
@keyframes one-stroke {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}

#resultSub {
  font-size: 14px;
  font-weight: 700;
  line-height: 1.4;
  letter-spacing: .08em;
  font-variant-numeric: tabular-nums;
  color: var(--sumi-soft);
}

/* 結果画像: 札に貼った一枚。長押し保存を妨げない */
#resultImage {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1200 / 675;
  object-fit: contain;
  margin: 14px 0 0;
  background: var(--night-1);
  border: 1.5px solid var(--sumi);
  box-shadow: 3px 4px 0 var(--sumi-faint);
  transform: rotate(-.6deg);
  -webkit-user-select: auto;
  user-select: auto;
  -webkit-touch-callout: default;
  -webkit-user-drag: auto;
}
#resultImage:not([src]), #resultImage[src=""] { display: none; }

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 18px;
}
.actions > * {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 48px;
  margin: 0;
  padding: 6px 10px;
  border: 1.5px solid var(--sumi);
  border-radius: var(--brush-radius);
  background: transparent;
  color: var(--sumi);
  font-family: var(--font-ui);
  font-size: 15px;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: .04em;
  text-align: center;
  text-decoration: none;
  overflow-wrap: anywhere;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition: transform .08s ease, background-color .12s ease, box-shadow .12s ease;
}
.actions > :nth-child(even) { border-radius: 6px 14px 5px 12px / 12px 6px 14px 5px; }

/* もう一度だけ墨の塗り */
#again {
  grid-column: 1 / -1;
  min-height: 54px;
  background:
    linear-gradient(100deg, rgba(247, 243, 234, .1) 0%, transparent 30%, transparent 78%, rgba(247, 243, 234, .08) 100%),
    var(--sumi);
  color: var(--washi-hi);
  font-family: var(--font-brush);
  font-size: 19px;
  font-weight: 800;
  letter-spacing: .16em;
  box-shadow: 0 3px 0 rgba(30, 27, 26, .3);
}
/* ほかのゲーム: 線だけの静かな出口 */
#more {
  grid-column: 1 / -1;
  border-style: dashed;
  border-color: var(--sumi-soft);
  font-size: 14px;
  color: var(--sumi);
}
@media (hover: hover) {
  .actions > :hover { background-color: rgba(30, 27, 26, .08); }
  #again:hover { background-color: #2A2730; box-shadow: 0 3px 0 rgba(30, 27, 26, .3), 0 0 0 3px rgba(30, 27, 26, .12); }
}
.actions > :active {
  transform: translateY(2px);
  background-color: rgba(30, 27, 26, .16); /* 墨がにじむ */
}
#again:active { background-color: #000; box-shadow: 0 1px 0 rgba(30, 27, 26, .3); }
.actions > :focus-visible {
  outline: 2px solid var(--sumi);
  outline-offset: 3px;
}
.actions > :disabled, .actions > [aria-disabled="true"] {
  opacity: .4;
  pointer-events: none;
}

#saveHint {
  margin: 12px 0 0;
  font-size: 12px;
  line-height: 1.4;
  letter-spacing: .06em;
  color: var(--sumi-soft);
}
#saveHint::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 3px;
  margin-right: 6px;
  vertical-align: middle;
  background: var(--sumi-soft);
  clip-path: polygon(0 0, 100% 40%, 100% 60%, 0 100%);
}

/* ───────── 画面ごとの調整 ───────── */

/* 狭いスマホ */
@media (max-width: 359px) {
  header { gap: 5px; padding-left: calc(var(--sal) + 8px); padding-right: calc(var(--sar) + 6px); }
  #floor { font-size: 25px; }
  #hearts > * { flex-basis: 25px; width: 25px; height: 25px; }
  #language { max-width: 68px; padding: 0 6px; }
  .result-card { width: 90%; padding: 20px 14px 18px; }
  .actions > * { font-size: 14px; }
}

/* 低い横長（スマホ横持ち）: 画像を左、記録と操作を右に並べる */
@media (orientation: landscape) and (max-height: 540px) {
  .overlay { padding-top: calc(var(--sat) + 20px); padding-bottom: calc(var(--sab) + 20px); }
  .result-card {
    width: 92%;
    max-width: 760px;
    display: grid;
    grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
    column-gap: 18px;
    align-items: center;
    align-content: center;
    padding: 16px 18px;
  }
  .result-card > * { grid-column: 2; }
  #resultImage { grid-column: 1; grid-row: 1 / span 6; margin: 0; align-self: center; }
  #resultFloor { justify-self: center; font-size: min(56px, 9vw); margin: 2px 0 10px; }
  .actions { margin-top: 10px; gap: 8px; }
  #again { min-height: 48px; font-size: 17px; }
  #saveHint { margin-top: 8px; }
  #hint { bottom: calc(var(--sab) + 40px); font-size: 14px; }
}

/* タブレットとPC */
@media (min-width: 700px) and (min-height: 541px) {
  header { padding-top: calc(var(--sat) + 12px); padding-left: calc(var(--sal) + 20px); padding-right: calc(var(--sar) + 18px); gap: 12px; }
  #floor { font-size: 34px; -webkit-text-stroke-width: 6px; }
  #hearts > * { flex-basis: 32px; width: 32px; height: 32px; }
  #hint { font-size: 17px; bottom: calc(var(--sab) + 56px); }
  #status { font-size: 14px; }
  .result-card { padding: 28px 26px 24px; }
  #resultFloor { font-size: 96px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
  #hint { opacity: .85; }
}
