:root {
  --sky: #34b7e6;
  --sky-deep: #239dcc;
  --blue: #184b9f;
  --blue-2: #2e64c4;
  --coral: #ef625e;
  --orange: #f59b42;
  --green: #6bbf58;
  --yellow: #ffc94a;
  --purple: #6a50bd;
  --ink: #19334d;
  --muted: #6f8090;
  --line: #dfeaf2;
  --paper: #ffffff;
  --soft: #f6fbff;
  --shadow: 0 16px 42px rgba(29, 88, 128, .14);
  --radius: 26px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: linear-gradient(180deg, #e9f9ff 0%, #f7fcff 42%, #f8fff4 100%);
  font-family: "Tajawal", Tahoma, Arial, sans-serif;
  font-weight: 400;
}
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
button:focus-visible { outline: 3px solid rgba(24,75,159,.28); outline-offset: 2px; }

.title-font, h1, h2, h3, .big-letter, .stage-letter, .story-title {
  font-family: "Baloo Bhaijaan 2", "Tajawal", sans-serif;
}


[hidden] { display: none !important; }
.app-shell { min-height: 100vh; }
.persistent-brand {
  width: 100%;
  background: var(--sky);
  overflow: hidden;
  box-shadow: 0 14px 40px rgba(27,92,135,.18);
  scroll-margin-top: 72px;
}
.persistent-brand img { display:block; width:100%; height:auto; object-fit:contain; object-position:center top; }
.home-start { padding: 22px 0 30px; }
#app { scroll-margin-top: 82px; }
#app { min-height: 55vh; padding-bottom: calc(88px + var(--safe-bottom)); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  min-height: 72px;
  display: grid;
  grid-template-columns: 54px minmax(0,1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 8px max(14px, env(safe-area-inset-right)) 8px max(14px, env(safe-area-inset-left));
  background: rgba(255,255,255,.94);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid rgba(24,75,159,.07);
  box-shadow: 0 5px 22px rgba(26,83,122,.08);
}
.top-title { text-align: center; line-height: 1; }
.top-title span { display: block; color: var(--blue); font-family: "Baloo Bhaijaan 2", sans-serif; font-size: 1.32rem; font-weight: 800; }
.top-title small { display: block; margin-top: 2px; color: var(--coral); font-weight: 700; }
.top-actions { display: flex; gap: 7px; }
.icon-btn {
  width: 46px; height: 46px; border: 0; border-radius: 15px;
  background: #edf7ff; color: var(--blue); font-weight: 800; cursor: pointer;
  box-shadow: inset 0 -2px 0 rgba(24,75,159,.07);
}
.icon-btn:active { transform: translateY(1px); }

.bottom-nav {
  position: fixed; z-index: 55; bottom: 0; left: 0; right: 0;
  display: grid; grid-template-columns: repeat(4,1fr);
  padding: 8px max(8px, env(safe-area-inset-right)) calc(8px + var(--safe-bottom)) max(8px, env(safe-area-inset-left));
  background: rgba(255,255,255,.96);
  border-top: 1px solid rgba(24,75,159,.08);
  box-shadow: 0 -10px 30px rgba(25,75,110,.08);
}
.nav-item {
  border: 0; background: transparent; color: #7e8d99; cursor: pointer;
  display: grid; justify-items: center; gap: 2px; padding: 6px 2px; border-radius: 15px;
  font-weight: 800;
}
.nav-item span { font-size: 1.18rem; line-height: 1; }
.nav-item small { font-size: .74rem; font-weight: 800; }
.nav-item.active { color: var(--blue); background: #eef5ff; }

/* Home */
.hero-screen { padding: 0 0 28px; }
.hero-brand {
  width: 100%;
  background: var(--sky);
  overflow: hidden;
  box-shadow: 0 14px 40px rgba(27,92,135,.18);
}
.hero-brand img {
  display: block;
  width: 100%;
  height: auto;
  object-fit: contain;
  object-position: center top;
}
.hero-cta {
  width: min(900px, calc(100% - 28px));
  margin: 22px auto 0;
  display: grid;
  grid-template-columns: minmax(0,1fr) auto;
  gap: 20px;
  align-items: center;
  background: rgba(255,255,255,.97);
  border: 1px solid rgba(24,75,159,.08);
  border-radius: 28px;
  padding: 20px 22px;
  box-shadow: var(--shadow);
}
.hero-cta h1 { margin: 0; color: var(--blue); font-size: clamp(1.7rem, 4vw, 2.6rem); line-height: 1.1; }
.hero-cta p { margin: 5px 0 0; color: var(--muted); font-weight: 500; line-height: 1.8; }
.cta-btn, .primary-btn, .secondary-btn, .success-btn, .ghost-btn {
  border: 0; border-radius: 18px; cursor: pointer; padding: 14px 22px;
  font-family: "Tajawal", sans-serif; font-weight: 800; font-size: 1.03rem;
  transition: transform .15s ease, box-shadow .15s ease;
}
.cta-btn, .primary-btn { color: #fff; background: linear-gradient(180deg, #2b62c6, #184b9f); box-shadow: 0 10px 22px rgba(24,75,159,.23); }
.cta-btn { min-width: 190px; font-size: 1.13rem; }
.secondary-btn { background: #fff; color: var(--blue); border: 2px solid #d9e8fb; }
.success-btn { background: linear-gradient(180deg, #7acb64, #59ac49); color: #fff; box-shadow: 0 10px 22px rgba(72,154,58,.2); }
.ghost-btn { background: #eef7ff; color: var(--blue); }
.cta-btn:active, .primary-btn:active, .secondary-btn:active, .success-btn:active, .ghost-btn:active { transform: translateY(2px); }

/* Journey */
.page { width: min(1160px, 100%); margin: 0 auto; padding: 28px 18px 34px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 18px; margin-bottom: 22px; }
.page-head h2 { margin: 0; color: var(--blue); font-size: clamp(1.8rem, 4vw, 2.65rem); line-height: 1.1; }
.page-head p { margin: 5px 0 0; color: var(--muted); font-weight: 500; }
.progress-pill { direction: rtl; unicode-bidi: plaintext; background: #fff; color: var(--purple); border-radius: 999px; padding: 10px 15px; font-weight: 800; box-shadow: var(--shadow); white-space: nowrap; }
.journey-map { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 18px; }
.letter-stop {
  position: relative; min-height: 182px; border: 0; border-radius: 30px; padding: 22px;
  color: #fff; text-align: right; cursor: pointer; overflow: hidden; box-shadow: var(--shadow);
  font-weight: 700; transition: transform .16s ease;
}
.letter-stop:hover { transform: translateY(-3px); }
.letter-stop h3 { margin: 0 0 5px; font-size: 2rem; color: #fff; }
.letter-stop p { margin: 0; opacity: .94; line-height: 1.75; }
.letter-stop .big-letter { position: absolute; left: 16px; bottom: -26px; font-size: 8.8rem; opacity: .2; line-height: 1; }
.letter-stop .status { display: inline-flex; margin-top: 15px; border-radius: 999px; padding: 7px 12px; background: rgba(255,255,255,.18); font-size: .78rem; font-weight: 800; }
.letter-stop.m { background: linear-gradient(135deg,#f06a63,#dc4854); }
.letter-stop.b { background: linear-gradient(135deg,#2e77d1,#184ba7); }
.letter-stop.l { background: linear-gradient(135deg,#45b8e1,#238cb9); }
.letter-stop.d { background: linear-gradient(135deg,#f9ae44,#ed7b2e); }
.letter-stop.n { background: linear-gradient(135deg,#7bc865,#4aaa4e); }
.letter-stop.r { background: linear-gradient(135deg,#a36bd9,#6845bd); }

/* Lesson with right sidebar */
.lesson-page { width: min(1280px,100%); margin: 0 auto; padding: 20px 18px 34px; }
.lesson-head {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  margin-bottom: 16px; background: #fff; border-radius: 24px; padding: 16px 18px; box-shadow: var(--shadow);
}
.lesson-head-main { display: flex; align-items: center; gap: 14px; min-width: 0; }
.stage-letter {
  width: 66px; height: 66px; border-radius: 22px; color: #fff; display: grid; place-items: center;
  font-size: 3rem; line-height: 1; flex: none; box-shadow: inset 0 -4px 0 rgba(0,0,0,.07);
}
.lesson-head h2 { margin: 0; color: var(--blue); font-size: clamp(1.65rem, 3.5vw, 2.35rem); }
.lesson-head p { margin: 2px 0 0; color: var(--muted); font-weight: 500; }
.lesson-layout {
  direction: ltr;
  display: grid;
  grid-template-columns: minmax(0,1fr) 286px;
  gap: 18px;
  align-items: start;
}
.lesson-main, .lesson-sidebar { direction: rtl; }
.lesson-sidebar {
  position: sticky; top: 90px;
  background: #fff; border-radius: 26px; padding: 12px; box-shadow: var(--shadow);
  border: 1px solid rgba(24,75,159,.07);
}
.section-nav-title { padding: 7px 8px 12px; color: var(--blue); font-family: "Baloo Bhaijaan 2", sans-serif; font-size: 1.25rem; font-weight: 800; }
.section-nav { display: grid; gap: 8px; }
.section-nav-btn {
  width: 100%; border: 0; border-radius: 17px; background: #f7fbfe; color: var(--ink); cursor: pointer;
  display: grid; grid-template-columns: 44px minmax(0,1fr) 24px; align-items: center; gap: 9px;
  padding: 8px 9px; text-align: right; font-weight: 800; transition: .15s ease;
}
.section-nav-btn:hover { transform: translateX(-2px); }
.section-nav-btn.active { background: var(--section-soft); color: var(--section-color); box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--section-color) 22%, transparent); }
.section-icon {
  width: 42px; height: 42px; border-radius: 14px; display: grid; place-items: center;
  color: #fff; background: var(--section-color); box-shadow: inset 0 -3px 0 rgba(0,0,0,.08);
}
.section-icon svg { width: 23px; height: 23px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.section-nav-btn .nav-check { color: var(--green); font-weight: 800; opacity: 0; }
.section-nav-btn.done .nav-check { opacity: 1; }

.lesson-card {
  min-height: 590px;
  background: rgba(255,255,255,.98); border: 1px solid rgba(24,75,159,.07);
  border-radius: 30px; padding: 26px; box-shadow: var(--shadow); overflow: hidden;
}
.lesson-card-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; margin-bottom: 20px; }
.lesson-card-head h3 { margin: 0; color: var(--blue); font-size: clamp(1.65rem, 3.4vw, 2.35rem); line-height: 1.15; }
.lesson-card-head p { margin: 7px 0 0; color: var(--muted); line-height: 1.85; max-width: 760px; }
.section-badge { display: inline-flex; align-items: center; gap: 7px; border-radius: 999px; padding: 8px 12px; color: var(--section-color); background: var(--section-soft); font-weight: 800; white-space: nowrap; }
.content-stack { display: grid; gap: 16px; }
.feature-box { background: #f8fcff; border: 1px solid #e2eef6; border-radius: 22px; padding: 18px; }
.feature-box h4 { margin: 0 0 8px; color: var(--blue); font-size: 1.1rem; font-weight: 800; }
.feature-box p { margin: 0; line-height: 1.95; }
.letter-orb {
  min-height: 285px; border-radius: 30px; display: grid; place-items: center;
  background: radial-gradient(circle at 50% 28%, #fff 0%, #fff8ed 47%, #ffe4ba 100%);
  border: 3px solid #fff; box-shadow: inset 0 0 0 1px #f3dfc2, 0 12px 35px rgba(34,89,125,.12);
}
.letter-orb .glyph { font-family: "Baloo Bhaijaan 2", sans-serif; font-size: clamp(9rem,24vw,14.5rem); color: var(--letter-color); line-height: .78; }
.letter-forms { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.form-tabs { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); gap:10px; }
.form-tab { border:0; border-radius:16px; padding:12px 10px; background:#f1f7ff; color:var(--blue); font-weight:800; cursor:pointer; }
.form-tab.active { background:var(--letter-color); color:#fff; box-shadow:0 8px 18px color-mix(in srgb, var(--letter-color) 24%, transparent); }
.form-display { min-height:315px; text-align:center; }
.form-current-label { color:var(--muted); font-weight:800; margin-bottom:22px; font-size:1.05rem; }
.form-display .glyph { font-size:clamp(8rem,22vw,13rem); }
.form-chip, .word-chip, .sound-choice, .quiz-choice, .hunt-chip {
  border: 0; border-radius: 16px; padding: 11px 15px; cursor: pointer; background: #f1f7ff; color: var(--blue); font-weight: 800;
}
.form-chip { font-family: "Baloo Bhaijaan 2", sans-serif; font-size: 1.5rem; min-width: 74px; }
.word-grid { display: grid; grid-template-columns: repeat(4,minmax(0,1fr)); gap: 11px; }
.word-chip { min-height: 58px; font-size: 1.02rem; }
.word-chip:hover, .sound-choice:hover, .quiz-choice:hover, .hunt-chip:hover { background: #e7f1ff; }
.sound-row { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 12px; }
.sound-pair { text-align: center; border-radius: 22px; padding: 17px; background: #f7fbff; border: 1px solid #e2edf6; }
.sound-pair strong { display: block; font-family: "Baloo Bhaijaan 2", sans-serif; font-size: 2.15rem; color: var(--blue); }
.sound-pair span { color: var(--muted); font-weight: 700; }
.audio-btn { display: inline-flex; align-items: center; gap: 7px; border: 0; border-radius: 14px; padding: 10px 13px; background: #eef7ff; color: var(--blue); cursor: pointer; font-weight: 800; }
.audio-btn::before { content: "♪"; font-size: 1.15rem; }
.audio-note { border-radius: 16px; padding: 12px 14px; background: #fff8e4; color: #7e5d14; font-weight: 500; line-height: 1.7; }

.hunt-grid, .quiz-grid { display: flex; flex-wrap: wrap; gap: 10px; }
.hunt-chip.selected { background: #dcecff; box-shadow: inset 0 0 0 2px var(--blue); }
.hunt-chip.good, .quiz-choice.correct { background: #e2f7df; color: #2d7f31; box-shadow: inset 0 0 0 2px #72bf68; }
.hunt-chip.bad, .quiz-choice.wrong { background: #ffe7e5; color: #a73e39; box-shadow: inset 0 0 0 2px #ed7b73; }

.trace-wrap { display: grid; grid-template-columns: minmax(0,1fr) 200px; gap: 16px; align-items: stretch; }
.trace-board { position: relative; min-height: 360px; border-radius: 26px; overflow: hidden; background: #fcfdff; border: 2px dashed #c9ddeb; touch-action: none; }
.trace-guide { position: absolute; inset: 0; display: grid; place-items: center; font-family: "Baloo Bhaijaan 2", sans-serif; font-size: min(47vw, 16rem); color: #e1e7ec; user-select: none; pointer-events: none; }
.trace-board canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.trace-tools { display: grid; align-content: center; gap: 10px; }
.trace-tools .status-box { background: #eff8ff; border-radius: 18px; padding: 14px; color: var(--blue); font-weight: 800; text-align: center; }

.story-box { position: relative; overflow: hidden; background: linear-gradient(135deg,#eef9ff,#fff8e7); border-radius: 26px; padding: 22px; }
.story-box::after { content: ""; position: absolute; width: 180px; height: 180px; border-radius: 50%; background: rgba(52,183,230,.12); left: -60px; bottom: -80px; }
.story-title { margin: 0 0 9px; color: var(--purple); font-size: 1.8rem; }
.story-box p { margin: 0; line-height: 2; position: relative; z-index: 1; }
.lesson-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; margin-top: 22px; padding-top: 18px; border-top: 1px solid #e5eef5; }
.lesson-actions .side-actions { display: flex; gap: 10px; }

.empty-page { text-align: center; padding: 90px 18px; }
.empty-page h2 { color: var(--blue); font-size: 2.4rem; margin: 0 0 10px; }
.empty-page p { color: var(--muted); }
.badge-grid { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 16px; }
.badge-card { background: #fff; border-radius: 24px; padding: 20px; box-shadow: var(--shadow); text-align: center; }
.badge-card .badge-letter { font-family: "Baloo Bhaijaan 2", sans-serif; font-size: 4rem; color: var(--coral); }
.badge-card.locked { opacity: .45; }

.toast {
  position: fixed; z-index: 90; left: 50%; bottom: calc(98px + var(--safe-bottom)); transform: translateX(-50%) translateY(18px);
  width: min(520px, calc(100% - 28px)); padding: 13px 16px; border-radius: 16px; background: #18334b; color: #fff;
  text-align: center; font-weight: 700; opacity: 0; pointer-events: none; transition: .2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 980px) {
  .journey-map { grid-template-columns: repeat(2,minmax(0,1fr)); }
  .lesson-layout { grid-template-columns: minmax(0,1fr) 250px; gap: 14px; }
  .word-grid { grid-template-columns: repeat(3,minmax(0,1fr)); }
}
@media (max-width: 760px) {
  #app { padding-bottom: calc(82px + var(--safe-bottom)); }
  .hero-cta { grid-template-columns: 1fr; text-align: center; }
  .cta-btn { width: 100%; }
  .page-head, .lesson-head { align-items: flex-start; }
  .lesson-layout { grid-template-columns: 1fr; }
  .lesson-sidebar { position: static; order: -1; padding: 10px; overflow-x: auto; }
  .section-nav-title { display: none; }
  .section-nav { display: flex; min-width: max-content; gap: 7px; }
  .section-nav-btn { width: auto; min-width: 132px; grid-template-columns: 38px auto 18px; padding: 7px 8px; }
  .section-icon { width: 38px; height: 38px; border-radius: 12px; }
  .lesson-card { min-height: 520px; padding: 20px 16px; }
  .lesson-card-head { display: grid; }
  .section-badge { justify-self: start; }
  .trace-wrap { grid-template-columns: 1fr; }
  .trace-tools { grid-template-columns: repeat(2,minmax(0,1fr)); }
  .trace-tools .status-box { grid-column: 1/-1; }
  .word-grid { grid-template-columns: repeat(2,minmax(0,1fr)); }
  .sound-row { grid-template-columns: 1fr; }
  .form-tabs { grid-template-columns: repeat(2,minmax(0,1fr)); }

  .badge-grid { grid-template-columns: repeat(2,minmax(0,1fr)); }
}
@media (max-width: 520px) {
  .journey-map { grid-template-columns: 1fr; }
  .letter-stop { min-height: 150px; }
  .page { padding-inline: 12px; }
  .lesson-page { padding-inline: 10px; }
  .lesson-head { padding: 13px; }
  .stage-letter { width: 56px; height: 56px; font-size: 2.5rem; }
  .lesson-head p { display: none; }
  .progress-pill { display: none; }
  .word-grid { grid-template-columns: 1fr 1fr; }
  .badge-grid { grid-template-columns: 1fr 1fr; }
}

/* v4: writing form selector */
.trace-selected-label {
  width: fit-content;
  margin-inline: auto;
  padding: 8px 16px;
  border-radius: 999px;
  background: #eef9ef;
  color: #2d7d3b;
  font-weight: 800;
}
.write-form-tabs { margin-bottom: 2px; }
