/* ============================================
   RESET & BASE
   ============================================ */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Colors */
    --paper-bg: #fefdfb;
    --paper-shadow: rgba(27, 27, 27, 0.08);
    --ruled-line: #e3ddd4;
    --margin-line: #c9a57a;
    --text-primary: #1a1410;
    --text-secondary: #5a4f3f;
    --margin-text: #8b6f47;
    --critical-bg: #fef9e7;
    --important-mark: #d4a574;
    --hole-bg: #e8e5e1;
    --critical-highlight: rgba(
        254,
        243,
        199,
        0.15
    ); /* Very subtle yellow tint */

    /* Typography */
    --font-ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif;
    --font-heading:
        -apple-system, BlinkMacSystemFont, "SF Pro Display", sans-serif;
    --font-body: "Charter", "Palatino", "Georgia", serif;
    --font-margin: "SF Mono", "Menlo", monospace;

    /* Spacing */
    --line-height: 2em;
    --margin-width: 80px;
    --hole-width: 35px;
    --content-padding: 20px;
}

body {
    background: linear-gradient(180deg, #f5f3f0 0%, #ebe8e3 100%);
    font-family: var(--font-body);
    color: var(--text-primary);
    line-height: var(--line-height);
    padding: 0;
    min-height: 100vh;
}

/* ============================================
   UNIFIED TOP BAR
   ============================================ */
.top-bar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid #e0dcd7;
    padding: 12px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
}

.header-section {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-info {
    flex: 1;
    flex-direction: column;
    gap: 4px;
    min-width: 0; /* Allow text truncation */
}

.action-btn {
    font-family: var(--font-ui);
    font-size: 13px;
    padding: 7px 14px;
    background: white;
    border: 1px solid #d0cbc4;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.action-btn svg {
    flex-shrink: 0;
}

.action-btn:hover {
    background: #f5f3f0;
    border-color: #a39b8f;
}

.topic-title {
    font-family: var(--font-heading);
    font-size: 15px;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.stats-display {
    font-size: 11px;
    color: var(--text-secondary);
    font-family: var(--font-margin);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ============================================
   PAPER SHEET
   ============================================ */
.paper-sheet {
    width: 794px;
    max-width: calc(100% - 40px);
    min-height: 1123px;
    margin: 40px auto 80px;
    background: var(--paper-bg);
    background-image: repeating-linear-gradient(
        0deg,
        transparent,
        transparent calc(var(--line-height) - 1px),
        var(--ruled-line) calc(var(--line-height) - 1px),
        var(--ruled-line) var(--line-height)
    );
    background-position: 0 0;
    box-shadow:
        0 1px 2px rgba(0, 0, 0, 0.04),
        0 4px 12px rgba(0, 0, 0, 0.06),
        0 16px 48px rgba(0, 0, 0, 0.08);
    position: relative;
    padding: 0 50px 60px calc(var(--hole-width) + var(--margin-width) + 20px);
    padding-top: calc(var(--line-height) * 1.5);

    /* Performance optimizations */
    will-change: scroll-position;
    contain: layout style paint;
}

/* ============================================
   HOLES (3-hole punch)
   ============================================ */
.holes {
    position: absolute;
    left: calc(var(--hole-width) / 2);
    top: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;
    padding: 100px 0;
}

.hole {
    width: 32px;
    height: 32px;
    background: var(--hole-bg);
    border-radius: 50%;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15);
}

/* ============================================
   MARGIN LINE
   ============================================ */
.margin-line {
    position: absolute;
    left: calc(var(--hole-width) + var(--margin-width));
    top: 0;
    width: 1.5px;
    height: 100%;
    background: var(--margin-line);
    opacity: 0.6;
}

/* ============================================
   CONTENT AREA
   ============================================ */
.note-content {
    font-family: var(--font-body);
    font-size: 16px;
    line-height: var(--line-height);
    color: var(--text-primary);
    outline: none;
    min-height: 800px;

    /* Performance optimizations */
    contain: layout style;
}

/* Preserve line alignment - critical for ruled lines */
.note-content > * {
    line-height: var(--line-height);
    margin: 0;
    padding: 0;
}

/* Ensure paragraphs align with lines */
.note-content p,
.concept-content p,
.section-content p {
    line-height: var(--line-height);
    margin: 0;
}

/* ============================================
   CONTENT BLOCKS
   ============================================ */

/* Hierarchical indentation based on level */
.level-1 {
    padding-left: 0;
}
.level-2 {
    padding-left: 20px;
}
.level-3 {
    padding-left: 40px;
}
.level-4 {
    padding-left: 60px;
}

/* Performance: Isolate layout calculations for content blocks */
.section-block,
.concept-block,
.list-block,
.formula-block,
.example-block,
.generic-block {
    contain: layout style;
}

/* Headings */
.block-heading {
    font-family: var(--font-heading);
    font-weight: 600;
    color: var(--text-primary);
    margin-top: calc(var(--line-height) * 2);
    position: relative;
}

.block-heading.level-1 {
    font-size: 24px;
    line-height: calc(var(--line-height) * 1.5);
    border-bottom: 2px solid var(--margin-line);
    padding-bottom: 4px;
}

.block-heading.level-2 {
    font-size: 20px;
    margin-top: var(--line-height);
}

.block-heading.level-3 {
    font-size: 18px;
    font-weight: 500;
    margin-top: calc(var(--line-height) / 2);
}

/* Definition blocks */
.block-definition {
    background: var(--critical-bg);
    padding: 8px 12px;
    border-left: 3px solid var(--important-mark);
    margin: calc(var(--line-height) / 2) 0;
    position: relative;
}

.block-definition strong {
    font-weight: 600;
    color: var(--text-primary);
}

/* Section blocks */
.section-block {
    margin: calc(var(--line-height) * 1) 0;
    position: relative;
}

.section-title {
    font-family: var(--font-heading);
    font-weight: 600;
    color: var(--text-primary);
    line-height: var(--line-height);
    margin: 0 0 var(--line-height) 0;
}

.level-1 .section-title {
    font-size: 24px;
    line-height: calc(var(--line-height) * 1.5);
    border-bottom: 2px solid var(--margin-line);
    padding-bottom: 4px;
    margin-bottom: calc(var(--line-height) * 0.5);
}

.level-2 .section-title {
    font-size: 20px;
    line-height: var(--line-height);
    letter-spacing: -0.01em;
}

.level-3 .section-title {
    font-size: 18px;
    font-weight: 600;
    line-height: var(--line-height);
    letter-spacing: -0.01em;
}

.level-4 .section-title {
    font-size: 17px;
    font-weight: 600;
    line-height: var(--line-height);
    letter-spacing: -0.005em;
}

.section-content {
    margin: 0;
    color: var(--text-primary);
}

/* Exam badge */
.exam-badge {
    display: inline-block;
    margin-right: 8px;
    font-size: 14px;
}

/* Concept blocks */
.concept-block {
    margin: var(--line-height) 0;
    position: relative;
}

.concept-block p {
    margin: 0;
    line-height: var(--line-height);
}

/* Exam-critical styling - removed background, only badge matters */
.exam-critical {
    position: relative; /* Clean - no background needed, the ⚡ badge is enough */
}

.block-concept {
    margin: calc(var(--line-height) / 2) 0;
    position: relative;
}

/* Procedures */
.block-procedure {
    margin: var(--line-height) 0;
    position: relative;
}

.block-procedure ol {
    margin-left: 24px;
    padding-left: 0;
}

.block-procedure li {
    margin: calc(var(--line-height) / 2) 0;
}

/* Visual blocks - simplified */
.visual-block {
    margin: var(--line-height) 0;
    position: relative;
}

.visual-description {
    margin-bottom: var(--line-height);
    line-height: var(--line-height);
}

.visual-ascii {
    font-family: var(--font-margin);
    font-size: 13px;
    line-height: 1.4;
    overflow-x: auto;
}

/* Formula blocks - simplified */
.formula-block {
    margin: var(--line-height) 0;
    position: relative;
}

.formula-content {
    font-family: var(--font-margin);
    font-size: 15px;
    line-height: var(--line-height);
    white-space: pre-line;
}

/* Example blocks - simplified */
.example-block {
    margin: var(--line-height) 0;
    position: relative;
    padding-left: 16px;
    border-left: 2px solid var(--ruled-line);
}

.example-content {
    font-style: italic;
    color: var(--text-secondary);
    line-height: var(--line-height);
    font-size: 15px;
    opacity: 0.85;
}

/* Examples (legacy) */
.block-example {
    font-style: italic;
    color: var(--text-secondary);
    padding-left: 16px;
    border-left: 2px solid var(--ruled-line);
    margin: calc(var(--line-height) / 2) 0;
    position: relative;
    font-size: 15px;
    opacity: 0.85;
}

/* Warnings */
.block-warning {
    background: #fff9f0;
    border-left: 3px solid #e8a04a;
    padding: 8px 12px;
    margin: calc(var(--line-height) / 2) 0;
    position: relative;
}

.block-warning::before {
    content: "⚠️ ";
}

/* List blocks */
.list-block {
    margin: var(--line-height) 0;
    position: relative;
}

.list-intro {
    margin: 0 0 var(--line-height) 0;
    font-weight: 500;
    line-height: var(--line-height);
}

.list-block ul {
    margin: 0;
    padding-left: 24px;
}

.list-block li {
    margin: 0;
    line-height: var(--line-height);
}

/* Table blocks */
.table-block {
    margin: var(--line-height) 0;
    overflow-x: auto;
    position: relative;
}

.table-block table {
    width: 100%;
    border-collapse: collapse;
    font-size: 15px;
}

.table-block th,
.table-block td {
    padding: 10px 12px;
    text-align: left;
    border: 1px solid var(--ruled-line);
}

.table-block th {
    background: #f9f7f4;
    font-weight: 600;
    font-family: var(--font-heading);
}

/* Comparison tables (for client/server comparisons, etc.) */
.comparison-table {
    width: 100%;
    border-collapse: collapse;
    margin: var(--line-height) 0;
    font-size: 15px;
    line-height: 1.6;
}

.comparison-table th {
    background: #f9f7f4;
    font-weight: 600;
    font-family: var(--font-heading);
    padding: 12px 16px;
    text-align: left;
    border: 1px solid var(--ruled-line);
    width: 200px;
    vertical-align: top;
}

.comparison-table td {
    padding: 12px 16px;
    text-align: left;
    border: 1px solid var(--ruled-line);
    vertical-align: top;
}

.comparison-table tr:hover {
    background: rgba(0, 0, 0, 0.02);
}

/* Exam mode table - simplified single column */
.comparison-table.exam-table th {
    display: none;
}

.comparison-table.exam-table td {
    width: 100%;
    background: white;
    font-weight: 500;
}

/* Comparisons & Tables (legacy) */
.block-comparison {
    position: relative;
    margin: calc(var(--line-height) / 2) 0;
}

.block-comparison table {
    width: 100%;
    border-collapse: collapse;
    margin: calc(var(--line-height) / 2) 0;
    font-size: 15px;
    background: var(--paper-bg);
}

.block-comparison th,
.block-comparison td {
    border: 1px solid var(--ruled-line);
    padding: 8px 12px;
    text-align: left;
    line-height: 1.5;
}

.block-comparison th {
    background: #f9f7f4;
    font-weight: 600;
    font-family: var(--font-heading);
}

.block-comparison td {
    font-family: var(--font-body);
}

/* ============================================
   MARGIN ANNOTATIONS & HOVER TOPICS
   ============================================ */
.margin-note {
    position: absolute;
    left: calc(-1 * (var(--margin-width) + 20px));
    width: var(--margin-width);
    font-family: var(--font-margin);
    font-size: 11px;
    line-height: 1.3;
    color: var(--margin-text);
    text-align: right;
    padding-right: 12px;
}

.margin-note.critical {
    color: #b8860b;
    font-weight: 600;
}

.margin-note.important {
    color: var(--important-mark);
}

/* ============================================
   INLINE FORMATTING
   ============================================ */
strong,
b {
    font-weight: 600;
    color: var(--text-primary);
}

em,
i {
    font-style: italic;
    color: var(--text-secondary);
}

code {
    font-family: var(--font-margin);
    font-size: 0.9em;
    background: #f5f3f0;
    padding: 2px 6px;
    border-radius: 3px;
    border: 1px solid #e0dcd7;
}

/* ============================================
   SIMPLIFIED HIGHLIGHT SYSTEM (Student-Focused)
   ============================================ */
/* All highlights use the same yellow - simple and clear */
mark-critical,
.mark-critical,
mark-key,
.mark-key {
    background: linear-gradient(
        180deg,
        transparent 65%,
        rgba(255, 244, 214, 0.8) 65%
    );
    color: inherit;
    font-weight: 500;
    padding: 0;
    border-radius: 0;
    display: inline;
}

/* ============================================
   TOGGLE OPTIONS (Checkboxes)
   ============================================ */
.toggle-option {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    user-select: none;
}

.toggle-option input[type="checkbox"] {
    appearance: none;
    width: 16px;
    height: 16px;
    border: 1.5px solid #d0cbc4;
    border-radius: 3px;
    background: white;
    cursor: pointer;
    position: relative;
    transition: all 0.2s ease;
}

.toggle-option input[type="checkbox"]:hover {
    border-color: #a39b8f;
}

.toggle-option input[type="checkbox"]:checked {
    background: var(--margin-line);
    border-color: var(--margin-line);
}

.toggle-option input[type="checkbox"]:checked::after {
    content: "✓";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-size: 11px;
    font-weight: bold;
}

.toggle-label {
    font-family: var(--font-ui);
    font-size: 12px;
    color: var(--text-secondary);
}

/* ============================================
   MODE TOGGLE (in header)
   ============================================ */
.mode-toggle {
    display: flex;
    gap: 6px;
    background: #f5f3f0;
    padding: 4px;
    border-radius: 8px;
}

.mode-btn {
    padding: 6px 12px;
    border: none;
    background: transparent;
    border-radius: 5px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    font-family: var(--font-ui);
    transition: all 0.2s;
    color: var(--text-secondary);
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.mode-btn svg {
    flex-shrink: 0;
}

.mode-btn.active {
    background: white;
    color: var(--text-primary);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.mode-btn:hover:not(.active) {
    color: var(--text-primary);
}

/* ============================================
   GENERIC BLOCKS
   ============================================ */
.generic-block {
    margin: calc(var(--line-height) / 2) 0;
    position: relative;
}

/* ============================================
   PATH BADGE & TOPIC HEADING
   ============================================ */
.path-badge {
    font-family: var(--font-ui);
    font-size: 10px;
    color: #9ca3af;
    margin-bottom: 6px;
    line-height: 1.4;
    letter-spacing: 0.02em;
}

.topic-heading {
    font-family: var(--font-heading);
    font-size: 18px;
    font-weight: 600;
    color: var(--margin-text);
    margin: 0 0 calc(var(--line-height) * 0.5) 0;
    line-height: var(--line-height);
}

/* ============================================
   KEY SENTENCE HIGHLIGHTING (Realistic highlighter effect)
   ============================================ */
.key-sentence {
    --mark-color: #fff4d6;
    --mark-skew: 0.25em;
    --mark-height: 1em;
    --mark-overlap: 0.3em;
    margin-inline: calc(var(--mark-overlap) * -1);
    padding-inline: var(--mark-overlap);
    background-color: transparent;
    background-image:
        linear-gradient(
            to bottom right,
            transparent 50%,
            var(--mark-color) 50%
        ),
        linear-gradient(var(--mark-color), var(--mark-color)),
        linear-gradient(to top left, transparent 50%, var(--mark-color) 50%);
    background-size:
        var(--mark-skew) var(--mark-height),
        calc(100% - var(--mark-skew) * 2 + 1px) var(--mark-height),
        var(--mark-skew) var(--mark-height);
    background-position:
        left center,
        center,
        right center;
    background-repeat: no-repeat;
    color: inherit;
    font-weight: 500;
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
}

/* ============================================
   METADATA PANEL (Hidden - using hover tooltips instead)
   ============================================ */
.metadata-panel {
    display: none;
}

/* ============================================
   CONTENT BULLETS
   ============================================ */
.content-bullets {
    margin: 0;
    padding-left: 24px;
}

.content-bullets li {
    margin: 0;
    line-height: var(--line-height);
}

.content-bullets ul,
.content-bullets ol {
    margin: 0;
    margin-left: 20px;
}

/* ============================================
   EXAM BADGE - Primary indicator for exam-critical content
   ============================================ */
.exam-badge {
    font-size: 16px;
    margin-right: 8px;
    opacity: 1;
}

/* ============================================
   TOGGLE-SPECIFIC STYLES
   ============================================ */
.tooltips-disabled [data-simple],
.tooltips-disabled [data-analogy],
.tooltips-disabled [data-why] {
    pointer-events: none;
}

.tooltips-disabled .metadata-tooltip {
    display: none !important;
}

.paths-disabled .path-badge {
    display: none;
}

/* ============================================
   MODE-SPECIFIC STYLES
   ============================================ */

/* ============================================
   NOTIFICATION SYSTEM
   ============================================ */
.notification {
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 12px 20px;
    background: #10b981;
    color: white;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    animation: slideIn 0.3s ease;
    font-family: var(--font-ui);
    font-size: 14px;
}

.notification.error {
    background: #ef4444;
}

.notification.fade-out {
    animation: fadeOut 0.3s ease;
}

@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes fadeOut {
    to {
        opacity: 0;
        transform: translateY(-10px);
    }
}

/* ============================================
   RESPONSIVE
   ============================================ */
@media (max-width: 850px) {
    .paper-sheet {
        margin: 20px;
        padding-left: 65px;
    }

    .holes {
        left: 8px;
    }

    .margin-line {
        left: 55px;
    }

    :root {
        --margin-width: 50px;
        --hole-width: 20px;
    }
}

@media print {
    /* Hide UI elements only */
    .top-bar,
    .legend-btn,
    .legend-modal,
    .toc-outline,
    .margin-note {
        display: none !important;
    }

    /* Remove body background */
    body {
        background: white;
        margin: 0;
        padding: 0;
    }

    /* Optimize paper sheet for printing - match screen exactly */
    .paper-sheet {
        box-shadow: none;
        margin: 0;
        padding: 0 50px 60px
            calc(var(--hole-width) + var(--margin-width) + 20px);
        padding-top: calc(var(--line-height) * 1.5);
        width: 100%;
        max-width: 100%;
        background: white;
        /* Keep ruled lines matching screen */
        background-image: repeating-linear-gradient(
            0deg,
            transparent,
            transparent calc(var(--line-height) - 1px),
            #e3ddd4 calc(var(--line-height) - 1px),
            #e3ddd4 var(--line-height)
        );
        background-position: 0 0;
        page-break-after: avoid;
    }

    /* Keep holes visible */
    .holes {
        display: flex;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .hole {
        background: #e8e5e1;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    /* Hide margin line in print */
    .margin-line {
        display: none;
    }

    /* Ensure content is readable */
    .note-content {
        color: #1a1410;
    }

    /* Preserve simplified highlighting - match screen exactly */
    mark-critical,
    .mark-critical,
    mark-key,
    .mark-key {
        background: linear-gradient(
            180deg,
            transparent 65%,
            rgba(255, 244, 214, 0.8) 65%
        );
        color: inherit;
        font-weight: 500;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .key-sentence {
        --mark-color: #fff4d6;
        --mark-skew: 0.25em;
        --mark-height: 1em;
        --mark-overlap: 0.3em;
        margin-inline: calc(var(--mark-overlap) * -1);
        padding-inline: var(--mark-overlap);
        background-color: transparent;
        background-image:
            linear-gradient(
                to bottom right,
                transparent 50%,
                var(--mark-color) 50%
            ),
            linear-gradient(var(--mark-color), var(--mark-color)),
            linear-gradient(to top left, transparent 50%, var(--mark-color) 50%);
        background-size:
            var(--mark-skew) var(--mark-height),
            calc(100% - var(--mark-skew) * 2 + 1px) var(--mark-height),
            var(--mark-skew) var(--mark-height);
        background-position:
            left center,
            center,
            right center;
        background-repeat: no-repeat;
        color: inherit;
        font-weight: 500;
        -webkit-box-decoration-break: clone;
        box-decoration-break: clone;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    /* No background for exam-critical - match screen */
    .exam-critical {
        position: relative; /* Clean - no background */
    }

    /* Keep sections together */
    .section-block,
    .concept-block,
    .list-block,
    .formula-block,
    .example-block {
        page-break-inside: avoid;
    }

    /* Preserve margin notes */
    .margin-note {
        color: #8b6f47;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    /* Preserve exam badge */
    .exam-badge {
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    /* Preserve path badge and topic heading */
    .path-badge {
        color: #9ca3af;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .topic-heading {
        color: #1a1410;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    /* Hide tooltips and interactive elements */
    .metadata-tooltip,
    .metadata-indicator {
        display: none !important;
    }

    /* Preserve user notes if added */
    .user-notes {
        background: #fffbeb;
        border-left: 3px solid #f59e0b;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
        page-break-inside: avoid;
    }

    .added-note {
        border-bottom: 1px solid #fef3c7;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .note-label {
        color: #92400e;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .note-text {
        color: #78350f;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    /* Ensure all text uses proper line height */
    p,
    li,
    div {
        line-height: var(--line-height);
    }

    /* Preserve comparison tables */
    .comparison-table {
        page-break-inside: avoid;
        border: 1px solid var(--ruled-line);
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .comparison-table th {
        background: #f9f7f4;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .comparison-table th,
    .comparison-table td {
        border: 1px solid var(--ruled-line);
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }
}

/* ============================================
   METADATA INDICATOR & HOVER TOOLTIP
   ============================================ */

/* Metadata indicator icon (hidden for cleaner paper) */
.metadata-indicator {
    display: none;
}

/* Hover tooltip */
.metadata-tooltip {
    position: absolute;
    z-index: 1000;
    background: white;
    border-radius: 12px;
    box-shadow:
        0 4px 6px -1px rgba(0, 0, 0, 0.1),
        0 10px 25px -3px rgba(0, 0, 0, 0.1),
        0 0 0 1px rgba(0, 0, 0, 0.05);
    width: 360px;
    max-width: calc(100vw - 40px);
    opacity: 0;
    transform: translateY(-4px);
    transition:
        opacity 0.2s ease,
        transform 0.2s ease;
    pointer-events: none;

    /* Performance optimizations */
    will-change: transform, opacity;
    contain: layout style paint;
}

.metadata-tooltip.visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: all;
}

/* Tooltip header with tabs */
.tooltip-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px;
    background: #fafafa;
    border-radius: 12px 12px 0 0;
}

.tooltip-tabs {
    display: flex;
    gap: 4px;
    flex: 1;
}

.tooltip-tab {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: #6b7280;
    font-family: var(--font-ui);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: 6px 6px 0 0;
}

.tooltip-tab:hover {
    background: #f9fafb;
    color: #374151;
}

.tooltip-tab.active {
    background: #f9fafb;
    color: var(--margin-line);
    border-bottom-color: var(--margin-line);
}

.tab-icon {
    font-size: 14px;
    line-height: 1;
}

.tab-label {
    font-weight: 500;
}

.tooltip-close {
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    color: #9ca3af;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
}

.tooltip-close:hover {
    background: #f3f4f6;
    color: #1f2937;
}

/* Tooltip body */
.tooltip-body {
    padding: 16px;
    max-height: 50vh;
    overflow-y: auto;
    background: white;
    border-radius: 0 0 12px 12px;
    /* No border-top to eliminate gap between header and body */
}

/* Tooltip content (tab panels) */
.tooltip-content {
    display: none;
    animation: fadeIn 0.2s ease;
}

.tooltip-content.active {
    display: block;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.tooltip-text {
    font-family: var(--font-body);
    font-size: 14px;
    line-height: 1.6;
    color: #1f2937;
    margin-bottom: 12px;
    min-height: 40px;
}

/* Add to note button */
.add-to-note-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--margin-line);
    color: white;
    border: none;
    border-radius: 6px;
    font-family: var(--font-ui);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.add-to-note-btn:hover:not(:disabled) {
    background: #b8941f;
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(201, 165, 122, 0.3);
}

.add-to-note-btn:active:not(:disabled) {
    transform: translateY(0);
}

.add-to-note-btn.added {
    background: #8b7355;
    cursor: default;
}

.add-to-note-btn:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}

.add-to-note-btn svg {
    flex-shrink: 0;
}

/* User notes area */
.user-notes {
    margin-top: 16px;
    padding: 12px;
    background: #fffbeb;
    border-left: 3px solid #f59e0b;
    border-radius: 4px;
}

.added-note {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin: 8px 0;
    padding: 8px 0;
    border-bottom: 1px solid #fef3c7;
}

.added-note:last-child {
    border-bottom: none;
}

.note-label {
    font-family: var(--font-ui);
    font-size: 12px;
    font-weight: 600;
    color: #92400e;
}

.note-text {
    font-family: var(--font-body);
    font-size: 14px;
    line-height: 1.5;
    color: #78350f;
}

/* Animation for newly added notes */
@keyframes noteAdded {
    0% {
        opacity: 0;
        transform: translateX(-10px);
    }
    100% {
        opacity: 1;
        transform: translateX(0);
    }
}

.note-added-animation {
    animation: noteAdded 0.3s ease;
}

/* Tooltip placement indicators */
.metadata-tooltip[data-placement="right"]::before {
    content: "";
    position: absolute;
    left: -6px;
    top: 20px;
    width: 12px;
    height: 12px;
    background: white;
    border-left: 1px solid rgba(0, 0, 0, 0.05);
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    transform: rotate(45deg);
}

.metadata-tooltip[data-placement="left"]::before {
    content: "";
    position: absolute;
    right: -6px;
    top: 20px;
    width: 12px;
    height: 12px;
    background: white;
    border-right: 1px solid rgba(0, 0, 0, 0.05);
    border-top: 1px solid rgba(0, 0, 0, 0.05);
    transform: rotate(45deg);
}

.metadata-tooltip[data-placement="bottom"]::before {
    content: "";
    position: absolute;
    top: -6px;
    left: 20px;
    width: 12px;
    height: 12px;
    background: white;
    border-left: 1px solid rgba(0, 0, 0, 0.05);
    border-top: 1px solid rgba(0, 0, 0, 0.05);
    transform: rotate(45deg);
}

/* Invisible bridge to fill gap between block and tooltip */
.tooltip-bridge {
    position: absolute;
    z-index: 999; /* Just below tooltip (1000) but above content */
    background: transparent;
    pointer-events: all;
    /* Debug: uncomment to visualize the bridge
    background: rgba(255, 0, 0, 0.1);
    border: 1px dashed red;
    */
}

/* Responsive adjustments - Tablet (768px and below) */
@media (max-width: 768px) {
    .top-bar {
        padding: 12px 16px;
        gap: 12px;
        flex-wrap: wrap;
    }

    /* First row: Actions and Info */
    .header-section:first-child {
        flex: 0 0 auto;
    }

    .header-info {
        flex: 1 1 auto;
        min-width: 0;
    }

    /* Second row: Options and Mode */
    .header-section:nth-child(3),
    .mode-toggle {
        flex: 0 0 auto;
    }

    .action-btn {
        padding: 8px 14px;
        font-size: 13px;
    }

    .topic-title {
        font-size: 14px;
    }

    .stats-display {
        font-size: 11px;
    }

    .mode-btn {
        padding: 6px 14px;
        font-size: 13px;
    }

    .toggle-label {
        font-size: 12px;
    }
}

/* Responsive adjustments - Mobile (480px and below) */
@media (max-width: 480px) {
    .top-bar {
        padding: 10px 12px;
        gap: 10px;
    }

    /* Full width stacking */
    .header-section:first-child,
    .header-info,
    .header-section:nth-child(3),
    .mode-toggle {
        flex: 1 1 100%;
        width: 100%;
    }

    .header-section:first-child {
        display: flex;
        gap: 8px;
    }

    .header-section:nth-child(3) {
        display: flex;
        justify-content: space-around;
    }

    .action-btn {
        flex: 1;
        padding: 8px 12px;
        font-size: 12px;
    }

    .topic-title {
        font-size: 13px;
    }

    .stats-display {
        font-size: 10px;
    }

    .mode-btn {
        flex: 1;
        padding: 8px 16px;
        font-size: 12px;
    }

    .toggle-label {
        font-size: 11px;
    }

    .metadata-tooltip {
        width: calc(100vw - 40px);
    }

    .tooltip-body {
        max-height: 40vh;
    }

    .tooltip-tabs {
        flex-wrap: wrap;
    }

    .tooltip-tab {
        padding: 6px 10px;
        font-size: 12px;
    }

    .tab-icon {
        font-size: 13px;
    }
}

/* Metadata indicators hidden in both modes (using hover tooltips instead) */
.metadata-indicator {
    display: none;
}

/* ============================================
   LIBRARY VIEW
   ============================================ */
.paper-sheet.library-mode {
    background: var(--paper-bg);
    background-image: none;
    padding: 60px 50px;
}

.library-view {
    max-width: 900px;
    margin: 0 auto;
}

.library-header {
    text-align: center;
    margin-bottom: 48px;
}

.library-header h2 {
    font-family: var(--font-heading);
    font-size: 32px;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.library-header p {
    font-family: var(--font-ui);
    font-size: 16px;
    color: var(--text-secondary);
}

.library-header p.error-message {
    color: #ef4444;
    font-weight: 500;
}

/* Search input */
.library-search {
    margin-top: 24px;
    max-width: 500px;
    margin-left: auto;
    margin-right: auto;
}

.search-input {
    width: 100%;
    padding: 12px 16px;
    font-family: var(--font-ui);
    font-size: 15px;
    border: 2px solid #e0dcd7;
    border-radius: 8px;
    background: white;
    transition: all 0.2s ease;
}

.search-input:focus {
    outline: none;
    border-color: var(--margin-line);
    box-shadow: 0 0 0 3px rgba(201, 165, 122, 0.1);
}

.search-input::placeholder {
    color: #9ca3af;
}

.library-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 20px;
}

.library-card {
    background: white;
    border: 2px solid #e0dcd7;
    border-radius: 12px;
    padding: 32px 24px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s ease;

    /* Make it a proper button */
    display: block;
    width: 100%;
    font: inherit;
    position: relative;
}

.library-card:hover {
    border-color: var(--margin-line);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

/* Accessibility: Focus states */
.library-card:focus {
    outline: none;
    border-color: var(--margin-line);
    box-shadow: 0 0 0 3px rgba(201, 165, 122, 0.2);
}

.library-card:focus-visible {
    outline: 2px solid var(--margin-line);
    outline-offset: 2px;
}

/* Loading state */
.library-card.loading {
    opacity: 0.6;
    pointer-events: none;
    cursor: wait;
}

.library-card.loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 24px;
    height: 24px;
    margin: -12px 0 0 -12px;
    border: 3px solid var(--margin-line);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.library-card.import-card {
    border-style: dashed;
    background: #fafaf9;
}

.library-card.import-card:hover {
    background: #f5f3f0;
}

.card-icon {
    font-size: 48px;
    margin-bottom: 12px;
}

.card-title {
    font-family: var(--font-heading);
    font-size: 16px;
    font-weight: 500;
    color: var(--text-primary);
    word-break: break-word;
}

/* Hide holes and margin line in library mode */
.paper-sheet.library-mode .holes,
.paper-sheet.library-mode .margin-line {
    display: none;
}

/* Loading state */
.library-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    gap: 20px;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid #e0dcd7;
    border-top-color: var(--margin-line);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.library-loading p {
    font-family: var(--font-ui);
    font-size: 15px;
    color: var(--text-secondary);
}

/* Error state */
.library-error {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    gap: 20px;
    text-align: center;
}

.error-icon {
    font-size: 64px;
    opacity: 0.5;
}

.error-details {
    font-family: var(--font-ui);
    font-size: 15px;
    color: var(--text-secondary);
    max-width: 500px;
    line-height: 1.6;
}

.error-hint {
    font-family: var(--font-ui);
    font-size: 13px;
    color: #9ca3af;
    max-width: 500px;
    line-height: 1.5;
    margin-top: 8px;
}

.retry-btn {
    margin-top: 8px;
}

/* No results message */
.no-results {
    grid-column: 1 / -1;
    text-align: center;
    padding: 60px 20px;
    font-family: var(--font-ui);
    font-size: 15px;
    color: var(--text-secondary);
}

@media (max-width: 850px) {
    .library-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        gap: 16px;
    }

    .library-card {
        padding: 24px 16px;
    }

    .card-icon {
        font-size: 36px;
    }

    .card-title {
        font-size: 14px;
    }
}

/* ============================================
   LEGEND/HELP BUTTON & MODAL
   ============================================ */
.legend-btn {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: white;
    border: 2px solid var(--margin-line);
    color: var(--margin-line);
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    z-index: 999;
}

.legend-btn:hover {
    background: var(--margin-line);
    color: white;
    transform: scale(1.05);
}

.legend-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(4px);
}

.legend-modal.active {
    display: flex;
}

.legend-content {
    background: white;
    border-radius: 12px;
    max-width: 500px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: legendSlideIn 0.3s ease;
}

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

.legend-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid #e5e7eb;
}

.legend-header h3 {
    font-family: var(--font-heading);
    font-size: 20px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.legend-close {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: #6b7280;
    font-size: 28px;
    line-height: 1;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.legend-close:hover {
    background: #f3f4f6;
    color: #1f2937;
}

.legend-body {
    padding: 24px;
}

.legend-section {
    margin-bottom: 24px;
}

.legend-section:last-child {
    margin-bottom: 0;
}

.legend-section h4 {
    font-family: var(--font-heading);
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0 0 12px 0;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 0;
}

.legend-sample {
    font-family: var(--font-body);
    font-size: 14px;
    padding: 4px 8px;
    border-radius: 4px;
    white-space: nowrap;
    min-width: 140px;
}

.legend-desc {
    font-family: var(--font-ui);
    font-size: 13px;
    color: var(--text-secondary);
    flex: 1;
}

/* Sample styles matching the actual highlights */
.legend-sample.key-sentence {
    background: linear-gradient(
        180deg,
        transparent 65%,
        rgba(212, 165, 116, 0.25) 65%
    );
    font-weight: 500;
}

.legend-sample.mark-critical {
    background: linear-gradient(180deg, transparent 60%, #fee2e2 60%);
    color: #991b1b;
    font-weight: 600;
}

.legend-sample.mark-key {
    background: linear-gradient(180deg, transparent 60%, #fef3c7 60%);
    color: #92400e;
    font-weight: 500;
}

.legend-sample.exam-critical-sample {
    background: rgba(254, 243, 199, 0.15);
    border-left: 2px solid var(--margin-line);
    padding-left: 8px;
}

.legend-sample.example-sample {
    background: rgba(0, 0, 0, 0.02);
    border-left: 3px solid var(--ruled-line);
    font-style: italic;
    color: var(--text-secondary);
}

.legend-sample.formula-sample {
    background: #fef9e7;
    border-left: 4px solid var(--important-mark);
    font-family: var(--font-margin);
}

@media (max-width: 480px) {
    .legend-btn {
        bottom: 16px;
        right: 16px;
        width: 44px;
        height: 44px;
    }

    .legend-content {
        width: 95%;
        max-height: 85vh;
    }

    .legend-header {
        padding: 16px 20px;
    }

    .legend-body {
        padding: 20px;
    }

    .legend-sample {
        min-width: 120px;
        font-size: 13px;
    }

    .legend-desc {
        font-size: 12px;
    }
}

/* ============================================
   TABLE OF CONTENTS OUTLINE (Right Side)
   ============================================ */
.toc-outline {
    position: fixed;
    right: 20px;
    top: 120px;
    width: 220px;
    max-height: calc(100vh - 160px);
    overflow-y: auto;
    padding: 16px;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(8px);
    border-radius: 8px;
    border: 1px solid rgba(201, 165, 122, 0.15);
    opacity: 0.3;
    transition:
        opacity 0.3s ease,
        background 0.3s ease;
    z-index: 90;

    /* Hide scrollbar but keep functionality */
    scrollbar-width: thin;
    scrollbar-color: rgba(201, 165, 122, 0.3) transparent;
}

.toc-outline::-webkit-scrollbar {
    width: 4px;
}

.toc-outline::-webkit-scrollbar-track {
    background: transparent;
}

.toc-outline::-webkit-scrollbar-thumb {
    background: rgba(201, 165, 122, 0.3);
    border-radius: 2px;
}

.toc-outline:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.95);
    border-color: rgba(201, 165, 122, 0.3);
}

.toc-title {
    font-family: var(--font-ui);
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0 0 12px 0;
    padding-bottom: 8px;
    border-bottom: 1px solid rgba(201, 165, 122, 0.2);
}

.toc-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.toc-item {
    margin: 0;
    padding: 0;
}

.toc-link {
    display: block;
    font-family: var(--font-body);
    font-size: 13px;
    line-height: 1.4;
    color: var(--text-primary);
    text-decoration: none;
    padding: 6px 8px;
    border-radius: 4px;
    transition: all 0.2s ease;
    cursor: pointer;
    position: relative;
}

.toc-link:hover {
    background: rgba(201, 165, 122, 0.1);
    color: var(--margin-line);
    padding-left: 12px;
}

.toc-link.active {
    background: rgba(201, 165, 122, 0.15);
    color: var(--margin-line);
    font-weight: 500;
}

.toc-link.active::before {
    content: "";
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 60%;
    background: var(--margin-line);
    border-radius: 2px;
}

/* Level 1 items */
.toc-item.level-1 > .toc-link {
    font-weight: 500;
    margin-top: 8px;
}

.toc-item.level-1:first-child > .toc-link {
    margin-top: 0;
}

/* Level 2 items - indented */
.toc-item.level-2 > .toc-link {
    font-size: 12px;
    padding-left: 20px;
    color: var(--text-secondary);
}

.toc-item.level-2 > .toc-link:hover {
    padding-left: 24px;
}

.toc-item.level-2 > .toc-link.active {
    color: var(--margin-line);
}

/* Hide TOC in library mode */
.paper-sheet.library-mode ~ .toc-outline {
    display: none;
}

/* Responsive: Hide on smaller screens */
@media (max-width: 1200px) {
    .toc-outline {
        display: none;
    }
}
