:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e6e8eb;
  --border-strong: #d8dbe0;
  --text: #1a1d21;
  --muted: #6b7280;
  --faint: #9aa1ab;
  --accent: #4f46e5;
  --accent-soft: #eef0fe;
  --good: #0f9d58;
  --warn: #b7791f;
  --danger: #d1453b;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1c2027;
    --border: #262b33;
    --border-strong: #333944;
    --text: #e7eaee;
    --muted: #9aa1ab;
    --faint: #6b7280;
    --accent: #8b87f5;
    --accent-soft: #23233a;
    --good: #48c78e;
    --warn: #e0a53f;
    --danger: #f0736a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
  width: 224px;
  flex: 0 0 224px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 15px;
}
.brand-name { font-weight: 650; letter-spacing: 0.02em; font-size: 15px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 8px;
  color: var(--muted); text-decoration: none; font-weight: 500;
  cursor: pointer;
}
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-item.disabled { color: var(--faint); cursor: default; }
.nav-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: 0.55; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; padding: 0 8px; }
.acct { display: flex; flex-direction: column; gap: 1px; }
.acct-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--faint); }
.acct-id { font-size: 12.5px; font-variant-numeric: tabular-nums; color: var(--muted); }

/* Main */
.main { flex: 1; min-width: 0; padding: 24px 28px 40px; max-width: 1200px; }
.topbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
h1 { margin: 0; font-size: 21px; font-weight: 650; }
.sub { margin: 3px 0 0; color: var(--muted); font-size: 13px; }
.controls { display: flex; align-items: center; gap: 10px; }

.scope-toggle { display: inline-flex; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 2px; }
.scope-toggle button {
  border: 0; background: transparent; color: var(--muted);
  padding: 6px 11px; border-radius: 6px; font: inherit; font-size: 13px; cursor: pointer;
}
.scope-toggle button.active { background: var(--accent-soft); color: var(--accent); font-weight: 550; }

.btn {
  display: inline-flex; align-items: center; gap: 7px;
  background: var(--accent); color: #fff; border: 0;
  padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 550; cursor: pointer;
}
.btn:hover { filter: brightness(1.05); }
.btn:disabled { opacity: 0.65; cursor: default; }
.btn-spin {
  width: 13px; height: 13px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.updated { color: var(--faint); font-size: 12.5px; margin: 14px 0 18px; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow);
}
.kpi-label { color: var(--muted); font-size: 12.5px; font-weight: 500; }
.kpi-value { font-size: 26px; font-weight: 650; margin-top: 8px; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
.kpi-note { color: var(--faint); font-size: 12px; margin-top: 5px; }

/* Cards */
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 14px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.card-head {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 15px 18px; border-bottom: 1px solid var(--border);
}
.card-head h2 { margin: 0; font-size: 14.5px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.card-body { padding: 16px 18px; }
.card-body.no-pad { padding: 0; }
.muted { color: var(--muted); font-size: 12.5px; }

.count-pill {
  background: var(--accent-soft); color: var(--accent);
  font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 20px;
}

/* Spend by service bars */
.bar-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; padding: 7px 0; }
.bar-row + .bar-row { border-top: 1px solid var(--border); }
.bar-label { font-size: 13px; display: flex; align-items: center; gap: 9px; min-width: 0; }
.bar-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { height: 6px; border-radius: 4px; background: var(--accent); opacity: 0.85; }
.bar-amount { font-variant-numeric: tabular-nums; font-weight: 550; font-size: 13px; }

/* Daily chart */
.chart { width: 100%; height: 150px; }
.chart .bar { fill: var(--accent); opacity: 0.85; }
.chart .axis { stroke: var(--border-strong); stroke-width: 1; }
.chart text { fill: var(--faint); font-size: 10px; }

/* Empty states */
.empty { color: var(--muted); text-align: center; padding: 26px 16px; font-size: 13px; }
.empty.info { background: var(--surface-2); border-radius: 8px; }

/* Table */
.table-controls { display: flex; gap: 8px; }
#filter {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 7px 11px; border-radius: 8px; font: inherit; font-size: 13px; width: 260px;
}
#filter:focus { outline: none; border-color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 12px 18px; border-bottom: 1px solid var(--border); }
.chip {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
  font-size: 12px; padding: 4px 10px; border-radius: 20px; cursor: pointer;
}
.chip.active { background: var(--accent-soft); border-color: transparent; color: var(--accent); font-weight: 550; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 18px; font-size: 13px; }
th {
  color: var(--muted); font-weight: 550; font-size: 12px;
  border-bottom: 1px solid var(--border); cursor: pointer; white-space: nowrap;
  position: sticky; top: 0; background: var(--surface);
}
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr { border-bottom: 1px solid var(--border); }
tbody tr:last-child { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
.type-tag { display: inline-block; font-size: 12px; color: var(--text); font-weight: 500; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12px; color: var(--muted); }
.rid-name { font-weight: 500; }
.state { font-size: 11.5px; padding: 2px 8px; border-radius: 20px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.state.good { color: var(--good); }
.state.idle, .state.warn { color: var(--warn); }
.detail-kv { color: var(--muted); font-size: 12px; }
.est { font-weight: 600; }
.est.zero { color: var(--faint); font-weight: 400; }

.foot { color: var(--faint); font-size: 12px; margin-top: 22px; line-height: 1.6; }

@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .grid { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}

/* Login */
.login {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: var(--bg); padding: 20px; z-index: 50;
}
.login-card {
  width: 100%; max-width: 360px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
  padding: 30px 28px; display: flex; flex-direction: column; gap: 4px;
}
.login-brand { display: flex; align-items: center; gap: 9px; font-weight: 650; letter-spacing: 0.02em; margin-bottom: 14px; }
.login-title { margin: 0; font-size: 19px; font-weight: 650; }
.login-sub { margin: 3px 0 18px; color: var(--muted); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field span { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.field input {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 10px 12px; border-radius: 8px; font: inherit; font-size: 14px;
}
.field input:focus { outline: none; border-color: var(--accent); }
.login-btn { justify-content: center; width: 100%; padding: 11px; margin-top: 4px; font-size: 14px; }
.login-error {
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger); font-size: 12.5px; padding: 9px 11px; border-radius: 8px; margin-bottom: 12px;
}

/* Sidebar user row */
.user-row {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding-top: 12px; margin-top: 4px; border-top: 1px solid var(--border);
}
.user-name { font-size: 13px; font-weight: 500; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.signout {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  font: inherit; font-size: 12px; padding: 5px 10px; border-radius: 7px; cursor: pointer; flex: 0 0 auto;
}
.signout:hover { border-color: var(--border-strong); color: var(--text); }

/* Roles + admin controls */
.user-meta { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.role-badge {
  align-self: flex-start; font-size: 10.5px; font-weight: 600; letter-spacing: 0.02em;
  padding: 1px 7px; border-radius: 20px; text-transform: uppercase;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.role-badge[data-role="super_admin"] { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); border-color: transparent; }
.role-badge[data-role="admin"] { background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good); border-color: transparent; }
.btn-secondary { background: var(--surface); color: var(--accent); border: 1px solid var(--border-strong); }
.btn-secondary:hover { background: var(--accent-soft); filter: none; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); font-size: 13px; font-weight: 500;
  padding: 10px 16px; border-radius: 9px; box-shadow: 0 6px 24px rgba(0,0,0,0.18);
  z-index: 60; max-width: 90vw;
}
.toast.error { background: var(--danger); color: #fff; }

/* Nav buttons (view switching) */
.nav-item { width: 100%; text-align: left; font: inherit; background: transparent; }
button.nav-item { border: 0; }

/* Network flow view */
.kpis-3 { grid-template-columns: repeat(3, 1fr); }
#netFilter {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 7px 11px; border-radius: 8px; font: inherit; font-size: 13px; width: 260px;
}
#netFilter:focus { outline: none; border-color: var(--accent); }

.flow-card {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface-2); padding: 14px 16px; margin-bottom: 12px;
}
.flow-card:last-child { margin-bottom: 0; }
.flow-card.exposed { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.flow-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.flow-name { font-weight: 600; font-size: 14px; }
.flow-region { margin-left: auto; color: var(--faint); font-size: 12px; }
.exposed-badge {
  font-size: 10.5px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.02em;
  padding: 1px 8px; border-radius: 20px;
  background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger);
}
.flow-meta { color: var(--muted); font-size: 12px; margin-top: 6px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.sg-tag {
  font-size: 11px; padding: 1px 7px; border-radius: 6px;
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
}

.flow-cols {
  display: grid; grid-template-columns: 1fr auto 1fr; gap: 16px; align-items: center;
  margin-top: 12px;
}
.flow-col { min-width: 0; }
.flow-col-title {
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); margin-bottom: 7px;
}
.flow-col-title.in { color: var(--good); }
.flow-col-title.out { color: var(--accent); text-align: right; }
.flow-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.flow-list li {
  font-size: 12.5px; padding: 5px 9px; border-radius: 7px; background: var(--surface);
  border: 1px solid var(--border); display: flex; align-items: center; gap: 7px; flex-wrap: wrap;
}
.flow-col:last-child .flow-list li { justify-content: flex-end; }
.flow-list li.flow-risk { border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); background: color-mix(in srgb, var(--danger) 7%, var(--surface)); }
.flow-list li.flow-none { color: var(--faint); font-style: italic; background: transparent; border-style: dashed; }
.arrow { color: var(--faint); }
.svc { font-weight: 600; }
.net-internet { color: var(--warn); font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; }
.net-target { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; color: var(--muted); }
.net-target.is-sg { color: var(--accent); }
.net-warn {
  font-size: 10px; font-weight: 600; text-transform: uppercase; padding: 0 6px; border-radius: 20px;
  background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger);
}
.flow-mid { display: grid; place-items: center; }
.flow-node {
  font-size: 11.5px; font-weight: 600; color: var(--accent);
  background: var(--accent-soft); border: 1px solid transparent; padding: 6px 12px; border-radius: 8px;
  max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: center;
}

@media (max-width: 760px) {
  .kpis-3 { grid-template-columns: 1fr; }
  .flow-cols { grid-template-columns: 1fr; }
  .flow-mid { justify-self: start; }
  .flow-col-title.out { text-align: left; }
  .flow-col:last-child .flow-list li { justify-content: flex-start; }
}

/* Console + terminal */
.btn-sm { padding: 5px 12px; font-size: 12.5px; }
#consoleBody td { vertical-align: middle; }

.term-overlay {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(10, 12, 16, 0.55); backdrop-filter: blur(2px);
  display: grid; place-items: center; padding: 24px;
}
.term-window {
  width: min(1000px, 96vw); height: min(620px, 88vh);
  background: #0f1115; border: 1px solid var(--border-strong);
  border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  display: flex; flex-direction: column; overflow: hidden;
}
.term-bar {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 14px; background: #171a20; border-bottom: 1px solid #262b33;
}
.term-title { color: #e7eaee; font-size: 13px; font-weight: 600; }
.term-status {
  color: #9aa1ab; font-size: 11.5px; margin-left: 2px;
  padding: 1px 8px; border: 1px solid #262b33; border-radius: 20px;
}
.term-close {
  margin-left: auto; background: transparent; border: 1px solid #333944;
  color: #9aa1ab; font: inherit; font-size: 12px; padding: 5px 12px;
  border-radius: 7px; cursor: pointer;
}
.term-close:hover { color: #fff; border-color: #4a515e; }
.term-body { flex: 1; min-height: 0; padding: 8px 10px; background: #0f1115; }
.term-body .xterm { height: 100%; }

/* Rate + notes + cost controls */
.spend-controls { display: flex; align-items: center; gap: 12px; }
.link-btn {
  background: transparent; border: 0; color: var(--accent); font: inherit; font-size: 12.5px;
  cursor: pointer; padding: 0; font-weight: 550;
}
.link-btn:hover { text-decoration: underline; }
.link-btn:disabled { color: var(--faint); cursor: default; text-decoration: none; }

.rate-cell { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12px; color: var(--muted); white-space: nowrap; }

.note-td { min-width: 150px; }
.note-cell {
  min-width: 130px; min-height: 22px; padding: 3px 8px; border-radius: 6px;
  border: 1px solid transparent; font-size: 12.5px; color: var(--text);
  cursor: text; outline: none; white-space: pre-wrap; word-break: break-word;
}
.note-cell:hover { border-color: var(--border); background: var(--surface-2); }
.note-cell:focus { border-color: var(--accent); background: var(--surface); }
.note-cell:empty:before { content: "+ note"; color: var(--faint); }
.note-cell:empty:focus:before { content: ""; }

/* Lambda Cloud */
.kpis-4 { grid-template-columns: repeat(4, 1fr); }
.lam-ssh {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--accent);
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px;
  padding: 2px 8px; border-radius: 6px; cursor: pointer;
}
.lam-ssh:hover { border-color: var(--accent); }
.lam-link { color: var(--accent); font-size: 12px; font-weight: 550; text-decoration: none; margin-left: 6px; }
.lam-link:hover { text-decoration: underline; }
.lam-act {
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
  font: inherit; font-size: 12px; padding: 4px 10px; border-radius: 7px; cursor: pointer; margin-left: 6px;
}
.lam-act:hover:not(:disabled) { border-color: var(--border-strong); color: var(--text); }
.lam-act:disabled { opacity: 0.45; cursor: default; }
.lam-act.danger { color: var(--danger); }
.lam-act.danger:hover { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, var(--surface)); }

.launch-overlay {
  position: fixed; inset: 0; z-index: 70; background: rgba(10,12,16,0.5);
  display: grid; place-items: center; padding: 24px;
}
.launch-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,0.25); padding: 24px;
  display: flex; flex-direction: column; gap: 2px;
}
.launch-head { font-size: 16px; font-weight: 650; margin-bottom: 14px; }
.launch-card .field select, .launch-card .field input {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 9px 11px; border-radius: 8px; font: inherit; font-size: 13px; width: 100%;
}
.launch-card .field select:focus, .launch-card .field input:focus { outline: none; border-color: var(--accent); }
.launch-cost { color: var(--muted); font-size: 12.5px; margin: 4px 0 12px; }
.launch-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.launch-actions .btn { padding: 9px 18px; }

/* RunPod SSH key */
.ssh-key-box {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px;
}
.ssh-key-box code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0;
}
.card-body code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

/* EC2 row terminate */
.row-term {
  margin-left: 8px; background: transparent; border: 1px solid var(--border);
  color: var(--danger); font: inherit; font-size: 11px; padding: 2px 8px;
  border-radius: 6px; cursor: pointer;
}
.row-term:hover { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, transparent); }
