:root {
  --ink: #183044;
  --cream: #fff8df;
  --paper: #f3e5ba;
  --blue: #2a7fa3;
  --blue-dark: #15516c;
  --yellow: #f2bd43;
  --coral: #e86f51;
  --mint: #6fbf9a;
  --danger: #b9433f;
  --shadow: #0d2c3f33;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-width: 280px; min-height: 100%; }
body {
  color: var(--ink);
  background: #16394b;
  font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", system-ui, sans-serif;
  font-size: 14px;
}
button { font: inherit; color: inherit; }
button:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }
[hidden] { display: none !important; }

.game-shell {
  width: min(100%, 980px);
  height: 100svh;
  height: 100dvh;
  min-height: 0;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: grid;
  grid-template-rows: 44px minmax(0, 1fr);
  background: var(--cream);
  box-shadow: 0 0 40px #06192388;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-width: 0;
  padding: 0 8px 0 12px;
  color: #fff;
  background: linear-gradient(to bottom, var(--ink) 0 calc(100% - 4px), var(--yellow) calc(100% - 4px));
}
.brand { display: flex; align-items: center; gap: 8px; letter-spacing: .08em; }
.brand-cube { width: 20px; height: 20px; background: var(--coral); border: 2px solid #fff; transform: rotate(30deg) skew(-8deg); }
.top-actions { display: flex; gap: 4px; }
.icon-button, .sound-button { min-width: 44px; height: 44px; border: 0; color: #fff; background: transparent; font-weight: 800; }
.sound-button { min-width: 70px; }

.screen { min-height: 0; position: relative; }
.primary-button, .secondary-button, .tool-button {
  min-height: 44px;
  border: 3px solid var(--ink);
  border-radius: 8px 8px 3px 3px;
  font-weight: 900;
  box-shadow: 0 4px 0 var(--ink);
  cursor: pointer;
}
.primary-button { color: #fff; background: var(--coral); padding: 8px 20px; }
.secondary-button { background: #fff8df; padding: 8px 14px; }
.primary-button:active, .secondary-button:active, .tool-button:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }

.start-screen {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(220px, .9fr);
  grid-template-rows: minmax(0, 1fr) auto;
  grid-template-areas: "world copy" "world actions";
  background: #f7e7aa;
}
.start-world { grid-area: world; position: relative; min-height: 0; background: #77bfd1; border-right: 8px solid var(--ink); }
.start-world::before { content: ""; position: absolute; inset: auto 0 0; height: 33%; background: #d79a56; border-top: 7px solid var(--ink); }
.start-world::after { content: ""; position: absolute; width: 190px; height: 190px; left: 50%; top: 48%; border: 5px dashed #ffffffaa; border-radius: 50%; transform: translate(-50%, -50%); }
.toy-crate { position: absolute; z-index: 2; width: 230px; height: 180px; left: 50%; top: 52%; transform: translate(-50%, -50%) skewY(-4deg); border: 12px solid var(--ink); border-top-width: 7px; background: #fff5c7; box-shadow: 16px 18px 0 #0d2c3f44; }
.toy-block { position: absolute; width: 64px; height: 56px; border: 5px solid var(--ink); box-shadow: inset -10px -9px 0 #0002; }
.b1 { left: 8px; bottom: 8px; background: var(--coral); }.b2 { left: 76px; bottom: 8px; background: var(--blue); }.b3 { left: 144px; bottom: 8px; background: var(--yellow); }
.b4 { left: 8px; bottom: 68px; background: var(--yellow); }.b5 { left: 76px; bottom: 68px; background: var(--coral); }.b6 { left: 144px; bottom: 68px; background: var(--blue); }
.toy-block.missing { opacity: .2; border-style: dashed; animation: missing-pulse 1.4s ease-in-out infinite; }
.crane-hook { position: absolute; z-index: 3; left: 72%; top: -10px; width: 6px; height: 110px; background: var(--ink); }
.crane-hook::after { content: ""; position: absolute; left: -16px; bottom: -24px; width: 32px; height: 32px; border: 7px solid var(--ink); border-top: 0; border-radius: 0 0 22px 22px; }
.start-copy { grid-area: copy; align-self: end; padding: 20px 26px 4px; }
.eyebrow { margin: 0 0 6px; color: var(--blue-dark); font-weight: 900; letter-spacing: .08em; }
.start-copy h1 { margin: 0; font-size: clamp(34px, 5.2vw, 66px); line-height: 1.02; letter-spacing: .03em; }
.start-copy p:last-child { font-size: clamp(15px, 2vw, 19px); line-height: 1.6; }
.start-actions { grid-area: actions; padding: 8px 26px 24px; }
.start-actions .primary-button { width: min(100%, 330px); }
.start-actions p { margin: 10px 0 0; font-size: 14px; font-weight: 700; }

.play-screen {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(218px, 1fr) auto 44px;
  grid-template-areas: "hud" "field" "tray" "controls";
  gap: 4px;
  padding: 5px;
  background: #ead8a3;
}
.hud { grid-area: hud; min-height: 45px; display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.hud > div { min-width: 0; padding: 3px 5px; border: 2px solid var(--ink); background: #fff9df; box-shadow: 2px 2px 0 var(--shadow); }
.hud span { display: block; font-size: 14px; font-weight: 800; line-height: 1.1; }
.hud strong { display: block; font-size: clamp(14px, 3.6vw, 18px); line-height: 1.1; overflow-wrap: anywhere; }
.lift-gauge.danger { color: #fff; background: var(--danger); animation: warning-pulse 1s ease-in-out infinite; }

.game-field {
  grid-area: field;
  position: relative;
  min-width: 0;
  min-height: 218px;
  display: grid;
  grid-template-rows: auto minmax(160px, 1fr);
  gap: 6px;
  padding: 6px 8px 10px;
  border: 4px solid var(--ink);
  background: #77bfd1;
  touch-action: pan-y;
}
.world-backdrop { position: absolute; inset: 0; background: linear-gradient(#76c1d3 0 62%, #cf9051 62%); }
.world-backdrop::before { content: ""; position: absolute; left: 4%; top: 10%; width: 13%; height: 36%; border: 5px solid var(--ink); background: #f6cf5d; transform: skewY(-5deg); box-shadow: 0 0 0 6px #fff3; }
.world-backdrop::after { content: ""; position: absolute; right: 3%; bottom: 7%; width: 17%; height: 44%; background: repeating-linear-gradient(90deg, var(--ink) 0 7px, transparent 7px 24px); border-bottom: 8px solid var(--ink); opacity: .65; }
.world-backdrop span { position: absolute; width: 90px; height: 22px; border: 4px solid var(--ink); border-radius: 50%; background: #fff8df; opacity: .75; }
.world-backdrop span:nth-child(1) { top: 9%; left: 22%; }.world-backdrop span:nth-child(2) { top: 17%; right: 18%; transform: scale(.65); }.world-backdrop span:nth-child(3) { top: 40%; left: 3%; transform: scale(.5); }
.crate-heading { position: relative; z-index: 3; display: grid; grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr); align-items: start; gap: 8px; }
.crate-heading > div { padding: 3px 7px; color: #fff; background: var(--ink); transform: rotate(-1deg); }
.crate-heading span, .crate-heading strong { display: block; font-size: 14px; }
.crate-heading p { min-width: 0; margin: 0; padding: 4px 7px; border: 2px solid var(--ink); background: #fff8df; font-size: 14px; font-weight: 800; line-height: 1.2; overflow-wrap: anywhere; }
.crate-grid {
  position: relative;
  z-index: 4;
  width: min(76%, 390px);
  height: 100%;
  min-width: 160px;
  min-height: 160px;
  justify-self: center;
  align-self: stretch;
  display: grid;
  gap: 5px;
  padding: 9px;
  border: 9px solid var(--ink);
  border-top-width: 5px;
  background: #f5e3ad;
  box-shadow: 12px 13px 0 #0d2c3f55;
  transform: skewY(-2deg);
}
.cube-cell { position: relative; min-width: 0; min-height: 0; border: 3px solid var(--ink); background: #fff7d6; box-shadow: inset 0 -7px 0 #00000012; }
.cube-cell::before { content: attr(data-height); position: absolute; z-index: 2; right: 3px; bottom: 1px; font-size: 14px; font-weight: 900; }
.cube-cell[data-height="0"]::before { color: #6b756f; }
.height-bars { position: absolute; inset: 4px; display: flex; flex-direction: column-reverse; gap: 2px; }
.height-bars i { flex: 1; min-height: 3px; border: 1px solid #18304466; background: var(--cell-color, var(--blue)); }
.cube-cell.drag-over { outline: 5px solid #fff; outline-offset: -6px; filter: brightness(1.15); }
.cube-cell.invalid-over { outline-color: var(--danger); }
.cube-cell.keyboard-cursor { outline: 4px dashed #fff; outline-offset: -6px; }
.release-burst { position: absolute; z-index: 8; inset: 45% auto auto 50%; padding: 8px 16px; border: 4px solid var(--ink); color: #fff; background: var(--mint); font-size: clamp(22px, 7vw, 40px); font-weight: 1000; transform: translate(-50%, -50%) rotate(-4deg) scale(0); pointer-events: none; }
.release-burst.show { animation: release-pop .85s ease-out; }
.drag-ghost { position: fixed; z-index: 50; width: 70px; min-height: 50px; padding: 5px; border: 4px solid var(--ink); background: var(--yellow); box-shadow: 8px 8px 0 #0d2c3f44; font-size: 14px; font-weight: 900; pointer-events: none; transform: translate(-50%, -60%); }

.tray-area { grid-area: tray; min-width: 0; display: grid; grid-template-columns: 80px minmax(0, 1fr); gap: 4px; }
.tray-label { padding: 3px 5px; border: 3px solid var(--ink); background: #fff8df; }
.tray-label strong, .tray-label span { display: block; }
.tray-label span { font-size: 14px; line-height: 1.2; overflow-wrap: anywhere; }
.piece-tray { min-width: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; }
.piece-token { position: relative; min-width: 0; min-height: 64px; padding: 4px 3px; border: 3px solid var(--ink); background: #fff8df; box-shadow: 0 4px 0 var(--ink); font-size: 14px; font-weight: 900; line-height: 1.15; overflow-wrap: anywhere; cursor: grab; touch-action: none; }
.piece-token[data-color="blue"] { border-top: 12px solid var(--blue); }.piece-token[data-color="yellow"] { border-top: 12px solid var(--yellow); }.piece-token[data-color="coral"] { border-top: 12px solid var(--coral); }
.piece-token.selected { outline: 4px solid #fff; background: #def5ff; }
.piece-token:disabled { opacity: .4; cursor: default; }
.piece-token .dims { display: block; padding-top: 12px; font-size: 14px; }
.piece-token .count { position: absolute; right: 3px; top: 1px; padding: 1px 4px; color: #fff; background: var(--ink); font-size: 14px; }
.piece-token .star { color: #9b4b00; }
.control-row { grid-area: controls; display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4px; min-height: 44px; }
.tool-button { min-width: 0; min-height: 44px; padding: 3px; background: #fff8df; font-size: 14px; }
.tool-button.rescue { background: #bfe7d2; }
.tool-button:disabled { opacity: .45; box-shadow: none; }

.panel-overlay { position: absolute; z-index: 40; inset: 0; display: grid; place-items: center; padding: 12px; background: #102b3bd9; }
.panel-card { width: min(100%, 430px); max-height: 100%; padding: 14px; border: 5px solid var(--ink); background: #fff8df; box-shadow: 10px 10px 0 #06192366; text-align: center; }
.panel-card h2 { margin: 4px 0; font-size: clamp(22px, 6vw, 32px); }.panel-card p { margin: 5px 0 10px; }.panel-card button { width: 100%; margin-top: 7px; }
.panel-kicker { font-size: 14px; font-weight: 1000; letter-spacing: .1em; }.success-card { border-color: var(--mint); }.danger-card { border-color: var(--coral); }

.quiz-screen, .result-screen { padding: clamp(10px, 3vw, 24px); background: #f6e8b9; overflow-y: auto; }
.quiz-screen { display: grid; grid-template-rows: auto auto auto minmax(0, 1fr) auto 48px; gap: 7px; align-content: center; }
.quiz-cube { width: 54px; height: 54px; position: relative; margin: 0 auto; transform: rotate(30deg); }.quiz-cube span { position: absolute; inset: 8px; border: 4px solid var(--ink); background: var(--yellow); }.quiz-cube span:nth-child(2) { transform: translate(12px, -8px); background: var(--coral); }.quiz-cube span:nth-child(3) { transform: translate(6px, -4px); background: var(--blue); }
.quiz-screen h2 { margin: 0 auto; max-width: 720px; font-size: clamp(18px, 4vw, 25px); line-height: 1.35; text-align: center; }
.quiz-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; width: min(100%, 680px); margin: 0 auto; }
.quiz-choice { min-height: 48px; border: 3px solid var(--ink); background: #fff; font-size: 16px; font-weight: 800; }
.quiz-choice.correct { background: #bfe7d2; }.quiz-choice.wrong { background: #ffd1c7; }
.quiz-feedback { min-height: 42px; margin: 0 auto; max-width: 720px; font-size: 14px; line-height: 1.35; text-align: center; }
.quiz-screen > .primary-button { width: min(100%, 420px); margin: 0 auto; }

.result-screen { text-align: center; }
.result-stack { width: 94px; height: 42px; margin: 2px auto 8px; display: flex; align-items: end; }.result-stack i { width: 32px; border: 3px solid var(--ink); background: var(--blue); }.result-stack i:nth-child(1) { height: 27px; }.result-stack i:nth-child(2) { height: 42px; background: var(--coral); }.result-stack i:nth-child(3) { height: 34px; background: var(--yellow); }
.result-screen h2 { margin: 3px 0 8px; font-size: clamp(22px, 5vw, 34px); }
.result-score { display: inline-flex; align-items: baseline; gap: 12px; padding: 5px 16px; border: 4px solid var(--ink); background: #fff8df; }.result-score strong { font-size: clamp(30px, 8vw, 48px); }
.result-stats { display: flex; justify-content: center; flex-wrap: wrap; gap: 5px 16px; }.result-stats p { margin: 8px 0; }
.result-message { margin: 3px auto 8px; max-width: 620px; font-size: 14px; }.result-actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; }
.ranking-panel { width: min(100%, 650px); max-height: 220px; margin: 10px auto 0; padding: 8px; border: 3px solid var(--ink); background: #fff; overflow: auto; text-align: left; }

.help-panel { position: fixed; z-index: 100; inset: 0; display: grid; place-items: center; padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom)); background: #061923dd; }
.help-sheet { position: relative; width: min(100%, 650px); max-height: 100%; overflow-y: auto; padding: 18px 18px 14px; border: 5px solid var(--yellow); background: #fff8df; }.help-sheet h2 { margin: 0 48px 8px 0; }.help-sheet li { margin: 8px 0; line-height: 1.45; }.help-sheet p { font-size: 14px; line-height: 1.5; }
.close-button { position: absolute; right: 6px; top: 6px; width: 44px; height: 44px; border: 3px solid var(--ink); background: var(--coral); color: #fff; font-size: 24px; font-weight: 900; }

@keyframes missing-pulse { 50% { opacity: .55; } }
@keyframes warning-pulse { 50% { filter: brightness(1.25); } }
@keyframes release-pop { 0% { transform: translate(-50%, -50%) rotate(-4deg) scale(.3); } 28% { transform: translate(-50%, -50%) rotate(2deg) scale(1.1); } 75% { transform: translate(-50%, -50%) rotate(-2deg) scale(1); opacity: 1; } 100% { transform: translate(-50%, -65%) scale(.9); opacity: 0; } }

@media (max-width: 620px) {
  .start-screen {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(170px, 1fr) auto auto;
    grid-template-areas: "world" "copy" "actions";
  }
  .start-world { border-right: 0; border-bottom: 6px solid var(--ink); }
  .start-copy { align-self: auto; padding: 10px 12px 2px; text-align: center; }
  .start-actions { padding: 6px 12px 14px; text-align: center; }
  .crane-hook { top: 0; }
  .toy-crate { width: 172px; height: 146px; }.toy-block { width: 46px; height: 46px; }.b2 { left: 55px; }.b3 { left: 102px; }.b4 { left: 8px; bottom: 58px; }.b5 { left: 55px; bottom: 58px; }.b6 { left: 102px; bottom: 58px; }
  .crate-grid { width: min(82%, 330px); }
  .tray-area { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto; gap: 3px; }
  .tray-label { min-height: 28px; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 2px 5px; }
  .piece-token { min-height: 72px; }
}

@media (max-width: 390px) {
  .game-shell { grid-template-rows: 44px minmax(0, 1fr); }
  .brand { font-size: 14px; }
  .start-copy h1 { font-size: 30px; }
  .play-screen { grid-template-rows: auto minmax(218px, 1fr) auto 44px; gap: 3px; padding: 3px; }
  .hud > div { padding: 2px 3px; }.hud span, .hud strong { font-size: 14px; }
  .game-field { padding: 4px 5px 8px; }
  .crate-heading { gap: 4px; }.crate-heading > div { padding: 2px 4px; }.crate-heading span, .crate-heading strong, .crate-heading p { font-size: 14px; }
  .crate-grid { padding: 6px; gap: 3px; border-width: 7px; border-top-width: 4px; }
  .tray-label strong, .tray-label span { font-size: 14px; }
  .piece-tray { gap: 3px; }.piece-token, .piece-token .dims, .piece-token .count { font-size: 14px; }
  .tool-button { font-size: 14px; }
  .quiz-screen { padding: 8px; gap: 4px; }.quiz-cube { width: 42px; height: 42px; }.quiz-choice { font-size: 14px; }
}

@media (max-height: 600px) and (min-width: 621px) {
  .start-copy { padding-top: 8px; }.start-actions { padding-bottom: 10px; }
  .play-screen { grid-template-rows: auto minmax(218px, 1fr) auto 44px; }
  .quiz-cube { display: none; }
}

@media (max-height: 540px), (max-width: 300px) {
  body { overflow-y: auto; }
  .game-shell { height: auto; min-height: 568px; overflow: visible; }
}

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