:root {
    --bg: #f5f7fa;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e2e8f0;
    --border-strong: #cbd5e1;
    --text: #0f172a;
    --text-muted: #64748b;
    --text-soft: #94a3b8;
    --primary: #1e40af;
    --primary-hover: #1e3a8a;
    --primary-soft: #dbeafe;
    --accent: #0ea5e9;
    --success: #16a34a;
    --warning: #d97706;
    --danger: #dc2626;
    --sidebar-bg: #0f172a;
    --sidebar-text: #cbd5e1;
    --sidebar-active: #1e40af;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);
    --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.18);
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --sidebar-w: 248px;
}

* {
    box-sizing: border-box;
}

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

body {
    display: flex;
    min-height: 100vh;
}

/* ===== Sidebar ===== */
.sidebar {
    width: var(--sidebar-w);
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}

.sidebar-header {
    padding: 20px 20px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.logo {
    font-size: 20px;
    font-weight: 700;
    color: #fff;
    letter-spacing: 1px;
}

.logo-sub {
    font-size: 11px;
    color: var(--text-soft);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-top: 2px;
}

.nav {
    flex: 1;
    padding: 12px 0;
    overflow-y: auto;
}

.nav-section {
    padding: 8px 12px;
}

.nav-title {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    color: var(--text-soft);
    padding: 8px 12px 6px;
    font-weight: 600;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    color: var(--sidebar-text);
    text-decoration: none;
    border-radius: var(--radius-sm);
    font-size: 13px;
    transition: background 0.15s, color 0.15s;
}

.nav-link:hover {
    background: rgba(255, 255, 255, 0.06);
    color: #fff;
}

.nav-link.active {
    background: var(--sidebar-active);
    color: #fff;
}

.nav-icon {
    width: 18px;
    text-align: center;
    font-size: 14px;
    opacity: 0.8;
}

.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.user-name {
    color: #fff;
    font-size: 13px;
    font-weight: 500;
}

.user-role {
    color: var(--text-soft);
    font-size: 11px;
}

/* ===== Main ===== */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.topbar {
    height: 56px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 20px;
    gap: 16px;
    position: sticky;
    top: 0;
    z-index: 10;
}

.menu-toggle {
    display: none;
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: var(--text);
}

.breadcrumb {
    font-size: 14px;
    color: var(--text);
    font-weight: 500;
    flex: 1;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.env-badge {
    background: var(--warning);
    color: #fff;
    font-size: 10px;
    padding: 3px 8px;
    border-radius: 4px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.content {
    padding: 24px;
    flex: 1;
}

/* ===== Page header ===== */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 20px;
    gap: 16px;
    flex-wrap: wrap;
}

.page-title {
    font-size: 22px;
    font-weight: 600;
    color: var(--text);
    margin: 0 0 4px 0;
}

.page-desc {
    color: var(--text-muted);
    font-size: 13px;
    margin: 0;
}

.page-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
    font-family: inherit;
}

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

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

.btn-ghost {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border-strong);
}

.btn-ghost:hover {
    background: var(--surface-2);
}

.btn-danger {
    background: var(--surface);
    color: var(--danger);
    border-color: var(--border-strong);
}

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

.btn-sm {
    padding: 4px 10px;
    font-size: 12px;
}

.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 22px;
    color: var(--text-muted);
    line-height: 1;
    padding: 4px 8px;
    border-radius: 4px;
}

.icon-btn:hover {
    background: var(--surface-2);
    color: var(--text);
}

/* ===== Cards / Tables ===== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.card-pad {
    padding: 20px;
}

.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
    gap: 12px;
    flex-wrap: wrap;
}

.search {
    flex: 1;
    max-width: 320px;
    padding: 7px 12px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: inherit;
    background: #fff;
}

.search:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

thead th {
    background: var(--surface-2);
    text-align: left;
    padding: 10px 14px;
    font-weight: 600;
    color: var(--text-muted);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

tbody td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}

tbody tr:last-child td {
    border-bottom: none;
}

tbody tr:hover {
    background: var(--surface-2);
}

.col-actions {
    text-align: right;
    white-space: nowrap;
}

.empty {
    padding: 48px 20px;
    text-align: center;
    color: var(--text-muted);
}

.empty h3 {
    margin: 0 0 6px;
    color: var(--text);
    font-size: 15px;
}

.empty p {
    margin: 0;
    font-size: 13px;
}

/* ===== Badges ===== */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 500;
    background: var(--surface-2);
    color: var(--text-muted);
    border: 1px solid var(--border);
    white-space: nowrap;
}

.badge.success { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.badge.warning { background: #fef3c7; color: #92400e; border-color: #fde68a; }
.badge.danger { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
.badge.info { background: var(--primary-soft); color: var(--primary); border-color: #bfdbfe; }
.badge.muted { background: #f1f5f9; color: #475569; }

/* ===== Form ===== */
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 14px 16px;
    padding: 20px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.field.full {
    grid-column: 1 / -1;
}

.field label {
    font-size: 12px;
    font-weight: 500;
    color: var(--text);
}

.field .req {
    color: var(--danger);
}

.field input,
.field select,
.field textarea {
    padding: 8px 10px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: inherit;
    color: var(--text);
    background: #fff;
}

.field textarea {
    resize: vertical;
    min-height: 70px;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.field .hint {
    font-size: 11px;
    color: var(--text-muted);
}

.field.error input,
.field.error select,
.field.error textarea {
    border-color: var(--danger);
}

.field.error .hint {
    color: var(--danger);
}

/* ===== Modal ===== */
.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.modal[hidden] {
    display: none;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.4);
    backdrop-filter: blur(2px);
}

.modal-card {
    position: relative;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 720px;
    max-height: calc(100vh - 32px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.modal-card form {
    overflow-y: auto;
}

.modal-footer {
    padding: 14px 20px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    background: var(--surface-2);
}

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: 24px;
    right: 24px;
    background: var(--text);
    color: #fff;
    padding: 12px 18px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    z-index: 200;
    font-size: 13px;
    animation: toast-in 0.2s ease;
}

.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

@keyframes toast-in {
    from { transform: translateY(8px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

/* ===== Dashboard ===== */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 14px;
    margin-bottom: 20px;
}

.kpi {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    box-shadow: var(--shadow-sm);
}

.kpi-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--text-muted);
    font-weight: 600;
}

.kpi-value {
    font-size: 28px;
    font-weight: 600;
    color: var(--text);
    margin-top: 6px;
    line-height: 1;
}

.kpi-sub {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
}

.dash-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 16px;
}

/* ===== Dashboard charts ===== */
.chart-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
    gap: 16px;
}

.chart-card {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chart-head {
    padding: 16px 18px 4px;
}

.chart-head .section-title {
    margin-bottom: 4px;
}

.chart-sub {
    font-size: 12px;
    color: var(--text-muted);
    margin: 0;
    line-height: 1.45;
}

.chart-body {
    padding: 12px 18px 18px;
    height: 300px;
    position: relative;
}

.chart-body canvas {
    max-width: 100%;
}

.chart-body-split {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    height: 300px;
}

.chart-body-split > div {
    position: relative;
    min-width: 0;
}

.chart-empty {
    padding: 32px 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
    background: var(--surface-2);
    border-radius: var(--radius-sm);
    margin: 12px 18px 18px;
}

@media (max-width: 640px) {
    .chart-body-split {
        grid-template-columns: 1fr;
        height: auto;
    }
    .chart-body-split > div {
        height: 240px;
    }
}

.section-title {
    font-size: 14px;
    font-weight: 600;
    margin: 0 0 12px;
    color: var(--text);
}

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

.report-list li {
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.report-list li:last-child { border-bottom: none; }

.report-list .label { color: var(--text); font-weight: 500; }

.report-list .meta { color: var(--text-muted); font-size: 12px; }

/* ===== Auth (login + fatal error) ===== */
.auth-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: var(--bg);
}

.auth-card {
    width: 100%;
    max-width: 380px;
    padding: 28px;
}

.auth-brand {
    text-align: center;
    margin-bottom: 24px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--border);
}

.auth-brand .logo {
    color: var(--text);
    font-size: 22px;
    letter-spacing: 1px;
}

.auth-brand .logo-sub {
    color: var(--text-muted);
}

.auth-error {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fecaca;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    margin-bottom: 14px;
}

.auth-card .field {
    margin-bottom: 14px;
}

.auth-card .field input {
    width: 100%;
    padding: 9px 11px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-family: inherit;
}

.auth-card .field input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.auth-card form {
    padding: 0;
}

/* ===== Hidden when not authenticated ===== */
.sidebar[hidden], .topbar[hidden] { display: none; }

/* ===== Responsive ===== */
@media (max-width: 880px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        transform: translateX(-100%);
        transition: transform 0.2s;
        z-index: 50;
        box-shadow: var(--shadow-lg);
    }
    .sidebar.open { transform: translateX(0); }
    .menu-toggle { display: block; }
    .form-grid { grid-template-columns: 1fr; }
    .content { padding: 16px; }
}
