:root {
  --bg: #0f172a; --panel: #111c33; --card: #172343; --line: #24345c; --text: #e5e9f2; --muted: #8b9bb8;
  --accent: #38bdf8; --good: #22c55e; --bad: #ef4444; --warn: #f59e0b;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); font: 14px/1.4 var(--font); }
button, select, input { font: inherit; }
.mono { font-family: var(--mono); font-size: 12px; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

.topbar { height: 52px; display: flex; align-items: center; gap: 18px; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; font-size: 17px; letter-spacing: .3px; white-space: nowrap; }
.brand .logo { color: var(--accent); }
.brand .sub { color: var(--muted); font-weight: 400; font-size: 13px; margin-left: 6px; }
.rigbox { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.rigbox select { background: var(--card); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; }
.actions { display: flex; align-items: center; gap: 10px; }

.pill { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; letter-spacing: .4px; background: #334155; color: #cbd5e1; }
.pill-on { background: #14532d; color: #86efac; }
.pill-off { background: #4c1d1d; color: #fca5a5; }
.state-STOPPED { background: #334155; color: #cbd5e1; }
.state-IDLE, .state-WAIT_REDIAL { background: #1e3a8a; color: #bfdbfe; }
.state-DIALING, .state-RINGING { background: #713f12; color: #fde68a; }
.state-ACTIVE { background: #14532d; color: #86efac; }
.state-ENDING, .state-ENDED { background: #7c2d12; color: #fed7aa; }

.btn { border: 1px solid var(--line); background: var(--card); color: var(--text); padding: 7px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; }
.btn:hover:not(:disabled) { filter: brightness(1.15); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-start { background: #166534; border-color: #16a34a; }
.btn-stop { background: #7f1d1d; border-color: #dc2626; }
.btn-ghost { background: transparent; }
.toggle { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 13px; user-select: none; }

.layout { display: grid; grid-template-columns: 1fr 380px; height: calc(100% - 52px); }
#map { height: 100%; background: #0b1220; }
.sidebar { overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; background: var(--panel); border-left: 1px solid var(--line); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.card h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); font-weight: 700; }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 3px 10px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-family: var(--mono); font-size: 12.5px; word-break: break-word; }
.kv dd.good { color: #86efac; } .kv dd.bad { color: #fca5a5; } .kv dd.warn { color: #fde68a; }
select.wide { width: 100%; margin-bottom: 8px; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; }

.score-card { padding: 12px; }
.score-row { display: flex; align-items: center; gap: 14px; }
.score-num { font-size: 44px; font-weight: 800; line-height: 1; min-width: 80px; font-variant-numeric: tabular-nums; }
.score-class { font-weight: 700; font-size: 15px; text-transform: capitalize; }
.score-reasons { font-size: 12px; }
.score-bar { height: 6px; background: #0b1220; border-radius: 3px; margin-top: 10px; overflow: hidden; }
.score-fill { height: 100%; width: 0; background: var(--muted); transition: width .4s, background .4s; }

.events { list-style: none; margin: 0; padding: 0; max-height: 320px; overflow-y: auto; font-size: 12.5px; }
.events li { display: grid; grid-template-columns: 62px 18px 1fr; gap: 6px; padding: 4px 2px; border-bottom: 1px solid var(--line); cursor: pointer; }
.events li:hover { background: #1c2a4d; }
.events .t { color: var(--muted); font-family: var(--mono); }
.events .ic { text-align: center; }
.events li.alert { background: #3b0d0d; }
.events li.alert .ic { color: #fca5a5; font-weight: 800; }
.events .d { color: var(--muted); }

.legend { position: absolute; left: 12px; bottom: 12px; z-index: 500; background: rgba(15,23,42,.9); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 12px; display: flex; gap: 6px 10px; align-items: center; flex-wrap: wrap; max-width: calc(100% - 420px); }
.legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 2px; }
.legend .alert-sw { background: #ef4444; color: #fff; font-weight: 900; font-size: 10px; line-height: 12px; text-align: center; border-radius: 3px; }

.alert-icon { width: 22px; height: 22px; background: #ef4444; color: #fff; font-weight: 900; font-size: 15px; line-height: 20px; text-align: center; border: 2px solid #fff; border-radius: 6px; box-shadow: 0 2px 6px rgba(0,0,0,.6); transform: rotate(45deg); }
.alert-icon span { display: block; transform: rotate(-45deg); }
.here-icon { width: 18px; height: 18px; border-radius: 50%; background: #38bdf8; border: 3px solid #fff; box-shadow: 0 0 0 4px rgba(56,189,248,.35), 0 2px 6px rgba(0,0,0,.6); }
.here-icon .arrow { position: absolute; left: 50%; top: -12px; width: 0; height: 0; margin-left: -5px; border-left: 5px solid transparent; border-right: 5px solid transparent; border-bottom: 10px solid #38bdf8; }

.leaflet-container { font: 12px var(--font); }
.leaflet-control-attribution { background: rgba(15,23,42,.8) !important; color: var(--muted) !important; }
.leaflet-control-attribution a { color: var(--accent) !important; }
.leaflet-bar a { background: var(--card) !important; color: var(--text) !important; border-color: var(--line) !important; }

.modal { position: fixed; inset: 0; background: rgba(2,6,23,.7); display: flex; align-items: center; justify-content: center; z-index: 1000; }
.modal-box { width: min(880px, 94vw); max-height: 90vh; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,.6); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { overflow-y: auto; padding: 12px 16px 18px; }
.modal-body h4 { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); }
.modal-body h4:first-child { margin-top: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 24px; }
table.cells { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 12px; }
table.cells th, table.cells td { text-align: left; padding: 3px 6px; border-bottom: 1px solid var(--line); }
table.cells th { color: var(--muted); font-weight: 600; }
details summary { cursor: pointer; color: var(--accent); margin-top: 10px; }
pre.raw { background: #0b1220; padding: 10px; border-radius: 8px; font-size: 11.5px; overflow-x: auto; max-height: 320px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 6px; font-weight: 700; color: #fff; }

.good { color: #86efac; } .bad { color: #fca5a5; } .warn { color: #fde68a; font-size: 12px; }
a.btn { text-decoration: none; display: inline-block; }

/* account menu */
.me { position: relative; }
.me-btn { padding: 5px 10px; }
.me-drop { position: absolute; right: 0; top: 36px; min-width: 180px; display: flex; flex-direction: column; padding: 4px 0; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 10px 30px rgba(0,0,0,.5); z-index: 900; }
.me-drop a, .me-drop span { padding: 7px 14px; color: var(--text); text-decoration: none; white-space: nowrap; }
.me-drop a:hover { background: #1c2a4d; }

/* forms */
.form label { display: block; margin-bottom: 10px; color: var(--muted); font-size: 13px; }
.form input, .form select { display: block; width: 100%; margin-top: 4px; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; }
.form-row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.form-row label { margin: 0; min-width: 200px; }
.form-actions { margin-top: 8px; display: flex; gap: 8px; }
.form p { margin: 0 0 12px; }
input.inline, select.inline { display: block; width: 100%; max-width: 230px; margin: 2px 0; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; }

/* login */
.login-page { display: flex; align-items: center; justify-content: center; }
.login { width: min(380px, 92vw); }
.login-card { padding: 22px; }
.login-card .brand { margin-bottom: 18px; }
.login-msg { margin-top: 12px; }

/* two-factor */
.totp-setup { display: flex; gap: 20px; align-items: flex-start; flex-wrap: wrap; }
.totp-setup img { width: 220px; height: 220px; border-radius: 8px; background: #fff; }
code.secret { display: block; margin: 6px 0 14px; word-break: break-all; font-family: var(--mono); }

/* admin */
.tabs { display: flex; gap: 4px; flex: 1; }
.tab { background: transparent; border: 0; color: var(--muted); padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; }
.tab.active { background: var(--card); color: var(--text); }
.admin { padding: 16px; display: flex; flex-direction: column; gap: 12px; max-width: 1300px; margin: 0 auto; overflow-y: auto; height: calc(100% - 52px); }
.admin .card h3 { display: flex; justify-content: space-between; align-items: center; }
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { text-align: left; padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .6px; }
.tbl tr.disabled td { opacity: .55; }
.tbl .rigs { display: flex; flex-wrap: wrap; gap: 4px 12px; }
.row-actions { white-space: nowrap; }
.row-actions .btn { padding: 5px 10px; margin-bottom: 4px; }
.tbl td .toggle { display: inline-flex; margin: 4px 0; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1e293b; border: 1px solid var(--line); color: var(--text); padding: 10px 16px; border-radius: 8px; z-index: 1100; box-shadow: 0 8px 30px rgba(0,0,0,.5); }
.toast.err { border-color: var(--bad); }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 55vh 1fr; }
  .sidebar { border-left: 0; border-top: 1px solid var(--line); }
  .legend { max-width: calc(100% - 24px); bottom: auto; top: 64px; }
  .brand .sub, #sessionLabel { display: none; }
}
