:root {
  --font-display: 'Chillax', 'Satoshi', sans-serif;
  --font-body: 'Satoshi', system-ui, sans-serif;
  --bg: #f3eee4;
  --bg-2: #ebe4d6;
  --surface: #fbf8f2;
  --ink: #1e2a2f;
  --ink-2: #4a565b;
  --muted: #6b7477;
  --line: #d8cfbf;
  --accent: #c4513a;
  --accent-ink: #fff;
  --ok: #2a7f6f;
  --bad: #b8402f;
  --target: #e4dccb;
  --radius: 14px;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font-body);
  color: var(--ink);
  background: radial-gradient(1200px 600px at 50% -10%, #f9f5ec 0%, var(--bg) 60%) fixed, var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums lining-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
kbd {
  font-family: var(--font-body); font-size: 11px; font-weight: 700;
  border: 1px solid currentColor; border-radius: 4px; padding: 0 4px; opacity: 0.7;
}

#app { height: 100dvh; position: relative; }
.screen { position: absolute; inset: 0; display: none; }
.screen.active { display: flex; animation: fade-in 320ms var(--ease); }
@keyframes fade-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 28px; border-radius: 999px; border: 1.5px solid var(--ink);
  background: transparent; font-weight: 700; font-size: 16px; cursor: pointer;
  transition: transform 160ms var(--ease), background 160ms, color 160ms, box-shadow 160ms;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(1px) scale(0.98); }
.btn:focus-visible, .icon-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.btn-primary { background: var(--ink); color: var(--bg); box-shadow: 0 6px 18px -8px rgba(30, 42, 47, 0.6); }
.btn-ghost { background: var(--surface); }
.btn-small { min-height: 42px; padding: 0 16px; font-size: 14px; }
.btn-accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 1.5px solid var(--line);
  background: var(--surface); display: grid; place-items: center; cursor: pointer;
}

/* ---------- menu ---------- */
#menu, #results { align-items: center; justify-content: center; padding: 24px; overflow-y: auto; }
.menu-inner, .results-inner { margin: auto 0; width: 100%; max-width: 560px; text-align: center; display: flex; flex-direction: column; align-items: center; }
.eyebrow { font-size: 13px; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; color: var(--accent); margin: 0 0 6px; }
.title { font-family: var(--font-display); font-weight: 700; font-size: clamp(56px, 13vw, 96px); line-height: 0.95; margin: 0; letter-spacing: -0.02em; }
.tagline { font-size: 17px; color: var(--ink-2); margin: 12px 0 20px; }
.menu-shape-wrap { display: flex; flex-direction: column; align-items: center; gap: 14px; margin: 4px 0 12px; flex-shrink: 0; }
.menu-shape { width: min(260px, 62vw, 34dvh); aspect-ratio: 1; height: auto; overflow: visible; flex-shrink: 0; display: block; }
.menu-shape-label {
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 16px; display: flex; gap: 8px; align-items: baseline; white-space: nowrap; font-size: 15px;
  box-shadow: 0 6px 16px -10px rgba(0,0,0,.4);
}
.menu-buttons { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; margin: 12px 0 16px; }
.menu-buttons .btn { min-width: 150px; }

/* ---------- game ---------- */
#game { flex-direction: column; }
.hud {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
}
.hud-title { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.hud-title strong { font-family: var(--font-display); font-size: 20px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-stats { margin-left: auto; display: flex; gap: 10px; }
.stat {
  display: flex; flex-direction: column; align-items: flex-end; line-height: 1.15;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 5px 12px;
}
.stat strong { font-size: 18px; }
.stat.timer strong { min-width: 64px; text-align: right; }

.coach {
  margin: 0 16px; padding: 10px 14px; border-radius: 12px; background: var(--ink); color: var(--bg);
  font-size: 15px; line-height: 1.4; text-align: center;
}
.coach b { color: #f2b49f; }

.board-wrap { flex: 1; min-height: 0; position: relative; display: grid; place-items: center; padding: 8px; }
.board { display: block; overflow: visible; }
.toast {
  position: absolute; left: 50%; top: 12px; transform: translate(-50%, -8px); opacity: 0;
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 999px; font-size: 14px; font-weight: 500;
  transition: opacity 200ms, transform 200ms var(--ease); pointer-events: none; max-width: 90%; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.controls { display: flex; align-items: center; gap: 10px; padding: 4px 16px 8px; }
.hint { flex: 1; text-align: center; margin: 0; }
#btn-solution.armed { background: var(--ink); color: var(--bg); border-color: var(--ink); }

.tray {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px;
  padding: 10px 16px; padding-bottom: max(14px, env(safe-area-inset-bottom));
  background: var(--bg-2); border-top: 1px solid var(--line);
}
.slot {
  position: relative; aspect-ratio: 1; max-height: 110px; border-radius: 12px; background: var(--surface);
  border: 1.5px solid var(--line); display: grid; place-items: center; touch-action: none;
  transition: transform 200ms var(--ease), opacity 200ms, border-color 200ms, box-shadow 200ms;
}
.slot svg { width: 82%; height: 82%; overflow: visible; }
.slot .idx {
  position: absolute; top: 4px; left: 7px; font-size: 12px; font-weight: 700; color: var(--muted);
}
.slot.locked svg { opacity: 0.35; filter: grayscale(0.6); }
.slot.locked .lock { position: absolute; right: 6px; top: 5px; width: 12px; height: 12px; color: var(--muted); }
.slot.active {
  border-color: var(--ink); cursor: grab; box-shadow: 0 0 0 3px rgba(196, 81, 58, 0.35), 0 10px 20px -12px rgba(0,0,0,.5);
  transform: translateY(-3px);
}
.slot.active .idx { color: var(--accent); }
.slot.active.dragging { opacity: 0.35; }
.slot.placed { background: transparent; border-style: dashed; }
.slot.placed svg { opacity: 0; }
.slot.placed::after {
  content: ''; width: 18px; height: 10px; border-left: 2.5px solid var(--ok); border-bottom: 2.5px solid var(--ok);
  transform: rotate(-45deg) translate(2px, -2px); position: absolute;
}
.slot.shake { animation: shake 360ms; }
@keyframes shake { 20%,60% { transform: translateX(-4px); } 40%,80% { transform: translateX(4px); } }

#drag-layer { position: fixed; inset: 0; pointer-events: none; z-index: 50; }
#drag-layer svg { position: absolute; overflow: visible; filter: drop-shadow(0 12px 14px rgba(0,0,0,.25)); transition: opacity 120ms; }

.piece-fill { stroke-width: 0.03; stroke-linejoin: round; }
.piece-edge { fill: none; stroke: rgba(20, 28, 32, 0.75); stroke-width: 0.07; stroke-linecap: round; stroke-linejoin: round; }
.placed-piece { animation: pop 260ms var(--ease); transform-box: fill-box; transform-origin: center; }
@keyframes pop { from { transform: scale(1.06); } to { transform: scale(1); } }
.board.win .placed-piece { animation: winwave 700ms var(--ease) both; }
@keyframes winwave { 50% { transform: scale(1.04); filter: brightness(1.12); } }

/* ---------- results ---------- */
.res-time { font-family: var(--font-display); font-size: clamp(56px, 14vw, 88px); font-weight: 700; margin: 0; line-height: 1; }
.res-tier { font-family: var(--font-display); font-size: 22px; font-weight: 600; color: var(--accent); margin: 8px 0 18px; }
.res-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 100%; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 8px;
  display: flex; flex-direction: column; gap: 2px;
}
.card strong { font-size: 24px; font-family: var(--font-display); }
.curve-fig { width: 100%; margin: 18px 0 6px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 12px 10px; }
.curve { width: 100%; height: auto; display: block; overflow: visible; }
.curve-fig figcaption { margin-top: 6px; }

@media (max-width: 640px) {
  .tray { grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 8px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
  .slot { max-height: none; }
  .hud { padding: 8px 12px; gap: 8px; }
  .hud-title strong { font-size: 17px; }
  .stat { padding: 4px 9px; }
  .stat strong { font-size: 16px; }
  .controls { padding: 2px 12px 6px; }
  .hint { font-size: 12px; }
  .btn-small { padding: 0 12px; }
  .btn-small kbd { display: none; }
  .controls { gap: 6px; }
  .btn-small { padding: 0 10px; }
  .coach { font-size: 14px; margin: 0 12px; }
}
@media (max-width: 420px) { .hint { display: none; } .controls { justify-content: space-between; } }
@media (prefers-reduced-motion: reduce) { * { animation-duration: 1ms !important; transition-duration: 1ms !important; } }
