:root {
  --bg: #0b1418;
  --surface: #122027;
  --surface-2: #1a2c34;
  --border: #294049;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #5eead4;
  --primary-hover: #2dd4bf;
  --accent: #ff8a7a;
  --danger: #f87171;
  --warning: #fbbf24;
  --success: #4ade80;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ---- Environment ribbon ----------------------------------------------- */
/* Always-visible bar that makes DEV vs PRODUCTION unmistakable. Rendered by
   admin.js from GET /admin-api/meta, so it shows even on the login screen. */
.env-ribbon {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 6px 16px;
  font-size: 13px; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase;
  text-align: center;
}
.env-ribbon .env-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: currentColor; box-shadow: 0 0 0 3px rgba(255,255,255,0.18);
}
.env-ribbon.prod {
  background: repeating-linear-gradient(45deg, #7f1d1d 0 14px, #991b1b 14px 28px);
  color: #fff;
}
.env-ribbon.dev {
  background: repeating-linear-gradient(45deg, #134e4a 0 14px, #115e59 14px 28px);
  color: #99f6e4;
}
/* Solid accent border under the topbar to reinforce the environment colour. */
body.is-prod .topbar { border-bottom: 3px solid #ef4444; }
body.is-dev  .topbar { border-bottom: 3px solid #14b8a6; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.brand em { color: var(--muted); font-style: normal; font-weight: 400; margin-left: 4px; }
.logo {
  display: block;
  height: 28px; width: auto;
  flex-shrink: 0;
}
.who { color: var(--muted); font-size: 14px; display: flex; gap: 12px; align-items: center; }
.who button { background: transparent; color: var(--muted); border: 1px solid var(--border); padding: 4px 10px; border-radius: 6px; cursor: pointer; }
.who button:hover { color: var(--text); border-color: var(--primary); }

main { max-width: 1080px; margin: 0 auto; padding: 24px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 20px;
}
.card h2 { margin: 0 0 16px; font-size: 18px; }
.card h3 { margin: 16px 0 8px; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

.tabs { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.tabs button {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.tabs button.active { background: var(--primary); color: var(--bg); border-color: var(--primary); font-weight: 600; }

label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input, textarea, select {
  width: 100%; padding: 9px 11px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font: inherit;
}
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; min-height: 90px; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: var(--primary); }
.field { margin-bottom: 14px; }
.row { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
@media (max-width: 600px) { .row { grid-template-columns: 1fr; } }

/* Typed setting card (System Settings → Core settings list).
   Each setting is a bordered card with header on top and an
   input + Save button row laid out horizontally below. */
.typed-field {
  display: flex; flex-direction: column; gap: 6px;
  padding: 12px 14px; margin-bottom: 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2);
}
.typed-field-header { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.typed-field-header label { display: inline; margin: 0; color: var(--text); font-size: 14px; font-weight: 600; }
.typed-field-key { font-size: 11px; }
.typed-field-help { margin: 0 0 4px; }
.typed-field-input-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.typed-field-input-row > input[type="text"],
.typed-field-input-row > input[type="number"],
.typed-field-input-row > select { flex: 1 1 220px; width: auto; min-width: 0; }
.typed-field-input-row > button { flex: 0 0 auto; }
.typed-field-bool { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; margin: 0; color: var(--text); }
.typed-field-bool input[type="checkbox"] { width: auto; }
.typed-field-status { margin-left: 4px; }

button.primary {
  background: var(--primary); color: var(--bg); border: none;
  padding: 9px 16px; border-radius: 8px; cursor: pointer; font-weight: 600;
}
button.primary:hover { background: var(--primary-hover); }
button.ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border); padding: 8px 14px; border-radius: 8px; cursor: pointer;
}
button.ghost:hover { border-color: var(--primary); }
button.danger {
  background: transparent; color: var(--danger);
  border: 1px solid var(--danger); padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
button.danger:hover { background: var(--danger); color: var(--bg); }

.error { color: var(--danger); padding: 8px 12px; background: rgba(248,113,113,0.1); border-radius: 6px; margin-bottom: 12px; font-size: 14px; }
.ok { color: var(--success); padding: 8px 12px; background: rgba(74,222,128,0.1); border-radius: 6px; margin-bottom: 12px; font-size: 14px; }

table { width: 100%; border-collapse: collapse; margin-top: 8px; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); font-size: 14px; vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
td.actions {
  white-space: normal;
  width: 200px;
  min-width: 200px;
}
/* Wrap the action buttons into a tidy, fixed-width column so a row of many
   actions (Add credit / Re-enable billing / Mark tester / caps…) wraps onto
   multiple lines instead of forcing the whole table to scroll sideways. */
td.actions button {
  margin: 0 6px 6px 0;
  font-size: 12px;
  padding: 6px 10px;
  vertical-align: top;
}

.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; }
.pill.PENDING_REVIEW { background: rgba(251,191,36,0.15); color: var(--warning); }
.pill.APPROVED { background: rgba(74,222,128,0.15); color: var(--success); }
.pill.REJECTED, .pill.REMOVED { background: rgba(248,113,113,0.15); color: var(--danger); }
.pill.HIDDEN { background: rgba(148,163,184,0.15); color: var(--muted); }

.login-wrap { display: grid; place-items: center; min-height: 70vh; }
.login-card { width: 100%; max-width: 420px; }

.muted { color: var(--muted); font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-all; }
hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

/* AI Models tab */
.pill.ok-pill { background: rgba(74,222,128,0.18); color: var(--success); }
.pill.warn-pill { background: rgba(251,191,36,0.18); color: var(--warning); }
.pill.slow-pill { background: rgba(248,113,113,0.18); color: var(--danger); }
.sample-cell { max-width: 280px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-word; }

/* Selected-role state on action buttons. Pinned models also get a left border. */
button.ghost.selected {
  background: color-mix(in srgb, var(--primary) 14%, transparent);
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 600;
}
button.ghost.selected:hover { background: color-mix(in srgb, var(--primary) 22%, transparent); }
tr.model-row-pinned > td:first-child { box-shadow: inset 3px 0 0 var(--primary); }
.role-badges { display: inline-flex; gap: 4px; margin-left: 6px; vertical-align: middle; }
.role-badges .pill { font-size: 10px; padding: 1px 6px; background: color-mix(in srgb, var(--primary) 18%, transparent); color: var(--primary); }

/* Compact summary header for the AI Models tab */
.model-roles-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; margin: 12px 0 18px; }
.model-role-card { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.model-role-card .role-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin-bottom: 4px; }
.model-role-card .role-value { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-all; }
.model-role-card .role-value.empty { color: var(--muted); font-style: italic; font-family: inherit; font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; word-break: break-all; }
hr { border: 0; border-top: 1px solid var(--border); margin: 24px 0; }

/* Exempt-cap inline status under the Billing column */
.exempt-cap-cell { margin-top: 4px; font-size: 11px; }
.exempt-cap-cell .err { color: var(--danger); }

/* Lightweight modal built on <dialog>. CSP-safe (no inline styles). */
dialog.admin-modal { border: 1px solid var(--border); border-radius: 8px; padding: 0; background: var(--surface, #fff); color: inherit; max-width: 440px; width: 90vw; }
dialog.admin-modal::backdrop { background: rgba(0,0,0,0.45); }
.admin-modal-body { padding: 18px 20px; }
.admin-modal-body h3 { margin: 0 0 10px; font-size: 16px; }
.admin-modal-body p { margin: 0 0 10px; font-size: 13px; color: var(--muted); }
.admin-modal-body input[type="number"], .admin-modal-body input[type="text"] { width: 100%; box-sizing: border-box; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: transparent; color: inherit; font-size: 14px; }
.admin-modal-actions { display: flex; gap: 8px; justify-content: flex-end; padding: 10px 20px 16px; }

/* ---------- Nests tab: clickable rows + detail popup ---------- */
tr.nest-row { cursor: pointer; }
tr.nest-row:hover { background: var(--surface-2); }
tr.nest-row:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }

dialog.nest-detail-modal { max-width: 760px; }
.nest-detail-body { padding: 18px 22px; }
.nest-detail-title { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.nest-detail-title h3 { margin: 0; font-size: 18px; }
.nest-detail-actions {
  display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 18px;
  padding-bottom: 16px; border-bottom: 1px solid var(--border);
}
.nest-detail-actions button { font-size: 13px; }
.nest-detail-report { display: flex; flex-direction: column; gap: 18px; }
.nest-report-block h5 { margin: 0 0 8px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

/* ---------- Dashboard ---------- */

/* ---------- Dashboard ---------- */
.dashboard { display: flex; flex-direction: column; gap: 16px; }
.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
@media (max-width: 900px) { .kpi-row { grid-template-columns: repeat(2, 1fr); } }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 14px 16px; display: flex; flex-direction: column; gap: 4px;
}
.kpi-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.kpi-value { font-size: 22px; font-weight: 700; color: var(--text); }
.kpi-sub { font-size: 11px; color: var(--muted); }
.kpi-pos .kpi-value { color: var(--success); }
.kpi-neg .kpi-value { color: var(--danger); }
.kpi-pos { border-left: 3px solid var(--success); }
.kpi-neg { border-left: 3px solid var(--danger); }

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

.donut-row { display: flex; align-items: center; gap: 16px; margin: 8px 0; color: var(--text); }
.donut-row svg { flex-shrink: 0; }
.donut-legend, .chart-legend { display: flex; flex-direction: column; gap: 6px; flex: 1; }
.chart-legend { flex-direction: row; flex-wrap: wrap; gap: 14px; margin-top: 8px; }
.legend-row { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text); }
.legend-swatch { width: 12px; height: 12px; border-radius: 2px; flex-shrink: 0; }
.legend-label { color: var(--muted); }
.legend-value { margin-left: auto; font-weight: 600; color: var(--text); }

.dashboard .muted { color: var(--muted); }
.dashboard .small { font-size: 12px; }
.dashboard-footer { text-align: right; opacity: 0.7; }

/* ---------- Dashboard info popups ---------- */
.info-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; margin-left: 6px; padding: 0;
  border-radius: 50%; border: 1px solid var(--border);
  background: transparent; color: var(--muted);
  font-size: 10px; font-weight: 700; font-family: serif; line-height: 1;
  cursor: pointer; vertical-align: middle;
}
.info-btn:hover { color: var(--text); border-color: var(--primary); background: var(--surface-2); }
.heading-with-info { display: flex; align-items: center; gap: 6px; }
.heading-with-info .info-btn { width: 18px; height: 18px; font-size: 11px; }
.kpi-label { display: flex; align-items: center; }
.acct-tile-label { display: flex; align-items: center; }

.info-modal { max-width: 560px; }
.info-modal-body { padding: 18px 22px; }
.info-modal-body h3 { font-size: 17px; margin: 0 0 12px; color: var(--text); }
.info-modal-content { font-size: 13px; line-height: 1.55; color: var(--text); }
.info-modal-content p { margin: 0 0 10px; }
.info-modal-content p:last-child { margin-bottom: 0; }
.info-modal-content ul { margin: 0 0 10px; padding-left: 20px; }
.info-modal-content li { margin-bottom: 6px; }
.info-modal-content code {
  background: var(--surface-2); padding: 1px 5px; border-radius: 3px;
  font-size: 12px; color: var(--accent);
}
.info-modal-content pre {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 6px; padding: 10px 12px; margin: 8px 0;
  font-size: 12px; line-height: 1.45; overflow-x: auto;
  color: var(--text); white-space: pre-wrap;
}
.info-modal-content strong { color: var(--text); }
.info-modal-content em { color: var(--muted); font-style: normal; }

/* ---------- Accounting ---------- */
.acct-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
  margin: 8px 0 16px;
}
.acct-tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
}
.acct-tile-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.acct-tile-value { font-size: 20px; font-weight: 700; color: var(--text); margin-top: 4px; font-variant-numeric: tabular-nums; }
.acct-tile-sub { font-size: 11px; color: var(--muted); margin-top: 2px; }
.acct-tile.tone-good { border-left-color: var(--success); }
.acct-tile.tone-good .acct-tile-value { color: var(--success); }
.acct-tile.tone-warn { border-left-color: var(--warning); }
.acct-tile.tone-warn .acct-tile-value { color: var(--warning); }
.acct-tile.tone-bad { border-left-color: var(--danger); }
.acct-tile.tone-bad .acct-tile-value { color: var(--danger); }

/* ---------- Searchable model dropdown (Global config) ---------- */
.model-combo { position: relative; flex: 1 1 320px; min-width: 0; }
.typed-field-input-row > .model-combo { flex: 1 1 320px; }

.model-combo-trigger {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface-2); color: var(--text); cursor: pointer;
  font-size: 13px; text-align: left;
}
.model-combo-trigger:hover { border-color: var(--primary); }
.model-combo.open .model-combo-trigger { border-color: var(--primary); outline: 2px solid var(--primary); outline-offset: 0; }
.model-combo-trigger-label { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; flex-wrap: wrap; }
.model-combo-trigger-id { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-all; }
.model-combo-caret { flex-shrink: 0; color: var(--muted); font-size: 11px; }

.model-combo-panel {
  display: none; position: absolute; z-index: 40; top: calc(100% + 4px); left: 0;
  width: min(760px, 94vw); background: var(--surface); border: 1px solid var(--primary);
  border-radius: 8px; box-shadow: 0 10px 30px rgba(0,0,0,0.45); overflow: hidden;
}
.model-combo.open .model-combo-panel { display: block; }
.model-combo-search {
  width: 100%; border: 0; border-bottom: 1px solid var(--border);
  border-radius: 0; padding: 10px 12px; background: var(--surface-2); color: var(--text); font-size: 13px;
}
.model-combo-search:focus { outline: none; border-bottom-color: var(--primary); }
.model-combo-list { max-height: 320px; overflow-y: auto; }

/* Free / Paid / All segmented filter inside the panel. */
.model-combo-filter { display: flex; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--border); background: var(--surface-2); flex-wrap: wrap; }
.model-combo-filter-btn {
  flex: 0 0 auto; padding: 4px 12px; border: 1px solid var(--border); border-radius: 999px;
  background: transparent; color: var(--muted); font-size: 12px; cursor: pointer;
}
.model-combo-filter-btn:hover { border-color: var(--primary); color: var(--text); }
.model-combo-filter-btn.active { background: color-mix(in srgb, var(--primary) 18%, transparent); border-color: var(--primary); color: var(--primary); font-weight: 600; }

/* Capability multi-select pills (Vision, Tools, Reasoning, ...). Distinct
   accent color from the Free/Paid/All segmented control so it reads as a
   separate, independent filter row. */
.model-combo-cap-filter { gap: 6px 8px; }
.model-combo-cap-btn.active { background: color-mix(in srgb, var(--success) 18%, transparent); border-color: var(--success); color: var(--success); font-weight: 600; }

.model-combo-row {
  display: grid;
  grid-template-columns: minmax(160px, 2fr) 70px minmax(130px, 1.4fr) minmax(110px, 1.1fr) 76px 76px;
  align-items: center; gap: 10px; padding: 7px 12px; cursor: pointer;
  border-bottom: 1px solid color-mix(in srgb, var(--border) 55%, transparent);
}
.model-combo-row:hover { background: var(--surface-2); }
.model-combo-row.selected { background: color-mix(in srgb, var(--primary) 14%, transparent); }
.model-combo-row.model-combo-head {
  position: sticky; top: 0; z-index: 1; cursor: default; background: var(--surface);
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted);
}
.model-combo-row.model-combo-head:hover { background: var(--surface); }
.model-combo-cell { min-width: 0; font-size: 12px; }
.model-combo-name { display: flex; flex-direction: column; gap: 1px; }
.model-combo-id { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-all; color: var(--text); }
.model-combo-sub { font-size: 11px; }
.model-combo-type { display: flex; gap: 4px; flex-wrap: wrap; }
.model-combo-caps, .model-combo-cell.model-combo-caps { display: flex; gap: 4px; flex-wrap: wrap; }
.model-combo-price, .model-combo-ctx { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; text-align: right; font-variant-numeric: tabular-nums; }
.model-combo-added { font-size: 11px; color: var(--muted); text-align: right; }
.model-combo-empty { padding: 14px 12px; font-size: 13px; }

.pill.model-tag-free { background: rgba(74,222,128,0.16); color: var(--success); }
.pill.model-tag-paid { background: rgba(94,234,212,0.14); color: var(--primary); }
.pill.model-tag-new { background: rgba(251,191,36,0.18); color: var(--warning); }
.pill.model-tag-stale { background: rgba(248,113,113,0.16); color: var(--danger); text-transform: none; }

/* Capability pills shown per-row. Each capability gets its own subtle hue so
   the pill group is scannable at a glance. */
.pill.model-cap-pill { text-transform: none; font-size: 10px; padding: 1px 7px; letter-spacing: 0; }
.pill.model-cap-vision { background: rgba(94,234,212,0.16); color: var(--primary); }
.pill.model-cap-imageGen { background: rgba(232,121,249,0.16); color: #e879f9; }
.pill.model-cap-audio { background: rgba(96,165,250,0.16); color: #60a5fa; }
.pill.model-cap-pdf { background: rgba(251,191,36,0.16); color: var(--warning); }
.pill.model-cap-tools { background: rgba(74,222,128,0.16); color: var(--success); }
.pill.model-cap-reasoning { background: rgba(167,139,250,0.18); color: #a78bfa; }
.pill.model-cap-structuredOutput { background: rgba(148,163,184,0.18); color: var(--muted); }

@media (max-width: 640px) {
  .model-combo-row { grid-template-columns: 1fr 70px 90px; }
  .model-combo-caps, .model-combo-ctx, .model-combo-added,
  .model-combo-head .model-combo-caps, .model-combo-head .model-combo-ctx, .model-combo-head .model-combo-added { display: none; }
}

.acct-reconcile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.acct-reconcile-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  font-size: 13px;
}
.acct-reconcile-row strong { font-variant-numeric: tabular-nums; color: var(--text); }
.acct-reconcile-row.acct-drift strong { color: var(--warning); }

.acct-credit { color: var(--success); }
.acct-debit  { color: var(--danger); }

.pill.pill-good    { background: rgba(74,222,128,0.18); color: var(--success); }
.pill.pill-warn    { background: rgba(251,191,36,0.18); color: var(--warning); }
.pill.pill-bad     { background: rgba(248,113,113,0.18); color: var(--danger); }
.pill.pill-debit   { background: rgba(248,113,113,0.12); color: var(--danger); }
.pill.pill-neutral { background: rgba(148,163,184,0.18); color: var(--muted); }
