:root {
    --bg: #f5f6f4;
    --surface: #ffffff;
    --surface-2: #f0f2ef;
    --text: #1d2420;
    --muted: #66706a;
    --border: #e1e5e0;
    --primary: #128c7e;
    --primary-ink: #ffffff;
    --primary-soft: #e3f3f0;
    --danger: #c63b30;
    --danger-soft: #fbe9e7;
    --warn: #b7791f;
    --warn-soft: #fdf3e1;
    --ok: #1f8a4c;
    --ok-soft: #e4f4ea;
    --info-soft: #e8eef9;
    --shadow: 0 1px 2px rgba(20, 30, 25, .06), 0 4px 16px rgba(20, 30, 25, .05);
    --radius: 12px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111513;
        --surface: #1a201d;
        --surface-2: #222a26;
        --text: #e7ece9;
        --muted: #97a29c;
        --border: #2d3631;
        --primary: #25b8a4;
        --primary-ink: #0b1210;
        --primary-soft: #16302b;
        --danger: #ef6b5f;
        --danger-soft: #3a1c19;
        --warn: #e5a54a;
        --warn-soft: #3a2c15;
        --ok: #4cc283;
        --ok-soft: #173123;
        --info-soft: #1c2638;
        --shadow: none;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--primary); text-decoration: none; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar {
    background: var(--surface); border-right: 1px solid var(--border);
    display: flex; flex-direction: column; padding: 18px 12px; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 0 8px 18px; }
.brand.big { font-size: 26px; justify-content: center; padding-bottom: 4px; }
.logo {
    display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 9px;
    background: var(--primary); color: var(--primary-ink); font-weight: 800;
}
nav { display: flex; flex-direction: column; gap: 2px; }
nav a { color: var(--text); padding: 9px 12px; border-radius: 8px; }
nav a:hover { background: var(--surface-2); }
nav a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding: 8px; color: var(--muted); font-size: 13px; }
.view { padding: 24px 28px 60px; max-width: 1240px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

@media (max-width: 860px) {
    .shell { grid-template-columns: minmax(0, 1fr); }
    .sidebar, .view { min-width: 0; }
    .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 16px; }
    .brand { padding: 0 12px 0 0; }
    nav { flex-direction: row; overflow-x: auto; gap: 4px; width: 100%; order: 3; padding-top: 8px; }
    nav a { white-space: nowrap; font-size: 14px; }
    .sidebar-footer { margin: 0 0 0 auto; flex-direction: row; align-items: center; padding: 0; }
    .view { padding: 16px; }
}

/* ---------- cards & grids ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); min-width: 0; }
.grid { display: grid; gap: 16px; }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); margin-bottom: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.grid.three { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
@media (max-width: 420px) { .grid.two, .grid.three { grid-template-columns: 1fr; } }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12.5px; color: var(--muted); }
.kpi.alert .value { color: var(--danger); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; font-weight: 500; overflow-wrap: anywhere; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12.5px; text-transform: uppercase; letter-spacing: .03em; }
th, td { padding: 10px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td.wrap { white-space: normal; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); padding: 18px 4px; text-align: center; }

/* ---------- badges ---------- */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--info-soft); color: var(--primary); }

/* ---------- forms & buttons ---------- */
.btn {
    border: 1px solid var(--border); background: var(--surface); color: var(--text);
    padding: 8px 14px; border-radius: 9px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); border-color: var(--danger-soft); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); font-weight: 600; }
input, select, textarea {
    font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
    border-radius: 9px; padding: 9px 11px; width: 100%; min-width: 0;
}
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.form { display: grid; gap: 12px; }
.form.cols { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; color: var(--text); }
.check input { width: auto; }
.checks { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 6px 14px; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.tab { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 600; font-size: 13px; color: var(--muted); }
.tab.active { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.search { max-width: 280px; }

/* ---------- quotas ---------- */
.meter { display: grid; gap: 4px; margin-bottom: 10px; }
.meter .bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.meter .fill { height: 100%; background: var(--primary); border-radius: 99px; }
.meter .fill.hot { background: var(--danger); }
.meter .top { display: flex; justify-content: space-between; font-size: 13px; }

/* ---------- chart ---------- */
.bars { display: flex; align-items: flex-end; gap: 10px; height: 150px; padding-top: 10px; }
.bars .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; min-width: 0; }
.bars .b { width: 100%; max-width: 46px; background: var(--primary); border-radius: 6px 6px 2px 2px; min-height: 2px; }
.bars .cap { font-size: 11.5px; color: var(--muted); }
.bars .v { font-size: 11.5px; font-variant-numeric: tabular-nums; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 28px; width: 100%; max-width: 360px; display: grid; gap: 14px; box-shadow: var(--shadow); }
.login-card p { margin: 0; text-align: center; }
.error { color: var(--danger); min-height: 1em; font-size: 13.5px; }

/* ---------- modal & toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(10, 15, 12, .45); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal-card { background: var(--surface); border-radius: 16px; padding: 22px; width: 100%; max-width: 620px; max-height: 90vh; overflow: auto; position: relative; border: 1px solid var(--border); }
.modal-close { position: absolute; top: 10px; right: 12px; border: 0; background: none; font-size: 24px; cursor: pointer; color: var(--muted); }
.modal-card h2 { padding-right: 30px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; z-index: 30; font-weight: 600; max-width: calc(100vw - 32px); }
.toast.error { background: var(--danger); color: #fff; }
.qr { display: grid; place-items: center; gap: 10px; padding: 10px; }
.qr img { width: 260px; height: 260px; max-width: 100%; background: #fff; padding: 8px; border-radius: 10px; }
.code { font-size: 28px; letter-spacing: .2em; font-weight: 800; font-family: ui-monospace, monospace; }
pre.json { background: var(--surface-2); padding: 12px; border-radius: 10px; overflow: auto; max-height: 360px; font-size: 12.5px; margin: 0; }
.chat { display: flex; flex-direction: column; gap: 6px; max-height: 360px; overflow: auto; }
.bubble { padding: 7px 11px; border-radius: 10px; max-width: 85%; white-space: pre-wrap; font-size: 13.5px; overflow-wrap: anywhere; }
.bubble.in { background: var(--surface-2); align-self: flex-start; }
.bubble.out { background: var(--primary-soft); align-self: flex-end; }
.bubble .meta { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; }
