:root {
  --ink: #1e1b4b;
  --muted: #5b5880;
  --card: #ffffff;
  --brand: #6d28d9;
  --brand-2: #0ea5e9;
  --sel: #f5f3ff;
  --sel-border: #7c3aed;
  --play: #16a34a;
  --play-dark: #15803d;
  --good: #22c55e;
  --bad: #ef4444;
  --space-1: #0f0a2e;
  --space-2: #1e1b4b;
  --space-3: #4c1d95;
  --radius: 22px;
  --font: 'Fredoka', ui-rounded, 'SF Pro Rounded', 'Segoe UI', system-ui, sans-serif;
  --emoji: 'Apple Color Emoji', 'Segoe UI Emoji', 'Noto Color Emoji', sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%;
  font-family: var(--font); color: var(--ink);
  background: radial-gradient(ellipse at top, var(--space-3), var(--space-2) 45%, var(--space-1)) fixed;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.screen { display: none; }
.screen.active { display: block; }

/* ---------- Home & results ---------- */
#home, #results {
  min-height: 100dvh;
  padding: calc(14px + var(--safe-t)) calc(16px + var(--safe-r)) calc(24px + var(--safe-b)) calc(16px + var(--safe-l));
}
.inner { max-width: 820px; margin: 0 auto; display: grid; gap: 16px; }
.back { color: #fff; font-weight: 600; text-decoration: none; justify-self: start; padding: 6px 2px; }
.brand { text-align: center; color: #fff; }
.brand .logo { font-size: clamp(40px, 10vw, 64px); line-height: 1.1; font-family: var(--emoji); }
.brand h1 { margin: 2px 0 0; font-size: clamp(34px, 8vw, 56px); text-shadow: 0 3px 0 rgba(0,0,0,.35); }
.brand p { margin: 4px 0 0; font-size: clamp(16px, 4vw, 20px); font-weight: 500; color: #e0e7ff; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: 0 10px 30px rgba(0,0,0,.35); }
.card h2 { margin: 0 0 12px; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.step { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--brand); color: #fff; font-size: 16px; }

.years { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
@media (min-width: 640px) { .years { grid-template-columns: repeat(6, 1fr); } }
.year { border: 3px solid #e5e7eb; background: #fafafa; border-radius: 18px; min-height: 76px; display: grid; place-content: center; padding: 6px; }
.year b { font-size: 28px; line-height: 1; }
.year small { color: var(--muted); font-size: 13px; }
/* My Content (the teacher's pack): a wide button under the years */
.year.pack { grid-column: 1 / -1; min-height: 60px; border-style: dashed; }
.year.pack b { font-size: 20px; line-height: 1.2; overflow-wrap: anywhere; }
.year[aria-checked="true"] { border-color: var(--sel-border); background: var(--sel); box-shadow: 0 0 0 3px rgba(124,58,237,.2); }

.dskp { margin-top: 12px; background: #fafafa; border-radius: 14px; padding: 10px 12px; }
.dskp summary { cursor: pointer; font-weight: 600; color: var(--brand); }
.dskp ul { margin: 10px 0 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.dskp li { display: grid; grid-template-columns: auto 1fr; gap: 8px; font-size: 14px; line-height: 1.35; }
.dskp .code { font-weight: 700; color: var(--brand); white-space: nowrap; }
.dskp .note { margin: 10px 0 0; font-size: 12px; color: var(--muted); }

.option { display: grid; gap: 6px; margin-bottom: 12px; }
.option-label { font-weight: 600; color: var(--muted); font-size: 14px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1 1 auto; min-height: 44px; padding: 8px 12px; border-radius: 14px; border: 2px solid #e5e7eb; background: #fafafa; font-weight: 500; font-size: 15px; }
.seg button[aria-pressed="true"] { border-color: var(--sel-border); background: var(--sel); font-weight: 600; }
.level-seg button { min-height: 56px; font-size: 17px; }
.level-note { margin: 8px 0 0; font-size: 14px; color: var(--muted); line-height: 1.35; }
.tip { margin: 4px 0 0; font-size: 14px; color: var(--muted); line-height: 1.4; }

.btn { border: 0; border-radius: 999px; min-height: 52px; padding: 10px 26px; font-weight: 700; font-size: 20px; transition: transform .1s; }
.btn:active { transform: translateY(2px); }
.btn-play { background: var(--play); color: #fff; box-shadow: 0 5px 0 var(--play-dark); }
.btn-ghost { background: #fff; color: var(--brand); box-shadow: 0 5px 0 #c4b5fd; }
.btn-small { border: 2px solid #ddd6fe; background: #fff; border-radius: 999px; padding: 6px 16px; font-weight: 600; font-size: 16px; min-height: 44px; }
#playBtn { font-size: 28px; min-height: 68px; justify-self: center; width: min(100%, 360px); }
.install { display: grid; justify-items: center; gap: 6px; color: #fff; font-size: 14px; text-align: center; }
.install p { margin: 0; }

/* ---------- Game ---------- */
#game:focus { outline: none; }
#game.active {
  position: fixed; inset: 0; display: flex; flex-direction: column; overflow: hidden;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.hud {
  display: flex; gap: 6px; align-items: center; flex-wrap: wrap;
  padding: calc(8px + var(--safe-t)) calc(10px + var(--safe-r)) 6px calc(10px + var(--safe-l));
}
.pill { background: rgba(255,255,255,.95); border: 0; border-radius: 999px; padding: 5px 12px; font-weight: 700; font-size: clamp(14px, 2.2vw, 20px); white-space: nowrap; }
.hud-year { margin-left: auto; background: var(--brand); color: #fff; }
.hud-btn { min-width: 44px; min-height: 40px; }
#shields:empty { display: none; }
@media (max-width: 480px) {
  .hud { gap: 4px; flex-wrap: nowrap; }
  .pill { padding: 4px 8px; font-size: 13px; }
  .hud-btn { min-width: 34px; min-height: 34px; padding: 4px 6px; }
}

.stage-wrap { position: relative; flex: 1 1 auto; min-height: 0; }
#stage { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.toast {
  position: absolute; left: 50%; top: 12px; transform: translate(-50%, -10px); opacity: 0; transition: opacity .2s, transform .2s;
  background: #fff; border-radius: 16px; padding: 8px 16px; font-weight: 700; font-size: clamp(15px, 2.4vw, 22px);
  max-width: calc(100% - 24px); text-align: center; pointer-events: none; box-shadow: 0 6px 18px rgba(0,0,0,.35);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* On-screen keyboard */
.keyboard {
  flex: 0 0 auto; display: grid; gap: 6px;
  padding: 8px calc(6px + var(--safe-r)) calc(8px + var(--safe-b)) calc(6px + var(--safe-l));
  background: rgba(15, 10, 46, .85); border-top: 2px solid rgba(255,255,255,.15);
}
.keyboard.off { display: none; }
.kb-row { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1 1 0; max-width: 78px; min-height: clamp(42px, 6.5vh, 60px); border: 0; border-radius: 10px;
  background: #f8fafc; color: var(--ink); font-size: clamp(18px, 3vw, 26px); font-weight: 700;
  box-shadow: 0 4px 0 #94a3b8; padding: 0; touch-action: manipulation;
}
.key:active, .key.pressed { transform: translateY(3px); box-shadow: 0 1px 0 #94a3b8; }
.key.wide { flex-grow: 1.6; max-width: 120px; font-size: clamp(15px, 2.4vw, 20px); }
.key.space { flex-grow: 5; max-width: 360px; }
.key.on { background: #c4b5fd; }
.key.hint { background: #fde047; box-shadow: 0 4px 0 #ca8a04, 0 0 0 4px rgba(253,224,71,.5); }
.key.miss { background: #fecaca; }

.overlay { position: fixed; inset: 0; z-index: 7; display: grid; place-items: center; padding: 16px; background: rgba(15, 10, 46, .6); }
.panel { background: #fff; border-radius: var(--radius); padding: 20px; width: min(100%, 480px); text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.4); }
.ov-icon { font-size: 52px; line-height: 1.1; }
.panel h2 { margin: 6px 0; font-size: 26px; }
.panel p { margin: 0 0 14px; color: var(--muted); font-size: 17px; line-height: 1.4; }
.buttons { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

/* ---------- Results ---------- */
.results { display: grid; gap: 10px; text-align: center; }
.results h2 { justify-content: center; font-size: 30px; margin: 0; }
.results h3 { margin: 10px 0 0; font-size: 18px; }
.results h3 small { font-weight: 400; color: var(--muted); }
.stars { font-size: clamp(48px, 13vw, 72px); letter-spacing: 4px; }
.stars .off { filter: grayscale(1); opacity: .3; }
.res-sub { margin: 0; font-size: 18px; font-weight: 600; color: var(--muted); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stats div { background: var(--sel); border-radius: 16px; padding: 10px 4px; display: grid; }
.stats b { font-size: 28px; color: var(--brand); }
.stats span { font-size: 13px; color: var(--muted); }
.table-wrap { overflow-x: auto; }
.std-table { width: 100%; border-collapse: collapse; text-align: left; font-size: 14px; }
.std-table th, .std-table td { padding: 8px; border-bottom: 1px solid #f1f5f9; vertical-align: top; }
.std-table th { color: var(--muted); font-weight: 600; }
.std-table td:first-child { font-weight: 700; color: var(--brand); white-space: nowrap; }
.std-table td:last-child { font-weight: 700; white-space: nowrap; }
#copyBtn { justify-self: center; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip { border: 2px solid #fecaca; background: #fef2f2; border-radius: 999px; padding: 6px 14px; font-size: 18px; font-weight: 600; min-height: 44px; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
