:root {
    --navy: #0f1b2e;
    --navy-2: #16233a;
    --navy-3: #1e2f4d;
    --bg: #eef1f6;
    --panel: #ffffff;
    --text: #1b2436;
    --muted: #7b869c;
    --border: #e4e8f0;
    --blue: #2f6bff;
    --green: #16b364;
    --amber: #f59e0b;
    --red: #ef4444;
    --shadow: 0 4px 18px rgba(20, 35, 70, 0.06);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
    font-family: 'Inter', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.material-icons-round { font-size: 20px; line-height: 1; }

/* ===================== LOGIN ===================== */
.login-overlay {
    position: fixed; inset: 0; z-index: 100;
    display: flex; align-items: center; justify-content: center;
    background: radial-gradient(1200px 600px at 70% 10%, #1c3a6e 0%, var(--navy) 55%);
    overflow: hidden;
}
.login-bg-grid {
    position: absolute; inset: 0;
    background-image: linear-gradient(rgba(255,255,255,.04) 1px, transparent 1px),
                      linear-gradient(90deg, rgba(255,255,255,.04) 1px, transparent 1px);
    background-size: 42px 42px;
    mask-image: radial-gradient(700px 400px at 50% 40%, #000 30%, transparent 80%);
}
.login-card {
    position: relative; z-index: 2;
    width: 380px; max-width: 92vw;
    background: rgba(255,255,255,0.98);
    border-radius: 18px; padding: 34px 30px;
    box-shadow: 0 30px 80px rgba(0,0,0,.4);
}
.login-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 26px; }
.login-brand .brand-icon { font-size: 40px; color: var(--blue); }
.login-brand h1 { font-size: 24px; font-weight: 800; letter-spacing: -.5px; }
.login-brand p { font-size: 12px; color: var(--muted); }
.login-form { display: flex; flex-direction: column; gap: 16px; }
.login-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: #46506a; }
.login-form input {
    padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px;
    font-size: 14px; font-family: inherit; outline: none; transition: border .15s;
}
.login-form input:focus { border-color: var(--blue); }
.login-error { color: var(--red); font-size: 13px; font-weight: 600; }
.login-hint { text-align: center; font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ===================== BUTTONS ===================== */
.btn-primary {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    background: var(--blue); color: #fff; border: none; border-radius: 10px;
    padding: 12px 16px; font-size: 14px; font-weight: 600; cursor: pointer;
    font-family: inherit; transition: filter .15s;
}
.btn-primary:hover { filter: brightness(1.07); }
.btn-primary.sm, .btn-ghost.sm { padding: 8px 12px; font-size: 13px; border-radius: 8px; }
.btn-ghost {
    display: inline-flex; align-items: center; gap: 8px;
    background: #eef1f6; color: #46506a; border: 1px solid var(--border);
    border-radius: 10px; padding: 12px 16px; font-weight: 600; cursor: pointer; font-family: inherit;
}
.btn-ghost:disabled { opacity: .5; cursor: not-allowed; }
.link-btn { background: none; border: none; color: var(--blue); font-weight: 600; cursor: pointer; font-size: 13px; font-family: inherit; }

/* ===================== SHELL ===================== */
.app-shell { display: flex; height: 100vh; overflow: hidden; }

/* Sidebar */
.sidebar {
    width: 232px; flex-shrink: 0; background: var(--navy);
    color: #cdd6e6; display: flex; flex-direction: column;
}
.sidebar-brand {
    display: flex; align-items: center; gap: 10px; padding: 20px 22px;
    font-size: 18px; font-weight: 800; color: #fff;
    border-bottom: 1px solid rgba(255,255,255,.06);
}
.sidebar-brand .material-icons-round { color: var(--blue); font-size: 26px; }
.side-nav { flex: 1; padding: 14px 12px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.nav-item {
    display: flex; align-items: center; gap: 12px;
    padding: 11px 14px; border-radius: 9px; border: none; background: none;
    color: #9fabc4; font-size: 14px; font-weight: 500; cursor: pointer;
    font-family: inherit; text-align: left; transition: .15s;
}
.nav-item:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav-item.active { background: var(--blue); color: #fff; }
.nav-item .material-icons-round { font-size: 20px; }
.sidebar-footer { padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.06); }
.backend-status { display: flex; align-items: center; gap: 8px; font-size: 12px; color: #9fabc4; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.dot-online { background: var(--green); box-shadow: 0 0 8px var(--green); }
.dot-offline { background: var(--muted); }
.dot-lite { background: var(--amber); box-shadow: 0 0 8px var(--amber); }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 16px 26px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.topbar-title h2 { font-size: 20px; font-weight: 700; }
.topbar-title p { font-size: 12px; color: var(--muted); }
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.search-box { display: flex; align-items: center; gap: 8px; background: var(--bg); border-radius: 10px; padding: 8px 12px; }
.search-box input { border: none; background: none; outline: none; font-family: inherit; font-size: 13px; width: 180px; }
.search-box .material-icons-round { color: var(--muted); font-size: 18px; }
.icon-btn { position: relative; background: none; border: none; color: #56617b; cursor: pointer; display: inline-flex; padding: 6px; border-radius: 8px; }
.icon-btn:hover { background: var(--bg); }
.badge { position: absolute; top: -2px; right: -2px; background: var(--red); color: #fff; font-size: 10px; font-weight: 700; border-radius: 10px; padding: 1px 5px; }
.user-chip { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; color: #46506a; }
.user-chip > .material-icons-round { font-size: 26px; color: var(--muted); }

.view-wrap { flex: 1; overflow-y: auto; padding: 22px 26px; }
.view { display: none; }
.view.active { display: block; }

/* ===================== PANELS / GRID ===================== */
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 18px; box-shadow: var(--shadow); }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.panel-head h3 { font-size: 15px; font-weight: 700; }
.panel-head h3 small { font-weight: 500; color: var(--muted); font-size: 11px; }

.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 18px; }
.stat-card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 18px; display: flex; align-items: center; gap: 14px; box-shadow: var(--shadow); }
.stat-icon { width: 48px; height: 48px; border-radius: 12px; display: grid; place-items: center; color: #fff; }
.stat-icon .material-icons-round { font-size: 24px; }
.si-blue { background: var(--blue); } .si-amber { background: var(--amber); } .si-green { background: var(--green); } .si-red { background: var(--red); }
.stat-value { font-size: 26px; font-weight: 800; letter-spacing: -.5px; }
.stat-label { font-size: 12px; color: var(--muted); }

.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; margin-bottom: 18px; }
.grid-3 { display: grid; grid-template-columns: 1.2fr 1fr 1fr; gap: 16px; }
.analytics-grid { grid-template-columns: 1fr 1fr; }

/* Chart.js needs a sized parent when maintainAspectRatio=false;
   otherwise charts collapse to ~0px (especially when the view was hidden). */
.chart-box {
    position: relative;
    width: 100%;
    height: 220px;
    min-height: 220px;
}
.chart-box.tall { height: 320px; min-height: 320px; }
.chart-box.donut { height: 300px; min-height: 300px; }
.chart-box canvas { width: 100% !important; height: 100% !important; display: block; }

.event-type-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 16px;
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}
.event-type-legend .etl-item {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
}
.event-type-legend .etl-swatch {
    width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0;
}
.event-type-legend .etl-count {
    color: var(--muted); font-weight: 500;
}

/* Camera grid */
.camera-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.cam-tile { position: relative; aspect-ratio: 16/10; background: #0c1522; border-radius: 10px; overflow: hidden; }
.cam-tile img, .cam-tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam-tile .mirror { position: absolute; inset: 0; z-index: 1; }
.cam-tile .cam-label, .cam-tile .cam-live { z-index: 2; }
.cam-tile .cam-placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: #33445f; }
.cam-tile .cam-placeholder .material-icons-round { font-size: 36px; }
.cam-label { position: absolute; left: 8px; bottom: 8px; background: rgba(0,0,0,.6); color: #fff; font-size: 11px; padding: 3px 8px; border-radius: 6px; }
.cam-live { position: absolute; right: 8px; top: 8px; background: var(--red); color: #fff; font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 6px; display: flex; align-items: center; gap: 4px; }
.cam-live::before { content: ""; width: 6px; height: 6px; background: #fff; border-radius: 50%; }

/* Alerts */
.alert-list { display: flex; flex-direction: column; gap: 10px; max-height: 320px; overflow-y: auto; }
.alert-item { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; background: var(--bg); }
.alert-icon { width: 36px; height: 36px; border-radius: 9px; display: grid; place-items: center; color: #fff; flex-shrink: 0; }
.alert-main { flex: 1; min-width: 0; }
.alert-title { font-size: 13px; font-weight: 700; }
.alert-meta { font-size: 11px; color: var(--muted); }
.alert-sev { font-size: 10px; font-weight: 700; padding: 3px 8px; border-radius: 6px; }
.sev-high { background: #fde8e8; color: var(--red); } .sev-med { background: #fef3d7; color: #b6790a; } .sev-low { background: #e6f6ee; color: var(--green); }

/* Locations */
.loc-list { display: flex; flex-direction: column; gap: 10px; }
.loc-item { display: flex; align-items: center; justify-content: space-between; font-size: 13px; padding: 8px 0; border-bottom: 1px dashed var(--border); }
.loc-item:last-child { border-bottom: none; }
.loc-bar { flex: 1; height: 6px; background: var(--bg); border-radius: 4px; margin: 0 12px; overflow: hidden; }
.loc-bar span { display: block; height: 100%; background: var(--blue); border-radius: 4px; }

.pill { font-size: 11px; font-weight: 700; padding: 4px 10px; border-radius: 20px; background: var(--bg); color: #56617b; }
.pill-red { background: #fde8e8; color: var(--red); }
.pill-green { background: #e6f6ee; color: var(--green); }
.pill-amber { background: #fef3d7; color: #b6790a; }

/* ===================== LIVE VIEW ===================== */
.grid-live { display: grid; grid-template-columns: 1fr 320px; gap: 16px; }
.live-controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.live-file-meta {
    display: flex; align-items: center; gap: 8px;
    margin-top: 10px; font-size: 13px; color: var(--muted);
}
.live-file-meta .material-icons-round { font-size: 18px; }
.live-file-meta[hidden] { display: none !important; }
#live-file-name { font-weight: 600; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 60%; }
.live-stage { position: relative; aspect-ratio: 16/9; background: #0c1522; border-radius: 12px; overflow: hidden; }
.live-stage video, .live-stage img, .live-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.live-stage video, .live-stage img { object-fit: contain; }
.live-stage canvas { z-index: 2; pointer-events: auto; }
#backend-stream { display: none; }
.person-tooltip { position: absolute; z-index: 5; max-width: 250px; padding: 8px 10px; font-size: 12px; line-height: 1.4; color: #0c1522; background: rgba(255,255,255,.97); border: 1px solid #2f6bff; border-left-width: 4px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.18); pointer-events: none; }
.live-placeholder { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; color: #4a5c7a; }
.live-placeholder .material-icons-round { font-size: 48px; }
.live-side { display: flex; flex-direction: column; gap: 16px; }

.threat-panel { text-align: center; }
.gauge { margin: 6px 0 12px; }
.gauge-value { font-size: 44px; font-weight: 800; line-height: 1; }
.gauge-label { font-size: 13px; font-weight: 700; letter-spacing: 1px; color: var(--green); }
.gauge-bar { height: 10px; background: var(--bg); border-radius: 6px; overflow: hidden; }
.gauge-bar-fill { height: 100%; width: 0%; background: var(--green); border-radius: 6px; transition: width .4s, background .4s; }

.det-list { display: flex; flex-direction: column; gap: 8px; max-height: 240px; overflow-y: auto; }
.det-item { display: flex; align-items: center; justify-content: space-between; font-size: 13px; padding: 8px 10px; border-radius: 8px; background: var(--bg); }
.det-item .conf { font-weight: 700; color: var(--blue); font-size: 12px; }
.det-item.danger { background: #fde8e8; }
.det-item.danger .conf { color: var(--red); }
.action-box { font-size: 15px; font-weight: 700; padding: 12px; border-radius: 8px; background: var(--bg); text-align: center; }
.action-box.danger { background: #fde8e8; color: var(--red); }
.assess-list { display: flex; flex-direction: column; gap: 8px; max-height: 260px; overflow-y: auto; }
.assess-item { padding: 8px 10px; border-radius: 8px; background: var(--bg); border-left: 4px solid #16b364; }
.assess-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 3px; }
.assess-level { font-weight: 800; font-size: 12px; letter-spacing: .03em; }
.assess-pct { font-weight: 700; font-size: 12px; color: var(--muted); }
.assess-text { font-size: 12.5px; line-height: 1.45; color: #26324a; }
.empty { color: var(--muted); font-size: 13px; text-align: center; padding: 8px; }

/* Tables / misc */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
.placeholder-panel { text-align: center; padding: 60px 20px; color: var(--muted); }
.placeholder-panel .material-icons-round { font-size: 54px; color: var(--border); }
.placeholder-panel h3 { margin: 12px 0 6px; color: var(--text); }
/* Reports */
.report-select { padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; font-family: inherit; font-size: 13px; background: #fff; color: var(--text); }
.report-meta { color: var(--muted); font-size: 12px; margin: 4px 0 14px; }
.report-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.report-card { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 16px; }
.report-card .rc-value { font-size: 26px; font-weight: 800; color: var(--text); }
.report-card .rc-label { font-size: 12px; color: var(--muted); margin-top: 2px; }
.report-card.rc-high { border-left: 3px solid #ef4444; }
.report-card.rc-med { border-left: 3px solid #f59e0b; }
.report-card.rc-low { border-left: 3px solid #16b364; }

.settings-form { display: flex; flex-direction: column; gap: 16px; max-width: 420px; }
.settings-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; }
.settings-form input[type=text] { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-family: inherit; }

@media (max-width: 1100px) {
    .grid-2, .grid-3, .grid-live { grid-template-columns: 1fr; }
    .chart-box.tall { height: 280px; }
    .stat-row, .report-cards { grid-template-columns: repeat(2, 1fr); }
    .sidebar { width: 68px; }
    .brand-text, .nav-item span:not(.material-icons-round), .backend-status span:last-child { display: none; }
}
