*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { margin: 0; height: 100%; }
body {
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.45;
    color: var(--text);
    background: var(--bg);
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    overscroll-behavior: none;
}
button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
svg { width: 1.4em; height: 1.4em; flex: none; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 6px; }

/* ---------- Views & app bar ---------- */
.view {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    padding-bottom: env(safe-area-inset-bottom);
}
.bar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 10px;
    min-height: calc(var(--bar-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
    background: var(--navy);
    color: #fff;
}
.bar-title { margin: 0; font-size: 1.15rem; font-weight: 700; letter-spacing: .01em; flex: 1; min-width: 0;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-title:focus { outline: none; }
.bar-actions { display: flex; align-items: center; gap: 8px; flex: none; }

.icon-btn {
    display: inline-grid; place-items: center; width: 44px; height: 44px; border-radius: 50%;
    border: 0; background: rgba(255,255,255,.12); color: #fff; text-decoration: none;
}
.icon-btn:hover { background: rgba(255,255,255,.2); }

.btn {
    display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 16px;
    border: 0; border-radius: 999px; background: var(--yellow); color: var(--navy);
    font-weight: 700; font-size: .9rem; white-space: nowrap;
    transition: transform .12s ease, filter .12s ease;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: scale(.97); }
.btn.ghost { background: rgba(255,255,255,.12); color: #fff; }
.btn.ghost:hover { background: rgba(255,255,255,.2); }
.btn.confirm { background: var(--red); color: #fff; }

/* ---------- Home ---------- */
#view-home { height: 100vh; height: 100dvh; }
#view-home .bar { background: #fff; padding-top: calc(env(safe-area-inset-top) + 4px); padding-bottom: 4px; color: var(--navy); box-shadow: 0 1px 0 var(--border); }
#view-home .bar-title { font-size: 1.3rem; display: flex; align-items: center; overflow: visible; }
.logo { height: 56px; width: auto; max-width: none; flex: 0 0 auto; display: block; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
#view-home .level-select { background: var(--mist); }
#view-home .level-select button { color: var(--navy); }
#view-home .level-select button[aria-pressed="true"] { background: var(--navy); color: #fff; }
#view-home .level-name { background: var(--navy); color: #fff; }
.level-select { display: inline-flex; padding: 3px; border-radius: 999px; background: rgba(255,255,255,.14); }
.level-select button {
    border: 0; background: transparent; color: #fff; font-weight: 600; font-size: .85rem;
    min-height: 38px; padding: 0 14px; border-radius: 999px;
}
.level-select button[aria-pressed="true"] { background: var(--yellow); color: var(--navy); }
.level-name { padding: 6px 14px; border-radius: 999px; background: var(--yellow); color: var(--navy); font-weight: 700; }

.tiles {
    flex: 1;
    display: grid; gap: 14px;
    grid-template-columns: repeat(2, 1fr);
    grid-auto-rows: minmax(150px, 1fr);
    min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding: 14px max(14px, env(safe-area-inset-right)) 14px max(14px, env(safe-area-inset-left));
}
@media (min-width: 620px) { .tiles { grid-template-columns: repeat(3, 1fr); } }
.tile {
    position: relative; display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
    gap: 10px; padding: 16px 12px; min-height: 0;
    border-radius: var(--radius); color: #fff; text-decoration: none;
    font-weight: 700; font-size: clamp(.95rem, 3.4vw, 1.2rem); text-align: center; line-height: 1.15;
    box-shadow: var(--shadow);
    transition: transform .15s ease, box-shadow .15s ease;
}
.tile:hover { transform: translateY(-2px); }
.tile:active { transform: scale(.97); }
.tile-icon { flex: 1 1 0; height: 0; min-height: 0; width: 100%; object-fit: contain; object-position: center; }
.t1 { background: var(--navy); } .t2 { background: var(--red); } .t3 { background: var(--green); }
.t4 { background: var(--blue); } .t5 { background: var(--purple); } .t6 { background: var(--magenta); }
.t7 { background: var(--yellow); color: var(--navy); } .t8 { background: var(--stone); color: var(--navy); }
.t9 { background: var(--navy); }
.t7 .tile-icon, .t8 .tile-icon { filter: brightness(0); opacity: .8; }
@media (prefers-color-scheme: dark) { .t1, .t9 { background: #223066; } }

/* ---------- Whiteboard ---------- */
#view-whiteboard { height: 100vh; height: 100dvh; overflow: hidden; }
.wb-stage {
    position: relative; flex: 1; min-height: 0; overflow: hidden; touch-action: none; cursor: crosshair;
    background-color: #fff; user-select: none; -webkit-user-select: none;
}
.wb-stage[data-bg="grid"] {
    background-image: linear-gradient(#dfe3ea 1px, transparent 1px), linear-gradient(90deg, #dfe3ea 1px, transparent 1px);
    background-size: 32px 32px;
}
.wb-stage[data-bg="dots"] { background-image: radial-gradient(#c3c9d6 1.6px, transparent 1.7px); background-size: 28px 28px; }
.wb-stage canvas { position: absolute; left: 0; top: 0; touch-action: none; }
#wb-live { pointer-events: none; }
.wb-tools {
    position: absolute; left: 50%; bottom: calc(14px + env(safe-area-inset-bottom)); transform: translateX(-50%);
    z-index: 15; display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 14px;
    width: max-content; max-width: calc(100vw - 20px); padding: 8px 12px;
    background: var(--surface); color: var(--text); border: 1px solid var(--border);
    border-radius: 22px; box-shadow: var(--shadow);
}
.wb-group { display: flex; align-items: center; gap: 4px; }
.wb-group + .wb-group { padding-left: 14px; border-left: 1px solid var(--border); }
.wb-tools .tool, .wb-tools .size {
    display: inline-grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; border: 0;
    background: transparent; color: var(--text);
}
.wb-tools .tool:hover, .wb-tools .size:hover { background: var(--surface-2); }
.wb-tools .tool[aria-pressed="true"], .wb-tools .size[aria-pressed="true"] { background: var(--navy); color: #fff; }
.wb-tools .tool:disabled { opacity: .35; cursor: default; }
.wb-tools .tool.danger:hover, .wb-tools .tool.confirm { background: var(--red); color: #fff; }
.wb-tools .size i { display: block; border-radius: 50%; background: currentColor; }
.wb-tools .size[data-size="3"] i { width: 5px; height: 5px; }
.wb-tools .size[data-size="6"] i { width: 10px; height: 10px; }
.wb-tools .size[data-size="12"] i { width: 18px; height: 18px; }
.swatch {
    width: 34px; height: 34px; margin: 5px 2px; border-radius: 50%; border: 3px solid var(--surface);
    box-shadow: 0 0 0 1px var(--border); background: var(--c);
}
.swatch[aria-pressed="true"] { box-shadow: 0 0 0 3px var(--navy); }
@media (prefers-color-scheme: dark) { .swatch[aria-pressed="true"] { box-shadow: 0 0 0 3px #fff; } }
@media (max-width: 700px) {
    .wb-group + .wb-group { border-left: 0; padding-left: 0; }
    .wb-tools { gap: 2px 8px; padding: 6px 8px; }
}

/* ---------- Notes tools (Traffic Lights, WWW/EBI) ---------- */
.note { display: flex; flex-direction: column; gap: 8px; }
.note > span { font-weight: 700; }
.note textarea {
    width: 100%; flex: 1; min-height: 160px; resize: none; padding: 16px; font-size: 1.1rem; line-height: 1.5;
    background: var(--surface); border: 2px solid var(--border); border-radius: var(--radius-sm);
}
.note textarea:focus { outline: none; border-color: var(--focus); }
.note-red > span { color: var(--red); } .note-amber > span { color: #b87400; } .note-green > span { color: var(--green); }
.note-yellow > span { color: #8a6d00; }
.note-red textarea { border-top: 8px solid var(--red); } .note-amber textarea { border-top: 8px solid #f0a500; }
.note-green textarea { border-top: 8px solid var(--green); } .note-yellow textarea { border-top: 8px solid var(--yellow); }
@media (prefers-color-scheme: dark) { .note-amber > span, .note-yellow > span { color: var(--yellow); } .note-green > span { color: #6fcf97; } .note-red > span { color: #ff8a8f; } }

.traffic { flex: 1; display: grid; gap: 16px; padding: 16px max(16px, env(safe-area-inset-right)) 16px max(16px, env(safe-area-inset-left)); grid-template-columns: 1fr; align-content: start; }
.lights { display: flex; flex-direction: row; gap: 12px; }
.light {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    min-height: 96px; border: 4px solid transparent; border-radius: var(--radius); color: #fff;
    background: var(--c); box-shadow: var(--shadow); transition: opacity .15s, transform .15s;
}
.light span { font-weight: 800; font-size: 1.2rem; } .light small { font-weight: 600; opacity: .95; }
.light-red { --c: var(--red); } .light-amber { --c: #f0a500; color: var(--navy); } .light-green { --c: var(--green); }
.lights:has([aria-pressed="true"]) .light[aria-pressed="false"] { opacity: .5; }
.light[aria-pressed="true"] { opacity: 1; border-color: var(--text); transform: scale(1.03); }
.light-notes { display: flex; flex-direction: column; }
.light-notes .note { flex: 1; }
.light-notes textarea { min-height: 220px; }
.hint { color: var(--muted); margin: 0; padding: 12px 0; }
@media (min-width: 760px) {
    .traffic { grid-template-columns: 240px 1fr; }
    .lights { flex-direction: column; }
    .light { min-height: 130px; }
}

.wwwebi { flex: 1; display: grid; gap: 16px; padding: 16px max(16px, env(safe-area-inset-right)) 16px max(16px, env(safe-area-inset-left)); grid-template-columns: 1fr; }
@media (min-width: 760px) { .wwwebi { grid-template-columns: 1fr 1fr; } .wwwebi .note textarea { min-height: 0; } }

/* ---------- Stem sentences ---------- */
.stems { flex: 1; display: grid; gap: 16px; padding: 16px max(16px, env(safe-area-inset-right)) 24px max(16px, env(safe-area-inset-left)); grid-template-columns: 1fr; align-content: start; }
@media (min-width: 760px) { .stems { grid-template-columns: 1fr 1fr; } }
.stem-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px 22px; border-top: 8px solid var(--green); }
.stem-card.challenge { border-top-color: var(--red); }
.stem-card h3 { margin: 0 0 8px; font-size: 1.1rem; }
.stem-card ul { margin: 0; padding: 0; list-style: none; }
.stem-card li { padding: 12px 0; font-size: 1.2rem; font-weight: 500; border-bottom: 1px solid var(--border); }
.stem-card li:last-child { border-bottom: 0; }

/* ---------- Choice (True/False, A B C D, E S D M) ---------- */
.choice { flex: 1; display: grid; gap: 12px; padding: 12px max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left)); grid-template-columns: 1fr; grid-auto-rows: 1fr; min-height: 0; }
.choice-4 { grid-template-columns: 1fr 1fr; }
@media (orientation: landscape) { .choice-2 { grid-template-columns: 1fr 1fr; } }
.choice-opt {
    display: grid; place-items: center; border: 0; border-radius: var(--radius); color: #fff;
    background: var(--c); box-shadow: var(--shadow); font-weight: 800; text-transform: uppercase; letter-spacing: .02em;
    transition: transform .12s ease;
}
.choice-opt:active { transform: scale(.985); }
.choice-label { font-size: clamp(3rem, 18vmin, 11rem); line-height: 1; }
.choice-words .choice-label { font-size: clamp(1.5rem, 7vmin, 4.5rem); }
.opt-true { --c: var(--green); } .opt-false { --c: var(--red); }
.opt-a { --c: var(--red); } .opt-b { --c: var(--blue); } .opt-c { --c: var(--green); } .opt-d { --c: var(--yellow); color: var(--navy); }
.choice[data-sel] { grid-template-columns: 1fr; }
.choice[data-sel] .choice-opt:not(.is-selected) { display: none; }
.choice[data-sel] .choice-label { font-size: clamp(5rem, 36vmin, 24rem); }
.choice-words[data-sel] .choice-label { font-size: clamp(3rem, 14vmin, 9rem); }
.choice-opt.is-selected { animation: pop .25s ease; }
@keyframes pop { from { transform: scale(.92); opacity: .6; } to { transform: none; opacity: 1; } }

/* ---------- Checklist ---------- */
.lead { margin: 0; padding: 14px max(16px, env(safe-area-inset-left)) 0; color: var(--muted); }
.progress { font-weight: 700; font-size: .9rem; padding-right: 4px; white-space: nowrap; }
.progress.done { color: var(--yellow); }
.checklist { display: grid; gap: 16px; padding: 14px max(16px, env(safe-area-inset-right)) 28px max(16px, env(safe-area-inset-left)); grid-template-columns: 1fr; align-content: start; }
@media (min-width: 760px) { .checklist { grid-template-columns: 1fr 1fr; } }
.cl-box { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border-top: 8px solid var(--c); padding: 8px 16px 12px; }
.cl-spelling { --c: var(--red); } .cl-punctuation { --c: var(--blue); } .cl-grammar { --c: var(--green); } .cl-presentation { --c: var(--magenta); }
.cl-box h3 { margin: 12px 0 4px; font-size: 1.25rem; color: var(--c); }
@media (prefers-color-scheme: dark) { .cl-box h3 { filter: brightness(1.5); } }
.cl-box ul { list-style: none; margin: 0; padding: 0; }
.cl-box li + li { border-top: 1px solid var(--border); }
.cl-box button[role="checkbox"] {
    position: relative; width: 100%; text-align: left; border: 0; background: transparent; padding: 14px 4px 14px 48px;
    font-size: 1rem; line-height: 1.4;
}
.cl-box button[role="checkbox"]::before {
    content: ""; position: absolute; left: 4px; top: 14px; width: 28px; height: 28px; border-radius: 8px;
    border: 3px solid var(--c); background: transparent; transition: background .15s;
}
.cl-box button[aria-checked="true"]::before {
    background: var(--c) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M5 12.5l4.5 4.5L19 7.5' fill='none' stroke='%23fff' stroke-width='3.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 80% no-repeat;
}
.cl-box button[aria-checked="true"] { color: var(--muted); }
.examples { display: block; margin-top: 8px; font-size: .85rem; color: var(--muted); }

/* ---------- Emoji ---------- */
.emoji-stage { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: 12px; }
#emoji-big { width: min(52vmin, 360px); height: min(52vmin, 360px); object-fit: contain; border-radius: 50%; }
#emoji-big.blank { border: 6px dashed var(--border); opacity: .6; }
.emoji-name { margin: 0; font-size: 1.4rem; font-weight: 700; min-height: 1.6em; }
.emoji-grid { list-style: none; margin: 0; padding: 12px max(12px, env(safe-area-inset-right)) 16px max(12px, env(safe-area-inset-left));
    display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px; }
@media (max-width: 620px) { .emoji-grid { grid-template-columns: repeat(4, 1fr); } }
.emoji-grid button { width: 100%; aspect-ratio: 1; padding: 6px; border: 3px solid transparent; border-radius: var(--radius-sm); background: var(--surface); box-shadow: var(--shadow); }
.emoji-grid button[aria-pressed="true"] { border-color: var(--navy); background: var(--yellow); }
@media (prefers-color-scheme: dark) { .emoji-grid button[aria-pressed="true"] { border-color: #fff; } }
.emoji-grid img { display: block; width: 100%; height: 100%; object-fit: contain; }
.egg { position: absolute; top: 4px; left: 4px; width: 14px; height: 14px; padding: 0; border: 0; background: transparent; }
#boo { position: absolute; top: 16px; left: 8px; max-width: 70vw; z-index: 5; }
@media (orientation: landscape) and (max-height: 520px) {
    .view#view-emoji { flex-direction: column; }
    #emoji-big { width: 34vh; height: 34vh; }
}

/* ---------- Toast ---------- */
.toast {
    position: fixed; left: 50%; top: calc(var(--bar-h) + env(safe-area-inset-top) + 12px); transform: translateX(-50%);
    z-index: 100; padding: 12px 20px; border-radius: 999px; background: var(--navy); color: #fff;
    font-weight: 600; box-shadow: var(--shadow); max-width: 90vw;
}

/* ---------- Narrow screens: icon-only action buttons ---------- */
@media (max-width: 640px) {
    .btn span { display: none; }
    .btn { padding: 0; width: 44px; justify-content: center; }
    #view-whiteboard .btn span { display: none; }
    .bar-actions { gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media print { .bar, .wb-tools, .toast { display: none !important; } }
