:root {
  --page: #6d3b1e;
  --paint: #ff7a5c;
  --paint2: #f2506b;
  --cream: #fff6e6;
  --cream-2: #f6e6cc;
  --ink: #3b2212;
  --ink-soft: #7c5a40;
  --shadow: 0 2px 0 rgba(90, 45, 15, .35), 0 6px 14px rgba(40, 15, 0, .28);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--page);
  color: var(--ink);
  font: 15px/1.4 ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#game {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}

/* The canvas fills the window; this grid only says where the maze may sit. */
#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  gap: 6px;
  padding:
    max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  pointer-events: none;
}

#app button { pointer-events: auto; }

/* ---------- HUD ---------- */
.hud {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
}

.hud-main {
  flex: 1;
  min-width: 0;
  background: var(--cream);
  border-radius: 14px;
  padding: 6px 12px 8px;
  box-shadow: var(--shadow);
}

.hud-title {
  font-weight: 800;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bar {
  height: 8px;
  margin-top: 4px;
  border-radius: 99px;
  background: var(--cream-2);
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  width: 0;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--paint), var(--paint2));
  transition: width .18s ease-out;
}

.score {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 60px;
  background: var(--cream);
  border-radius: 14px;
  padding: 4px 10px 6px;
  box-shadow: var(--shadow);
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}
.score-num { font-size: 22px; font-weight: 800; }
.score-num.over { color: #b4501c; }
.score-par { font-size: 11px; font-weight: 700; color: var(--ink-soft); }

.icon-btn {
  flex: none;
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  background: var(--cream);
  color: var(--ink);
  border: 0;
  border-radius: 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform .08s ease;
}
.icon-btn:active { transform: translateY(2px); }

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- stage ---------- */
.stage {
  position: relative;
  min-height: 0;
  min-width: 0;
}

.tip, .toast, .stuck {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(92%, 440px);
  width: max-content;
  background: var(--cream);
  color: var(--ink);
  border-radius: 14px;
  box-shadow: var(--shadow);
  font-weight: 700;
  font-size: 14px;
  text-align: center;
  pointer-events: none;
}
.tip[hidden], .stuck[hidden] { display: none; }

.tip { bottom: 2px; padding: 8px 14px; }

.stuck {
  bottom: 2px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 8px 7px 14px;
  background: #4a1f12;
  color: #fff2e4;
  animation: rise .22s ease;
}

.pill {
  font: inherit;
  font-weight: 800;
  background: var(--cream);
  color: var(--ink);
  border: 0;
  border-radius: 10px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}

.toast {
  top: 2px;
  padding: 7px 14px;
  opacity: 0;
  transition: opacity .2s ease;
}
.toast.is-on { opacity: 1; }

@keyframes rise {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ---------- tools ---------- */
.tools {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
}

.tool {
  font: inherit;
  font-size: 12px;
  font-weight: 700;
  color: var(--ink);
  background: var(--cream);
  border: 0;
  border-radius: 14px;
  padding: 7px 4px 6px;
  min-height: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform .08s ease, opacity .15s ease, background .15s ease;
}
.tool:active { transform: translateY(2px); }
.tool:disabled { opacity: .5; cursor: default; transform: none; }

.tool.attention { background: #ffd84d; animation: nudge 1s ease-in-out infinite; }
@keyframes nudge {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

#btnSound[aria-pressed="true"] .sound-off { display: none; }
#btnSound[aria-pressed="false"] .sound-on { display: none; }
#btnSound[aria-pressed="false"] { color: var(--ink-soft); }

/* ---------- sheets ---------- */
.sheet {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(35, 15, 3, .5);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade .22s ease;
  z-index: 5;
}
.sheet[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.card {
  background: var(--cream);
  color: var(--ink);
  border-radius: 22px;
  padding: 22px;
  box-shadow: 0 4px 0 rgba(90, 45, 15, .35), 0 18px 40px rgba(30, 10, 0, .45);
  text-align: center;
  font-weight: 700;
}

.win-card { width: min(100%, 340px); animation: pop .3s cubic-bezier(.2, 1.4, .4, 1); }
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.stars { display: flex; justify-content: center; gap: 4px; margin-bottom: 8px; }
.stars svg { width: 56px; height: 56px; fill: var(--cream-2); stroke: #d9bf97; stroke-width: 1.4; }
.stars svg:nth-child(2) { transform: translateY(-8px); }
.stars svg.on { fill: #ffc623; stroke: #d48a00; animation: star .36s cubic-bezier(.2, 1.7, .4, 1); }
.stars svg:nth-child(2).on { animation-name: star-mid; }
@keyframes star { from { transform: scale(.2) rotate(-40deg); } to { transform: scale(1) rotate(0); } }
@keyframes star-mid { from { transform: translateY(-8px) scale(.2) rotate(-40deg); } to { transform: translateY(-8px) scale(1) rotate(0); } }

.card h2 { margin: 0 0 4px; font-size: 24px; font-weight: 800; letter-spacing: -.01em; }
.win-meta { margin: 0; font-size: 16px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.win-note { margin: 4px 0 16px; font-size: 14px; color: var(--ink-soft); font-weight: 600; min-height: 1.4em; }
.card-actions { display: flex; gap: 8px; justify-content: center; }

.btn {
  font: inherit;
  font-weight: 800;
  color: var(--ink);
  background: var(--cream-2);
  border: 0;
  border-radius: 13px;
  padding: 12px 16px;
  min-height: 46px;
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(90, 45, 15, .28);
  transition: transform .08s ease, filter .15s ease;
}
.btn:active { transform: translateY(2px); box-shadow: none; }
.btn:hover { filter: brightness(1.04); }
.btn.primary {
  color: #fff;
  background: linear-gradient(180deg, var(--paint), var(--paint2));
  text-shadow: 0 1px 1px rgba(60, 10, 10, .3);
  box-shadow: 0 2px 0 rgba(90, 20, 20, .4);
}
.btn.wide { width: 100%; }

.menu-card {
  width: min(100%, 460px);
  max-height: 100%;
  overflow-y: auto;
  text-align: left;
  -webkit-overflow-scrolling: touch;
}
.menu-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.menu-head h2 { margin: 0; font-size: 26px; }
.menu-head .icon-btn { background: var(--cream-2); box-shadow: none; }
.menu-lede { margin: 4px 0 14px; color: var(--ink-soft); font-weight: 600; }
.card h3 { margin: 20px 0 6px; font-size: 15px; font-weight: 800; }
.menu-sub { margin: 0 0 8px; font-size: 13px; color: var(--ink-soft); font-weight: 600; }

.free-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }

.level-grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(58px, 1fr));
  gap: 7px;
  max-height: 248px;
  overflow-y: auto;
  padding: 2px;
  -webkit-overflow-scrolling: touch;
}
.level {
  font: inherit;
  color: var(--ink);
  background: var(--cream-2);
  border: 2px solid transparent;
  border-radius: 12px;
  height: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  line-height: 1.1;
}
.level-n { font-size: 16px; font-weight: 800; }
.level-stars { font-size: 10px; color: #cf8a00; letter-spacing: -.02em; min-height: 11px; }
.level.done { background: #ffe9b0; }
.level.current { border-color: var(--paint); }
.level.locked { opacity: .42; cursor: default; }

:focus-visible { outline: 3px solid #2b7fff; outline-offset: 2px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.sr-only button:focus { position: fixed; top: 8px; left: 8px; width: auto; height: auto; z-index: 9; clip-path: none; }

/* Short screens (phones on their side): HUD and tools move beside the maze. */
@media (orientation: landscape) and (max-height: 560px) {
  #app {
    grid-template-rows: minmax(0, 1fr);
    grid-template-columns: 190px minmax(0, 1fr) 76px;
    gap: 10px;
  }
  .hud { flex-wrap: wrap; align-content: flex-start; align-self: start; }
  .hud-main { flex: 1 1 100%; order: 3; }
  .score { flex: 1; }
  .tools { grid-template-columns: 1fr; grid-auto-rows: minmax(0, 1fr); height: 100%; max-height: 300px; align-self: center; }
  .tool { min-height: 0; }
  .tip, .stuck, .toast { font-size: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
