:root {
  --cream: #f4efe4;
  --paper: #fffdf7;
  --ink: #2c2620;
  --muted: #6f655c;
  --line: #ded3c3;
  --accent: #b5502e;
  --accent-dark: #80351f;
  --sage: #536b51;
  --green: #376548;
  --red: #9c382a;
  --shadow: 0 8px 24px rgba(61, 43, 31, .08);
  --radius: 18px;
}

* { box-sizing: border-box; }
html { background: var(--cream); color: var(--ink); scroll-behavior: smooth; }
body {
  margin: 0;
  min-width: 300px;
  background: var(--cream);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}
button, input { font: inherit; }
button { color: inherit; }
button:focus-visible, summary:focus-visible, input:focus-visible { outline: 3px solid rgba(181, 80, 46, .35); outline-offset: 3px; }
button:disabled { cursor: not-allowed; opacity: .55; }
#app {
  width: min(100%, 700px);
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px 16px calc(104px + env(safe-area-inset-bottom));
}
h1, h2, h3, p { margin-top: 0; }
h1, h2, h3, .question-card .option, .lesson, dd { font-family: Georgia, "Times New Roman", serif; }
h1 { margin-bottom: 12px; font-size: clamp(2rem, 9vw, 3.35rem); line-height: 1.02; letter-spacing: -.035em; }
h2 { margin-bottom: 8px; font-size: 1.42rem; line-height: 1.16; }
h3 { margin-bottom: 6px; font-size: 1.12rem; }
p { color: var(--muted); }
.eyebrow { margin-bottom: 8px; color: var(--accent-dark); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-size: .72rem; font-weight: 800; letter-spacing: .13em; }

.bottom-nav {
  position: fixed;
  z-index: 50;
  right: 0;
  bottom: 0;
  left: 0;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  width: min(100%, 700px);
  margin: 0 auto;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(255, 253, 247, .97);
  box-shadow: 0 -8px 24px rgba(61, 43, 31, .07);
  backdrop-filter: blur(14px);
}
.bottom-nav button {
  position: relative;
  display: grid;
  place-items: center;
  gap: 1px;
  min-height: 52px;
  border: 0;
  border-radius: 13px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.bottom-nav button > span { min-width: 28px; font-family: Georgia, serif; font-size: 1.15rem; font-weight: 700; }
.bottom-nav button small { font-size: .68rem; font-weight: 700; }
.bottom-nav button.active { background: #efe0d4; color: var(--accent-dark); }
.bottom-nav button b { position: absolute; top: 3px; right: 12%; min-width: 19px; padding: 2px 5px; border-radius: 20px; background: var(--accent); color: white; font-size: .62rem; }

.hero {
  position: relative;
  overflow: hidden;
  margin: -18px -16px 18px;
  padding: 38px 22px 28px;
  background: var(--ink);
  color: var(--paper);
}
.hero::after { content: "Q"; position: absolute; right: -18px; bottom: -78px; color: rgba(255,255,255,.045); font-family: Georgia, serif; font-size: 16rem; line-height: 1; }
.hero .eyebrow { color: #e5a68e; }
.hero h1 { position: relative; z-index: 1; max-width: 500px; color: white; }
.hero p { position: relative; z-index: 1; max-width: 470px; color: #d9cfc4; }
.hero-stats { position: relative; z-index: 1; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 24px 0 14px; }
.hero-stats div { display: grid; padding-right: 8px; border-right: 1px solid #544a42; }
.hero-stats div:last-child { border-right: 0; }
.hero-stats strong { color: white; font-family: Georgia, serif; font-size: 1.45rem; }
.hero-stats span { color: #bdb2a8; font-size: .72rem; text-transform: uppercase; }
.hero .bar { background: #50463e; }
.hero .bar span { background: #d87854; }

.card, .start-card, .backup-strip, .topic-card, .glossary-chapter, .history, .source-note {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow);
}
.card { padding: 20px; }
.start-card { display: grid; gap: 16px; padding: 20px; }
.start-card p { margin-bottom: 0; }
.primary, .secondary, .grade-row button, .text-button {
  min-height: 48px;
  border-radius: 13px;
  font-weight: 800;
  cursor: pointer;
}
.primary { width: 100%; border: 1px solid var(--accent); background: var(--accent); color: white; box-shadow: 0 5px 12px rgba(181,80,46,.18); }
.primary:not(:disabled):active { transform: translateY(1px); background: var(--accent-dark); }
.secondary { padding: 10px 14px; border: 1px solid #c5aa97; background: #fff9f1; color: var(--accent-dark); }
.text-button { min-height: auto; padding: 5px; border: 0; background: transparent; color: var(--accent-dark); }
.icon-button { display: grid; width: 40px; height: 40px; place-items: center; border: 1px solid var(--line); border-radius: 50%; background: var(--paper); font-size: 1.4rem; cursor: pointer; }
.bar { overflow: hidden; width: 100%; height: 7px; border-radius: 20px; background: #e6ded2; }
.bar span { display: block; height: 100%; border-radius: inherit; background: var(--accent); transition: width .3s ease; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 28px 2px 12px; }
.section-head h2 { margin: 0; }
.section-head span { color: var(--muted); font-size: .8rem; }

.chapter-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.mini-card { display: grid; gap: 7px; min-height: 134px; padding: 14px; border: 1px solid var(--line); border-radius: 16px; background: var(--paper); text-align: left; box-shadow: var(--shadow); cursor: pointer; }
.mini-card > span { font-size: 1.4rem; }
.mini-card strong { align-self: end; font-family: Georgia, serif; font-size: .93rem; line-height: 1.15; }
.backup-strip { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 18px; padding: 16px; }
.backup-strip p { margin: 2px 0 0; font-size: .83rem; }

.page-head { padding: 18px 4px 16px; }
.page-head > p:last-child { max-width: 570px; }
.topic-list, .glossary-list { display: grid; gap: 14px; }
.topic-card, .glossary-chapter { overflow: hidden; }
.topic-card > summary, .glossary-chapter > summary { display: grid; align-items: center; gap: 12px; padding: 18px; list-style: none; cursor: pointer; }
.topic-card > summary::-webkit-details-marker, .glossary-chapter > summary::-webkit-details-marker, .concept-card details > summary::-webkit-details-marker { display: none; }
.topic-card > summary { grid-template-columns: auto 1fr auto; }
.topic-card > summary::after, .glossary-chapter > summary::after { content: "+"; color: var(--accent); font-size: 1.4rem; }
.topic-card[open] > summary::after, .glossary-chapter[open] > summary::after { content: "−"; }
.topic-card > summary > b { grid-column: 3; grid-row: 1; font-size: .8rem; }
.topic-card > summary::after { grid-column: 3; grid-row: 2; }
.topic-card summary h2 { margin-bottom: 5px; font-size: 1.16rem; }
.topic-card summary p { margin-bottom: 10px; font-size: .83rem; line-height: 1.35; }
.topic-icon { display: grid; width: 42px; height: 42px; place-items: center; border-radius: 13px; background: #f1e6d8; font-size: 1.35rem; }
.concept-list { display: grid; gap: 10px; padding: 0 10px 10px; border-top: 1px solid var(--line); background: #f7f1e8; }
.concept-card { padding: 16px; border: 1px solid var(--line); border-radius: 14px; background: var(--paper); }
.concept-card:first-child { margin-top: 10px; }
.concept-title { display: flex; justify-content: space-between; gap: 10px; }
.concept-title span { color: var(--accent-dark); font-size: .7rem; font-weight: 800; }
.concept-title h3 { margin: 2px 0 10px; }
.concept-title > b { color: var(--muted); font-size: .78rem; }
.concept-card details > summary { margin: 14px 0 0; color: var(--accent-dark); font-weight: 800; cursor: pointer; }
.lesson { padding-top: 10px; font-size: 1.02rem; line-height: 1.63; }
.lesson p { color: var(--ink); }
.lesson p:last-child { margin-bottom: 4px; }
.drill-button { width: 100%; margin-top: 14px; }

.term-chips { display: flex; flex-wrap: wrap; gap: 7px; margin: 14px 0; }
.chip { min-height: 34px; padding: 6px 10px; border: 1px solid #cda994; border-radius: 999px; background: #fff8ef; color: var(--accent-dark); font-size: .76rem; font-weight: 800; cursor: pointer; }
.chip.saved { border-color: #9aae98; background: #edf3ea; color: var(--green); }

.study-top { display: grid; grid-template-columns: 42px 1fr auto; align-items: center; gap: 10px; margin: -4px 0 16px; }
.study-top > div { display: grid; gap: 6px; color: var(--muted); font-size: .72rem; font-weight: 800; text-transform: uppercase; }
.study-top > b { color: var(--muted); font-size: .8rem; }
.question-card { padding: 22px 18px; border: 1px solid var(--line); border-radius: 22px; background: var(--paper); box-shadow: var(--shadow); }
.question-card h1 { margin: 20px 0; font-size: clamp(1.6rem, 7vw, 2.45rem); line-height: 1.16; }
.question-meta { display: flex; align-items: center; justify-content: space-between; gap: 10px; color: var(--muted); font-size: .74rem; font-weight: 700; }
.question-meta b { padding: 4px 8px; border-radius: 20px; background: #ede4d6; color: var(--accent-dark); text-transform: uppercase; }
.ke-tag { display: inline-block; margin: 12px 0 0; padding: 4px 8px; border-radius: 6px; background: #f0e7da; color: var(--accent-dark); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-size: .7rem; font-weight: 800; }
.options { display: grid; gap: 10px; counter-reset: option; }
.option { counter-increment: option; display: grid; grid-template-columns: 32px 1fr; align-items: center; gap: 10px; width: 100%; min-height: 62px; padding: 11px 12px; border: 1px solid #d7cbbb; border-radius: 14px; background: #fffaf3; color: var(--ink); font-size: 1rem; line-height: 1.3; text-align: left; cursor: pointer; }
.option-letter::before { content: counter(option, upper-alpha); display: grid; width: 30px; height: 30px; place-items: center; border-radius: 50%; background: #ede3d5; color: var(--accent-dark); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-size: .72rem; font-weight: 900; }
.option.selected { border: 2px solid var(--accent); background: #f8e8de; }
.option.correct { border: 2px solid var(--green); background: #e9f2e9; }
.option.correct .option-letter::before { background: var(--green); color: white; }
.option.wrong { border: 2px solid var(--red); background: #f7e4e0; }
.option.wrong .option-letter::before { background: var(--red); color: white; }
.feedback-stack { display: grid; gap: 8px; margin-top: 14px; }
.feedback { padding: 12px; border-left: 4px solid #a58f7e; border-radius: 6px 12px 12px 6px; background: #f4eee5; }
.feedback.is-correct { border-color: var(--green); background: #eaf2e8; }
.feedback strong { color: var(--ink); font-size: .78rem; text-transform: uppercase; }
.feedback p { margin: 3px 0 0; color: var(--ink); font-family: Georgia, serif; }
.next-button { margin-top: 16px; }
.model-answer { margin: 16px 0; padding: 16px; border-left: 4px solid var(--accent); border-radius: 4px 12px 12px 4px; background: #f4e6dc; font-family: Georgia, serif; font-size: 1.04rem; }
.model-answer p { margin: 0; color: var(--ink); }
.grade-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.grade-row button { min-height: 48px; padding: 6px; border: 1px solid var(--line); background: var(--paper); font-size: .78rem; }
.grade-row button:first-child { border-color: #d7aaa2; color: var(--red); }
.grade-row button:last-child { border-color: #9aae98; color: var(--green); }
.term-card { text-align: center; }
.term-card h1 { margin-top: 30px; font-size: 2.5rem; }
.prompt-hint { margin-bottom: 30px; }
.empty-state { display: grid; place-items: center; min-height: 70vh; text-align: center; }
.empty-state > span { display: grid; width: 74px; height: 74px; place-items: center; border-radius: 50%; background: var(--sage); color: white; font-size: 2rem; }

.glossary-chapter > summary { grid-template-columns: auto 1fr auto auto; }
.glossary-chapter > summary b { color: var(--muted); font-size: .75rem; }
.add-all { margin: 0 16px 12px; }
dl { margin: 0; }
dl > div { padding: 14px 16px; border-top: 1px solid var(--line); }
dt { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-weight: 800; }
dd { margin: 7px 0 0; color: var(--muted); line-height: 1.5; }

.mock-head { padding-bottom: 10px; }
.exam-ticket { display: grid; grid-template-columns: repeat(3, 1fr); margin-bottom: 14px; overflow: hidden; border-radius: var(--radius); background: var(--ink); color: white; }
.exam-ticket div { display: grid; place-items: center; padding: 18px 5px; border-right: 1px solid #50473f; }
.exam-ticket div:last-child { border: 0; }
.exam-ticket span { color: #cfc4b9; font-size: .7rem; text-transform: uppercase; }
.exam-ticket strong { font-family: Georgia, serif; font-size: 1.5rem; }
.check-list { margin: 16px 0 22px; padding: 0; list-style: none; }
.check-list li { padding: 9px 0 9px 27px; border-bottom: 1px solid var(--line); }
.check-list li::before { content: "✓"; float: left; margin-left: -27px; color: var(--green); font-weight: 900; }
.history { margin-top: 18px; padding: 18px; }
.history > div { display: grid; grid-template-columns: 1fr auto auto; gap: 14px; padding: 10px 0; border-top: 1px solid var(--line); }
.history b { font-size: .72rem; }
.pass-text { color: var(--green); }
.fail-text { color: var(--red); }
.result-hero { margin: -18px -16px 16px; padding: 36px 22px; color: white; }
.result-hero.pass { background: var(--green); }
.result-hero.fail { background: var(--red); }
.result-hero .eyebrow, .result-hero p { color: white; }
.breakdown { display: grid; gap: 15px; }
.breakdown > div { display: grid; grid-template-columns: 1fr auto; gap: 6px; }
.breakdown .bar { grid-column: 1 / -1; }
.missed-card { margin-bottom: 10px; border: 1px solid var(--line); border-radius: 14px; background: var(--paper); }
.missed-card summary { padding: 15px; font-family: Georgia, serif; font-weight: 700; cursor: pointer; }
.missed-card > div { padding: 0 15px 15px; }
.missed-card p { margin-bottom: 8px; color: var(--ink); }
.why-wrong { padding-left: 10px; border-left: 3px solid var(--red); }
.why-right { padding-left: 10px; border-left: 3px solid var(--green); }

.progress-overview { display: grid; gap: 9px; margin-bottom: 16px; }
.progress-overview .card { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px; padding: 14px; }
.progress-overview .card > div { display: grid; gap: 7px; }
.weak-list { display: grid; }
.weak-list button { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 11px; padding: 11px 0; border: 0; border-top: 1px solid var(--line); background: transparent; text-align: left; cursor: pointer; }
.weak-list button div { display: grid; }
.weak-list small { color: var(--muted); }
.backup-card, .source-note { margin-top: 16px; }
.button-row { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.file-button { display: grid; place-items: center; cursor: pointer; }
.file-button input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.status-line { margin: 10px 0 0; color: var(--green); font-weight: 700; }
.source-note { padding: 18px; }
.source-note p { margin: 4px 0 0; font-size: .84rem; }
.source-note a { color: var(--accent-dark); font-weight: 750; text-underline-offset: 2px; }

@media (min-width: 560px) {
  #app { padding-right: 22px; padding-left: 22px; }
  .hero, .result-hero { margin-right: -22px; margin-left: -22px; padding-right: 30px; padding-left: 30px; border-radius: 0 0 22px 22px; }
  .start-card { grid-template-columns: 1fr auto; align-items: center; }
  .start-card .primary { width: auto; padding: 0 22px; }
  .chapter-grid { grid-template-columns: repeat(3, 1fr); }
  .question-card { padding: 30px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
