:root {
    --primary: #B4552F;
    --primary-light: #C8643B;
    --primary-dark: #8F4224;
    --accent: #C4813A;
    --accent-light: #E0A96C;
    --bg-main: #F5F3EF;
    --bg-card: #FFFFFF;
    --bg-surface: #EBE8E2;
    --domain-rgb: 180, 85, 47;
    --text-primary: #2C2A26;
    --text-secondary: #5C5850;
    --text-muted: #6B6458;
    --success: #5A9E5F;
    --error: #C7564F;
    --warning: #C4913A;
    --border: #D8D4CC;
    --shadow: rgba(44, 42, 38, 0.08);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 20px;
    --font-main: 'Segoe UI', system-ui, -apple-system, sans-serif;
    --xp-text: #5a3e00;

    /* Motion system */
    --ease-out: cubic-bezier(0.25, 0.46, 0.45, 0.94);
    --ease-spring: cubic-bezier(0.22, 1, 0.36, 1);
    --dur-fast: 150ms;
    --dur-normal: 250ms;
    --dur-slow: 400ms;
}

@font-face {
    font-family: 'Cinzel';
    font-weight: 700;
    font-display: swap;
    src: url('../fonts/cinzel-700-latin.woff2') format('woff2');
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    scroll-behavior: smooth;
}

html, body {
    font-family: var(--font-main);
    background: var(--bg-main);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    overscroll-behavior-y: contain;
}

/* Per-domain hue map, used for selection and focus tints. */
html[data-domain="math"]        { --domain-rgb: 91, 106, 191; }
html[data-domain="philosophy"]  { --domain-rgb: 142, 99, 181; }
html[data-domain="physics"]     { --domain-rgb: 74, 144, 217; }
html[data-domain="biology"]     { --domain-rgb: 46, 204, 113; }
html[data-domain="chemistry"]   { --domain-rgb: 233, 30, 99; }
html[data-domain="geology"]     { --domain-rgb: 141, 110, 99; }
html[data-domain="materials"]   { --domain-rgb: 96, 125, 139; }
html[data-domain="history"]     { --domain-rgb: 196, 129, 58; }
html[data-domain="linguistics"] { --domain-rgb: 255, 112, 67; }
html[data-domain="misc"]        { --domain-rgb: 94, 53, 177; }
html[data-domain="economics"]   { --domain-rgb: 22, 160, 133; }
html[data-domain="literature"]  { --domain-rgb: 142, 44, 64; }
html[data-domain="cs"]          { --domain-rgb: 38, 166, 154; }
html[data-domain="politics"]    { --domain-rgb: 57, 73, 171; }
html[data-domain="media"]       { --domain-rgb: 2, 119, 189; }
html[data-domain="religion"]    { --domain-rgb: 162, 59, 114; }
html[data-domain="psychology"]  { --domain-rgb: 192, 80, 158; }
html[data-domain="sociology"]   { --domain-rgb: 212, 160, 23; }


html.font-small { font-size: 13px; }
html.font-medium { font-size: 16px; }
html.font-large { font-size: 19px; }

body {
    padding-bottom: calc(70px + env(safe-area-inset-bottom));
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-surface); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

@supports (scrollbar-color: auto) {
    html { scrollbar-color: var(--border) var(--bg-surface); scrollbar-width: thin; }
}

::selection {
    background: rgba(var(--domain-rgb), 0.2);
    color: var(--text-primary);
}

.app-container {
    min-height: 100vh;
}

.main-content {
    max-width: 900px;
    margin: 0 auto;
    padding: 16px;
    padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

h1, h2, h3 {
    color: var(--text-primary);
    line-height: 1.3;
    text-wrap: balance;
    text-rendering: optimizeLegibility;
}

h1 { font-size: 1.75rem; font-weight: 700; }
h2 { font-size: 1.4rem; font-weight: 600; }
h3 { font-size: 1.15rem; font-weight: 600; }

a {
    color: var(--primary);
    text-decoration: none;
}

a, button, .card-clickable, .option-btn, .tab, .nav-item {
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

/* Cards */
.card {
    background: var(--bg-card);
    border-radius: var(--radius-md);
    padding: 16px;
    box-shadow: 0 2px 8px var(--shadow);
    border: 1px solid var(--border);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.card-clickable:hover, .card-clickable:active {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px var(--shadow);
}

.card-clickable {
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.card-raised {
    box-shadow: 0 4px 16px var(--shadow);
}

.card-floating {
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.2s ease;
    -webkit-tap-highlight-color: transparent;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    filter: brightness(1.1);
}

.btn-accent {
    background: var(--accent);
    color: white;
}

.btn-success {
    background: var(--success);
    color: white;
}

.btn-outline {
    background: transparent;
    color: var(--primary);
    border: 2px solid var(--primary);
}

.btn-sm {
    padding: 6px 14px;
    font-size: 0.85rem;
}

.btn-lg {
    padding: 14px 28px;
    font-size: 1.1rem;
}

/* Progress bar */
.progress-bar-container {
    background: var(--bg-surface);
    border-radius: 10px;
    height: 12px;
    overflow: hidden;
}

.progress-bar-fill {
    height: 100%;
    background: var(--primary);
    border-radius: 10px;
    transition: width 0.5s ease;
}

.progress-bar-fill.accent {
    background: var(--accent);
}

/* XP display */
.xp-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: color-mix(in srgb, var(--accent) 18%, transparent);
    color: var(--xp-text);
    font-weight: 700;
    font-size: 0.85rem;
    padding: 4px 10px;
    border-radius: 4px;
}

/* Streak */
.streak-display {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-weight: 700;
    color: var(--warning);
}

/* Grid layouts */
.grid-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.grid-3 {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 12px;
}

/* Section spacing */
.section {
    margin-bottom: 24px;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

/* Toast notifications */
#toast-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    border: 1px solid var(--accent);
    animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
    pointer-events: auto;
    font-weight: 600;
}

.toast.achievement {
    border-color: var(--primary);
}

.toast.level-up {
    border-color: var(--success);
}

@media (min-width: 1024px) {
    #toast-container {
        left: 236px;
        right: 16px;
    }
}

@keyframes toastIn {
    from { opacity: 0; transform: translateX(100%); }
    to { opacity: 1; transform: translateX(0); }
}

@keyframes toastOut {
    from { opacity: 1; transform: translateX(0); }
    to { opacity: 0; transform: translateX(100%); }
}

/* Quiz option styles */
.option-btn {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    font-size: 1rem;
    color: var(--text-primary);
    text-align: left;
    cursor: pointer;
    transition: all 0.2s ease;
    -webkit-tap-highlight-color: transparent;
}

.option-btn:hover {
    border-color: var(--primary-light);
    background: var(--bg-surface);
}

.option-btn.selected {
    border-color: var(--primary);
    background: color-mix(in srgb, var(--primary) 8%, transparent);
}

.option-btn.correct {
    border-color: var(--success);
    background: rgba(76, 175, 80, 0.1);
}

.option-btn.incorrect {
    border-color: var(--error);
    background: rgba(239, 83, 80, 0.1);
}

.option-btn.eliminated {
    opacity: 0.25;
    text-decoration: line-through;
    cursor: not-allowed;
}

/* Shared quiz layout & feedback styles */
.quiz-progress {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.question-prompt {
    font-size: 1.1rem;
    font-weight: 600;
    line-height: 1.5;
    margin-bottom: 8px;
}

.options-grid {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

@keyframes optionEnter {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.options-grid.stagger-options > .option-btn {
    animation: optionEnter 0.3s var(--ease-out) backwards;
}
.options-grid.stagger-options > .option-btn:nth-child(1) { animation-delay: 0.03s; }
.options-grid.stagger-options > .option-btn:nth-child(2) { animation-delay: 0.06s; }
.options-grid.stagger-options > .option-btn:nth-child(3) { animation-delay: 0.09s; }
.options-grid.stagger-options > .option-btn:nth-child(4) { animation-delay: 0.12s; }
.options-grid.stagger-options > .option-btn:nth-child(5) { animation-delay: 0.15s; }
.options-grid.stagger-options > .option-btn:nth-child(6) { animation-delay: 0.18s; }

.feedback {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    font-weight: 600;
    margin-top: 12px;
}

.feedback-correct {
    background: color-mix(in srgb, var(--success) 10%, transparent);
    color: var(--success);
    border: 1px solid var(--success);
}

.feedback-incorrect {
    background: color-mix(in srgb, var(--error) 10%, transparent);
    color: var(--error);
    border: 1px solid var(--error);
}

.feedback-icon { font-size: 1.2rem; }
.correct-answer { margin-left: auto; font-weight: 500; }

.explanation {
    background: var(--bg-surface);
    padding: 12px;
    border-radius: var(--radius-sm);
    margin-top: 8px;
    color: var(--text-secondary);
    font-size: 0.9rem;
    line-height: 1.5;
}

.anim-correct { animation: popCorrect 0.25s var(--ease-out); }
.anim-incorrect { animation: shakeWrong 0.3s ease; }

@keyframes popCorrect {
    from { opacity: 0; transform: translateY(4px); }
}

@keyframes shakeWrong {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-4px); }
    50% { transform: translateX(4px); }
    75% { transform: translateX(-2px); }
}

/* Math expression container */
.math-display {
    background: var(--bg-surface);
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    margin: 8px 0;
    text-align: center;
    font-size: 1.2rem;
    overflow-x: auto;
}

/* Canvas container */
.canvas-container {
    background: var(--bg-card);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    overflow: hidden;
    margin: 12px 0;
    position: relative;
}

.canvas-container canvas {
    display: block;
    width: 100%;
    touch-action: none;
}

/* Page header */
/* FocusOnNavigate moves focus to each page's h1 for screen readers; it is not a tab stop, so no ring. */
h1:focus { outline: none; }

.page-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

.page-header .back-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-secondary);
    padding: 12px;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: -8px;
    -webkit-tap-highlight-color: transparent;
}

/* Tag/chip */
.chip {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    background: var(--bg-surface);
    color: var(--text-secondary);
}

/* Loading */
.loading {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 40px;
    gap: 12px;
    color: var(--text-muted);
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--bg-surface);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Shared empty state */
.empty-state {
    text-align: center;
    padding: 40px 24px;
}

.empty-state .empty-icon {
    margin-bottom: 16px;
    color: var(--primary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: color-mix(in srgb, var(--primary) 10%, transparent);
}

.empty-state h2 {
    margin-bottom: 8px;
}

.empty-state p {
    color: var(--text-secondary);
    margin: 0 auto 20px;
    max-width: 320px;
    line-height: 1.6;
}

/* ── Responsive ── */

/* Small phones */
@media (max-width: 480px) {
    .main-content {
        padding: 12px;
        padding-bottom: calc(96px + env(safe-area-inset-bottom));
    }

    h1 { font-size: 1.5rem; }
    h2 { font-size: 1.2rem; }

    .grid-2 {
        grid-template-columns: 1fr;
    }
}

/* Tablets and small desktops */
@media (min-width: 768px) {
    .main-content {
        padding: 24px 32px;
        padding-bottom: 40px;
    }

    h1 { font-size: 2rem; }

    .card {
        padding: 20px;
    }

    .options-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
    }
}

/* Desktop: sidebar layout */
@media (min-width: 1024px) {
    .options-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 14px;
    }

    .app-container {
        display: flex;
        flex-direction: row;
    }

    .main-content {
        max-width: none;
        flex: 1;
        padding: 32px 48px;
        padding-bottom: 32px;
        margin-left: 220px;
    }

    .card {
        padding: 24px;
    }

    .btn-lg {
        padding: 16px 32px;
    }

    .grid-2 {
        gap: 16px;
    }
}

@media (min-width: 1280px) {
    .main-content {
        max-width: 1100px;
        padding: 40px 64px;
    }
}

/* Ensure all interactive elements inherit theme colors */
input, select, textarea, button {
    color: inherit;
}

input[type="text"],
input[type="number"],
input[type="time"],
select,
textarea {
    background: var(--bg-surface);
    color: var(--text-primary);
    border: 1.5px solid var(--border);
    font-family: var(--font-main);
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

input::placeholder,
textarea::placeholder {
    color: var(--text-muted);
    opacity: 0.7;
}

/* Global input focus ring */
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: none;
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 3px rgba(var(--domain-rgb), 0.2);
}

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

/* KaTeX overrides for dark themes */
.katex {
    color: var(--text-primary);
}

/* ── Enhanced touch feedback ── */
.card-clickable:active {
    transform: scale(0.97) !important;
    box-shadow: 0 1px 4px var(--shadow) !important;
    transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.btn:active {
    transform: scale(0.95);
    filter: brightness(0.92);
    transition: transform 0.08s ease, filter 0.08s ease;
}

.btn:not(:active) {
    transition: transform 0.2s var(--ease-out), filter 0.2s ease, box-shadow 0.15s ease;
}

.btn-primary:active, .btn-accent:active {
    box-shadow: none;
}

/* Smooth option button press */
.option-btn:active:not(:disabled) {
    transform: scale(0.97);
    transition: transform 0.08s ease;
}

/* ── Skeleton loaders ── */
.skeleton {
    background: var(--bg-surface);
    border-radius: var(--radius-sm);
    position: relative;
    overflow: hidden;
}

.skeleton::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, transparent 0%, color-mix(in srgb, var(--bg-card) 55%, transparent) 50%, transparent 100%);
    animation: skeletonShimmer 1.8s ease infinite;
}

@keyframes skeletonShimmer {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}

.skeleton-text {
    height: 14px;
    margin-bottom: 8px;
    border-radius: 4px;
}

.skeleton-text.short { width: 40%; }
.skeleton-text.medium { width: 65%; }
.skeleton-text.long { width: 90%; }

.skeleton-circle {
    border-radius: 50%;
}

.skeleton-card {
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
}

/* Skeleton layout for dashboard */
.skeleton-dashboard .skeleton-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    margin-bottom: 24px;
}

.skeleton-dashboard .skeleton-topics {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.skeleton-dashboard .skeleton-topic {
    display: flex;
    gap: 12px;
    align-items: center;
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
}

/* Skeleton for profile */
.skeleton-profile .skeleton-avatar {
    width: 80px;
    height: 80px;
    margin: 0 auto 12px;
}

.skeleton-profile .skeleton-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-top: 16px;
}

.skeleton-profile .skeleton-stat {
    height: 70px;
    border-radius: var(--radius-md);
}

/* ── Progress bar animate from 0 ── */
.progress-bar-fill.animate-in {
    animation: progressGrow 0.6s var(--ease-out);
}

@keyframes progressGrow {
    from { width: 0; }
}

/* ── Tab content crossfade ── */
.tab-content {
    animation: tabFade 0.2s ease;
}

@keyframes tabFade {
    from { opacity: 0; }
}

/* Page-level fade in */
.main-content {
    animation: pageIn 0.25s ease;
}

@keyframes pageIn {
    from { opacity: 0; }
}

/* ── Staggered card entrance ──
   Fill "backwards" only: a forwards fill pins opacity/transform and overrides dimming, hover and press. */
.stagger-in > * {
    animation: staggerUp 0.3s var(--ease-out) backwards;
}
.stagger-in > *:nth-child(2) { animation-delay: 0.03s; }
.stagger-in > *:nth-child(3) { animation-delay: 0.06s; }
.stagger-in > *:nth-child(4) { animation-delay: 0.09s; }
.stagger-in > *:nth-child(5) { animation-delay: 0.12s; }
.stagger-in > *:nth-child(6) { animation-delay: 0.15s; }
.stagger-in > *:nth-child(7) { animation-delay: 0.18s; }
.stagger-in > *:nth-child(n+8) { animation-delay: 0.2s; }

@keyframes staggerUp {
    from { opacity: 0; transform: translateY(8px); }
}

/* ── Card hover ── */
.card-clickable:hover {
    box-shadow: 0 4px 16px var(--shadow);
}

/* ── Pulse animation ── */
.pulse {
    animation: settleIn 0.35s var(--ease-out) backwards;
}

/* ── XP float animation ── */
.xp-float {
    position: fixed;
    pointer-events: none;
    z-index: 999;
    font-weight: 800;
    font-size: 1.2rem;
    color: var(--accent);
    text-shadow: 0 1px 4px rgba(0,0,0,0.2);
    animation: xpFloat 1.2s ease forwards;
}

@keyframes xpFloat {
    0% { opacity: 1; transform: translateY(0) scale(1); }
    60% { opacity: 1; transform: translateY(-36px); }
    100% { opacity: 0; transform: translateY(-56px); }
}

/* ── Confetti container ── */
#confetti-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 998;
    overflow: hidden;
}

.confetti-piece {
    position: absolute;
    width: 10px;
    height: 10px;
    top: -10px;
    border-radius: 2px;
    animation: confettiFall linear forwards;
}

@keyframes confettiFall {
    0% { transform: translateY(0) rotate(0deg); opacity: 1; }
    80% { opacity: 1; }
    100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}

/* ── Streak flame animation ── */
.streak-fire {
    display: inline-block;
}

/* ── Question slide transition ── */
.question-enter {
    animation: questionSlideIn 0.25s var(--ease-out);
}

@keyframes questionSlideIn {
    from { opacity: 0; transform: translateX(12px); }
}

/* ── Results count-up effect ── */
.count-up {
    animation: settleIn 0.35s var(--ease-out) backwards;
}

@keyframes settleIn {
    from { opacity: 0; transform: translateY(6px); }
}

/* ── Level-up burst ── */
.level-up-burst {
    animation: settleIn 0.4s var(--ease-out) 0.15s backwards;
}

/* ── Achievement pop-in ── */
.achievement-pop {
    animation: settleIn 0.4s var(--ease-out) 0.25s backwards;
}

/* ── Ripple effect for tappable elements ── */
.ripple {
    position: relative;
    overflow: hidden;
}

.ripple::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: radial-gradient(circle, rgba(255,255,255,0.25) 10%, transparent 70%);
    transform: scale(0);
    opacity: 0;
    pointer-events: none;
}

.ripple:active::after {
    transform: scale(2.5);
    opacity: 1;
    transition: 0s;
}

/* ── Shared tab bar system ── */
.tab-bar {
    display: flex;
    gap: 4px;
    background: var(--bg-surface);
    border-radius: var(--radius-md);
    padding: 4px;
    margin-bottom: 16px;
}

.tab-bar .tab {
    flex: 1;
    padding: 8px 4px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.85rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: color var(--dur-normal) var(--ease-out),
                background var(--dur-normal) var(--ease-out),
                box-shadow var(--dur-normal) var(--ease-out);
    -webkit-tap-highlight-color: transparent;
    min-height: 44px;
}

.tab-bar .tab:active {
    transform: scale(0.96);
}

.tab-bar .tab.active {
    background: var(--bg-card);
    color: var(--primary);
    box-shadow: 0 2px 8px var(--shadow);
}

/* ── Smooth tab indicator ── */
.tab-nav {
    position: relative;
}

.tab-nav .tab-item {
    transition: color 0.25s ease, background 0.25s ease;
}

/* ── Toast enhanced ── */
.toast {
    backdrop-filter: blur(8px);
}

@keyframes toastIn {
    from { opacity: 0; transform: translateX(100%) scale(0.9); }
    to { opacity: 1; transform: translateX(0) scale(1); }
}

/* ── Safe area insets (notch + gesture bar) ── */
body {
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    /* bottom padding is handled by .main-content and bottom-nav */
}

/* main-content padding-bottom defined above with safe-area inset */

/* ── Focus-visible accessibility ── */
:focus {
    outline: none;
}

:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* Buttons get a box-shadow focus ring (nicer on rounded elements) */
button:focus-visible,
.btn:focus-visible,
.option-btn:focus-visible,
.card-clickable:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 40%, transparent);
}

/* ── Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-delay: 0s !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    html { scroll-behavior: auto; }

    .skeleton::after {
        animation: none;
    }
}

/* ── Motion var usage — upgrade key transitions ── */
.card {
    transition: transform var(--dur-normal) var(--ease-out),
                box-shadow var(--dur-normal) var(--ease-out);
}

.btn {
    transition: transform var(--dur-fast) var(--ease-out),
                box-shadow var(--dur-fast) var(--ease-out),
                background var(--dur-normal) var(--ease-out),
                filter var(--dur-fast) var(--ease-out);
}

.btn:not(:active) {
    transition: transform var(--dur-normal) var(--ease-out),
                filter var(--dur-normal) var(--ease-out),
                box-shadow var(--dur-fast) var(--ease-out);
}

/* ── ARIA label reminder: icon-only buttons ── */
/* Back buttons in .page-header already have aria from context;
   toggle buttons get tooltip via title in HTML */

/* ── Tap-target minimum: ensure icon-only controls are ≥44px ── */
.page-header .back-btn {
    min-width: 44px;
    min-height: 44px;
    border-radius: var(--radius-sm);
    transition: background var(--dur-fast) var(--ease-out);
}

.page-header .back-btn:hover {
    background: var(--bg-surface);
}

.page-header .back-btn:active {
    transform: scale(0.92);
    transition: transform 0.1s ease;
}

.page-header .back-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 40%, transparent);
}

/* ── Concept figures (history topic images) ─────────────────────────── */
.concept-figure {
    margin: 16px 0;
    padding: 0;
    border-radius: var(--radius-md);
    overflow: hidden;
    background: var(--bg-surface);
}

.concept-figure img {
    width: 100%;
    height: auto;
    display: block;
    max-height: 400px;
    object-fit: contain;
    background: #fff;
}

.concept-figure figcaption {
    padding: 8px 12px;
    font-size: 0.8rem;
    color: var(--text-muted);
    font-style: italic;
}

/* ============================================================
   Page transitions
   ============================================================ */
.page-shell {
    animation: pageEnter 150ms ease-out;
}

@keyframes pageEnter {
    from { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
    .page-shell { animation: none; }
}

/* ── Card frame cosmetics ── */
[data-frame="gold"] .card {
    border: 2px solid #D4AF37;
    box-shadow: 0 0 0 1px rgba(212, 175, 55, 0.4), 0 4px 18px rgba(212, 175, 55, 0.18);
}
[data-frame="silver"] .card {
    border: 2px solid #C0C0C0;
    box-shadow: 0 0 0 1px rgba(192, 192, 192, 0.4), 0 4px 14px rgba(180, 180, 200, 0.18);
}
[data-frame="holo"] .card {
    border: 2px solid transparent;
    background-image:
        linear-gradient(var(--bg-card), var(--bg-card)),
        linear-gradient(135deg, #ff7eb3, #ff758c, #ffb86c, #7afcff, #b388ff);
    background-origin: border-box;
    background-clip: padding-box, border-box;
    box-shadow: 0 0 18px rgba(180, 130, 255, 0.25);
}
[data-frame="foil"] .card {
    border: 2px solid transparent;
    background-image:
        linear-gradient(var(--bg-card), var(--bg-card)),
        linear-gradient(120deg, #74ebd5, #9face6, #c2e9fb);
    background-origin: border-box;
    background-clip: padding-box, border-box;
}
[data-frame="parchment"] .card {
    border: 2px double #8B5A2B;
    box-shadow: inset 0 0 0 1px rgba(139, 90, 43, 0.2), 0 2px 10px rgba(80, 50, 20, 0.15);
}

/* ── Streak flame skins ── */
[data-flame="blue"] .streak-fire { color: #4AB8FF; filter: drop-shadow(0 0 4px rgba(74, 184, 255, 0.6)); }
[data-flame="purple"] .streak-fire { color: #C880FF; filter: drop-shadow(0 0 4px rgba(200, 128, 255, 0.6)); }
[data-flame="lightning"] .streak-fire {
    color: #FFE066;
    filter: drop-shadow(0 0 6px rgba(255, 224, 102, 0.7));
}
[data-flame="rainbow"] .streak-fire {
    color: #ff5f6d;
    animation: rainbowHue 8s linear infinite;
    filter: drop-shadow(0 0 4px rgba(255, 100, 200, 0.5));
}
@keyframes rainbowHue {
    from { filter: drop-shadow(0 0 4px rgba(255, 100, 200, 0.5)) hue-rotate(0deg); }
    to { filter: drop-shadow(0 0 4px rgba(255, 100, 200, 0.5)) hue-rotate(360deg); }
}

@media print {
    .bottom-nav, #toast-container, #confetti-container { display: none !important; }
    .main-content { max-width: none; margin: 0; padding: 0; }
    body { padding: 0; }
}
