:root {
    --bg: #f5f5f0;
    --bg-alt: #ffffff;
    --text: #1a1a1a;
    --text-muted: #555;
    --card-bg: #ffffff;
    --card-border: #d4d4c8;
    --card-shadow: rgba(0,0,0,0.08);
    --primary: #2d6a4f;
    --primary-hover: #1b4d3b;
    --primary-light: #e8f5ee;
    --warning: #d97706;
    --danger: #c0392b;
    --success: #27ae60;
    --nav-bg: #1a3a2a;
    --nav-text: #f0f0e8;
    --input-bg: #ffffff;
    --input-border: #bbb;
    --code-bg: #f0f0e8;
    --focus-ring: rgba(45,106,79,0.25);
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
}

html.dark {
    --bg: #121212;
    --bg-alt: #1e1e1e;
    --text: #e0e0d8;
    --text-muted: #999;
    --card-bg: #1e1e1e;
    --card-border: #333;
    --card-shadow: rgba(0,0,0,0.4);
    --primary: #4caf7a;
    --primary-hover: #66c791;
    --primary-light: #1a2e22;
    --warning: #d97706;
    --danger: #e74c3c;
    --success: #2ecc71;
    --nav-bg: #0d1f17;
    --nav-text: #c0c0b8;
    --input-bg: #2a2a2a;
    --input-border: #444;
    --code-bg: #2a2a2a;
    --focus-ring: rgba(76,175,122,0.3);
}

html.dark .mol-img { filter: invert(0.9) hue-rotate(180deg); }

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

body {
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

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

/* ─── Navbar ─── */
.navbar {
    background: var(--nav-bg);
    color: var(--nav-text);
    padding: 0.6rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 100;
}
.navbar .brand {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--nav-text);
    text-decoration: none;
    letter-spacing: 0.03em;
}
.navbar .brand:hover { text-decoration: none; opacity: 0.9; }
.navbar .nav-links { display: flex; align-items: center; gap: 0.8rem; }
.navbar .nav-links a {
    color: var(--nav-text);
    text-decoration: none;
    padding: 0.3rem 0.6rem;
    border-radius: 4px;
    font-size: 0.9rem;
    transition: background 0.15s;
}
.navbar .nav-links a:hover { background: rgba(255,255,255,0.1); text-decoration: none; }
.navbar .nav-links a.active { background: rgba(255,255,255,0.15); font-weight: 600; }
.nav-toggle {
    display: none;
    background: none;
    border: none;
    color: var(--nav-text);
    font-size: 1.4rem;
    cursor: pointer;
    padding: 0.2rem;
}

/* ─── Theme toggle ─── */
.theme-toggle {
    background: none;
    border: 1px solid rgba(255,255,255,0.3);
    color: var(--nav-text);
    cursor: pointer;
    padding: 0.3rem 0.6rem;
    border-radius: 4px;
    font-size: 0.9rem;
    transition: background 0.15s;
}
.theme-toggle:hover { background: rgba(255,255,255,0.15); }

/* ─── Container ─── */
.container {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1.5rem 1rem;
    width: 100%;
    flex: 1;
}

/* ─── Cards ─── */
.card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 1.2rem;
    box-shadow: 0 1px 3px var(--card-shadow);
}
.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 1rem;
}
.molecule-card {
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
    text-align: center;
    position: relative;
}
.molecule-card.favorite-active {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
    background: var(--primary-light);
}
.molecule-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--card-shadow);
}
.molecule-card img {
    max-width: 100%;
    height: 120px;
    object-fit: contain;
    display: block;
    margin: 0 auto 0.5rem;
}
.btn-star-card {
    position: absolute;
    top: 0.4rem;
    right: 0.4rem;
    z-index: 2;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}
.molecule-card .fav-star {
    display: block;
    width: 18px;
    height: 18px;
    margin: 0;
}
.molecule-card h3 { font-size: 1rem; margin-bottom: 0.2rem; }
.molecule-card .abbrev { color: var(--text-muted); font-size: 0.85rem; }
.molecule-card .formula { color: var(--text-muted); font-size: 0.8rem; }

/* ─── Buttons ─── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1.2rem;
    border-radius: 6px;
    border: none;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s;
    text-decoration: none;
    gap: 0.4rem;
}
.btn:hover { text-decoration: none; opacity: 0.9; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--code-bg); color: var(--text); border: 1px solid var(--card-border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-warning { background: var(--warning); color: #fff; }
.btn-warning:hover { background: #b45309; }
.btn-success { background: var(--success); color: #fff; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ─── Forms ─── */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; margin-bottom: 0.3rem; font-weight: 600; font-size: 0.9rem; }
.form-input {
    width: 100%;
    padding: 0.6rem 0.8rem;
    border: 1px solid var(--input-border);
    border-radius: 6px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 0.95rem;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.form-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--focus-ring);
}
.form-select {
    width: 100%;
    padding: 0.6rem 0.8rem;
    border: 1px solid var(--input-border);
    border-radius: 6px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 0.95rem;
}

/* ─── Flashcard ─── */
.flashcard-container {
    max-width: 500px;
    margin: 1.5rem auto;
    perspective: 1200px;
}
.flashcard {
    position: relative;
    width: 100%;
    padding-bottom: 70%;
    cursor: pointer;
    transition: transform 0.5s;
    transform-style: preserve-3d;
}
.flashcard.flipped { transform: rotateY(180deg); }
.flashcard-face {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    box-shadow: 0 4px 20px var(--card-shadow);
}
.flashcard-back {
    transform: rotateY(180deg);
}
.flashcard img {
    max-width: 90%;
    max-height: 60%;
    object-fit: contain;
}
.flashcard h2 { font-size: 1.4rem; margin-bottom: 0.5rem; text-align: center; }
.flashcard .detail { font-size: 0.95rem; color: var(--text-muted); text-align: center; margin-bottom: 0.3rem; }
.flashcard-progress {
    text-align: center;
    margin-bottom: 0.8rem;
    color: var(--text-muted);
    font-size: 0.85rem;
}
.flashcard-actions {
    display: flex;
    gap: 0.8rem;
    justify-content: center;
    margin-top: 1.2rem;
    flex-wrap: wrap;
}
.familiarity-dots {
    display: flex;
    gap: 0.3rem;
    justify-content: center;
    margin-top: 0.8rem;
}
.familiarity-dots span {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--card-border);
    transition: background 0.2s;
}
.familiarity-dots span.filled { background: var(--primary); }

.rating-legend {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 0.5rem;
    text-align: center;
    margin-top: 1rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    max-width: 400px;
    margin-left: auto;
    margin-right: auto;
}
.rating-legend .label-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    vertical-align: middle;
    margin-right: 0.2rem;
}
.rating-legend .dot-again { background: var(--danger); }
.rating-legend .dot-good { background: var(--warning); }
.rating-legend .dot-easy { background: var(--success); }
.rating-legend strong { color: var(--text); }
.rating-legend .muted { color: var(--text-muted); }

.skip-row {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    margin-top: 0.6rem;
}
.skip-row .btn {
    background: var(--code-bg);
    color: var(--text);
    border: 1px solid var(--card-border);
    font-size: 0.9rem;
    padding: 0.5rem 1.4rem;
}
.skip-row .btn:hover:not(:disabled) {
    background: var(--card-border);
}
.skip-row .btn:disabled {
    opacity: 0.4;
}
.skip-row .btn-star {
    padding: 0.4rem 0.6rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
}
.skip-row .btn-star.starred img {
    filter: drop-shadow(0 0 2px rgba(245, 158, 11, 0.6));
}
.btn-star {
    padding: 0.4rem 0.6rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
}

/* ─── Quiz ─── */
.quiz-question {
    margin-bottom: 1.5rem;
    padding: 1.2rem;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
}
.quiz-question h3 { font-size: 1rem; margin-bottom: 0.8rem; }
.quiz-question img {
    max-height: 80px;
    object-fit: contain;
    display: block;
    margin-bottom: 0.8rem;
}
.quiz-options { display: grid; gap: 0.5rem; grid-template-columns: 1fr; }
@media (min-width: 600px) {
    .quiz-options { grid-template-columns: 1fr 1fr; }
}
.quiz-option {
    display: block;
    padding: 0.7rem 1rem;
    border: 1px solid var(--card-border);
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.1s, border-color 0.1s;
    background: var(--input-bg);
    font-size: 0.95rem;
}
.quiz-option:hover { border-color: var(--primary); background: var(--primary-light); }
.quiz-option.selected { border-color: var(--primary); background: var(--primary-light); font-weight: 600; }
.quiz-option.correct { border-color: var(--success); background: #d4edda; color: #155724; }
html.dark .quiz-option.correct { border-color: var(--success); background: #1a3a25; color: #8bddb0; }
.quiz-option.incorrect { border-color: var(--danger); background: #f8d7da; color: #721c24; }
html.dark .quiz-option.incorrect { border-color: var(--danger); background: #3d1a1a; color: #f5a0a0; }

.quiz-option.review-option { cursor: default; opacity: 0.9; }
.quiz-option-image { padding: 0.5rem; text-align: center; }
.quiz-option-image img { display: block; margin: 0 auto; max-width: 100%; }
.quiz-progress {
    display: flex;
    gap: 0.3rem;
    margin-bottom: 1.2rem;
}
.quiz-progress span {
    flex: 1;
    height: 6px;
    border-radius: 3px;
    background: var(--card-border);
    transition: background 0.2s;
}
.quiz-progress span.answered { background: var(--primary); }
.quiz-progress span.current { background: var(--primary); opacity: 0.6; }

/* ─── Category tabs ─── */
.category-tabs {
    display: flex;
    gap: 0.4rem;
    margin-bottom: 1.2rem;
    flex-wrap: wrap;
}
.category-tabs a {
    padding: 0.4rem 1rem;
    border-radius: 20px;
    font-size: 0.85rem;
    border: 1px solid var(--card-border);
    color: var(--text);
    text-decoration: none;
    transition: background 0.15s;
}
.category-tabs a:hover { background: var(--primary-light); text-decoration: none; }
.category-tabs a.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}
#tab-fav.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}
.category-tabs .sep {
    color: var(--text-muted);
    margin: 0 0.3rem;
    display: flex;
    align-items: center;
}

/* ─── Alerts ─── */
.alert {
    padding: 0.8rem 1rem;
    border-radius: 6px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}
.alert-error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
html.dark .alert-error { background: #3d1a1a; color: #f5a0a0; border-color: #5a2020; }
.alert-success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
html.dark .alert-success { background: #1a3a25; color: #8bddb0; border-color: #2a5a35; }

/* ─── Flash messages ─── */
.flashes { list-style: none; margin-bottom: 1rem; }

/* ─── Auth pages ─── */
.auth-form {
    max-width: 400px;
    margin: 2rem auto;
}

/* ─── Molecule detail ─── */
.mol-detail {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}
.mol-detail img {
    width: 100%;
    max-height: 300px;
    object-fit: contain;
}
.mol-detail .info-table { width: 100%; border-collapse: collapse; }
.mol-detail .info-table td {
    padding: 0.4rem 0.6rem;
    border-bottom: 1px solid var(--card-border);
    font-size: 0.9rem;
}
.mol-detail .info-table td:first-child {
    font-weight: 600;
    width: 40%;
    color: var(--text-muted);
}
@media (max-width: 600px) {
    .mol-detail { grid-template-columns: 1fr; }
}

.info-table td { padding: 0.4rem 1rem 0.4rem 0; border-bottom: 1px solid var(--card-border); font-size: 0.9rem; }
.info-table td:first-child { font-weight: 600; white-space: nowrap; color: var(--text-muted); }

/* ─── Dashboard ─── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}
.stat-card {
    text-align: center;
    padding: 1.2rem;
}
.stat-card .stat-value { font-size: 2rem; font-weight: 700; color: var(--primary); }
.stat-card .stat-label { font-size: 0.85rem; color: var(--text-muted); }
.quick-launch {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
}
.quick-launch-actions {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
}
.quick-launch-divider {
    height: 0;
    margin: 0.8rem 0;
    border-top: 1px solid var(--card-border);
}

/* ─── Search ─── */
.search-bar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}
.search-bar .form-input { flex: 1; }

/* ─── Footer ─── */
.footer {
    text-align: center;
    padding: 1rem;
    color: var(--text-muted);
    font-size: 0.8rem;
    border-top: 1px solid var(--card-border);
    margin-top: auto;
}

/* ─── Quiz results ─── */
.score-display {
    text-align: center;
    padding: 2rem;
    margin-bottom: 1.5rem;
}
.score-display .score-num { font-size: 3rem; font-weight: 700; color: var(--primary); }
.score-display .score-label { font-size: 1rem; color: var(--text-muted); }

/* ─── Responsive ─── */
@media (max-width: 768px) {
    .navbar .nav-links {
        display: none;
        flex-direction: column;
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        background: var(--nav-bg);
        padding: 0.5rem 1rem;
        gap: 0.3rem;
    }
    .navbar .nav-links.open { display: flex; }
    .nav-toggle { display: block; }
    .card-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
    .container { padding: 1rem 0.6rem; }
    .flashcard-container { max-width: 100%; }
    .flashcard { padding-bottom: 90%; }
}

@media (max-width: 480px) {
    .card-grid { grid-template-columns: 1fr 1fr; gap: 0.6rem; }
    .molecule-card { padding: 0.8rem; }
    .molecule-card img { height: 80px; }
    .molecule-card h3 { font-size: 0.85rem; }
}
