/* ===== Mobile / touch overrides (Phase 2) =====
 *
 * Two breakpoints:
 *   - any coarse pointer (mostly phones + tablets) → bigger tap targets
 *   - narrow width (≤ 720 px) → toolbar wraps, nav links shrink
 *
 * The desktop look is unchanged. */

@media (pointer: coarse) {
    /* Bigger color picker dots — iOS HIG says 44 px is the minimum tap target. */
    .color-dot {
        width: 44px;
        height: 44px;
        border-width: 3px;
    }
    .color-picker { padding: 10px; gap: 10px; }
    /* No hover state on touch — the scale was annoying. */
    .color-dot:hover { transform: none; }
    /* Bigger toolbar buttons */
    .toolbar-btn {
        padding: 8px 12px;
        font-size: 0.95rem;
        min-height: 40px;
    }
}

@media (max-width: 720px) {
    /* Stack the toolbar so it doesn't horizontal-scroll on phone screens */
    .reader-toolbar {
        flex-wrap: wrap;
        gap: 6px;
        padding: 6px;
    }
    .reader-toolbar .toolbar-divider { display: none; }
    /* Hide the book title in the header to free up nav space */
    header .container > span { display: none; }
    /* Shrink the nav to essentials */
    header .nav-links li:nth-child(n+3) { display: none; }
    /* Library link + back-to-library = enough on phone */

    /* Reclaim screen width for the page itself — 20px gutters waste a lot
     * on a narrow phone. computeFitScale() reads this padding live, so the
     * fit math stays correct. */
    .reader-viewport { padding: 8px; }
}

.toolbar-btn.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

/* ===== Link-style button (used for logout in nav) ===== */
.link-button {
    background: none;
    border: none;
    color: inherit;
    font: inherit;
    cursor: pointer;
    padding: 0;
    text-decoration: none;
}
.link-button:hover { text-decoration: underline; }

/* ===== Light Theme Variables ===== */
:root {
    --bg: #f8f9fa;
    --bg-card: #ffffff;
    --bg-hover: #f0f1f3;
    --text: #1a1a2e;
    --text-muted: #6b7280;
    --border: #e2e5e9;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success: #16a34a;
    --error: #dc2626;
    --warning: #d97706;

    --hl-yellow: #eab308;
    --hl-yellow-bg: rgba(234, 179, 8, 0.25);
    --hl-green: #16a34a;
    --hl-green-bg: rgba(22, 163, 74, 0.25);
    --hl-red: #dc2626;
    --hl-red-bg: rgba(220, 38, 38, 0.2);
    --hl-blue: #2563eb;
    --hl-blue-bg: rgba(37, 99, 235, 0.2);
    --hl-purple: #7c3aed;
    --hl-purple-bg: rgba(124, 58, 237, 0.2);

    --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
    --radius: 8px;
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== Layout ===== */
.container { max-width: 1200px; margin: 0 auto; padding: 0 20px; }

/* ===== Header ===== */
header {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    padding: 12px 0;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow);
}
header .container { display: flex; align-items: center; justify-content: space-between; }
.logo { font-size: 1.3rem; font-weight: 700; color: var(--text); }
.logo span { color: var(--primary); }
.logo:hover { text-decoration: none; }
.nav-links { display: flex; list-style: none; gap: 8px; }
.nav-links a {
    padding: 6px 14px;
    border-radius: 6px;
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.15s;
}
.nav-links a:hover { background: var(--bg-hover); color: var(--text); text-decoration: none; }
.nav-links a.active { background: var(--primary); color: white; }
.nav-badge {
    background: var(--error);
    color: white;
    font-size: 0.7rem;
    padding: 1px 6px;
    border-radius: 10px;
    margin-left: 4px;
}

/* ===== Flash Messages ===== */
.flash-messages { margin: 16px 0; }
.flash {
    padding: 12px 16px;
    border-radius: var(--radius);
    margin-bottom: 8px;
    font-size: 0.9rem;
}
.flash-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.flash-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--bg-hover); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: var(--error); color: white; }
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 4px 10px; font-size: 0.8rem; }

/* ===== Forms ===== */
input[type="text"], input[type="search"], input[type="number"], select, textarea {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.9rem;
    background: var(--bg-card);
    color: var(--text);
    width: 100%;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

/* ===== Cards ===== */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
}

/* ===== Library ===== */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 24px 0 16px;
}
.page-header h1 { font-size: 1.5rem; }

.upload-area {
    background: var(--bg-card);
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 24px;
    text-align: center;
    margin-bottom: 24px;
    transition: border-color 0.2s;
}
.upload-area:hover { border-color: var(--primary); }
.upload-area input[type="file"] { margin: 8px 0; }

.book-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 20px;
    margin-bottom: 40px;
}
.book-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    transition: transform 0.15s, box-shadow 0.15s;
    cursor: pointer;
}
.book-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.book-cover {
    width: 100%;
    height: 280px;
    background: #e8eaed;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}
.book-cover canvas { max-width: 100%; max-height: 100%; }
.book-cover .no-cover { color: var(--text-muted); font-size: 0.85rem; }
.book-info { padding: 12px; }
.book-info h3 { font-size: 0.95rem; line-height: 1.3; margin-bottom: 4px; }
.book-info .author { color: var(--text-muted); font-size: 0.8rem; margin-bottom: 8px; }
.book-progress { display: flex; align-items: center; gap: 8px; font-size: 0.8rem; color: var(--text-muted); }
.progress-bar { flex: 1; height: 4px; background: var(--bg); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--primary); border-radius: 2px; transition: width 0.3s; }
.book-status {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    margin-top: 6px;
}
.status-reading { background: #dbeafe; color: #1e40af; }
.status-completed { background: #dcfce7; color: #166534; }
.status-paused { background: #fef3c7; color: #92400e; }

/* ===== Reader ===== */
.reader-layout {
    display: flex;
    position: fixed;
    top: 49px;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
}
.reader-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}
.reader-viewport {
    flex: 1;
    min-height: 0;
    overflow: auto;
    display: flex;
    /* flex-start (NOT center) + auto side-margins on the child: the page
     * centers when it fits, but when it's wider than the screen the margins
     * collapse to 0 and the LEFT edge stays scroll-reachable. `center` here
     * pushed the left edge into unreachable negative space — that was the
     * "page stuck on the right, can't scroll to the start" bug on mobile. */
    justify-content: flex-start;
    align-items: flex-start;
    padding: 20px;
    background: #e8eaed;
}
#pdf-container {
    position: relative;
    box-shadow: var(--shadow-lg);
    margin: 0 auto;   /* centers when there's room, collapses on overflow */
}
#pdf-canvas { display: block; }
#overlay-canvas {
    position: absolute;
    top: 0;
    left: 0;
    cursor: crosshair;
    z-index: 1;
}

.reader-toolbar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 10px 16px;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    z-index: 50;
}
.reader-toolbar .page-nav { display: flex; align-items: center; gap: 8px; }
.reader-toolbar input[type="number"] { width: 60px; text-align: center; }
.reader-toolbar .zoom-controls { display: flex; align-items: center; gap: 6px; }
.toolbar-btn {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-card);
    color: var(--text);
    cursor: pointer;
    font-size: 0.85rem;
}
.toolbar-btn:hover { background: var(--bg-hover); }
.toolbar-divider { width: 1px; height: 24px; background: var(--border); }

/* ===== Color Picker ===== */
.color-picker {
    position: fixed;
    display: none;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    padding: 8px;
    z-index: 200;
    gap: 6px;
}
.color-picker.visible { display: flex; }
.color-dot {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: transform 0.1s;
    position: relative;
}
.color-dot:hover { transform: scale(1.2); }
.color-dot[data-color="yellow"] { background: var(--hl-yellow); }
.color-dot[data-color="green"] { background: var(--hl-green); }
.color-dot[data-color="red"] { background: var(--hl-red); }
.color-dot[data-color="blue"] { background: var(--hl-blue); }
.color-dot[data-color="purple"] { background: var(--hl-purple); }
.color-dot[title]::after {
    content: attr(title);
    position: absolute;
    bottom: -22px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.65rem;
    white-space: nowrap;
    color: var(--text-muted);
    display: none;
}
.color-dot:hover::after { display: block; }

/* ===== AI Side Panel ===== */
/* Absolute overlay, NOT an in-flow flex child. As a flex child it kept its
 * 400px slot even while translated off-screen, permanently squeezing
 * .reader-main to (screen − 400px): a dead gap on desktop, an unusable
 * sliver on phones. Anchored to .reader-layout (position: fixed), it now
 * takes zero layout space when closed and overlays the right edge when open. */
.ai-panel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 400px;
    max-width: 100%;
    background: var(--bg-card);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    z-index: 100;
}
.ai-panel.open { transform: translateX(0); }

/* On phones the panel is full-width so the AI text is readable. */
@media (max-width: 720px) {
    .ai-panel { width: 100%; border-left: none; }
}
.ai-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
}
.ai-header .close-btn { cursor: pointer; font-size: 1.2rem; color: var(--text-muted); border: none; background: none; }
.ai-context {
    padding: 10px 16px;
    background: var(--hl-blue-bg);
    font-size: 0.85rem;
    line-height: 1.5;
    max-height: 100px;
    overflow-y: auto;
    border-bottom: 1px solid var(--border);
}
.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.ai-msg {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 0.9rem;
    line-height: 1.5;
    max-width: 90%;
}
.ai-msg-user {
    background: var(--primary);
    color: white;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}
.ai-msg-assistant {
    background: var(--bg);
    color: var(--text);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}
.ai-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 16px;
    border-top: 1px solid var(--border);
}
.ai-actions .btn { font-size: 0.75rem; padding: 4px 8px; }
.ai-input-row {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}
.ai-input-row input { flex: 1; }
.ai-loading { text-align: center; padding: 20px; color: var(--text-muted); }

/* ===== Highlights Page ===== */
.filter-bar {
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
    flex-wrap: wrap;
    align-items: center;
}
.filter-bar select, .filter-bar input { width: auto; min-width: 150px; }
.highlight-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 40px; }
.highlight-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    border-left: 4px solid var(--border);
    box-shadow: var(--shadow);
}
.highlight-card[data-color="yellow"] { border-left-color: var(--hl-yellow); }
.highlight-card[data-color="green"] { border-left-color: var(--hl-green); }
.highlight-card[data-color="red"] { border-left-color: var(--hl-red); }
.highlight-card[data-color="blue"] { border-left-color: var(--hl-blue); }
.highlight-card[data-color="purple"] { border-left-color: var(--hl-purple); }
.highlight-text { font-size: 0.95rem; line-height: 1.6; margin-bottom: 8px; }
.highlight-meta { display: flex; gap: 12px; font-size: 0.8rem; color: var(--text-muted); flex-wrap: wrap; }
.topic-badge {
    display: inline-block;
    padding: 2px 8px;
    background: #eff6ff;
    color: var(--primary);
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 500;
}
.further-reading {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* ===== Stats ===== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}
.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    text-align: center;
    box-shadow: var(--shadow);
}
.stat-value { font-size: 2rem; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 0.8rem; color: var(--text-muted); margin-top: 4px; }
.chart-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: var(--shadow);
}
.chart-container h3 { margin-bottom: 16px; font-size: 1rem; }

/* ===== Review Page ===== */
.flashcard {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 40px;
    text-align: center;
    box-shadow: var(--shadow-lg);
    max-width: 600px;
    margin: 40px auto;
    min-height: 200px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}
.flashcard-question { font-size: 1.2rem; line-height: 1.6; }
.flashcard-answer { font-size: 1rem; line-height: 1.6; color: var(--text-muted); margin-top: 16px; }
.flashcard-meta { font-size: 0.8rem; color: var(--text-muted); margin-top: 16px; }
.review-buttons { display: flex; justify-content: center; gap: 12px; margin-top: 20px; }
.review-btn {
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    font-size: 0.9rem;
}
.review-btn.again { background: #fef2f2; color: var(--error); border: 1px solid #fecaca; }
.review-btn.hard { background: #fef3c7; color: #92400e; border: 1px solid #fde68a; }
.review-btn.good { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.review-btn.easy { background: #dbeafe; color: #1e40af; border: 1px solid #bfdbfe; }

/* ===== Empty State ===== */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}
.empty-state h2 { font-size: 1.2rem; margin-bottom: 8px; color: var(--text); }
