:root {
    color-scheme: light;
    --bg: #f4efe2;
    --surface: #fffaf0;
    --surface-2: #e7f6f4;
    --text: #1f2933;
    --muted: #69717d;
    --line: #d7ccb8;
    --primary: #2f9f9a;
    --primary-strong: #167c78;
    --accent: #ffbf55;
    --danger: #b84848;
    --ok: #15803d;
    --shadow: 0 18px 45px rgba(61, 46, 20, .12);
}

body.dark {
    color-scheme: dark;
    --bg: #111827;
    --surface: #1e293b;
    --surface-2: #0f766e;
    --text: #f8fafc;
    --muted: #cbd5e1;
    --line: #334155;
    --primary: #5eead4;
    --primary-strong: #2dd4bf;
    --accent: #facc15;
    --danger: #f87171;
    --ok: #86efac;
    --shadow: 0 18px 45px rgba(0, 0, 0, .32);
}

* {
    box-sizing: border-box;
}

html {
    touch-action: manipulation;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: "DFKai-SB", "KaiTi", "STKaiti", "BiauKai", "Noto Serif TC", serif;
    touch-action: manipulation;
}

a {
    color: var(--primary-strong);
}

button,
input {
    font: inherit;
    touch-action: manipulation;
}

.ad-rail {
    position: fixed;
    top: 132px;
    z-index: 1;
    width: 160px;
    height: 600px;
}

.ad-rail-left {
    left: max(16px, calc((100vw - 1900px) / 2));
}

.ad-rail-right {
    right: max(16px, calc((100vw - 1900px) / 2));
}

.app-shell {
    width: min(1180px, 100%);
    margin: 0 auto;
    padding: 18px;
}

.site-header {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    padding: 14px clamp(18px, 4vw, 48px);
    border-bottom: 1px solid var(--line);
    background: color-mix(in srgb, var(--surface), white 58%);
    backdrop-filter: blur(10px);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.icon-button {
    display: grid;
    place-items: center;
    width: 46px;
    height: 46px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
    cursor: pointer;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
    color: var(--text);
    text-decoration: none;
}

.brand-mark {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    flex: 0 0 auto;
    border-radius: 6px;
    background: #087a7a;
    color: white;
    font-size: 24px;
    font-weight: 700;
}

.brand strong,
.brand small {
    display: block;
}

.brand strong {
    font-size: 1.08rem;
    line-height: 1.15;
    white-space: nowrap;
}

.brand small {
    margin-top: 3px;
    color: var(--muted);
    font-size: 13px;
    white-space: nowrap;
}

.top-nav {
    display: flex;
    align-items: center;
    gap: 18px;
    flex-wrap: wrap;
    justify-content: flex-end;
    color: var(--muted);
    font-size: 15px;
}

.top-nav a,
.nav-link {
    display: inline-grid;
    place-items: center;
    min-height: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--muted);
    text-decoration: none;
    font-weight: 800;
    cursor: pointer;
}

.top-nav a {
    font-weight: 700;
}

.top-nav a:hover,
.nav-link:hover {
    color: var(--primary-strong);
    text-decoration: underline;
}

.menu-toggle {
    display: none;
    width: 42px;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    place-items: center;
    gap: 4px;
    padding: 9px;
    cursor: pointer;
}

.menu-toggle span {
    display: block;
    width: 20px;
    height: 2px;
    border-radius: 999px;
    background: currentColor;
}

.theme-toggle {
    position: relative;
    display: grid;
    place-items: center;
    width: 42px;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0;
    color: var(--muted);
    background: var(--surface);
    box-shadow: none;
    cursor: pointer;
    font-size: 26px;
    line-height: 1;
}

.theme-toggle:hover {
    border-color: var(--primary);
    color: var(--primary-strong);
    background: color-mix(in srgb, var(--surface), var(--primary) 6%);
}

.hint-button {
    font-family: Arial, Helvetica, sans-serif;
    font-weight: 900;
}

.hint-button.hint-off {
    color: var(--danger);
    opacity: .65;
    text-decoration: line-through;
}

.sound-button {
    position: relative;
}

.sound-note {
    display: block;
}

.sound-muted-mark {
    position: absolute;
    inset: 0;
    display: none;
    place-items: center;
    color: var(--danger);
    font-family: Arial, Helvetica, sans-serif;
    font-size: 2.1rem;
    font-weight: 800;
    line-height: 1;
}

.sound-button.sound-off .sound-muted-mark {
    display: grid;
}

.sound-button.sound-off .sound-note {
    opacity: .5;
}

.site-header .hidden {
    display: none !important;
}

.hidden {
    display: none !important;
}

.group-grid {
    display: grid;
    gap: 12px;
    max-width: 680px;
    margin: 18px auto 0;
}

.group-button {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 62px;
    padding: 12px 20px;
    border: 1px solid color-mix(in srgb, var(--primary), white 35%);
    border-radius: 8px;
    background: var(--surface-2);
    color: var(--text);
    font-size: clamp(1.45rem, 5vw, 2.4rem);
    text-align: left;
    cursor: pointer;
    box-shadow: var(--shadow);
}

.group-button small {
    justify-self: end;
    white-space: nowrap;
    font-size: 1rem;
    color: var(--muted);
}

.game-view {
    display: grid;
    grid-template-columns: minmax(150px, 210px) 1fr minmax(150px, 210px);
    gap: 14px;
}

.side-panel,
.play-panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.side-panel {
    min-height: 620px;
    padding: 12px;
    overflow: hidden;
}

.panel-title {
    position: sticky;
    top: 0;
    padding-bottom: 8px;
    color: var(--muted);
    font-weight: 700;
}

.history-list,
.next-list {
    display: grid;
    align-content: start;
    gap: 8px;
    max-height: 570px;
    overflow: auto;
}

.history-item,
.next-item {
    display: grid;
    align-items: center;
    min-height: 38px;
    padding: 7px 9px;
    border-radius: 8px;
    background: color-mix(in srgb, var(--surface), var(--primary) 9%);
    font-size: 1.15rem;
}

.history-item {
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 8px;
}

.next-item {
    justify-content: center;
}

.history-roots {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--muted);
    font-family: Arial, Helvetica, sans-serif;
    font-size: .95rem;
    font-weight: 800;
}

.history-tick {
    color: var(--ok);
    font-weight: 900;
}

.play-panel {
    padding: 14px;
}

.score-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    color: var(--muted);
    font-weight: 700;
}

.score-row strong {
    color: var(--text);
    font-size: 1.15rem;
}

.word-stage {
    display: grid;
    justify-items: center;
    gap: 6px;
    padding: 10px 0 4px;
}

.word-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    width: 100%;
}

.target-word {
    display: grid;
    place-items: center;
    width: min(220px, 54vw);
    aspect-ratio: 1;
    border: 2px solid var(--primary);
    border-radius: 8px;
    background: #fff;
    color: #132230;
    font-size: clamp(6rem, 18vw, 10rem);
    line-height: 1;
    font-family: "DFKai-SB", "KaiTi", "STKaiti", "BiauKai", "Noto Serif TC", serif;
    font-weight: 500;
    overflow: hidden;
}

.target-word.showing-answer {
    grid-template-rows: auto minmax(0, 1fr);
    align-content: center;
    gap: 6px;
    padding: 8px;
    font-size: 1rem;
}

.target-answer-slots {
    display: flex;
    justify-content: center;
    gap: 4px;
    width: 100%;
    min-width: 0;
}

.target-answer-slots .hint-slot {
    flex: 1 1 0;
    min-width: 0;
    max-width: 48px;
    height: clamp(48px, 8vw, 62px);
    font-size: clamp(1rem, 3.2vw, 1.45rem);
}

.target-answer-image {
    display: block;
    max-width: 100%;
    min-height: 0;
    width: auto;
    height: 100%;
    object-fit: contain;
    background: #fff;
}

.mobile-next-panel {
    display: none;
}

.input-track {
    display: flex;
    justify-content: center;
    gap: clamp(6px, 1.8vw, 10px);
    min-height: 64px;
    width: 100%;
}

.plain-input {
    min-height: 42px;
    color: var(--primary-strong);
    font-family: Arial, Helvetica, sans-serif;
    font-size: clamp(1.55rem, 4vw, 2.2rem);
    font-weight: 900;
    letter-spacing: 0;
}

.hint-slot {
    display: grid;
    place-items: center;
    width: clamp(44px, 9vw, 58px);
    height: clamp(56px, 11vw, 72px);
    border-radius: 8px;
    color: #101820;
    font-size: clamp(1.75rem, 6vw, 2.55rem);
    line-height: 1;
    box-shadow: inset 0 -8px 18px rgba(0, 0, 0, .08), 0 8px 18px rgba(0, 0, 0, .08);
}

.hint-slot.filled {
    grid-template-rows: 1fr 1fr;
    padding: 6px 0;
    font-size: clamp(1.25rem, 4.5vw, 1.85rem);
    font-family: "DFKai-SB", "KaiTi", "STKaiti", "BiauKai", "Noto Serif TC", serif;
}

.hint-slot strong {
    font-family: Arial, Helvetica, sans-serif;
    font-size: .92em;
}

.slot-1 {
    background: #ff7246;
}

.slot-2 {
    background: #2f86ff;
}

.slot-3 {
    background: #71bd86;
}

.slot-4 {
    background: #e5b760;
}

.slot-5 {
    background: #b76ee5;
}

.feedback {
    min-height: 34px;
    color: var(--danger);
    font-size: clamp(1.35rem, 3.2vw, 1.9rem);
    font-weight: 700;
}

.feedback.ok {
    color: var(--ok);
}

.answer-panel {
    display: grid;
    gap: 12px;
    margin: 0 auto 16px;
    padding: 12px;
    max-width: 520px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: color-mix(in srgb, var(--surface), var(--primary) 7%);
}

.answer-line {
    display: grid;
    justify-items: center;
    gap: 12px;
    font-size: 1.2rem;
}

#answerRoots {
    display: flex;
    justify-content: center;
    gap: clamp(8px, 2.2vw, 14px);
    width: 100%;
}

#answerRoots .hint-slot {
    width: clamp(48px, 10vw, 68px);
    height: clamp(66px, 13vw, 88px);
}

.answer-panel img {
    display: block;
    max-width: 100%;
    max-height: 320px;
    margin: 0 auto;
    object-fit: contain;
    background: white;
    border-radius: 6px;
}

.keyboard {
    display: grid;
    gap: 7px;
    margin-top: 0;
    width: 100%;
    overflow: hidden;
}

.key-row {
    display: flex;
    justify-content: center;
    gap: 8px;
    width: 100%;
    max-width: 720px;
    margin: 0 auto;
}

.key-row.home-row {
    padding-left: 4%;
    padding-right: 4%;
}

.key-row.bottom-row {
    padding-left: 12%;
    padding-right: 12%;
}

.action-row {
    max-width: 440px;
    margin-top: 4px;
}

.key {
    display: grid;
    place-items: center;
    gap: 1px;
    flex: 1 1 0;
    min-width: 0;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    font-weight: 800;
    cursor: pointer;
    font-family: Arial, Helvetica, sans-serif;
}

.key small {
    font-size: .78rem;
    color: var(--muted);
    font-weight: 700;
    font-family: "DFKai-SB", "KaiTi", "STKaiti", "BiauKai", "Noto Serif TC", serif;
}

.key.action {
    background: var(--primary);
    color: #06201e;
}

.site-footer {
    margin: 24px auto 0;
    padding: 18px 8px 8px;
    max-width: 860px;
    color: var(--muted);
    text-align: center;
    line-height: 1.8;
    font-size: 1rem;
}

.site-footer a {
    font-weight: 800;
    text-decoration: none;
}

.site-footer a:hover {
    text-decoration: underline;
}

@media (max-width: 1900px) {
    .ad-rail {
        display: none;
    }
}

@media (max-width: 1180px) {
    .game-view {
        grid-template-columns: 1fr;
    }

    .side-panel {
        min-height: 0;
    }

    .history-panel {
        order: 2;
    }

    .play-panel {
        order: 1;
    }

    .next-panel {
        display: none;
    }

    .word-row {
        display: grid;
        grid-template-columns: minmax(0, auto) 58px;
        align-items: stretch;
        justify-content: center;
        gap: 10px;
        max-width: 100%;
    }

    .target-word {
        justify-self: center;
    }

    .mobile-next-panel {
        display: grid;
        align-content: start;
        gap: 6px;
        min-width: 0;
    }

    .mobile-next-panel .panel-title {
        position: static;
        padding: 0;
        font-size: 1rem;
        line-height: 1.15;
        text-align: center;
    }

    .mobile-next-list {
        display: grid;
        gap: 5px;
    }

    .mobile-next-list .next-item {
        display: grid;
        place-items: center;
        text-align: center;
        min-height: 42px;
        padding: 0;
        font-size: 1.55rem;
    }
}

@media (max-width: 820px) {
    .app-shell {
        padding: 12px;
    }

    .site-header {
        position: static;
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        padding: 12px;
        gap: 10px;
        align-items: center;
    }

    .menu-toggle {
        display: grid;
    }

    .brand-mark {
        width: 46px;
        height: 46px;
        font-size: 1.7rem;
    }

    .brand strong {
        font-size: clamp(1.02rem, 4vw, 1.35rem);
    }

    .brand small {
        font-size: clamp(.78rem, 3vw, .95rem);
    }

    .top-nav {
        display: none;
        grid-column: 1 / -1;
        width: 100%;
        justify-content: flex-start;
        gap: 10px;
        padding-top: 8px;
        border-top: 1px solid var(--line);
    }

    .site-header.menu-open .top-nav {
        display: flex;
    }

    .nav-link {
        min-height: 32px;
        padding: 0 4px;
        font-size: .92rem;
    }

    .play-panel {
        padding: 10px;
    }

    .history-list,
    .next-list {
        grid-template-columns: repeat(auto-fill, minmax(58px, 1fr));
        max-height: 128px;
    }

    .history-list {
        grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
    }

    .word-stage {
        padding-top: 6px;
    }

    .word-row {
        grid-template-columns: minmax(0, 1fr) 58px;
        justify-content: center;
    }

    .target-word {
        width: 100%;
        max-width: 235px;
        font-size: clamp(6rem, 25vw, 10rem);
        justify-self: center;
    }

    .target-word.showing-answer {
        padding: 8px;
        gap: 6px;
    }

    .target-answer-slots {
        gap: 4px;
    }

    .target-answer-slots .hint-slot {
        max-width: none;
        height: clamp(40px, 10vw, 54px);
        font-size: clamp(.88rem, 3.8vw, 1.25rem);
    }

    .group-button {
        grid-template-columns: auto minmax(0, 1fr) auto;
        min-height: 64px;
        padding: 10px 14px;
        font-size: clamp(1.28rem, 5vw, 1.8rem);
    }

    .group-button small {
        grid-column: auto;
        font-size: clamp(.9rem, 3.8vw, 1.05rem);
    }

    .keyboard {
        gap: 7px;
        margin: 0 -2px 0;
    }

    .key-row {
        gap: 4px;
        max-width: 100%;
    }

    .key-row.home-row {
        padding-left: 5%;
        padding-right: 5%;
    }

    .key-row.bottom-row {
        padding-left: 14%;
        padding-right: 14%;
    }

    .key {
        min-height: 46px;
        border-radius: 7px;
        font-size: clamp(.78rem, 3.2vw, 1.05rem);
    }

    .key small {
        font-size: clamp(.66rem, 2.8vw, .88rem);
    }

    .action-row {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 8px;
        max-width: none;
        padding: 0;
    }

    .action-row .key {
        min-height: 48px;
        font-size: 1.05rem;
    }
}

@media (max-width: 420px) {
    .app-shell {
        padding: 8px;
    }

    .site-header {
        grid-template-columns: minmax(0, 1fr) auto;
        gap: 8px;
    }

    .brand {
        gap: 8px;
    }

    .brand-mark {
        width: 38px;
        height: 38px;
        font-size: 1.42rem;
    }

    .theme-toggle {
        width: 38px;
        min-height: 38px;
        font-size: 23px;
    }

    .top-nav {
        grid-column: 1 / -1;
        justify-content: flex-start;
    }

    .nav-link {
        font-size: .85rem;
    }

    .target-word {
        max-width: 220px;
    }
}
