:root {
    --bg: #0a0a0c;
    --panel: #121216;
    --panel-2: #18181d;
    --line: #26262d;
    --text: #ececf1;
    --muted: #8d8d99;
    --accent: #e3263a;
    --accent-2: #ff4d5e;
    --accent-soft: rgba(227, 38, 58, .14);
    --ok: #2fbf71;
    --warn: #f0a531;
    --bad: #e3263a;
    --info: #4b9cf5;
    --radius: 10px;
    --font: 'Inter', 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { width: 100%; height: 100%; background: transparent; font-family: var(--font); color: var(--text); font-size: 13px; overflow: hidden; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
input, select, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 8px 10px; outline: none; width: 100%; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input[type=checkbox] { width: auto; accent-color: var(--accent); }
label { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 12px; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #2c2c34; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

#root { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.45); }

/* ── buttons ── */
.btn { padding: 8px 14px; border-radius: 7px; background: var(--panel-2); border: 1px solid var(--line); font-weight: 600; transition: .12s; white-space: nowrap; }
.btn:hover { border-color: #3a3a44; background: #1f1f26; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--accent-2); border-color: rgba(227,38,58,.4); }
.btn.danger:hover { background: var(--accent-soft); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { width: 32px; height: 32px; border-radius: 7px; font-size: 20px; line-height: 1; color: var(--muted); }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

/* ── brand ── */
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 40px; height: 40px; border-radius: 10px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; }
.brand-mark svg { width: 24px; height: 24px; }
.brand-top { font-size: 10px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); }
.brand-name { font-size: 20px; font-weight: 800; letter-spacing: .12em; }
.brand.small .brand-mark { width: 34px; height: 34px; }
.brand.small .brand-name { font-size: 16px; }

/* ── login ── */
.login-card { position: relative; width: 380px; background: var(--panel); border: 1px solid var(--line); border-top: 3px solid var(--accent); border-radius: var(--radius); padding: 28px; box-shadow: 0 30px 80px rgba(0,0,0,.6); }
.login-close { position: absolute; top: 10px; right: 10px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 24px; }
.login-foot { margin-top: 16px; font-size: 11px; color: var(--muted); text-align: center; }
.form-error { color: var(--accent-2); min-height: 16px; font-size: 12px; }

/* ── panel ── */
.panel { width: min(1500px, 94vw); height: min(900px, 90vh); display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; box-shadow: 0 30px 80px rgba(0,0,0,.6); }
.sidebar { width: 220px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 18px 12px; gap: 18px; }
#nav { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; flex: 1; }
.nav-group { font-size: 10px; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); padding: 12px 10px 4px; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 7px; color: var(--muted); font-weight: 600; text-align: left; }
.nav-item:hover { background: var(--panel-2); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--text); box-shadow: inset 3px 0 0 var(--accent); }
.nav-item .badge { margin-left: auto; }
.sidebar-foot { display: flex; flex-direction: column; gap: 8px; }
.user-chip { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 7px; background: var(--panel-2); border: 1px solid var(--line); text-align: left; }
.user-chip .avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--accent); display: grid; place-items: center; font-weight: 800; font-size: 12px; color: #fff; }
.user-chip small { display: block; color: var(--muted); font-size: 10px; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 16px 22px; border-bottom: 1px solid var(--line); }
.topbar h1 { font-size: 18px; }
.topbar .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.content { flex: 1; overflow-y: auto; padding: 20px 22px 30px; display: flex; flex-direction: column; gap: 16px; }

/* ── layout bits ── */
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g-wide { grid-template-columns: 2fr 1fr; }
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.kpi .k { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.kpi .v { font-size: 22px; font-weight: 800; margin-top: 6px; font-variant-numeric: tabular-nums; }
.kpi .s { color: var(--muted); font-size: 11px; margin-top: 3px; }
.kpi.alert { border-color: rgba(227,38,58,.5); }
.kpi.alert .v { color: var(--accent-2); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0; }
.card h3 { font-size: 13px; margin-bottom: 12px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card h3 .muted { font-weight: 500; }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.toolbar { display: flex; gap: 8px; align-items: center; }
.toolbar input, .toolbar select { width: auto; min-width: 200px; }

/* ── tables ── */
.table-wrap { overflow: auto; max-height: 520px; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); }
td { padding: 8px; border-bottom: 1px solid #1c1c22; vertical-align: middle; }
tr:hover td { background: #16161b; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td .actions { display: flex; gap: 4px; justify-content: flex-end; }
.empty { color: var(--muted); padding: 18px; text-align: center; }
.mono { font-family: Consolas, monospace; font-size: 12px; }
.trunc { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; background: var(--panel-2); border: 1px solid var(--line); white-space: nowrap; }
.badge.ok { color: var(--ok); border-color: rgba(47,191,113,.35); background: rgba(47,191,113,.08); }
.badge.warn { color: var(--warn); border-color: rgba(240,165,49,.35); background: rgba(240,165,49,.08); }
.badge.bad { color: var(--accent-2); border-color: rgba(227,38,58,.4); background: var(--accent-soft); }
.badge.info { color: var(--info); border-color: rgba(75,156,245,.35); background: rgba(75,156,245,.08); }

/* ── charts ── */
.chart { width: 100%; height: 200px; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .axis { stroke: var(--line); }
.chart text { fill: var(--muted); font-size: 10px; }
.legend { display: flex; gap: 14px; font-size: 11px; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.hbars { display: flex; flex-direction: column; gap: 7px; }
.hbar { display: grid; grid-template-columns: 150px 1fr 60px; gap: 10px; align-items: center; font-size: 12px; }
.hbar .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .track { height: 8px; background: var(--panel-2); border-radius: 4px; overflow: hidden; }
.hbar .fill { height: 100%; background: var(--accent); border-radius: 4px; }
.hbar .n { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.map { width: 100%; aspect-ratio: 0.75; max-height: 520px; background: #0d0d11; border: 1px solid var(--line); border-radius: 8px; display: block; }

/* ── feed ── */
.feed { display: flex; flex-direction: column; gap: 6px; max-height: 340px; overflow-y: auto; }
.feed-item { display: flex; gap: 10px; padding: 8px 10px; background: var(--panel-2); border-radius: 7px; border-left: 3px solid var(--line); }
.feed-item.bad { border-left-color: var(--accent); }
.feed-item.warn { border-left-color: var(--warn); }
.feed-item .t { color: var(--muted); font-size: 11px; white-space: nowrap; }
.feed-item .d { color: var(--muted); font-size: 11px; margin-top: 2px; word-break: break-all; }

/* ── rules editor ── */
.rules td input[type=number] { width: 80px; padding: 5px 7px; }
.rules td select { width: 90px; padding: 5px 7px; }
.desc { color: var(--muted); font-size: 11px; margin-top: 2px; max-width: 420px; }

/* ── AI ── */
.ai-head { display: flex; gap: 22px; align-items: center; }
.gauge { width: 120px; height: 120px; flex-shrink: 0; }
.gauge text { font-weight: 800; }
.ai-headline { font-size: 16px; font-weight: 700; }
.ai-summary { color: #c9c9d3; line-height: 1.55; margin-top: 6px; }
.finding { border: 1px solid var(--line); border-left: 3px solid var(--line); border-radius: 8px; padding: 12px; background: var(--panel-2); }
.finding.sev-critical, .finding.sev-high { border-left-color: var(--accent); }
.finding.sev-medium { border-left-color: var(--warn); }
.finding.sev-low { border-left-color: var(--info); }
.finding .title { font-weight: 700; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.finding p { color: #c9c9d3; margin-top: 6px; line-height: 1.5; }
.finding .rec { color: var(--muted); margin-top: 6px; }
.answer { white-space: pre-wrap; line-height: 1.55; color: #d4d4dc; background: var(--panel-2); border-radius: 8px; padding: 12px; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── modal / drawer / toasts ── */
.modal, .drawer { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 50; display: flex; }
.modal { align-items: center; justify-content: center; }
.modal-card { width: 440px; max-height: 86vh; overflow-y: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; display: flex; flex-direction: column; gap: 14px; }
.modal-card h2 { font-size: 16px; }
.drawer { justify-content: flex-end; }
.drawer-card { width: 620px; max-width: 92vw; height: 100%; background: var(--panel); border-left: 1px solid var(--line); padding: 22px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 6px 12px; font-size: 12px; }
.kv div:nth-child(odd) { color: var(--muted); }
.perm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.perm-grid label { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 12px; }
.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 60; width: 360px; }
.toast { background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--info); border-radius: 8px; padding: 10px 12px; box-shadow: 0 10px 30px rgba(0,0,0,.5); animation: slide .2s ease-out; }
.toast.bad { border-left-color: var(--accent); }
.toast.ok { border-left-color: var(--ok); }
.toast.warn { border-left-color: var(--warn); }
.toast b { display: block; margin-bottom: 2px; }
@keyframes slide { from { transform: translateX(20px); opacity: 0; } }

/* ── browser mode (served by the server, not in game) ── */
body.web, body.web #root { background: var(--bg); }
body.web { overflow: hidden; }
body.web [data-close] { display: none; }
body.web .panel { width: 100vw; height: 100vh; height: 100dvh; border: 0; border-radius: 0; box-shadow: none; }

/* ── narrow screens (phones, small tablets) ── */
@media (max-width: 860px) {
    .panel { flex-direction: column; }
    .sidebar { width: 100%; flex-direction: row; align-items: center; padding: 8px 12px; gap: 10px; border-right: 0; border-bottom: 1px solid var(--line); }
    .sidebar .brand { display: none; }
    #nav { flex-direction: row; overflow-x: auto; overflow-y: hidden; flex: 1; gap: 4px; scrollbar-width: none; }
    #nav::-webkit-scrollbar { display: none; }
    .nav-group { display: none; }
    .nav-item { white-space: nowrap; padding: 8px 10px; }
    .nav-item.active { box-shadow: inset 0 -2px 0 var(--accent); }
    .sidebar-foot { flex-direction: row; flex-shrink: 0; }
    .user-chip > div:last-child { display: none; }
    .sidebar-foot .btn.wide { width: auto; }
    .topbar { padding: 12px 14px; }
    .topbar .sub { display: none; }
    .content { padding: 14px 14px 24px; gap: 12px; }
    .g2, .g3, .g-wide { grid-template-columns: minmax(0, 1fr); }
    .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
    .kpi { padding: 10px; }
    .kpi .v { font-size: 18px; }
    .toolbar, .card h3 { flex-wrap: wrap; }
    .toolbar input, .toolbar select { min-width: 0; width: 100%; }
    .hbar { grid-template-columns: 100px minmax(0, 1fr) 70px; }
    .trunc { max-width: 160px; }
    .modal-card { width: 94vw; }
    .drawer-card { width: 100vw; max-width: 100vw; }
    .login-card { width: 92vw; padding: 22px; }
    .ai-head { flex-direction: column; align-items: flex-start; }
    .toasts { left: 12px; right: 12px; width: auto; }
    .kv { grid-template-columns: 100px minmax(0, 1fr); word-break: break-all; }
}
