.app {
  display: flex;
  min-height: 100%;
  max-width: 1400px;
  margin: 0 auto;
}

/* ---------- Shell: rail nav on tablet, bottom bar on phone ---------- */

.rail {
  position: sticky;
  top: 0;
  align-self: flex-start;
  height: 100vh;
  width: 104px;
  flex: 0 0 104px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 22px 0 16px;
  background: var(--cream);
  border-right: 1px solid var(--line);
  z-index: 20;
}
.rail-logo {
  width: 46px; height: 46px;
  border-radius: 14px;
  background: var(--blue);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 800; font-size: 21px;
  box-shadow: var(--shadow-sticker);
  margin-bottom: 14px;
}
.rail-btn {
  width: 76px;
  padding: 10px 0 8px;
  border-radius: var(--r-md);
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  font-size: 11px; font-weight: 700;
  color: var(--ink-faint);
  transition: background 0.15s ease, color 0.15s ease;
}
.rail-btn svg { width: 24px; height: 24px; }
.rail-btn:hover { color: var(--ink-soft); background: var(--cream-deep); }
.rail-btn.active { color: var(--blue); background: var(--blue-soft); }
.rail-spacer { flex: 1; }

.main { flex: 1 1 auto; min-width: 0; padding: 30px 38px 70px; }
.view { max-width: 1060px; }
.view-head { margin-bottom: 24px; }
.view-head h1 { margin-bottom: 6px; }

@media (max-width: 780px) {
  .app { flex-direction: column; }
  .rail {
    position: fixed; bottom: 0; top: auto; left: 0; right: 0;
    height: auto; width: auto; flex: none;
    flex-direction: row; justify-content: space-around;
    padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
    border-right: none; border-top: 1px solid var(--line);
  }
  .rail-logo, .rail-spacer { display: none; }
  .rail-btn { width: auto; flex: 1; font-size: 10px; }
  .main { padding: 18px 16px 100px; }
}

/* ---------- Stat + topic cards ---------- */

.grid { display: grid; gap: 16px; }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }

.stat { padding: 18px 20px; position: relative; overflow: hidden; }
.stat-label { font-size: 11px; font-weight: 800; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-faint); }
.stat-value { font-size: 36px; font-weight: 800; letter-spacing: -0.03em; margin-top: 4px; }
.stat-sub { font-size: 13px; color: var(--ink-soft); }

.topic { padding: 18px 20px; }
.topic + .topic { margin-top: 12px; }
.topic-head { display: flex; align-items: center; gap: 14px; cursor: pointer; }
.topic-index {
  width: 36px; height: 36px; flex: none;
  border-radius: 12px;
  background: var(--cream-deep);
  display: grid; place-items: center;
  font-weight: 800; font-size: 15px;
  color: var(--ink-soft);
}
.topic.is-open .topic-index { background: var(--blue); color: #fff; }
.topic-types { margin-top: 14px; padding-top: 6px; border-top: 1px solid var(--line); display: none; }
.topic.is-open .topic-types { display: block; }

.qtype { display: flex; align-items: center; gap: 12px; padding: 11px 8px; margin: 0 -6px; border-radius: var(--r-sm); }
.qtype + .qtype { border-top: 1px dashed var(--line); }
.qtype-name { font-weight: 600; font-size: 15px; }
.qtype.is-go { cursor: pointer; }
.qtype.is-go:hover { background: var(--blue-soft); }
.qtype-go { flex: none; font-size: 13px; font-weight: 800; color: var(--blue); }

/* Mastery meter: 5 discrete segments reading Unseen -> Retained. */
.meter { display: flex; gap: 3px; flex: none; }
.meter span { width: 18px; height: 8px; border-radius: 3px; background: #EBE6D6; }
.meter span.on { background: var(--blue); }
.meter span.on.full { background: var(--green); }

.bar { height: 9px; border-radius: var(--r-pill); background: #EBE6D6; overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: var(--r-pill); background: var(--blue); transition: width 0.4s ease; }
.bar.green > span { background: var(--green); }
.bar.amber > span { background: var(--amber); }

.status-progress {
  margin: -8px 0 20px;
  padding: 16px 18px 14px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sticker);
}
.status-progress.tone-amber { border-color: var(--amber); background: var(--amber-soft); }
.status-progress.tone-green { border-color: var(--green); background: var(--green-soft); }
.status-progress-copy {
  display: flex;
  align-items: center;
  gap: 14px;
}
.status-progress-count {
  width: 54px;
  height: 54px;
  flex: none;
  border-radius: 16px;
  background: var(--yellow);
  color: var(--ink);
  display: grid;
  place-items: center;
  font-size: 26px;
  font-weight: 800;
  box-shadow: var(--shadow-sticker);
}
.status-progress.tone-green .status-progress-count { background: var(--green); color: #fff; }
.status-progress.tone-amber .status-progress-count { background: var(--amber); color: #fff; }
.status-progress-title { font-size: 20px; font-weight: 800; letter-spacing: -0.02em; }
.status-progress-action { font-size: 14px; font-weight: 600; color: var(--ink-soft); margin-top: 3px; }
.status-steps {
  display: grid;
  gap: 8px;
  list-style: none;
  padding: 0;
  margin: 14px 0 0;
}
.status-step {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 46px;
  padding: 8px 10px;
  border-radius: 12px;
  background: var(--cream);
  border: 2px solid var(--line);
  font-size: 13px;
  font-weight: 700;
  color: var(--ink-faint);
}
.status-step.done {
  background: var(--green-soft);
  border-color: var(--green);
  color: #176B45;
}
.status-step.now {
  background: var(--yellow-soft);
  border-color: var(--yellow);
  color: var(--ink);
}
.status-step-mark {
  width: 24px;
  height: 24px;
  flex: none;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #fff;
  font-size: 12px;
  font-weight: 800;
}
.status-step.now .status-step-mark { background: var(--yellow); }
.status-step.done .status-step-mark { background: var(--green); color: #fff; }
.status-step-label { line-height: 1.2; }

/* ---------- Practice ---------- */

.practice { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
@media (max-width: 1080px) { .practice { grid-template-columns: 1fr; } }

.question { padding: 20px 24px; position: relative; }
.question-label { font-size: 12px; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; color: var(--blue); }
.question-body { font-size: 21px; margin-top: 8px; }

.ink-wrap { margin-top: 16px; position: relative; }
.ink-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  padding: 10px 12px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-bottom: none;
  border-radius: var(--r-md) var(--r-md) 0 0;
}
.ink-tool {
  min-height: 40px;
  padding: 6px 11px;
  border-radius: var(--r-sm);
  border: 2px solid transparent;
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; font-weight: 700;
  color: var(--ink-soft);
}
.ink-tool svg { width: 18px; height: 18px; flex: none; }
.ink-tool.active {
  background: var(--blue-soft);
  color: var(--blue);
  border-color: var(--blue);
}
.ink-hint { margin-left: auto; font-size: 12px; color: var(--ink-faint); }

.ink-canvas {
  display: block;
  width: 100%;
  height: 340px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 0 0 var(--r-md) var(--r-md);
  /* Critical for stylus: stop Safari stealing the gesture for scroll/zoom. */
  touch-action: none;
  cursor: crosshair;
}
.ink-canvas.is-circle { cursor: crosshair; }
.ink-canvas.is-drag { cursor: grab; }
.ink-canvas.is-dragging { cursor: grabbing; }
.ink-empty {
  position: absolute;
  left: 50%; top: 58%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--ink-faint);
  font-size: 15px;
  pointer-events: none;
}
.ink-empty.hidden { display: none; }

.working-box { margin-top: 16px; padding: 16px 18px; }
.work-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.work-n {
  width: 26px; height: 26px; flex: none;
  border-radius: 8px; background: var(--cream-deep);
  display: grid; place-items: center;
  font-size: 12px; font-weight: 800; color: var(--ink-soft);
}
.work-input { font-size: 18px; padding: 10px 12px; flex: 1; min-width: 0; }
.work-use {
  flex: none;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: var(--yellow-soft, #FFF4CC);
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
}
.work-drop {
  width: 32px; height: 32px; flex: none;
  border-radius: 8px; color: var(--ink-faint); font-size: 18px;
}
.work-row.work-ok .work-input { border-color: var(--green); background: var(--green-soft); }
.work-row.work-no .work-input { border-color: var(--amber); background: var(--amber-soft); }
.work-row.work-ok .work-n { background: var(--green-soft); color: #176B45; }
.work-row.work-no .work-n { background: var(--amber-soft); color: #8E5212; }
.work-note { font-size: 12px; font-weight: 600; margin: 4px 0 0 34px; }
.work-note.ok { color: #176B45; }
.work-note.no { color: #8E5212; }
.working-script {
  margin-top: 10px; padding-left: 0; list-style: none;
}
.working-script li { display: flex; gap: 8px; margin-bottom: 6px; font-size: 14px; }
.working-script .brk { color: #8E5212; font-weight: 700; }

.answer-box { margin-top: 16px; padding: 18px 20px; }
.answer-input {
  width: 100%;
  padding: 14px 16px;
  border: 2px solid var(--line);
  border-radius: var(--r-md);
  background: var(--cream);
  font-family: var(--math);
  font-size: 21px;
  color: var(--ink);
  outline: none;
}
.answer-crop {
  display: block;
  max-width: 100%;
  max-height: 120px;
  margin: 0 0 12px;
  padding: 8px;
  background: #fff;
  border: 2px solid var(--yellow);
  border-radius: var(--r-md);
  object-fit: contain;
}
.answer-input:focus { border-color: var(--blue); background: var(--paper); }
.answer-input.ok { border-color: var(--green); background: var(--green-soft); }
.answer-input.no { border-color: var(--amber); background: var(--amber-soft); }

.keypad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 6px; margin-top: 12px; }
.keypad button {
  padding: 11px 0;
  border-radius: var(--r-sm);
  background: var(--cream);
  border: 1px solid var(--line);
  font-family: var(--math);
  font-size: 17px;
  font-weight: 600;
}
.keypad button:hover { background: var(--yellow-soft); border-color: var(--yellow); }
.keypad button.wide { grid-column: span 2; font-family: var(--ui); font-size: 13px; font-weight: 700; }
@media (max-width: 700px) { .keypad { grid-template-columns: repeat(6, 1fr); } }

.side { display: flex; flex-direction: column; gap: 14px; }

.tutor-bubble {
  padding: 15px 17px;
  font-size: 15px;
  position: relative;
}
.tutor-bubble + .tutor-bubble { margin-top: 10px; }
.tutor-who { display: flex; align-items: center; gap: 9px; margin-bottom: 8px; }
.avatar {
  width: 34px; height: 34px; flex: none;
  border-radius: var(--r-pill);
  background: var(--blue-soft);
  border: 2px solid var(--yellow);
  display: grid; place-items: center;
  font-size: 15px; font-weight: 800; color: var(--blue);
}
.tutor-name { font-size: 13px; font-weight: 800; }

.verdict { padding: 15px 18px; border-radius: var(--r-md); font-weight: 700; display: flex; gap: 10px; align-items: flex-start; }
.verdict-ok { background: var(--green-soft); border: 2px solid var(--green); color: #176B45; }
.verdict-no { background: var(--amber-soft); border: 2px solid var(--amber); color: #8E5212; }
.verdict-icon { font-size: 19px; line-height: 1.2; }

.solution { margin-top: 12px; padding: 16px 18px; background: var(--cream); border: 1px dashed var(--ink-faint); border-radius: var(--r-md); }
.solution ol { margin: 8px 0 0; padding-left: 20px; }
.solution li { margin-bottom: 7px; }

/* ---------- Rewards ---------- */

.star-count { font-size: 56px; font-weight: 800; letter-spacing: -0.04em; line-height: 1; }
.streak-week { display: flex; gap: 8px; margin-top: 14px; }
.streak-day { flex: 1; text-align: center; }
.streak-dot {
  height: 38px;
  border-radius: var(--r-md);
  background: #EFECE0;
  display: grid; place-items: center;
  font-size: 17px;
  margin-bottom: 5px;
}
.streak-dot.done { background: var(--yellow); }
.streak-dot.today { outline: 2px solid var(--blue); outline-offset: 2px; }

.reward-item { padding: 16px 18px; display: flex; align-items: center; gap: 14px; }
.reward-icon { width: 46px; height: 46px; flex: none; border-radius: 14px; background: var(--yellow-soft); display: grid; place-items: center; font-size: 23px; }

/* ---------- Tutor dashboard ---------- */

.focus-item { padding: 16px 18px; }
.focus-item + .focus-item { margin-top: 12px; }
.focus-rank {
  width: 30px; height: 30px; flex: none;
  border-radius: 10px;
  background: var(--amber-soft); color: #B36A17;
  display: grid; place-items: center;
  font-weight: 800; font-size: 14px;
}
.error-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.snapshot {
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--paper);
  overflow: hidden;
}
.snapshot img { display: block; width: 100%; max-height: 190px; object-fit: contain; object-position: left top; background: #fff; }
.snapshot-cap { padding: 7px 12px; font-size: 12px; color: var(--ink-faint); border-top: 1px solid var(--line); }

/* ---------- Auth ---------- */

.app-auth {
  max-width: none;
  min-height: 100%;
  min-height: 100dvh;
  align-items: center;
  justify-content: center;
}
.app-auth .main {
  width: 100%;
  min-height: 100dvh;
  padding: 32px 20px;
  display: flex;
  align-items: safe center;
  justify-content: safe center;
}
.app-auth .view { max-width: none; width: 100%; }

.auth { width: 100%; max-width: 440px; margin: 0 auto; }
.auth-brand { text-align: center; margin-bottom: 22px; }
.auth-brand .rail-logo { margin: 0 auto 12px; }
.auth-brand h1 { font-size: 28px; margin-top: 4px; }
.auth-card { padding: 28px 26px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; font-weight: 700; color: var(--ink-soft); }
.field .answer-input { margin: 0; font-family: var(--ui); font-size: 16px; }
.password-wrap { position: relative; }
.password-wrap .answer-input { padding-right: 72px; }
.password-toggle {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  font-size: 13px; font-weight: 700; color: var(--blue); padding: 6px 8px;
}
.form-error { color: #B36A17; font-size: 14px; font-weight: 700; margin: 0 0 10px; }
.auth-links { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; text-align: center; }
.auth-links button { font-size: 14px; font-weight: 700; color: var(--blue); }
.level-row { display: flex; flex-wrap: wrap; gap: 8px; }
.choice-chip {
  padding: 9px 12px; border-radius: var(--r-pill);
  border: 2px solid var(--line); background: var(--paper);
  font-size: 13px; font-weight: 700;
}
.choice-chip.sel { border-color: var(--blue); background: var(--blue-soft); color: var(--blue); }

/* ---------- Onboarding ---------- */

.onb { max-width: 760px; margin: 0 auto; }
.onb-steps { display: flex; gap: 6px; margin-bottom: 26px; }
.onb-steps i { height: 6px; flex: 1; border-radius: var(--r-pill); background: #EBE6D6; }
.onb-steps i.on { background: var(--blue); }
.hero { position: relative; padding: 34px 36px; overflow: hidden; }
.choice { padding: 15px 18px; text-align: left; width: 100%; border-radius: var(--r-md); border: 2px solid var(--line); background: var(--paper); font-weight: 600; transition: border-color 0.12s ease, background 0.12s ease; }
.choice + .choice { margin-top: 10px; }
.choice:hover { border-color: var(--blue); background: var(--blue-soft); }
.choice.sel { border-color: var(--blue); background: var(--blue-soft); }
.choice.is-wrong { border-color: var(--amber); background: var(--amber-soft); }
.choice[disabled] { cursor: default; }
.teach-methods { display: grid; gap: 16px; margin-top: 16px; align-items: stretch; }
.teach-methods.is-pair { grid-template-columns: 1fr 1fr; }
.teach-method { padding: 20px; min-width: 0; }
.teach-method .teach-step:last-child { margin-bottom: 0; }
.teach-step { display: flex; gap: 12px; margin-bottom: 14px; }
.teach-n {
  width: 28px; height: 28px; flex: none;
  border-radius: 8px; background: var(--blue-soft); color: var(--blue);
  display: grid; place-items: center;
  font-size: 13px; font-weight: 800;
}
@media (max-width: 780px) {
  .teach-methods.is-pair { grid-template-columns: 1fr; }
}

/* ---------- Snap & ask ---------- */

.drop {
  padding: 42px 28px;
  text-align: center;
  border: 2px dashed var(--ink-faint);
  border-radius: var(--r-lg);
  background: var(--paper);
}
.drop.over { border-color: var(--blue); background: var(--blue-soft); }
.snap-preview { max-width: 100%; max-height: 300px; border-radius: var(--r-md); border: 1px solid var(--line); display: block; margin: 0 auto; }

.hint-step { padding: 14px 16px; border-radius: var(--r-md); background: var(--yellow-soft); border: 2px solid var(--yellow); }
.hint-step + .hint-step { margin-top: 10px; }
.hint-tier { font-size: 11px; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; color: #92670A; margin-bottom: 4px; }

.empty-state { padding: 46px 30px; text-align: center; color: var(--ink-soft); }
.toast {
  position: fixed; left: 50%; bottom: 30px; transform: translateX(-50%);
  background: var(--ink); color: #fff;
  padding: 13px 22px; border-radius: var(--r-pill);
  font-weight: 700; font-size: 15px;
  box-shadow: var(--shadow-float);
  z-index: 80;
  animation: toast-in 0.22s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 14px); } }
