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

:root {
    --primary: #3b82f6;
    --primary-dark: #1d4ed8;
    --success: #22c55e;
    --warning: #f59e0b;
    --danger: #ef4444;
    --bg: #f1f5f9;
    --surface: #ffffff;
    --text: #0f172a;
    --text2: #64748b;
    --border: #e2e8f0;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-lg: 0 10px 25px rgba(0,0,0,.12);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
}
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* === LOGIN === */
.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
}
.login-card {
    background: var(--surface);
    border-radius: 12px;
    padding: 40px;
    width: 380px;
    box-shadow: var(--shadow-lg);
}
.login-card h1 {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 6px;
    color: var(--text);
}
.login-card p {
    color: var(--text2);
    font-size: 14px;
    margin-bottom: 28px;
}
.form-group {
    margin-bottom: 16px;
}
.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text2);
}
.form-group input, .form-group select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: inherit;
    transition: border-color .2s;
    outline: none;
}
.form-group input:focus, .form-group select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: all .15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #16a34a; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-ghost { background: transparent; color: var(--text2); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-full { width: 100%; }
.alert { padding: 10px 14px; border-radius: var(--radius); font-size: 13px; margin-bottom: 16px; }
.alert-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }

/* === TOPBAR === */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 24px;
    height: 56px;
    min-height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    z-index: 50;
}
.topbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
}
.topbar-logo {
    font-size: 18px;
    font-weight: 700;
    color: var(--primary);
}
.topbar-right {
    display: flex;
    align-items: center;
    gap: 12px;
}
.user-badge {
    font-size: 13px;
    color: var(--text2);
}
.class-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    background: var(--primary);
    color: #fff;
}
.class-badge.c0 { background: var(--danger); }
.class-badge.c1 { background: var(--warning); color: #000; }
.class-badge.c2 { background: var(--primary); }
.class-badge.c3 { background: var(--success); }

/* === BREADCRUMB === */
.breadcrumb-bar {
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    flex-wrap: wrap;
    flex-shrink: 0;
}
.breadcrumb-bar a {
    color: var(--primary);
    text-decoration: none;
    cursor: pointer;
}
.breadcrumb-bar a:hover { text-decoration: underline; }
.breadcrumb-bar span { color: var(--text2); }

/* === FILE LIST === */
.file-container {
    padding: 20px 24px;
    max-width: 1100px;
    margin: 0 auto;
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    width: 100%;
}
.file-table {
    width: 100%;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border-collapse: collapse;
    overflow: hidden;
}
.file-table th {
    text-align: left;
    padding: 10px 16px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text2);
    text-transform: uppercase;
    letter-spacing: .5px;
    border-bottom: 1px solid var(--border);
    background: #f8fafc;
}
.file-table td {
    padding: 10px 16px;
    font-size: 14px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.file-table tr:last-child td { border-bottom: none; }
.file-table tr:hover td { background: #f8fafc; }
.file-row { cursor: pointer; }
.file-name {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 500;
}
.file-icon {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}
.file-icon.folder { background: #fef3c7; }
.file-icon.file { background: #e0f2fe; }
.file-icon.image { background: #fce7f3; }
.file-icon.pdf { background: #fee2e2; }
.file-icon.code { background: #e0e7ff; }
.file-size, .file-date { color: var(--text2); font-size: 13px; }
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text2);
}
.empty-state .icon { font-size: 48px; margin-bottom: 12px; }

/* === MODAL / POPUP === */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal {
    background: var(--surface);
    border-radius: 12px;
    width: 700px;
    max-width: 95vw;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
    animation: slideUp .2s ease;
}
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.modal-header h2 { font-size: 18px; font-weight: 600; }
.modal-close {
    width: 32px; height: 32px;
    border: none; background: transparent;
    font-size: 20px; cursor: pointer;
    border-radius: 6px; color: var(--text2);
    display: flex; align-items: center; justify-content: center;
}
.modal-close:hover { background: var(--bg); }
.modal-body {
    padding: 20px 24px;
    overflow-y: auto;
    flex: 1;
}
.modal-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0;
}
.modal-tab {
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    border: none;
    background: transparent;
    cursor: pointer;
    color: var(--text2);
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    font-family: inherit;
}
.modal-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* === ADMIN TABLES === */
.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
.admin-table th {
    text-align: left;
    padding: 8px 12px;
    font-weight: 600;
    color: var(--text2);
    border-bottom: 1px solid var(--border);
    font-size: 12px;
}
.admin-table td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.admin-table tr:last-child td { border-bottom: none; }
.inline-form {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    margin-bottom: 20px;
    flex-wrap: wrap;
}
.inline-form .form-group { margin-bottom: 0; }
.inline-form input, .inline-form select {
    padding: 8px 10px;
    font-size: 13px;
}
.perm-select {
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 12px;
    font-family: inherit;
    background: var(--surface);
    cursor: pointer;
}
.perm-select:focus {
    border-color: var(--primary);
    outline: none;
}
.perm-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}
.perm-item:last-child { border-bottom: none; }
.perm-item .perm-path {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 6px;
}
.perm-item .perm-indent {
    display: inline-block;
    width: 20px;
}
.perm-none { color: var(--text2); font-style: italic; font-size: 12px; }

/* === NAMED CLASS BADGES === */
.named-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    background: #e0e7ff;
    color: #3730a3;
    white-space: nowrap;
}
.named-badges {
    display: inline-flex;
    gap: 4px;
    flex-wrap: wrap;
}
.access-cell {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
}

/* === PERM CONTROLS (level + named checkboxes) === */
.perm-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    flex-shrink: 0;
}
.nc-check, .nc-check-sm {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
}
.nc-check input, .nc-check-sm input {
    cursor: pointer;
    accent-color: var(--primary);
}

/* === ALIAS INPUT === */
.alias-input {
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    font-family: inherit;
    width: 200px;
    transition: border-color .2s;
}
.alias-input:focus {
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}

/* === MODAL WIDTH for more tabs === */
.modal { width: 800px; }

/* === ADMIN TOOLBAR === */
.admin-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    margin-bottom: 4px;
}

/* === DROP ZONE === */
.drop-zone {
    position: relative;
    transition: background .2s, border-color .2s;
    border: 2px dashed transparent;
    border-radius: var(--radius);
    min-height: 100px;
}
.drop-zone.drop-active {
    background: rgba(59, 130, 246, .06);
    border-color: var(--primary);
}
.drop-zone.drop-active::after {
    content: 'Drop files to upload';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(59, 130, 246, .08);
    color: var(--primary);
    font-size: 16px;
    font-weight: 600;
    border-radius: var(--radius);
    pointer-events: none;
    z-index: 10;
}
.drop-zone.drop-uploading::after {
    content: 'Uploading...';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255,255,255,.85);
    color: var(--primary);
    font-size: 16px;
    font-weight: 600;
    border-radius: var(--radius);
    pointer-events: none;
    z-index: 10;
}

/* === FILE ROW SELECTION === */
.file-row.selected {
    background: rgba(59, 130, 246, .08) !important;
}
.file-row.selected td { background: transparent; }
.chk-cell { text-align: center; width: 32px; padding: 0 4px !important; }
.chk-cell input, #select-all-chk {
    cursor: pointer;
    accent-color: var(--primary);
    width: 15px; height: 15px;
}

/* === SELECTION TOOLBAR === */
.sel-toolbar {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: 12px;
    padding-left: 12px;
    border-left: 1px solid var(--border);
}
.sel-count {
    font-size: 12px;
    font-weight: 600;
    color: var(--primary);
    white-space: nowrap;
}

/* === CONTEXT MENU === */
.ctx-menu {
    position: absolute;
    z-index: 9999;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    min-width: 160px;
    padding: 4px 0;
    font-size: 13px;
}
.ctx-item {
    padding: 8px 16px;
    cursor: pointer;
    transition: background .15s;
    white-space: nowrap;
}
.ctx-item:hover { background: rgba(59,130,246,.08); }
.ctx-danger { color: var(--danger); }
.ctx-danger:hover { background: rgba(239,68,68,.08); }

/* === DRAG-OUT DOWNLOAD LINK === */
.file-dl-link {
    display: flex;
    align-items: center;
    gap: 8px;
    color: inherit;
    text-decoration: none;
    cursor: default;
}
.file-dl-link:hover { color: inherit; text-decoration: none; }
.file-dl-link[draggable="true"] { cursor: grab; }
.file-dl-link[draggable="true"]:active { cursor: grabbing; }

/* === COMPACT DROPDOWN (cdd) === */
.cdd-wrap {
    position: relative;
    display: inline-block;
}
.cdd-badge {
    cursor: pointer;
    user-select: none;
    min-width: 28px;
    text-align: center;
}
.cdd-badge-none {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    background: var(--bg);
    color: var(--text2);
    border: 1px dashed var(--border);
    cursor: pointer;
    user-select: none;
    min-width: 28px;
    text-align: center;
}
.cdd-panel {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 100;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    min-width: 140px;
    max-height: 220px;
    overflow-y: auto;
    padding: 4px 0;
    margin-top: 4px;
}
.cdd-panel.open { display: block; }
.cdd-opt {
    padding: 6px 14px;
    font-size: 12px;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s;
}
.cdd-opt:hover { background: rgba(59,130,246,.08); }
.cdd-opt.active { color: var(--primary); font-weight: 600; }
.cdd-chk {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 14px;
    font-size: 12px;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s;
}
.cdd-chk:hover { background: rgba(59,130,246,.08); }
.cdd-chk input {
    cursor: pointer;
    accent-color: var(--primary);
}

/* === MARKDOWN PREVIEW MODAL === */
.md-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(0,0,0,.55);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.md-modal {
    position: relative;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 860px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.md-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.md-title {
    font-weight: 600;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.md-actions { display: flex; gap: 6px; flex-shrink: 0; }
.md-body {
    padding: 24px 32px;
    overflow-y: auto;
    flex: 1;
    line-height: 1.7;
    font-size: 15px;
}

/* Markdown content styles */
.markdown-content h1 { font-size: 1.8em; font-weight: 700; margin: 0 0 16px; padding-bottom: 8px; border-bottom: 2px solid var(--border); }
.markdown-content h2 { font-size: 1.4em; font-weight: 600; margin: 24px 0 12px; padding-bottom: 6px; border-bottom: 1px solid var(--border); }
.markdown-content h3 { font-size: 1.15em; font-weight: 600; margin: 20px 0 8px; }
.markdown-content h4, .markdown-content h5, .markdown-content h6 { font-size: 1em; font-weight: 600; margin: 16px 0 6px; }
.markdown-content p { margin: 0 0 12px; }
.markdown-content ul, .markdown-content ol { margin: 0 0 12px; padding-left: 24px; }
.markdown-content li { margin-bottom: 4px; }
.markdown-content blockquote {
    margin: 0 0 12px;
    padding: 8px 16px;
    border-left: 4px solid var(--primary);
    background: rgba(59,130,246,.06);
    border-radius: 0 var(--radius) var(--radius) 0;
    color: var(--text2);
}
.markdown-content code {
    background: rgba(0,0,0,.06);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.9em;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}
.markdown-content pre {
    background: #1e1e2e;
    color: #cdd6f4;
    padding: 16px;
    border-radius: var(--radius);
    overflow-x: auto;
    margin: 0 0 16px;
    line-height: 1.5;
}
.markdown-content pre code {
    background: none;
    padding: 0;
    color: inherit;
    font-size: 13px;
}
.markdown-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 0 0 16px;
    font-size: 14px;
}
.markdown-content th, .markdown-content td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    text-align: left;
}
.markdown-content th {
    background: var(--bg);
    font-weight: 600;
}
.markdown-content img { max-width: 100%; border-radius: var(--radius); }
.markdown-content a { color: var(--primary); text-decoration: none; }
.markdown-content a:hover { text-decoration: underline; }
.markdown-content hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

/* Floating Back button — hidden by default, shows on hover/tap */
.md-back-btn {
    position: absolute;
    bottom: 20px;
    left: 20px;
    background: rgba(0,0,0,.7);
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 24px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity .3s ease, transform .3s ease;
    transform: translateY(8px);
    z-index: 10;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}
.md-back-btn.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}
.md-back-btn:hover {
    background: rgba(0,0,0,.85);
}

/* No-preview fallback */
.preview-nopreview {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 24px;
    text-align: center;
    min-height: 200px;
}
.preview-file-icon { font-size: 56px; margin-bottom: 12px; }
.preview-file-name { font-weight: 600; font-size: 16px; word-break: break-all; }
.preview-file-ext { font-size: 13px; color: var(--text2); margin-top: 4px; }

/* Wide modal for PDF */
.md-modal-wide { max-width: 95vw; max-height: 92vh; }
.md-modal-wide .md-body { padding: 0; }
.md-modal-wide .md-body iframe { display: block; min-height: 80vh; }

/* Text/code preview */
.preview-code {
    margin: 0;
    padding: 20px 24px;
    background: #1e1e2e;
    color: #cdd6f4;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 13px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    border-radius: 0;
    overflow-x: auto;
    tab-size: 4;
}

/* ─── Activity Log ─────────────────────────────────────────── */
.activity-summary {
    display: flex; flex-wrap: wrap; gap: 10px; margin: 12px 0;
}
.stat-card {
    flex: 1 1 120px; min-width: 100px;
    background: var(--bg); border-radius: var(--radius);
    padding: 14px 12px; text-align: center;
    border: 1px solid var(--border);
}
.stat-num { font-size: 28px; font-weight: 700; color: var(--primary); line-height: 1.2; }
.stat-label { font-size: 12px; color: var(--text2); margin-top: 2px; }
.act-row-clickable:hover { background: var(--primary-bg, #eff6ff) !important; }

@media (max-width: 600px) {
    .md-overlay { padding: 8px; }
    .md-body { padding: 16px; font-size: 14px; }
    .md-modal-wide { max-width: 100vw; max-height: 100vh; border-radius: 0; }
    .preview-code { font-size: 11px; padding: 12px; }
    .activity-summary { gap: 6px; }
    .stat-card { flex: 1 1 80px; min-width: 70px; padding: 10px 8px; }
    .stat-num { font-size: 20px; }
}
