/* 來學華語 study site. Mobile first; tokens on :root, dark mode below. */
:root {
  --bg: #faf7f2;
  --surface: #ffffff;
  --surface-2: #f2ede4;
  --text: #22201c;
  --muted: #6d675d;
  --line: #e3ddd2;
  --accent: #b23a2b;
  --accent-ink: #ffffff;
  --accent-soft: #f6e3df;
  --right: #2f7d4a;
  --right-soft: #e1f1e5;
  --wrong: #b23a2b;
  --wrong-soft: #f8e0dc;
  --t1: #2463c7; --t2: #2f8a46; --t3: #c76a12; --t4: #c4302b; --t5: #8a857c;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.05);
  --hz-font: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Heiti TC", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171513; --surface: #211f1c; --surface-2: #2b2824; --text: #eeeae3; --muted: #a39d92;
    --line: #3a3631; --accent: #e0644f; --accent-ink: #1a0f0c; --accent-soft: #3d2420;
    --right: #6cc58a; --right-soft: #1f3526; --wrong: #ef7a66; --wrong-soft: #3d2420;
    --t1: #6ea4ff; --t2: #6cc58a; --t3: #f0a050; --t4: #ff7a6e; --t5: #9b968c;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #171513; --surface: #211f1c; --surface-2: #2b2824; --text: #eeeae3; --muted: #a39d92;
  --line: #3a3631; --accent: #e0644f; --accent-ink: #1a0f0c; --accent-soft: #3d2420;
  --right: #6cc58a; --right-soft: #1f3526; --wrong: #ef7a66; --wrong-soft: #3d2420;
  --t1: #6ea4ff; --t2: #6cc58a; --t3: #f0a050; --t4: #ff7a6e; --t5: #9b968c;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, var(--hz-font), sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
h1, h2, h3 { line-height: 1.25; margin: .6em 0 .4em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
a { color: var(--accent); }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small, small { font-size: .82rem; }

/* header */
.top {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--accent); color: #fff;
}
.brand { color: #fff; text-decoration: none; font: 600 1.15rem var(--hz-font); display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.brand span { font: 400 .8rem -apple-system, sans-serif; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topnav a { color: #fff; text-decoration: none; font-size: 1.3rem; padding: 4px 8px; }
#account { max-width: 760px; margin: 0 auto; padding: 8px 16px 0; font-size: .85rem; color: var(--muted); }
#account:empty { display: none; }
main { max-width: 760px; margin: 0 auto; padding: 12px 16px 48px; }
.loading { color: var(--muted); text-align: center; padding: 48px 0; }

/* buttons */
.btn {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  padding: 10px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); text-decoration: none; font-weight: 600; min-height: 44px; text-align: center;
}
.btn small { font-weight: 400; color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary small { color: inherit; opacity: .85; }
.btn.big { padding: 14px 18px; font-size: 1.1rem; width: 100%; }
.btn.small { padding: 6px 12px; min-height: 36px; font-size: .9rem; }
.link { background: none; border: 0; color: var(--accent); padding: 4px 2px; text-decoration: underline; text-underline-offset: 2px; }
.back { display: inline-block; text-decoration: none; margin: 4px 0 6px; font-weight: 600; }

/* panels */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 12px 0; box-shadow: var(--shadow); }
.panel > :first-child { margin-top: 0; }
.panel > :last-child { margin-bottom: 0; }
.section-title { margin-top: 24px; }
.prose p, .prose li { max-width: 65ch; }
.prose img { max-width: 100%; }

/* home */
.hero { display: grid; gap: 10px; margin: 8px 0 4px; }
.hero-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.hero-stats div { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; text-align: center; }
.hero-stats b { display: block; font-size: 1.6rem; line-height: 1.1; }
.hero-stats span { font-size: .8rem; color: var(--muted); }
.lesson-grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 560px) { .lesson-grid { grid-template-columns: 1fr 1fr; } }
.lesson-card {
  display: block; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; color: var(--text); text-decoration: none; box-shadow: var(--shadow);
}
.lesson-card:hover { border-color: var(--accent); }
.lesson-card.soon { background: transparent; border-style: dashed; box-shadow: none; }
.lc-num { color: var(--accent); font: 600 .85rem var(--hz-font); }
.lc-title { font: 600 1.3rem var(--hz-font); margin: 2px 0; }
.lc-en { color: var(--muted); font-size: .9rem; }
.lc-stats { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: .8rem; color: var(--muted); }
.lc-stats .due, .due { color: var(--accent); font-weight: 600; }
.meter { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin: 10px 0 6px; }
.meter i { display: block; height: 100%; background: var(--right); border-radius: 3px; min-width: 0; }
.meter.big { height: 10px; border-radius: 5px; }
.guide-link { display: block; margin: 24px 0 8px; text-align: center; }

/* lesson */
.lesson-head h1 { margin-bottom: 0; font-family: var(--hz-font); }
.lesson-head p { margin: 2px 0 0; color: var(--muted); }
.tabs {
  display: flex; gap: 4px; overflow-x: auto; margin: 12px -16px; padding: 0 16px;
  border-bottom: 1px solid var(--line); scrollbar-width: none; position: sticky; top: 52px; background: var(--bg); z-index: 5;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a { flex: none; padding: 10px 12px; text-decoration: none; color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent; }
.tabs a.on { color: var(--accent); border-bottom-color: var(--accent); }
.objectives { padding-left: 1.2em; }
.objectives li { margin: 4px 0; }
.path { list-style: none; padding: 0; margin: 0; counter-reset: step; }
.path li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.path li:last-child { border-bottom: 0; }
.path li a { flex: 1; }
.path li.done a { color: var(--muted); text-decoration: line-through; }
.path .tick {
  flex: none; width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--line); background: var(--surface);
  font-size: .9rem; line-height: 1; color: transparent;
}
.path li.done .tick { background: var(--right); border-color: var(--right); color: #fff; }
.checkin { padding: 10px 0; border-bottom: 1px solid var(--line); }
.checkin:last-child { border-bottom: 0; }
.checkin p { margin: 0 0 6px; }
.seg { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; padding: 7px 12px; font-size: .88rem; border-right: 1px solid var(--line); }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }

/* Chinese text, zhuyin and tones */
.hz { font-family: var(--hz-font); }
ruby { ruby-position: over; ruby-align: center; }
rt { font-size: .5em; letter-spacing: 0; font-family: var(--hz-font); }
.t1 { color: var(--t1); } .t2 { color: var(--t2); } .t3 { color: var(--t3); } .t4 { color: var(--t4); } .t5 { color: var(--t5); } .t0 { color: inherit; }
.hz ruby { color: var(--text); }
.hz ruby rt { color: var(--t5); }
.hz ruby.t1 rt { color: var(--t1); } .hz ruby.t2 rt { color: var(--t2); } .hz ruby.t3 rt { color: var(--t3); } .hz ruby.t4 rt { color: var(--t4); }
.py { font-size: .95em; }
.no-zy rt { display: none; }
.tone-legend { list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: 6px 16px; }
.tone-legend li { font-weight: 600; }

.say {
  display: inline-flex; align-items: center; justify-content: center; vertical-align: middle;
  width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface);
  font-size: .95rem; padding: 0; flex: none;
}
.say:hover { border-color: var(--accent); }
.say.huge { width: 96px; height: 96px; font-size: 2.6rem; margin: 8px auto; }
body.no-voice .say, body.no-voice .say-all, body.no-voice .hear { display: none; }

/* dialogue */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 8px 0; }
.toggles { display: flex; gap: 12px; flex-wrap: wrap; }
.toggles label { display: inline-flex; align-items: center; gap: 4px; font-size: .92rem; }
.quiz-hint:empty { display: none; }
.dialogue { display: grid; gap: 12px; margin-top: 8px; }
.line { display: flex; flex-direction: column; align-items: flex-start; max-width: 92%; }
.line.s1 { align-self: flex-end; align-items: flex-end; }
.who { font: 600 .8rem var(--hz-font); color: var(--muted); margin: 0 6px 2px; }
.bubble { background: var(--surface); border: 1px solid var(--line); border-radius: 16px 16px 16px 4px; padding: 10px 14px; box-shadow: var(--shadow); }
.line.s1 .bubble { background: var(--accent-soft); border-color: transparent; border-radius: 16px 16px 4px 16px; }
.line.playing .bubble { outline: 2px solid var(--accent); }
.zh { font-size: 1.35rem; line-height: 2.1; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.pyl { color: var(--muted); font-size: .92rem; margin-top: 2px; }
.enl { font-size: .95rem; margin-top: 4px; }
.dialogue.no-zy rt { display: none; }
.dialogue.no-zy .zh { line-height: 1.6; }
.dialogue.no-py .pyl, .dialogue.no-en .enl { display: none; }
.reveal { display: none; margin-top: 6px; border: 1px dashed var(--accent); background: none; color: var(--accent); border-radius: 8px; padding: 6px 12px; font-size: .9rem; }
.dialogue[data-quiz="zh"] .line:not(.shown) .zh,
.dialogue[data-quiz="zh"] .line:not(.shown) .pyl,
.dialogue[data-quiz="en"] .line:not(.shown) .enl { display: none; }
.dialogue[data-quiz="zh"] .line:not(.shown) .reveal,
.dialogue[data-quiz="en"] .line:not(.shown) .reveal { display: inline-block; }
.dialogue[data-quiz="en"] .line:not(.shown) .enl + .reveal { display: inline-block; }
.dialogue[data-quiz="zh"].no-en .line:not(.shown) .enl { display: block; }

/* words */
.study-start { text-align: center; }
.study-start p { margin: 0 0 12px; }
.wordlist { list-style: none; padding: 0; margin: 0; }
.wordlist li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.wordlist li:last-child { border-bottom: 0; }
.wordlist .w { flex: none; min-width: 5.5em; font-size: 1.3rem; line-height: 2; }
.wordlist .m { flex: 1; min-width: 0; font-size: .92rem; }
.pos { font-size: .75rem; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; margin-right: 2px; }
.tag { flex: none; font-size: .72rem; border-radius: 20px; padding: 2px 8px; background: var(--surface-2); color: var(--muted); }
.tag.learning { background: var(--accent-soft); color: var(--accent); }
.tag.due { background: var(--accent); color: var(--accent-ink); }
.tag.mastered { background: var(--right-soft); color: var(--right); }

/* flashcards */
.study { display: flex; flex-direction: column; gap: 12px; }
.study-top { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: .9rem; }
.flash {
  position: relative; min-height: 300px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 18px; box-shadow: var(--shadow); display: flex; cursor: pointer; user-select: none;
}
.flash.flipped { cursor: default; }
.face { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding: 32px 18px 20px; text-align: center; }
.face .from { position: absolute; top: 10px; left: 12px; font-size: .75rem; color: var(--muted); background: var(--surface-2); border-radius: 20px; padding: 2px 8px; }
.face .big { font-size: 3rem; line-height: 1.8; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: center; }
.face.back .big { font-size: 2.3rem; }
.en-big { font-size: 1.6rem; font-weight: 600; }
.py-big { font-size: 1.35rem; }
.face .en { font-size: 1.1rem; }
.example { margin-top: 8px; padding-top: 12px; border-top: 1px solid var(--line); width: 100%; font-size: .95rem; display: grid; gap: 2px; }
.example .hz { font-size: 1.2rem; line-height: 2; }
.rate { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate button {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 10px 4px; min-height: 56px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-weight: 600;
}
.rate small { font-weight: 400; color: var(--muted); }
.rate .g0 { color: var(--wrong); }
.rate .g2 { background: var(--right-soft); border-color: transparent; color: var(--right); }
.rate .g3 { color: var(--t1); }
.flip-btn { width: 100%; }
.rate:not(.hidden) ~ .flip-btn { display: none; }
.keys { text-align: center; margin: 0; }
@media (hover: none) { .keys { display: none; } }
.done-panel { text-align: center; }
.again-btn { margin-top: 8px; }

/* grammar */
.grammar h2 { margin-top: 0; font-family: var(--hz-font); }
.pattern { font: 600 1.15rem var(--hz-font); background: var(--surface-2); border-left: 4px solid var(--accent); border-radius: 6px; padding: 8px 12px; margin: 8px 0; }
.examples { list-style: none; padding: 0; margin: 10px 0; display: grid; gap: 10px; }
.examples li, .examples > div { display: grid; gap: 1px; }
.examples .hz { font-size: 1.2rem; line-height: 2; }
.exercises { display: grid; gap: 12px; margin-top: 12px; }
.order { border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: var(--bg); }
.order.right { border-color: var(--right); background: var(--right-soft); }
.order.wrong { border-color: var(--wrong); }
.built { min-height: 48px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 6px; border-bottom: 2px dashed var(--line); margin-bottom: 10px; }
.pool, .bank { display: flex; flex-wrap: wrap; gap: 6px; }
.piece {
  font: 1.1rem var(--hz-font); padding: 8px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); box-shadow: 0 1px 0 var(--line); min-height: 44px;
}
.piece small { color: var(--muted); font-size: .75rem; }
.piece:disabled { opacity: .35; }
.piece.right { border-color: var(--right); background: var(--right-soft); }
.piece.wrong { border-color: var(--wrong); background: var(--wrong-soft); }
.order-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 10px; }
.ok { color: var(--right); font-weight: 600; }
.bad { color: var(--wrong); font-weight: 600; }

/* practice */
.activity h2, .fill h2 { margin-top: 0; }
.q { display: grid; gap: 12px; }
.q-prompt { font-size: 1.3rem; text-align: center; padding: 12px 0; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.q-prompt .hz { font-size: 2.2rem; line-height: 1.9; }
.q-foot { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: .88rem; }
.choices { display: grid; gap: 8px; }
@media (min-width: 560px) { .choices { grid-template-columns: 1fr 1fr; } }
.choices button {
  padding: 12px; min-height: 52px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); text-align: center;
}
.choices button.right { border-color: var(--right); background: var(--right-soft); color: var(--right); font-weight: 600; }
.choices button.wrong { border-color: var(--wrong); background: var(--wrong-soft); color: var(--wrong); }
.hz-choice { font: 1.5rem/1.9 var(--hz-font); }
.answer { text-align: center; }
.next-btn, .back-btn { width: 100%; }

/* fill in the blanks */
.fb { display: grid; gap: 10px; }
.fb-line { font: 1.15rem/2.2 var(--hz-font); }
.blank {
  display: inline-block; min-width: 3.2em; min-height: 1.8em; vertical-align: middle; margin: 0 2px;
  border: 0; border-bottom: 2px solid var(--accent); background: var(--accent-soft); border-radius: 6px 6px 0 0;
  font: inherit; line-height: 1.5; padding: 0 6px;
}
.blank.right { border-color: var(--right); background: var(--right-soft); }
.blank.wrong { border-color: var(--wrong); background: var(--wrong-soft); text-decoration: line-through; }
.fb-msg { font-size: .9rem; }

/* activities */
.table-wrap { overflow-x: auto; margin: 8px -4px; }
.table-wrap table { border-collapse: collapse; width: 100%; min-width: 420px; font-size: .92rem; }
.table-wrap th, .table-wrap td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; }
.table-wrap th { background: var(--surface-2); }
.table-wrap input { width: 100%; font: inherit; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--text); }
.bizcards { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
.biz { border: 1px solid var(--line); border-radius: 6px; padding: 14px; background: var(--surface); box-shadow: var(--shadow); }
.biz-name { display: flex; gap: 4px; margin-bottom: 4px; }
.biz-name button { font: 600 1.5rem var(--hz-font); border: 1px solid transparent; background: var(--surface-2); border-radius: 8px; padding: 2px 8px; }
.biz-name button.right { background: var(--right-soft); border-color: var(--right); color: var(--right); }
.biz-name button.wrong { background: var(--wrong-soft); border-color: var(--wrong); color: var(--wrong); }
.biz-msg:empty { display: none; }
.biz-msg { margin-top: 6px; }

/* sounds */
.notes { padding-left: 1.2em; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 14px; }
.chip {
  display: inline-flex; flex-direction: column; align-items: center; min-width: 54px; padding: 6px 10px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
}
.chip b { font: 600 1.15rem var(--hz-font); }
.chip span { font-size: .8rem; color: var(--muted); }
.pairs { display: grid; gap: 10px; }
.pair { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.hear-box { display: grid; gap: 10px; }
.hear-q { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

/* settings */
.form { display: grid; gap: 14px; }
.form label.row, label.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.form select, .form input[type="number"] { font: inherit; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--text); }
.form input[type="range"] { flex: 1; min-width: 140px; accent-color: var(--accent); }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 50;
  max-width: calc(100% - 32px); font-size: .92rem; box-shadow: 0 6px 20px rgba(0,0,0,.2);
  animation: toast-in .2s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }
