:root {
  color-scheme: light;
  --ink: #29251f;
  --paper: #f3e8cf;
  --sky: #9ed8d0;
  --sky-deep: #397b78;
  --brick: #c7653d;
  --brick-dark: #7d382b;
  --beam: #f0b84b;
  --beam-dark: #8c5b18;
  --safe: #17685b;
  --danger: #ad342d;
  --shadow: rgba(45, 35, 22, .2);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-width: 280px; min-height: 100%; }
body {
  background: #315d5a;
  color: var(--ink);
  font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", system-ui, sans-serif;
  overscroll-behavior-x: none;
}
button, input { font: inherit; }
button:focus-visible, input:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }

#app {
  position: relative;
  width: min(100%, 920px);
  height: 100svh;
  height: 100dvh;
  min-height: 500px;
  margin: 0 auto;
  overflow: hidden;
  background: var(--paper);
  box-shadow: 0 0 40px rgba(0,0,0,.25);
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  min-width: 0;
  min-height: 0;
}
.screen.is-active { display: grid; }

/* Start: an asymmetrical poster rather than a card stack. */
#startScreen {
  grid-template-columns: minmax(0, 1.15fr) minmax(180px, .85fr);
  grid-template-rows: minmax(0, 1fr) auto auto;
  gap: 12px 20px;
  padding: max(18px, env(safe-area-inset-top)) 22px max(16px, env(safe-area-inset-bottom));
  background:
    linear-gradient(90deg, transparent 49.6%, rgba(41,37,31,.14) 50%, transparent 50.4%),
    repeating-linear-gradient(0deg, rgba(41,37,31,.045) 0 1px, transparent 1px 26px),
    var(--paper);
}
.start-art { grid-row: 1 / 4; position: relative; min-height: 0; overflow: hidden; border: 3px solid var(--ink); background: var(--sky); }
.sun-disc { position: absolute; width: 88px; aspect-ratio: 1; border: 3px solid var(--ink); border-radius: 50%; top: 7%; left: 9%; background: #ffe590; }
.start-wind { position: absolute; top: 18%; right: 7%; font-weight: 900; letter-spacing: 8px; transform: rotate(-6deg); }
.start-tower { position: absolute; left: 50%; bottom: 8%; width: 68%; height: 66%; transform: translateX(-50%); display: flex; flex-direction: column-reverse; align-items: center; justify-content: flex-start; }
.start-tower::after { content: ""; width: 92%; height: 8%; border: 3px solid var(--ink); background: #7a5032; }
.start-tower i { display: block; width: var(--w); height: 22%; border: 3px solid var(--ink); border-bottom: 0; background: var(--brick); box-shadow: inset 0 -7px 0 rgba(92,39,29,.18); }
.start-tower b { position: absolute; left: 50%; color: #fff7df; text-shadow: 2px 2px var(--brick-dark); font-size: clamp(24px, 5vw, 48px); }
.start-tower b:nth-of-type(1) { bottom: 13%; }.start-tower b:nth-of-type(2) { bottom: 33%; }.start-tower b:nth-of-type(3) { bottom: 53%; }
.roof-stones { position: absolute; left: 49%; bottom: 73%; display: flex; gap: 4px; transform: translateX(-50%); }
.roof-stones i { width: 18px; height: 16px; border: 2px solid var(--ink); border-radius: 6px 5px 3px 7px; background: #eee0b4; transform: rotate(var(--r, -4deg)); }
.roof-stones i:nth-child(2) { --r: 6deg; }
.start-copy { align-self: end; }
.eyebrow { margin: 0 0 7px; color: var(--brick-dark); font-size: 14px; font-weight: 900; letter-spacing: .08em; }
h1 { margin: 0; font-family: Georgia, "Yu Mincho", serif; font-size: clamp(38px, 6vw, 68px); line-height: .98; letter-spacing: -.05em; }
h1 span { color: var(--brick); }
.lead { margin: 14px 0 0; font-size: clamp(15px, 2vw, 19px); font-weight: 700; line-height: 1.6; }
.start-actions { display: grid; gap: 8px; }
.start-note { margin: 0; font-size: 14px; font-weight: 700; }

.primary-button, .quiet-button, .build-button, .icon-button {
  min-height: 44px;
  border: 2px solid var(--ink);
  border-radius: 5px;
  cursor: pointer;
  font-weight: 900;
  box-shadow: 3px 3px 0 var(--ink);
}
.primary-button, .build-button { color: #fff; background: var(--brick); }
.quiet-button { color: var(--ink); background: #fff5d8; }
button:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--ink); }
button:disabled { cursor: default; opacity: .55; transform: none; }

/* Gameplay height budget: 52 HUD + flexible world + 126 controls + wrapping status. */
.play-screen {
  grid-template-rows: 52px minmax(160px, 1fr) 126px auto;
  gap: 5px;
  padding: max(5px, env(safe-area-inset-top)) 6px max(5px, env(safe-area-inset-bottom));
  background: #d7eee6;
}
#hud {
  grid-row: 1;
  display: grid;
  grid-template-columns: minmax(80px, 1.2fr) minmax(48px, .65fr) minmax(74px, 1fr) 52px 44px 44px;
  gap: 4px;
  min-width: 0;
}
#hud > div, .icon-button { border: 2px solid var(--ink); background: #fff8dc; }
#hud > div { min-width: 0; padding: 3px 5px; }
#hud span { display: block; font-size: 14px; font-weight: 700; line-height: 1.1; white-space: nowrap; }
#hud strong { display: block; overflow: hidden; font-size: 16px; line-height: 1.25; text-overflow: ellipsis; white-space: nowrap; }
.score-mark strong { color: var(--brick-dark); }
.stability-track { width: 100%; height: 10px; margin-top: 4px; border: 1px solid var(--ink); background: #d3b5a4; overflow: hidden; }
.stability-track i { display: block; width: 72%; height: 100%; background: var(--safe); transition: width .25s ease, background .25s ease; }
.icon-button { width: 44px; min-width: 44px; min-height: 44px; padding: 0; box-shadow: none; }
.repair-button { width: 52px; min-width: 52px; font-size: 14px; line-height: 1.05; }
.repair-button b { display: block; }

#gameField {
  grid-row: 2;
  display: grid;
  grid-template-columns: minmax(160px, 1fr) minmax(106px, 28%);
  grid-template-rows: minmax(0, 1fr);
  min-width: 0;
  min-height: 160px;
  border: 3px solid var(--ink);
  background: var(--sky);
  overflow: hidden;
}
.world { position: relative; grid-column: 1; min-width: 0; min-height: 0; overflow: hidden; border-right: 3px solid var(--ink); background: var(--sky); }
.sky-lines { position: absolute; inset: 0; opacity: .16; background: repeating-linear-gradient(-10deg, transparent 0 30px, var(--ink) 31px 32px); }
.wind-flag { position: absolute; z-index: 5; top: 6px; left: 6px; display: flex; align-items: center; gap: 5px; min-height: 34px; padding: 3px 7px; border: 2px solid var(--ink); background: #fff4c6; font-weight: 900; }
.wind-flag span { font-size: 14px; }.wind-flag b { font-size: 22px; }
.risk-flag { position: absolute; z-index: 4; top: 48px; right: 5px; padding: 3px 5px; border: 2px dashed var(--brick-dark); background: #ffe36f; font-size: 14px; font-weight: 900; transition: right .25s ease, left .25s ease; }
.risk-flag.is-left { right: auto; left: 5px; }
.crane { position: absolute; z-index: 3; inset: 0; pointer-events: none; }
.crane-mast { position: absolute; right: 8%; top: 7%; bottom: 5%; width: 8px; border: 2px solid var(--ink); background: var(--beam); }
.crane-arm { position: absolute; right: 7%; top: 9%; width: 76%; height: 8px; border: 2px solid var(--ink); background: var(--beam); }
.crane-cable { position: absolute; left: calc(50% - 1px); top: 10%; height: 15%; border-left: 3px dashed var(--ink); transition: left .2s ease; }
.crane-beam { position: absolute; z-index: 6; top: 24%; left: 50%; width: 36%; height: 28px; transform: translateX(-50%); border: 3px solid var(--ink); background: var(--beam); box-shadow: inset 0 -5px rgba(140,91,24,.22); transition: left .2s ease; }
.crane-beam b { display: grid; place-items: center; height: 100%; font-size: 20px; }
.tower { position: absolute; left: 50%; bottom: 4%; width: 64%; height: 69%; transform: translateX(-50%); display: flex; flex-direction: column; justify-content: flex-end; align-items: center; transition: transform .35s ease; }
.tower.is-danger:not(.is-collapsed) { filter: drop-shadow(0 0 7px rgba(173,52,45,.7)); animation: danger-sway .9s ease-in-out infinite alternate; }
.tower.is-collapsed { transform: translateX(-50%) rotate(-8deg) translateY(10px); }
.tower-floors { width: 100%; min-height: 0; display: flex; flex-direction: column-reverse; align-items: center; }
.floor { position: relative; display: grid; place-items: center; width: calc(92% - var(--offset, 0) * 3%); height: clamp(26px, 6vh, 42px); margin-bottom: -2px; border: 3px solid var(--ink); background: var(--brick); color: #fff8e3; font-size: 20px; font-weight: 900; text-shadow: 1px 1px var(--brick-dark); transform: translateX(calc(var(--offset, 0) * 8%)); animation: floor-in .34s cubic-bezier(.2,.9,.2,1.2); }
.floor.risk { background: #d88a35; }
.floor small { position: absolute; right: 4px; bottom: 1px; font-size: 14px; }
.tower-base { display: grid; place-items: center; width: 100%; min-height: 32px; border: 3px solid var(--ink); background: #7b5338; color: white; font-size: 14px; font-weight: 900; }
.roof-tray { order: 2; display: flex; justify-content: center; gap: 2px; width: 68%; min-height: 8px; border-bottom: 3px solid var(--ink); }
.roof-tray i { width: 13px; height: 12px; border: 2px solid var(--ink); border-radius: 4px 5px 3px 5px; background: #f6e5b9; }
.burst { position: absolute; z-index: 9; left: 50%; top: 42%; width: 12px; height: 12px; transform: translate(-50%,-50%); pointer-events: none; }
.burst.is-on { animation: burst .45s ease-out; box-shadow: 0 -40px #ffe360, 34px -20px #fff, 34px 20px #ffe360, 0 40px #fff, -34px 20px #ffe360, -34px -20px #fff; }

.written-panel { grid-column: 2; min-width: 0; padding: 7px 6px; background: var(--paper); background-image: linear-gradient(rgba(41,37,31,.07) 1px, transparent 1px); background-size: 100% 24px; }
.written-panel p { margin: 0 0 6px; font-size: 14px; font-weight: 800; }
.division-work { margin: 0 auto 7px; width: min(100%, 126px); font-family: ui-monospace, "Yu Gothic", monospace; font-size: clamp(20px, 4vw, 29px); font-weight: 900; text-align: right; }
.quotient-work { min-height: 31px; padding-right: 4px; letter-spacing: .08em; }
.division-line { display: grid; grid-template-columns: auto 1fr; align-items: end; }
.division-line b { padding: 0 5px 2px 0; }
.division-line span { padding: 1px 4px; border-top: 3px solid var(--ink); border-left: 3px solid var(--ink); }
.step-math { margin: 0; }
.step-math div { display: flex; justify-content: space-between; min-height: 28px; padding: 2px 3px; border-bottom: 1px solid rgba(41,37,31,.22); }
.step-math dt, .step-math dd { margin: 0; font-size: 14px; font-weight: 800; }
.step-math dd { font-size: 17px; }
.math-hint { min-height: 38px; padding: 4px; border-left: 4px solid var(--safe); background: rgba(255,255,255,.56); line-height: 1.35; }

.control-dock { grid-row: 3; display: grid; grid-template-rows: 38px 38px 46px; gap: 2px; min-height: 0; }
.slider-row { display: grid; grid-template-columns: 42px minmax(0,1fr) 52px; align-items: center; gap: 7px; min-width: 0; padding: 0 7px; border: 2px solid var(--ink); background: #fff8dc; font-weight: 900; }
.slider-row > span { font-size: 16px; }
.slider-row output { text-align: center; font-size: 16px; white-space: nowrap; }
input[type="range"] { width: 100%; min-width: 0; height: 44px; margin: -3px 0; accent-color: var(--brick); cursor: ew-resize; }
.build-button { width: 100%; min-height: 44px; font-size: 17px; }
.status-message { grid-row: 4; min-height: 44px; margin: 0; padding: 4px 7px; background: #29251f; color: #fff8dc; font-size: 14px; font-weight: 700; line-height: 1.3; white-space: normal; }
.play-screen.is-paused #gameField, .play-screen.is-paused #controlDock { filter: grayscale(.7); opacity: .62; }

.quiz-screen, .result-screen {
  align-content: center;
  justify-items: center;
  gap: 9px;
  padding: max(12px, env(safe-area-inset-top)) 14px max(12px, env(safe-area-inset-bottom));
  overflow: hidden;
  background: var(--paper);
  background-image: repeating-linear-gradient(0deg, transparent 0 27px, rgba(41,37,31,.07) 28px);
}
.quiz-roof { display: flex; align-items: end; gap: 4px; min-height: 42px; padding: 0 10px 5px; border-bottom: 4px solid var(--ink); }
.quiz-roof i { width: 22px; height: 18px; border: 2px solid var(--ink); background: #efd598; border-radius: 6px 4px; }.quiz-roof span { margin-left: 6px; font-weight: 900; }
.quiz-screen h2, .result-screen h2 { margin: 0; font-family: Georgia, "Yu Mincho", serif; font-size: clamp(26px, 6vw, 42px); }
.quiz-progress { position: absolute; top: max(12px, env(safe-area-inset-top)); right: 14px; margin: 0; font-weight: 900; }
.quiz-question { width: min(100%, 640px); margin: 2px 0; font-size: clamp(17px, 3vw, 22px); font-weight: 900; line-height: 1.45; text-align: center; }
.quiz-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; width: min(100%, 640px); }
.quiz-choice { min-height: 52px; padding: 5px 8px; border: 2px solid var(--ink); border-radius: 4px; background: #fff8dc; color: var(--ink); font-size: 16px; font-weight: 800; box-shadow: 2px 2px 0 var(--ink); }
.quiz-choice.is-correct::before { content: "○ "; }.quiz-choice.is-wrong::before { content: "× "; }
.quiz-choice.is-correct { background: #b9e1c7; }.quiz-choice.is-wrong { background: #f3c1b4; }
.quiz-explanation { width: min(100%, 640px); min-height: 46px; margin: 0; padding: 6px 9px; border-left: 5px solid var(--safe); background: #fff; font-size: 15px; line-height: 1.45; }
.quiz-screen > .primary-button { width: min(100%, 360px); }

.completed-tower { display: flex; flex-direction: column-reverse; align-items: center; width: 130px; }
.completed-tower i { width: calc(100% - var(--n, 0) * 7px); height: 16px; margin-top: -2px; border: 2px solid var(--ink); background: var(--brick); }
.completed-tower i:nth-child(2) { --n:1 }.completed-tower i:nth-child(3) { --n:2 }.completed-tower i:nth-child(4) { --n:3 }.completed-tower i:nth-child(5) { --n:4 }.completed-tower i:nth-child(6) { --n:5 }
.completed-tower b { font-size: 24px; color: var(--beam-dark); }
.result-numbers { display: flex; justify-content: center; gap: 10px; width: min(100%, 480px); }
.result-numbers p { flex: 1; margin: 0; padding: 6px; border: 2px solid var(--ink); background: #fff8dc; text-align: center; }
.result-numbers span { display: block; font-size: 14px; font-weight: 700; }.result-numbers strong { font-size: 24px; }
.result-summary { margin: 0; max-width: 620px; font-size: 15px; line-height: 1.45; text-align: center; }
.result-actions { display: grid; grid-template-columns: 1.4fr 1fr; gap: 8px; width: min(100%, 480px); }
.leaderboard-panel { position: absolute; z-index: 20; inset: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom)); padding: 10px; overflow: auto; border: 3px solid var(--ink); background: #fff8dc; box-shadow: 6px 6px 0 var(--ink); }
.leaderboard-panel::before { content: "ランキングを閉じるには下のボタンを押してください"; display: block; margin-bottom: 6px; font-size: 14px; font-weight: 800; }
.result-screen:has(.leaderboard-panel:not([hidden])) #rankingButton { position: relative; z-index: 30; align-self: end; }

.help-dialog { width: min(92vw, 560px); max-height: min(88vh, 680px); padding: 0; border: 3px solid var(--ink); background: var(--paper); color: var(--ink); box-shadow: 7px 7px 0 var(--ink); }
.help-dialog::backdrop { background: rgba(25,35,32,.72); }
.help-dialog form { padding: 18px; overflow: auto; }
.help-dialog h2 { margin: 0 0 10px; }.help-dialog ol { padding-left: 1.5em; line-height: 1.65; }.help-dialog button { width: 100%; }

@keyframes floor-in { from { opacity: 0; transform: translateX(calc(var(--offset, 0) * 8%)) translateY(-28px) scaleX(.75); } }
@keyframes burst { 0% { opacity: 0; transform: translate(-50%,-50%) scale(.2); } 40% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%,-50%) scale(1.5); } }
@keyframes danger-sway { from { transform: translateX(-50%) rotate(-1.1deg); } to { transform: translateX(-50%) rotate(1.1deg); } }

@media (max-width: 560px) {
  #startScreen { grid-template-columns: 44% 1fr; gap: 8px 12px; padding-left: 12px; padding-right: 12px; }
  .start-art { grid-row: 1 / 3; }.start-copy { align-self: center; }.start-actions { grid-column: 2; }.start-note { grid-column: 1 / 3; text-align: center; }
  .sun-disc { width: 54px; }.start-tower { width: 84%; }.start-wind { letter-spacing: 2px; }
  h1 { font-size: clamp(36px, 11vw, 50px); }
}

@media (max-width: 370px) {
  .play-screen { grid-template-rows: 52px minmax(160px,1fr) 126px auto; }
  #hud { grid-template-columns: minmax(58px,1fr) 42px minmax(58px,.85fr) 44px 44px 44px; gap: 2px; }
  #hud > div { padding-left: 3px; padding-right: 3px; }
  #hud strong { font-size: 15px; }
  .repair-button { width: 44px; min-width: 44px; }
  #gameField { grid-template-columns: minmax(160px,1fr) 104px; }
  .written-panel { padding-left: 4px; padding-right: 4px; }
  .tower { width: 72%; }.crane-beam { width: 43%; }
  .slider-row { grid-template-columns: 35px minmax(0,1fr) 49px; gap: 4px; padding: 0 5px; }
  .quiz-screen, .result-screen { gap: 6px; padding-left: 9px; padding-right: 9px; }
}

@media (max-height: 580px) {
  #app { min-height: 480px; }
  #startScreen { padding-top: 8px; padding-bottom: 8px; }
  .lead { margin-top: 7px; line-height: 1.35; }
  .start-note { display: none; }
  .quiz-screen, .result-screen { gap: 4px; padding-top: 7px; padding-bottom: 7px; }
  .completed-tower { width: 90px; }.completed-tower i { height: 11px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}

@media (max-width: 279px), (max-height: 499px) {
  #app { height: auto; min-height: 500px; overflow: visible; }
  body { overflow-y: auto; }
}
