/* ============================================
   Course Viewer v2 — Sidebar + Thumbnails
   
   Layout: fixed topbar + left sidebar + main content
   Uses CSS variables from main.css where possible.
   ============================================ */

:root {
    --cv-sidebar-w: 260px;
    --cv-topbar-h: 52px;
}

/* ---- Base ---- */
.cv-page {
    font-family: 'DM Sans', -apple-system, sans-serif;
    background: var(--bg-deep, #0a0e1a);
    color: var(--text-primary, #f1f5f9);
}
.cv-page main { padding: 0; }

/* ---- Viewer Topbar ---- */
.cv-topbar {
    position: fixed; top: 0; left: 0; right: 0; z-index: 50;
    height: var(--cv-topbar-h);
    display: flex; align-items: center; gap: 0.75rem;
    padding: 0 1rem;
    background: var(--bg-card, #111827);
    border-bottom: 1px solid var(--border-subtle);
    backdrop-filter: blur(12px);
}
.cv-topbar-back {
    color: var(--text-secondary); font-size: 0.82rem; font-weight: 500;
    display: flex; align-items: center; gap: 0.25rem;
    text-decoration: none; flex-shrink: 0; transition: color 0.15s;
}
.cv-topbar-back:hover { color: var(--text-primary); }
.cv-topbar-title {
    font-family: 'Source Serif 4', serif; font-weight: 600; font-size: 1rem;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cv-topbar-spacer { flex: 1; }
.cv-topbar-filters { display: flex; gap: 0.3rem; flex-shrink: 0; }

.cv-filter-btn {
    padding: 0.3rem 0.6rem; font-size: 0.78rem; font-weight: 600;
    border-radius: 5px; border: 1px solid var(--border-subtle);
    color: var(--text-secondary); background: transparent;
    cursor: pointer; transition: all 0.15s; white-space: nowrap;
}
.cv-filter-btn:hover, .cv-filter-btn.active {
    background: rgba(52,211,153,0.1); color: var(--accent-bio, #34d399);
    border-color: var(--accent-bio, #34d399);
}

.cv-topbar-search {
    width: 180px; padding: 0.35rem 0.7rem; font-size: 0.82rem;
    font-family: inherit; border: 1px solid var(--border-subtle);
    border-radius: 6px; background: var(--bg-deep); color: var(--text-primary);
    outline: none; transition: border-color 0.2s, width 0.2s;
}
.cv-topbar-search:focus { border-color: var(--accent-bio, #34d399); width: 240px; }
.cv-topbar-search::placeholder { color: var(--text-dim); }

.cv-cc-btn {
    padding: 0.3rem 0.55rem; font-size: 0.72rem; font-weight: 700;
    border-radius: 5px; border: 1px solid var(--border-subtle);
    color: var(--text-dim); background: transparent;
    cursor: pointer; transition: all 0.15s; letter-spacing: 0.03em;
}
.cv-cc-btn:hover { border-color: #2563eb; color: #60a5fa; }
.cv-cc-btn.active { background: #2563eb; border-color: #2563eb; color: white; }

.cv-sidebar-toggle {
    display: none; width: 32px; height: 32px; border-radius: 6px;
    border: 1px solid var(--border-subtle); background: var(--bg-deep);
    color: var(--text-secondary); font-size: 1rem; cursor: pointer;
    align-items: center; justify-content: center;
}

/* ---- Sidebar ---- */
.cv-sidebar {
    position: fixed; top: var(--cv-topbar-h); left: 0; bottom: 0;
    width: var(--cv-sidebar-w); overflow-y: auto;
    background: var(--bg-card, #111827);
    border-right: 1px solid var(--border-subtle);
    padding: 0.75rem 0; z-index: 40; transition: transform 0.3s ease;
}
.cv-sidebar::-webkit-scrollbar { width: 3px; }
.cv-sidebar::-webkit-scrollbar-thumb { background: var(--border-subtle); border-radius: 3px; }
.cv-sidebar-label {
    padding: 0.5rem 1rem 0.3rem; font-size: 0.6rem; font-weight: 700;
    letter-spacing: 0.1em; text-transform: uppercase; color: var(--text-dim);
}
.cv-sidebar-unit {
    display: flex; align-items: center; gap: 0.5rem;
    padding: 0.45rem 1rem; cursor: pointer; transition: all 0.12s;
    border-left: 3px solid transparent; font-size: 0.82rem;
    font-weight: 500; color: var(--text-secondary);
}
.cv-sidebar-unit:hover { background: var(--bg-card-hover); color: var(--text-primary); }
.cv-sidebar-unit.active {
    background: rgba(52,211,153,0.08); color: var(--accent-bio, #34d399);
    border-left-color: var(--accent-bio, #34d399);
}
.cv-sidebar-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.cv-sidebar-name { flex: 1; line-height: 1.3; }
.cv-sidebar-count {
    font-size: 0.68rem; font-weight: 600; color: var(--text-dim);
    background: var(--bg-deep); padding: 0.08rem 0.4rem; border-radius: 100px;
}

/* ---- Main ---- */
.cv-main {
    margin-left: var(--cv-sidebar-w); margin-top: var(--cv-topbar-h);
    min-height: calc(100vh - var(--cv-topbar-h));
    padding: 1.5rem 1.75rem 3rem; max-width: 880px;
}
.cv-unit-header {
    margin-bottom: 1.5rem; padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border-subtle);
}
.cv-unit-header h2 {
    font-family: 'Source Serif 4', serif; font-weight: 600;
    font-size: 1.4rem; line-height: 1.2; margin-bottom: 0.15rem;
}
.cv-unit-meta { font-size: 0.82rem; color: var(--text-secondary); }

/* ---- Topic blocks ---- */
.cv-topic-block {
    margin-bottom: 1rem; border-radius: 8px; border: 1px solid var(--border-subtle);
    background: var(--bg-card); overflow: hidden; transition: border-color 0.2s;
}
.cv-topic-block:hover { border-color: var(--border-hover); }
.cv-topic-head {
    display: flex; align-items: center; gap: 0.6rem;
    padding: 0.75rem 1rem; cursor: pointer; transition: background 0.12s;
    user-select: none;
}
.cv-topic-head:hover { background: var(--bg-card-hover); }
.cv-topic-dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.cv-topic-code { font-size: 0.75rem; font-weight: 700; color: var(--text-dim); min-width: 2.2rem; }
.cv-topic-name { font-weight: 600; font-size: 0.9rem; flex: 1; }
.cv-topic-count { font-size: 0.68rem; color: var(--text-dim); font-weight: 500; }
.cv-topic-chevron { font-size: 0.65rem; color: var(--text-dim); transition: transform 0.2s; }
.cv-topic-block.open .cv-topic-chevron { transform: rotate(90deg); }
.cv-topic-resources {
    display: none; padding: 0 1rem 0.75rem;
    border-top: 1px solid var(--border-subtle);
}
.cv-topic-block.open .cv-topic-resources { display: block; }

/* ---- Type groups inside topic ---- */
.cv-type-group { margin-top: 0.65rem; }
.cv-type-label {
    font-size: 0.68rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: 0.08em; color: var(--text-dim);
    display: flex; align-items: center; gap: 0.35rem; margin-bottom: 0.4rem;
}

/* ---- Resource items ---- */
.cv-res {
    display: flex; align-items: flex-start; gap: 0.65rem;
    padding: 0.5rem; margin-bottom: 0.25rem;
    border-radius: 6px; transition: background 0.12s;
}
.cv-res:hover { background: var(--bg-card-hover); }

.cv-res-thumb {
    flex-shrink: 0; width: 96px; height: 54px;
    border-radius: 4px; overflow: hidden; background: var(--bg-deep);
}
.cv-res-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cv-res-thumb-ph {
    width: 100%; height: 100%; display: flex;
    align-items: center; justify-content: center;
    font-size: 1.3rem; color: var(--text-dim);
}

.cv-res-body { flex: 1; min-width: 0; }
.cv-res-link {
    color: var(--accent-env, #60a5fa); font-weight: 600; font-size: 0.85rem;
    text-decoration: none; transition: color 0.15s;
    display: block; overflow: hidden; text-overflow: ellipsis;
}
.cv-res-link:hover { text-decoration: underline; }
.cv-res-plain { color: var(--text-primary); font-weight: 600; font-size: 0.85rem; }
.cv-res-meta {
    font-size: 0.75rem; color: var(--text-dim);
    display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap;
    margin-top: 0.1rem;
}
.cv-res-tags { display: flex; gap: 0.25rem; margin-top: 0.2rem; flex-wrap: wrap; }
.cv-rtag {
    font-size: 0.62rem; font-weight: 600; padding: 0.1rem 0.35rem;
    border-radius: 3px; white-space: nowrap;
}
.cv-rtag-wida { background: rgba(96,165,250,0.15); color: #93c5fd; }
.cv-rtag-pace { background: rgba(52,211,153,0.12); color: #6ee7b7; }
.cv-rtag-visuals { background: rgba(244,114,182,0.12); color: #f9a8d4; }

.cv-res-qr {
    flex-shrink: 0; width: 48px; height: 48px;
    border-radius: 4px; overflow: hidden;
    background: var(--bg-deep); border: 1px solid var(--border-subtle);
    align-self: center;
}
.cv-res-qr img { width: 100%; height: 100%; display: block; }

.cv-res-notes {
    font-size: 0.75rem; color: var(--text-secondary); font-style: italic;
    padding: 0.1rem 0 0.3rem calc(96px + 0.65rem);
}

.cv-cc-badge {
    display: inline-block; font-size: 0.58rem; font-weight: 700;
    background: rgba(37,99,235,0.12); color: #60a5fa;
    padding: 0.08rem 0.3rem; border-radius: 3px;
    margin-left: 0.3rem; vertical-align: middle; letter-spacing: 0.04em;
}

/* ---- States ---- */
.cv-empty {
    text-align: center; padding: 3rem 1rem; color: var(--text-dim);
}
.cv-empty-icon { font-size: 2.5rem; margin-bottom: 0.75rem; }

.cv-loading {
    display: flex; flex-direction: column; align-items: center;
    justify-content: center; min-height: 40vh;
    color: var(--text-secondary); font-size: 0.9rem;
}
.cv-spinner {
    width: 32px; height: 32px;
    border: 3px solid var(--border-subtle);
    border-top-color: var(--accent-bio, #34d399);
    border-radius: 50%; animation: cv-spin 0.8s linear infinite;
    margin-bottom: 1rem;
}
@keyframes cv-spin { to { transform: rotate(360deg); } }

.cv-error { text-align: center; padding: 3rem 1rem; color: var(--text-secondary); }
.cv-error h2 { font-size: 1.1rem; margin-bottom: 0.5rem; color: var(--text-primary); }
.cv-error p { max-width: 400px; margin: 0 auto; line-height: 1.6; }
.cv-error a { color: var(--accent-bio); }
.cv-retry {
    display: inline-block; margin-top: 1rem; padding: 0.45rem 1.1rem;
    background: var(--accent-bio, #34d399); color: #0a0e1a;
    border: none; border-radius: 6px; cursor: pointer; font-size: 0.82rem; font-weight: 600;
}

.cv-fallback-banner {
    text-align: center; padding: 0.45rem 0.75rem;
    background: rgba(251,191,36,0.12); color: #fbbf24;
    font-size: 0.75rem; border-radius: 6px; margin-bottom: 1rem;
}

/* ---- Footer ---- */
.cv-footer {
    text-align: center; padding: 1.5rem 1rem;
    margin-top: 2rem; margin-left: var(--cv-sidebar-w);
    border-top: 1px solid var(--border-subtle);
    font-size: 0.75rem; color: var(--text-dim);
}
.cv-footer-sep { margin: 0 0.35rem; }
.cv-footer-link { color: var(--accent-env, #60a5fa); text-decoration: none; }
.cv-footer-link:hover { text-decoration: underline; }

/* ---- Light theme ---- */
[data-theme="light"] .cv-page { background: #f5f6f8; color: #1a202c; }
[data-theme="light"] .cv-topbar { background: #fff; }
[data-theme="light"] .cv-sidebar { background: #fff; }
[data-theme="light"] .cv-topic-block { background: #fff; }
[data-theme="light"] .cv-res-link { color: #2563eb; }
[data-theme="light"] .cv-rtag-wida { background: #dbeafe; color: #1e40af; }
[data-theme="light"] .cv-rtag-pace { background: #d1fae5; color: #065f46; }
[data-theme="light"] .cv-rtag-visuals { background: #fce7f3; color: #9d174d; }
[data-theme="light"] .cv-fallback-banner { background: #fef3c7; color: #92400e; }
[data-theme="light"] .cv-sidebar-unit.active { background: rgba(5,150,105,0.08); color: #059669; border-left-color: #059669; }
[data-theme="light"] .cv-filter-btn:hover, [data-theme="light"] .cv-filter-btn.active { background: rgba(5,150,105,0.08); color: #059669; border-color: #059669; }

/* ---- Accessibility ---- */
[data-dyslexic="true"] .cv-page,
[data-dyslexic="true"] .cv-page input,
[data-dyslexic="true"] .cv-page button {
    font-family: "OpenDyslexic", "DM Sans", sans-serif !important;
}
[data-dyslexic="true"] .cv-topbar-title,
[data-dyslexic="true"] .cv-unit-header h2 {
    font-family: "OpenDyslexic", "Source Serif 4", serif !important;
}
[data-spacing="true"] .cv-topic-name { line-height: 1.7; }
[data-spacing="true"] .cv-res-link { line-height: 1.6; white-space: normal; }

[data-colorblind="true"] .cv-topic-dot,
[data-colorblind="true"] .cv-sidebar-dot {
    border-radius: 0; background: var(--text-dim) !important;
    border: 2px solid var(--text-secondary);
}
[data-colorblind="true"] .cv-rtag-wida,
[data-colorblind="true"] .cv-rtag-pace,
[data-colorblind="true"] .cv-rtag-visuals { border: 1px solid currentColor; }

/* ---- Mobile ---- */
@media (max-width: 768px) {
    .cv-sidebar { transform: translateX(-100%); z-index: 55; }
    .cv-sidebar.open { transform: translateX(0); box-shadow: 8px 0 24px rgba(0,0,0,0.3); }
    .cv-main { margin-left: 0; padding: 1rem 0.75rem 3rem; }
    .cv-footer { margin-left: 0; }
    .cv-sidebar-toggle { display: flex; }
    .cv-topbar-search { width: 120px; }
    .cv-topbar-search:focus { width: 160px; }
    .cv-topbar-title { display: none; }
    .cv-res-thumb { width: 76px; height: 43px; }
    .cv-res-qr { width: 40px; height: 40px; }
    .cv-res-notes { padding-left: calc(76px + 0.65rem); }
    .cv-res { gap: 0.5rem; }
    .cv-topbar-filters { display: none; }
}
@media (max-width: 480px) {
    .cv-res-thumb { display: none; }
    .cv-res-notes { padding-left: 0; }
}

/* ---- Print ---- */
@media print {
    .cv-topbar, .cv-sidebar, .cv-sidebar-toggle { display: none !important; }
    .cv-main { margin-left: 0; padding: 0; }
    .cv-footer { margin-left: 0; }
    .cv-topic-block.open .cv-topic-resources { display: block; }
    .cv-res-qr { width: 60px; height: 60px; }
}
