:root {
  --ink: #140c1e;
  --panel: rgba(20, 12, 30, 0.82);
  --panel-solid: #1c1229;
  --line: rgba(243, 194, 74, 0.55);
  --gold: #f3c24a;
  --hozuki: #ff7a1a;
  --text: #fbf4e6;
  --dim: #b9aec8;
  --hp: #ff5a5a;
  --mp: #5ab4ff;
  --heal: #7dff9a;
  --font: "Zen Maru Gothic", system-ui, sans-serif;
  --brush: "Yuji Syuku", "Zen Maru Gothic", serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--ink); color: var(--text); font-family: var(--font); -webkit-tap-highlight-color: transparent; }
body { user-select: none; -webkit-user-select: none; touch-action: none; }
#gl { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
#labels { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 4; }

/* ---------- HUD ---------- */
.hud { position: fixed; top: calc(max(8px, env(safe-area-inset-top)) + 42px); left: 12px; z-index: 5; min-width: 250px; padding: 10px 14px; border-radius: 14px; background: var(--panel); border: 1px solid var(--line); box-shadow: 0 8px 24px rgba(0,0,0,.35); backdrop-filter: blur(6px); transition: opacity .3s; }
body[data-mode="title"] .hud, body[data-mode="loading"] .hud { opacity: 0; pointer-events: none; }
.hud-top { display: flex; align-items: center; gap: 10px; font-size: 14px; margin-bottom: 6px; }
.hud-top strong { font-size: 16px; font-weight: 900; letter-spacing: .04em; margin-right: auto; }
.hud-top .gold, .gold { color: var(--gold); font-weight: 900; }
.hud-bars { display: grid; grid-template-columns: auto 1fr auto; gap: 4px 8px; align-items: center; font-size: 12px; }
.hud-bars b { font-variant-numeric: tabular-nums; font-size: 12px; min-width: 52px; text-align: right; }
.bar { display: block; height: 9px; border-radius: 99px; background: rgba(255,255,255,.12); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: inherit; transition: width .35s ease; }
.bar.hp i { background: linear-gradient(90deg, #ff3d5a, #ff9a5a); }
.bar.mp i { background: linear-gradient(90deg, #3a7bff, #6fd6ff); }
.chip { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: #3a7bff; color: #fff; font-weight: 700; }

.enemy-bar { position: fixed; top: max(10px, env(safe-area-inset-top)); right: 12px; z-index: 5; width: min(320px, 46vw); padding: 10px 14px; border-radius: 14px; background: var(--panel); border: 1px solid rgba(255,90,90,.55); display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; align-items: center; }
.enemy-bar strong { font-weight: 900; }
.enemy-bar small { color: var(--dim); grid-column: 1; grid-row: 2; }
.enemy-bar b { grid-column: 2; grid-row: 1; font-size: 12px; font-variant-numeric: tabular-nums; }
.enemy-bar .bar { grid-column: 1 / -1; }

.banner { position: fixed; top: 22%; left: 50%; transform: translate(-50%, -10px); z-index: 6; font-family: var(--brush); font-size: clamp(34px, 7vw, 64px); color: #fff; text-shadow: 0 0 18px rgba(255,140,40,.9), 0 3px 0 rgba(0,0,0,.4); opacity: 0; pointer-events: none; letter-spacing: .1em; white-space: nowrap; }
.banner.show { animation: banner 2.2s ease forwards; }
@keyframes banner { 0% { opacity: 0; transform: translate(-50%, 10px); letter-spacing: .4em; } 20%, 70% { opacity: 1; transform: translate(-50%, 0); letter-spacing: .12em; } 100% { opacity: 0; transform: translate(-50%, -8px); } }

.hint { position: fixed; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%); z-index: 5; padding: 6px 14px; border-radius: 99px; background: rgba(20,12,30,.78); border: 1px solid var(--line); font-size: 14px; font-weight: 700; animation: hint 1.6s ease-in-out infinite; }
.hint kbd { font-family: var(--font); background: var(--gold); color: var(--ink); border-radius: 6px; padding: 1px 6px; margin-right: 4px; font-size: 12px; }
@keyframes hint { 50% { transform: translateX(-50%) translateY(-3px); } }

/* ---------- メッセージとコマンド ---------- */
.dock { position: fixed; left: 50%; bottom: calc(12px + var(--safe-b)); transform: translateX(-50%); z-index: 7; width: min(760px, calc(100% - 24px)); padding: 12px 14px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line); box-shadow: 0 10px 30px rgba(0,0,0,.45); backdrop-filter: blur(6px); }
.dock::before { content: ""; position: absolute; inset: 4px; border-radius: 12px; border: 1px solid rgba(243,194,74,.18); pointer-events: none; }
.message { margin: 0 0 10px; min-height: 2.9em; line-height: 1.45; font-size: 16px; font-weight: 700; }
.message.pulse { animation: pulse .35s ease; }
@keyframes pulse { from { opacity: .3; transform: translateY(3px); } }
.commands { display: flex; flex-wrap: wrap; gap: 8px; }
.commands button, .use-btn { font: 700 15px/1 var(--font); color: var(--text); background: linear-gradient(#35244a, #251836); border: 1px solid rgba(243,194,74,.45); border-radius: 10px; padding: 11px 16px; cursor: pointer; transition: transform .08s, background .15s, opacity .15s; }
.commands button:hover:not(:disabled), .use-btn:hover:not(:disabled) { background: linear-gradient(#4a3263, #33214a); }
.commands button:active:not(:disabled) { transform: translateY(1px) scale(.98); }
.commands button.primary { background: linear-gradient(#ff9a3a, #e8601a); border-color: #ffd08a; color: #2a1206; }
.commands button:disabled, .use-btn:disabled { opacity: .38; cursor: default; }
.commands button { counter-increment: cmd; position: relative; }
.commands { counter-reset: cmd; }
@media (pointer: fine) { .commands button::after { content: counter(cmd); position: absolute; top: -7px; right: -5px; font-size: 10px; background: var(--ink); border: 1px solid var(--line); color: var(--gold); border-radius: 99px; padding: 2px 5px; } }

/* ---------- オーバーレイ ---------- */
#overlay { position: fixed; inset: 0; z-index: 6; display: none; pointer-events: none; }
#overlay.show { display: flex; align-items: center; justify-content: center; padding: 84px 16px calc(170px + var(--safe-b)); }
#overlay.show > * { pointer-events: auto; }
.title-card { display: flex; align-items: center; gap: 24px; max-width: 760px; padding: 24px 28px; border-radius: 22px; background: linear-gradient(135deg, rgba(20,12,30,.8), rgba(60,20,30,.72)); border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0,0,0,.5); animation: rise .8s ease both; }
.title-card img { width: 180px; height: 180px; object-fit: contain; filter: drop-shadow(0 10px 20px rgba(0,0,0,.5)); animation: bob 3s ease-in-out infinite; }
.title-card h1 { margin: 4px 0 8px; font-family: var(--brush); font-weight: 400; font-size: clamp(36px, 6vw, 58px); line-height: 1.1; color: #fff; text-shadow: 0 0 22px rgba(255,122,26,.75); }
.title-card .eyebrow { margin: 0; color: var(--gold); font-weight: 900; letter-spacing: .2em; font-size: 13px; }
.title-card .lead { margin: 0 0 10px; font-weight: 700; }
.title-card .keys { margin: 0; font-size: 12px; color: var(--dim); }
@keyframes rise { from { opacity: 0; transform: translateY(20px) scale(.97); } }
@keyframes bob { 50% { transform: translateY(-8px) rotate(-2deg); } }

#overlay.sheet { background: rgba(10,6,16,.45); }
.sheet-grid { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: minmax(0, 1fr); gap: 12px; width: min(980px, 100%); height: 100%; }
.panel-block { background: var(--panel-solid); border: 1px solid var(--line); border-radius: 16px; padding: 14px; overflow: auto; max-height: 100%; animation: rise .3s ease both; }
.shop-block { width: min(560px, 100%); max-height: 100%; }
.panel-block h3 { margin: 0 0 10px; font-size: 16px; display: flex; justify-content: space-between; }
.item-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.item-row { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; padding: 8px; border-radius: 12px; background: rgba(255,255,255,.04); border: 1px solid transparent; }
.item-row.equipped { border-color: rgba(243,194,74,.6); background: rgba(243,194,74,.08); }
.item-row.off { opacity: .45; }
.item-row img { width: 44px; height: 44px; object-fit: contain; }
.item-head { display: flex; gap: 8px; align-items: baseline; }
.item-head strong { font-size: 14px; }
.qty { font-size: 12px; color: var(--gold); }
.item-text small { display: block; font-size: 12px; line-height: 1.4; }
.effect { color: var(--heal); }
.note { color: var(--dim); }
.use-btn { padding: 8px 10px; font-size: 13px; white-space: nowrap; }
.use-btn.on { background: linear-gradient(#ffcf6a, #e8a51a); color: var(--ink); }
.equip-grid, .stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 8px; }
.stat-grid { grid-template-columns: repeat(4, 1fr); }
.equip, .stat { background: rgba(255,255,255,.05); border-radius: 10px; padding: 6px 8px; font-size: 12px; }
.equip span, .stat span { display: block; color: var(--dim); font-size: 11px; }
.stat b { font-size: 18px; }
.stat em { font-style: normal; font-size: 10px; color: var(--gold); margin-left: 4px; }
.skill-line { margin: 0 0 6px; padding: 6px 8px; border-radius: 10px; background: rgba(255,255,255,.04); font-size: 13px; display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.skill-line small { flex-basis: 100%; color: var(--dim); font-size: 12px; }
.skill-line.off { opacity: .45; }
.foot-note { font-size: 12px; color: var(--dim); margin: 8px 0 0; }

#overlay.ending { background: radial-gradient(ellipse at center, rgba(40,15,10,.35), rgba(10,4,12,.7)); }
.ending-card { max-width: 560px; text-align: center; padding: 20px 24px; border-radius: 22px; background: rgba(20,12,30,.8); border: 1px solid var(--line); animation: rise 1s ease both; }
.ending-card img { width: 100%; max-height: 34vh; object-fit: cover; border-radius: 14px; }
.ending-card h2 { font-family: var(--brush); font-weight: 400; font-size: 40px; margin: 12px 0 4px; color: #fff; text-shadow: 0 0 20px rgba(255,140,40,.9); }
.ending-card p { line-height: 1.7; margin: 6px 0; }
.ending-card .dim { color: var(--dim); font-size: 13px; }

/* ---------- ダメージ数値 ---------- */
.pop3d { position: absolute; left: 0; top: 0; font: 900 28px/1 var(--font); color: #fff; -webkit-text-stroke: 1.5px #2a0a0a; paint-order: stroke fill; text-shadow: 0 3px 0 rgba(0,0,0,.35); white-space: nowrap; will-change: transform, opacity; }
.pop3d.crit { color: #ffd84a; font-size: 38px; }
.pop3d.hurt { color: #ff6a6a; }
.pop3d.heal { color: var(--heal); }
.pop3d.mp { color: #7fd0ff; }

/* ---------- タッチパッド ---------- */
.touch-pad { display: none; }
@media (pointer: coarse) {
  .touch-pad { position: fixed; z-index: 8; left: 0; right: 0; bottom: calc(150px + var(--safe-b)); display: flex; justify-content: space-between; align-items: flex-end; padding: 0 14px; pointer-events: none; }
  body[data-mode="explore"] .touch-pad { display: flex; }
  body:not([data-mode="explore"]) .touch-pad { display: none; }
  .touch-pad button { pointer-events: auto; width: 54px; height: 54px; border-radius: 16px; border: 1px solid var(--line); background: rgba(20,12,30,.62); color: var(--text); font-size: 18px; backdrop-filter: blur(4px); touch-action: none; }
  .touch-pad button:active { background: rgba(255,122,26,.55); }
  .dpad { display: grid; grid-template-columns: repeat(3, 54px); grid-template-rows: repeat(3, 54px); gap: 4px; }
  .dpad [data-dir="up"] { grid-area: 1 / 2; }
  .dpad [data-dir="left"] { grid-area: 2 / 1; }
  .dpad [data-dir="right"] { grid-area: 2 / 3; }
  .dpad [data-dir="down"] { grid-area: 3 / 2; }
  .acts { display: flex; gap: 10px; align-items: flex-end; }
  .acts .ok { width: 70px; height: 70px; border-radius: 50%; background: linear-gradient(#ff9a3a, #e8601a); color: #2a1206; font-weight: 900; font-size: 22px; }
  .hint { bottom: calc(330px + var(--safe-b)); }
}

@media (max-width: 720px) {
  .hud { left: 8px; right: 8px; min-width: 0; padding: 8px 10px; }
  .enemy-bar { top: calc(max(8px, env(safe-area-inset-top)) + 42px); }
  body[data-mode="battle"] .hud, body[data-mode="encounter"] .hud { top: auto; bottom: calc(140px + var(--safe-b)); }
  .enemy-bar { left: 8px; right: 8px; width: auto; }
  .dock { width: calc(100% - 16px); bottom: calc(8px + var(--safe-b)); padding: 10px; }
  .message { font-size: 14px; min-height: 2.9em; }
  .commands button { padding: 10px 12px; font-size: 14px; flex: 1 1 auto; }
  .title-card { flex-direction: column; text-align: center; gap: 8px; padding: 18px; }
  .title-card img { width: 120px; height: 120px; }
  .sheet-grid { grid-template-columns: 1fr; grid-template-rows: auto; overflow: auto; }
  #overlay.show { padding: 76px 8px calc(160px + var(--safe-b)); }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- 読み込み ---------- */
.loading { position: fixed; inset: 0; z-index: 20; display: grid; place-content: center; gap: 12px; text-align: center; background: var(--ink); transition: opacity .6s; }
body.ready .loading { opacity: 0; pointer-events: none; }
.spin { width: 46px; height: 46px; margin: auto; border-radius: 50%; border: 4px solid rgba(255,122,26,.25); border-top-color: var(--hozuki); animation: spin 1s linear infinite; box-shadow: 0 0 24px rgba(255,122,26,.6); }
@keyframes spin { to { transform: rotate(360deg); } }
.nogl { position: fixed; inset: 0; z-index: 30; display: grid; place-content: center; padding: 24px; text-align: center; background: var(--ink); line-height: 1.8; }
.nogl[hidden] { display: none; }
@media (prefers-reduced-motion: reduce) { .title-card img, .hint { animation: none; } }
