:root {
    color-scheme: light;
    --bg: #fffdf8;
    --surface: #ffffff;
    --surface-soft: #f8f7f3;
    --ink: #171717;
    --muted: #686866;
    --line: #e5e4df;
    --accent: #f04444;
    --accent-dark: #c92d36;
    --accent-soft: #fff0ef;
    --blue: #3568d4;
    --green: #168765;
    --shadow: 0 24px 70px rgba(32, 27, 20, .14);
    --font: "Noto Sans SC", sans-serif;
    --display: "Fredoka", "Noto Sans SC", sans-serif;
}

html[data-theme="dark"] {
    color-scheme: dark;
    --bg: #171716;
    --surface: #21211f;
    --surface-soft: #292926;
    --ink: #f7f5ee;
    --muted: #b8b6ae;
    --line: #3b3a36;
    --accent: #ff6969;
    --accent-dark: #ff8585;
    --accent-soft: #3b2222;
    --blue: #8aafff;
    --green: #5ad1aa;
    --shadow: 0 24px 70px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.5;
    letter-spacing: 0;
}
button, input { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
a { color: inherit; text-decoration: none; }

.skip-link { position: fixed; top: 8px; left: 8px; z-index: 100; padding: 10px 14px; background: var(--ink); color: var(--bg); transform: translateY(-150%); }
.skip-link:focus { transform: translateY(0); }

.site-header {
    position: sticky;
    top: 0;
    z-index: 20;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    min-height: 72px;
    padding: 0 clamp(20px, 5vw, 72px);
    border-bottom: 1px solid color-mix(in srgb, var(--line) 80%, transparent);
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(14px);
}
.brand { display: inline-flex; align-items: center; gap: 10px; width: max-content; font-weight: 700; }
.brand-mark { display: grid; place-items: center; width: 36px; height: 36px; border: 2px solid var(--ink); border-radius: 8px; font-family: serif; font-size: 19px; }
.header-nav { display: flex; align-items: center; gap: 28px; color: var(--muted); }
.header-nav a { position: relative; padding: 24px 0; }
.header-nav a.active { color: var(--ink); }
.header-nav a.active::after { content: ""; position: absolute; right: 0; bottom: -1px; left: 0; height: 2px; background: var(--accent); }
.theme-toggle { justify-self: end; }

.icon-button, .primary-button, .secondary-button, .text-button, .filter-button, .unit-tab {
    min-width: 44px;
    min-height: 44px;
    border: 0;
    cursor: pointer;
}
.icon-button { display: inline-grid; place-items: center; width: 44px; height: 44px; padding: 0; border-radius: 6px; background: transparent; }
.icon-button:hover { background: var(--surface-soft); }
.icon-button svg, button svg { width: 19px; height: 19px; stroke-width: 2; }
button:focus-visible, a:focus-visible, input:focus-visible { outline: 3px solid color-mix(in srgb, var(--blue) 65%, transparent); outline-offset: 3px; }

main { overflow: hidden; }
.intro {
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(280px, .9fr);
    align-items: center;
    min-height: 500px;
    padding: 72px max(6vw, 24px) 56px;
    border-bottom: 1px solid var(--line);
    background-image: radial-gradient(var(--line) 1px, transparent 1px);
    background-size: 24px 24px;
}
.intro::after { content: ""; position: absolute; inset: 0; pointer-events: none; background: linear-gradient(90deg, var(--bg) 18%, transparent 58%); }
.intro-copy { position: relative; z-index: 2; max-width: 720px; }
.eyebrow { margin: 0 0 12px; color: var(--accent-dark); font-size: 13px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.intro h1 { margin: 0; font-family: var(--display); font-size: clamp(52px, 7vw, 102px); font-weight: 600; line-height: .98; }
.intro h1 span { color: var(--accent); }
.intro-note { max-width: 510px; margin: 28px 0 0; color: var(--muted); font-size: 18px; }
.featured-glyph { position: relative; z-index: 1; display: grid; place-items: center; justify-self: center; width: min(34vw, 370px); aspect-ratio: 1; border: 2px solid var(--accent); background: color-mix(in srgb, var(--surface) 88%, transparent); }
.featured-glyph::before, .featured-glyph::after, .writer-board::before, .writer-board::after { content: ""; position: absolute; background: var(--accent); opacity: .28; }
.featured-glyph::before, .writer-board::before { top: 50%; right: 0; left: 0; height: 1px; }
.featured-glyph::after, .writer-board::after { top: 0; bottom: 0; left: 50%; width: 1px; }
.featured-glyph > span:first-child { font-family: "STKaiti", "KaiTi", serif; font-size: clamp(140px, 20vw, 250px); line-height: 1; }
.stroke { position: absolute; background: var(--accent); }
.stroke-one { top: 15%; right: -30px; width: 60px; height: 8px; }
.stroke-two { bottom: 16%; left: -16px; width: 9px; height: 54px; }

.workspace { width: min(1440px, 100%); margin: 0 auto; padding: 48px max(4vw, 20px) 72px; }
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; }
.search-box { display: flex; align-items: center; flex: 1; max-width: 640px; height: 54px; padding: 0 14px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); transition: border-color .2s, box-shadow .2s; }
.search-box:focus-within { border-color: var(--blue); box-shadow: 0 0 0 3px color-mix(in srgb, var(--blue) 16%, transparent); }
.search-box svg { flex: none; width: 20px; color: var(--muted); }
.search-box input { width: 100%; height: 100%; padding: 0 12px; border: 0; outline: 0; background: transparent; color: var(--ink); }
.search-box kbd { padding: 3px 7px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.view-switch { display: flex; gap: 4px; padding: 4px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface-soft); }
.filter-button { padding: 0 16px; border-radius: 5px; background: transparent; color: var(--muted); }
.filter-button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 4px rgba(0, 0, 0, .08); }
.unit-tabs { display: flex; gap: 8px; margin-top: 24px; overflow-x: auto; scrollbar-width: none; }
.unit-tab { flex: none; padding: 0 17px; border: 1px solid var(--line); border-radius: 999px; background: transparent; color: var(--muted); }
.unit-tab.active { border-color: var(--ink); background: var(--ink); color: var(--bg); }
.result-meta { display: flex; justify-content: space-between; margin: 42px 0 18px; color: var(--muted); font-size: 14px; }
.result-meta p { margin: 0; }
.result-meta strong { color: var(--ink); font-size: 18px; }

.character-grid, .loading-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); border-top: 1px solid var(--line); border-left: 1px solid var(--line); }
.character-card { position: relative; min-height: 152px; padding: 16px; border: 0; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); background: var(--surface); text-align: left; cursor: pointer; transition: background .2s, color .2s; }
.character-card:hover { z-index: 1; background: var(--accent); color: white; }
.character-card:hover .card-meta, .character-card:hover .card-badge { color: rgba(255,255,255,.82); }
.character-card:hover .card-arrow { opacity: 1; transform: translate(0, 0); }
.card-character { display: block; font-family: "STKaiti", "KaiTi", serif; font-size: 68px; line-height: 1.08; }
.card-meta { display: block; max-width: 92px; margin-top: 10px; overflow: hidden; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.card-badge { position: absolute; top: 12px; right: 12px; color: var(--accent-dark); }
.card-badge svg { width: 15px; height: 15px; }
.card-arrow { position: absolute; right: 12px; bottom: 14px; opacity: 0; transform: translate(-5px, 5px); transition: opacity .2s, transform .2s; }
.card-arrow svg { width: 18px; height: 18px; }
.character-card.practiced::after { content: ""; position: absolute; top: 0; right: 0; width: 0; height: 0; border-top: 18px solid var(--green); border-left: 18px solid transparent; }
.loading-grid span { min-height: 152px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); background: linear-gradient(105deg, var(--surface) 20%, var(--surface-soft) 40%, var(--surface) 60%); background-size: 240% 100%; animation: shimmer 1.2s infinite; }
.empty-state { padding: 72px 20px; border: 1px solid var(--line); text-align: center; }
.empty-state svg { width: 36px; height: 36px; color: var(--muted); }
.empty-state h2 { margin: 16px 0 4px; font-size: 20px; }
.empty-state p { margin: 0; color: var(--muted); }

.progress-panel { display: grid; grid-template-columns: 1fr auto; gap: 36px 64px; width: min(1320px, calc(100% - 40px)); margin: 0 auto 72px; padding: 42px 48px; border: 1px solid var(--line); border-left: 6px solid var(--accent); background: var(--surface); }
.progress-panel h2 { margin: 0; font-family: var(--display); font-size: 32px; }
.progress-stats { display: flex; gap: clamp(28px, 5vw, 72px); }
.progress-stats div { display: flex; flex-direction: column; }
.progress-stats strong { font-family: var(--display); font-size: 36px; line-height: 1; }
.progress-stats span, #progress-caption { color: var(--muted); font-size: 13px; }
.progress-track { grid-column: 1 / -1; height: 8px; overflow: hidden; border-radius: 99px; background: var(--surface-soft); }
.progress-track span { display: block; width: 0; height: 100%; background: var(--accent); transition: width .4s ease; }
#progress-caption { grid-column: 1 / -1; margin: -26px 0 0; text-align: right; }
footer { display: flex; justify-content: space-between; gap: 20px; padding: 28px max(5vw, 24px); border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }
footer p { margin: 0; }

.dialog { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 20px; }
.dialog[hidden] { display: none; }
.dialog-backdrop { position: absolute; inset: 0; background: rgba(10, 10, 9, .62); backdrop-filter: blur(5px); }
.dialog-panel { position: relative; width: min(940px, 100%); max-height: calc(100dvh - 40px); overflow: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow); animation: dialog-in .24s ease-out; }
.dialog-header { display: flex; align-items: center; justify-content: space-between; padding: 22px 26px; border-bottom: 1px solid var(--line); }
.dialog-header .eyebrow { margin-bottom: 3px; }
.dialog-header h2 { display: flex; align-items: baseline; gap: 12px; margin: 0; font-size: 18px; }
#dialog-character { font-family: "STKaiti", "KaiTi", serif; font-size: 38px; }
.dialog-header small { color: var(--muted); font-size: 14px; font-weight: 500; }
.practice-layout { display: grid; grid-template-columns: 1fr 1fr; }
.writer-section { padding: 28px 36px 32px; }
.writer-section + .writer-section { border-left: 1px solid var(--line); }
.section-heading { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 22px; }
.section-heading > div { display: flex; align-items: center; gap: 8px; }
.section-heading span { color: var(--accent-dark); font-family: var(--display); font-size: 13px; }
.section-heading h3 { margin: 0; font-size: 18px; }
.section-heading p { margin: 0; color: var(--muted); font-size: 12px; }
.writer-board { position: relative; width: min(100%, 320px); aspect-ratio: 1; margin: 0 auto; border: 2px solid var(--accent); background: var(--bg); touch-action: none; }
.writer-board svg { position: relative; z-index: 1; display: block; }
.quiz-board.is-success { border-color: var(--green); box-shadow: 0 0 0 4px color-mix(in srgb, var(--green) 15%, transparent); }
.control-row { display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 20px; }
.primary-button, .secondary-button, .text-button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 0 15px; border-radius: 6px; font-size: 14px; font-weight: 600; }
.primary-button { background: var(--ink); color: var(--bg); }
.primary-button:hover { background: var(--accent); color: white; }
.secondary-button { border: 1px solid var(--line); background: var(--surface); }
.secondary-button:hover, .text-button:hover { background: var(--surface-soft); }
.dialog-footer { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 16px 26px; border-top: 1px solid var(--line); }
.dialog-footer .text-button:first-child { justify-self: start; }
.dialog-footer .text-button:last-child { justify-self: end; }
.dialog-footer p { display: flex; align-items: center; gap: 10px; margin: 0; color: var(--muted); font-size: 13px; }
.dialog-footer small { padding: 3px 7px; border-radius: 3px; background: var(--accent-soft); color: var(--accent-dark); }
.toast { position: fixed; right: 24px; bottom: 24px; z-index: 70; max-width: calc(100% - 48px); padding: 12px 18px; border-radius: 6px; background: var(--ink); color: var(--bg); opacity: 0; pointer-events: none; transform: translateY(12px); transition: opacity .2s, transform .2s; }
.toast.show { opacity: 1; transform: translateY(0); }
body.dialog-open { overflow: hidden; }

@keyframes shimmer { to { background-position-x: -240%; } }
@keyframes dialog-in { from { opacity: 0; transform: translateY(12px) scale(.985); } }
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

@media (max-width: 760px) {
    .site-header { grid-template-columns: 1fr auto; min-height: 64px; }
    .header-nav { display: none; }
    .intro { grid-template-columns: 1fr; min-height: 470px; padding-top: 54px; }
    .intro h1 { font-size: clamp(40px, 11.5vw, 46px); line-height: 1.06; }
    .intro-note { max-width: 68%; font-size: 16px; }
    .featured-glyph { position: absolute; right: -96px; bottom: 22px; width: 210px; opacity: .18; }
    .toolbar { align-items: stretch; flex-direction: column; }
    .search-box { max-width: none; }
    .view-switch { display: grid; grid-template-columns: repeat(3, 1fr); }
    .result-meta { margin-top: 32px; }
    #active-description { display: none; }
    .character-grid, .loading-grid { grid-template-columns: repeat(3, 1fr); }
    .character-card, .loading-grid span { min-height: 126px; }
    .character-card { padding: 12px; }
    .card-character { font-size: 56px; }
    .progress-panel { grid-template-columns: 1fr; padding: 32px 24px; }
    .progress-stats { justify-content: space-between; gap: 12px; }
    .progress-stats strong { font-size: 30px; }
    footer { flex-direction: column; }
    .dialog { align-items: end; padding: 0; }
    .dialog-panel { width: 100%; max-height: 94dvh; border-radius: 8px 8px 0 0; }
    .dialog-header { padding: 16px 18px; }
    .practice-layout { grid-template-columns: 1fr; }
    .writer-section { padding: 22px 20px 26px; }
    .writer-section + .writer-section { border-top: 1px solid var(--line); border-left: 0; }
    .writer-board { width: min(76vw, 320px); }
    .dialog-footer { position: sticky; bottom: 0; z-index: 2; padding: 12px 14px; background: var(--surface); }
    .dialog-footer small { display: none; }
}

@media (max-width: 390px) {
    .brand > span:last-child { font-size: 14px; }
    .workspace { padding-right: 14px; padding-left: 14px; }
    .character-grid, .loading-grid { grid-template-columns: repeat(3, 1fr); }
    .character-card { min-height: 118px; }
    .card-character { font-size: 50px; }
    .card-meta { font-size: 11px; }
    .control-row { flex-wrap: wrap; }
}