:root {
  --bg: #f7f3ec;
  --surface: #ffffff;
  --surface-2: #efe9df;
  --border: #e2dacc;
  --text: #1c1a17;
  --muted: #5f584f;
  --accent: #a8202b;
  --accent-soft: #f6e3e1;
  --on-accent: #ffffff;
  --ink: #1c1a17;
  --on-ink: #f7f3ec;
  --focus: #1d4ed8;
  --oral: #0e6b62;
  --oral-soft: #ddf0ec;
  --written: #3730a3;
  --written-soft: #e7e8fd;
  --both: #8a5a00;
  --both-soft: #f8ebd3;
  --t1: #b42318; --t2: #b45309; --t3: #15803d; --t4: #1d4ed8; --t5: #6d28d9; --t6: #be185d;
  --shadow: 0 1px 2px rgb(28 26 23 / .05), 0 6px 20px rgb(28 26 23 / .05);
  --radius: 16px;
  --cjk: "PingFang HK", "PingFang TC", "Noto Sans CJK HK", "Noto Sans HK", "Noto Sans CJK TC", "Microsoft JhengHei", "Source Han Sans HK", sans-serif;
  /* Display face for headwords: serif Hanzi, falling back to the system's CJK fonts. */
  --hanzi: "Noto Serif HK", "Noto Serif CJK HK", "Songti TC", "PMingLiU", var(--cjk);
  --ui: Manrope, system-ui, -apple-system, "Segoe UI", "Helvetica Neue", var(--cjk);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161412; --surface: #1f1c19; --surface-2: #2a2622; --border: #3a3530; --text: #f7f3ec; --muted: #aaa196;
    --accent: #f2938f; --accent-soft: #3a1f1d; --on-accent: #1c1a17; --ink: #f7f3ec; --on-ink: #1c1a17; --focus: #7aa2ff;
    --oral: #6ed3c3; --oral-soft: #132e2a; --written: #aab4fc; --written-soft: #22244a; --both: #f2c46b; --both-soft: #36290f;
    --t1: #f87171; --t2: #fbbf24; --t3: #4ade80; --t4: #60a5fa; --t5: #c084fc; --t6: #f472b6;
    --shadow: 0 1px 2px rgb(0 0 0 / .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #161412; --surface: #1f1c19; --surface-2: #2a2622; --border: #3a3530; --text: #f7f3ec; --muted: #aaa196;
  --accent: #f2938f; --accent-soft: #3a1f1d; --on-accent: #1c1a17; --ink: #f7f3ec; --on-ink: #1c1a17; --focus: #7aa2ff;
  --oral: #6ed3c3; --oral-soft: #132e2a; --written: #aab4fc; --written-soft: #22244a; --both: #f2c46b; --both-soft: #36290f;
  --t1: #f87171; --t2: #fbbf24; --t3: #4ade80; --t4: #60a5fa; --t5: #c084fc; --t6: #f472b6;
  --shadow: 0 1px 2px rgb(0 0 0 / .4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 var(--ui); }
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; z-index: 50; }
.skip:focus { left: 8px; }
.noscript { padding: 24px; text-align: center; }
.cjk { font-family: var(--cjk); }

/* ---- Top bar ---- */
.topbar { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px); border-bottom: 1px solid var(--border); }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: 10px 16px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.brand { display: flex; align-items: baseline; gap: 8px; text-decoration: none; font-weight: 800; min-width: 44px; min-height: 44px; padding-block: 4px; }
.brand-mark { font-family: var(--hanzi); font-weight: 700; color: var(--accent); font-size: 24px; letter-spacing: 1px; }
.brand-name { font-size: 17px; letter-spacing: -.2px; }
#search-dock { max-width: 1100px; margin: 0 auto; padding: 14px 16px 0; }
#search-dock[hidden] { display: none; }
.search { display: flex; align-items: center; gap: 6px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 4px 4px 12px; box-shadow: var(--shadow); min-width: 0; }
.search:focus-within { border-color: var(--focus); }
.search-icon { width: 18px; height: 18px; fill: var(--muted); flex: none; }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 6px 2px; outline: none; font-size: 16px; }
.search select { border: 0; background: var(--surface-2); border-radius: 999px; padding: 6px 10px; font-size: 13px; max-width: 42%; cursor: pointer; }
.nav { display: flex; align-items: center; gap: 2px; margin-left: auto; }
.nav a { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; text-decoration: none; padding: 6px 10px; border-radius: 999px; color: var(--muted); font-weight: 500; }
.nav a:hover { color: var(--text); background: var(--surface-2); }
.nav a.active { color: var(--accent); background: var(--accent-soft); }
.icon-btn { border: 0; background: transparent; width: 44px; height: 44px; border-radius: 999px; display: grid; place-items: center; cursor: pointer; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; fill: currentColor; }

main { max-width: 1100px; margin: 0 auto; padding: 16px 16px 64px; outline: none; }

/* ---- Toolbar / chips ---- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; margin: 4px 0 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--border); background: var(--surface); padding: 6px 13px; border-radius: 999px; cursor: pointer; font-size: 13px; font-weight: 600; }
.chip:hover { border-color: var(--muted); }
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip .cjk { margin-left: 3px; }
.select { border: 1px solid var(--border); background: var(--surface); padding: 5px 10px; border-radius: 999px; font-size: 13px; cursor: pointer; }
.label { color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; margin-right: 2px; }
.group { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
/* The circle stays 32px so Sort and Filters still share one row at 320px. ::before is the 44px hit area. */
.dir-btn { position: relative; width: 32px; height: 32px; border: 1px solid var(--border); background: var(--surface); border-radius: 50%; cursor: pointer; font-size: 15px; font-weight: 700; line-height: 1; color: var(--text); }
.dir-btn::before { content: ""; position: absolute; left: 50%; top: 50%; width: 44px; height: 44px; transform: translate(-50%, -50%); }
.dir-btn:hover { border-color: var(--muted); }
.filters { position: relative; margin-left: auto; }
.filters-btn { position: relative; list-style: none; cursor: pointer; color: var(--muted); font-size: 13px; font-weight: 600; padding: 6px 4px; display: inline-flex; align-items: center; gap: 6px; min-width: 44px; min-height: 44px; }
.filters-btn::-webkit-details-marker { display: none; }
.filters-btn::after { content: "▾"; font-size: 11px; }
.filters-btn.active { color: var(--text); }
.filters-btn .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); clip-path: inset(50%); white-space: nowrap; border: 0; }
.filters-pop { position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; width: min(360px, calc(100vw - 32px)); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px; }
.filters-pop p { color: var(--muted); font-size: 12px; line-height: 1.45; margin: 10px 0 0; }
.summary { color: var(--muted); font-size: 13px; margin: 0 0 10px; }
.summary b { color: var(--text); }

/* ---- Word rows ---- */
.list { display: flex; flex-direction: column; gap: 8px; }
.row { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.row-main { display: grid; grid-template-columns: minmax(110px, auto) 1fr auto; gap: 4px 16px; padding: 12px 14px; align-items: center; cursor: pointer; }
.row-main:hover { background: var(--surface-2); border-radius: var(--radius); }
.hanzi { font-family: var(--hanzi); font-size: 28px; line-height: 1.2; font-weight: 700; text-decoration: none; }
.hanzi small { display: block; font-family: var(--cjk); font-size: 13px; color: var(--muted); font-weight: 400; }
.readings { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.reading { display: flex; align-items: center; gap: 6px; font-size: 14px; flex-wrap: wrap; }
.reading .tag { font-size: 10px; font-weight: 700; color: var(--muted); width: 18px; font-family: var(--cjk); }
.defs { color: var(--text); font-size: 14px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.row-side { display: flex; align-items: center; gap: 6px; }
.badge { font-size: 11px; font-weight: 700; border-radius: 999px; padding: 2px 8px; white-space: nowrap; font-family: var(--cjk); }
.badge.oral { color: var(--oral); background: var(--oral-soft); }
.badge.written { color: var(--written); background: var(--written-soft); }
.badge.both { color: var(--both); background: var(--both-soft); }
.auto { font-size: 10px; color: var(--muted); border: 1px dashed var(--border); border-radius: 4px; padding: 0 4px; cursor: help; }
.t1 { color: var(--t1); } .t2 { color: var(--t2); } .t3 { color: var(--t3); } .t4 { color: var(--t4); } .t5 { color: var(--t5); } .t6 { color: var(--t6); }
.say { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; height: 26px; padding: 0 8px; display: inline-flex; align-items: center; gap: 4px; cursor: pointer; font-size: 12px; color: var(--muted); }
.say:hover { color: var(--accent); border-color: var(--accent); }
.say svg { width: 14px; height: 14px; fill: currentColor; }
.say.speaking { color: var(--on-accent); background: var(--accent); border-color: var(--accent); }


.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 7px 15px; cursor: pointer; font-weight: 700; text-decoration: none; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.btn.ink { background: var(--ink); color: var(--on-ink); border-color: var(--ink); }
.btn.danger { color: var(--accent); }

/* ---- Pagination ---- */
.pager { display: flex; gap: 6px; justify-content: center; align-items: center; margin: 20px 0 0; flex-wrap: wrap; }
.pager button { min-width: 36px; height: 36px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; padding: 0 10px; }
.pager button[aria-current="page"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.pager button:disabled { opacity: .4; cursor: default; }
.pager .gap { color: var(--muted); padding: 0 2px; }

/* ---- Detail pages ---- */
.detail-head { background: var(--surface); border: 1px solid var(--border); border-radius: 22px; padding: 22px; box-shadow: var(--shadow);
  display: grid; grid-template-columns: auto 1fr; gap: 8px 28px; align-items: center; }
.detail-hanzi { font-family: var(--hanzi); font-size: 72px; line-height: 1.1; font-weight: 700; letter-spacing: 2px; }
.detail-hanzi small { display: block; font-family: var(--cjk); font-size: 22px; color: var(--muted); font-weight: 400; letter-spacing: 0; }
.detail-read { display: flex; flex-direction: column; gap: 8px; }
.detail-read .reading { font-size: 20px; }
.detail-read .reading .tag { width: auto; font-size: 12px; min-width: 64px; }
.detail-meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 4px; }
.section { margin-top: 22px; }
.section h2 { font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin: 0 0 10px; }
.sounds-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sounds-head h2 { margin: 0; }
.sounds-reading { margin: 8px 0; color: var(--muted); font-size: 14px; }
.sounds .mini-list { margin-top: 8px; }
.meanings { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 18px 14px 36px; margin: 0; }
.meanings li { margin: 3px 0; }
.char-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.char-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; text-decoration: none; }
.char-card:hover { border-color: var(--muted); }
.char-card .big { font-family: var(--hanzi); font-weight: 700; font-size: 40px; line-height: 1; grid-row: span 3; }
.char-card .sub { font-size: 13px; color: var(--muted); }
.mini-list { display: flex; flex-wrap: wrap; gap: 8px; }
.mini { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 6px 10px; text-decoration: none; display: flex; flex-direction: column; min-width: 0; max-width: 100%; }
.mini:hover { border-color: var(--muted); }
.mini .w { font-family: var(--hanzi); font-weight: 700; font-size: 19px; }
.mini .g { font-size: 12px; color: var(--muted); max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 18px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty .cjk { font-family: var(--hanzi); font-weight: 700; font-size: 44px; display: block; margin-bottom: 8px; color: var(--border); }
.crumbs { margin: 0 0 12px; font-size: 13px; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--text); }

.char-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.char-chip { font-family: var(--hanzi); font-weight: 700; font-size: 24px; width: 48px; height: 48px; display: grid; place-items: center; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; text-decoration: none; }
.char-chip:hover { border-color: var(--accent); color: var(--accent); }

.prose { max-width: 720px; }
.prose h1 { font-size: 30px; font-weight: 800; letter-spacing: -.5px; margin: 4px 0 12px; }
.prose h2 { font-size: 18px; font-weight: 800; margin: 24px 0 8px; }
.prose p, .prose li { color: var(--text); }
.prose .muted { color: var(--muted); }
.prose code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
#privacy, #licences { scroll-margin-top: 76px; }
.follow h2 { margin-bottom: 4px; }
.follow p { display: flex; flex-wrap: wrap; gap: 4px; margin: 0; }
.follow a { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; color: var(--ink); text-decoration: none; }
.follow a:hover, .follow a:focus-visible { color: var(--accent); }
.follow a svg { width: 24px; height: 24px; fill: currentColor; display: block; }
.follow a svg.cup { fill: none; stroke: currentColor; stroke-width: 2.1; stroke-linecap: round; stroke-linejoin: round; }
.page-links { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin: 24px 0 12px; }
.page-links a { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px; text-decoration: none; color: var(--text);
  display: flex; flex-direction: column; gap: 2px; }
.page-links a:hover { border-color: var(--muted); }
.page-links a b::after { content: " →"; color: var(--muted); }
.page-links span { color: var(--muted); font-size: 13px; }
@media (max-width: 520px) { .page-links { grid-template-columns: 1fr; } }
.licence { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 14px 16px; margin: 12px 0; }
.licence h3 { margin: 0 0 6px; font-size: 16px; }
.licence-name { color: var(--oral); font-weight: 800; margin: 0 0 6px; }
.notice { white-space: pre-wrap; font: 12px/1.45 ui-monospace, SFMono-Regular, monospace; background: var(--surface-2); padding: 10px; border-radius: 8px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; max-width: min(92vw, 520px); opacity: 0; pointer-events: none; transition: .2s; z-index: 60; font-size: 14px; box-shadow: var(--shadow); }
.toast.show { opacity: 1; transform: translateX(-50%); }
.skeleton { height: 70px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: shimmer 1.2s infinite; border: 1px solid var(--border); }
@keyframes shimmer { to { background-position: -200% 0; } }

.debug-log { position: fixed; left: 8px; right: 8px; bottom: 8px; z-index: 70; margin: 0; max-height: 40vh; overflow: auto;
  background: rgb(0 0 0 / .82); color: #8ef58e; font: 11px/1.35 ui-monospace, monospace; padding: 8px; border-radius: 8px; pointer-events: none; white-space: pre-wrap; }

@media (max-width: 720px) {
  .topbar-inner { gap: 8px; }
  .nav { margin-left: auto; }
  .nav a { padding: 6px 8px; font-size: 14px; }
  /* Sort and Filters share one row. Nothing may run past the screen: a wider document makes mobile browsers zoom out. */
  .toolbar { flex-wrap: nowrap; gap: 8px; }
  .toolbar .group { flex-wrap: nowrap; min-width: 0; }
  .toolbar .select { min-width: 0; max-width: 46vw; }
  .chip, .select { white-space: nowrap; }
  .row-main { grid-template-columns: 1fr auto; }
  .row-main .readings { grid-column: 1 / -1; grid-row: 2; }
  .row-main .row-side { grid-column: 2; grid-row: 1; }
  .detail-head { grid-template-columns: 1fr; }
  .detail-hanzi { font-size: 52px; }
}
@media (max-width: 420px) {
  .topbar-inner { padding: 10px 12px; }
  .brand-mark { font-size: 21px; letter-spacing: 0; }
  .brand-name { display: none; }
  .nav { gap: 0; }
  .nav a { padding: 5px 6px; font-size: 13px; }
  .search select { max-width: 42%; }
}
@media (max-width: 350px) {
  .topbar-inner { padding: 10px 8px; gap: 4px 6px; }
  .brand-mark { font-size: 19px; }
  .nav a { padding: 5px 3px; font-size: 12.5px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ---- Home ---- */
.home { display: flex; flex-direction: column; gap: 26px; padding: 18px 0 8px; }
.features { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; max-width: 880px; }
.feature { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 18px 19px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.feature-hanzi { font-family: var(--hanzi); font-weight: 700; font-size: 54px; line-height: 1.1; letter-spacing: 3px; text-decoration: none; margin-bottom: 3px; }
.feature-hanzi .take { color: var(--accent); }
.feature .reading { font-size: 14px; font-weight: 600; }
.feature-def { margin: 3px 0 6px; font-size: 13px; }
.feature .btn { align-self: flex-start; padding: 6px 12px; font-size: 12px; }
.lede { margin: 0; color: var(--muted); font-size: 15px; line-height: 1.5; max-width: 68ch; }
/* theme.js sets this on a link to another view, so index.html's home sentence doesn't flash first. */
:root[data-deep-link] #main > .home { display: none; }
.formula { margin: 0 0 -12px; color: var(--muted); font-size: 15px; }
.formula .cjk { font-family: var(--hanzi); font-weight: 700; color: var(--accent); }
.formula b { color: var(--text); }
.try { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.try .chip { text-decoration: none; font-size: 14px; }
.try .chip small { color: var(--muted); font-weight: 500; margin-left: 4px; }
.recent-empty { flex-basis: 100%; margin: 0; color: var(--muted); font-size: 14px; }
@media (max-width: 720px) {
  .features { grid-template-columns: 1fr; }
  .feature-hanzi { font-size: 45px; }
}
