:root {
    --ink: #14213d;
    --ink-2: #33415c;
    --muted: #64708a;
    --line: #dde2ea;
    --line-2: #eef1f5;
    --bg: #f6f7f9;
    --surface: #ffffff;
    --accent: #1d4ed8;
    --accent-ink: #ffffff;
    --accent-soft: #e8eefc;
    --mark: #fff1c2;
    --mark-strong: #ffd666;
    --saved: #0f766e;
    --saved-soft: #e2f3f1;
    --danger: #b42318;
    --danger-soft: #fdecea;
    --radius: 8px;
    --shadow: 0 1px 2px rgba(20, 33, 61, .06), 0 2px 8px rgba(20, 33, 61, .04);
    --sans: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: var(--sans);
    font-size: 15px;
    line-height: 1.55;
    color: var(--ink);
    background: var(--bg);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .4em; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 26px; }
h2 { font-size: 18px; }
h3 { font-size: 16px; }
p { margin: 0 0 1em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
[hidden] { display: none !important; }

.container { width: 100%; max-width: 1180px; margin: 0 auto; padding: 0 16px; }
.main { flex: 1; padding-top: 28px; padding-bottom: 48px; }

/* ---------- Top bar ---------- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.topbar-inner { display: flex; align-items: center; gap: 24px; height: 60px; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none !important; }
.brand-mark {
    width: 30px; height: 30px; border-radius: 6px; background: var(--ink); color: #fff;
    display: grid; place-items: center; font-family: var(--serif); font-weight: 600; font-size: 17px;
}
.brand-name { font-weight: 600; font-size: 17px; letter-spacing: -.01em; }
.mainnav { display: flex; gap: 4px; flex: 1; }
.mainnav a {
    color: var(--ink-2); padding: 8px 12px; border-radius: 6px; font-weight: 500; font-size: 14px;
    text-decoration: none;
}
.mainnav a:hover { background: var(--line-2); }
.mainnav a.active { color: var(--accent); background: var(--accent-soft); }
.account { display: flex; align-items: center; gap: 14px; margin-left: auto; }
.account form { margin: 0; }
.account-name { color: var(--muted); font-size: 14px; }

/* ---------- Buttons & forms ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    font: inherit; font-weight: 500; font-size: 14px;
    padding: 9px 16px; border-radius: 6px; border: 1px solid var(--accent);
    background: var(--accent); color: var(--accent-ink); cursor: pointer;
    text-decoration: none !important; transition: background .15s, border-color .15s;
}
.btn:hover { background: #1a43bd; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn-ghost:hover { background: var(--line-2); }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-lg { padding: 12px 22px; font-size: 15px; }
.btn-block { width: 100%; }
.btn-saved { background: var(--saved); border-color: var(--saved); }
.btn-saved:hover { background: var(--saved); }
.btn-saved:disabled { opacity: 1; }
.link-btn {
    background: none; border: 0; padding: 0; font: inherit; font-size: 14px;
    color: var(--accent); cursor: pointer;
}
.link-btn:hover { text-decoration: underline; }
.link-btn.danger { color: var(--danger); }

.form { display: grid; gap: 16px; }
.field { display: grid; gap: 6px; }
.field > span { font-size: 13px; font-weight: 500; color: var(--ink-2); }
.field small { color: var(--muted); font-size: 12px; }
input[type=text], input[type=tel], input[type=password], input[type=search], textarea {
    width: 100%; font: inherit; color: var(--ink);
    padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface);
}
input:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; line-height: 1.6; }

.alert { padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; font-size: 14px; border: 1px solid; }
.alert-error { background: var(--danger-soft); border-color: #f4c7c2; color: var(--danger); }
.alert-success { background: var(--saved-soft); border-color: #b9e0db; color: var(--saved); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card-head h2 { margin: 0; font-size: 16px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 0; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.row-actions { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.row-actions.center { justify-content: center; }

.empty { text-align: center; padding: 48px 20px; max-width: 560px; margin: 0 auto; }

/* ---------- Home ---------- */
.hero { display: grid; grid-template-columns: 1.1fr .9fr; gap: 48px; align-items: center; padding: 24px 0 40px; }
.eyebrow { text-transform: uppercase; letter-spacing: .08em; font-size: 12px; font-weight: 600; color: var(--accent); margin-bottom: 12px; }
.hero h1 { font-family: var(--serif); font-size: 40px; font-weight: 600; letter-spacing: -.02em; margin-bottom: 18px; }
.lead { font-size: 17px; color: var(--ink-2); max-width: 560px; }
.hero-actions { display: flex; gap: 12px; margin-top: 24px; flex-wrap: wrap; }
.hero-sample { display: grid; gap: 14px; }
.sample-sentence { font-family: var(--serif); font-size: 22px; padding: 4px 4px; }
.sample-sentence mark { background: var(--mark-strong); padding: 0 3px; border-radius: 3px; }
.features { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.feature { background: var(--surface); padding: 22px; }
.feature-num { font-family: var(--serif); color: var(--accent); font-size: 14px; font-weight: 600; margin-bottom: 8px; }
.feature p { color: var(--ink-2); font-size: 14px; margin: 0; }

/* ---------- Auth ---------- */
.auth { max-width: 400px; margin: 24px auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 32px; box-shadow: var(--shadow); }
.auth h1 { font-size: 22px; }
.auth-alt { margin: 18px 0 0; font-size: 14px; text-align: center; color: var(--muted); }

/* ---------- Reader ---------- */
.quota { font-size: 13px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); padding: 6px 12px; border-radius: 20px; }
.quota b { color: var(--ink); }
.reader { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 20px; align-items: start; }
.workspace { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 0 20px 20px; min-width: 0; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 0 -20px 18px; padding: 0 20px; }
.tab {
    font: inherit; font-weight: 500; font-size: 14px; color: var(--muted);
    background: none; border: 0; border-bottom: 2px solid transparent;
    padding: 14px 12px 12px; cursor: pointer; margin-bottom: -1px;
}
.tab.active { color: var(--ink); border-bottom-color: var(--accent); }
.text-view-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }

.tokens { font-family: var(--serif); font-size: 19px; line-height: 1.9; word-wrap: break-word; }
.tokens p { margin: 0 0 .8em; }
.small-tokens { font-size: 16px; line-height: 1.8; }
.w {
    cursor: pointer; border-radius: 3px; padding: 1px 1px;
    transition: background .12s;
}
.w:hover { background: var(--accent-soft); }
.w.ctx { background: var(--mark); }
.w.active { background: var(--mark-strong); }
.w.saved { box-shadow: inset 0 -2px 0 var(--saved); }
.w.pulse { animation: pulse .6s ease-out; }
@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(15, 118, 110, .45); }
    100% { box-shadow: 0 0 0 10px rgba(15, 118, 110, 0); }
}

.dropzone {
    display: grid; place-items: center; text-align: center; gap: 6px;
    border: 1.5px dashed #b8c1d1; border-radius: var(--radius); padding: 48px 20px;
    cursor: pointer; background: #fbfcfd; transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.dz-title { font-weight: 500; }
.dz-sub { font-size: 13px; color: var(--muted); }
.ocr-progress { display: grid; gap: 8px; margin: 18px 0; }
.progress { height: 6px; background: var(--line-2); border-radius: 3px; overflow: hidden; }
.progress.thin { height: 4px; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

.image-stage { position: relative; display: inline-block; max-width: 100%; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; line-height: 0; }
.image-stage img { max-width: 100%; height: auto; display: block; }
.boxes { position: absolute; inset: 0; }
.box {
    position: absolute; border: 1.5px solid rgba(29, 78, 216, .55); border-radius: 2px;
    background: rgba(29, 78, 216, .06); cursor: pointer; transition: background .12s;
}
.box:hover { background: rgba(29, 78, 216, .2); }
.box.active { border-color: #d97706; background: rgba(255, 214, 102, .45); }
.box.saved { border-color: var(--saved); background: rgba(15, 118, 110, .18); }
.ocr-text { margin-top: 14px; }
.ocr-text summary { cursor: pointer; font-size: 14px; color: var(--ink-2); margin-bottom: 8px; }

.panel { position: sticky; top: 80px; }
.panel-empty { background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); padding: 24px; }
.panel-empty-title { font-weight: 600; margin-bottom: 6px; }
.panel-empty p { margin: 0; font-size: 14px; }

.result-card { padding: 22px; }
.rc-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.rc-word { font-family: var(--serif); font-size: 28px; font-weight: 600; line-height: 1.15; letter-spacing: -.01em; }
.rc-meta { color: var(--muted); font-size: 14px; margin-top: 4px; }
.rc-translation { font-size: 19px; font-weight: 500; margin: 14px 0 10px; }
.rc-note { font-size: 13px; color: var(--ink-2); background: var(--accent-soft); border-radius: 6px; padding: 8px 10px; margin-bottom: 12px; }
.rc-section { border-top: 1px solid var(--line-2); padding-top: 12px; margin-top: 12px; }
.rc-label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; margin-bottom: 6px; }
.rc-sentence { font-size: 14px; color: var(--ink-2); }
.rc-source { font-family: var(--serif); font-size: 15px; margin-bottom: 4px; }
.rc-others { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; font-size: 14px; }
.rc-others .pos { color: var(--muted); font-size: 12px; margin-right: 6px; }
.rc-actions { margin-top: 16px; display: flex; gap: 10px; align-items: center; }
.rc-error { color: var(--danger); }
.level {
    display: inline-block; font-size: 12px; font-weight: 600; letter-spacing: .02em;
    padding: 3px 8px; border-radius: 4px; background: var(--line-2); color: var(--ink-2);
    white-space: nowrap;
}

.loading .sk { background: linear-gradient(90deg, var(--line-2), #f8f9fb, var(--line-2)); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: 4px; margin-bottom: 10px; }
.sk-title { height: 28px; width: 55%; }
.sk-line { height: 12px; width: 90%; }
.sk-line.short { width: 35%; }
@keyframes sk { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ---------- Dictionary ---------- */
.toolbar { margin-bottom: 18px; max-width: 360px; }
.day-group { margin-bottom: 28px; }
.day-title { font-size: 15px; margin-bottom: 10px; }
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; box-shadow: var(--shadow); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 10px 14px; border-bottom: 1px solid var(--line); background: #fafbfc; white-space: nowrap; }
.table td { padding: 12px 14px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.table.compact td { padding: 8px 4px; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.cell-word { font-family: var(--serif); font-size: 17px; font-weight: 600; }
.cell-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.cell-context { max-width: 380px; }
.col-actions { text-align: right; white-space: nowrap; width: 1%; }

/* ---------- Quiz ---------- */
.quiz { max-width: 640px; margin: 0 auto; padding: 28px; }
.quiz-start { text-align: center; padding: 20px 0; }
.quiz-top { display: grid; gap: 8px; margin-bottom: 24px; }
.quiz-q { text-align: center; margin-bottom: 24px; }
.quiz-word { font-family: var(--serif); font-size: 36px; font-weight: 600; letter-spacing: -.01em; }
.quiz-meta { color: var(--muted); font-size: 14px; margin-top: 4px; }
.quiz-context { font-family: var(--serif); font-size: 15px; color: var(--ink-2); margin-top: 12px; font-style: italic; }
.options { display: grid; gap: 10px; }
.option {
    font: inherit; font-size: 15px; text-align: left; padding: 13px 16px;
    border: 1px solid var(--line); border-radius: 6px; background: var(--surface); cursor: pointer; color: var(--ink);
    transition: border-color .12s, background .12s;
}
.option:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
.option:disabled { cursor: default; }
.option.correct { border-color: var(--saved); background: var(--saved-soft); }
.option.wrong { border-color: var(--danger); background: var(--danger-soft); }
.quiz-next { display: flex; justify-content: flex-end; margin-top: 18px; min-height: 40px; }
.quiz-score { text-align: center; margin-bottom: 20px; }
.score-big { font-family: var(--serif); font-size: 48px; font-weight: 600; }
.mistakes { list-style: none; padding: 0; margin: 0 0 16px; border-top: 1px solid var(--line-2); }
.mistakes li { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line-2); font-size: 14px; }
.mistakes b { font-family: var(--serif); font-size: 16px; }

/* ---------- Stats ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
.kpi { padding: 16px 18px; }
.kpi-label { font-size: 13px; color: var(--muted); }
.kpi-value { font-size: 30px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi-sub { font-size: 12px; color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-bottom: 18px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.sw-a, .bar-a { background: var(--accent); }
.sw-b, .bar-b { background: #9db4e8; }
.bars { display: grid; grid-template-columns: repeat(7, 1fr); gap: 10px; height: 200px; align-items: end; }
.bar-col { display: grid; grid-template-rows: 1fr auto; height: 100%; gap: 6px; }
.bar-pair { display: flex; align-items: flex-end; justify-content: center; gap: 3px; height: 100%; }
.bar { width: 40%; max-width: 18px; min-height: 2px; border-radius: 3px 3px 0 0; position: relative; }
.bar span { position: absolute; top: -18px; left: 50%; transform: translateX(-50%); font-size: 11px; color: var(--muted); }
.bar-label { text-align: center; font-size: 12px; color: var(--muted); }
.hbars { display: grid; gap: 10px; margin-bottom: 12px; }
.hbar-row { display: grid; grid-template-columns: 28px 1fr 32px; gap: 10px; align-items: center; font-size: 13px; }
.hbar-label { font-weight: 600; color: var(--ink-2); }
.hbar-track { height: 10px; background: var(--line-2); border-radius: 5px; overflow: hidden; }
.hbar { height: 100%; background: var(--accent); border-radius: 5px; }
.hbar-val { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.leaders { list-style: none; padding: 0; margin: 0 0 12px; }
.leaders li { display: grid; grid-template-columns: 28px 1fr auto; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line-2); font-size: 14px; align-items: center; }
.leaders li.me { font-weight: 600; }
.rank { width: 24px; height: 24px; border-radius: 50%; background: var(--line-2); display: grid; place-items: center; font-size: 12px; font-weight: 600; }
.leaders li:first-child .rank { background: var(--ink); color: #fff; }
.lscore { color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- Footer & toast ---------- */
.footer { border-top: 1px solid var(--line); background: var(--surface); }
.footer-inner { display: flex; justify-content: space-between; gap: 12px; padding-top: 16px; padding-bottom: 16px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.toast {
    position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
    background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 6px; font-size: 14px;
    box-shadow: 0 6px 24px rgba(20, 33, 61, .25); opacity: 0; pointer-events: none;
    transition: opacity .2s, transform .2s; z-index: 50; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }
.toast.success { background: var(--saved); }

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
    .reader { grid-template-columns: 1fr; }
    .panel { position: static; }
    .hero { grid-template-columns: 1fr; gap: 28px; }
    .features { grid-template-columns: 1fr 1fr; }
    .kpis { grid-template-columns: 1fr 1fr; }
    .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
    .topbar-inner { height: auto; flex-wrap: wrap; gap: 8px 16px; padding-top: 10px; padding-bottom: 0; }
    .mainnav { order: 3; flex-basis: 100%; overflow-x: auto; margin: 0 -16px; padding: 0 12px 8px; }
    .mainnav a { white-space: nowrap; }
    .account-name { display: none; }
    .hero h1 { font-size: 30px; }
    .features { grid-template-columns: 1fr; }
    .hide-sm { display: none; }
    .auth { padding: 24px 20px; }
    .tokens { font-size: 17px; }
    .quiz { padding: 20px; }
    .quiz-word { font-size: 30px; }
    .panel.sheet {
        position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 30;
        max-height: 75vh; overflow-y: auto; padding: 0;
        box-shadow: 0 -8px 30px rgba(20, 33, 61, .2); border-radius: 12px 12px 0 0;
    }
    .panel.sheet .result-card { border-radius: 12px 12px 0 0; border: 0; }
    .sheet-close { display: block !important; }
}
.sheet-close { display: none; margin-left: auto; }
