:root {
  --bg: #f7f4ec; --surface: #ffffff; --surface-2: #eef0e8; --text: #1f2522; --muted: #5f6a65; --line: #dfe2d6;
  --accent: #1f4f4a; --accent-2: #2f6f66; --gold: #b8892d; --ink: #1f2522; --ok: #2f8a4c; --bad: #b8433a; --change: #b8433a;
  --radius: 14px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(30,50,40,.07);
  --serif: 'Noto Serif CJK TC', 'Noto Serif TC', 'Noto Serif CJK SC', 'Noto Serif SC', 'Songti TC', 'Songti SC', 'PMingLiU', serif;
  color-scheme: light;
}
/* Dark theme: follows the phone unless the user picked Light or Dark (html[data-theme]). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101614; --surface: #19211e; --surface-2: #222c28; --text: #e8ece6; --muted: #9aa69f; --line: #2e3a35;
    --accent: #7cc4b4; --accent-2: #9ad6c8; --gold: #e2b85c; --ink: #e8ece6; --change: #f08b7e;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #101614; --surface: #19211e; --surface-2: #222c28; --text: #e8ece6; --muted: #9aa69f; --line: #2e3a35;
  --accent: #7cc4b4; --accent-2: #9ad6c8; --gold: #e2b85c; --ink: #e8ece6; --change: #f08b7e;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 16px/1.55 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  display: flex; flex-direction: column; overscroll-behavior-y: none; }
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px; padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: #1f4f4a; color: #fff; }
@media (prefers-color-scheme: dark) { :where(:root:not([data-theme="light"])) .topbar { background: #13211f; } }
:where(:root[data-theme="dark"]) .topbar { background: #13211f; }
.topbar h1 { font-size: 18px; margin: 0; flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .logo { font-family: var(--serif); font-size: 20px; opacity: .85; }
.topbar .hbtn { background: rgba(255,255,255,.14); color: inherit; border: 0; border-radius: 999px; min-width: 34px; height: 32px; padding: 0 9px; font: 600 15px/32px system-ui, sans-serif; cursor: pointer; }
.back { background: none; border: 0; color: inherit; font-size: 30px; line-height: 1; padding: 0 8px 4px 0; cursor: pointer; }
main { flex: 1; overflow-y: auto; padding: 16px 16px calc(110px + env(safe-area-inset-bottom)); max-width: 760px; width: 100%; margin: 0 auto; outline: none; zoom: var(--text-zoom, 1); }
.tabs { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); z-index: 5; }
.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; padding: 6px 0 8px; color: var(--muted); text-decoration: none; font-size: 12px; min-width: 0; }
.tabs a .ti { font-family: var(--serif); font-size: 22px; line-height: 1.2; }
.tabs a.active { color: var(--accent); font-weight: 600; }
h2 { font-size: 22px; margin: 4px 0 12px; }
h3 { font-size: 17px; margin: 20px 0 6px; }
h4 { font-size: 15px; margin: 14px 0 2px; color: var(--accent); }
p { margin: 8px 0; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.zh { font-family: var(--serif); }
.accent { color: var(--accent); }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin: 0 0 14px; border: 1px solid var(--line); }
.card.inset { box-shadow: none; background: var(--surface-2); }
.hero { background: linear-gradient(135deg, #1f4f4a, #10302c); color: #fff; border: 0; }
.hero p { color: #dcebe6; }
.hero .big { font-family: var(--serif); font-size: 44px; line-height: 1; }
.progress { height: 8px; background: rgba(255,255,255,.25); border-radius: 99px; overflow: hidden; margin-top: 10px; }
.progress > i { display: block; height: 100%; background: var(--gold); border-radius: 99px; }
.list { display: flex; flex-direction: column; gap: 10px; }
.list > .card { margin: 0; }
.lvl-head { display: flex; align-items: baseline; justify-content: space-between; margin: 20px 4px 8px; }
.lvl-head h3 { margin: 0; }
.lesson-item, .daily, .reading-row { display: flex; gap: 14px; align-items: center; text-decoration: none; color: inherit; }
.lesson-item .badge { flex: 0 0 52px; height: 52px; border-radius: 12px; background: var(--surface-2); display: grid; place-items: center;
  font-family: var(--serif); font-size: 19px; color: var(--accent); }
.lesson-item.done .badge { background: var(--accent); color: var(--surface); }
.lesson-item.here { box-shadow: 0 0 0 2px var(--gold); }
.t { font-weight: 600; }
.s { display: block; font-size: 13px; color: var(--muted); }
.lvl { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--gold); font-weight: 600; }
.daily { border-color: color-mix(in srgb, var(--gold) 45%, var(--line)); }
.about-link { display: flex; align-items: center; gap: 12px; text-decoration: none; color: var(--text); margin-top: 18px; }
.about-link > span:first-child { font-size: 22px; color: var(--muted); }
.go { margin-left: auto; font-size: 22px; color: var(--muted); }
.lesson-body ul, .lesson-body ol { padding-left: 22px; }
.lesson-body li { margin: 4px 0; }
.note, .tip { border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.note { background: var(--surface-2); color: var(--muted); }
.tip { background: color-mix(in srgb, var(--gold) 16%, transparent); }
.tip::before { content: '💡 '; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 0; border-radius: 12px; padding: 12px 18px; font: inherit; font-weight: 600;
  background: var(--accent); color: #fff; cursor: pointer; text-decoration: none; min-height: 46px; text-align: center; }
@media (prefers-color-scheme: dark) { :where(:root:not([data-theme="light"])) .btn { color: #0d1a17; } }
:where(:root[data-theme="dark"]) .btn { color: #0d1a17; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.gold { background: var(--gold); color: #1b1410; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > * { flex: 1; }
label { display: block; font-size: 14px; font-weight: 600; margin: 12px 0 4px; }
input, select, textarea { width: 100%; font: inherit; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text); min-height: 46px; }
textarea { resize: vertical; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: var(--surface); color: var(--text); padding: 11px 6px; font: inherit; font-size: 14px; cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--surface); font-weight: 600; }
.help { font-size: 12px; color: var(--muted); margin-top: 6px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; overflow-wrap: anywhere; }
.table-wrap { overflow-x: auto; }
.chip { display: inline-block; padding: 2px 10px; border-radius: 99px; background: var(--surface-2); font-size: 13px; margin: 2px 4px 2px 0; }
.chips { margin-top: 6px; }
.pill { font-size: 11px; background: var(--gold); color: #1b1410; padding: 2px 8px; border-radius: 99px; font-weight: 600; }

/* Figures */
svg.fig { display: block; width: 64px; height: auto; fill: var(--ink); overflow: visible; flex: 0 0 auto; }
svg.fig .ln.ch rect:not([fill]) { fill: var(--change); }
svg.fig .ln.hi rect:not([fill]) { fill: var(--gold); }
svg.fig .mk { fill: none; stroke: var(--change); stroke-width: 2.2; }
svg.fig .num { font: 600 10px system-ui, sans-serif; fill: var(--muted); }
svg.fig.inline { display: inline-block; vertical-align: middle; }
svg.fig [data-line] { cursor: pointer; }

/* Lesson diagrams */
.diagram { margin: 14px 0; }
.dg-row { display: flex; gap: 16px; align-items: center; }
.dg-center { justify-content: center; }
.dg-cap { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; text-decoration: none; color: inherit; }
.arrow { font-size: 26px; color: var(--muted); }
.hexlink { display: flex; flex-direction: column; align-items: center; gap: 6px; text-decoration: none; color: inherit; text-align: center; font-size: 14px; }
.tri-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tri-card { display: flex; gap: 12px; align-items: center; text-align: left; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 12px; padding: 12px; font: inherit; cursor: pointer; }
.tri-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.tri-table tr { cursor: pointer; }
.bagua svg { width: 100%; max-width: 340px; display: block; margin: 8px auto 0; }
.bagua-tri { fill: var(--ink); cursor: pointer; }
.yy-ring { fill: var(--surface-2); stroke: var(--line); }
.bagua-mid { font: 22px var(--serif); fill: var(--accent); }
.bagua-lbl { font: 20px var(--serif); fill: var(--accent); }
.bagua-dir { font: 600 10px system-ui, sans-serif; fill: var(--muted); }
.builder-out { display: flex; gap: 14px; align-items: center; margin-top: 12px; }
.builder label { margin-top: 0; }
.pairs { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.pair { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 6px; background: var(--surface-2); border-radius: 12px; padding: 10px; text-align: center; }
.pair .small { flex-basis: 100%; }
.pair .hexlink { font-size: 12px; max-width: 80px; }
.pos-list { display: flex; flex-direction: column; gap: 10px; font-size: 13px; }
.hidden-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.hidden-grid .hexlink { font-size: 12px; }
.sovereign { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.sov { display: flex; flex-direction: column; align-items: center; gap: 3px; text-decoration: none; color: inherit; font-size: 11px; text-align: center; line-height: 1.25; }
.sov span { color: var(--muted); font-size: 10px; }
.big-name { text-decoration: none; color: inherit; font-size: 18px; }
.coin-demo { text-align: center; }
.coins { display: flex; justify-content: center; gap: 12px; margin: 6px 0 12px; }
.coin { width: 58px; height: 58px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; background: var(--surface-2); border: 3px solid var(--line); color: var(--muted); }
.coin.heads { background: color-mix(in srgb, var(--gold) 30%, var(--surface)); border-color: var(--gold); color: var(--text); }
.coin.tails { background: var(--surface); border-color: var(--muted); color: var(--text); }
.coins.big .coin { width: 70px; height: 70px; }
.coin.flip, .coin.spin { animation: flip .5s ease-out; }
@keyframes flip { from { transform: rotateY(0) scale(.8); } 50% { transform: rotateY(540deg) scale(1.1); } to { transform: rotateY(720deg) scale(1); } }
@media (prefers-reduced-motion: reduce) { .coin.flip, .coin.spin { animation: none; } }

/* Casting & reading */
.question { font-family: Georgia, var(--serif); font-style: italic; font-size: 18px; text-align: center; margin: 4px 8px 12px; }
.cast-build { display: flex; flex-direction: column-reverse; align-items: center; gap: 10.5px; min-height: 140px; margin: 4px 0 8px; }
.ghost-line { width: 150px; height: 15px; border: 2px dashed var(--line); border-radius: 3px; }
.manual { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.manual .btn { flex-direction: column; gap: 4px; font-size: 13px; font-weight: 400; }
.yarrow-steps { background: var(--surface-2); border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; line-height: 1.7; }
.reading-head .dg-cap { font-size: 14px; }
.text-block { border-left: 4px solid var(--line); padding: 4px 12px; margin: 12px 0; }
.text-block.main { border-color: var(--gold); background: color-mix(in srgb, var(--gold) 9%, transparent); border-radius: 0 10px 10px 0; padding: 8px 12px; }
.text-block .tb-head { font-size: 13px; font-weight: 600; color: var(--muted); margin-top: 4px; }
.line-item { display: flex; gap: 12px; align-items: flex-start; padding: 10px 0; border-top: 1px solid var(--line); }
.line-item p { margin: 4px 0 0; }
.btnlike { width: 100%; text-align: left; background: none; border-left: 0; border-right: 0; border-bottom: 0; color: inherit; font: inherit; cursor: pointer; }
.btnlike svg.fig { margin-top: 6px; }
.hex-row { display: flex; gap: 12px; align-items: center; text-decoration: none; color: inherit; padding: 8px 0; }
.list .hex-row { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.card .hex-row + .hex-row { border-top: 1px solid var(--line); }
.reading-row .t { display: block; }
.hex-head { display: flex; gap: 18px; align-items: center; }
.hh-name { font-size: 18px; font-weight: 600; }
.brief { font-size: 17px; font-weight: 500; }
.line-text { font-size: 17px; }
.facts { padding-left: 18px; }
.facts li { margin: 6px 0; }
.big-zh { font-family: var(--serif); font-size: 52px; color: var(--accent); }
.sheet .head { display: flex; gap: 14px; align-items: center; margin-bottom: 8px; padding-right: 36px; }

/* Library grid */
.hex-grid { display: grid; grid-template-columns: repeat(9, 1fr); gap: 3px; }
.hex-grid .corner { font-size: 8px; line-height: 1.2; display: grid; place-items: center; text-align: center; }
.hex-grid .gh { border: 0; background: var(--surface-2); border-radius: 6px; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 0; color: var(--accent); font: 13px var(--serif); cursor: pointer; }
.hex-grid .gh svg.fig { width: 18px; }
.hex-grid .gc { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 5px 0 3px; text-decoration: none; color: var(--muted); font-size: 10px; }
.hex-grid .gc svg.fig { width: 70%; max-width: 30px; }
.hex-grid .gc.dim { opacity: .25; }

/* Quiz */
.q-opts { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.q-opts.fig-opts { display: grid; grid-template-columns: 1fr 1fr; }
.q-opts.fig-opts .q-opt { display: grid; place-items: center; padding: 16px; }
.q-opt { text-align: left; padding: 14px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface); color: var(--text); font: inherit; cursor: pointer; }
.q-opt.right { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 15%, var(--surface)); }
.q-opt.wrong { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, var(--surface)); }
.q-fig { display: flex; justify-content: center; margin: 10px 0; }
.q-explain { margin-top: 12px; font-size: 14px; }
.q-head { display: flex; justify-content: space-between; color: var(--muted); font-size: 14px; }
.score-big { font-size: 48px; font-weight: 700; text-align: center; color: var(--accent); }
.cat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.cat { text-align: left; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: var(--radius); padding: 14px; cursor: pointer; font: inherit; box-shadow: var(--shadow); }
.cat .zh { font-size: 26px; }
.cat .best { font-size: 12px; color: var(--gold); font-weight: 600; margin-top: 4px; }

/* Sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--surface); width: 100%; max-width: 760px; max-height: 85vh; overflow-y: auto; border-radius: 20px 20px 0 0; padding: 20px 18px calc(24px + env(safe-area-inset-bottom)); position: relative; animation: up .2s ease-out; zoom: var(--text-zoom, 1); }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-close { position: absolute; top: 10px; right: 12px; border: 0; background: var(--surface-2); color: var(--text); width: 36px; height: 36px; border-radius: 50%; font-size: 22px; cursor: pointer; }
.web-foot { text-align: center; margin-top: 24px; }

@media (max-width: 380px) {
  .tri-grid, .tri-pair { grid-template-columns: 1fr; }
  .sovereign { grid-template-columns: repeat(4, 1fr); }
  .hidden-grid { grid-template-columns: repeat(2, 1fr); }
  .tabs a { font-size: 11px; }
}

/* Casting by hand */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-btn { border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 99px; padding: 9px 14px; font: inherit; font-size: 14px; cursor: pointer; min-height: 40px; }
.chip-btn.on { background: var(--accent); color: var(--surface); border-color: var(--accent); font-weight: 600; }
button.coin { cursor: pointer; font: inherit; font-size: 12px; font-weight: 700; padding: 0; }
.ghost-line.next { border-color: var(--gold); }
.yarrow-guide h4 { margin-top: 4px; }
.steps { padding-left: 20px; margin: 6px 0 10px; }
.steps li { margin: 3px 0; }
.rem-row { display: flex; align-items: center; gap: 8px; margin: 8px 0; }
.rem-row > span { flex: 0 0 92px; font-size: 14px; font-weight: 600; }
.rem-row .chip-btn { flex: 1; padding: 9px 0; }
.error { color: var(--bad); font-weight: 600; }
.done-note { background: color-mix(in srgb, var(--ok) 14%, transparent); border-radius: 10px; padding: 8px 12px; margin: 0 0 8px; }
.how-to { counter-reset: s; list-style: none; padding: 0; }
.how-to > li { counter-increment: s; position: relative; padding: 2px 0 10px 40px; }
.how-to > li::before { content: counter(s); position: absolute; left: 0; top: 0; width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: var(--surface); display: grid; place-items: center; font-weight: 700; font-size: 14px; }
.paper { font-family: ui-monospace, Menlo, monospace; background: var(--surface-2); border-radius: 10px; padding: 10px 14px; font-size: 14px; line-height: 1.7; white-space: pre; overflow-x: auto; }

/* Question coach */
.q-hint { font-size: 13px; border-radius: 10px; padding: 8px 12px; margin: 8px 0 0; }
.q-hint.warn { background: color-mix(in srgb, var(--gold) 15%, transparent); }
.q-hint.ok { background: color-mix(in srgb, var(--ok) 13%, transparent); }
.q-check textarea { margin-top: 4px; }
.why { display: block; font-size: 14px; color: var(--muted); margin-top: 4px; }
.linklike { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); text-decoration: underline; cursor: pointer; }
.ex-list { display: flex; flex-direction: column; gap: 6px; }
.ex-btn { text-align: left; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 10px; padding: 10px 12px; font: inherit; font-size: 14px; cursor: pointer; }

/* Premium */
.premium-banner { display: flex; align-items: center; gap: 12px; text-decoration: none; color: var(--text); font-size: 14px;
  border: 1px solid color-mix(in srgb, var(--gold) 55%, var(--line)); background: color-mix(in srgb, var(--gold) 10%, var(--surface)); }
.premium-banner .pb-icon { color: var(--gold); font-size: 22px; }
.lesson-item.locked .t, .lesson-item.locked .s { opacity: .65; }
.paywall { text-align: center; border: 1px solid color-mix(in srgb, var(--gold) 55%, var(--line)); }
.paywall .pw-icon { font-size: 32px; color: var(--gold); line-height: 1; }
.paywall h3 { margin: 8px 0 4px; }
.benefits { list-style: none; padding: 0; margin: 0 0 16px; }
.benefits li { padding: 10px 0 10px 30px; border-bottom: 1px solid var(--line); position: relative; }
.benefits li::before { content: '✦'; color: var(--gold); position: absolute; left: 4px; top: 10px; }
.benefits span { display: block; font-size: 13px; color: var(--muted); }

/* Feng shui & Plum Blossom */
.fs-icon { flex: 0 0 52px; height: 52px; border-radius: 12px; background: var(--surface-2); display: grid; place-items: center; font: 24px var(--serif); color: var(--accent); }
.compass { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 10px 0; }
.compass .cg { background: var(--surface-2); border-radius: 10px; padding: 8px 4px; min-height: 84px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; text-align: center; line-height: 1.25; cursor: pointer; }
.compass .cg.centre { background: color-mix(in srgb, var(--gold) 18%, var(--surface-2)); }
.compass .cg svg.fig { width: 24px; }
.compass.map .cg b { font-size: 13px; }
.compass .ls-n { font-size: 24px; font-family: var(--serif); color: var(--accent); }
.compass .ls-n.missing { color: var(--line); }
.compass.birth .ls-n { font-size: 18px; letter-spacing: 1px; }
.compass .good, .mansion.good b, tr.good td:last-child { color: var(--ok); }
.compass .bad, .mansion.bad b, tr.bad td:last-child { color: var(--bad); }
.kua-head { display: flex; align-items: center; gap: 16px; }
.kua-n { font-size: 52px; font-weight: 700; color: var(--accent); line-height: 1; }
.mansion { padding: 8px 0; border-top: 1px solid var(--line); }
.mansion .small { display: block; color: var(--muted); }
.plum-steps { background: var(--surface-2); border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; line-height: 1.8; }
.plum-card { border-color: color-mix(in srgb, #d86a8a 45%, var(--line)); }
.seg-wrap { flex-wrap: wrap; }
.mansions td, .mansions th { padding: 5px 3px; }
