:root {
    --bg: #0f1117;
    --bg-2: #161922;
    --bg-3: #1d2230;
    --border: #262b3a;
    --text: #e6e8ee;
    --text-dim: #9aa3b2;
    --accent: #7c5cff;
    --accent-2: #00d4aa;
    --warn: #ff9f43;
    --code-bg: #0b0e15;
    --shadow: 0 8px 24px rgba(0,0,0,.35);
}

[data-theme="light"] {
    --bg: #f8fafc;
    --bg-2: #ffffff;
    --bg-3: #f1f5f9;
    --border: #e2e8f0;
    --text: #0f172a;
    --text-dim: #475569;
    --accent: #6d4cff;
    --accent-2: #00a386;
    --code-bg: #1e293b;
    --shadow: 0 4px 16px rgba(15,23,42,.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { overflow-x: hidden; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
    overflow-x: hidden;
}

/* Topbar */
.topbar {
    position: sticky; top: 0; z-index: 50;
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 24px;
    background: linear-gradient(180deg, var(--bg-2), color-mix(in oklab, var(--bg-2) 90%, transparent));
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand .logo {
    width: 42px; height: 42px; border-radius: 10px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    display: grid; place-items: center; font-size: 20px;
    box-shadow: var(--shadow);
}
.brand h1 { margin: 0; font-size: 18px; letter-spacing: -.01em; }
.brand p  { margin: 2px 0 0; font-size: 12px; color: var(--text-dim); }

.menu-toggle {
    display: none;
    background: var(--bg-3); border: 1px solid var(--border);
    color: var(--text);
    width: 38px; height: 38px;
    border-radius: 8px; cursor: pointer;
    font-size: 18px; line-height: 1;
}
.menu-toggle:hover { border-color: var(--accent); color: var(--accent); }

.sidebar-backdrop {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 40;
    backdrop-filter: blur(2px);
}
.sidebar-backdrop.show { display: block; }

.topbar-actions { display: flex; gap: 8px; align-items: center; }
.topbar-actions input[type="search"] {
    background: var(--bg-3); border: 1px solid var(--border);
    color: var(--text); padding: 8px 14px; border-radius: 999px;
    width: 280px; outline: none; font-size: 14px;
}
.topbar-actions input[type="search"]:focus { border-color: var(--accent); }
.topbar-actions button {
    background: var(--bg-3); border: 1px solid var(--border);
    color: var(--text); padding: 8px 12px; border-radius: 8px; cursor: pointer;
}

/* Layout */
.layout {
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    min-height: calc(100vh - 70px);
}
.sidebar {
    border-right: 1px solid var(--border);
    background: var(--bg-2);
    padding: 18px 14px;
    overflow-y: auto;
    max-height: calc(100vh - 70px);
    position: sticky; top: 70px;
}
.content {
    padding: 30px 40px 80px;
    max-width: 1100px;
    min-width: 0;
}

/* Progress card */
.progress-card {
    background: var(--bg-3);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px;
    margin-bottom: 18px;
}
.progress-row { margin-bottom: 12px; }
.progress-row:last-of-type { margin-bottom: 8px; }
.progress-label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .08em; }
.progress-bar { background: var(--bg); height: 6px; border-radius: 999px; overflow: hidden; margin: 6px 0 4px; }
.progress-fill {
    height: 100%; width: 0%;
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    transition: width .4s ease;
}
.progress-fill.read {
    background: linear-gradient(90deg, var(--accent-2), #4ade80);
}
.progress-text { font-size: 12px; color: var(--text-dim); }
.reset-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-dim);
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 11px;
    cursor: pointer;
    margin-top: 4px;
    width: 100%;
}
.reset-btn:hover { color: var(--warn); border-color: var(--warn); }

/* Nav */
.week-block { margin-bottom: 8px; }
.week-title {
    font-size: 12px; text-transform: uppercase; letter-spacing: .1em;
    color: var(--text-dim); padding: 10px 8px 4px;
    display: flex; align-items: center; justify-content: space-between;
    gap: 8px;
}
.week-stat {
    font-size: 10px;
    background: var(--bg-3);
    color: var(--text-dim);
    padding: 1px 6px;
    border-radius: 999px;
    letter-spacing: 0;
    flex-shrink: 0;
}
.day-link {
    display: flex; gap: 10px; align-items: flex-start;
    padding: 8px 10px;
    border-radius: 8px;
    text-decoration: none; color: var(--text);
    font-size: 14px; cursor: pointer;
    border: 1px solid transparent;
}
.day-link:hover { background: var(--bg-3); }
.day-link.active {
    background: color-mix(in oklab, var(--accent) 14%, var(--bg-3));
    border-color: color-mix(in oklab, var(--accent) 35%, transparent);
}
.day-link.pending { color: var(--text-dim); }
.day-tag {
    flex-shrink: 0; width: 36px; text-align: center;
    background: var(--bg-3); border-radius: 6px; padding: 2px 0;
    font-size: 11px; color: var(--text-dim); font-weight: 600;
    transition: background .2s, color .2s;
}
.day-link.active .day-tag { background: var(--accent); color: white; }
.day-link.read .day-tag {
    background: var(--accent-2);
    color: white;
}
.day-link.read .day-text .title { color: var(--text-dim); text-decoration: line-through; text-decoration-color: color-mix(in oklab, var(--accent-2) 40%, transparent); }
.day-link.read.active .day-text .title { color: var(--text); text-decoration: none; }
.day-text { flex: 1; }
.day-text .title { display: block; }
.day-text .sub { display: block; font-size: 11px; color: var(--text-dim); margin-top: 2px; }

/* Welcome */
.welcome h2 { margin-top: 0; font-size: 28px; letter-spacing: -.02em; }
.welcome p { color: var(--text-dim); max-width: 70ch; }
.grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 14px; margin-top: 24px;
}
.card {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
    transition: transform .15s ease, border-color .15s ease;
}
.card:hover { transform: translateY(-2px); border-color: color-mix(in oklab, var(--accent) 40%, var(--border)); }
.card h3 { margin: 0 0 6px; font-size: 15px; }
.card p { margin: 0; font-size: 13px; color: var(--text-dim); }

/* Lesson */
.hidden { display: none; }
.lesson .crumbs { font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.lesson h2 {
    margin: 0 0 10px;
    font-size: 28px; letter-spacing: -.02em;
}
.lesson .meta {
    display: flex; gap: 12px; flex-wrap: wrap;
    color: var(--text-dim); font-size: 13px; margin-bottom: 24px;
}
.lesson .meta span {
    background: var(--bg-2); border: 1px solid var(--border);
    padding: 4px 10px; border-radius: 999px;
}

.lesson section { margin-bottom: 28px; }
.lesson h3 {
    font-size: 18px; margin: 0 0 12px;
    border-left: 3px solid var(--accent);
    padding-left: 10px;
}
.lesson p, .lesson li { color: var(--text); overflow-wrap: anywhere; }
.lesson h2, .lesson h3 { overflow-wrap: anywhere; }
.lesson ul, .lesson ol { padding-left: 22px; }
.lesson a { overflow-wrap: anywhere; }
.lesson code:not(pre code) {
    background: var(--bg-3);
    border: 1px solid var(--border);
    padding: 1px 6px; border-radius: 4px;
    font-size: 0.9em;
    overflow-wrap: anywhere;
    word-break: break-word;
}
.lesson pre {
    background: var(--code-bg) !important;
    border: 1px solid var(--border);
    border-radius: 10px; padding: 14px 16px;
    overflow-x: auto; font-size: 13px;
}
.lesson pre code { background: transparent !important; padding: 0 !important; border: 0 !important; }

.code-context {
    margin: 18px 0 10px;
    line-height: 1.7;
}
.code-context.after {
    margin: 10px 0 28px;
    padding: 14px 18px;
    background: color-mix(in oklab, var(--accent) 4%, var(--bg-2));
    border-left: 3px solid color-mix(in oklab, var(--accent) 50%, var(--border));
    border-radius: 0 8px 8px 0;
}
.code-context p:first-child { margin-top: 0; }
.code-context p:last-child { margin-bottom: 0; }
.code-context strong { color: var(--accent); }

.code-block { margin-bottom: 14px; }
.code-block .code-title {
    background: var(--bg-2); border: 1px solid var(--border);
    border-bottom: 0; border-radius: 10px 10px 0 0;
    padding: 8px 14px; font-size: 12px; color: var(--text-dim);
    display: flex; justify-content: space-between; align-items: center;
}
.code-block .code-title strong { color: var(--text); font-weight: 600; }
.code-block .code-title button {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-dim); padding: 2px 8px; border-radius: 6px;
    font-size: 11px; cursor: pointer;
}
.code-block .code-title button:hover { color: var(--accent); border-color: var(--accent); }
.code-block pre { border-radius: 0 0 10px 10px; margin: 0; }

.callout {
    background: color-mix(in oklab, var(--accent) 8%, var(--bg-2));
    border-left: 3px solid var(--accent);
    padding: 12px 16px; border-radius: 8px;
    margin: 14px 0;
}
.callout.warn {
    background: color-mix(in oklab, var(--warn) 8%, var(--bg-2));
    border-left-color: var(--warn);
}

.notebook-link {
    display: inline-flex; align-items: center; gap: 6px;
    margin-right: 10px;
    color: var(--accent-2); text-decoration: none;
    font-size: 13px;
    background: var(--bg-2); border: 1px solid var(--border);
    padding: 6px 12px; border-radius: 8px;
    max-width: 100%;
    word-break: break-all;
}
.notebook-link:hover { border-color: var(--accent-2); }

/* Read badge in meta */
.read-badge {
    background: color-mix(in oklab, var(--accent-2) 18%, var(--bg-2)) !important;
    border-color: color-mix(in oklab, var(--accent-2) 50%, var(--border)) !important;
    color: var(--accent-2) !important;
    font-weight: 600;
}

/* Finish section — bottom of lesson */
.finish-section {
    margin-top: 48px;
    padding-top: 28px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}
.mark-read-btn {
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    border: 0;
    color: white;
    font-size: 14px;
    font-weight: 600;
    padding: 12px 24px;
    border-radius: 10px;
    cursor: pointer;
    box-shadow: var(--shadow);
    transition: transform .15s ease, box-shadow .15s ease;
}
.mark-read-btn:hover { transform: translateY(-2px); }
.mark-read-btn.is-read {
    background: var(--bg-3);
    color: var(--accent-2);
    border: 1px solid var(--accent-2);
    box-shadow: none;
}
.next-lesson-btn {
    background: var(--bg-2);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 14px;
    padding: 12px 20px;
    border-radius: 10px;
    cursor: pointer;
    transition: border-color .15s, color .15s;
    margin-left: auto;
}
.next-lesson-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}
.finish-final {
    margin-left: auto;
    color: var(--text-dim);
    font-size: 14px;
}

/* ===== Tablet ===== */
@media (max-width: 1024px) {
    .topbar-actions input[type="search"] { width: 200px; }
    .content { padding: 24px 28px 60px; }
    .layout { grid-template-columns: 280px 1fr; }
}

/* ===== Mobile ===== */
@media (max-width: 768px) {
    .menu-toggle { display: grid; place-items: center; }
    .topbar { padding: 10px 14px; gap: 10px; }
    .brand { gap: 10px; min-width: 0; flex: 1; }
    .brand .logo { width: 36px; height: 36px; font-size: 18px; }
    .brand h1 { font-size: 16px; }
    .brand p { display: none; }
    .brand-text { min-width: 0; }
    .topbar-actions { gap: 6px; }
    .topbar-actions input[type="search"] {
        width: 140px;
        padding: 7px 12px;
        font-size: 16px;
    }
    .topbar-actions button { padding: 7px 10px; }

    .layout {
        grid-template-columns: 1fr;
        min-height: calc(100vh - 58px);
    }
    .sidebar {
        position: fixed;
        top: 58px; left: 0; bottom: 0;
        width: min(86vw, 320px);
        max-height: none;
        z-index: 45;
        transform: translateX(-100%);
        transition: transform .25s ease;
        box-shadow: 4px 0 24px rgba(0,0,0,.3);
    }
    .sidebar.open { transform: translateX(0); }

    .content {
        padding: 18px 16px 60px;
        max-width: 100%;
    }
    .welcome h2 { font-size: 22px; }
    .grid {
        grid-template-columns: 1fr;
        gap: 10px;
        margin-top: 18px;
    }
    .card { padding: 14px; }

    .lesson h2 { font-size: 22px; }
    .lesson h3 { font-size: 16px; }
    .lesson .meta { gap: 8px; font-size: 12px; }
    .lesson .meta span { padding: 3px 8px; }
    .lesson pre { font-size: 12px; padding: 12px; border-radius: 8px; }
    .code-block .code-title { padding: 6px 10px; font-size: 11px; }
    .code-context.after { padding: 12px 14px; }

    .finish-section {
        margin-top: 32px;
        padding-top: 20px;
        flex-direction: column;
        align-items: stretch;
    }
    .mark-read-btn, .next-lesson-btn {
        width: 100%;
        margin-left: 0;
        padding: 12px 16px;
    }
    .finish-final { margin-left: 0; text-align: center; }

    .notebook-link {
        display: flex;
        margin-right: 0;
        margin-bottom: 8px;
    }
}

/* ===== Small phones ===== */
@media (max-width: 420px) {
    .topbar-actions input[type="search"] { width: 110px; }
    .brand h1 { font-size: 15px; }
    .content { padding: 14px 12px 50px; }
    .welcome h2 { font-size: 20px; }
    .lesson h2 { font-size: 20px; }
}
