/* ABC Ceria
   Light theme: a Malaysian school exercise book (buku tulis) — blue rules, red margin, crayon letters.
   Dark theme: the classroom chalkboard (papan hitam) — green board, chalk colours, wooden tray. */

:root {
  --font: 'Andika', 'Nunito', 'Segoe UI', 'Trebuchet MS', system-ui, sans-serif;
  --font-ar: 'Noto Naskh Arabic', 'Geeza Pro', 'Amiri', 'Traditional Arabic', 'Arial', serif;
  --font-emoji: 'Apple Color Emoji', 'Segoe UI Emoji', 'Noto Color Emoji', 'Twemoji Mozilla', sans-serif;
  --radius-lg: 28px;
  --radius: 20px;
  --ease-pop: cubic-bezier(.34, 1.56, .64, 1);
  color-scheme: light;
}

[data-theme='light'] {
  --paper: #fafcff;
  --card: #ffffff;
  --ink: #1d2b4a;
  --muted: #5e6b86;
  --line: #dbe5f4;
  --rule: #d3e2f6;
  --rule-strong: #9fbde6;
  --margin: #ef8f8f;
  --focus: #2e6bd6;
  --shadow: 0 2px 0 #dbe5f4;
  --backdrop: rgba(29, 43, 74, .42);
  --c0: #e04848; /* red */
  --c1: #e8800f; /* orange */
  --c2: #c99400; /* yellow (deep enough to read on white) */
  --c3: #2c9b55; /* green */
  --c4: #2e6bd6; /* blue */
  --c5: #8a4fd6; /* purple */
  --on-k: #ffffff;
}

[data-theme='dark'] {
  color-scheme: dark;
  --paper: #1f3a32;
  --card: #274a40;
  --ink: #f1f4ea;
  --muted: #b2c5b9;
  --line: rgba(241, 244, 234, .16);
  --rule: rgba(241, 244, 234, .07);
  --rule-strong: rgba(241, 244, 234, .32);
  --margin: rgba(255, 160, 160, .55);
  --focus: #ffe07a;
  --shadow: 0 2px 0 rgba(0, 0, 0, .18);
  --backdrop: rgba(8, 18, 14, .62);
  --wood: #7a5230;
  --wood-light: #9a6a3f;
  --c0: #ff8f8f;
  --c1: #ffb870;
  --c2: #ffe07a;
  --c3: #8fe3a8;
  --c4: #92c5ff;
  --c5: #cdaeff;
  --on-k: #1f3a32;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.4;
  color: var(--ink);
  background-color: var(--paper);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* Exercise-book page: blue rules and a red margin line */
[data-theme='light'] body {
  background-image:
    linear-gradient(to right, transparent 22px, var(--margin) 22px, var(--margin) 24px, transparent 24px),
    linear-gradient(to bottom, transparent 35px, var(--rule) 35px, var(--rule) 36px);
  background-size: 100% 100%, 100% 36px;
}
/* Chalkboard: faint smudges of old chalk */
[data-theme='dark'] body {
  background-image:
    radial-gradient(60% 40% at 20% 30%, rgba(255, 255, 255, .045), transparent 70%),
    radial-gradient(50% 35% at 80% 70%, rgba(255, 255, 255, .035), transparent 70%),
    radial-gradient(40% 30% at 60% 15%, rgba(255, 255, 255, .03), transparent 70%);
}

button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:focus { outline: none; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.icon { width: 24px; height: 24px; display: block; }
.emoji { font-family: var(--font-emoji); line-height: 1; }
[lang='ar'] { font-family: var(--font-ar); }

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 6px max(34px, env(safe-area-inset-left));
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 6px; min-width: 0; }
.logo { display: flex; align-items: flex-end; height: 34px; }
.logo span { display: block; height: 100%; margin-right: -3px; }
.logo span:nth-child(1) { transform: rotate(-6deg); }
.logo span:nth-child(2) { transform: translateY(-2px) rotate(3deg); }
.logo span:nth-child(3) { transform: rotate(-2deg); }
.logo .glyph { height: 100%; width: auto; display: block; }
.brand-name { font-weight: 700; font-size: 26px; letter-spacing: .01em; margin-left: 4px; line-height: 1; }

.controls { display: flex; align-items: center; gap: 6px; }
.lang {
  display: flex;
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 999px;
  padding: 3px;
}
.lang button {
  border: 0;
  background: transparent;
  min-width: 46px;
  height: 38px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 16px;
  color: var(--muted);
}
.lang button[aria-pressed='true'] { background: var(--ink); color: var(--paper); }
.icon-btn {
  width: 46px; height: 46px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: var(--card);
}
.icon-btn:active { transform: scale(.92); }

/* ---------- section tabs (like notebook index tabs) ---------- */
.sections {
  display: flex;
  gap: 8px;
  padding: 8px max(16px, env(safe-area-inset-right)) 0 max(34px, env(safe-area-inset-left));
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
}
.sections button {
  flex: 1;
  max-width: 220px;
  border: 2px solid var(--line);
  border-bottom: 0;
  border-top: 6px solid color-mix(in srgb, var(--k) 45%, transparent);
  border-radius: 16px 16px 0 0;
  background: color-mix(in srgb, var(--card) 55%, transparent);
  padding: 6px 8px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  color: var(--muted);
  transition: transform .2s var(--ease-pop);
}
.sections button[aria-selected='true'] {
  background: var(--card);
  border-top-color: var(--k);
  color: var(--ink);
  transform: translateY(-3px);
  box-shadow: 0 6px 0 -2px var(--card);
}
.tab-big { font-size: 24px; font-weight: 700; line-height: 1.1; letter-spacing: .04em; }
.sections button[aria-selected='true'] .tab-big { color: var(--k); }
.tab-ar { letter-spacing: 0; font-size: 24px; line-height: 1.25; }
.tab-small { font-size: 13px; }

main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  border-top: 2px solid var(--line);
  background: color-mix(in srgb, var(--card) 35%, transparent);
}

/* ---------- learn grid ---------- */
.learn { max-width: 1120px; margin: 0 auto; padding: 10px max(16px, env(safe-area-inset-right)) 28px max(34px, env(safe-area-inset-left)); }
.learn-hint { margin: 4px 0 12px; color: var(--muted); font-size: 16px; }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(102px, 1fr));
  gap: 14px 12px;
}
.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 12px 6px 10px;
  border-radius: var(--radius);
  border: 2.5px solid color-mix(in srgb, var(--k) 40%, transparent);
  background: color-mix(in srgb, var(--k) 8%, var(--card));
  box-shadow: 0 3px 0 color-mix(in srgb, var(--k) 30%, transparent);
  color: var(--ink);
  transition: transform .18s var(--ease-pop);
}
.tile:nth-child(4n + 1) { transform: rotate(-1deg); }
.tile:nth-child(4n + 3) { transform: rotate(.8deg); }
.tile:active { transform: scale(.94); }
.tile-glyph { display: flex; justify-content: center; align-items: flex-start; gap: 0; height: 56px; color: var(--k); }
.tile-glyph .glyph { height: 100%; width: auto; }
.tile-pic { font-size: 30px; height: 36px; display: grid; place-items: center; }
.tile-pic .pic-svg { width: 36px; height: 36px; }
.tile-word { font-weight: 700; font-size: 15px; line-height: 1.15; text-align: center; overflow-wrap: anywhere; }
.tile-num { padding-top: 14px; }
.tile-num .tile-glyph { height: 58px; }
.tile-num .tile-word { margin-top: 4px; }
.tile-hij { padding-top: 6px; }
.tile-ar { font-size: 52px; line-height: 1.5; height: 82px; color: var(--k); font-weight: 700; display: grid; place-items: center; }
.tile-stars { position: absolute; top: 4px; right: 7px; display: flex; gap: 1px; font-size: 13px; line-height: 1; }
.grid[dir='rtl'] .tile-stars { right: auto; left: 7px; }
.mini-star { color: var(--line); }
.mini-star.is-on { color: #f2b705; }
[data-theme='dark'] .mini-star.is-on { color: var(--c2); }

/* ---------- flashcard ---------- */
dialog {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--ink);
  width: min(500px, calc(100vw - 20px));
  max-width: none;
  max-height: calc(100dvh - 20px);
  overflow: visible;
}
dialog::backdrop { background: var(--backdrop); }
.card {
  position: relative;
  background: var(--card);
  border-radius: var(--radius-lg);
  border-top: 12px solid var(--k);
  box-shadow: 0 10px 0 color-mix(in srgb, var(--k) 35%, transparent), 0 24px 60px rgba(0, 0, 0, .22);
  max-height: calc(100dvh - 30px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
[data-theme='dark'] .card { background: #2b5146; }
.card-top { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px 0 20px; }
.card-pos { color: var(--muted); font-size: 15px; font-variant-numeric: tabular-nums; }
.card-top .icon-btn { width: 44px; height: 44px; }
.card-body {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 0 20px 16px;
  gap: 6px;
}
.card-body > button { border: 0; background: transparent; padding: 0; }
.card-glyph { display: flex; justify-content: center; height: 118px; color: var(--k); }
.card-glyph .glyph { height: 100%; width: auto; }
.card-glyph-num { height: 104px; }
.card-ar { font-size: 96px; line-height: 1.35; height: 140px; font-weight: 700; color: var(--k); }
.card-pic { font-size: 86px; line-height: 1; min-height: 96px; display: grid; place-items: center; }
.card-pic .pic-svg { width: 100px; height: 100px; }
.card-word { margin: 0; font-size: 34px; font-weight: 700; line-height: 1.15; }
.card-word .first { color: var(--k); }
.card-word-ar { font-size: 38px; line-height: 1.6; }
.card-line { margin: 0 0 4px; color: var(--muted); font-size: 17px; }
.card-name { margin: -6px 0 6px; display: flex; gap: 12px; align-items: baseline; font-size: 26px; font-weight: 700; }
.card-name-ar { font-size: 26px; color: var(--muted); font-weight: 500; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 2px solid var(--line);
  background: var(--paper);
  border-radius: 999px;
  padding: 6px 10px 6px 14px;
  min-height: 44px;
}
.chip-label { font-size: 13px; color: var(--muted); }
.chip-text { font-weight: 700; font-size: 18px; }
.chip-icon { width: 20px; height: 20px; color: var(--muted); }

.syl { margin-top: 8px; width: 100%; }
.syl-label { margin: 0 0 6px; font-size: 14px; color: var(--muted); }
.syl-row { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }
.syl-btn {
  min-width: 54px; height: 54px;
  padding: 0 10px;
  border-radius: 16px;
  border: 2px solid color-mix(in srgb, var(--k) 40%, transparent);
  background: color-mix(in srgb, var(--k) 10%, var(--card));
  font-size: 22px;
  font-weight: 700;
}
.syl-ar { height: auto; min-width: 70px; padding: 2px 10px 6px; display: flex; flex-direction: column; align-items: center; }
.syl-ar span { font-size: 40px; line-height: 1.5; color: var(--k); }
.syl-ar small { font-family: var(--font); font-size: 16px; color: var(--muted); line-height: 1; }

.count-area {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-width: 340px;
  margin: 6px 0 0;
}
.count-thing {
  position: relative;
  width: 58px; height: 58px;
  border-radius: 16px;
  border: 2px dashed var(--line);
  background: transparent;
  display: grid; place-items: center;
  font-size: 34px;
  transition: transform .2s var(--ease-pop), background .2s;
}
.count-area.is-many { max-width: 360px; }
.count-area.is-many .count-thing { width: 46px; height: 46px; font-size: 26px; border-radius: 12px; }
.count-thing.is-counted { border-style: solid; border-color: var(--k); background: color-mix(in srgb, var(--k) 14%, var(--card)); }
.count-num {
  position: absolute; top: -8px; right: -6px;
  min-width: 24px; height: 24px; padding: 0 5px;
  border-radius: 999px;
  background: var(--k); color: var(--on-k);
  font-size: 14px; font-weight: 700; line-height: 24px;
  opacity: 0; transform: scale(.4);
  transition: transform .25s var(--ease-pop), opacity .15s;
}
.count-thing.is-counted .count-num { opacity: 1; transform: scale(1); }
.count-empty {
  width: 130px; height: 130px;
  border-radius: 50%;
  border: 3px dashed var(--line);
  display: grid; place-items: center;
  padding: 18px;
  color: var(--muted);
  font-size: 15px;
}
.count-hint { margin: 2px 0 6px; font-size: 14px; color: var(--muted); }

.card-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px calc(14px + env(safe-area-inset-bottom));
  border-top: 2px solid var(--line);
}
.round-btn {
  width: 54px; height: 54px; flex: none;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: var(--card);
  display: grid; place-items: center;
}
.round-btn:active { transform: scale(.92); }
.pill-btn {
  display: inline-flex; align-items: center; gap: 8px;
  height: 54px; padding: 0 24px;
  border-radius: 999px;
  border: 0;
  background: var(--k);
  color: var(--on-k);
  font-weight: 700; font-size: 19px;
  box-shadow: 0 4px 0 color-mix(in srgb, var(--k) 55%, #000);
}
.pill-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 color-mix(in srgb, var(--k) 55%, #000); }

/* ---------- trace ---------- */
.trace {
  height: 100%;
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px max(14px, env(safe-area-inset-right)) 10px max(30px, env(safe-area-inset-left));
}
.trace-head { display: grid; grid-template-columns: 54px 1fr 54px; align-items: center; gap: 10px; }
.trace-title {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  min-height: 54px;
  border: 0; background: transparent;
  border-radius: 999px;
}
.tt-big { font-size: 34px; font-weight: 700; color: var(--k); line-height: 1; }
.tt-ar { font-size: 40px; line-height: 1.3; color: var(--k); font-weight: 700; }
.tt-name { font-size: 22px; font-weight: 700; }
.tt-icon { color: var(--muted); }
.trace[data-section='hij'] .trace-head .round-btn .icon { transform: scaleX(-1); }

.strip-row { display: flex; align-items: center; gap: 10px; min-width: 0; }
.case-toggle { display: flex; flex: none; border: 2px solid var(--line); border-radius: 14px; padding: 2px; background: var(--card); }
.case-toggle button {
  width: 42px; height: 40px; border: 0; border-radius: 10px; background: transparent;
  font-size: 22px; font-weight: 700; color: var(--muted);
}
.case-toggle button[aria-pressed='true'] { background: var(--k); color: var(--on-k); }
.strip {
  display: flex; gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 4px 2px;
  min-width: 0;
  flex: 1;
  -webkit-mask-image: linear-gradient(to right, transparent, #000 14px, #000 calc(100% - 14px), transparent);
  mask-image: linear-gradient(to right, transparent, #000 14px, #000 calc(100% - 14px), transparent);
}
.strip::-webkit-scrollbar { display: none; }
.strip-btn {
  flex: none;
  position: relative;
  min-width: 40px; height: 40px; padding: 0 6px;
  border-radius: 12px;
  border: 2px solid var(--line);
  background: var(--card);
  font-weight: 700; font-size: 18px;
  color: var(--muted);
}
.strip-btn [lang='ar'] { font-size: 22px; line-height: 1; }
.strip-btn.is-starred::after {
  content: '★';
  position: absolute; top: -7px; right: -4px;
  font-size: 13px; color: #f2b705;
}
[data-theme='dark'] .strip-btn.is-starred::after { color: var(--c2); }
.strip-btn.is-current { border-color: var(--k); background: var(--k); color: var(--on-k); }

.stage-wrap {
  position: relative;
  flex: 1;
  min-height: 200px;
  border-radius: var(--radius-lg);
  background: var(--card);
  border: 2px solid var(--line);
  box-shadow: var(--shadow);
  overflow: hidden;
}
[data-theme='dark'] .stage-wrap { background: #22423a; box-shadow: inset 0 0 0 6px var(--wood), inset 0 0 0 8px var(--wood-light); border-color: transparent; }
.stage { position: absolute; inset: 12px; }
.trace-guide, .trace-ink, .trace-demo { position: absolute; inset: 0; width: 100%; height: 100%; }
.trace-ink { touch-action: none; cursor: crosshair; }
.trace-demo { pointer-events: none; }
.trace-guide { overflow: hidden; }

.rule { stroke: var(--rule-strong); stroke-width: .7; }
.rule-mid { stroke: var(--margin); stroke-dasharray: 2.5 2.5; }
.rule-base { stroke-width: 1.1; }
.rule-desc { opacity: .5; }
.guide path { fill: none; stroke: color-mix(in srgb, var(--k) 16%, var(--card)); stroke-linecap: round; stroke-linejoin: round; }
[data-theme='dark'] .guide path { stroke: rgba(241, 244, 234, .14); }
.guide-dots path { fill: none; stroke: var(--k); stroke-width: 2.4; stroke-dasharray: .01 5.2; stroke-linecap: round; opacity: .6; }
.badge circle { fill: var(--k); }
.badge text { fill: var(--on-k); font-size: 8px; font-weight: 700; font-family: var(--font); }
.badge { transition: opacity .3s; }
.badge.is-done { opacity: 0; }
.stage.is-done .guide-dots { opacity: 0; transition: opacity .4s; }
.demo-path { fill: none; stroke-linecap: round; stroke-linejoin: round; opacity: .5; }
.demo-finger { fill: rgba(255, 255, 255, .7); stroke: var(--ink); stroke-width: 1.6; }

.praise {
  position: absolute; left: 50%; bottom: 14px;
  padding: 6px 22px 8px;
  border-radius: 999px;
  background: var(--k);
  color: var(--on-k);
  font-size: 30px; font-weight: 700; line-height: 1.2;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transform: translateX(-50%) scale(.5);
}
.praise::before { content: '★ '; }
.praise:empty { display: none; }
.praise.is-on { animation: praise .6s var(--ease-pop) forwards; }
@keyframes praise { to { opacity: 1; transform: translateX(-50%) scale(1); } }

.meter { height: 10px; border-radius: 999px; background: var(--line); overflow: hidden; flex: none; }
.meter-fill { height: 100%; background: var(--k); transform-origin: left; transform: scaleX(0); transition: transform .25s; border-radius: 999px; }
.trace-hint { margin: -2px 0 0; text-align: center; color: var(--muted); font-size: 16px; min-height: 22px; transition: opacity .3s; }
.trace-hint.is-quiet { opacity: 0; }

.tools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: center; }
.tool {
  display: inline-flex; align-items: center; gap: 6px;
  height: 48px; padding: 0 16px 0 12px;
  border-radius: 999px;
  border: 2px solid var(--line);
  background: var(--card);
  font-weight: 700; font-size: 16px;
}
.tool:active { transform: scale(.95); }
.swatches { display: flex; gap: 6px; padding: 0 4px; }
.swatch {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 3px solid var(--card);
  background: var(--sw);
  box-shadow: 0 0 0 2px var(--line);
}
.swatch[aria-pressed='true'] { box-shadow: 0 0 0 3px var(--ink); transform: scale(1.08); }

#traceNext.nudge { background: var(--k); color: var(--on-k); border-color: var(--k); animation: nudge 1s ease-in-out 3; }
@keyframes nudge { 50% { transform: translateX(4px) scale(1.06); } }

/* ---------- bottom nav ---------- */
.bottom-nav {
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 8px max(14px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  background: var(--card);
  border-top: 2px solid var(--line);
}
[data-theme='dark'] .bottom-nav { background: linear-gradient(var(--wood-light), var(--wood)); border-top: 0; box-shadow: 0 -4px 0 rgba(0, 0, 0, .2); }
.bottom-nav button {
  flex: 1;
  max-width: 240px;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  height: 54px;
  border-radius: 18px;
  border: 0;
  background: transparent;
  font-weight: 700; font-size: 18px;
  color: var(--muted);
}
[data-theme='dark'] .bottom-nav button { color: #f3dcc0; }
.bottom-nav button[aria-current='page'] { background: var(--ink); color: var(--paper); }
[data-theme='dark'] .bottom-nav button[aria-current='page'] { background: #f1f4ea; color: #4a3019; }

/* ---------- toast & misc ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  max-width: min(440px, calc(100vw - 32px));
  background: var(--ink);
  color: var(--paper);
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s, transform .25s;
  z-index: 50;
}
.toast.is-on { opacity: 1; transform: translate(-50%, 0); }
.confetti { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 60; }
.noscript { padding: 24px; font-size: 18px; }

.bounce { animation: bounce .45s var(--ease-pop); }
@keyframes bounce { 30% { transform: scale(.9); } 70% { transform: scale(1.06); } }
.cheer { animation: cheer .8s ease-in-out 2; }
@keyframes cheer { 25% { transform: rotate(-6deg) scale(1.08); } 75% { transform: rotate(6deg) scale(1.08); } }
.enter-next .card-body { animation: in-next .3s ease-out; }
.enter-prev .card-body { animation: in-prev .3s ease-out; }
@keyframes in-next { from { opacity: 0; transform: translateX(30px); } }
@keyframes in-prev { from { opacity: 0; transform: translateX(-30px); } }
.card[data-sec='hij'].enter-next .card-body { animation-name: in-prev; }
.card[data-sec='hij'].enter-prev .card-body { animation-name: in-next; }

/* ---------- larger screens ---------- */
@media (min-width: 700px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 18px 16px; }
  .tile { padding: 16px 8px 12px; }
  .tile-glyph { height: 72px; }
  .tile-pic { font-size: 38px; height: 44px; }
  .tile-pic .pic-svg { width: 44px; height: 44px; }
  .tile-word { font-size: 17px; }
  .tile-ar { font-size: 66px; height: 104px; }
  .tile-num .tile-glyph { height: 76px; }
  .top, .sections, .learn { padding-left: max(44px, env(safe-area-inset-left)); }
  [data-theme='light'] body {
    background-image:
      linear-gradient(to right, transparent 30px, var(--margin) 30px, var(--margin) 32px, transparent 32px),
      linear-gradient(to bottom, transparent 35px, var(--rule) 35px, var(--rule) 36px);
  }
  .trace { padding-left: max(44px, env(safe-area-inset-left)); }
}
@media (max-width: 430px) {
  .top { gap: 6px; }
  .brand-name { font-size: 22px; }
  .logo { height: 28px; }
  .lang button { min-width: 40px; height: 34px; font-size: 15px; }
  .icon-btn { width: 40px; height: 40px; }
  .controls { gap: 5px; }
}
@media (max-width: 360px) {
  .brand-name { display: none; }
}
@media (max-width: 380px) {
  .grid { grid-template-columns: repeat(3, 1fr); gap: 10px 8px; }
  .tool { padding: 0 12px 0 10px; }
  .swatch { width: 30px; height: 30px; }
}
@media (max-height: 640px) {
  .trace { gap: 6px; padding-top: 6px; }
  .trace-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* Alif Ba Ta reads right to left: "back" sits on the right */
.trace[data-section='hij'] .trace-head,
.card[data-sec='hij'] .card-foot { direction: rtl; }
.card[data-sec='hij'] .card-foot .round-btn .icon { transform: scaleX(-1); }

/* Small phones: give the tracing board as much room as possible */
@media (max-height: 720px) {
  .tab-small { display: none; }
  .sections button { padding: 4px 8px 6px; }
  .top { padding-top: calc(6px + env(safe-area-inset-top)); padding-bottom: 2px; }
}
@media (max-width: 400px) {
  .tool { width: 48px; padding: 0; justify-content: center; }
  .tool-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .swatches { gap: 4px; padding: 0; }
  .swatch { width: 32px; height: 32px; }
}
