:root {
  --bg: #fff3e4;
  --ink: #5a3a24;
  --muted: #b08968;
  --orange: #ea580c;
  --orange-deep: #c2410c;
  --wood: #e0a066;
  --wood-deep: #c98448;
  --cell: #fffaf1;
  --given: #fff1d6;
  --safe-t: max(10px, env(safe-area-inset-top));
  --safe-b: max(14px, env(safe-area-inset-bottom));
  --safe-x: max(14px, env(safe-area-inset-left), env(safe-area-inset-right));
  font-family: Manrope, "Noto Sans SC", system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
button { appearance: none; }
body {
  min-height: 100dvh;
  background:
    radial-gradient(circle at 12% 8%, #ffe9c4 0 18%, transparent 19%),
    radial-gradient(circle at 88% 12%, #ffd9b0 0 14%, transparent 15%),
    linear-gradient(#fff6ea, #ffe7cc 58%, #ffd8b0);
  overflow-x: hidden;
}

.room { position: fixed; inset: 0; pointer-events: none; z-index: 0; }
.cloud {
  position: absolute; background: #fff; border-radius: 999px;
  box-shadow: 18px 8px 0 #fff, 36px 4px 0 #fff;
  opacity: .7;
}
.c1 { width: 54px; height: 22px; top: 18px; left: 18px; }
.c2 { width: 40px; height: 16px; top: 28px; right: 64px; }
.star-deco { position: absolute; color: #fbbf24; font-size: 18px; }
.s1 { top: 22px; right: 28px; }
.s2 { top: 58px; left: 46%; }

.app {
  position: relative; z-index: 1;
  width: min(1100px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  padding: var(--safe-t) var(--safe-x) var(--safe-b);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
}

.hero {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
}
.mascot {
  width: 58px; height: 58px; border-radius: 22px;
  box-shadow: 0 6px 0 #e07a28;
}
.title-cloud {
  background: #fff8ee;
  border-radius: 28px;
  padding: 8px 16px;
  box-shadow: 0 5px 0 #f0d2b0;
  min-width: 0;
}
.kicker { margin: 0; font-size: 11px; font-weight: 800; color: var(--orange); }
h1, h2, p { margin: 0; }
h1 { font-size: clamp(20px, 5vw, 32px); letter-spacing: -.04em; color: #ea580c; }

.ghost, .key, .cell, .hint-btn, .win-btn, .difficulty, .mode, .erase, .rest-secret {
  border: 0; cursor: pointer; touch-action: manipulation; font: inherit; background: none;
}
.hero-actions { display: flex; align-items: center; gap: 8px; }
.sound-btn { min-width: 44px; font-size: 18px; }
.sound-btn[aria-pressed="true"] { opacity: .55; }
.ghost {
  color: var(--muted);
  font-size: 13px; font-weight: 800; padding: 8px 10px; border-radius: 16px;
  background: rgba(255,255,255,.55);
}

.play {
  min-height: 0;
  display: grid;
  gap: 12px;
}
.board-wrap {
  min-height: 0;
  display: grid;
  justify-items: center;
  align-content: center;
}
.board-frame {
  width: min(100%, 58dvh, 460px);
  aspect-ratio: 1;
  padding: 14px;
  background: linear-gradient(#f0b072, #d98a45);
  border-radius: 28px;
  box-shadow: 0 10px 0 #b56b32, inset 0 2px 0 rgba(255,255,255,.35);
}
.board {
  --n: 4;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(var(--n), minmax(0, 1fr));
  grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  gap: 8px;
  padding: 6px;
  background: #c98448;
  border-radius: 18px;
}
.cell {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border-radius: 14px;
  background: var(--cell);
  color: #ef6b4a;
  font-size: clamp(16px, calc(34vw / var(--n)), 42px);
  font-weight: 800;
  line-height: 1;
  display: grid;
  place-items: center;
  box-shadow: 0 3px 0 #ead7be;
}
.cell.given { background: var(--given); color: #ef6b4a; }
.cell.user { color: #ea580c; }
.cell.selected {
  outline: 4px solid #fbbf24;
  outline-offset: -2px;
  background: #fff7d6;
  z-index: 1;
}
.cell.wrong { background: #ffd6c8; color: #c2410c; }
.cell.hinted { background: #e7f5df; color: #4f8a47; }
.toys {
  display: grid;
  gap: 10px;
  align-content: start;
}
.hud {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) auto minmax(0, 1fr);
  gap: 8px;
  align-items: stretch;
}
.hud-item {
  background: #fff7ed;
  border-radius: 20px;
  min-height: 58px;
  padding: 8px 10px;
  box-shadow: 0 5px 0 #f0d2b0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-weight: 800;
  white-space: nowrap;
}
.level-pill {
  background: linear-gradient(#fb923c, #ea580c);
  color: #fff;
  box-shadow: 0 5px 0 #c2410c;
}
.hint-btn { color: #c2410c; }
.hint-btn:disabled { opacity: .45; }
.star-jar { display: flex; gap: 2px; font-size: 16px; }
.star { color: #fde68a; text-shadow: 0 1px 0 #f59e0b; }
.star.on { color: #fbbf24; }
.timer-card strong {
  font-family: "DM Mono", ui-monospace, monospace;
  font-size: 18px;
}
.cookie { font-size: 20px; }

.keypad {
  background: #fff7ed;
  border-radius: 24px;
  padding: 10px;
  box-shadow: 0 5px 0 #f0d2b0;
}
.keypad-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.mode-pills { display: flex; background: #fff; padding: 4px; border-radius: 14px; }
.mode {
  min-width: 44px; min-height: 32px; border-radius: 10px;
  color: #8a6b55; font-weight: 800;
}
.mode.active { background: #fff0dd; color: #aa4e12; box-shadow: inset 0 0 0 2px #ed8a2e; }
.erase { color: var(--muted); font-weight: 800; padding: 6px 8px; }
.keys {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}
body[data-size="4"] .keys { grid-template-columns: repeat(2, minmax(0, 1fr)); }
body[data-size="6"] .keys,
body[data-size="9"] .keys { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.key {
  width: 100%;
  height: 48px;
  min-height: 48px;
  border-radius: 14px;
  color: #fff;
  font-size: 22px;
  font-weight: 800;
  display: grid;
  place-items: center;
  box-shadow: 0 4px 0 rgba(0,0,0,.12);
}
.key.c0 { background: #fb7185; box-shadow: 0 5px 0 #e11d48; }
.key.c1 { background: #fbbf24; box-shadow: 0 5px 0 #d97706; }
.key.c2 { background: #4ade80; box-shadow: 0 5px 0 #16a34a; }
.key.c3 { background: #60a5fa; box-shadow: 0 5px 0 #2563eb; }
.key.c4 { background: #c084fc; box-shadow: 0 5px 0 #7c3aed; }
.key.c5 { background: #fb923c; box-shadow: 0 5px 0 #c2410c; }
.key.c6 { background: #22d3ee; box-shadow: 0 5px 0 #0891b2; }
.key.c7 { background: #f472b6; box-shadow: 0 5px 0 #db2777; }
.key.c8 { background: #a3e635; box-shadow: 0 5px 0 #65a30d; }
.key:active { transform: translateY(3px); box-shadow: none; }

.worlds {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}
.world {
  background: #fff8ee;
  border-radius: 22px;
  min-height: 88px;
  padding: 10px 8px;
  display: grid;
  justify-items: center;
  gap: 4px;
  color: #8a6b55;
  font-size: 12px;
  font-weight: 800;
  box-shadow: 0 5px 0 #f0d2b0;
}
.world-icon { font-size: 28px; line-height: 1; }
.world.active {
  background: #fff0dd;
  color: #c2410c;
  box-shadow: 0 5px 0 #ed8a2e, inset 0 0 0 3px #ed8a2e;
}

.toast, .win { position: fixed; z-index: 30; }
.toast {
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translate(-50%, 16px);
  opacity: 0; pointer-events: none;
  max-width: min(90vw, 360px);
  background: #263238; color: #fff;
  padding: 12px 16px; border-radius: 14px; font-size: 13px; font-weight: 700;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.win {
  inset: 0; display: none; place-items: center;
  background: rgba(43, 33, 24, .42); padding: 24px;
}
.win.show { display: grid; }
.win-card {
  width: min(92vw, 360px); background: #fff; border-radius: 28px;
  padding: 28px 22px; text-align: center;
  box-shadow: 0 20px 50px rgba(194, 65, 12, .18);
}
.win-mascot { font-size: 42px; margin-bottom: 6px; }
.win-card p { color: var(--muted); margin: 8px 0 18px; }
.win-btn {
  width: 100%; min-height: 48px; border-radius: 16px;
  background: #f97316; color: #fff; font-weight: 800;
  box-shadow: 0 4px 0 var(--orange-deep);
}

body.resting { background: #d7d2cc; overflow: hidden; touch-action: none; }
body.resting .app,
body.resting .win,
body.resting .toast,
body.resting .room {
  filter: grayscale(1) contrast(.92) brightness(.96);
  pointer-events: none;
  user-select: none;
}
.rest {
  position: fixed; inset: 0; z-index: 70;
  display: none; place-items: center;
  background: rgba(70, 62, 56, .38);
  padding: 24px;
}
.rest.show { display: grid; }
.rest-card {
  position: relative;
  width: min(92vw, 360px);
  background: #fff;
  border-radius: 28px;
  padding: 32px 22px 26px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(80, 70, 60, .18);
}
.rest-mascot {
  width: 72px; height: 72px; margin: 0 auto 10px;
  display: grid; place-items: center;
  font-size: 42px;
  background: #fff4e5;
  border-radius: 24px;
  box-shadow: 0 4px 0 #f0dfcc;
}
.rest-card h2 { font-size: 26px; letter-spacing: -.04em; }
.rest-card p { color: var(--muted); margin: 8px 0 0; font-weight: 700; line-height: 1.6; }
.rest-secret {
  position: absolute; top: 12px; right: 12px;
  width: 36px; height: 36px; border-radius: 12px;
  font-size: 18px; opacity: .12 !important;
}
.rest-secret:active { transform: scale(.92); }
.debug-rest {
  position: fixed; left: 12px; right: 12px; bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 90;
  background: #263238; color: #fff;
  border-radius: 14px; padding: 10px 12px;
  font-size: 12px; font-weight: 800;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.debug-rest button {
  border: 0; background: #fff; color: #c2410c;
  border-radius: 10px; padding: 6px 10px; font: inherit; font-weight: 800;
}

@media (max-width: 420px) {
  .key { height: 44px; min-height: 44px; font-size: 20px; }
  .world { font-size: 11px; min-height: 82px; }
}
@media (max-height: 740px) {
  .app { gap: 8px; }
  .board-frame { width: min(100%, 48dvh, 380px); padding: 10px; }
  .mascot { width: 48px; height: 48px; }
}
@media (min-width: 900px) {
  .play {
    grid-template-columns: minmax(0, 1.2fr) minmax(280px, .8fr);
    align-items: stretch;
  }
  .board-frame { width: min(100%, 70dvh, 560px); }
  .toys { align-content: center; }
  .key { height: 52px; min-height: 52px; font-size: 24px; }
}
@media (orientation: landscape) and (min-width: 740px) {
  .play { grid-template-columns: minmax(0, 1.2fr) 300px; }
  .board-frame { width: min(100%, 72dvh, 560px); }
  .worlds { max-width: 520px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
