/* IndicStack Admin — shared light/dark theme system (mirrors the console's
   design system). Every surface (operator dashboard, customer portal) reads
   from these tokens, so a single toggle (see base.html) switches both
   consistently. The login page is deliberately exempt — its dark card is a
   fixed brand moment, not a user preference; it uses hardcoded literals. */
:root {
  --background: #F7F7F8;
  --surface: #FFFFFF;
  --surface-hover: #F0F0F1;
  --foreground: #18191B;
  --foreground-muted: #6B7280;
  --accent: #FF6602;
  --accent-rgb: 255, 102, 2;
  --border: rgba(0, 0, 0, 0.08);
  --border-hover: rgba(0, 0, 0, 0.12);
  --success: #16a34a;
  --error: #dc2626;
  --warning: #d97706;
  --success-rgb: 22, 163, 74;
  --error-rgb: 220, 38, 38;
  --warning-rgb: 217, 119, 6;
  --badge-success-text: #15803d;
  --badge-error-text: #b91c1c;
  --badge-warning-text: #b45309;
  --overlay: rgba(0, 0, 0, .4);
  --scrollbar-thumb: rgba(0, 0, 0, .15);
  --radius: 16px;
}

:root[data-theme="dark"] {
  --background: #0B0C0E;
  --surface: #141517;
  --surface-hover: #1a1b1e;
  --foreground: #F5F5F5;
  --foreground-muted: #888;
  --border: rgba(255, 255, 255, .08);
  --border-hover: rgba(255, 255, 255, .12);
  --success: #22c55e;
  --error: #f87171;
  --warning: #f59e0b;
  --success-rgb: 34, 197, 94;
  --error-rgb: 248, 113, 113;
  --warning-rgb: 245, 158, 11;
  --badge-success-text: #4ade80;
  --badge-error-text: #f87171;
  --badge-warning-text: #fbbf24;
  --overlay: rgba(0, 0, 0, .6);
  --scrollbar-thumb: rgba(255, 255, 255, .15);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--background);
  color: var(--foreground);
  font-family: "Inter", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  transition: background-color .15s, color .15s;
}

code, .mono { font-family: "JetBrains Mono", ui-monospace, monospace; }
.muted { color: var(--foreground-muted); }
a { color: var(--accent); text-decoration: none; }

::selection { background: var(--accent); color: #fff; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 16px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--foreground);
  font-size: 13px; font-weight: 500; cursor: pointer; transition: .15s;
}
.btn:hover { background: var(--surface-hover); border-color: var(--border-hover); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: #e65c00; }
.btn-danger { color: var(--error); border-color: rgba(var(--error-rgb), .3); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Theme toggle */
.theme-toggle { font-size: 15px; line-height: 1; padding: 8px 10px; }

/* LiteLLM gateway liveness indicator (topbar) */
.health-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--foreground-muted); }
.health-dot.ok { background: var(--success); }
.health-dot.down { background: var(--error); }

/* Inputs */
label { display: block; font-size: 12px; color: var(--foreground-muted); margin: 12px 0 4px; }
input[type=text], input[type=number], input[type=email], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: 9px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--foreground);
  font-size: 14px; font-family: inherit;
}
/* Search pill — rounded like buttons, inline magnifier icon (currentColor
   inherits the theme's muted tone via the data-URI stroke). */
input[type=search] {
  border-radius: 999px; padding-left: 34px; -webkit-appearance: none; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236B7280' stroke-width='2'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m21 21-4.3-4.3'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: 12px center; background-size: 14px;
}
input::placeholder, textarea::placeholder { color: var(--foreground-muted); opacity: .8; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
input:disabled, textarea:disabled { opacity: .5; cursor: not-allowed; }

/* Login — deliberately fixed dark, not tied to the theme toggle (see header note). */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: #0B0C0E; }
.login-card {
  width: 360px; text-align: center; padding: 20px 32px 32px;
  background: #141517; border: 1px solid rgba(255,255,255,.08);
}
.login-form { display: grid; gap: 10px; }
.login-error { color: #f87171; font-size: 13px; margin-bottom: 12px; }
.brand { display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 18px; font-weight: 700; }
.brand-logo { height: 56px; width: auto; margin: 4px 0 20px; }
.login-card input[type=text], .login-card input[type=password] {
  background: #0B0C0E; border-color: rgba(255,255,255,.12); color: #fff;
}
.login-card input::placeholder { color: rgba(255,255,255,.4); }
.login-card input:focus { border-color: var(--accent); }
.login-card .btn:not(.btn-primary) {
  background: transparent; border-color: rgba(255,255,255,.12); color: #fff;
}
.login-card .btn:not(.btn-primary):hover { background: rgba(255,255,255,.06); }

/* Layout */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.container { max-width: 1100px; margin: 0 auto; padding: 24px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.page-head { margin-bottom: 20px; }

h1 { font-size: 18px; font-weight: 600; margin: 0; }
h2 { font-size: 14px; font-weight: 600; margin: 0; }

/* Table */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th { text-align: left; font-size: 11px; font-weight: 500; color: var(--foreground-muted);
  padding: 10px 14px; border-bottom: 1px solid var(--border); }
tbody td { padding: 11px 14px; border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: none; }
.table-wrap { overflow-x: auto; }

/* Badges — tint background derived from the same success/error/warning rgb
   triplet in both themes; text color uses a dedicated --badge-*-text token
   (not --success/--error/--warning directly) tuned for AA contrast against
   that specific tinted background, which needs different lightness than
   text used directly on the page background. */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11px; font-weight: 500; }
.badge-active { background: rgba(var(--success-rgb), .14); color: var(--badge-success-text); }
.badge-suspended { background: rgba(var(--error-rgb), .14); color: var(--badge-error-text); }
/* Distinct from badge-suspended: "over budget" is a spend fact, not an
   account-status fact — an active user can be over budget, so it must not
   look identical to the account being suspended. */
.badge-over { background: rgba(var(--warning-rgb), .14); color: var(--badge-warning-text); }

/* Token usage progress bar */
.usage-bar { width: 100%; max-width: 140px; height: 5px; border-radius: 999px; background: var(--surface-hover); margin-top: 4px; overflow: hidden; }
.usage-bar-fill { height: 100%; border-radius: 999px; background: var(--accent); }
.usage-bar-fill.over { background: var(--error); }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; background: var(--overlay);
  display: none; place-items: center; padding: 24px; z-index: 50;
}
.modal-overlay.open { display: grid; }
.modal { width: 460px; max-width: 100%; max-height: 90vh; overflow-y: auto; }
/* Model picker in the user modal. Two rows per item, not one: the price/flag
   sits under the model name instead of beside it, because a fixed 1fr 1fr grid
   put a long flag next to a long model id and both wrapped mid-word. The
   checkbox spans both rows so it stays aligned with the name it belongs to. */
.model-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px 16px; margin-top: 6px; }
.model-list label {
  display: grid; grid-template-columns: auto 1fr; gap: 1px 8px;
  align-items: start; margin: 0; color: var(--foreground); font-size: 13px;
}
.model-list input { width: auto; grid-row: 1 / span 2; margin: 3px 0 0; }
.model-list .model-name { overflow-wrap: anywhere; }
.model-list .model-note { grid-column: 2; font-size: 11px; line-height: 1.4; }

/* One-time credentials panel */
.cred-panel { margin-top: 16px; padding: 14px; border-radius: 12px;
  background: var(--surface-hover); border: 1px solid rgba(255,102,2,.35); }
.cred-panel .mono { display: block; margin: 4px 0 10px; word-break: break-all; color: var(--accent); }
.copy-row { display: flex; align-items: center; gap: 8px; margin: 4px 0 10px; }
.copy-row .mono { margin: 0; flex: 1; }
.copy-row-top { align-items: flex-start; }
.copy-row-top .mono {
  background: var(--surface-hover); padding: 10px; border-radius: 8px; font-size: 12px; line-height: 1.5;
  white-space: pre; overflow-x: auto; max-width: 100%;
}
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
.error-text { color: var(--error); font-size: 13px; margin-top: 8px; min-height: 16px; }
.success-text { color: var(--success); font-size: 13px; margin-top: 8px; min-height: 16px; }
.warn-text { color: var(--warning); }

/* Non-destructive failure banner for a background refresh (e.g. after a
   mutation) — unlike the full-table error state, this never replaces data
   already on screen, it just flags that what's showing may be stale. */
.error-banner {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  background: rgba(var(--warning-rgb), .08); border: 1px solid rgba(var(--warning-rgb), .3);
  color: var(--badge-warning-text); border-radius: 12px; padding: 10px 14px; margin-bottom: 16px; font-size: 13px;
}
/* Same class of bug as .menu[hidden] below: an explicit display:flex on the
   element's own class has equal specificity to the browser's default
   [hidden]{display:none} and, being an author rule loaded later, silently
   wins — without this the banner renders on every page load regardless of
   the `hidden` attribute. */
.error-banner[hidden] { display: none; }
.error-banner span { flex: 1; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.mt-16 { margin-top: 16px; }

/* Customer portal shell — layout only below; all colors come from the
   shared tokens above, so the shell renders correctly in either theme with
   no portal-specific color overrides needed. */
.portal-shell { display: flex; min-height: 100vh; }
.portal-sidebar {
  width: 224px; flex-shrink: 0; display: flex; flex-direction: column;
  border-right: 1px solid var(--border); position: sticky; top: 0; height: 100vh;
}
.portal-sidebar-top { display: flex; align-items: center; gap: 8px; padding: 18px 20px; }
.portal-sidebar-top img { height: 22px; width: auto; }
.portal-brand-text { font-size: 15px; font-weight: 700; }
.portal-brand-text .accent { color: var(--accent); }
.portal-nav { flex: 1; padding: 12px; display: flex; flex-direction: column; gap: 2px; }
.portal-nav-item {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 8px;
  font-size: 14px; color: var(--foreground-muted); text-decoration: none; border-left: 2px solid transparent;
}
.portal-nav-item svg { width: 16px; height: 16px; flex-shrink: 0; }
.portal-nav-item:hover { color: var(--foreground); background: var(--surface-hover); }
.portal-nav-item.active { color: var(--foreground); background: var(--surface-hover); border-left-color: var(--accent); }
.portal-sidebar-bottom { border-top: 1px solid var(--border); padding: 12px; }
.portal-user { padding: 8px 12px; margin-bottom: 4px; }
.portal-user strong { display: block; font-size: 14px; color: var(--foreground); }
.portal-user span { font-size: 12px; color: var(--foreground-muted); }

.portal-content { flex: 1; padding: 32px 40px; max-width: 1200px; }
.portal-section { display: none; }
.portal-section.active { display: block; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.stat-label { font-size: 12px; color: var(--foreground-muted); margin: 0 0 4px; }
.stat-value { font-size: 24px; font-weight: 600; margin: 0; color: var(--foreground); }
.stat-sub { font-size: 12px; color: var(--foreground-muted); margin: 4px 0 0; }

/* Wider modal variant for content with many columns (usage tables). */
.modal-wide { width: 760px; }

/* Row-action overflow menu (Reset password / Rotate key / Suspend).
   position:fixed (not absolute-inside-table-wrap) because .table-wrap has
   overflow-x:auto, which per spec forces overflow-y:auto too — an
   absolutely-positioned menu inside it would get clipped by that scroll
   box on the very rows operators use most (bottom of the table). Exact
   top/left/right are computed in JS (positionMenu()) from the toggle
   button's real position, independent of any scrolling ancestor. */
.menu-wrap { display: inline-block; }
.menu {
  position: fixed; z-index: 50;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.12); min-width: 170px; padding: 4px; display: flex; flex-direction: column;
}
/* `.menu`'s explicit display:flex above has the same specificity as the
   browser's default [hidden]{display:none} and loads later, so it silently
   wins — without this rule every menu stays visible regardless of the
   `hidden` attribute the JS toggle relies on. */
.menu[hidden] { display: none; }
.menu button {
  display: block; width: 100%; text-align: left; padding: 8px 10px; border-radius: 6px;
  border: none; background: transparent; color: var(--foreground); font-size: 13px; cursor: pointer;
}
.menu button:hover { background: var(--surface-hover); }
.menu button.menu-danger { color: var(--error); }

/* Playground (chat) */
.pg-shell { display: flex; flex-direction: column; height: calc(100vh - 140px); min-height: 420px; }
.pg-toolbar { display: flex; align-items: center; gap: 8px; padding-bottom: 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.pg-model-select { max-width: 260px; }
.pg-settings-wrap { position: relative; display: inline-block; }
.pg-settings-popover {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 50; width: 220px; padding: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.12);
}
.pg-settings-popover[hidden] { display: none; }
.pg-messages { flex: 1; overflow-y: auto; padding: 16px 4px; display: flex; flex-direction: column; gap: 12px; }
.pg-empty { margin: auto; text-align: center; color: var(--foreground-muted); font-size: 13px; max-width: 340px; }
.pg-msg { display: flex; }
.pg-msg-user { justify-content: flex-end; }
.pg-msg-assistant { justify-content: flex-start; }
.pg-bubble {
  max-width: 75%; border-radius: 14px; padding: 10px 14px; font-size: 14px; line-height: 1.5;
  background: var(--surface); border: 1px solid var(--border);
}
.pg-msg-user .pg-bubble { background: rgba(var(--accent-rgb), .10); border-color: rgba(var(--accent-rgb), .35); }
.pg-msg-assistant .pg-bubble { background: var(--surface-hover); }
.pg-msg-text { white-space: pre-wrap; word-break: break-word; }
.pg-msg-image { max-height: 220px; border-radius: 10px; margin-bottom: 8px; display: block; }
.pg-cursor { display: inline-block; width: 6px; height: 14px; margin-left: 2px; background: var(--accent); animation: pg-blink 1s step-start infinite; vertical-align: text-bottom; }
@keyframes pg-blink { 50% { opacity: 0; } }
.pg-footer { text-align: center; font-size: 12px; color: var(--foreground-muted); padding: 6px 0; }
.pg-image-preview { display: flex; align-items: center; gap: 8px; padding-bottom: 10px; }
.pg-image-preview img { height: 64px; border-radius: 8px; border: 1px solid var(--border); }
.pg-composer { display: flex; align-items: flex-end; gap: 8px; border-top: 1px solid var(--border); padding-top: 12px; }
.pg-textarea { flex: 1; resize: none; max-height: 140px; }

@media (max-width: 720px) {
  .portal-shell { flex-direction: column; }
  .portal-sidebar { width: 100%; height: auto; position: static; flex-direction: row; align-items: center; }
  .portal-nav { flex-direction: row; overflow-x: auto; }
  .portal-sidebar-bottom { border-top: none; border-left: 1px solid var(--border); }
  .portal-user { display: none; }
  .portal-content { padding: 20px; }
  .pg-shell { height: auto; min-height: 60vh; }
}

/* Usage time-range picker (components/RangePicker.tsx) — a segmented control.
   Uses the same tokens as .btn so it themes with everything else; the active
   segment borrows the accent the way .btn-primary does, at lower weight so a
   row of four doesn't shout louder than the data it filters. */
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
}
.card-head h2 { margin: 0; }

.range-picker {
  display: inline-flex; border: 1px solid var(--border);
  border-radius: 999px; overflow: hidden; background: var(--surface);
}
.range-btn {
  padding: 5px 12px; font-size: 12px; font-weight: 500; line-height: 1.6;
  border: 0; border-right: 1px solid var(--border);
  background: transparent; color: var(--foreground-muted);
  cursor: pointer; transition: .15s; white-space: nowrap;
}
.range-btn:last-child { border-right: 0; }
.range-btn:hover:not(:disabled):not(.active) {
  background: var(--surface-hover); color: var(--foreground);
}
.range-btn.active {
  background: rgba(var(--accent-rgb), .12);
  color: var(--accent); font-weight: 600;
}
.range-btn:disabled { opacity: .5; cursor: not-allowed; }

/* Rate-limit inputs in the user modal — two short numeric fields side by side,
   stacking on narrow screens rather than squeezing. */
.limit-row { display: flex; gap: 12px; flex-wrap: wrap; }
.limit-row > div { flex: 1 1 160px; min-width: 0; }
.limit-row label { display: block; font-size: 12px; margin-bottom: 4px; }
.limit-row .input { width: 100%; }
