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

:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --danger: #dc2626;
    --danger-dark: #b91c1c;
    --bg: #f8fafc;
    --card-bg: #ffffff;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --radius: 8px;
}

html {
    font-size: 15px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.6;
}

/* Navbar */

.navbar {
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
    padding: 0 1.5rem;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow);
}

.nav-inner {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
}

.nav-brand {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--primary);
    text-decoration: none;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 1.2rem;
}

.nav-links a {
    color: var(--text);
    text-decoration: none;
    font-size: 0.9rem;
    transition: color 0.15s;
}

.nav-links a:hover {
    color: var(--primary);
}

.nav-user {
    font-weight: 600;
    font-size: 0.9rem;
}

.badge {
    display: inline-block;
    font-size: 0.72rem;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 600;
}

.badge-advisor {
    background: #fef3c7;
    color: #b45309;
}

.badge-student {
    background: #dbeafe;
    color: #1d4ed8;
}

.btn-logout {
    color: var(--danger) !important;
    font-weight: 600;
}

/* Container */

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1.5rem;
}

/* Flash messages */

.flash-messages {
    margin-bottom: 1.5rem;
}

.flash-msg {
    background: #dbeafe;
    color: #1e40af;
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 0.5rem;
    border-left: 4px solid var(--primary);
}

/* Auth pages */

.auth-container {
    max-width: 420px;
    margin: 3rem auto;
    background: var(--card-bg);
    padding: 2rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
}

.auth-container h2 {
    text-align: center;
    margin-bottom: 1.5rem;
    font-size: 1.3rem;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.auth-switch {
    text-align: center;
    margin-top: 1.2rem;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.auth-switch a {
    color: var(--primary);
    text-decoration: none;
    font-weight: 600;
}

/* Forms */

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.form-group label {
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--text);
}

.form-group input,
.form-group select,
.form-group textarea {
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    font-family: inherit;
    transition: border-color 0.15s;
    background: var(--card-bg);
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.input-title {
    font-size: 1.1rem !important;
    font-weight: 600;
}

/* Buttons */

.btn {
    display: inline-block;
    padding: 0.55rem 1.1rem;
    border: 1px solid var(--border);
    background: var(--card-bg);
    color: var(--text);
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
    font-size: 0.9rem;
    font-family: inherit;
    transition: all 0.15s;
}

.btn:hover {
    background: #f1f5f9;
}

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

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-danger {
    background: var(--danger);
    color: #fff;
    border-color: var(--danger);
}

.btn-danger:hover {
    background: var(--danger-dark);
}

.btn-full {
    width: 100%;
}

.btn-sm {
    padding: 0.3rem 0.75rem;
    font-size: 0.82rem;
}

.btn-download {
    color: var(--primary);
    border-color: var(--primary);
}

.btn-download:hover {
    background: var(--primary);
    color: #fff;
}

/* Page header */

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1.5rem;
}

.page-header h1 {
    font-size: 1.4rem;
}

/* Dashboard */

.dashboard h1 {
    font-size: 1.4rem;
    margin-bottom: 1.5rem;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    text-align: center;
    box-shadow: var(--shadow);
}

.stat-number {
    font-size: 2rem;
    font-weight: 700;
    color: var(--primary);
}

.stat-label {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

.quick-actions h2 {
    font-size: 1.15rem;
    margin-bottom: 1rem;
}

.action-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 1rem;
}

.action-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    text-align: center;
    text-decoration: none;
    color: var(--text);
    box-shadow: var(--shadow);
    transition: transform 0.15s, box-shadow 0.15s;
}

.action-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.action-icon {
    display: block;
    font-size: 1.8rem;
    margin-bottom: 0.5rem;
}

/* Upload */

.upload-section {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--shadow);
}

.upload-row {
    display: flex;
    align-items: flex-end;
    gap: 1rem;
    flex-wrap: wrap;
}

.upload-file-group {
    flex: 0 0 auto;
}

.upload-desc-group {
    flex: 1;
    min-width: 200px;
}

.file-label {
    display: inline-block;
    padding: 0.55rem 1rem;
    background: var(--primary);
    color: #fff;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.9rem;
}

.upload-file-group input[type="file"] {
    display: block;
    margin-top: 0.35rem;
    font-size: 0.85rem;
}

.input-desc {
    width: 100%;
}

/* Tables */

.table-wrapper {
    overflow-x: auto;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

thead {
    background: #f1f5f9;
}

th {
    text-align: left;
    padding: 0.75rem 1rem;
    font-weight: 700;
    color: var(--text-muted);
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

td {
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border);
}

.action-cell {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.inline-form {
    display: inline;
}

/* Documents */

.file-list h2 {
    font-size: 1.15rem;
    margin-bottom: 1rem;
}

.doc-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1rem;
}

.doc-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: var(--shadow);
}

.doc-card h3 {
    font-size: 1.05rem;
    margin-bottom: 0.5rem;
}

.doc-meta {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.doc-preview {
    font-size: 0.88rem;
    color: var(--text-muted);
    margin: 0.75rem 0;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-all;
}

.doc-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

/* Editor */

.editor-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.editor-area {
    width: 100%;
    min-height: 450px;
    padding: 1rem !important;
    font-family: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", "Cascadia Code", monospace !important;
    font-size: 0.9rem !important;
    line-height: 1.6 !important;
    resize: vertical;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fafbfc;
}

/* Empty state */

.empty-state {
    text-align: center;
    color: var(--text-muted);
    padding: 3rem 1rem;
    font-size: 0.95rem;
}

/* Responsive */

@media (max-width: 768px) {
    .nav-links {
        gap: 0.6rem;
        font-size: 0.8rem;
    }
    .nav-links a {
        font-size: 0.78rem;
    }
    .upload-row {
        flex-direction: column;
        align-items: stretch;
    }
    .doc-grid {
        grid-template-columns: 1fr;
    }
}

/* Viewer */

.viewer-toolbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
    background: #fafbfc;
}

.viewer-toolbar select {
    padding: 0.35rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.82rem;
    font-family: inherit;
}

.toolbar-sep {
    width: 1px;
    height: 20px;
    background: var(--border);
    margin: 0 0.25rem;
}

.toolbar-info {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-left: auto;
}

.pdf-viewport {
    position: relative;
    display: flex;
    justify-content: center;
    padding: 1rem;
    overflow: auto;
    min-height: 500px;
    background: #525659;
}

.pdf-page-container {
    position: relative;
    display: inline-block;
    box-shadow: 0 2px 12px rgba(0,0,0,0.3);
}

.pdf-page-container canvas {
    display: block;
}

.annotation-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.annotation-layer.active {
    pointer-events: auto;
    cursor: crosshair;
}

.pdf-scroll-viewport {
    position: relative;
    overflow: auto;
    min-height: 500px;
    background: #525659;
    padding: 1rem;
}

.annotation-marker {
    position: absolute;
    border: 2px solid transparent;
    border-radius: 2px;
    pointer-events: auto;
    cursor: pointer;
    transition: box-shadow 0.15s;
}

.annotation-marker.highlight {
    background: rgba(255, 235, 59, 0.35);
    border-color: rgba(255, 193, 7, 0.6);
}

.annotation-marker.note {
    background: rgba(33, 150, 243, 0.3);
    border-color: rgba(33, 150, 243, 0.6);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    color: #1565c0;
    font-weight: 700;
    min-width: 20px;
    min-height: 20px;
}

.annotation-marker:hover {
    z-index: 10;
    outline: 2px solid var(--primary);
}

.selection-box {
    position: absolute;
    top: 0;
    left: 0;
    border: 1px dashed #2196f3;
    background: rgba(33, 150, 243, 0.15);
    pointer-events: none;
    display: none;
}

.annotation-popup {
    position: fixed;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem;
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
    z-index: 1000;
    min-width: 200px;
    display: none;
}

.annotation-popup textarea {
    width: 100%;
    min-height: 60px;
    font-size: 0.82rem;
    padding: 0.4rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    resize: vertical;
    font-family: inherit;
}

.popup-actions {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.5rem;
    justify-content: flex-end;
}

.color-presets {
    display: flex;
    gap: 0.3rem;
    margin-bottom: 0.4rem;
}

.color-dot {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.15s;
}

.color-dot.selected {
    border-color: var(--text);
}

.content-viewer {
    padding: 2rem 1.5rem;
    max-height: 75vh;
    overflow-y: auto;
    line-height: 1.8;
    color: #333;
}

.content-viewer h1, .content-viewer h2, .content-viewer h3 {
    margin: 1em 0 0.5em;
    color: var(--text);
}

.content-viewer p {
    margin-bottom: 0.75em;
}

.content-viewer table {
    border-collapse: collapse;
    margin: 0.75em 0;
    font-size: 0.9rem;
}

.content-viewer td, .content-viewer th {
    padding: 0.4rem 0.75rem;
    border: 1px solid var(--border);
}

.content-viewer img {
    max-width: 100%;
}

.text-editor-area {
    width: 100%;
    min-height: 500px;
    padding: 1rem;
    font-family: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", monospace;
    font-size: 0.88rem;
    line-height: 1.6;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    resize: vertical;
    background: #fafbfc;
}

.image-viewport {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 1rem;
    overflow: auto;
    max-height: 80vh;
    background: #525659;
}

.image-viewport img {
    max-width: 100%;
    object-fit: contain;
    box-shadow: 0 2px 12px rgba(0,0,0,0.3);
}

.file-info-bar {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
}

.file-info-bar .file-name {
    font-weight: 600;
    color: var(--text);
}

.btn-viewer {
    border: none;
    padding: 0.4rem 0.75rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.82rem;
    font-family: inherit;
    transition: all 0.15s;
}

.btn-viewer:hover {
    opacity: 0.85;
}

.btn-tool {
    background: #f1f5f9;
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-tool:hover {
    background: #e2e8f0;
}

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

/* PDF Text Layer */
.pdf-text-layer {
    position: absolute;
    top: 0; left: 0;
    overflow: hidden;
    z-index: 2;
    line-height: 1;
}

.pdf-text-layer span {
    user-select: text;
    -webkit-user-select: text;
    cursor: text;
}

/* Selection Action Bar */
#selection-action-bar {
    z-index: 1000 !important;
}

#selection-action-bar button:hover {
    opacity: 0.8;
}

/* Quill editor overrides */
.ql-editor {
    font-size: 15px;
    line-height: 1.7;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.ql-editor p {
    margin-bottom: 0.5em;
}

.ql-toolbar.ql-snow {
    border: none;
    border-bottom: 1px solid var(--border);
    padding: 4px 8px;
}

.ql-container.ql-snow {
    border: none;
}

.ql-comment {
    cursor: pointer;
}

/* Comments panel */
.comment-item {
    transition: opacity 0.2s;
}

.comment-item.resolved {
    opacity: 0.55;
    border-left: 3px solid #22c55e !important;
}

/* Revision history */
#revisions-list h3 {
    margin-bottom: 0.75rem;
    font-size: 1rem;
}

/* Category tabs */
.category-tabs {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.cat-tab {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0.45rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--card-bg);
    color: var(--text);
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 600;
    transition: all 0.15s;
}

.cat-tab:hover {
    border-color: var(--primary);
    color: var(--primary);
}

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

.cat-icon { font-size: 1rem; }

.cat-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
}

.cat-badge.cat-draft { background: #fef3c7; color: #b45309; }
.cat-badge.cat-paper { background: #dbeafe; color: #1d4ed8; }
.cat-badge.cat-ppt { background: #fce7f3; color: #be185d; }
.cat-badge.cat-video { background: #dcfce7; color: #16a34a; }
.cat-badge.cat-other { background: #f1f5f9; color: #64748b; }

/* Dashboard category cards */
.category-cards {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
    margin-top: 1.5rem;
}

.cat-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem 1rem;
    text-align: center;
    text-decoration: none;
    color: var(--text);
    box-shadow: var(--shadow);
    transition: all 0.15s;
}

.cat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0,0,0,0.1);
    border-color: var(--primary);
}

.cat-card-icon { font-size: 2rem; display: block; margin-bottom: 0.5rem; }
.cat-card-text { font-size: 0.95rem; font-weight: 600; }

@media (max-width: 600px) {
    .category-cards { grid-template-columns: repeat(2, 1fr); }
}

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

/* ===== Mobile Responsive ===== */
@media (max-width: 768px) {
    html { font-size: 14px; }

    /* Navbar */
    .navbar { padding: 0 0.75rem; }
    .nav-inner { height: auto; padding: 0.5rem 0; flex-wrap: wrap; gap: 0.4rem; }
    .nav-brand { font-size: 1rem; }
    .nav-links { width: 100%; gap: 0.35rem; overflow-x: auto; overflow-y: hidden; flex-wrap: nowrap; padding-bottom: 6px; -webkit-overflow-scrolling: touch; scrollbar-width: none; justify-content: flex-start; }
    .nav-links::-webkit-scrollbar { display: none; }
    .nav-links a { font-size: 0.75rem; white-space: nowrap; padding: 0.3rem 0.5rem; flex-shrink: 0; }
    .nav-user { font-size: 0.75rem; white-space: nowrap; flex-shrink: 0; }
    .badge { font-size: 0.68rem; padding: 1px 6px; }
    .btn-logout { white-space: nowrap; flex-shrink: 0; }

    /* Container */
    .container { padding: 0.75rem; }

    /* Page header */
    .page-header { flex-direction: column; align-items: flex-start; gap: 0.5rem; margin-bottom: 1rem; }
    .page-header h1 { font-size: 1.2rem; }

    /* Dashboard */
    .dashboard h1 { font-size: 1.2rem; margin-bottom: 1rem; }
    .category-cards { grid-template-columns: repeat(2, 1fr); gap: 0.6rem; }
    .cat-card { padding: 1rem 0.5rem; }
    .cat-card-icon { font-size: 1.5rem; }
    .cat-card-text { font-size: 0.85rem; }
    .action-grid { grid-template-columns: repeat(2, 1fr); gap: 0.6rem; }
    .action-card { padding: 1rem 0.5rem; }
    .action-icon { font-size: 1.5rem; }

    /* Auth */
    .auth-container { margin: 1rem; padding: 1.5rem; }

    /* Upload */
    .upload-section { padding: 1rem; }
    .upload-row { flex-direction: column; gap: 0.6rem; }
    .upload-desc-group { min-width: 100% !important; }
    .upload-file-group { width: 100%; }
    .upload-file-group label { width: 100%; text-align: center; }
    .upload-file-group input[type="file"] { width: 100%; font-size: 0.8rem; }

    /* Filter bar */
    .filter-bar { flex-direction: column; align-items: stretch !important; gap: 0.35rem; }
    #filter-form { flex-direction: column; }
    #filter-form select { width: 100%; }

    /* Table -> stacked cards */
    .table-wrapper { overflow-x: auto; }
    table { font-size: 0.78rem; }
    th, td { padding: 0.5rem 0.5rem; }
    .action-cell { flex-wrap: wrap; gap: 0.3rem; }
    .action-cell .btn-sm { font-size: 0.72rem; padding: 0.25rem 0.5rem; }

    /* Documents */
    .doc-grid { grid-template-columns: 1fr; }
    .doc-card { padding: 1rem; }

    /* Editor */
    .editor-area { min-height: 300px; font-size: 0.85rem !important; }
    .text-editor-area { min-height: 350px; }

    /* Viewer */
    .viewer-toolbar { padding: 0.5rem 0.75rem; gap: 0.35rem; }
    .toolbar-info { display: none; }
    .pdf-viewport, .pdf-scroll-viewport { padding: 0.5rem; }
    .content-viewer { padding: 1rem; max-height: none; }
    .file-info-bar { flex-wrap: wrap; gap: 0.4rem; font-size: 0.78rem; }

    /* Notification panel */
    #notify-panel { width: 100%; max-width: 100%; right: 0; left: 0; top: auto; position: fixed; top: 0; bottom: auto; border-radius: 0; }

    /* Flash messages */
    .flash-msg { font-size: 0.85rem; padding: 0.6rem 0.75rem; }

    /* Stats */
    .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 0.6rem; }
    .stat-card { padding: 1rem; }
    .stat-number { font-size: 1.5rem; }

    /* Category tabs */
    .category-tabs { gap: 0.3rem; }
    .cat-tab { padding: 0.35rem 0.65rem; font-size: 0.78rem; }
}

@media (max-width: 480px) {
    html { font-size: 13px; }

    .nav-links a { font-size: 0.72rem; }
    .container { padding: 0.5rem; }

    .category-cards { grid-template-columns: repeat(2, 1fr); }
    .action-grid { grid-template-columns: repeat(2, 1fr); }
    .stats-grid { grid-template-columns: 1fr 1fr; }

    .auth-container { margin: 0.5rem; padding: 1.25rem; border-radius: 0; }

    table { font-size: 0.72rem; }
    th, td { padding: 0.4rem 0.4rem; }

    .btn { padding: 0.5rem 0.8rem; font-size: 0.82rem; }
    .btn-sm { padding: 0.25rem 0.5rem; font-size: 0.7rem; }

    .editor-area { min-height: 250px; }
    .text-editor-area { min-height: 280px; }
}
