/* 筆順小先生 — app.css */
@font-face {
  font-family: 'NotoSerifHK';
  src: url('../fonts/noto-serif-hk-bold.woff2') format('woff2');
  font-weight: 700;
  font-display: swap;
}

:root {
  --bg: #fdf6ec;
  --card: #ffffff;
  --ink: #3d3327;
  --ink-soft: #8a7a66;
  --primary: #ff7a3d;
  --primary-dark: #e85d1f;
  --teal: #1fa28a;
  --teal-dark: #157f6c;
  --blue: #3b82f6;
  --star: #ffc93c;
  --grid: #e8b84b;
  --grid-soft: #f3d9a4;
  --red: #e5484d;
  --green: #2fb344;
  --radius: 20px;
  --shadow: 0 4px 16px rgba(120, 84, 30, 0.10);
  --shadow-lg: 0 8px 28px rgba(120, 84, 30, 0.16);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang TC', 'Hiragino Sans TC',
    'Microsoft JhengHei', 'Noto Sans CJK TC', 'Noto Sans TC', sans-serif;
  font-size: 17px;
  line-height: 1.5;
  min-height: 100vh;
}

.page {
  max-width: 1080px;
  margin: 0 auto;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
}

/* ---------- header ---------- */
header.app {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0 14px;
}
header.app .logo {
  width: 44px; height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, #ff9a5c, #ff6b2b);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-family: 'NotoSerifHK', serif;
  font-size: 28px; font-weight: 700;
  box-shadow: var(--shadow);
}
header.app .title { font-size: 20px; font-weight: 700; }
header.app .subtitle { font-size: 12.5px; color: var(--ink-soft); margin-top: -2px; }
header.app .spacer { flex: 1; }
header.app nav a {
  text-decoration: none;
  color: var(--ink);
  background: var(--card);
  border: 1.5px solid #f0e2cf;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 14.5px;
  box-shadow: var(--shadow);
}

/* ---------- cards ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}

/* ---------- search ---------- */
.search-row { display: flex; gap: 10px; }
.search-row input {
  flex: 1;
  font-size: 30px;
  font-family: 'NotoSerifHK', serif;
  text-align: center;
  padding: 10px;
  border: 2.5px solid var(--grid-soft);
  border-radius: 16px;
  background: #fffdf7;
  color: var(--ink);
  outline: none;
  min-width: 0;
}
.search-row input:focus { border-color: var(--primary); }
.search-row input::placeholder { font-size: 19px; color: #c9b797; font-family: inherit; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 16px;
  font-size: 17px;
  font-weight: 700;
  padding: 12px 18px;
  cursor: pointer;
  transition: transform 0.06s ease, filter 0.15s ease;
  color: #fff;
  font-family: inherit;
  user-select: none;
  -webkit-user-select: none;
}
.btn:active { transform: scale(0.96); }
.btn-primary { background: var(--primary); box-shadow: 0 4px 0 var(--primary-dark); }
.btn-teal { background: var(--teal); box-shadow: 0 4px 0 var(--teal-dark); }
.btn-soft { background: #fff2e4; color: var(--primary-dark); }
.btn-ghost { background: #f4ead9; color: var(--ink); }
.btn:disabled { opacity: 0.5; pointer-events: none; }
.btn .ico { font-size: 20px; line-height: 1; }

.btn-big {
  flex: 1;
  flex-direction: column;
  padding: 14px 8px;
  font-size: 18px;
  border-radius: 18px;
}
.btn-big .ico { font-size: 30px; }

/* ---------- grade pills ---------- */
.grade-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.grade-row::-webkit-scrollbar { display: none; }
.pill {
  border: 2px solid #f0e2cf;
  background: var(--card);
  border-radius: 999px;
  padding: 9px 16px;
  font-size: 15.5px;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.pill.active {
  background: var(--teal);
  border-color: var(--teal);
  color: #fff;
  box-shadow: 0 3px 0 var(--teal-dark);
}
.pill .cnt { font-size: 12px; opacity: 0.75; margin-left: 4px; }

/* ---------- char grid ---------- */
.char-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}
@media (min-width: 640px) { .char-grid { grid-template-columns: repeat(8, 1fr); } }
@media (min-width: 960px) { .char-grid { grid-template-columns: repeat(10, 1fr); } }

.char-cell {
  position: relative;
  background: #fffdf7;
  border: 1.5px solid #f3e6d0;
  border-radius: 14px;
  padding: 8px 2px 6px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.08s ease;
  text-decoration: none;
  color: inherit;
}
.char-cell:active { transform: scale(0.94); }
.char-cell .ch {
  font-family: 'NotoSerifHK', serif;
  font-size: 30px;
  font-weight: 700;
  line-height: 1.25;
}
.char-cell .jp { font-size: 11.5px; color: var(--ink-soft); min-height: 15px; }
.char-cell .star-badge {
  position: absolute;
  top: -6px; right: -6px;
  background: var(--star);
  color: #7a5b00;
  font-size: 12px;
  font-weight: 800;
  border-radius: 999px;
  padding: 1px 6px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.15);
}

.progress-wrap { margin-top: 4px; }
.progress-label {
  display: flex; justify-content: space-between;
  font-size: 13.5px; color: var(--ink-soft); margin-bottom: 4px;
}
.progress-bar {
  height: 12px;
  background: #f3e6d0;
  border-radius: 999px;
  overflow: hidden;
}
.progress-bar > div {
  height: 100%;
  background: linear-gradient(90deg, #ffd166, #ffb703);
  border-radius: 999px;
  transition: width 0.4s ease;
}

.recent-strip {
  display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none;
}
.recent-strip::-webkit-scrollbar { display: none; }
.recent-strip a {
  flex-shrink: 0;
  width: 52px;
  background: #fffdf7;
  border: 1.5px solid #f3e6d0;
  border-radius: 14px;
  text-align: center;
  padding: 8px 0 4px;
  text-decoration: none;
  color: inherit;
}
.recent-strip a .ch { font-family: 'NotoSerifHK', serif; font-size: 26px; font-weight: 700; display: block; }
.recent-strip a .jp { font-size: 10.5px; color: var(--ink-soft); }

/* ---------- character page ---------- */
.writer-wrap {
  position: relative;
  width: min(78vw, 420px);
  aspect-ratio: 1;
  margin: 0 auto;
  border-radius: 22px;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  background: #fffdf7;
}
@media (min-width: 880px) { .writer-wrap { width: min(44vw, 460px); } }
.writer-wrap .grid-svg {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none;  /* decorative — quiz taps must reach the writer SVG */
}
.writer-wrap #writer {
  position: absolute; inset: 0;
  touch-action: none;    /* let hanzi-writer own the touch gestures on iOS */
}
/* calligraphy ink layer (js/brush.js): paints the child's quiz strokes
   with variable brush width + Apple Pencil pressure; purely visual, the
   stroke judging stays in hanzi-writer */
.writer-wrap canvas.brush-ink {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 5;
}
/* while a quiz is running, everything the COMPUTER draws (completed target
   strokes, the finished character) stays in a light sand tone — the focus is
   the child's own blue brush ink on the board */
#writer.quiz-live svg path[stroke='rgba(61,51,39,1)'],
#practiceWrite.quiz-live svg path[stroke='rgba(61,51,39,1)'] {
  stroke: #d8c49a !important;
}

/* next-stroke preview (light sand calligraphic shape, revealed with a
   writing animation; see renderPreview in char.js) */
.preview-svg {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 3;   /* above the grid + library strokes, below the brush ink */
}
.preview-svg.pv-out { opacity: 0; transition: opacity 0.4s ease; }

/* ---------- practice mode 對着寫 ---------- */
.btn-gold {
  background: #e8b84b;
  color: #fff;
  box-shadow: 0 4px 0 #cf9c2e;
}
#btnPractice { grid-column: 1 / -1; }   /* full-width row under the 2×2 grid */

body.practice-mode .char-layout,
body.practice-mode footer.foot,
body.practice-mode header.app { display: none !important; }

.practice {
  position: fixed; inset: 0; z-index: 50;
  background: var(--paper, #fdf6ec);
  display: flex; flex-direction: column;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  overflow: hidden;
}
.practice-top { display: flex; align-items: center; gap: 8px; }
.practice-top .practice-title {
  flex: 1; display: flex; align-items: baseline; justify-content: center;
  gap: 8px; font-weight: 700; font-size: 16px;
}
.practice-title .pc-char { font-size: 24px; }
.practice-title .pc-mode { color: #a08c6d; font-size: 14px; font-weight: 600; }
.practice-sound { display: flex; gap: 6px; }
.practice-sound .btn { padding: 9px 12px; font-size: 15px; }
.practice button { touch-action: manipulation; }

/* right / left hand toggle: swaps which side the model sits on
   (right-handed default: model LEFT, child writes RIGHT) */
.practice-hand { display: flex; background: #fff2e4; border-radius: 999px; padding: 3px; }
.hand-btn {
  border: 0; background: transparent; cursor: pointer;
  padding: 6px 12px; font-size: 13px; font-weight: 600;
  color: #b39b7d; border-radius: 999px;
}
.hand-btn.active { background: #e8b84b; color: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.practice.lefty .practice-col:first-child { order: 2; }
.practice.lefty .practice-col:last-child { order: 1; }

/* per-stroke feedback badge on the writing board */
.pf-badge {
  position: absolute; left: 50%; top: 42%;
  transform: translate(-50%, -50%);
  font-size: 84px; font-weight: 900;
  opacity: 0; pointer-events: none; z-index: 6;
  text-shadow: 0 2px 10px rgba(0,0,0,0.12);
}
.pf-badge.show { animation: pf-pop 0.9s ease forwards; }
.pf-badge.ok { color: #2e9e5b; }
.pf-badge.bad { color: #e05252; }
.pf-badge.win { color: #e8b84b; font-size: 60px; }
@keyframes pf-pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
  18% { opacity: 1; transform: translate(-50%, -50%) scale(1.18); }
  40% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1); }
}
.practice-board.shake { animation: shake 0.4s ease; }
.practice-hint.ok { color: #2e9e5b; font-weight: 600; }
.practice-hint.err { color: #e05252; font-weight: 600; }

.practice-boards {
  flex: 1; min-height: 0;
  display: flex; justify-content: center; align-items: flex-start;
  gap: 14px;
}
.practice-col { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.practice-board {
  position: relative;
  width: min(46vw, calc(100vh - 185px));
  max-width: calc(50vw - 20px);
  aspect-ratio: 1;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(90, 70, 40, 0.10);
}
@media (orientation: landscape) {
  .practice { padding-top: 8px; gap: 6px; }
  .practice-hint { font-size: 12.5px; }
  .practice-controls .btn { min-width: 120px; padding: 9px 14px; }
  .practice-board { width: min(46vw, calc(100vh - 150px)); }
}
.practice-board .grid-svg {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none;
}
.practice-board .model-svg {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none;
}
.practice-host { position: absolute; inset: 0; touch-action: none; }
.practice-host canvas.brush-ink {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none;
  z-index: 5;   /* the child's ink must stay ABOVE the preview (z3) + library strokes */
}
.practice-label { font-size: 13px; font-weight: 600; color: #a08c6d; }
.practice-hint { text-align: center; font-size: 13.5px; color: #a08c6d; }
.practice-controls { display: flex; justify-content: center; }
.practice-controls .btn { min-width: 150px; padding: 12px 18px; }
/* small portrait phones: nudge rotating for bigger boards */
@media (orientation: portrait) and (max-width: 560px) {
  .practice-hint::after { content: ' · 轉橫畫面，寫得更大'; font-weight: 600; }
}
.writer-wrap.shake { animation: shake 0.4s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-7px); }
  50% { transform: translateX(7px); }
  75% { transform: translateX(-4px); }
}
/* status/hint line: its own row BELOW the writing grid (must never overlap the grid) */
.hint-row {
  display: flex; justify-content: center;
  margin: 10px 0 2px;
  min-height: 34px;
}
.hint {
  background: #fff;
  border-radius: 999px;
  padding: 6px 16px;
  font-size: 14.5px;
  color: var(--ink-soft);
  box-shadow: var(--shadow);
  text-align: center;
}
.hint.ok { color: var(--teal-dark); font-weight: 700; }
.hint.err { color: var(--red); font-weight: 700; }

.char-top { display: flex; align-items: center; justify-content: center; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.char-top h1 {
  font-family: 'NotoSerifHK', serif;
  font-size: 40px;
  margin: 0;
}
.badge {
  display: inline-block;
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 13.5px;
  font-weight: 700;
}
.badge.grade { background: #e3f4f0; color: var(--teal-dark); }
.badge.stage { background: #fdeeda; color: var(--primary-dark); }
.badge.strokes { background: #e8eefc; color: #2b50b8; }
.stars-row { font-size: 22px; letter-spacing: 2px; }

.controls {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin: 14px 0;
}
.controls .btn { white-space: nowrap; }

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}
.info-item { background: #fffdf7; border: 1.5px solid #f3e6d0; border-radius: 14px; padding: 10px 12px; }
.info-item .k { font-size: 12.5px; color: var(--ink-soft); }
.info-item .v { font-size: 17px; font-weight: 600; margin-top: 2px; word-break: break-all; }

.word-list { display: flex; flex-direction: column; gap: 8px; }
.word-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #fffdf7;
  border: 1.5px solid #f3e6d0;
  border-radius: 14px;
  padding: 8px 12px;
}
.word-row .w {
  font-family: 'NotoSerifHK', serif;
  font-size: 21px;
  font-weight: 700;
  min-width: 84px;
}
.word-row .w a { color: inherit; text-decoration: none; }
.word-row .w a:hover { color: var(--primary); }
.word-row .p { font-size: 13.5px; color: var(--ink-soft); flex: 1; }
.word-row .p.p2 { display: flex; flex-direction: column; gap: 2px; }
.word-row .pl { display: flex; align-items: center; font-size: 13.5px; line-height: 1.4; color: var(--ink-soft); }
.word-row .tag {
  font-size: 10.5px; font-weight: 700; color: #fff;
  border-radius: 4px; padding: 1px 5px; margin-right: 6px; flex-shrink: 0;
}
.word-row .tag-py { background: #6b93e6; }  /* 普通話 */
.word-row .tag-jy { background: #f0913c; }  /* 粵語 */
.word-row .s { font-size: 11.5px; color: #fff; background: #c9b797; border-radius: 6px; padding: 1px 6px; white-space: nowrap; flex-shrink: 0; }
.word-row .s.s1 { background: var(--teal); }
.word-row .s.s2 { background: var(--blue); }
.mini-btn {
  border: none;
  background: #fdeeda;
  color: var(--primary-dark);
  border-radius: 999px;
  width: 40px; height: 40px;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
}
.mini-btn:active { transform: scale(0.92); }

.section-title {
  font-size: 17px;
  font-weight: 700;
  margin: 0 0 10px;
  display: flex; align-items: center; gap: 8px;
}
.section-title .more { margin-left: auto; font-size: 14px; font-weight: 500; color: var(--primary-dark); text-decoration: none; }

.nav-row { display: flex; gap: 10px; margin-top: 14px; }
.nav-row .btn { flex: 1; }

.char-layout { display: grid; grid-template-columns: 1fr; gap: 14px; }
@media (min-width: 880px) {
  .char-layout { grid-template-columns: 460px 1fr; align-items: start; }
  .char-layout .left { position: sticky; top: 14px; }
}

.fallback-note {
  background: #fff8e1;
  border: 1.5px solid #ffe082;
  border-radius: 14px;
  padding: 10px 14px;
  font-size: 14px;
  color: #8a6d00;
  margin-bottom: 12px;
}

/* ---------- parent page ---------- */
.stat-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  text-align: center;
}
.stat-card .num { font-size: 32px; font-weight: 800; color: var(--primary-dark); }
.stat-card .lbl { font-size: 13px; color: var(--ink-soft); }

.grade-progress { display: flex; flex-direction: column; gap: 10px; }
.grade-progress .g-row { display: grid; grid-template-columns: 44px 1fr 70px; gap: 10px; align-items: center; font-size: 15px; }
.grade-progress .g-row .g { font-weight: 700; }
.grade-progress .g-row .n { text-align: right; font-size: 13px; color: var(--ink-soft); }

.tip-list { margin: 0; padding-left: 22px; }
.tip-list li { margin-bottom: 8px; }

/* ---------- confetti ---------- */
.confetti-bit {
  position: fixed;
  top: -20px;
  z-index: 99;
  border-radius: 2px;
  pointer-events: none;
  animation: fall linear forwards;
}
@keyframes fall {
  to {
    transform: translate(var(--dx), 105vh) rotate(var(--rot));
    opacity: 0.7;
  }
}

/* ---------- footer ---------- */
footer.foot {
  margin-top: 26px;
  font-size: 12.5px;
  color: var(--ink-soft);
  text-align: center;
  line-height: 1.8;
}
footer.foot a { color: var(--ink-soft); }

/* ---------- print worksheet ---------- */
@media print {
  body { background: #fff; }
  .page { max-width: none; padding: 0; }
  .no-print { display: none !important; }
  .card { box-shadow: none; border: none; padding: 0; }
}
.ws-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 8px; }
@media print { .ws-grid { grid-template-columns: repeat(8, 1fr); gap: 4px; } }
.ws-cell {
  aspect-ratio: 1;
  position: relative;
  border: 1.5px solid #cbb280;
  border-radius: 8px;
  background: #fffdf7;
}
.ws-cell svg { position: absolute; inset: 0; width: 100%; height: 100%; }
/* model character: same filled stroke paths as the stroke animation
   (color via currentColor: tan on screen, light gray in print) */
.ws-cell svg.ref-svg { color: #b9a06a; }
@media print { .ws-cell svg.ref-svg { color: #999; } }
#wsChar svg { width: 34px; height: 34px; }
.ws-stroke-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.ws-stroke-cell {
  width: 76px;
  aspect-ratio: 1;
  position: relative;
  border: 1px solid #cbb280;
  border-radius: 6px;
  background: #fffdf7;
}
.ws-stroke-cell svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.ws-stroke-cell .num {
  position: absolute; left: 2px; top: 0;
  font-size: 11px; color: #a08b60; font-weight: 700;
}
