:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --border: #e1e5ea;
    --text: #1f2937;
    --text-muted: #6b7280;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --success-bg: #ecfdf5;
    --success-text: #065f46;
    --error-bg: #fef2f2;
    --error-text: #991b1b;
    --warn-bg: #fffbeb;
    --warn-text: #92400e;
    --radius: 8px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- Topbar --------------------------------------------------------- */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.topbar-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0.75rem 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.brand {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--text);
}
.brand:hover { text-decoration: none; }
.brand-logo { height: 32px; width: auto; border-radius: 4px; }
.topnav { display: flex; align-items: center; gap: 1.25rem; }
.topnav-user { color: var(--text-muted); font-size: 0.9rem; }

/* --- Layout ----------------------------------------------------------- */
.page { max-width: 1200px; margin: 0 auto; padding: 1.5rem; }
.admin-shell { display: flex; max-width: 1200px; margin: 0 auto; }
.admin-sidebar {
    width: 200px;
    flex-shrink: 0;
    padding: 1.5rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}
.admin-sidebar a {
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius);
    color: var(--text);
}
.admin-sidebar a:hover { background: var(--border); text-decoration: none; }
.admin-sidebar a.active { background: var(--primary); color: #fff; }
.admin-content { flex: 1; padding: 1.5rem; min-width: 0; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
h1 { margin: 0 0 0.5rem; font-size: 1.5rem; }
h2 { font-size: 1.1rem; margin-top: 0; }

/* --- Auth pages --------------------------------------------------------- */
.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.auth-wrap { display: flex; align-items: center; justify-content: center; width: 100%; padding: 1rem; }
.auth-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    width: 100%;
    max-width: 380px;
    text-align: center;
}
.auth-logo { height: 56px; margin-bottom: 0.75rem; }
.auth-card h1 { font-size: 1.25rem; }
.auth-card form { text-align: left; display: flex; flex-direction: column; gap: 0.9rem; margin-top: 1rem; }

/* --- Forms ------------------------------------------------------------- */
label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.9rem; color: var(--text-muted); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=file], select, textarea {
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    background: var(--surface);
    color: var(--text);
}
.checkbox-label { flex-direction: row; align-items: center; gap: 0.5rem; }
.form-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); align-items: end; }
.form-inline { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; }
.inline-form { display: inline; }

.btn {
    display: inline-block;
    padding: 0.6rem 1.2rem;
    border-radius: var(--radius);
    border: none;
    font-size: 0.95rem;
    cursor: pointer;
    text-align: center;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); text-decoration: none; }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover { background: #d3d9e0; text-decoration: none; }
.btn-link { background: none; border: none; color: var(--primary); cursor: pointer; padding: 0; font-size: 0.9rem; }
.btn-link:hover { text-decoration: underline; }
.btn-danger { color: var(--danger); }

/* --- Alerts -------------------------------------------------------------- */
.alert { padding: 0.7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; font-size: 0.9rem; }
.alert-success { background: var(--success-bg); color: var(--success-text); }
.alert-error { background: var(--error-bg); color: var(--error-text); }

/* --- Cards ---------------------------------------------------------------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1.25rem; }
.card-row { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); margin-bottom: 1.25rem; }

/* --- Stat grid -------------------------------------------------------------- */
.stat-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); margin: 1rem 0 1.5rem; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.2rem; text-align: center; }
.stat-num { font-size: 1.6rem; font-weight: 700; }
.stat-label { color: var(--text-muted); font-size: 0.85rem; margin-top: 0.2rem; }

/* --- Breadcrumbs ------------------------------------------------------------ */
.breadcrumbs { margin-bottom: 1rem; font-size: 0.9rem; color: var(--text-muted); }

/* --- File grid (user dashboard) --------------------------------------------- */
.file-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.file-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.2rem 0.8rem;
    text-align: center;
    color: var(--text);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
}
.file-card:hover { border-color: var(--primary); text-decoration: none; }
.file-icon { font-size: 2rem; }
.file-name { font-size: 0.85rem; word-break: break-word; }
.file-meta { font-size: 0.75rem; color: var(--text-muted); }

/* --- Tables ------------------------------------------------------------------ */
.admin-table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.admin-table th, .admin-table td { padding: 0.6rem 0.8rem; border-bottom: 1px solid var(--border); text-align: left; font-size: 0.9rem; }
.admin-table th { background: var(--bg); font-weight: 600; }
.admin-table td.actions { display: flex; gap: 0.7rem; flex-wrap: wrap; align-items: center; }

/* --- Badges ------------------------------------------------------------------ */
.badge { display: inline-block; padding: 0.15rem 0.5rem; border-radius: 999px; font-size: 0.75rem; }
.badge-admin { background: #ede9fe; color: #5b21b6; }
.badge-ok { background: var(--success-bg); color: var(--success-text); }
.badge-off { background: #f3f4f6; color: var(--text-muted); }
.badge-warn { background: var(--warn-bg); color: var(--warn-text); }

/* --- Viewer ------------------------------------------------------------------ */
.viewer-frame { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; min-height: 300px; display: flex; flex-direction: column; align-items: center; }
.no-select { user-select: none; -webkit-user-select: none; }
.viewer-image { max-width: 100%; height: auto; border-radius: 4px; }
.viewer-media { max-width: 100%; }
.pdf-container { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 0.75rem; }
.pdf-page { max-width: 100%; box-shadow: 0 1px 4px rgba(0,0,0,0.15); }
.text-viewer { width: 100%; white-space: pre-wrap; word-break: break-word; background: var(--bg); padding: 1rem; border-radius: 4px; max-height: 70vh; overflow: auto; text-align: left; }

/* --- Misc ------------------------------------------------------------------ */
.muted { color: var(--text-muted); }
.small { font-size: 0.82rem; }
.settings-logo-preview { height: 64px; display: block; margin-bottom: 1rem; border-radius: 4px; }
.pagination { display: flex; gap: 0.4rem; margin-top: 1rem; }
.pagination a { padding: 0.35rem 0.7rem; border: 1px solid var(--border); border-radius: 4px; color: var(--text); }
.pagination a.active { background: var(--primary); color: #fff; border-color: var(--primary); }

dialog { border-radius: var(--radius); border: 1px solid var(--border); padding: 1.5rem; max-width: 360px; width: 100%; }
dialog::backdrop { background: rgba(15, 23, 42, 0.45); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1rem; }

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1420;
        --surface: #161d2b;
        --border: #263042;
        --text: #e5e9f0;
        --text-muted: #94a3b8;
        --success-bg: #052e21;
        --success-text: #6ee7b7;
        --error-bg: #350a0a;
        --error-text: #fca5a5;
        --warn-bg: #3a2a05;
        --warn-text: #fcd34d;
    }
    .badge-admin { background: #2e1065; color: #d8b4fe; }
    .badge-off { background: #1f2937; color: var(--text-muted); }
}
