/* 503 — APP BATTLE 特設ページと同じ暗い色調。Webフォントは読み込まない（外部通信ゼロ） */
:root {
  --bg: #07090e; --bg2: #0e1320; --panel: #121826; --line: #232c40; --fg: #e8eef8; --dim: #8a94aa; --dim2: #5c667c;
  --acc: #d4ff3a; --hot: #ff4f86; --cyan: #3ad4ff;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); font: 14px/1.6 var(--sans); overflow: hidden; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; }
.app { display: grid; grid-template-rows: auto auto 1fr auto auto; height: 100svh; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }

.hud { display: flex; align-items: center; gap: 16px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--bg2); }
.hud-stage { display: flex; align-items: baseline; gap: 10px; min-width: 0; flex: 1; }
.hud-no { color: var(--acc); font: 700 12px/1 var(--mono); letter-spacing: .12em; white-space: nowrap; }
.hud-stage b { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-stats { display: flex; gap: 14px; }
.stat { display: flex; align-items: baseline; gap: 6px; }
.stat small { color: var(--dim); font-size: 11px; }
.stat b { font: 700 16px/1 var(--mono); }
.stat b.over { color: var(--hot); }
.btn { border: 0; border-radius: 999px; padding: 9px 20px; font-weight: 800; cursor: pointer; }
.hud-actions { display: flex; gap: 8px; }
.btn-abort { background: transparent; box-shadow: inset 0 0 0 1px var(--line); color: var(--dim); }
.btn-help { width: 38px; padding: 0; background: transparent; box-shadow: inset 0 0 0 1px var(--line); color: var(--dim); }
.btn-run { background: var(--acc); color: #0b0e15; min-width: 96px; }
.btn-run.is-running { background: transparent; color: var(--fg); box-shadow: inset 0 0 0 1px var(--line); }
.btn:focus-visible, .part:focus-visible, .sel-tools button:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }
.goal-line { margin: 0; padding: 6px 16px; color: var(--dim); font-size: 13px; border-bottom: 1px solid var(--line); }

.stage-wrap { position: relative; min-height: 0; }
#stage { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: none; }   /* 枠いっぱいに重ねる（画素数で行の高さを押し広げない） */
.sel-tools { position: absolute; display: flex; gap: 6px; transform: translate(-50%, -100%); pointer-events: none; }   /* 入れ物の余白でステージのタップを横取りしない */
.sel-tools button { pointer-events: auto; white-space: nowrap; }
.sel-tools button { padding: 6px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--panel); font-size: 12px; font-weight: 700; cursor: pointer; }
.sel-tools button:last-child { color: var(--hot); }

.palette { display: flex; gap: 8px; padding: 10px 16px; overflow-x: auto; border-top: 1px solid var(--line); background: var(--bg2); }
.part { flex: none; display: grid; grid-template-columns: auto auto; align-items: center; gap: 4px 8px; padding: 6px 12px 6px 8px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); cursor: pointer; text-align: left; }
.part canvas { grid-row: span 2; width: 56px; height: 40px; }
.part b { font-size: 12px; }
.part small { color: var(--acc); font: 700 11px/1 var(--mono); }
.part[aria-pressed="true"] { border-color: var(--acc); box-shadow: 0 0 0 1px var(--acc), 0 0 18px rgba(212,255,58,.15); }
.part:disabled { opacity: .35; cursor: not-allowed; }
.hint { margin: 0; padding: 4px 16px 8px; color: var(--dim2); font-size: 12px; background: var(--bg2); }

.result { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; background: rgba(3,6,14,.7); }
.result-panel { width: min(460px, 100%); padding: 28px; border: 1px solid var(--line); border-radius: 16px; background: var(--panel); text-align: center; animation: rise .3s cubic-bezier(.2,.8,.2,1); }
@keyframes rise { from { transform: translateY(12px); opacity: 0; } }
.result-panel h2 { margin: 0 0 6px; font-size: 26px; }
.result-panel .stars { font-size: 34px; letter-spacing: .1em; color: var(--acc); }
.result-panel .stars i { color: var(--line); font-style: normal; }
.result-panel p { color: var(--dim); }
.result-panel .learn { margin: 16px 0; padding: 12px 14px; border-radius: 10px; background: var(--bg2); color: var(--fg); text-align: left; font-size: 13px; }
.result-panel .learn b { display: block; color: var(--cyan); font-size: 11px; letter-spacing: .1em; }
.result-actions { display: flex; gap: 10px; justify-content: center; margin-top: 18px; }
.result-actions .btn:not(.btn-run) { background: transparent; box-shadow: inset 0 0 0 1px var(--line); }

@media (max-width: 700px) {
  .hud { flex-wrap: wrap; gap: 8px 12px; }
  .hud-stage { flex-basis: 100%; }
  .hint { display: none; }
}

/* 解説カード */
.cards { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; background: rgba(3,6,14,.72); z-index: 5; }
.card { width: min(420px, 100%); padding: 24px; border: 1px solid var(--line); border-radius: 16px; background: var(--panel); animation: rise .3s cubic-bezier(.2,.8,.2,1); }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.card-new { padding: 2px 8px; border-radius: 999px; background: var(--hot); color: #0b0e15; font: 800 11px/1.4 var(--mono); }
.card-count { margin-left: auto; color: var(--dim2); font: 600 11px/1 var(--mono); }
.card-icon { display: block; margin: 4px 0 6px; }
.card h2 { margin: 0 0 8px; font-size: 22px; }
.card p { margin: 0 0 10px; color: var(--fg); }
.card .card-example { padding: 10px 12px; border-radius: 10px; background: var(--bg2); color: var(--dim); font-size: 13px; }
.card .card-example b { display: block; color: var(--cyan); font-size: 11px; letter-spacing: .1em; }
.card .btn { margin-top: 8px; width: 100%; }

/* 503 画面：本物のエラーページ風（白地・明朝・横線・サーバー名）＋下に解説 */
.page503 { width: min(760px, 100%); max-height: 100%; overflow-y: auto; border-radius: 6px; background: #fff; color: #000; box-shadow: 0 30px 80px rgba(0,0,0,.6); animation: blink503 .5s steps(2) 1; }
@keyframes blink503 { 0% { opacity: 0; } 50% { opacity: 1; } }
.page503-doc { padding: 34px 40px 10px; font-family: "Times New Roman", "Hiragino Mincho ProN", "Yu Mincho", serif; text-align: center; }
.page503-doc h1 { margin: 0 0 14px; font-size: clamp(26px, 5vw, 40px); font-weight: 700; }
.page503-doc hr { border: 0; border-top: 1px solid #c8c8c8; }
.page503-doc p { margin: 8px 0 0; font-size: 15px; }
.page503-note { padding: 22px 28px 28px; background: var(--panel); color: var(--fg); border-radius: 0 0 6px 6px; }
.page503-explain { margin: 0 0 6px; font-size: 16px; font-weight: 800; color: var(--acc); }
.page503-stat { margin: 0 0 14px; color: var(--dim); font: 600 12px/1.6 var(--mono); }
.page503-note dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; padding: 14px 16px; border-radius: 10px; background: var(--bg2); }
.page503-note dt { color: var(--hot); font-weight: 800; font-size: 12px; }
.page503-note dt:nth-of-type(2) { color: var(--cyan); }
.page503-note dd { margin: 0; font-size: 14px; }

.result-panel .score-big { margin: 4px 0; color: var(--acc); font: 900 46px/1.1 var(--mono); }
.result-panel .score-big small { font-size: 14px; color: var(--dim); }
.new-best { color: var(--hot); }

/* タイトル・ステージ選択・図鑑 */
.screen { position: fixed; inset: 0; overflow-y: auto; background: var(--bg); }
.title-screen { display: grid; place-items: center; overflow: hidden; }
#title-bg { position: absolute; inset: 0; width: 100%; height: 100%; }
.title-inner { position: relative; text-align: center; padding: 16px; }
.title-tag { margin: 0; color: var(--acc); font: 700 12px/1 var(--mono); letter-spacing: .2em; }
.title-logo { margin: 8px 0 0; color: #fff; font: 900 clamp(120px, 26vw, 260px)/1 var(--mono); letter-spacing: -.04em; text-shadow: 4px 0 0 rgba(255,79,134,.8), -4px 0 0 rgba(58,212,255,.7); animation: glitch 4s steps(1) infinite; }
@keyframes glitch { 0%, 92%, 100% { transform: none; } 93% { transform: translate(-6px, 2px) skewX(-6deg); } 95% { transform: translate(5px, -2px); } 97% { transform: translate(-2px, 1px) skewX(4deg); } }
.title-sub { margin: 4px 0 26px; font-size: clamp(18px, 3vw, 26px); font-weight: 800; }
.title-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.btn-big { padding: 16px 40px; font-size: 18px; }
.btn-ghost { background: transparent; box-shadow: inset 0 0 0 1px var(--line); color: var(--fg); }
.title-note { margin: 22px auto 0; max-width: 32em; color: var(--dim); font-size: 13px; }
.load-fail { margin: 18px auto 0; max-width: 30em; padding: 10px 14px; border-radius: 8px; background: #7a1f1f; color: #fff; font-size: 14px; line-height: 1.6; }
.menu-head { position: sticky; top: 0; display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: var(--bg2); border-bottom: 1px solid var(--line); z-index: 1; }
.menu-head h2 { margin: 0; font-size: 18px; }
.zukan-count { margin-left: auto; color: var(--acc); font: 700 14px/1 var(--mono); }
.menu-body { width: min(1040px, 100%); margin: 0 auto; padding: 20px 16px 40px; }
.chapter { margin-bottom: 28px; }
.chapter h2 { margin: 0 0 12px; font-size: 18px; }
.chapter h2 small { display: block; color: var(--dim); font-size: 12px; font-weight: 600; }
.stage-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.stage-card { display: grid; gap: 4px; padding: 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); text-align: left; cursor: pointer; transition: border-color .2s, transform .2s; }
.stage-card:hover:not(:disabled) { border-color: var(--acc); transform: translateY(-2px); }
.stage-card:disabled { opacity: .45; cursor: not-allowed; }
.stage-card.is-endless { border-color: rgba(255,79,134,.5); }
.sc-no { color: var(--acc); font: 700 11px/1 var(--mono); letter-spacing: .12em; }
.stage-card.is-endless .sc-no { color: var(--hot); }
.stage-card b { font-size: 17px; }
.sc-foot { color: var(--dim); font-size: 12px; }
.sc-stars { color: var(--acc); font-size: 16px; letter-spacing: .1em; }
.sc-stars i { color: var(--line); font-style: normal; }
.zukan-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.zk { padding: 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); }
.zk h3 { margin: 4px 0 6px; font-size: 16px; }
.zk p { margin: 0 0 8px; font-size: 13px; }
.zk .zk-ex { color: var(--dim); font-size: 12px; }
.zk .zk-ex b { display: block; color: var(--cyan); font-size: 10px; letter-spacing: .1em; }
.zk.is-locked { opacity: .5; }
.zk-q, .zk-503 { font: 900 40px/1 var(--mono); color: var(--line); }
.zk-503 { color: var(--hot); }
.zk.is-503 { border-color: rgba(255,79,134,.5); }
.btn-menu { width: 38px; padding: 6px 0; }
.speed { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.speed button { padding: 7px 10px; border: 0; background: transparent; font: 700 12px/1 var(--mono); cursor: pointer; }
.speed button[aria-pressed="true"] { background: var(--acc); color: #0b0e15; }
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); font-size: 13px; z-index: 6; }
.share-box { margin-top: 16px; padding: 14px; border-radius: 10px; background: var(--bg2); text-align: left; }
.share-box .result-actions { justify-content: flex-start; flex-wrap: wrap; margin-top: 0; }
.share-box input { width: 100%; margin-top: 10px; padding: 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--dim); font: 12px var(--mono); }
.result-actions { flex-wrap: wrap; }

.btn-sound { padding: 9px 12px; font-size: 12px; }
.btn-sound[aria-pressed="true"], #btn-sound-title[aria-pressed="true"] { color: var(--acc); box-shadow: inset 0 0 0 1px var(--acc); }

/* スマホ：縦画面ではステージを横スクロールできる大きさにして、マスを指で狙えるようにする */
@media (orientation: portrait) and (max-width: 900px) {
  .stage-wrap { overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; }
  #stage { position: static; width: 880px; height: auto; aspect-ratio: 16 / 9; touch-action: pan-x; }
  .app { grid-template-rows: auto auto auto auto auto; align-content: start; }
  .hud-stats { width: 100%; justify-content: space-between; }
  .hud-actions { margin-left: auto; }
}
/* スマホの横画面：高さが足りないので、説明の行とヒントを畳んでステージを広く */
@media (orientation: landscape) and (max-height: 520px) {
  .app { grid-template-rows: auto 1fr auto; }   /* 説明の行とヒントを畳んだぶん、ステージが伸びてパレットを押し出さないように */
  .goal-line, .hint { display: none; }
  .hud { padding: 4px 10px; }
  .palette { padding: 4px 10px; }
  .part { padding: 2px 8px 2px 4px; }
  .part canvas { width: 42px; height: 30px; }
  #stage { touch-action: none; }
}

.sel-name { display: none; }
.sel-tools.is-docked { position: fixed; left: 12px; top: auto; bottom: 120px; transform: none; align-items: center; padding: 6px 6px 6px 12px; border: 1px solid var(--line); border-radius: 999px; background: rgba(14,19,32,.95); pointer-events: auto; z-index: 3; }
.sel-tools.is-docked .sel-name { display: inline; margin-right: 4px; color: var(--cyan); font-size: 12px; font-weight: 700; white-space: nowrap; }
@media (orientation: landscape) and (max-height: 520px) { .sel-tools.is-docked { bottom: 64px; } }

/* ==========================================================================
   盤の世界（2026-11-03 決定）：机の上に立てた盤。まわりは机の上の物
   （ステージの札・お金・手札のカード・赤い押しボタン）。以前の色は上で定義したまま、ここで上書きする
   ========================================================================== */
:root {
  --desk: #2a201a; --desk2: #3a2d24; --paper: #f6efe1; --paper2: #ece2cd; --ink: #2b2118; --ink2: #6a5a48;
  --gold: #e0a826; --gold2: #ffd34d; --red: #d93a25; --red2: #9a1f10; --cream: #f3ead8; --cream2: rgba(243,234,216,.62);
}
html, body { background: radial-gradient(120% 90% at 50% 15%, #4a3a2e 0%, #2a201a 55%, #140f0c 100%) fixed; color: var(--cream); }
.screen { background: transparent; }

/* 上のバー：ステージの札＋数字（机の上） */
.hud { background: transparent; border-bottom: 0; padding: 12px 16px 6px; }
.hud-stage { flex: 0 1 auto; align-items: baseline; padding: 8px 14px; border-radius: 8px; background: var(--paper); color: var(--ink); transform: rotate(-1.2deg); box-shadow: 0 2px 0 #d8ccb4, 0 8px 16px rgba(0,0,0,.4); }
.hud-no { color: #9a6b2f; font: 800 11px/1 var(--sans); letter-spacing: .14em; }
.hud-stage b { font-size: 18px; }
.hud-stats { margin-left: auto; gap: 18px; }
.stat small { color: var(--cream2); }
.stat b { color: var(--cream); font: 800 18px/1 var(--sans); font-variant-numeric: tabular-nums; }
.stat b.over { color: #ff7a5c; }
#budget::before { content: ""; display: inline-block; width: 14px; height: 14px; margin-right: 5px; vertical-align: -1px; border-radius: 50%; background: radial-gradient(60% 60% at 40% 35%, #ffe38a, var(--gold) 60%, #a8720e); box-shadow: 0 2px 0 #8a5c0a; }
.btn { font-family: var(--sans); }
.btn-ghost, .btn-help, .btn-abort, .btn-sound { background: rgba(243,234,216,.08); box-shadow: inset 0 0 0 1px rgba(243,234,216,.22); color: var(--cream); }
.btn-run { background: radial-gradient(70% 90% at 40% 25%, #ff7a5c, var(--red) 60%, var(--red2)); color: #fff; box-shadow: 0 4px 0 #6e140a, 0 8px 14px rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.35); text-shadow: 0 1px 0 rgba(0,0,0,.25); }
.btn-run:active { transform: translateY(3px); box-shadow: 0 1px 0 #6e140a, 0 4px 8px rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.35); }
.btn-run.is-running { background: rgba(243,234,216,.1); color: var(--cream); box-shadow: inset 0 0 0 1px rgba(243,234,216,.3); text-shadow: none; }
.btn-sound[aria-pressed="true"], #btn-sound-title[aria-pressed="true"] { color: var(--gold2); box-shadow: inset 0 0 0 1px var(--gold2); }
.btn:focus-visible, .part:focus-visible, .sel-tools button:focus-visible { outline-color: var(--gold2); }
.speed { border-color: rgba(243,234,216,.25); }
.speed button[aria-pressed="true"] { background: var(--gold2); color: var(--ink); }
.goal-line { border-bottom: 0; color: var(--cream2); padding: 2px 18px 4px; }

/* 部品：手札のカード */
.palette { background: transparent; border-top: 0; gap: 0; padding: 14px 16px 8px 24px; align-items: flex-end; }
.part { display: flex; flex-direction: column; align-items: center; gap: 2px; width: 92px; margin-left: -8px; padding: 6px 6px 8px; border: 0; border-radius: 9px; background: var(--paper); color: var(--ink); box-shadow: 0 2px 0 #d8ccb4, 0 8px 14px rgba(0,0,0,.45); transition: transform .15s; text-align: center; }
.part:first-child { margin-left: 0; }
.part:nth-child(odd) { transform: rotate(-2deg); }
.part:nth-child(even) { transform: rotate(1.5deg); }
.part:hover:not(:disabled) { transform: translateY(-6px); z-index: 1; }
.part canvas { width: 64px; height: 44px; border-radius: 6px; background: #2a5040; }
.part b { font-size: 12px; line-height: 1.2; }
.part small { color: #7a4b00; font: 800 11px/1 var(--sans); padding: 3px 7px; border-radius: 999px; background: radial-gradient(60% 70% at 40% 35%, #ffe38a, var(--gold) 70%); }
.part[aria-pressed="true"] { transform: translateY(-14px) rotate(0); z-index: 2; border: 0; box-shadow: 0 0 0 3px var(--gold2), 0 16px 22px rgba(0,0,0,.5); }
.part:disabled { opacity: .45; }
.hint { background: transparent; color: var(--cream2); }

/* 選んだ部品の道具 */
.sel-tools button { border: 0; background: var(--paper); color: var(--ink); box-shadow: 0 2px 0 #d8ccb4, 0 6px 10px rgba(0,0,0,.35); }
.sel-tools button:last-child { color: var(--red); }
.sel-tools.is-docked { border: 0; background: var(--paper); box-shadow: 0 6px 16px rgba(0,0,0,.45); }
.sel-tools.is-docked .sel-name { color: var(--ink); }

/* 結果・解説カード：机に置いた紙のカード */
.result, .cards { background: rgba(20,14,10,.62); }
.result-panel, .card { border: 0; border-radius: 14px; background: var(--paper); color: var(--ink); box-shadow: 0 3px 0 #d8ccb4, 0 24px 50px rgba(0,0,0,.55); }
.result-panel p, .card .card-example { color: var(--ink2); }
.result-panel .stars { color: var(--gold); text-shadow: 0 2px 0 #a8720e; }
.result-panel .stars i { color: #d8ccb4; text-shadow: none; }
.result-panel .learn, .card .card-example, .share-box { background: var(--paper2); color: var(--ink); }
.result-panel .learn b, .card .card-example b { color: #9a6b2f; }
.result-panel .score-big { color: var(--ink); }
.result-panel .score-big small { color: var(--ink2); }
.new-best { color: var(--red); }
.result-actions .btn:not(.btn-run), .card .btn:not(.btn-run) { background: transparent; color: var(--ink); box-shadow: inset 0 0 0 1.5px #cdbd9f; }
.card p { color: var(--ink); }
.card-new { background: var(--red); color: #fff; }
.card-count { color: var(--ink2); }
.share-box input { background: #fff; border-color: #cdbd9f; color: var(--ink2); }
.page503-note { background: var(--paper); color: var(--ink); }
.page503-explain { color: var(--red); }
.page503-stat { color: var(--ink2); }
.page503-note dl { background: var(--paper2); }
.page503-note dt { color: var(--red); }
.page503-note dt:nth-of-type(2) { color: #2f6fd6; }
.toast { background: var(--paper); color: var(--ink); border: 0; box-shadow: 0 8px 18px rgba(0,0,0,.45); }

/* タイトル・ステージ選択・図鑑 */
#title-bg { opacity: .6; }
.title-tag { color: var(--gold2); font-family: var(--sans); }
.title-logo { color: var(--paper); font-family: var(--sans); font-weight: 900; text-shadow: 0 3px 0 #cbbda2, 0 6px 0 #a89878, 0 18px 30px rgba(0,0,0,.6); animation: none; }
.title-sub { color: var(--cream); }
.title-note { color: var(--cream2); }
.menu-head { background: rgba(20,14,10,.55); border-bottom: 0; backdrop-filter: blur(6px); }
.zukan-count { color: var(--gold2); font-family: var(--sans); }
.chapter h2 small { color: var(--cream2); }
.stage-card, .zk { border: 0; background: var(--paper); color: var(--ink); box-shadow: 0 2px 0 #d8ccb4, 0 10px 18px rgba(0,0,0,.4); }
.stage-card:hover:not(:disabled) { border-color: transparent; transform: translateY(-3px) rotate(-.6deg); }
.stage-card.is-endless { box-shadow: 0 0 0 2px var(--red), 0 10px 18px rgba(0,0,0,.4); }
.sc-no { color: #9a6b2f; font-family: var(--sans); }
.stage-card.is-endless .sc-no { color: var(--red); }
.sc-foot { color: var(--ink2); }
.sc-stars { color: var(--gold); }
.sc-stars i { color: #d8ccb4; }
.zk .zk-ex { color: var(--ink2); }
.zk .zk-ex b { color: #9a6b2f; }
.zk-q { color: #cdbd9f; }
.zk-503 { color: var(--red); }
.zk.is-503 { box-shadow: 0 0 0 2px var(--red), 0 10px 18px rgba(0,0,0,.4); }
@media (orientation: landscape) and (max-height: 520px) {
  .palette { padding: 4px 10px 2px 16px; }
  .part { width: 76px; padding: 3px 4px 4px; }
  .part canvas { width: 48px; height: 30px; }
  .part[aria-pressed="true"] { transform: translateY(-6px); }
}
.stat, .stat small, .stat b { white-space: nowrap; }
@media (orientation: portrait) and (max-width: 900px) {
  .hud-stats { gap: 10px; }
  .stat b { font-size: 16px; }
  .part { width: 80px; }
  .part canvas { width: 54px; height: 36px; }
}

/* 盤のまわりの空間：奥に壁、手前に机。机の奥の縁（壁との境目）が盤の後ろを横切り、上から照明が当たる */
html, body {
  background:
    radial-gradient(55% 38% at 50% 58%, rgba(255,214,160,.16), rgba(255,214,160,0) 70%),            /* 机に落ちる照明の明るみ */
    radial-gradient(70% 45% at 50% 0%, rgba(255,200,150,.10), rgba(255,200,150,0) 70%),            /* 壁の上の明るみ */
    radial-gradient(130% 100% at 50% 50%, rgba(0,0,0,0) 55%, rgba(0,0,0,.55) 100%),                /* 四隅を暗く */
    linear-gradient(180deg, #17120f 0%, #241c17 36%, #2c221c 41.6%,                                 /* 壁 */
      #5a4434 41.6%, #4d392b 42.4%,                                                                 /* 机の奥の縁 */
      #3e2e23 44%, #4a3729 66%, #2b1f17 100%) fixed;                                                /* 机の面（手前ほど暗く） */
}

/* 盤のまわりの空間：教室の机（js/ui/room.js が後ろに絵を描く）。上の「色分けだけの空間」は使わない */
html { background: #8a5428; }
body { background: transparent !important; }
#room { position: fixed; inset: 0; width: 100%; height: 100%; z-index: -1; pointer-events: none; }
/* 明るい教室の上では、数字やボタンを紙の札に載せて読みやすく */
.hud-stats { padding: 7px 14px; border-radius: 10px; background: rgba(246,239,225,.92); box-shadow: 0 2px 0 #d8ccb4, 0 8px 16px rgba(0,0,0,.25); }
.stat small { color: var(--ink2); }
.stat b { color: var(--ink); }
.stat b.over { color: var(--red); }
.btn-ghost, .btn-help, .btn-abort, .btn-sound { background: rgba(246,239,225,.92); box-shadow: 0 2px 0 #d8ccb4, 0 6px 12px rgba(0,0,0,.22); color: var(--ink); }
.btn-run.is-running { background: rgba(246,239,225,.92); color: var(--ink); box-shadow: 0 2px 0 #d8ccb4, 0 6px 12px rgba(0,0,0,.22); }
.goal-line { justify-self: start; margin: 2px 16px 0; padding: 4px 12px; border-radius: 8px; background: rgba(246,239,225,.85); color: var(--ink2); }
.hint { color: rgba(255,245,230,.92); text-shadow: 0 1px 2px rgba(0,0,0,.6); }
.screen { background: rgba(28,20,14,.55); }
.title-screen { background: rgba(28,20,14,.62); }
.btn-sound[aria-pressed="true"], #btn-sound-title[aria-pressed="true"] { color: #9a6b2f; box-shadow: inset 0 0 0 1.5px #c99a10, 0 2px 0 #d8ccb4; }

/* ---- スマホの調整（盤の世界の指定がスマホ向けの小さい指定を上書きしていたので、最後にもう一度） ---- */
.app { grid-template-columns: minmax(0, 1fr); }   /* 中身が長くても画面の幅を超えない */
.hud .btn { white-space: nowrap; }
.goal-line { max-width: calc(100% - 32px); }
@media (orientation: portrait) and (max-width: 900px) {
  .hud { gap: 8px; padding: 10px 12px 4px; }
  .hud-stats { flex-wrap: wrap; gap: 4px 12px; padding: 6px 10px; width: 100%; justify-content: flex-start; }
  .stat b { font-size: 15px; }
  .stat small { font-size: 11px; }
  .hud-actions { flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
  .hud .btn { padding: 8px 14px; font-size: 13px; }
  .btn-help { width: 36px; }
  .goal-line { font-size: 12px; }
}
@media (orientation: landscape) and (max-height: 520px) {
  .hud { gap: 6px; padding: 4px 8px 2px; }
  .hud-stage { padding: 3px 8px; transform: none; }
  .hud-stage b { font-size: 13px; }
  .hud-no { font-size: 9px; }
  .hud-stats { padding: 3px 8px; gap: 10px; }
  .stat b { font-size: 13px; }
  .stat small { font-size: 9px; }
  #budget::before { width: 10px; height: 10px; margin-right: 3px; }
  .hud-actions { gap: 4px; }
  .hud .btn { padding: 5px 10px; font-size: 11px; min-width: 0; }
  .btn-help, .btn-menu { width: 28px; padding: 5px 0; }
  .palette { padding: 2px 8px 2px 12px; }
  .part { width: 58px; margin-left: -6px; padding: 2px 3px 3px; border-radius: 7px; }
  .part canvas { width: 40px; height: 24px; }
  .part b { font-size: 9px; }
  .part small { font-size: 9px; padding: 1px 5px; }
  .part[aria-pressed="true"] { transform: translateY(-4px); }
  .hint { display: none; }
}
/* カード（部品）や置いた部品を選んでいる間は、盤の上で指を動かしてもスクロールしない */
#stage.is-placing { touch-action: none !important; }

/* スマホの縦画面：盤が横長で見にくいので、横にするよう案内する（横にすると消える。「縦のまま遊ぶ」で閉じられる） */
.rotate-hint { display: none; }
@media (orientation: portrait) and (max-width: 900px) {
  .app:not(.portrait-ok) .rotate-hint { position: fixed; inset: 0; z-index: 7; display: grid; place-items: center; padding: 24px; background: rgba(28,20,14,.72); }
}
.rotate-card { width: min(340px, 100%); padding: 26px 22px 22px; border-radius: 14px; background: var(--paper); color: var(--ink); text-align: center; box-shadow: 0 3px 0 #d8ccb4, 0 24px 50px rgba(0,0,0,.55); }
.rotate-card h2 { margin: 14px 0 6px; font-size: 20px; }
.rotate-card p { margin: 0 0 18px; color: var(--ink2); font-size: 14px; }
.rotate-card .btn { color: var(--ink); background: transparent; box-shadow: inset 0 0 0 1.5px #cdbd9f; }
.rotate-icon { width: 46px; height: 76px; margin: 0 auto; border: 4px solid var(--ink); border-radius: 10px; position: relative; animation: tilt 2.4s ease-in-out infinite; }
.rotate-icon::after { content: ""; position: absolute; left: 50%; bottom: 5px; width: 10px; height: 3px; margin-left: -5px; border-radius: 2px; background: var(--ink); }
@keyframes tilt { 0%, 25% { transform: rotate(0); } 55%, 80% { transform: rotate(-90deg); } 100% { transform: rotate(0); } }
@media (prefers-reduced-motion: reduce) { .rotate-icon { animation: none; transform: rotate(-90deg); } }

/* タイトル：高さが足りないとき（スマホの横画面）でもボタンが押せるように。スクロールでき、横画面では小さくして 1 画面に収める */
.title-screen { overflow-y: auto; place-items: safe center; }
.title-inner { padding: 24px 16px; }
@media (orientation: landscape) and (max-height: 520px) {
  .title-logo { font-size: clamp(64px, 24vh, 120px); }
  .title-sub { margin: 0 0 12px; font-size: 16px; }
  .title-tag { font-size: 10px; }
  .btn-big { padding: 10px 28px; font-size: 15px; }
  .title-note { margin-top: 10px; font-size: 11px; }
  .title-inner { padding: 10px 16px; }
}
/* 結果・解説カード・横にする案内も、高さが足りないときはスクロールできるように */
.result, .cards, .app:not(.portrait-ok) .rotate-hint { overflow-y: auto; place-items: safe center; }

/* ==== スマホの横画面（高さ 520px 以下）の割り付け ====
   高さが一番足りないので、手札を左に縦に並べ、盤を上のバーの下いっぱいに広げる。他の画面も文字と余白を詰める */
@media (orientation: landscape) and (max-height: 520px) {
  .app { grid-template-columns: auto minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "hud hud" "pal stage"; }
  .hud { grid-area: hud; }
  .stage-wrap { grid-area: stage; }
  .palette { grid-area: pal; flex-direction: column; align-items: stretch; gap: 4px; width: 72px; padding: 2px 4px 6px 8px; overflow-x: hidden; overflow-y: auto; }
  .part, .part:nth-child(odd), .part:nth-child(even) { width: 100%; margin-left: 0; transform: none; flex: none; }
  .part:hover:not(:disabled) { transform: none; }
  .part[aria-pressed="true"] { transform: translateX(4px); }
  .sel-tools.is-docked { left: auto; right: 12px; bottom: 12px; }
  .toast { bottom: 16px; }

  /* ステージ選択・図鑑 */
  .menu-head { padding: 6px 12px; }
  .menu-head h2 { font-size: 15px; }
  .menu-head .btn { padding: 5px 12px; }
  .menu-body { padding: 10px 12px 24px; }
  .chapter { margin-bottom: 14px; }
  .chapter h2 { margin-bottom: 6px; font-size: 15px; }
  .chapter h2 small { font-size: 11px; }
  .stage-cards { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
  .stage-card { padding: 9px 12px; gap: 2px; }
  .stage-card b { font-size: 14px; }
  .sc-stars { font-size: 13px; }
  .sc-foot { font-size: 11px; }
  .zukan-grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 8px; }
  .zk { padding: 10px 12px; }
  .zk h3 { font-size: 14px; }
  .zk p { font-size: 12px; }

  /* 解説カード */
  .cards, .result { padding: 10px; }
  .card { width: min(560px, 100%); padding: 14px 18px; }
  .card h2 { margin-bottom: 4px; font-size: 18px; }
  .card p { margin-bottom: 6px; font-size: 13px; line-height: 1.5; }
  .card .card-example { padding: 6px 10px; font-size: 12px; }
  .card .btn { margin-top: 4px; padding: 8px 16px; font-size: 14px; }

  /* 結果・503 */
  .result-panel { width: min(560px, 100%); padding: 14px 18px; }
  .result-panel h2 { font-size: 20px; margin-bottom: 2px; }
  .result-panel .stars { font-size: 24px; }
  .result-panel .score-big { font-size: 32px; }
  .result-panel p { margin: 4px 0; font-size: 13px; }
  .result-panel .learn { margin: 8px 0; padding: 8px 12px; font-size: 12px; }
  .result-actions { margin-top: 10px; gap: 8px; }
  .result-actions .btn { padding: 7px 16px; font-size: 13px; }
  .share-box { margin-top: 8px; padding: 10px; }
  .page503 { width: min(720px, 100%); }
  .page503-doc { padding: 12px 20px 4px; }
  .page503-doc h1 { margin-bottom: 6px; font-size: 22px; }
  .page503-doc p { font-size: 12px; margin-top: 4px; }
  .page503-note { padding: 10px 18px 14px; }
  .page503-explain { font-size: 13px; }
  .page503-stat { margin-bottom: 8px; font-size: 11px; }
  .page503-note dl { padding: 8px 12px; gap: 4px 10px; }
  .page503-note dd { font-size: 12px; }
}
@media (orientation: landscape) and (max-height: 520px) {
  .hud { flex-wrap: nowrap; }
  .hud-stage { flex: 0 1 auto; min-width: 0; }
  .hud-stage b { overflow: hidden; text-overflow: ellipsis; }
}
@media (orientation: landscape) and (max-height: 520px) and (max-width: 740px) {
  .hud-no, #stat-handled { display: none !important; }   /* 狭い横画面では「STAGE n」と処理数を省いて 1 段に収める */
  .hud-stats { gap: 8px; padding: 3px 6px; }
  .hud .btn { padding: 5px 8px; }
}
/* iPhone の Safari は画面の一番上のタップをツールバーの表示に使うので、横画面の上のバーを端から離し、ボタンを押しやすい大きさに */
@media (orientation: landscape) and (max-height: 520px) {
  .hud { padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .hud .btn { min-height: 34px; }
  .btn-run { min-width: 64px; }
}
/* 横画面の Safari はアドレスバーとタブの列がページの上に重なる（実機で確認）。上のバーは下に移し、上は覆われる分だけ空ける */
@media (orientation: landscape) and (max-height: 520px) {
  .app { grid-template-rows: minmax(0, 1fr) auto; grid-template-areas: "pal stage" "hud hud"; padding-top: var(--cover-top, 0px); }
  .hud { padding: 4px 8px max(6px, env(safe-area-inset-bottom)); }
  .hud-stage { transform: none; }
}
@media (orientation: landscape) and (max-height: 520px) {
  .sel-tools.is-docked { right: 12px; bottom: calc(58px + env(safe-area-inset-bottom)); }   /* 下のバー（実行ボタン）に重ならないように */
}

/* ゲーム画面は「実際に見えている範囲」（visualViewport）に合わせる。アドレスバーが出ても隠れても収まる */
.app { position: fixed; left: 0; right: 0; top: var(--vv-top, 0px); height: var(--vv-h, 100svh); }
.app { right: auto; left: var(--vv-left, 0px); width: var(--vv-w, 100vw); }
/* 横画面の下のバー：数字とボタンもステージ名のすぐ右に詰めて並べる（右端に寄せるとはみ出しやすい） */
@media (orientation: landscape) and (max-height: 520px) {
  .hud { justify-content: flex-start; }
  .hud-stats { margin-left: 0; }
  .hud-actions { margin-left: 0; }
}

/* ==== スマホの横画面：3 列（左＝手札／真ん中＝盤／右＝ステージ名・数字・ボタン）====
   Safari のバーで高さがとても低く幅は余るので、上下の帯をなくして盤を画面の高さいっぱいに使う */
@media (orientation: landscape) and (max-height: 520px) {
  .app { grid-template-columns: auto minmax(0, 1fr) auto; grid-template-rows: minmax(0, 1fr); grid-template-areas: "pal stage hud"; padding-top: 0; }
  .hud { grid-area: hud; flex-direction: column; align-items: stretch; justify-content: flex-start; width: 132px; gap: 6px; padding: 6px 8px max(6px, env(safe-area-inset-bottom)) 4px; overflow-y: auto; }
  .btn-menu { align-self: flex-start; }
  .hud-stage { flex-direction: column; align-items: flex-start; gap: 0; padding: 5px 8px; }
  .hud-no, #stat-handled:not([hidden]) { display: revert !important; }
  .hud-no { font-size: 9px; }
  .hud-stage b { white-space: normal; font-size: 13px; line-height: 1.25; }
  .hud-stats { flex-direction: column; flex-wrap: nowrap; gap: 1px; padding: 5px 8px; width: auto; }
  .stat { justify-content: space-between; }
  .stat b { font-size: 13px; }
  .hud-actions { flex-wrap: wrap; gap: 6px; margin-top: auto; }
  .hud-actions .btn-sound { flex: 1; }
  .btn-run { flex-basis: 100%; order: 9; min-height: 40px; font-size: 14px !important; }
  .speed { flex-basis: 100%; justify-content: center; }
  .btn-abort { flex-basis: 100%; }
  /* 手札：大きくして押しやすく */
  .palette { width: 96px; gap: 6px; padding: 6px 6px max(6px, env(safe-area-inset-bottom)) 8px; }
  .part { padding: 5px 4px 6px; border-radius: 9px; }
  .part canvas { width: 64px; height: 38px; }
  .part b { font-size: 11px; }
  .part small { font-size: 10px; padding: 2px 6px; }
  .sel-tools.is-docked { right: 144px; bottom: max(10px, env(safe-area-inset-bottom)); }
}
@media (orientation: landscape) and (max-height: 520px) {
  #stat-handled:not([hidden]) { display: flex !important; }
  .hud-no { display: block !important; }
}

/* ==== スマホの横画面：ほかの画面も「左右に分ける」割り付け（高さが低く幅が余るので、縦に積まない） ==== */
@media (orientation: landscape) and (max-height: 520px) {
  /* タイトル：左にロゴ、右にボタン */
  .title-inner { display: grid; grid-template-columns: auto minmax(220px, 300px); grid-template-areas: "tag act" "logo act" "sub act" "sub note"; column-gap: 36px; align-items: center; text-align: left; }
  .title-tag { grid-area: tag; }
  .title-logo { grid-area: logo; font-size: clamp(64px, 30vh, 130px); margin: 0; }
  .title-sub { grid-area: sub; margin: 0; }
  .title-actions { grid-area: act; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-self: end; }
  .title-actions .btn-big { grid-column: 1 / -1; order: -1; padding: 12px 20px; font-size: 17px; }
  .title-actions .btn { padding: 9px 12px; }
  .title-note { grid-area: note; margin: 6px 0 0; text-align: left; align-self: start; }

  /* 結果・解説カード：左に見出し（題・★・数字）、右に説明とボタン */
  .result-panel, .card { width: min(780px, 100%); display: grid; grid-template-columns: minmax(0, .8fr) minmax(0, 1.2fr); grid-auto-flow: row dense; column-gap: 22px; align-content: center; text-align: left; }
  .result-panel > *, .card > * { grid-column: 2; }
  .result-panel > h2, .result-panel > .stars, .result-panel > p, .result-panel > .score-big,
  .card > .card-head, .card > .card-icon, .card > h2, .card > p:not(.card-example) { grid-column: 1; }
  .result-panel .result-actions { justify-content: flex-start; }
  .result-panel .learn { margin-top: 0; }
  .card .btn { width: 100%; }

  /* 503：左に本物のエラー画面、右に原因と対策 */
  .page503 { width: min(820px, 100%); display: grid; grid-template-columns: minmax(0, .75fr) minmax(0, 1.25fr); overflow-y: auto; }
  .page503-doc { display: flex; flex-direction: column; justify-content: center; padding: 14px 16px; }
  .page503-doc h1 { font-size: 20px; }
  .page503-note { border-radius: 0 6px 6px 0; }
  .page503-note .result-actions { justify-content: flex-start; }
}
@media (orientation: landscape) and (max-height: 520px) {
  /* 左の見出しを上に詰める（右の説明が左の行を引き伸ばさないように、説明を 4 行ぶんにまたがせる） */
  .result-panel, .card { grid-template-rows: auto auto auto 1fr; align-items: start; }
  .result-panel > .learn, .card > .card-example { grid-row: 1 / span 4; }
  .result-panel > h2 { margin-bottom: 0; }
}

/* ダブルタップでの拡大を止める（ボタンを素早く押したときにページが拡大されないように） */
html, body, button, canvas { touch-action: manipulation; }
#stage { touch-action: pan-x; }
@media (orientation: landscape) and (max-height: 520px) { #stage { touch-action: none; } }
.share-box input { font-size: 16px; }   /* 16px 未満の入力欄は、iPhone が触れたときに拡大してしまう */

/* ==== 横画面の結果・解説カード：左右を同じ幅にし、左（見出し）は上下の真ん中にまとめる ==== */
@media (orientation: landscape) and (max-height: 520px) {
  .result-panel, .card { display: flex !important; align-items: center; gap: 24px; width: min(760px, 100%); }
  .r-head, .r-body { flex: 1 1 0; min-width: 0; }
  .r-head { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
  .card .r-head { align-items: flex-start; text-align: left; }
  .card .card-head { width: 100%; }
  .r-head h2 { font-size: 26px; margin: 0 0 4px; }
  .result-panel .r-head .stars { font-size: 30px; }
  .r-body .learn, .r-body .card-example { margin: 0 0 10px; }
  .r-body .result-actions { justify-content: center; margin-top: 8px; }
  .r-body .result-actions .btn-run { flex: 1 1 100%; }
  /* 503：左右を同じくらいに */
  .page503 { grid-template-columns: 1fr 1fr; }
  /* タイトル：左右を同じ幅で真ん中に */
  .title-inner { grid-template-columns: 1fr 1fr; width: min(760px, 100%); justify-items: center; text-align: center; }
  .title-actions { width: 100%; }
  .title-note { text-align: center; }
}
@media (orientation: landscape) and (max-height: 520px) {
  /* 共有を開いたら「今日のまなび」を畳んで、共有の欄を同じ枠に収める。収まらなければ右側だけスクロール */
  .result-panel:has(.share-box) .learn { display: none; }
  .result-panel { max-height: calc(var(--vv-h, 100svh) - 20px); }
  .r-body { max-height: calc(var(--vv-h, 100svh) - 50px); overflow-y: auto; }
  .r-body .share-box { margin-top: 8px; }
}
a.btn { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
@media (orientation: landscape) and (max-height: 520px) {
  /* 共有の欄：ボタンを小さく 1 行に並べ、URL の欄は畳む（コピーのボタンで足りる） */
  .r-body .share-box { padding: 8px; }
  .r-body .share-box .result-actions { justify-content: center; gap: 6px; }
  .r-body .share-box .btn, .r-body .share-box .btn-run { flex: 0 0 auto !important; padding: 6px 12px; font-size: 12px; min-height: 0; }
  .r-body .share-box input { display: none; }
  .result-panel:has(.share-box) .r-body > .result-actions:last-of-type:not(:first-child) { display: none; }   /* 「リプレイ・共有」の行も畳む */
}

/* スマホの横画面：インカメラ（ノッチ・ダイナミックアイランド）の側は、安全な範囲よりさらに少し内側に寄せる。
   どちら側にカメラが来ても良いように左右とも同じだけ空ける */
@media (orientation: landscape) and (max-height: 520px) {
  :root { --side: max(16px, calc(max(env(safe-area-inset-left), env(safe-area-inset-right)) + 12px)); }
  .app { padding-left: var(--side); padding-right: var(--side); }
  .screen, .result, .cards, .app:not(.portrait-ok) .rotate-hint { padding-left: var(--side) !important; padding-right: var(--side) !important; }
  .sel-tools.is-docked { right: calc(var(--side) + 136px); }
}

/* 届けろ編の予算：コインを予算の数だけ並べ、使った分を暗くする */
#budget .coins { display: inline-flex; gap: 2px; margin-right: 6px; vertical-align: -1px; }
#budget .coins i { width: 11px; height: 11px; border-radius: 50%; background: rgba(0,0,0,.14); box-shadow: inset 0 1px 1px rgba(0,0,0,.2); }
#budget .coins i.on { background: radial-gradient(60% 60% at 40% 35%, #ffe38a, #e0a826 60%, #a8720e); box-shadow: 0 1px 0 #8a5c0a; }
#budget .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
#budget:has(.coins)::before { display: none; }
#budget.over .left { color: var(--red); }
@media (orientation: landscape) and (max-height: 520px) {
  #budget { display: flex; flex-direction: column; align-items: flex-end; }
  #budget .coins { margin: 0 0 2px; flex-wrap: wrap; justify-content: flex-end; max-width: 80px; }
  #budget .coins i { width: 8px; height: 8px; }
}

/* 全部外す（パレットの最後）：2 回押しで外す */
.part.part-clear { min-width: 0; padding: 4px 10px; font-size: 12px; font-weight: 700; color: var(--cream2, #eee); background: rgba(0,0,0,.25); border: 1px dashed rgba(255,255,255,.35); }
.part.part-clear.is-armed { color: #fff; background: #b3412e; border-style: solid; }
.card-stars { display: inline-block; margin-top: 6px; color: #b07a00; font-weight: 700; font-size: .92em; }
