/* ── Reset & Base ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #ffffff;
    --surface: #f5f5f7;
    --surface-elevated: #ffffff;
    --primary: #6366f1;
    --primary-light: #4f46e5;
    --primary-bg: rgba(99,102,241,0.08);
    --practice: #6366f1;
    --practice-light: #4f46e5;
    --practice-bg: rgba(99,102,241,0.08);
    --danger: #ef4444;
    --success: #16a34a;
    --warning: #d97706;
    --text: #111827;
    --text-secondary: #6b7280;
    --border: #e5e7eb;
    --radius: 14px;
    --radius-sm: 10px;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-md: 0 2px 8px rgba(0,0,0,0.1);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

html {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, sans-serif;
    font-size: 16px;
    color: var(--text);
    background: var(--bg);
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    min-height: 100dvh;
    padding-bottom: env(safe-area-inset-bottom, 20px);
    background: var(--bg);
    color: var(--text);
}

/* ── Layout ────────────────────────────────────────────────── */
.container {
    max-width: 520px;
    margin: 0 auto;
    padding: 20px 16px;
}

/* ── Header ────────────────────────────────────────────────── */
.app-header {
    border-bottom: 1px solid var(--border);
    padding: 14px 16px;
    position: sticky;
    top: 0;
    z-index: 100;
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    backdrop-filter: saturate(180%) blur(20px);
    background: rgba(255,255,255,0.9);
}

.app-header h1 {
    font-size: 1.1rem;
    font-weight: 700;
    text-align: center;
    letter-spacing: -0.01em;
    color: var(--primary);
}

.app-header .back-link {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--primary-light);
    text-decoration: none;
    font-size: 0.95rem;
    font-weight: 500;
    transition: opacity var(--transition);
}

.app-header .back-link:active { opacity: 0.5; }

.header-right {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-nav-link {
    color: var(--primary-light);
    text-decoration: none;
    font-size: 0.875rem;
    font-weight: 500;
    transition: opacity var(--transition);
}

.header-nav-link:active { opacity: 0.5; }

.header-nav-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0;
    font-family: inherit;
}

.header-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
}

.header-subtitle {
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--text-secondary);
    letter-spacing: 0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
    margin: 0;
}

/* ── Footer ────────────────────────────────────────────────── */
.app-footer {
    max-width: 520px;
    margin: 24px auto 0;
    padding: 20px 16px;
    border-top: 1px solid var(--border);
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-align: center;
    line-height: 1.5;
}

.app-footer-links {
    margin-top: 6px;
    display: flex;
    justify-content: center;
    gap: 8px;
}

.app-footer-links a {
    color: var(--primary-light);
    text-decoration: none;
}

.app-footer-links a:active { opacity: 0.5; }

/* ── Cards ─────────────────────────────────────────────────── */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 12px;
    transition: border-color var(--transition), transform var(--transition);
}

.card h3 {
    font-size: 1.05rem;
    margin-bottom: 4px;
    letter-spacing: -0.01em;
}

.card .meta {
    font-size: 0.82rem;
    color: var(--text-secondary);
}

.card-link {
    text-decoration: none;
    color: inherit;
    display: block;
}

.card-link:active .card { transform: scale(0.98); }

@media (hover: hover) {
    .card-link:hover .card {
        border-color: var(--primary);
        transform: translateY(-1px);
    }
}

/* ── Buttons ───────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 12px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: all var(--transition);
    -webkit-tap-highlight-color: transparent;
    position: relative;
    overflow: hidden;
}

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

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

.btn-primary { background: var(--primary); color: #fff; }
@media (hover: hover) { .btn-primary:hover:not(:disabled) { background: var(--primary-light); } }

.btn-success { background: var(--success); color: #000; }
@media (hover: hover) { .btn-success:hover:not(:disabled) { background: #2abd4e; } }

.btn-danger { background: var(--danger); color: #fff; }
@media (hover: hover) { .btn-danger:hover:not(:disabled) { background: #dc2626; } }

.btn-outline { background: transparent; border: 1.5px solid var(--primary); color: var(--primary-light); }
@media (hover: hover) { .btn-outline:hover:not(:disabled) { background: var(--primary-bg); } }

.btn-practice { background: var(--practice); color: #fff; }
@media (hover: hover) { .btn-practice:hover:not(:disabled) { background: var(--practice-light); } }

.btn-ghost { background: transparent; color: var(--text-secondary); padding: 8px 12px; }
@media (hover: hover) { .btn-ghost:hover { color: var(--text); } }

.btn-block { display: flex; width: 100%; }
.btn-lg { padding: 14px 24px; font-size: 1.05rem; border-radius: var(--radius); }
.btn-sm { padding: 8px 14px; font-size: 0.85rem; }

/* ── Forms ──────────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }

.form-group label {
    display: block;
    font-weight: 600;
    font-size: 0.82rem;
    margin-bottom: 6px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.form-group input,
.form-group textarea {
    width: 100%;
    padding: 12px 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-family: inherit;
    color: var(--text);
    background: var(--surface-elevated);
    appearance: none;
    -webkit-appearance: none;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.form-group input:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(108,160,220,0.2);
}

/* ── Capture ───────────────────────────────────────────────── */
.capture-area {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 32px 16px;
    text-align: center;
    cursor: pointer;
    transition: border-color var(--transition), background var(--transition);
    position: relative;
    overflow: hidden;
}

@media (hover: hover) {
    .capture-area:hover { border-color: var(--primary); background: var(--primary-bg); }
}

.capture-area.has-image { border-style: solid; border-color: var(--success); padding: 0; }
.capture-area img { width: 100%; display: block; border-radius: var(--radius); }
.capture-area .placeholder { color: var(--text-secondary); font-size: 0.9rem; }
.capture-area .placeholder .icon { font-size: 2rem; display: block; margin-bottom: 8px; }
.capture-area input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* ── Extraction states ─────────────────────────────────────── */
.capture-area.extracting { border-style: solid; border-color: var(--primary); padding: 24px 16px; }
.capture-area.extract-done { border-style: solid; border-color: var(--success); padding: 16px; }

.extract-progress-bar { width: 100%; height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; }
.extract-progress-fill {
    height: 100%; width: 30%;
    background: linear-gradient(90deg, var(--primary), var(--primary-light));
    border-radius: 2px;
    animation: extractPulse 1.5s ease-in-out infinite;
}

@keyframes extractPulse {
    0%   { width: 20%; margin-left: 0; }
    50%  { width: 50%; margin-left: 25%; }
    100% { width: 20%; margin-left: 80%; }
}

.extract-label { font-size: 0.85rem; color: var(--text-secondary); }
.extract-check { color: var(--success); font-size: 1.1rem; font-weight: 700; flex-shrink: 0; }

/* ── Quiz question ─────────────────────────────────────────── */
.progress-bar { height: 4px; background: var(--border); border-radius: 2px; margin-bottom: 24px; overflow: hidden; }
.progress-bar .fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary), var(--primary-light));
    border-radius: 2px;
    transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.question-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 28px 20px;
    text-align: center;
    margin-bottom: 24px;
}

.question-card .question-text {
    font-size: 1.25rem;
    font-weight: 600;
    line-height: 1.45;
    letter-spacing: -0.01em;
}

.choices { list-style: none; display: flex; flex-direction: column; gap: 10px; }

.choices li button {
    width: 100%;
    padding: 14px 16px;
    background: var(--surface);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-family: inherit;
    color: var(--text);
    text-align: left;
    cursor: pointer;
    transition: all var(--transition);
    -webkit-tap-highlight-color: transparent;
}

@media (hover: hover) {
    .choices li button:hover {
        border-color: var(--primary);
        background: var(--primary-bg);
        transform: translateY(-1px);
    }
}

.choices li button:active {
    border-color: var(--primary);
    background: var(--primary-bg);
    transform: scale(0.98);
}

.choices li button.choice-correct {
    border-color: var(--success);
    background: rgba(48,209,88,0.12);
    color: var(--success);
    font-weight: 700;
}

.choices li button.choice-wrong {
    border-color: var(--danger);
    background: rgba(239,68,68,0.12);
    color: var(--danger);
}

/* ── Results / Stats ───────────────────────────────────────── */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 20px; }

.stat-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 16px;
    text-align: center;
}

.stat-box .value { font-size: 1.8rem; font-weight: 800; letter-spacing: -0.02em; }
.stat-box .label {
    font-size: 0.75rem; color: var(--text-secondary);
    text-transform: uppercase; letter-spacing: 0.05em; margin-top: 4px; font-weight: 500;
}

.stat-box.correct .value { color: var(--success); }
.stat-box.wrong .value { color: var(--danger); }

/* ── Fact list ─────────────────────────────────────────────── */
.fact-list { list-style: none; }

.fact-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: border-color var(--transition), transform var(--transition);
}

@media (hover: hover) {
    .fact-item:hover { border-color: var(--primary); transform: translateY(-1px); }
}

.fact-item .fact-num {
    width: 30px; height: 30px;
    background: linear-gradient(135deg, var(--primary), var(--primary-light));
    color: #fff; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.8rem; font-weight: 700; flex-shrink: 0;
}

.fact-item .fact-body { flex: 1; min-width: 0; }
.fact-item .fact-q {
    font-weight: 600; font-size: 0.95rem;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.fact-item .fact-a {
    font-size: 0.82rem; color: var(--text-secondary);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px;
}

.fact-actions { display: flex; gap: 6px; flex-shrink: 0; }
.fact-actions .btn { padding: 6px 10px; font-size: 0.78rem; border-radius: 8px; }

/* ── Quiz header card ──────────────────────────────────────── */
.quiz-header-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.quiz-header-main { flex: 1; min-width: 0; }
.quiz-header-main h3 {
    margin-top: 4px; white-space: nowrap; overflow: hidden;
    text-overflow: ellipsis; font-size: 1.25rem;
}
.quiz-header-main .meta { font-size: 0.95rem; font-weight: 600; color: var(--text-secondary); }

.quiz-fact-count {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    background: var(--primary-bg); border: 1.5px solid rgba(108,160,220,0.2);
    border-radius: 12px; padding: 8px 12px; flex-shrink: 0;
    font-size: 1.5rem; font-weight: 800; color: var(--primary);
    line-height: 1; letter-spacing: -0.02em; min-width: 52px; text-align: center;
}

.quiz-fact-label {
    display: block; font-size: 0.65rem; font-weight: 600;
    text-transform: uppercase; letter-spacing: 0.05em;
    color: var(--text-secondary); margin-top: 3px;
}

/* ── Empty state ───────────────────────────────────────────── */
.empty-state { text-align: center; padding: 48px 16px; color: var(--text-secondary); }
.empty-state .icon { font-size: 3rem; margin-bottom: 12px; }
.empty-state p { margin-bottom: 20px; line-height: 1.5; }

/* ── Alert ─────────────────────────────────────────────────── */
.alert { padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 16px; font-size: 0.9rem; font-weight: 500; }
.alert-error { background: rgba(239,68,68,0.12); color: var(--danger); }
.alert-success { background: rgba(48,209,88,0.12); color: var(--success); }

/* ── Action bar ───────────────────────────────────────────── */
.action-bar { display: flex; flex-direction: column; gap: 10px; }
.action-bar-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* ── Form actions ─────────────────────────────────────────── */
.form-actions { display: flex; flex-direction: column; gap: 10px; }

/* ── Divider ──────────────────────────────────────────────── */
.divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

/* ── Feedback animation ───────────────────────────────────── */
.feedback-area { margin-top: 8px; animation: fadeSlideIn 0.25s ease; }

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

/* ── Step indicator ────────────────────────────────────────── */
.step-indicator { display: flex; align-items: center; justify-content: center; gap: 0; margin-bottom: 20px; }

.step-dot {
    width: 28px; height: 28px; border-radius: 50%;
    background: var(--surface-elevated); border: 2px solid var(--border);
    color: var(--text-secondary); font-size: 0.8rem; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    transition: all var(--transition); flex-shrink: 0;
}

.step-dot.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.step-line { flex: 1; max-width: 80px; height: 2px; background: var(--border); margin: 0 8px; }
.step-hidden { display: none !important; }

/* ── Practice mode ─────────────────────────────────────────── */
.practice-stats-bar { display: flex; gap: 8px; margin-bottom: 20px; }

.practice-stat {
    flex: 1; background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-sm); padding: 10px 8px; text-align: center;
}

.practice-stat-value {
    display: block; font-size: 1.5rem; font-weight: 800;
    letter-spacing: -0.02em; line-height: 1.1;
}

.practice-stat-value.correct { color: var(--success); }
.practice-stat-value.wrong { color: var(--danger); }

.practice-stat-label {
    display: block; font-size: 0.7rem; color: var(--text-secondary);
    text-transform: uppercase; letter-spacing: 0.05em; margin-top: 2px; font-weight: 500;
}

/* ── Adaptive practice difficulty ─────────────────────────── */
.level-indicator { display: flex; gap: 5px; justify-content: center; margin-bottom: 10px; }
.level-pip {
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--border); transition: background var(--transition);
}
.level-pip--active { background: var(--primary); }

.question-card--new { border-color: var(--practice); }

.first-look-badge {
    display: inline-block; font-size: 0.7rem; font-weight: 700;
    text-transform: uppercase; letter-spacing: 0.07em;
    color: var(--practice); background: var(--practice-bg);
    border-radius: 999px; padding: 2px 10px; margin-bottom: 10px;
}

.choices li button.choice-first-look {
    border-color: var(--practice);
    color: var(--practice);
    font-weight: 600;
}

/* ── Class grouping ────────────────────────────────────────── */
.class-group { margin-bottom: 8px; }
.class-group-header {
    font-size: 0.72rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: 0.07em; color: var(--text-secondary); padding: 4px 4px 8px; margin-top: 16px;
}
.class-group:first-child .class-group-header { margin-top: 0; }

/* ── Utilities ─────────────────────────────────────────────── */
.mt-8  { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.mb-16 { margin-bottom: 16px; }
.text-center { text-align: center; }
.text-secondary { color: var(--text-secondary); }
.text-sm { font-size: 0.85rem; }

/* ── Section titles ───────────────────────────────────────── */
.section-title {
    font-size: 0.72rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: 0.07em; color: var(--text-secondary); padding: 4px 4px 8px;
}

/* ── Share panel ──────────────────────────────────────────── */
.share-url-row { display: flex; gap: 8px; align-items: center; }

/* ── Feature rows (landing page) ─────────────────────────── */
.feature-row {
    display: flex; gap: 12px; align-items: flex-start;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}
.feature-row:last-child { border-bottom: none; }

.feature-icon {
    width: 28px; height: 28px; border-radius: 50%;
    background: var(--primary); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.8rem; font-weight: 700; flex-shrink: 0;
}

/* ── Chart canvas ─────────────────────────────────────────── */
canvas { display: block; margin: 0 auto; }

/* ── Store / Bookstore ───────────────────────────────────── */
.store-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 16px;
}

.store-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    transition: border-color var(--transition), transform var(--transition);
    text-decoration: none;
    color: inherit;
}

@media (hover: hover) {
    .store-card:hover {
        border-color: var(--primary);
        transform: translateY(-1px);
    }
}

.store-card:active { transform: scale(0.98); }

.store-card-title {
    font-size: 0.92rem;
    font-weight: 700;
    line-height: 1.3;
    color: var(--text);
}

.store-card-meta {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.price-badge {
    display: inline-block;
    background: var(--primary-bg);
    color: var(--primary-light);
    font-size: 0.78rem;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 20px;
    margin-top: auto;
    align-self: flex-start;
}

/* Stripe card element mount point */
.stripe-card-wrapper {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px 12px;
    background: var(--surface-elevated);
    margin-bottom: 12px;
}

/* Payment modal overlay */
.payment-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 200;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

@media (min-width: 480px) {
    .payment-overlay { align-items: center; }
}

.payment-sheet {
    background: var(--bg);
    border-radius: var(--radius) var(--radius) 0 0;
    padding: 24px 20px;
    width: 100%;
    max-width: 480px;
    box-shadow: var(--shadow-lg);
}

@media (min-width: 480px) {
    .payment-sheet { border-radius: var(--radius); }
}

.payment-sheet h2 {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 4px;
}

.payment-sheet .payment-amount {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 16px;
}

/* Store section on dashboard */
.store-section-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    margin-bottom: 10px;
    margin-top: 24px;
}

.store-section-header h2 {
    font-size: 1rem;
    font-weight: 700;
}

.store-section-header a {
    font-size: 0.82rem;
    color: var(--primary-light);
    text-decoration: none;
}

/* Footer store widget */
.footer-store-widget {
    margin-bottom: 10px;
}

.footer-store-link {
    color: var(--primary-light) !important;
    font-weight: 500;
    font-size: 0.8rem;
    text-decoration: none;
}

.footer-store-link:active { opacity: 0.5; }

/* Preview facts in store item detail */
.preview-fact {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    margin-bottom: 8px;
}

.preview-fact .pf-q {
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-bottom: 2px;
}

.preview-fact .pf-a {
    font-size: 0.9rem;
    font-weight: 600;
}

/* ── Responsive ──────────────────────────────────────────── */
@media (min-width: 640px) {
    .container { padding: 24px; }
    .stat-grid { gap: 16px; }
    .question-card { padding: 36px 28px; }
    .question-card .question-text { font-size: 1.35rem; }
}
