:root {
  color-scheme: dark;
  --bg: #070b14;
  --surface: #0e1526;
  --surface-2: #141e35;
  --border: #1e2d4a;
  --text: #e6edf7;
  --muted: #8fa3c4;
  --accent: #00e5a0;
  --accent-dim: rgba(0, 229, 160, 0.14);
  --cyan: #38bdf8;
  --cyan-dim: rgba(56, 189, 248, 0.14);
  --amber: #f59e0b;
  --amber-dim: rgba(245, 158, 11, 0.15);
  --danger: #ef4444;
  --danger-dim: rgba(239, 68, 68, 0.16);
  --radius: 12px;
  --scrollbar-thumb: #2b3f66;
  --scrollbar-track: #0b1120;
  accent-color: var(--accent);
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

@supports not (scrollbar-color: auto) {
  *::-webkit-scrollbar { width: 8px; height: 8px; }
  *::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }
  *::-webkit-scrollbar-track { background: var(--scrollbar-track); }
}

*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 14px; line-height: 1.5;
}
[hidden] { display: none !important; }
.mono, .mono-sm, pre, code, .code-input { font-family: 'JetBrains Mono', ui-monospace, monospace; }
.mono-sm { font-size: 12px; }
.muted { color: var(--muted); }
.error-text { color: #fca5a5; min-height: 1.25em; margin: 6px 0; font-size: 13px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  padding: 9px 14px; border-radius: 8px; font: inherit; font-weight: 500;
  cursor: pointer; text-decoration: none; transition: background .15s, border-color .15s, transform .1s;
}
.btn:hover:not(:disabled) { border-color: #365080; background: #1a2744; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-sm { padding: 6px 11px; font-size: 12.5px; }
.btn-full { width: 100%; }
.btn-icon { width: 36px; height: 36px; padding: 0; flex-shrink: 0; font-size: 16px; }
.btn-primary { background: var(--accent); color: #041510; border-color: var(--accent); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: #1ef3b4; border-color: #1ef3b4; }
.btn-cyan { background: var(--cyan-dim); color: var(--cyan); border-color: rgba(56, 189, 248, 0.35); }
.btn-cyan:hover:not(:disabled) { background: rgba(56, 189, 248, 0.24); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: #dc2626; }
.btn-ghost { background: transparent; border-color: var(--border); }

/* Login */
.login-shell {
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(circle at 20% 15%, rgba(0, 229, 160, 0.08), transparent 45%),
              radial-gradient(circle at 80% 85%, rgba(56, 189, 248, 0.09), transparent 45%), var(--bg);
}
.login-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 32px; box-shadow: 0 24px 60px rgba(0,0,0,.55);
}
.brand-badge {
  display: inline-block; font-size: 11px; font-weight: 700; letter-spacing: .12em;
  color: var(--accent); background: var(--accent-dim); padding: 4px 10px; border-radius: 999px; margin-bottom: 12px;
}
.login-card h1 { margin: 0 0 6px; font-size: 22px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-top: 14px; }
.field span { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.field input, .search-input, .input-sm, textarea {
  width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; font: inherit; outline: none;
}
.field input:focus, .search-input:focus, .input-sm:focus, textarea:focus { border-color: var(--cyan); }
.input-sm { padding: 6px 10px; width: 200px; font-size: 12.5px; }
.login-foot {
  margin-top: 22px; padding-top: 16px; border-top: 1px solid var(--border);
  display: flex; justify-content: space-between; font-size: 12px; color: var(--muted);
}
.login-foot a { color: var(--cyan); text-decoration: none; }

/* App Layout */
.app-shell { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 10px 18px; background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.logo-mark {
  width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--cyan)); color: #041510; font-weight: 800; font-size: 14px;
}
.topbar-title { font-weight: 700; font-size: 15px; }
.topbar-sub { font-size: 11.5px; color: var(--muted); }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.stat-pill {
  font-size: 12px; font-weight: 600; color: var(--accent); background: var(--accent-dim);
  border: 1px solid rgba(0,229,160,.25); padding: 4px 10px; border-radius: 999px;
}

.workspace { display: grid; grid-template-columns: 300px 1fr; flex: 1; min-height: 0; }
@media (max-width: 860px) {
  .workspace { grid-template-columns: 1fr; }
  .sidebar { max-height: 240px; border-right: none; border-bottom: 1px solid var(--border); }
}

/* Sidebar */
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { display: flex; gap: 8px; padding: 12px; border-bottom: 1px solid var(--border); }
.device-list { flex: 1; overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.device-item {
  width: 100%; text-align: left; background: transparent; border: 1px solid transparent;
  color: var(--text); padding: 10px 12px; border-radius: 10px; cursor: pointer;
  display: flex; align-items: flex-start; gap: 10px; transition: background .15s, border-color .15s;
}
.device-item:hover { background: var(--surface-2); }
.device-item.active { background: var(--surface-2); border-color: var(--cyan); }
.status-dot {
  width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex-shrink: 0;
  background: #475569;
}
.status-dot.online { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.dev-name { font-weight: 600; font-size: 13.5px; }
.dev-sub { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.dev-pills { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.mini-pill {
  font-size: 10.5px; padding: 1px 6px; border-radius: 999px; background: rgba(255,255,255,.06); color: var(--muted);
}
.mini-pill.ok { color: var(--accent); background: var(--accent-dim); }
.mini-pill.warn { color: var(--amber); background: var(--amber-dim); }

/* Stage */
.stage { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.empty-stage {
  flex: 1; display: grid; place-content: center; text-align: center; padding: 40px; max-width: 480px; margin: 0 auto;
}
.empty-icon { font-size: 40px; color: var(--cyan); margin-bottom: 10px; }
.device-workspace { display: flex; flex-direction: column; flex: 1; min-height: 0; }

.device-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 14px 20px; background: rgba(14, 21, 38, 0.7); border-bottom: 1px solid var(--border);
}
.device-banner-main { display: flex; align-items: center; gap: 12px; }
.device-banner-main h2 { margin: 0; font-size: 18px; }
.device-banner-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Accessible Tabs (Modern Web Guidance) */
.tablist {
  display: flex; overflow-x: auto; gap: 4px; padding: 0 18px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
[role="tab"] {
  position: relative; flex: none; background: transparent; border: none; color: var(--muted);
  padding: 12px 14px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
[role="tab"]:hover { color: var(--text); }
[role="tab"][aria-selected="true"] { color: var(--accent); }
[role="tab"][aria-selected="true"]::after {
  content: ""; position: absolute; inset: auto 0 0; border-block-end: 3px solid var(--accent);
}

[role="tabpanel"] { flex: 1; min-height: 0; overflow-y: auto; }
.panel-inner { padding: 18px 20px; display: flex; flex-direction: column; gap: 16px; }

/* Cards & KPIs */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.kpi-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px;
}
.kpi-label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.kpi-val { font-size: 15px; font-weight: 600; margin-top: 4px; word-break: break-word; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px;
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h3 { margin: 0 0 2px; font-size: 15px; }
.card-head p { margin: 0; font-size: 12.5px; }
.row-gap { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* Health Result */
.health-top { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; margin-bottom: 14px; }
.score-ring {
  width: 78px; height: 78px; border-radius: 50%; display: grid; place-items: center;
  font-size: 24px; font-weight: 800; border: 4px solid var(--accent); color: var(--accent); background: var(--accent-dim);
}
.score-ring.warn { border-color: var(--amber); color: var(--amber); background: var(--amber-dim); }
.score-ring.bad { border-color: var(--danger); color: var(--danger); background: var(--danger-dim); }
.disk-bar-wrap { margin: 8px 0; }
.disk-bar-label { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 4px; }
.disk-bar { height: 7px; border-radius: 999px; background: #1b2842; overflow: hidden; }
.disk-bar > span { display: block; height: 100%; background: linear-gradient(90deg, var(--cyan), var(--accent)); }
.issue-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.issue-pill { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--amber-dim); color: var(--amber); }

/* AI Technician */
.ai-layout { height: 100%; }
.ai-banner {
  display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
  background: var(--cyan-dim); border: 1px solid rgba(56,189,248,.3); border-radius: var(--radius); padding: 10px 14px;
}
.ai-banner strong { display: block; color: var(--cyan); font-size: 13px; }
.ai-banner span { font-size: 12px; }
.ai-messages {
  flex: 1; min-height: 280px; max-height: calc(100vh - 340px); overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px; padding: 4px 2px;
}
.msg {
  max-width: 88%; padding: 12px 14px; border-radius: 12px; white-space: pre-wrap;
}
.msg.user { align-self: flex-end; background: #1a2c4e; border: 1px solid #2b4575; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); width: 100%; max-width: 100%; }
.msg-meta { font-size: 11px; color: var(--muted); margin-bottom: 4px; }

.cmd-card {
  margin-top: 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px;
}
.cmd-card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.risk-badge { font-size: 10.5px; font-weight: 700; text-transform: uppercase; padding: 2px 7px; border-radius: 999px; }
.risk-low { background: var(--accent-dim); color: var(--accent); }
.risk-medium { background: var(--amber-dim); color: var(--amber); }
.risk-high { background: var(--danger-dim); color: var(--danger); }
.cmd-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.ai-composer { display: flex; gap: 10px; align-items: flex-end; }
.ai-composer textarea { flex: 1; resize: vertical; min-height: 48px; }

/* Playbooks */
.playbooks-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.pb-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: flex; flex-direction: column; justify-content: space-between; gap: 10px;
}
.pb-card h4 { margin: 4px 0; font-size: 14px; }
.pb-cat { font-size: 11px; color: var(--cyan); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }

/* Terminal & Tables */
.code-input { background: #04070e; font-size: 12.5px; margin-bottom: 10px; resize: vertical; }
.terminal-pre {
  background: #04070e; color: #c7f9e3; border: 1px solid #162238; border-radius: 8px;
  padding: 12px; margin: 8px 0 0; overflow-x: auto; max-height: 300px; font-size: 12px; white-space: pre-wrap; word-break: break-word;
}
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.data-table th, .data-table td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.data-table th { color: var(--muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; }

/* Native Modal Dialogs (Modern Web Guidance) */
dialog {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 14px; padding: 22px; max-width: 540px; width: calc(100% - 32px);
  box-shadow: 0 24px 80px rgba(0,0,0,.7);
}
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop { background: rgba(2, 6, 16, 0.72); backdrop-filter: blur(4px); overflow: hidden; overscroll-behavior: contain; }
dialog h1 { margin: 0 0 8px; font-size: 18px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }

/* Invoice Modal & Print Sheet */
.invoice-dialog { max-width: 820px; }
.invoice-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.invoice-sheet {
  background: #ffffff; color: #0f172a; border-radius: 10px; padding: 28px;
}
.invoice-sheet h2 { margin: 0; color: #0f172a; }
.inv-header { display: flex; justify-content: space-between; border-bottom: 2px solid #0f172a; padding-bottom: 14px; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; }
.inv-table { width: 100%; border-collapse: collapse; margin: 16px 0; }
.inv-table th, .inv-table td { border: 1px solid #cbd5e1; padding: 8px 10px; text-align: left; font-size: 13px; }
.inv-table th { background: #f1f5f9; }
.inv-price-input { width: 110px; padding: 4px 6px; border: 1px solid #94a3b8; border-radius: 4px; text-align: right; background: #fff; color: #0f172a; font: inherit; }
.inv-total { text-align: right; font-size: 17px; font-weight: 700; margin-top: 12px; }

/* Toast */
.toast-root { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 9999; }
.toast {
  background: var(--surface-2); border: 1px solid var(--cyan); color: var(--text);
  padding: 10px 14px; border-radius: 10px; font-size: 13px; box-shadow: 0 12px 30px rgba(0,0,0,.5);
}

@media print {
  body * { visibility: hidden; }
  #invoice-sheet, #invoice-sheet * { visibility: visible; }
  #invoice-sheet { position: fixed; inset: 0; border-radius: 0; padding: 24px; }
  .no-print { display: none !important; }
  .inv-price-input { border: none; padding: 0; }
}
