:root {
    color-scheme: light dark;
    --bg: #F7F5F2;
    --surface: #ffffff;
    --surface-2: #F0EEEB;
    --elevated: #ffffff;
    --text: #222222;
    --muted: #667085;
    --line: #E5E2DE;
    --brand: #A90000;
    --brand-dark: #8E0000;
    --accent: #E58A68;
    --good: #12B76A;
    --warn: #F79009;
    --risk: #F04438;
    --info: #2E90FA;
    --shadow: 0 8px 24px rgba(34, 34, 34, .07);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #111111;
    --surface: #1D1D1D;
    --surface-2: #242424;
    --elevated: #272727;
    --text: #F5F3F0;
    --muted: #A9A7A4;
    --line: #343434;
    --brand: #C51A1A;
    --brand-dark: #E02929;
    --accent: #E58A68;
    --good: #32D583;
    --warn: #FDB022;
    --risk: #F97066;
    --info: #53B1FD;
    --shadow: none;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; }
button, input, select, textarea { font: inherit; }
h1, h2, p { margin-top: 0; }
h1 { font-size: 28px; line-height: 1.2; margin-bottom: 6px; letter-spacing: 0; }
h2 { font-size: 18px; margin-bottom: 5px; letter-spacing: 0; }
.muted, small, .section-heading p { color: var(--muted); }
.brand-mark { position: relative; width: 36px; height: 36px; display: grid; place-items: center; color: transparent; background: var(--brand); border-radius: 8px; font-size: 0; overflow: hidden; }
.brand-mark::before { content: ""; position: absolute; inset: 8px; border: 2px solid #fff; border-radius: 7px; }
.brand-mark::after { content: ""; position: absolute; width: 9px; height: 9px; border-radius: 999px; background: #fff; box-shadow: 0 0 0 4px rgba(229, 138, 104, .55); }
.button { min-height: 40px; border: 1px solid transparent; border-radius: 6px; padding: 0 16px; cursor: pointer; font-weight: 700; }
.button-primary { width: 100%; background: var(--brand); color: #fff; }
.button-primary:hover { background: var(--brand-dark); }
.button-secondary { background: var(--surface); border-color: var(--line); color: var(--text); }
.button-quiet { background: transparent; border-color: var(--line); color: var(--text); }
.alert { border: 1px solid var(--line); border-radius: 6px; padding: 12px 14px; margin: 16px 0; }
.alert-error { border-color: #efb2b2; background: #fff4f4; color: #8f2222; }
.alert-success { border-color: #a8d7c2; background: #f0faf5; color: #155f42; }

.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at 50% 0%, color-mix(in srgb, var(--accent) 12%, transparent), transparent 34%), var(--bg); }
.auth-panel { width: min(100%, 420px); padding: 36px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow); }
.auth-panel .brand-name { margin: 10px 0 30px; font-weight: 800; }
.stack-lg { display: grid; gap: 18px; margin-top: 26px; }
label > span { display: block; margin-bottom: 7px; font-weight: 650; }
input, select, textarea { width: 100%; min-height: 42px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--text); }
select[multiple] { min-height: 120px; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 3px solid color-mix(in srgb, var(--brand) 18%, transparent); border-color: var(--brand); }
.check-row { display: flex; align-items: center; gap: 9px; justify-content: flex-start; }
.check-row input[type="checkbox"] { width: auto; min-height: auto; }
.check-row span { margin: 0; font-weight: 500; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 240px minmax(0, 1fr); }
.sidebar { background: #222222; color: #F5F3F0; padding: 22px 16px; display: flex; flex-direction: column; min-height: 100vh; }
.brand-lockup { display: flex; align-items: center; gap: 11px; padding: 0 8px 24px; font-size: 18px; }
.sidebar nav { display: grid; gap: 4px; }
.nav-group { margin: 18px 12px 5px; color: #A9A7A4; font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0; }
.nav-item { color: #F5F3F0; text-decoration: none; padding: 11px 12px; border-radius: 6px; }
.nav-item.active { background: var(--brand); color: #fff; font-weight: 700; }
.nav-item.muted { color: #A9A7A4; }
.context-chip button { width: 100%; min-height: 34px; border: 1px solid #4a4a4a; border-radius: 6px; background: #2b2b2b; color: #F5F3F0; font-weight: 700; }
.theme-control span { color: #A9A7A4; font-size: 11px; text-transform: uppercase; }
.theme-control select { min-height: 36px; margin-top: 5px; background: #2b2b2b; color: #F5F3F0; border-color: #4a4a4a; }
.sidebar-footer { margin-top: auto; padding: 18px 8px 0; border-top: 1px solid #3a3a3a; display: grid; gap: 14px; }
.sidebar-footer small { display: block; margin-top: 3px; color: #A9A7A4; text-transform: capitalize; }
.sidebar-footer .button { width: 100%; color: #F5F3F0; border-color: #4a4a4a; }
.content { width: min(100%, 1500px); min-width: 0; padding: 30px 36px 56px; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.eyebrow { margin-bottom: 8px; color: var(--muted); font-weight: 650; }
.live-indicator { display: inline-flex; align-items: center; gap: 8px; color: var(--good); font-weight: 700; }
.live-indicator span { width: 8px; height: 8px; border-radius: 50%; background: var(--good); }
.metric-grid, .stats-grid { display: grid; grid-template-columns: repeat(5, minmax(135px, 1fr)); gap: 12px; margin-bottom: 28px; }
.metric-grid-compact { grid-template-columns: repeat(4, minmax(135px, 1fr)); }
.metric-grid-seven { grid-template-columns: repeat(7, minmax(110px, 1fr)); }
.metric-grid-seven .metric { min-height: 92px; padding: 14px; }
.metric-grid-seven .metric strong { margin-top: 12px; font-size: 25px; }
.metric, .stat-card { min-height: 112px; padding: 18px; border-radius: 8px; border: 1px solid var(--line); border-top: 3px solid var(--accent); background: var(--surface); box-shadow: var(--shadow); }
.metric span, .stat-card span { color: var(--muted); font-weight: 650; }
.metric strong, .stat-card strong { display: block; margin-top: 18px; font-size: 30px; line-height: 1; }
.metric-good { border-top-color: var(--good); }
.metric-warn { border-top-color: var(--warn); }
.metric-risk { border-top-color: var(--risk); }
.section-block { min-width: 0; margin-top: 28px; }
.section-heading { display: flex; justify-content: space-between; align-items: end; gap: 20px; margin-bottom: 13px; }
.section-heading p { margin-bottom: 0; }
.count { min-width: 32px; height: 28px; display: grid; place-items: center; border-radius: 999px; background: var(--surface-2); font-weight: 800; }
.table-wrap { width: 100%; max-width: 100%; overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
table { width: 100%; border-collapse: collapse; min-width: 940px; }
th, td { padding: 14px 16px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { background: var(--surface-2); color: var(--muted); font-size: 12px; text-transform: uppercase; }
td small { display: block; margin-top: 3px; }
tbody tr:last-child td { border-bottom: 0; }
.badge { display: inline-block; padding: 5px 8px; border-radius: 5px; background: color-mix(in srgb, var(--good) 14%, var(--surface)); color: var(--good); font-size: 12px; font-weight: 800; }
.badge-risk, .priority-urgent { background: color-mix(in srgb, var(--risk) 14%, var(--surface)); color: var(--risk); }
.priority-high { background: color-mix(in srgb, var(--warn) 16%, var(--surface)); color: var(--warn); }
.priority-low { background: #edf0f2; color: #53606a; }
.empty { padding: 36px; text-align: center; color: var(--muted); }
.device-list { border-top: 1px solid var(--line); }
.device-row { display: flex; align-items: center; justify-content: space-between; gap: 18px; padding: 14px 0; border-bottom: 1px solid var(--line); }
.device-row small { display: block; margin-top: 4px; }
.tool-panel { padding: 20px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 18px; }
.span-2 { grid-column: 1 / -1; }
.form-actions { display: flex; justify-content: flex-end; }
.button-auto { width: auto; }
.filter-bar { display: grid; grid-template-columns: minmax(220px, 2fr) repeat(3, minmax(130px, 1fr)) auto; gap: 10px; align-items: end; margin-bottom: 14px; }
.report-filters { grid-template-columns: repeat(4, minmax(150px, 1fr)) auto; padding: 16px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.inline-actions, .export-actions { display: flex; align-items: center; gap: 8px; }
.inline-actions { flex-wrap: wrap; }
.export-actions { justify-content: flex-end; margin-top: 14px; }
.export-actions .button { display: inline-grid; place-items: center; text-decoration: none; }
.pagination { margin-top: 16px; }
.pagination nav > div:first-child { display: none; }
.pagination nav > div:last-child { display: flex; justify-content: space-between; align-items: center; }
.pagination svg { width: 18px; }
.report-table { min-width: 1180px; }
a { color: var(--brand); }
.link-button { display: inline-grid; place-items: center; text-decoration: none; }
.edit-popover { position: relative; }
.edit-popover summary { list-style: none; display: grid; place-items: center; }
.edit-popover summary::-webkit-details-marker { display: none; }
.edit-form { position: absolute; z-index: 10; right: 0; top: 46px; width: min(620px, 80vw); padding: 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow); }
.edit-form textarea, .edit-form button { grid-column: 1 / -1; }
.two-column-sections { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.simple-list { border-top: 1px solid var(--line); }
.simple-list > div { min-height: 54px; display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.simple-list small { display: block; margin-top: 3px; overflow-wrap: anywhere; }
.branch-map { width: 100%; min-height: 420px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.branch-editor { overflow: hidden; }
.edit-row td { background: color-mix(in srgb, var(--surface-2) 70%, var(--surface)); white-space: normal; }
.compact-form { margin: 8px 0; padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.profile-tabs { display: flex; flex-wrap: wrap; gap: 8px; margin: -8px 0 20px; }
.profile-tabs a { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--text); text-decoration: none; font-weight: 700; }
.permission-grid { max-height: 340px; min-width: 320px; overflow: auto; display: grid; gap: 6px; padding: 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface-2); }
.permission-grid strong { margin-top: 8px; color: var(--muted); font-size: 12px; text-transform: uppercase; }

:root[data-theme="dark"] .alert-error { background: #351d20; color: #ffc2c2; border-color: #723b42; }
:root[data-theme="dark"] .alert-success { background: #173126; color: #aee1c8; border-color: #315e4a; }

@media (max-width: 980px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar { min-height: auto; flex-direction: row; align-items: center; gap: 20px; overflow-x: auto; }
    .brand-lockup { padding: 0; }
    .sidebar nav { display: flex; }
    .sidebar-footer { margin: 0 0 0 auto; padding: 0; border: 0; grid-template-columns: auto auto; align-items: center; }
    .metric-grid, .stats-grid { grid-template-columns: repeat(2, minmax(140px, 1fr)); }
    .metric-grid-seven { grid-template-columns: repeat(3, minmax(130px, 1fr)); }
    .filter-bar, .report-filters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .two-column-sections { grid-template-columns: 1fr; gap: 0; }
}

@media (max-width: 620px) {
    .content { padding: 22px 16px 40px; }
    .sidebar { padding: 12px; }
    .sidebar nav, .sidebar-footer > div { display: none; }
    .metric-grid, .stats-grid { grid-template-columns: 1fr 1fr; }
    .metric { min-height: 96px; padding: 14px; }
    .metric strong { font-size: 25px; }
    .auth-panel { padding: 26px 22px; }
    .form-grid, .filter-bar, .report-filters { grid-template-columns: 1fr; }
    .span-2 { grid-column: auto; }
}
