/* ============ tokens ============ */
:root {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --surface-2: #f1f3f6;
    --surface-3: #e8ebf0;
    --border: #e3e6eb;
    --border-strong: #cfd4dc;
    --text: #15181d;
    --text-2: #4b5360;
    --text-3: #8a93a1;
    --accent: #e5484d;
    --accent-hover: #d93d42;
    --accent-soft: #feebec;
    --accent-text: #ffffff;
    --ok: #18794e;
    --ok-soft: #e6f6eb;
    --warn: #ad5700;
    --warn-soft: #fff1e0;
    --info: #0b68cb;
    --info-soft: #e6f1fd;
    --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
    --shadow-md: 0 8px 24px rgba(16, 24, 40, .10), 0 2px 6px rgba(16, 24, 40, .06);
    --shadow-lg: 0 24px 60px rgba(16, 24, 40, .22);
    --radius: 12px;
    --radius-sm: 8px;
    --font: "Inter", "Segoe UI Variable", "Segoe UI", system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
    --font-zh: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", "Source Han Sans SC", "Segoe UI", sans-serif;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0e1013;
        --surface: #16191e;
        --surface-2: #1c2026;
        --surface-3: #252a32;
        --border: #262b33;
        --border-strong: #353c46;
        --text: #eceef1;
        --text-2: #b0b7c3;
        --text-3: #6f7886;
        --accent: #ff6369;
        --accent-hover: #ff7a7f;
        --accent-soft: #3b1619;
        --accent-text: #1a0607;
        --ok: #4cc38a;
        --ok-soft: #12261b;
        --warn: #ffa057;
        --warn-soft: #2f1b0b;
        --info: #70b8ff;
        --info-soft: #0f2238;
        --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
        --shadow-md: 0 8px 24px rgba(0, 0, 0, .45);
        --shadow-lg: 0 24px 60px rgba(0, 0, 0, .6);
        color-scheme: dark;
    }
}

:root[data-theme="dark"] {
    --bg: #0e1013;
    --surface: #16191e;
    --surface-2: #1c2026;
    --surface-3: #252a32;
    --border: #262b33;
    --border-strong: #353c46;
    --text: #eceef1;
    --text-2: #b0b7c3;
    --text-3: #6f7886;
    --accent: #ff6369;
    --accent-hover: #ff7a7f;
    --accent-soft: #3b1619;
    --accent-text: #1a0607;
    --ok: #4cc38a;
    --ok-soft: #12261b;
    --warn: #ffa057;
    --warn-soft: #2f1b0b;
    --info: #70b8ff;
    --info-soft: #0f2238;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, .45);
    --shadow-lg: 0 24px 60px rgba(0, 0, 0, .6);
    color-scheme: dark;
}

/* ============ base ============ */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.5 var(--font);
    -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h2, h3, h4 { margin: 0; letter-spacing: -0.01em; }
.muted { color: var(--text-3); }
.icon { flex: none; display: block; }
.hidden-file { display: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ============ layout ============ */
.app {
    display: grid;
    grid-template-columns: 272px 1fr;
    height: 100vh;
    height: 100dvh;
}
.sidebar {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px 12px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    min-height: 0;
}
.main {
    overflow: auto;
    padding: 24px clamp(16px, 3vw, 40px) 64px;
    min-width: 0;
}
.scrim { display: none; }
.only-mobile { display: none !important; }

/* ============ sidebar ============ */
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 8px; }
.brand-mark {
    width: 32px; height: 32px; border-radius: 9px;
    display: grid; place-items: center;
    background: var(--accent); color: #fff;
    font: 700 18px var(--font-zh);
    box-shadow: 0 4px 12px color-mix(in srgb, var(--accent) 35%, transparent);
}
.brand-name { font-weight: 700; font-size: 16px; flex: 1; letter-spacing: -0.02em; }

.nb-list { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; padding: 0 4px; }
.nb-item {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 10px; border: 0; border-radius: var(--radius-sm);
    background: transparent; cursor: pointer; text-align: left;
    color: var(--text-2);
}
.nb-item:hover { background: var(--surface-2); color: var(--text); }
.nb-item.active { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.nb-emoji { font-size: 17px; width: 22px; text-align: center; }
.nb-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nb-count { font-size: 12px; color: var(--text-3); font-variant-numeric: tabular-nums; }

.sidebar-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding-top: 12px; border-top: 1px solid var(--border); }
.foot-actions { display: flex; gap: 2px; }

.seg { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-2); border-radius: 10px; border: 1px solid var(--border); }
.seg button { border: 0; background: transparent; padding: 5px 8px; border-radius: 7px; cursor: pointer; color: var(--text-3); }
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

/* ============ controls ============ */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 7px;
    height: 36px; padding: 0 14px;
    border: 1px solid var(--border); border-radius: 10px;
    background: var(--surface); color: var(--text);
    font-weight: 550; cursor: pointer; white-space: nowrap;
    box-shadow: var(--shadow-sm);
    transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-block { width: 100%; }

.icon-btn {
    display: inline-grid; place-items: center;
    width: 32px; height: 32px; border: 0; border-radius: 8px;
    background: transparent; color: var(--text-3); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { color: var(--accent); background: var(--accent-soft); }

.chip {
    display: inline-flex; align-items: center; gap: 6px;
    height: 30px; padding: 0 11px; border-radius: 999px;
    border: 1px solid var(--border); background: var(--surface);
    color: var(--text-2); font-size: 13px; cursor: pointer; white-space: nowrap;
}
.chip:hover { border-color: var(--border-strong); color: var(--text); }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }

.search { display: flex; align-items: center; gap: 8px; padding: 0 10px; height: 34px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); color: var(--text-3); }
.search:focus-within { border-color: var(--accent); background: var(--surface); }
.search input { border: 0; outline: 0; background: transparent; width: 100%; min-width: 0; color: var(--text); }
.search.small { height: 30px; font-size: 13px; }
.grow { flex: 1; min-width: 140px; }

/* ============ header ============ */
.nb-header { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.emoji-btn { font-size: 30px; line-height: 1; border: 0; background: var(--surface); border: 1px solid var(--border); width: 52px; height: 52px; border-radius: 14px; cursor: pointer; box-shadow: var(--shadow-sm); }
.emoji-btn:hover { transform: rotate(-6deg); }
.title-wrap { flex: 1; min-width: 200px; }
.nb-title {
    width: 100%; border: 0; outline: 0; background: transparent;
    font-size: 26px; font-weight: 750; letter-spacing: -0.03em; padding: 2px 6px; margin-left: -6px; border-radius: 8px;
}
.nb-title:hover { background: var(--surface-2); }
.nb-title:focus { background: var(--surface); box-shadow: 0 0 0 2px var(--accent); }
.nb-meta { color: var(--text-3); font-size: 13px; }
.header-actions { display: flex; gap: 8px; align-items: center; }

/* ============ composer ============ */
.composer {
    background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
    padding: 14px; margin-bottom: 18px; box-shadow: var(--shadow-sm);
    position: sticky; top: -24px; z-index: 5;
}
.composer-input { display: flex; gap: 10px; }
.hanzi-input {
    flex: 1; min-width: 0; height: 48px; padding: 0 16px;
    border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2);
    font: 500 20px var(--font-zh); outline: 0;
}
.hanzi-input:focus { border-color: var(--accent); background: var(--surface); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 15%, transparent); }
.hanzi-input::placeholder { font-size: 16px; color: var(--text-3); font-family: var(--font); }
.composer .btn-primary { height: 48px; padding: 0 18px; }
.composer-tools { display: flex; gap: 8px; align-items: center; margin-top: 12px; flex-wrap: wrap; }
.divider { width: 1px; height: 20px; background: var(--border); }
.note { margin-top: 10px; padding: 8px 12px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); font-size: 13px; }

/* ============ sheet ============ */
.sheet { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; overflow: hidden; box-shadow: var(--shadow-sm); }
.row {
    display: grid;
    grid-template-columns: 44px minmax(140px, 1.1fr) minmax(150px, 1.2fr) minmax(180px, 1.6fr) 112px;
    align-items: center;
    border-top: 1px solid var(--border);
    min-height: 48px;
    padding: 4px 0;
}
.row:first-child { border-top: 0; }
.row.head { background: var(--surface-2); font-size: 12px; font-weight: 650; color: var(--text-3); text-transform: uppercase; letter-spacing: .06em; min-height: 38px; }
.row.head .c-hanzi { font: 650 13px var(--font-zh); letter-spacing: .1em; }
.row:not(.head):hover { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
.row.busy { background: linear-gradient(90deg, transparent, var(--accent-soft), transparent); background-size: 200% 100%; animation: shimmer 1.2s linear infinite; }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }
.row > div { position: relative; padding: 0 6px; min-width: 0; }
.c-num { color: var(--text-3); font-size: 12px; text-align: right; padding-right: 10px !important; font-variant-numeric: tabular-nums; }
.c-act { display: flex; justify-content: flex-end; align-items: center; gap: 0; padding-right: 8px !important; }
.row:not(:hover) .c-act .icon-btn { opacity: .45; }

.cell {
    display: block; width: 100%; min-height: 36px; padding: 7px 10px; border: 1px solid transparent; border-radius: 8px;
    background: transparent; outline: 0; resize: none; overflow: hidden; line-height: 1.45;
    field-sizing: content; /* grows with long entries (Chrome/Edge); others stay 1 line */
}
.cell:hover { border-color: var(--border); }
.cell:focus { border-color: var(--accent); background: var(--surface); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 14%, transparent); }
.cell.hanzi { font: 500 19px/1.35 var(--font-zh); padding-top: 5px; padding-bottom: 5px; }
.cell.pinyin { color: var(--text-2); font-size: 15px; }
.cell.english { font-size: 14px; padding-right: 34px; }
.c-pinyin .cell.pinyin { padding-right: 30px; }

.tag {
    position: absolute; right: 12px; top: 10px;
    display: inline-grid; place-items: center; min-width: 20px; height: 18px; padding: 0 4px;
    border-radius: 5px; font-size: 10px; font-weight: 700; color: var(--text-3); background: var(--surface-2);
    pointer-events: auto; cursor: help;
}
.tag.warn { color: var(--warn); background: var(--warn-soft); font-size: 13px; }
.tag.info { color: var(--info); background: var(--info-soft); }

/* study mode: blur until focused */
.mask-pinyin .cell.pinyin:not(:focus), .mask-english .cell.english:not(:focus) { filter: blur(6px); color: var(--text-3); }
.mask-pinyin .cell.pinyin:not(:focus):hover, .mask-english .cell.english:not(:focus):hover { filter: blur(3px); }

/* ============ empty / boot ============ */
.empty { text-align: center; padding: 80px 20px; color: var(--text-2); display: grid; justify-items: center; gap: 12px; }
.empty.small { padding: 56px 20px; background: var(--surface); border: 1px dashed var(--border-strong); border-radius: 16px; }
.empty p { max-width: 420px; margin: 0; color: var(--text-3); }
.empty-mark { font-size: 44px; }
.boot { height: 100vh; display: grid; place-content: center; justify-items: center; gap: 14px; color: var(--text-3); }

.spinner { width: 28px; height: 28px; border: 3px solid var(--surface-3); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
.spinner.sm { width: 14px; height: 14px; border-width: 2px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ modal ============ */
.modal-backdrop {
    position: fixed; inset: 0; z-index: 50;
    background: rgba(8, 10, 14, .45); backdrop-filter: blur(4px);
    display: grid; place-items: center; padding: 16px;
    animation: fade .15s ease-out;
}
.modal {
    width: min(480px, 100%); max-height: calc(100vh - 32px); overflow: auto;
    background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
    padding: 22px; box-shadow: var(--shadow-lg);
    display: flex; flex-direction: column; gap: 14px;
    animation: pop .18s ease-out;
}
.modal.wide { width: min(640px, 100%); }
.modal p { margin: 0; }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal h4 { font-size: 13px; color: var(--text-3); text-transform: uppercase; letter-spacing: .06em; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }

.bulk { width: 100%; resize: vertical; padding: 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); font: 18px/1.6 var(--font-zh); outline: 0; }
.bulk:focus { border-color: var(--accent); background: var(--surface); }

.choice-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choice { display: flex; gap: 10px; padding: 14px; border: 1px solid var(--border); border-radius: 14px; cursor: pointer; background: var(--surface-2); }
.choice.on { border-color: var(--accent); background: var(--accent-soft); }
.choice input { accent-color: var(--accent); margin-top: 3px; }
.choice p { font-size: 13px; color: var(--text-2); margin-top: 6px; }
.choice strong { margin-right: 6px; }
.badge { display: inline-block; font-size: 11px; font-weight: 650; padding: 1px 7px; border-radius: 999px; background: var(--surface-3); color: var(--text-2); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.toggle { display: flex; align-items: center; gap: 10px; cursor: pointer; flex-wrap: wrap; }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-weight: 600; font-size: 13px; display: flex; justify-content: space-between; }
.field label a { font-weight: 500; }
.field input { height: 38px; padding: 0 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); outline: 0; }
.field input:focus { border-color: var(--accent); background: var(--surface); }
.fineprint { font-size: 12px; color: var(--text-3); border-top: 1px solid var(--border); padding-top: 12px; }

/* ============ toast ============ */
.toast {
    position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60;
    background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
    box-shadow: var(--shadow-md); font-weight: 550; max-width: calc(100vw - 32px);
    animation: pop .18s ease-out;
}
.toast.error { background: var(--accent); color: #fff; }

/* ============ error ui ============ */
#blazor-error-ui {
    display: none; position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;
    padding: 12px 20px; background: var(--warn-soft); color: var(--warn); box-shadow: var(--shadow-md);
}
#blazor-error-ui .dismiss { cursor: pointer; position: absolute; right: 16px; top: 10px; }

/* ============ responsive ============ */
@media (max-width: 900px) {
    .row { grid-template-columns: 34px 1fr 1fr 1.3fr 104px; }
    .hide-sm { display: none; }
    .header-actions .btn { padding: 0 10px; }
}

@media (max-width: 720px) {
    .app { grid-template-columns: 1fr; }
    .only-mobile { display: inline-grid !important; }
    .sidebar {
        position: fixed; inset: 0 auto 0 0; width: min(300px, 86vw); z-index: 40;
        transform: translateX(-102%); transition: transform .22s ease; box-shadow: var(--shadow-lg);
    }
    .sidebar-open .sidebar { transform: none; }
    .sidebar-open .scrim { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .4); }
    .main { padding: 14px 16px 64px; }
    .nb-title { font-size: 21px; }
    .emoji-btn { width: 42px; height: 42px; font-size: 24px; }
    .composer { top: -14px; }
    .choice-grid { grid-template-columns: 1fr; }

    /* stack rows as cards */
    .row.head { display: none; }
    .row {
        grid-template-columns: 1fr auto;
        grid-template-areas: "hanzi act" "pinyin pinyin" "english english";
        padding: 8px 6px; gap: 2px;
    }
    .c-num { display: none; }
    .c-hanzi { grid-area: hanzi; }
    .c-pinyin { grid-area: pinyin; }
    .c-english { grid-area: english; }
    .c-act { grid-area: act; }
    .row:not(:hover) .c-act .icon-btn { opacity: .7; }
}

.test-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.test-result { font-size: 13px; padding: 4px 10px; border-radius: 8px; }
.test-result.ok { background: var(--ok-soft); color: var(--ok); }
.test-result.bad { background: var(--accent-soft); color: var(--accent); }

/* ============ sharing ============ */
.join { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.join-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 28px; box-shadow: var(--shadow-md); display: grid; gap: 14px; justify-items: start; }
.join-card .hanzi-input { width: 100%; font-size: 15px; font-family: var(--font); }
.join-card p { margin: 0; }
.brand-mark.big { width: 48px; height: 48px; font-size: 26px; border-radius: 13px; }

.people { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 4px 0; border-top: 1px solid var(--border); }
.sync-state { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sync-state .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); flex: none; }
.sync-state.online .dot { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.sync-state.online { color: var(--ok); font-weight: 600; }
.sync-state.connecting .dot { background: var(--warn); animation: pulse 1s infinite alternate; }
.sync-state.offline .dot { background: var(--accent); }
@keyframes pulse { to { opacity: .3; } }
.avatars { display: flex; }
.avatar { width: 28px; height: 28px; border-radius: 50%; display: inline-grid; place-items: center; flex: none;
    font-size: 11px; font-weight: 700; background: var(--info-soft); color: var(--info); border: 2px solid var(--surface); margin-left: -6px; }
.avatar.me { background: var(--accent-soft); color: var(--accent); }
.avatars .avatar:first-child { margin-left: 0; }

.row.flash { animation: flash 2.5s ease-out; }
@keyframes flash { 0%, 30% { background: color-mix(in srgb, var(--info) 22%, transparent); } 100% { background: transparent; } }

.invite-new { display: flex; gap: 8px; }
.invite-new input { flex: 1; min-width: 0; height: 36px; padding: 0 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); outline: 0; }
.invite-new input:focus { border-color: var(--accent); background: var(--surface); }
.member-list { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
.member { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-top: 1px solid var(--border); }
.member:first-child { border-top: 0; }
.member .avatar { margin: 0; width: 34px; height: 34px; }
.member-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.member-info small { color: var(--text-3); }
.account-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
@media (max-width: 720px) { .invite-new { flex-direction: column; } }
