:root {
  --bg: #f4f6f9; --surface: #fff; --surface-2: #f8fafc; --border: #e3e8ef; --text: #101828; --muted: #667085;
  --accent: #c99a06; --primary: #2563eb; --primary-ink: #fff;
  --pos: #0f9d58; --neg: #d92d20; --warn: #b54708; --warn-bg: #fef0c7; --pos-bg: #dcfae6; --neg-bg: #fee4e2; --info-bg: #e0eaff; --info: #3538cd;
  --row-hover: #f1f5fb; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) {
  --bg: #0b0f14; --surface: #121821; --surface-2: #0f141c; --border: #222c38; --text: #e6edf5; --muted: #8b98a9;
  --accent: #e8b923; --primary: #3b82f6; --pos: #34d27b; --neg: #ff6b5e; --warn: #fdb022; --warn-bg: #3a2a07; --pos-bg: #0d2e1d; --neg-bg: #3a1411; --info-bg: #1b2350; --info: #a4bcfd;
  --row-hover: #18202b; --shadow: none; color-scheme: dark;
} }
:root[data-theme="dark"] {
  --bg: #0b0f14; --surface: #121821; --surface-2: #0f141c; --border: #222c38; --text: #e6edf5; --muted: #8b98a9;
  --accent: #e8b923; --primary: #3b82f6; --pos: #34d27b; --neg: #ff6b5e; --warn: #fdb022; --warn-bg: #3a2a07; --pos-bg: #0d2e1d; --neg-bg: #3a1411; --info-bg: #1b2350; --info: #a4bcfd;
  --row-hover: #18202b; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
a { color: var(--primary); }
h1, h2, h3 { margin: 0 0 .6rem; line-height: 1.2; }
h1 { font-size: 1.45rem; } h2 { font-size: 1.05rem; } h3 { font-size: .95rem; }
.muted { color: var(--muted); } .small { font-size: 12.5px; } .num { font-variant-numeric: tabular-nums; text-align: right; }
.pos { color: var(--pos); } .neg { color: var(--neg); }
code, .mono { font-family: ui-monospace, Consolas, monospace; }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; font-size: 12.5px; }

/* buttons & inputs */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .35rem; height: 34px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; text-decoration: none; }
.btn:hover { filter: brightness(.97); } .btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.success { background: var(--pos-bg); border-color: transparent; color: var(--pos); }
.btn.warn { background: var(--warn-bg); border-color: transparent; color: var(--warn); }
.btn.danger { background: var(--neg-bg); border-color: transparent; color: var(--neg); }
.btn.ghost { background: transparent; }
.btn.sm { height: 28px; padding: 0 10px; font-size: 12.5px; }
.btn.wide { width: 100%; height: 40px; }
.icon-btn { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: transparent; color: var(--text); cursor: pointer; font-size: 15px; display: inline-grid; place-items: center; }
.icon-btn:hover { background: var(--row-hover); }
input, select, textarea { width: 100%; height: 36px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; }
textarea { height: auto; padding: 8px 10px; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 40%, transparent); border-color: var(--primary); }
input[type=checkbox] { width: 16px; height: 16px; margin: 0; accent-color: var(--primary); }
label { display: block; margin-bottom: 12px; font-weight: 600; font-size: 13px; }
label > input, label > select, label > textarea { margin-top: 5px; font-weight: 400; }
label small, .hint { display: block; font-weight: 400; color: var(--muted); font-size: 12px; margin-top: 3px; }
label.check { display: flex; gap: 8px; align-items: flex-start; font-weight: 500; }
label.check input { margin-top: 2px; flex: none; }
.form-error { color: var(--neg); font-size: 13px; white-space: pre-line; margin-bottom: 8px; }
.form-error:empty { display: none; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; } .row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; } .row4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: radial-gradient(1200px 500px at 50% -10%, color-mix(in srgb, var(--accent) 18%, transparent), transparent), var(--bg); }
.auth-card { width: 100%; max-width: 390px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.auth-card h1 { margin-top: 22px; }

/* brand + topbar */
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: none; }
.brand b { display: block; font-size: 15px; white-space: nowrap; } .brand small { display: block; color: var(--muted); font-size: 11px; letter-spacing: .05em; text-transform: uppercase; }
.logo { width: 34px; height: 34px; border-radius: 9px; flex: none; background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M8 22l5-6 4 3 7-9' stroke='%23111' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/100% no-repeat; }
.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 18px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border); }
.nav { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.nav a { color: var(--muted); font-weight: 600; padding: 8px 12px; border-radius: 8px; text-decoration: none; white-space: nowrap; }
.nav a:hover { color: var(--text); background: var(--row-hover); }
.nav a.active { color: var(--text); background: var(--surface-2); box-shadow: inset 0 -2px 0 var(--accent); }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.pill i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.ok i { background: var(--pos); box-shadow: 0 0 0 3px var(--pos-bg); } .pill.bad i { background: var(--neg); }
.role-tag { font-size: 11px; font-weight: 700; text-transform: uppercase; padding: 2px 7px; border-radius: 6px; background: var(--info-bg); color: var(--info); }
main { padding: 18px 20px 48px; max-width: 1640px; margin: 0 auto; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 14px; }
.page-head h1 { margin: 0; } .page-head .grow { flex: 1; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; box-shadow: var(--shadow); min-width: 0; }
.kpi span { display: block; color: var(--muted); font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.kpi b { display: block; font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi em { font-style: normal; font-size: 12px; color: var(--muted); }
.kpi.hl { border-color: color-mix(in srgb, var(--accent) 60%, var(--border)); }

/* dashboard: client tiles (connected / not connected) */
.ctiles-card { padding: 14px 16px; }
.ctiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; max-height: 296px; overflow: auto; }
.ctile { display: flex; flex-direction: column; gap: 6px; text-align: left; font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-left: 4px solid var(--muted); border-radius: 10px; padding: 10px 12px; cursor: pointer; min-width: 0; }
.ctile:hover { background: var(--row-hover); }
.ctile.on { border-left-color: var(--pos); } .ctile.part { border-left-color: var(--warn); } .ctile.off { border-left-color: var(--neg); } .ctile.none { opacity: .75; }
.ctile .ct-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.ctile .ct-top b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ctile .ct-sub { font-size: 12px; color: var(--muted); }
.ctile.off .ct-sub { color: var(--neg); } .ctile.part .ct-sub { color: var(--warn); }
.ctile .ct-pl { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--muted); }
.ctile .ct-pl b { font-variant-numeric: tabular-nums; color: var(--text); } .ctile .ct-pl b.pos { color: var(--pos); } .ctile .ct-pl b.neg { color: var(--neg); }

/* toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .search { flex: 0 1 260px; }
.toolbar .end { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button { border: 1px solid var(--border); background: var(--surface); color: var(--muted); border-radius: 999px; padding: 5px 11px; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; }
.chips button b { font-weight: 600; opacity: .75; margin-left: 2px; }
.chips button.active { background: var(--text); color: var(--bg); border-color: var(--text); }

/* tables */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card-head { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.card-head h2 { margin: 0; }
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: auto; box-shadow: var(--shadow); }
table.grid { width: 100%; border-collapse: collapse; }
.grid th { position: sticky; top: 0; background: var(--surface-2); color: var(--muted); font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; text-align: left; padding: 10px; border-bottom: 1px solid var(--border); white-space: nowrap; user-select: none; }
.grid th[data-s] { cursor: pointer; }
.grid th.sorted::after { content: " ▾"; } .grid th.sorted.asc::after { content: " ▴"; }
.grid th.num { text-align: right; }
.grid td { padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; vertical-align: middle; }
.grid tbody tr { cursor: default; } .grid tbody tr.click { cursor: pointer; }
.grid tbody tr:hover { background: var(--row-hover); }
.grid tbody tr.off td.dim { color: var(--muted); }
.grid tfoot td { font-weight: 700; background: var(--surface-2); border-bottom: 0; }
.grid td b { font-weight: 600; } .grid td small { display: block; color: var(--muted); font-size: 12px; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }

.dot { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; font-size: 12.5px; }
.dot i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.on { color: var(--pos); } .dot.on i { background: var(--pos); box-shadow: 0 0 0 3px var(--pos-bg); } .dot.off { color: var(--muted); }
.badge { display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 11px; font-weight: 700; letter-spacing: .02em; white-space: nowrap; }
.badge.PLAY { background: var(--pos-bg); color: var(--pos); } .badge.PAUSE { background: var(--warn-bg); color: var(--warn); }
.badge.STOP, .badge.EXPIRED, .badge.DENIED { background: var(--neg-bg); color: var(--neg); }
.badge.soon { background: var(--warn-bg); color: var(--warn); } .badge.over { background: var(--neg-bg); color: var(--neg); } .badge.gray { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.badge.info { background: var(--info-bg); color: var(--info); }

/* play / pause / stop segmented control */
.ctl { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.ctl button { border: 0; background: none; color: var(--muted); font: inherit; font-size: 12px; font-weight: 700; padding: 4px 9px; cursor: pointer; display: inline-flex; align-items: center; gap: 4px; }
.ctl button + button { border-left: 1px solid var(--border); }
.ctl button:hover:not(:disabled) { background: var(--row-hover); color: var(--text); }
.ctl button.sel.play { background: var(--pos-bg); color: var(--pos); } .ctl button.sel.pause { background: var(--warn-bg); color: var(--warn); } .ctl button.sel.stop { background: var(--neg-bg); color: var(--neg); }
.ctl button:disabled { cursor: default; }
.ctl.lg button { padding: 7px 14px; font-size: 13px; }

.ddbar { width: 70px; height: 6px; border-radius: 3px; background: var(--surface-2); border: 1px solid var(--border); overflow: hidden; display: inline-block; vertical-align: middle; margin-right: 6px; }
.ddbar i { display: block; height: 100%; background: var(--pos); }
.ddbar.mid i { background: var(--warn); } .ddbar.hi i { background: var(--neg); }

.banner { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 10px 14px; border-radius: 10px; margin-bottom: 12px; font-weight: 500; }
.banner.warn { background: var(--warn-bg); color: var(--warn); } .banner.error { background: var(--neg-bg); color: var(--neg); } .banner.info { background: var(--info-bg); color: var(--info); }
.banner .grow { flex: 1; }

/* client account cards */
.acc-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 14px; }
.acc-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px; }
.acc-card.tripped { border-color: color-mix(in srgb, var(--neg) 55%, var(--border)); }
.acc-card .top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.acc-card .acc-no { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.acc-card .stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px 12px; }
.acc-card .stats div span { display: block; font-size: 11px; color: var(--muted); font-weight: 700; text-transform: uppercase; }
.acc-card .stats div b { font-variant-numeric: tabular-nums; font-size: 15px; }
.acc-card .foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; border-top: 1px solid var(--border); padding-top: 10px; }
.why { font-size: 12.5px; color: var(--muted); }

/* P&L chart */
.chart { width: 100%; height: 220px; display: block; }
.chart .bar.pos { fill: var(--pos); } .chart .bar.neg { fill: var(--neg); } .chart .axis { stroke: var(--border); } .chart text { fill: var(--muted); font-size: 11px; }
.chart .bar:hover { opacity: .8; }
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.cal div { border-radius: 6px; padding: 4px; min-height: 42px; font-size: 11px; background: var(--surface-2); border: 1px solid var(--border); }
.cal div b { display: block; font-size: 12px; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 520px) { .cal div b { font-size: 9.5px; } .cal div { padding: 3px 2px; } }
.cal div.p { background: var(--pos-bg); border-color: transparent; } .cal div.n { background: var(--neg-bg); border-color: transparent; }
.cal .hd { background: none; border: 0; min-height: 0; text-align: center; color: var(--muted); font-weight: 700; }

/* log */
.log { list-style: none; margin: 0; padding: 0; }
.log li { display: grid; grid-template-columns: 150px 90px 1fr; gap: 10px; padding: 7px 4px; border-bottom: 1px solid var(--border); font-size: 13px; }
.log time { color: var(--muted); font-variant-numeric: tabular-nums; }
.tag { display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 11px; font-weight: 700; text-transform: uppercase; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); text-align: center; }
.tag.online { background: var(--pos-bg); color: var(--pos); border-color: transparent; }
.tag.offline, .tag.rejected, .tag.risk { background: var(--neg-bg); color: var(--neg); border-color: transparent; }
.tag.control { background: var(--warn-bg); color: var(--warn); border-color: transparent; }

/* drawer + dialogs */
dialog { border: 1px solid var(--border); border-radius: 14px; padding: 20px; width: min(520px, calc(100vw - 32px)); background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgba(0,0,0,.28); }
dialog::backdrop { background: rgba(10,14,20,.55); }
dialog.drawer { margin: 0 0 0 auto; height: 100vh; max-height: 100vh; width: min(640px, 100vw); max-width: 100vw; overflow-x: hidden; border-radius: 0; border-width: 0 0 0 1px; padding: 0; }
dialog.drawer .d-head { position: sticky; top: 0; z-index: 2; background: var(--surface); border-bottom: 1px solid var(--border); padding: 14px 20px; display: flex; gap: 10px; align-items: center; }
dialog.drawer .d-head h2 { margin: 0; flex: 1; }
dialog.drawer .d-body { padding: 18px 20px 40px; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.dlg-head h2 { margin: 0; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.section { border-top: 1px solid var(--border); padding-top: 16px; margin-top: 16px; }
.section > h3 { display: flex; align-items: center; gap: 8px; }
.kv { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 14px; }
.kv div span { display: block; font-size: 11px; color: var(--muted); font-weight: 700; text-transform: uppercase; }
.kv div b { font-variant-numeric: tabular-nums; font-weight: 600; word-break: break-word; }
.keybox { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); border: 1px dashed var(--border); font-family: ui-monospace, Consolas, monospace; font-size: 16px; font-weight: 700; letter-spacing: .04em; }
.keybox span { flex: 1; word-break: break-all; }
.perm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; margin-bottom: 12px; }
.pick-list { max-height: 220px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; }
.pick-list label { margin: 0; padding: 5px 0; }
.steps { padding-left: 20px; margin: 0; } .steps li { margin-bottom: 10px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600; transition: .2s; z-index: 100; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%); } .toast.err { background: var(--neg); color: #fff; }

@media (max-width: 900px) {
  .topbar { flex-wrap: wrap; gap: 8px 10px; padding: 10px 16px; }
  .nav { order: 3; width: 100%; }
  .hide-sm { display: none !important; }
  main { padding: 14px 16px 36px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi b { font-size: 18px; }
  .toolbar .search { flex: 1 1 100%; } .toolbar .end { margin-left: 0; width: 100%; } .toolbar .end .btn { flex: 1; padding: 0 8px; }
  .row3, .row4 { grid-template-columns: 1fr 1fr; }
  .row2 { grid-template-columns: minmax(0, 1fr); } .perm-grid { grid-template-columns: 1fr; } .kv { grid-template-columns: 1fr 1fr; }
  .acc-cards { grid-template-columns: 1fr; }
  .log li { grid-template-columns: 1fr auto; } .log li .msg { grid-column: 1 / -1; }
  /* tables become cards */
  .table-wrap.cards { background: none; border: 0; box-shadow: none; overflow: visible; }
  .cards .grid thead { display: none; }
  .cards .grid, .cards .grid tbody, .cards .grid tfoot { display: block; }
  .cards .grid tr { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px; margin-bottom: 10px; }
  .cards .grid td { display: block; padding: 0; border: 0; white-space: normal; text-align: left; min-width: 0; }
  .cards .grid td[data-l]::before { content: attr(data-l); display: block; font-size: 10.5px; color: var(--muted); font-weight: 700; text-transform: uppercase; }
  .cards .grid td.wide { grid-column: 1 / -1; }
  .cards .grid td.c-main { grid-column: 1 / 3; } .cards .grid td.c-dot { grid-column: 3; grid-row: 1; text-align: right; }
  .cards .grid td:empty { display: none; }
}
