:root {
  --ink: #1f1235;
  --muted: #6b5b73;
  --card: #ffffff;
  --brand: #c2410c;
  --brand-2: #7c3aed;
  --sel: #fff7ed;
  --sel-border: #ea580c;
  --play: #16a34a;
  --play-dark: #15803d;
  --good: #16a34a;
  --bad: #dc2626;
  --bg-1: #fbbf24;
  --bg-2: #fb923c;
  --bg-3: #f472b6;
  --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: linear-gradient(160deg, var(--bg-1), var(--bg-2) 50%, var(--bg-3)) 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(124, 45, 18, .25); }
.brand p { margin: 4px 0 0; font-size: clamp(16px, 4vw, 20px); font-weight: 500; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: 0 10px 30px rgba(124, 45, 18, .2); }
.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: #fafaf9; 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(234,88,12,.2); }

.dskp { margin-top: 12px; background: #fafaf9; 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: #fafaf9; 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; }
.task-tools { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.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 #fed7aa; }
.btn-small { border: 2px solid #fde68a; 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); }
.privacy { margin: 0; text-align: center; color: #fff; font-size: 14px; line-height: 1.4; }
.install { display: grid; justify-items: center; gap: 6px; color: #fff; font-size: 14px; text-align: center; }
.install p { margin: 0; }

/* ---------- Game ---------- */
#game.active { position: fixed; inset: 0; overflow-y: auto; display: block; user-select: none; -webkit-user-select: none; }
.layout {
  min-height: 100%; max-width: 1100px; margin: 0 auto;
  display: flex; flex-direction: column; gap: 12px;
  padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) calc(16px + var(--safe-b)) calc(12px + var(--safe-l));
}
.hud { display: flex; gap: 8px; align-items: center; }
.pill { background: rgba(255,255,255,.95); border: 0; border-radius: 999px; padding: 6px 14px; font-weight: 700; font-size: clamp(15px, 2.4vw, 22px); box-shadow: 0 4px 12px rgba(0,0,0,.12); white-space: nowrap; }
.hud-year { margin-left: auto; background: var(--brand); color: #fff; }
.hud-pause { min-width: 48px; min-height: 42px; }

.task { background: rgba(255,255,255,.96); border-radius: var(--radius); padding: 12px 16px; box-shadow: 0 8px 24px rgba(0,0,0,.12); display: grid; gap: 6px; justify-items: center; text-align: center; }
.std { font-size: 12px; font-weight: 600; color: var(--brand); background: var(--sel); border-radius: 999px; padding: 2px 10px; }
.task-main { display: flex; align-items: center; justify-content: center; gap: 14px; flex-wrap: wrap; }
.tvisual { font-family: var(--emoji); font-size: clamp(44px, 8vh, 80px); line-height: 1.1; }
.tvisual:empty { display: none; }
.tcontext { font-size: clamp(17px, 2.6vw, 24px); background: #fef3c7; border-radius: 14px; padding: 6px 12px; margin-bottom: 6px; line-height: 1.35; }
.tcontext:empty { display: none; }
.tcontext .gap { display: inline-block; min-width: 3em; border-bottom: 3px solid var(--brand); }
.tprompt { font-size: clamp(19px, 3vw, 28px); font-weight: 700; }

/* The train */
.rail-wrap { background: rgba(255,255,255,.35); border-radius: var(--radius); padding: 14px 10px 10px; overflow: hidden; }
.train {
  display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: center; row-gap: 18px; column-gap: 6px;
  transition: transform 1.1s cubic-bezier(.5, 0, .9, .4);
}
.train.go { transform: translateX(-130vw); }
.engine { font-family: var(--emoji); font-size: clamp(44px, 7vh, 64px); line-height: 1; margin-right: 2px; }
.car {
  position: relative; min-width: clamp(54px, 9vw, 86px); min-height: clamp(52px, 8vh, 66px); padding: 6px 12px 12px;
  border-radius: 12px 12px 6px 6px; border: 3px dashed rgba(255,255,255,.9); background: rgba(255,255,255,.25);
  font-size: clamp(19px, 3.2vw, 30px); font-weight: 700; display: grid; place-items: center; margin-bottom: 10px;
}
.car::before, .car::after {
  content: ''; position: absolute; bottom: -12px; width: 18px; height: 18px; border-radius: 50%;
  background: #374151; border: 3px solid #9ca3af;
}
.car::before { left: 8px; }
.car::after { right: 8px; }
.car.filled { border-style: solid; border-color: #fff; background: var(--c, #fde68a); color: var(--ink); box-shadow: 0 4px 0 rgba(0,0,0,.15); }
.car.next { border-color: var(--brand); background: rgba(255,255,255,.55); }
.car.wrong { background: #fecaca; border-color: var(--bad); animation: shake .45s; }
.car.right { background: #bbf7d0; border-color: var(--good); }
.train.spell .car { min-width: clamp(42px, 7vw, 64px); }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }

.message { text-align: center; font-size: clamp(18px, 2.8vw, 26px); font-weight: 700; color: #fff; text-shadow: 0 2px 0 rgba(124,45,18,.35); min-height: 1.4em; }
.message.bad { color: #fff1f2; }

.pool { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; align-content: flex-start; flex: 1 0 auto; }
.tile {
  border: 0; border-radius: 16px; background: #fff; min-height: clamp(54px, 8vh, 70px); min-width: 56px; padding: 8px 16px;
  font-size: clamp(20px, 3.2vw, 30px); font-weight: 700; box-shadow: 0 5px 0 #fdba74, 0 8px 16px rgba(0,0,0,.12);
  transition: transform .1s, opacity .15s;
}
.tile:active { transform: translateY(3px); box-shadow: 0 2px 0 #fdba74; }
.tile.used { opacity: .2; pointer-events: none; box-shadow: none; }
.tile.hint { animation: glow 1s ease-in-out 3; }
@keyframes glow { 50% { box-shadow: 0 0 0 6px #fde047, 0 5px 0 #fdba74; } }
.pool.spell .tile { min-width: 56px; font-size: clamp(24px, 4vw, 34px); }

.actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

/* Hand mode */
.cam {
  position: fixed; right: calc(10px + var(--safe-r)); bottom: calc(10px + var(--safe-b)); z-index: 5;
  width: clamp(110px, 18vw, 200px); aspect-ratio: 4 / 3; object-fit: cover; transform: scaleX(-1);
  border-radius: 16px; border: 3px solid #fff; box-shadow: 0 6px 18px rgba(0,0,0,.25); display: none; pointer-events: none;
}
#game.hand .cam { display: block; }
.cursor { position: fixed; z-index: 6; width: 60px; height: 60px; margin: -30px 0 0 -30px; pointer-events: none; left: 0; top: 0; }
.cursor svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.cursor .ring-bg { fill: rgba(124, 58, 237, .25); stroke: #fff; stroke-width: 4; }
.cursor .ring { fill: none; stroke: var(--brand-2); stroke-width: 6; stroke-linecap: round; stroke-dasharray: 151; stroke-dashoffset: 151; }
.dwell-target { outline: 4px solid var(--brand-2); outline-offset: 2px; }

.overlay { position: fixed; inset: 0; z-index: 7; display: grid; place-items: center; padding: 16px; background: rgba(31, 18, 53, .45); }
.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,.3); }
.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; }
.spinner { width: 44px; height: 44px; margin: 4px auto; border-radius: 50%; border: 5px solid #ffedd5; border-top-color: var(--brand); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 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-score { margin: 0; font-size: 20px; font-weight: 600; }
.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; }
.review { text-align: left; margin: 0; padding-left: 24px; display: grid; gap: 8px; }
.review button { border: 0; background: none; padding: 0; text-align: left; font-size: 17px; font-weight: 600; }
.review .mark { font-size: 14px; font-weight: 600; margin-left: 6px; }
.review .ok { color: var(--good); }
.review .mid { color: #b45309; }
.review .no { color: var(--bad); }

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