:root {
  color-scheme: light;
  --ink: #263128;
  --paper: #f7efcf;
  --cream: #fff9df;
  --soil: #68452f;
  --soil-dark: #3d2b24;
  --leaf: #2f7d47;
  --leaf-light: #72aa55;
  --sun: #f4b83f;
  --alert: #a43b32;
  --blue: #397d98;
  --line: #2e372d;
  font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", system-ui, sans-serif;
  background: #d8ddba;
  color: var(--ink);
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-width: 300px;
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
}

body {
  min-height: 100svh;
  background:
    linear-gradient(90deg, rgba(56,74,45,.08) 1px, transparent 1px) 0 0 / 32px 32px,
    #d8ddba;
}

button { font: inherit; }
button:focus-visible, input:focus-visible { outline: 3px solid #174f63; outline-offset: 2px; }

#app {
  width: min(100%, 760px);
  height: 100svh;
  min-height: 568px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
  background: var(--paper);
  box-shadow: 0 0 0 2px rgba(46,55,45,.35), 0 12px 40px rgba(44,50,36,.22);
  overflow: hidden;
}

@supports (height: 100dvh) {
  #app { height: 100dvh; }
}

.screen {
  display: none;
  width: 100%;
  height: 100%;
  min-height: 0;
}

.screen.is-active { display: grid; }

.eyebrow {
  margin: 0;
  font-weight: 800;
  font-size: 14px;
  letter-spacing: .08em;
  color: #645436;
}

.primary, .secondary, .icon-button, .move-button, .build-button, .repair-button {
  min-height: 44px;
  border: 2px solid var(--line);
  border-radius: 6px 10px 5px 8px;
  box-shadow: 3px 3px 0 var(--line);
  cursor: pointer;
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
}

.primary { background: #f2c95c; padding: 10px 20px; }
.secondary { background: #fff8d6; padding: 10px 18px; }
.primary:active, .secondary:active, .icon-button:active, .move-button:active, .build-button:active, .repair-button:active {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 var(--line);
}

/* Start: a cut-paper poster rather than a floating card. */
.start-screen {
  grid-template-columns: minmax(0, 1.05fr) minmax(260px, .95fr);
  align-items: stretch;
  background: #dfe5bd;
}

.start-art {
  position: relative;
  min-height: 0;
  overflow: hidden;
  border-right: 3px solid var(--line);
  background: linear-gradient(#8fbfbd 0 55%, #79533a 55% 100%);
}

.sun-disc {
  position: absolute; width: 94px; height: 94px; border-radius: 50%;
  top: 8%; left: 12%; background: var(--sun); border: 3px solid var(--line);
  box-shadow: 8px 8px 0 rgba(77,74,39,.16);
}

.paper-cloud {
  position: absolute; width: 100px; height: 32px; border: 3px solid var(--line);
  border-radius: 60% 45% 50% 40%; background: #f5f0d9;
}
.cloud-a { top: 16%; right: 7%; transform: rotate(-4deg); }
.cloud-b { top: 29%; left: 17%; width: 70px; transform: rotate(6deg); }

.start-stem {
  position: absolute; width: 16px; height: 38%; left: 52%; bottom: 45%;
  border: 3px solid var(--line); background: var(--leaf); transform: rotate(-2deg); transform-origin: bottom;
}
.start-stem i, .start-stem b {
  position: absolute; width: 64px; height: 34px; top: 28%; background: var(--leaf-light);
  border: 3px solid var(--line); border-radius: 90% 10% 80% 15%;
}
.start-stem i { right: 9px; transform: rotate(15deg); }
.start-stem b { left: 9px; top: 48%; transform: scaleX(-1) rotate(12deg); }
.start-stem em {
  position: absolute; width: 42px; height: 42px; top: -30px; left: -16px;
  background: #efe166; border: 3px solid var(--line); border-radius: 55% 45% 50% 42%;
}

.soil-cut { position: absolute; inset: 55% 0 0; background: #79533a; }
.soil-cut::before, .soil-cut::after {
  content: ""; position: absolute; border-radius: 50%; background: #4d382d;
}
.soil-cut::before { width: 22px; height: 12px; left: 18%; top: 20%; box-shadow: 160px 60px 0 #4d382d, 70px 120px 0 #4d382d; }
.soil-cut::after { width: 12px; height: 8px; right: 15%; top: 34%; box-shadow: -100px 98px 0 #4d382d; }
.seed-shape {
  position: absolute; width: 44px; height: 29px; left: calc(52% - 21px); top: 14%;
  border: 3px solid var(--line); border-radius: 65% 35% 60% 40%; background: #d69e55; transform: rotate(-12deg);
}
.root-line { position: absolute; width: 5px; background: #e9d399; left: 52%; top: 23%; transform-origin: top; border-radius: 5px; }
.root-line.r1 { height: 53%; transform: rotate(8deg); }
.root-line.r2 { height: 36%; transform: rotate(-28deg); top: 44%; }

.start-copy {
  display: flex; flex-direction: column; justify-content: center; gap: 18px;
  padding: clamp(24px, 5vw, 54px);
  background: repeating-linear-gradient(-4deg, transparent 0 35px, rgba(72,91,57,.07) 36px 37px), var(--paper);
}
.start-copy h1 { margin: 0; font-family: serif; font-size: clamp(45px, 7vw, 72px); line-height: .98; letter-spacing: -.05em; }
.start-copy h1 span { display: inline-block; color: #27633d; transform: translateX(14%); }
.start-lead { margin: 0; font-size: 17px; line-height: 1.7; font-weight: 650; }
.start-actions { display: grid; grid-template-columns: 1.2fr .8fr; gap: 10px; }

/* Play screen uses a strict header/HUD/world/control height budget. */
.play-screen {
  grid-template-rows: 50px 48px minmax(0, 1fr) 104px;
  gap: 6px;
  padding: 0;
  background: #e8e6bd;
}

.play-header {
  display: grid; grid-template-columns: minmax(0,1fr) 44px 44px; gap: 6px; align-items: center;
  border-bottom: 2px solid var(--line); padding: 2px 4px 4px;
}
.mini-title { min-width: 0; display: flex; align-items: baseline; gap: 8px; }
.mini-title span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-family: serif; font-size: 17px; font-weight: 900; }
.mini-title small { flex: 0 0 auto; font-size: 14px; font-weight: 800; color: #625439; }
.icon-button { width: 44px; height: 44px; min-height: 44px; padding: 0; background: #fff9dc; }

.hud {
  display: grid; grid-template-columns: .75fr 1fr 1.45fr; gap: 5px;
}
.hud > div {
  min-width: 0; display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 5px;
  border: 2px solid var(--line); background: #f9f0c9; padding: 3px 7px;
}
.hud span { font-size: 14px; font-weight: 700; }
.hud strong { font-size: 17px; text-align: right; }
.hud .stability-stat { grid-template-columns: auto auto; grid-template-rows: 1fr 7px; }
.stability-stat i { grid-column: 1 / -1; display: block; height: 7px; border: 1px solid var(--line); background: #d6cba9; overflow: hidden; }
.stability-stat i b { display: block; height: 100%; width: 100%; background: var(--leaf); transition: width .28s ease, background .28s ease; }

.game-field {
  position: relative; min-height: 160px; overflow: hidden;
  display: grid; grid-template-rows: 28px 66px minmax(0, 1fr) 31px;
  border: 3px solid var(--line); background: #9cc5bb;
}
.sky-strip { display: flex; justify-content: space-between; align-items: center; padding: 2px 7px; border-bottom: 2px dashed rgba(38,49,40,.55); }
.wind-flag, .risk-mark { font-size: 14px; font-weight: 900; }
.risk-mark { color: #654110; background: #f2cf62; border: 1px solid var(--line); padding: 1px 5px; transform: rotate(1deg); }

.experiment-banner {
  z-index: 4; display: grid; grid-template-columns: minmax(0,1fr) 82px minmax(0,1fr); gap: 5px; align-items: stretch;
  padding: 4px 5px; background: rgba(247,239,207,.82); border-bottom: 2px solid var(--line);
}
.condition-card { min-width: 0; border: 2px solid var(--line); background: #fff9dc; padding: 3px 5px; font-size: 14px; line-height: 1.2; font-weight: 800; }
.condition-card strong { display: block; font-size: 14px; color: #7b392d; }
.condition-card span { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.compare-label { display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; }
.compare-label span { font-size: 14px; font-weight: 800; line-height: 1.1; }
.compare-label b { font-size: 14px; color: #8f392f; }

.tower-world { position: relative; min-height: 0; overflow: hidden; background: linear-gradient(#9cc5bb 0 56%, #755039 56% 100%); }
.tower-world::before {
  content: ""; position: absolute; inset: 56% 0 auto; height: 4px; background: #354833;
}
.tower { position: absolute; width: 58%; height: 82%; left: 21%; bottom: 4%; z-index: 2; }
.tower-trunk { position: absolute; left: calc(50% - 5px); width: 10px; height: 100%; bottom: 0; background: #7f5936; border: 2px solid var(--line); }
.tower-floor {
  position: absolute; left: 50%; width: 64%; height: 13%; transform: translateX(-50%);
  border: 2px solid var(--line); background: #e0b467; border-radius: 5px 8px 4px 7px;
  box-shadow: 0 3px 0 rgba(50,42,29,.28);
  animation: floor-set .45s cubic-bezier(.2,1.5,.5,1);
}
.tower-floor::before, .tower-floor::after { content: ""; position: absolute; width: 16px; height: 12px; top: -9px; background: #98633d; border: 2px solid var(--line); border-radius: 50%; }
.tower-floor::before { left: 5%; }
.tower-floor::after { right: 5%; }
.tower-floor strong { position: absolute; inset: 0; display: grid; place-items: center; font-size: 14px; }
.tower-floor.success { background: #efd16e; }
.tower-floor.shaky { transform: translateX(-50%) rotate(var(--tilt, 3deg)); background: #d88b62; }

.moving-beam {
  --beam-x: 0px;
  position: absolute; z-index: 3; width: 50%; height: 22px; left: 25%; top: 16%;
  transform: translateX(var(--beam-x)); transition: transform .16s ease;
  background: #f1c86a; border: 3px solid var(--line); border-radius: 5px;
}
.moving-beam i { position: absolute; left: calc(50% - 4px); top: 100%; width: 8px; height: 20px; background: #76533a; border: 2px solid var(--line); }
.pod { position: absolute; width: 29px; height: 18px; top: -18px; background: #bd7147; border: 2px solid var(--line); border-radius: 4px 4px 12px 12px; }
.pod-left { left: 3%; } .pod-right { right: 3%; }

.preview-sprout { position: absolute; z-index: 3; width: 5px; height: 22px; top: 5%; background: var(--leaf); border: 1px solid var(--line); opacity: .65; }
.preview-sprout::after { content: ""; position: absolute; width: 13px; height: 8px; top: 1px; left: 3px; background: var(--leaf-light); border: 1px solid var(--line); border-radius: 80% 15%; }
.left-sprout { left: 31%; } .right-sprout { right: 31%; }
.preview-sprout.is-low { height: 9px; opacity: .35; }
.preview-sprout.is-none { display: none; }

.soil-band { position: absolute; inset: 56% 0 0; overflow: hidden; pointer-events: none; }
.soil-band span, .soil-band i, .soil-band b { position: absolute; border-radius: 50%; background: #4d382d; }
.soil-band span { width: 10px; height: 7px; left: 8%; top: 20%; box-shadow: 80px 36px 0 #4d382d, 230px 21px 0 #4d382d, 340px 57px 0 #4d382d; }
.soil-band i { width: 16px; height: 9px; left: 38%; top: 55%; }
.soil-band b { width: 7px; height: 6px; right: 12%; top: 24%; }

.field-message {
  z-index: 5; margin: 0; display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 3px 7px; min-width: 0; border-top: 2px solid var(--line); background: #fff4c9;
  font-size: 14px; font-weight: 800; line-height: 1.15;
}
.field-message.success { background: #d9edbd; }
.field-message.danger { background: #f3c1a6; }

.controls {
  display: grid; grid-template-columns: 1fr 1fr 1.25fr; grid-template-rows: 48px 48px; gap: 5px;
}
.move-button { background: #d5e5d6; }
.build-button { grid-row: 1 / 3; grid-column: 3; background: #efc861; }
.repair-button { grid-column: 1 / 3; background: #c8ded9; }
.repair-button:disabled { opacity: .48; cursor: not-allowed; }

.quiz-screen, .collapse-screen, .result-screen {
  padding: clamp(14px, 4vw, 32px);
  align-content: center;
  gap: 10px;
  background: repeating-linear-gradient(0deg, transparent 0 33px, rgba(71,83,57,.09) 34px 35px), #f7efcf;
}
.quiz-top { display: flex; justify-content: space-between; align-items: center; }
.quiz-top span { font-size: 15px; font-weight: 900; border: 2px solid var(--line); padding: 4px 8px; background: #fff8d7; }
.quiz-screen h2, .collapse-screen h2, .result-screen h2 { margin: 0; font-family: serif; font-size: clamp(24px, 5vw, 34px); line-height: 1.2; }
.quiz-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.quiz-choice { min-height: 58px; border: 2px solid var(--line); background: #fff9df; font-size: 15px; font-weight: 800; text-align: left; padding: 7px 10px; cursor: pointer; }
.quiz-choice:disabled { color: var(--ink); opacity: .65; }
.quiz-feedback { border: 2px solid var(--line); background: #e4edc8; padding: 9px; }
.quiz-feedback strong { font-size: 17px; }
.quiz-feedback p { margin: 5px 0 9px; font-size: 14px; line-height: 1.5; }

.collapse-screen { text-align: center; justify-items: center; }
.collapse-screen > p:not(.eyebrow) { margin: 0; max-width: 600px; font-size: 16px; line-height: 1.6; }
.collapse-art { position: relative; width: 170px; height: 110px; }
.collapse-art span { position: absolute; width: 12px; height: 80px; left: 78px; bottom: 0; background: #805b39; border: 2px solid var(--line); transform: rotate(12deg); }
.collapse-art i, .collapse-art b { position: absolute; width: 105px; height: 16px; background: #dda95d; border: 2px solid var(--line); }
.collapse-art i { left: 23px; top: 45px; transform: rotate(-12deg); }
.collapse-art b { left: 57px; top: 73px; transform: rotate(18deg); }

.result-screen { text-align: center; justify-items: center; }
.result-plant { position: relative; width: 110px; height: 110px; }
.result-plant span { position: absolute; left: 50px; bottom: 0; width: 11px; height: 85px; background: var(--leaf); border: 2px solid var(--line); }
.result-plant i, .result-plant b, .result-plant em { position: absolute; width: 48px; height: 27px; background: var(--leaf-light); border: 2px solid var(--line); border-radius: 90% 10%; }
.result-plant i { left: 10px; top: 32px; transform: rotate(17deg); }
.result-plant b { right: 7px; top: 49px; transform: scaleX(-1) rotate(10deg); }
.result-plant em { left: 32px; top: 4px; background: #f1d759; border-radius: 50%; }
.final-score { min-width: 220px; border: 3px solid var(--line); background: #f4cf69; padding: 6px 16px; transform: rotate(-1deg); }
.final-score span, .final-score small { display: block; font-size: 14px; font-weight: 800; }
.final-score strong { display: block; font-size: 34px; line-height: 1; }
.result-summary { margin: 0; max-width: 570px; font-size: 15px; line-height: 1.55; }
.result-actions { display: grid; grid-template-columns: 1.3fr .9fr; gap: 9px; width: min(100%, 430px); }

.paper-dialog {
  width: min(92vw, 620px); max-height: min(86vh, 720px); border: 3px solid var(--line); border-radius: 4px;
  background: #fff7d7; color: var(--ink); padding: 0; box-shadow: 10px 10px 0 rgba(38,49,40,.35);
}
.paper-dialog::backdrop { background: rgba(30,38,31,.68); }
.paper-dialog form { position: relative; overflow-y: auto; max-height: min(84vh, 700px); padding: 22px; }
.paper-dialog h2 { margin: 0 45px 10px 0; font-family: serif; font-size: 28px; }
.paper-dialog p, .paper-dialog li { font-size: 15px; line-height: 1.65; }
.paper-dialog ol { padding-left: 24px; }
.dialog-close { position: absolute; right: 10px; top: 10px; width: 44px; height: 44px; border: 2px solid var(--line); background: #f5dfab; font-size: 25px; cursor: pointer; }
.compact-dialog { width: min(90vw, 420px); text-align: center; }
.ranking-dialog { width: min(94vw, 700px); }
.ranking-note { margin-top: 0; }
#leaderboard { min-height: 140px; font-size: 14px; }

.burst { animation: release-glow .7s ease; }
.shake { animation: warning-shake .38s ease; }
.paused .tower-world::after { content: "休けい中"; position: absolute; inset: 0; z-index: 9; display: grid; place-items: center; background: rgba(247,239,207,.72); font-size: 22px; font-weight: 900; }

@keyframes floor-set { from { opacity: 0; transform: translate(-50%, -24px) scale(.85); } to { opacity: 1; transform: translate(-50%, 0) scale(1); } }
@keyframes release-glow { 0%,100% { filter: none; } 45% { filter: brightness(1.25) saturate(1.4); } }
@keyframes warning-shake { 0%,100% { transform: translateX(0); } 33% { transform: translateX(-3px); } 66% { transform: translateX(3px); } }

@media (max-width: 640px) {
  #app { width: 100%; min-height: 0; box-shadow: none; }
  .start-screen { grid-template-columns: 1fr; grid-template-rows: minmax(180px, 42%) minmax(0, 58%); }
  .start-art { border-right: 0; border-bottom: 3px solid var(--line); }
  .start-copy { gap: 10px; padding: 12px 16px; justify-content: center; }
  .start-copy h1 { font-size: clamp(39px, 13vw, 54px); }
  .start-lead { font-size: 14px; line-height: 1.45; }
  .sun-disc { width: 58px; height: 58px; }
  .paper-cloud { width: 72px; height: 24px; }
  .start-actions { gap: 7px; }
  .play-screen { grid-template-rows: 46px 45px minmax(0, 1fr) 101px; gap: 4px; }
  .play-header { grid-template-columns: minmax(0,1fr) 44px 44px; gap: 4px; }
  .mini-title span { font-size: 15px; }
  .mini-title small { display: none; }
  .hud { gap: 3px; }
  .hud > div { padding: 2px 4px; gap: 3px; }
  .hud span { font-size: 14px; }
  .hud strong { font-size: 15px; }
  .game-field { grid-template-rows: 26px 62px minmax(0,1fr) 31px; }
  .experiment-banner { grid-template-columns: minmax(0,1fr) 68px minmax(0,1fr); gap: 3px; padding: 3px; }
  .condition-card { padding: 2px 3px; }
  .compare-label span { line-height: 1; }
  .tower { width: 70%; left: 15%; }
  .tower-floor { width: 66%; }
  .moving-beam { width: 62%; left: 19%; }
  .controls { gap: 4px; grid-template-rows: 48px 48px; }
  .move-button, .build-button, .repair-button { font-size: 14px; }
  .quiz-screen, .collapse-screen, .result-screen { padding: 11px; gap: 8px; }
  .quiz-choices { gap: 6px; }
  .quiz-choice { min-height: 52px; font-size: 14px; padding: 6px; }
  .quiz-feedback p { line-height: 1.35; }
  .result-plant { width: 90px; height: 82px; transform: scale(.78); margin: -8px 0; }
  .result-summary { font-size: 14px; line-height: 1.35; }
}

@media (max-width: 340px), (max-height: 590px) {
  .start-screen { grid-template-rows: 185px minmax(0,1fr); }
  .start-copy { padding: 8px 12px; gap: 7px; }
  .start-copy h1 { font-size: 39px; }
  .start-lead br { display: none; }
  .play-screen { grid-template-rows: 44px 44px minmax(0,1fr) 96px; }
  .game-field { grid-template-rows: 24px 58px minmax(0,1fr) 29px; }
  .condition-card span { font-size: 14px; }
  .controls { grid-template-rows: 46px 46px; }
  .quiz-screen h2, .collapse-screen h2, .result-screen h2 { font-size: 23px; }
  .quiz-choice { min-height: 48px; }
}

@media (min-width: 900px) {
  #app { max-height: 900px; }
  .game-field { margin-inline: 8px; }
  .controls { margin: 0 8px 6px; }
}

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