/* ============================================================
   AI Gateway — Design System
   Dark-first, Linear/Vercel-inspired. Light theme via [data-theme=light].
   ============================================================ */

:root {
  --bg0: #09090b;
  --bg1: #101013;
  --bg2: #15151a;
  --bg3: #1c1c22;
  --bg-hover: #20202a;
  --border: #232330;
  --border-strong: #343442;
  --text: #ececf1;
  --text2: #a2a2b0;
  --text3: #66667a;
  --accent: #6d7cff;
  --accent-hover: #8592ff;
  --accent-dim: rgba(109, 124, 255, 0.14);
  --accent-text: #aab4ff;
  --green: #3ddc97;
  --green-dim: rgba(61, 220, 151, 0.12);
  --amber: #f5b544;
  --amber-dim: rgba(245, 181, 68, 0.13);
  --red: #f4645f;
  --red-dim: rgba(244, 100, 95, 0.12);
  --blue: #5eb0f7;
  --blue-dim: rgba(94, 176, 247, 0.12);
  --purple: #b07af7;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  --radius: 10px;
  --radius-sm: 7px;
  --font: ui-sans-serif, -apple-system, "Segoe UI Variable", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, Menlo, monospace;
  --sidebar-w: 232px;
}

[data-theme="light"] {
  --bg0: #fafafc;
  --bg1: #ffffff;
  --bg2: #f4f4f7;
  --bg3: #ececf1;
  --bg-hover: #e7e7ee;
  --border: #e3e3ea;
  --border-strong: #cfcfd9;
  --text: #17171c;
  --text2: #55555f;
  --text3: #8a8a96;
  --accent: #5061e0;
  --accent-hover: #4050cc;
  --accent-dim: rgba(80, 97, 224, 0.09);
  --accent-text: #4050cc;
  --green: #0e9f6e; --green-dim: rgba(14,159,110,.1);
  --amber: #b45309; --amber-dim: rgba(180,83,9,.1);
  --red: #dc2626; --red-dim: rgba(220,38,38,.08);
  --blue: #2563eb; --blue-dim: rgba(37,99,235,.08);
  --purple: #7c3aed;
  --shadow: 0 8px 30px rgba(20, 20, 40, 0.12);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg0);
  -webkit-font-smoothing: antialiased;
}
::selection { background: var(--accent-dim); }
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); font-size: 0.92em; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 5px; border: 2px solid var(--bg0); }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Boot splash ---------- */
.boot-splash { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.5px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Layout ---------- */
.shell { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--bg1);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 18px 18px 14px;
  font-weight: 650; font-size: 14.5px; letter-spacing: 0.01em;
}
.brand-mark {
  width: 26px; height: 26px; border-radius: 7px; flex-shrink: 0;
  background: linear-gradient(135deg, var(--accent), var(--purple));
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 13px;
}
.sidebar-section { padding: 14px 12px 4px; }
.sidebar-label {
  padding: 0 10px 6px; font-size: 10.5px; font-weight: 650;
  letter-spacing: 0.09em; text-transform: uppercase; color: var(--text3);
}
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; margin: 1px 0;
  border-radius: var(--radius-sm);
  color: var(--text2); font-weight: 480; font-size: 13.5px;
  cursor: pointer; user-select: none; text-decoration: none !important;
  transition: background 0.1s, color 0.1s;
}
.nav-item svg { width: 15.5px; height: 15.5px; opacity: 0.75; flex-shrink: 0; }
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item.active { background: var(--accent-dim); color: var(--accent-text); font-weight: 560; }
.sidebar-footer {
  margin-top: auto; padding: 12px; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.avatar {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, var(--accent), var(--purple));
  color: #fff; display: grid; place-items: center;
  font-size: 11.5px; font-weight: 700;
}
.sidebar-user { flex: 1; min-width: 0; }
.sidebar-user .name { font-size: 12.5px; font-weight: 560; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-user .email { font-size: 11px; color: var(--text3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.main { flex: 1; min-width: 0; padding: 26px 32px 60px; max-width: 1280px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-title { font-size: 21px; font-weight: 680; letter-spacing: -0.015em; margin: 0 0 3px; }
.page-sub { color: var(--text2); font-size: 13px; margin: 0; }
.head-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.topbar-mobile { display: none; }

@media (max-width: 900px) {
  .shell { flex-direction: column; }
  .sidebar {
    position: fixed; z-index: 60; left: 0; top: 0; bottom: 0;
    transform: translateX(-105%); transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .topbar-mobile {
    display: flex; align-items: center; justify-content: space-between;
    position: sticky; top: 0; z-index: 50;
    background: color-mix(in srgb, var(--bg0) 88%, transparent);
    backdrop-filter: blur(8px);
    padding: 10px 16px; border-bottom: 1px solid var(--border);
  }
  .main { padding: 18px 16px 50px; }
}

/* ---------- Cards & stats ---------- */
.card {
  background: var(--bg1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}
.card + .card { margin-top: 16px; }
.card-title { font-size: 13px; font-weight: 640; margin: 0 0 12px; color: var(--text); }
.card-sub { font-size: 12px; color: var(--text3); margin: -8px 0 12px; }

.grid-stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat {
  background: var(--bg1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px 12px;
}
.stat .label { font-size: 11px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; color: var(--text3); }
.stat .value { font-family: var(--mono); font-size: 21px; font-weight: 620; margin-top: 5px; letter-spacing: -0.02em; }
.stat .delta { font-size: 11.5px; color: var(--text3); margin-top: 2px; }
.stat.good .value { color: var(--green); }
.stat.bad .value { color: var(--red); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-31 { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
@media (max-width: 1080px) { .grid-2, .grid-31 { grid-template-columns: 1fr; } }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg1); }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th {
  text-align: left; font-size: 11px; font-weight: 620; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--text3);
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  background: var(--bg2); white-space: nowrap;
}
.tbl td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
.tbl tr:last-child td { border-bottom: none; }
.tbl tbody tr { transition: background 0.08s; }
.tbl tbody tr:hover { background: var(--bg2); }
.tbl td.wrap { white-space: normal; word-break: break-all; }
.tbl .num { font-family: var(--mono); font-size: 12.5px; }
.row-actions { display: flex; gap: 6px; opacity: 0.35; transition: opacity 0.12s; }
tr:hover .row-actions { opacity: 1; }

.table-toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
.table-toolbar .grow { flex: 1; }

.pagination { display: flex; align-items: center; gap: 10px; padding: 10px 2px 0; font-size: 12.5px; color: var(--text2); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11.5px; font-weight: 570; padding: 2.5px 9px;
  border-radius: 999px; white-space: nowrap;
}
.badge::before { content: ""; width: 5.5px; height: 5.5px; border-radius: 50%; background: currentColor; }
.badge.green { color: var(--green); background: var(--green-dim); }
.badge.red { color: var(--red); background: var(--red-dim); }
.badge.amber { color: var(--amber); background: var(--amber-dim); }
.badge.blue { color: var(--blue); background: var(--blue-dim); }
.badge.gray { color: var(--text2); background: var(--bg3); }
.badge.purple { color: var(--purple); background: var(--accent-dim); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  font-family: inherit; font-size: 13px; font-weight: 550;
  padding: 7px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg2); color: var(--text);
  cursor: pointer; transition: all 0.12s; white-space: nowrap;
  text-decoration: none !important;
}
.btn:hover { background: var(--bg-hover); border-color: var(--border-strong); }
.btn:active { transform: translateY(1px); }
.btn svg { width: 14px; height: 14px; }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--red); border-color: color-mix(in srgb, var(--red) 35%, transparent); }
.btn.danger:hover { background: var(--red-dim); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text2); }
.btn.ghost:hover { background: var(--bg-hover); color: var(--text); }
.btn.sm { padding: 4.5px 10px; font-size: 12.5px; }
.btn.icon-only { padding: 6px; }
.btn:disabled { opacity: 0.45; pointer-events: none; }

/* ---------- Forms ---------- */
.field { margin-bottom: 15px; }
.field label { display: block; font-size: 12.5px; font-weight: 570; margin-bottom: 6px; color: var(--text2); }
.input, select.input, textarea.input {
  width: 100%; font-family: inherit; font-size: 13.5px;
  background: var(--bg0); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  padding: 8px 11px; outline: none; transition: border-color 0.12s, box-shadow 0.12s;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-dim); }
.input.mono { font-family: var(--mono); font-size: 12.5px; }
.hint { font-size: 11.5px; color: var(--text3); margin-top: 5px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.search-box { position: relative; }
.search-box svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 14px; height: 14px; color: var(--text3); }
.search-box .input { padding-left: 31px; width: 240px; }
select.input { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23888' fill='none' stroke-width='1.5'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 10px center; padding-right: 28px; }

.switch { position: relative; display: inline-block; width: 34px; height: 20px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; border-radius: 999px; background: var(--bg3); border: 1px solid var(--border-strong); transition: 0.15s; cursor: pointer; }
.slider::before { content: ""; position: absolute; width: 14px; height: 14px; left: 2px; top: 2px; border-radius: 50%; background: var(--text2); transition: 0.15s; }
.switch input:checked + .slider { background: var(--accent); border-color: transparent; }
.switch input:checked + .slider::before { transform: translateX(14px); background: #fff; }

.checkbox { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tab {
  padding: 8px 14px; font-size: 13px; font-weight: 550; color: var(--text2);
  cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px;
  user-select: none; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent-text); border-bottom-color: var(--accent); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(3px);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 9vh 16px 16px; animation: fadeIn 0.14s ease;
}
.modal {
  background: var(--bg1); border: 1px solid var(--border-strong);
  border-radius: 14px; box-shadow: var(--shadow);
  width: 100%; max-width: 480px; max-height: 82vh;
  display: flex; flex-direction: column;
  animation: slideUp 0.16s ease;
}
.modal.lg { max-width: 640px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 0; }
.modal-title { font-size: 15.5px; font-weight: 650; margin: 0; }
.modal-body { padding: 14px 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 9px; padding: 4px 20px 18px; }
@keyframes fadeIn { from { opacity: 0; } }
@keyframes slideUp { from { transform: translateY(10px); opacity: 0; } }

.secret-reveal {
  font-family: var(--mono); font-size: 12.5px;
  background: var(--bg0); border: 1px dashed var(--accent);
  border-radius: var(--radius-sm); padding: 11px 13px;
  word-break: break-all; user-select: all; color: var(--accent-text);
}

/* ---------- Toasts ---------- */
.toasts { position: fixed; right: 18px; bottom: 18px; z-index: 200; display: flex; flex-direction: column; gap: 9px; max-width: 380px; }
.toast {
  display: flex; align-items: flex-start; gap: 9px;
  background: var(--bg2); border: 1px solid var(--border-strong);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm); padding: 11px 14px;
  box-shadow: var(--shadow); font-size: 13px;
  animation: toastIn 0.18s ease;
}
.toast.success { border-left-color: var(--green); }
.toast.error { border-left-color: var(--red); }
.toast .msg { flex: 1; }
.toast .close { cursor: pointer; color: var(--text3); font-size: 15px; line-height: 1; padding: 0 2px; }
@keyframes toastIn { from { transform: translateX(20px); opacity: 0; } }

/* ---------- States ---------- */
.empty {
  text-align: center; padding: 46px 20px; color: var(--text3);
}
.empty .icon { font-size: 30px; margin-bottom: 10px; opacity: 0.6; }
.empty .title { font-size: 14px; font-weight: 600; color: var(--text2); margin-bottom: 4px; }
.empty .sub { font-size: 12.5px; }
.skeleton {
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--bg2) 25%, var(--bg-hover) 50%, var(--bg2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite;
  height: 14px; margin: 9px 0;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.error-banner {
  background: var(--red-dim); border: 1px solid color-mix(in srgb, var(--red) 30%, transparent);
  color: var(--red); border-radius: var(--radius-sm);
  padding: 10px 14px; font-size: 13px; margin-bottom: 14px;
}

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 18px; font-size: 13px; }
.kv dt { color: var(--text3); }
.kv dd { margin: 0; font-family: var(--mono); font-size: 12.5px; word-break: break-all; }

.progressbar { height: 6px; border-radius: 999px; background: var(--bg3); overflow: hidden; }
.progressbar > div { height: 100%; border-radius: 999px; background: var(--accent); transition: width 0.4s ease; }
.progressbar.warn > div { background: var(--amber); }
.progressbar.crit > div { background: var(--red); }

/* ---------- Charts ---------- */
.chart-box { position: relative; }
.chart-svg { width: 100%; display: block; }
.chart-legend { display: flex; gap: 16px; font-size: 11.5px; color: var(--text2); margin-top: 8px; flex-wrap: wrap; }
.chart-legend .dot { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; vertical-align: baseline; }
.tooltip {
  position: absolute; pointer-events: none; z-index: 10;
  background: var(--bg3); border: 1px solid var(--border-strong);
  border-radius: 6px; padding: 6px 10px; font-size: 11.5px;
  box-shadow: var(--shadow); white-space: nowrap; display: none;
}
.donut-center { font-family: var(--mono); }

/* ---------- Login ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(ellipse 80% 50% at 50% -10%, var(--accent-dim), transparent), var(--bg0); }
.auth-card { width: 100%; max-width: 380px; }
.auth-logo { display: flex; flex-direction: column; align-items: center; gap: 12px; margin-bottom: 26px; }
.auth-logo .brand-mark { width: 42px; height: 42px; border-radius: 12px; font-size: 19px; }
.auth-logo h1 { font-size: 17px; font-weight: 650; margin: 0; }
.auth-logo p { font-size: 12.5px; color: var(--text3); margin: 0; }

/* ---------- Misc ---------- */
.flex { display: flex; align-items: center; gap: 10px; }
.spacer { flex: 1; }
.mt0 { margin-top: 0; } .mb0 { margin-bottom: 0; }
.text-dim { color: var(--text3); }
.text-small { font-size: 12px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.theme-toggle { background: none; border: none; color: var(--text3); cursor: pointer; padding: 6px; border-radius: 7px; display: grid; place-items: center; }
.theme-toggle:hover { background: var(--bg-hover); color: var(--text); }
.theme-toggle svg { width: 16px; height: 16px; }
.copy-btn { cursor: pointer; color: var(--text3); background: none; border: none; padding: 3px; border-radius: 4px; display: inline-flex; }
.copy-btn:hover { color: var(--accent-text); background: var(--bg-hover); }
.copy-btn svg { width: 13px; height: 13px; }
