:root {
    --accent: #4f46e5;
    --accent-light: #818cf8;
    --bg: #f4f5fb;
    --card: #ffffff;
    --text: #1d1d1f;
    --muted: #6e6e73;
    --faint: #a1a1a6;
    --line: #e4e5ef;
    --green: #1f9d55;
    --orange: #d9730d;
    --panel: rgba(79, 70, 229, 0.07);
    --ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #12131a;
        --card: #1c1d27;
        --text: #f5f5f7;
        --muted: #a1a1a6;
        --faint: #6e6e73;
        --line: #2e3040;
        --accent: #a5b4fc;
        --accent-light: #c7d2fe;
        --panel: rgba(165, 180, 252, 0.1);
        --green: #4ade80;
        --orange: #fb923c;
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: var(--ui);
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
}

button {
    font: inherit;
    color: inherit;
    cursor: pointer;
}

code {
    font: 0.9em var(--mono);
    background: var(--panel);
    padding: 1px 5px;
    border-radius: 4px;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 760px;
    margin: 0 auto;
    padding: env(safe-area-inset-top) 12px env(safe-area-inset-bottom);
}

.topbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0 4px;
}

.brand {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1.15rem;
    font-weight: 600;
    min-width: 0;
}

.nabla {
    font-family: "Times New Roman", Times, serif;
    font-size: 1.85rem;
    line-height: 1;
    background: linear-gradient(var(--accent-light), var(--accent));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.icon-btn {
    width: 42px;
    height: 42px;
    border: none;
    border-radius: 12px;
    background: transparent;
    font-size: 1.35rem;
    display: grid;
    place-items: center;
    flex: none;
}

.icon-btn:active { background: var(--line); }

.switches {
    display: flex;
    gap: 8px;
    padding: 4px 0;
}

.segmented {
    flex: 1;
    display: flex;
    padding: 3px;
    border-radius: 12px;
    background: var(--line);
}

.segmented button {
    flex: 1;
    padding: 8px 2px;
    border: none;
    border-radius: 9px;
    background: transparent;
    font-weight: 500;
    font-size: 0.88rem;
}

.segmented button.on {
    background: var(--card);
    box-shadow: 0 1px 3px rgb(0 0 0 / 0.15);
    font-weight: 600;
}

.status {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 4px;
    font-size: 0.9rem;
    color: var(--muted);
}

.status strong { color: var(--text); }

main {
    flex: 1;
    min-height: 0;
    display: flex;
}

.card {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 18px 16px 28px;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: var(--card);
    box-shadow: 0 6px 18px rgb(0 0 0 / 0.08);
    text-align: center;
    touch-action: pan-y;
    user-select: text;
    -webkit-overflow-scrolling: touch;
}

.card.flipped { border-color: color-mix(in srgb, var(--accent) 45%, transparent); }

.badges {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-wrap: wrap;
    min-height: 22px;
    margin-bottom: 8px;
    text-align: left;
}

.badges .context {
    margin-right: auto;
    font-weight: 500;
    font-size: 0.82rem;
    color: var(--muted);
}

.badge {
    padding: 2px 9px;
    border-radius: 99px;
    font-size: 0.72rem;
    font-weight: 600;
    background: var(--panel);
    color: var(--accent);
}

.badge.known { background: color-mix(in srgb, var(--green) 18%, transparent); color: var(--green); }
.badge.review { background: color-mix(in srgb, var(--orange) 18%, transparent); color: var(--orange); }

.card-name {
    font-size: 1.35rem;
    font-weight: 650;
    letter-spacing: -0.01em;
    margin: 14px 0 6px;
    line-height: 1.3;
}

.prompt-meaning {
    font-size: 1.05rem;
    line-height: 1.5;
    margin: 14px auto 0;
    max-width: 36em;
    text-align: left;
}

.formula {
    text-align: center;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 6px 0;
    -webkit-overflow-scrolling: touch;
}

.formula .katex-display { margin: 0.35em 0; }
.hero { margin: 28px 0 10px; font-size: 1.55rem; }
.hero.small { margin: 10px 0 4px; font-size: 1.2rem; }

.hint {
    margin-top: 22px;
    font-size: 0.9rem;
    color: var(--muted);
}

.sections {
    margin-top: 16px;
    padding-top: 10px;
    border-top: 1px solid var(--line);
    text-align: left;
}

.sections section { margin-top: 14px; }

.sections h3 {
    margin: 0 0 4px;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--accent);
}

.sections p { margin: 0; font-size: 0.98rem; line-height: 1.5; }

.read {
    font-size: 1.05rem;
    font-style: italic;
    background: var(--panel);
    border-left: 3px solid var(--accent);
    padding: 8px 12px;
    border-radius: 4px;
}

.example { font-size: 1.05rem; text-align: left; }

table.terms {
    border-collapse: collapse;
    width: 100%;
    font-size: 0.92rem;
}

table.terms td {
    padding: 6px 8px;
    vertical-align: top;
    border-bottom: 1px solid var(--line);
}

table.terms tr:last-child td { border-bottom: none; }
table.terms td.sym {
    white-space: nowrap;
    text-align: right;
    width: 1%;
    padding-right: 10px;
}

.cite {
    margin-top: 14px;
    color: var(--muted);
    font-size: 0.82rem;
}

.empty, .summary {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 24px;
    text-align: center;
    color: var(--muted);
}

.summary .big { font-size: 3.2rem; }
.summary h2 { margin: 0; color: var(--text); }
.summary .score { font-size: 1.25rem; color: var(--text); }
.summary .buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: min(320px, 100%);
    margin-top: 8px;
}

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 0 14px;
}

.btn {
    flex: 1;
    min-height: 48px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--card);
    font-weight: 600;
    white-space: nowrap;
}

.btn:active { filter: brightness(0.93); }
.btn.primary { border-color: var(--accent); background: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) {
    .btn.primary { color: #12131a; }
}
.btn.good { border-color: var(--green); background: var(--green); color: #fff; }
.btn.bad { border-color: var(--orange); color: var(--orange); }
.btn.small { flex: 0 0 auto; min-height: 48px; min-width: 48px; }
.btn.on-known { color: var(--green); border-color: var(--green); }
.btn.on-review { color: var(--orange); border-color: var(--orange); }
.btn:disabled { opacity: 0.45; }

.backdrop {
    position: fixed;
    inset: 0;
    background: rgb(0 0 0 / 0.35);
    z-index: 10;
}

.drawer {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 11;
    display: flex;
    flex-direction: column;
    width: min(360px, 88vw);
    padding-top: env(safe-area-inset-top);
    background: var(--bg);
    box-shadow: 4px 0 24px rgb(0 0 0 / 0.2);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
}

.drawer.open { transform: none; }

.drawer-head {
    display: flex;
    align-items: center;
    padding: 8px 12px;
}

.drawer-body {
    flex: 1;
    overflow-y: auto;
    padding: 0 12px 12px;
    -webkit-overflow-scrolling: touch;
}

.drawer-foot {
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    font-weight: 600;
    text-align: center;
}

.drawer h4 {
    margin: 18px 4px 6px;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--muted);
}

.panel {
    border-radius: 14px;
    background: var(--card);
    overflow: hidden;
}

.row {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 46px;
    padding: 8px 14px;
    border-bottom: 1px solid var(--line);
}

.row:last-child { border-bottom: none; }
.row input[type="checkbox"] {
    width: 20px;
    height: 20px;
    accent-color: var(--accent);
    flex: none;
}
.row label, .row .grow { flex: 1; }
.row select {
    font: inherit;
    padding: 6px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--bg);
    color: var(--text);
    max-width: 55%;
}

.source-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    border: none;
    width: 100%;
    background: none;
    font-weight: 700;
    text-align: left;
}

.source-ai {
    padding: 0 14px 8px;
    font-size: 0.82rem;
    color: var(--muted);
    line-height: 1.4;
}

.source-head .chev { transition: transform 0.15s; }
.source-head.collapsed .chev { transform: rotate(-90deg); }
.source-head .count {
    margin-left: auto;
    font-weight: 500;
    color: var(--muted);
    font-size: 0.85rem;
}

.links {
    display: flex;
    gap: 16px;
    padding: 0 14px 8px;
}

.links button {
    padding: 4px 0;
    border: none;
    background: none;
    color: var(--accent);
    font-size: 0.9rem;
}

.menu-btn {
    display: block;
    width: 100%;
    padding: 13px 14px;
    border: none;
    border-bottom: 1px solid var(--line);
    background: none;
    text-align: left;
}

.menu-btn:last-child { border-bottom: none; }
.menu-btn.danger { color: #c9372c; }

.install-banner {
    margin: 10px 0 0;
    padding: 12px 14px;
    border-radius: 14px;
    background: var(--panel);
    border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
    font-size: 0.9rem;
    line-height: 1.4;
}

.install-banner strong { color: var(--accent); }
.install-banner .actions-row {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.install-banner .btn { min-height: 40px; font-size: 0.9rem; }
