:root {
  --bg: #0b0e14;
  --panel: #151a23;
  --panel-2: #1b212c;
  --border: #262d3a;
  --text: #e6e9ef;
  --dim: #8b95a7;
  --accent: #6ea8fe;
  --green: #4ade80;
  --amber: #fbbf24;
  --red: #f87171;
  --radius: 12px;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
h2 { font-size: 1.15rem; margin: 0.2rem 0 0.6rem; }
h3 { font-size: 0.95rem; color: var(--dim); margin: 1.4rem 0 0.6rem; font-weight: 600; }
code { background: var(--panel-2); padding: 1px 6px; border-radius: 6px; font-size: 0.85em; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.8rem 1.2rem; border-bottom: 1px solid var(--border); background: var(--panel);
}
.brand { font-weight: 700; font-size: 1.05rem; }
.brand .logo { margin-right: 4px; }
.dim { color: var(--dim); font-weight: 400; }
.topbar-actions { display: flex; align-items: center; gap: 0.5rem; }

.tabs { display: flex; gap: 2px; padding: 0 1.2rem; border-bottom: 1px solid var(--border); background: var(--panel); }
.tab {
  background: none; border: none; color: var(--dim); padding: 0.7rem 1rem; cursor: pointer;
  font-size: 0.9rem; border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab:hover { color: var(--text); }

main { padding: 1.4rem; max-width: 1100px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; }

.row { display: flex; align-items: center; }
.row.between { justify-content: space-between; }
.row.gap { gap: 0.5rem; }

.btn {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  padding: 0.5rem 0.9rem; border-radius: 8px; cursor: pointer; font-size: 0.85rem;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #0b0e14; border-color: var(--accent); font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); border-color: #3a2530; }
.btn.small { padding: 0.3rem 0.6rem; font-size: 0.78rem; }

.input, .select {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 0.5rem 0.7rem; border-radius: 8px; font-size: 0.85rem;
}

.pill { font-size: 0.75rem; padding: 3px 9px; border-radius: 20px; background: var(--panel-2); border: 1px solid var(--border); }
.pill.ok { color: var(--green); border-color: #1f3a2a; }
.pill.bad { color: var(--red); border-color: #3a2530; }

.hint { color: var(--dim); font-size: 0.82rem; margin: 0.4rem 0 1rem; line-height: 1.5; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0.8rem; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }
.card .label { color: var(--dim); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; }
.card .value { font-size: 1.6rem; font-weight: 700; margin-top: 0.3rem; }

.chart-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }

.capacity-list { display: flex; flex-direction: column; gap: 0.7rem; }
.cap-row { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 0.8rem 1rem; }
.cap-head { display: flex; justify-content: space-between; margin-bottom: 0.5rem; }
.bar { height: 8px; background: var(--panel-2); border-radius: 6px; overflow: hidden; margin: 4px 0; }
.bar > span { display: block; height: 100%; background: var(--green); }
.bar > span.warn { background: var(--amber); }
.bar > span.crit { background: var(--red); }
.bar-label { font-size: 0.72rem; color: var(--dim); }

.profiles, .keys { display: flex; flex-direction: column; gap: 0.7rem; }
.profile, .key-item {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 0.9rem 1rem;
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
}
.profile .meta, .key-item .meta { min-width: 0; }
.profile .meta { flex: 1 1 auto; }
.profile .title, .key-item .title { font-weight: 600; }
.profile .sub, .key-item .sub { color: var(--dim); font-size: 0.78rem; margin-top: 2px; overflow-wrap: anywhere; word-break: normal; }
.account-quota { max-width: 620px; margin-top: 0.65rem; }
.account-quota .bar + .bar-label { margin-top: 0.35rem; }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.status-active { background: var(--green); }
.status-cooldown { background: var(--amber); }
.status-disabled { background: var(--dim); }
.status-error { background: var(--red); }
.actions { display: flex; gap: 0.4rem; flex-shrink: 0; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: 0.8rem; }
th, td { text-align: left; padding: 0.5rem 0.7rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--dim); font-weight: 600; background: var(--panel); position: sticky; top: 0; }
td.err { color: var(--red); max-width: 300px; overflow: hidden; text-overflow: ellipsis; }
.st-2 { color: var(--green); } .st-4 { color: var(--amber); } .st-5 { color: var(--red); }
/* Any failed request (HTTP >= 400): tint the whole row and make ALL its text red
   (overrides per-cell colors like .st-4 amber, .dim placeholders, etc.). */
tr.row-err td { background: rgba(248, 113, 113, 0.09); }
tr.row-err td, tr.row-err td * { color: var(--red); }
.reason { color: var(--accent); }
.expandable {
  display: inline-block; max-width: 200px; vertical-align: bottom; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.expandable.expanded { max-width: 520px; white-space: pre-wrap; word-break: break-word; }
.dropped { color: var(--amber); font-size: 0.72rem; }
.params-cell { color: var(--dim); font-size: 0.72rem; font-family: ui-monospace, monospace; }

/* Logs tab breaks out to full page width (the table can be wide). */
#logs.panel.active { width: 100vw; margin-left: calc(50% - 50vw); padding: 0 1.2rem; }

.code { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; overflow-x: auto; font-size: 0.8rem; }

.toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%) translateY(200%);
  background: var(--panel-2); border: 1px solid var(--border); padding: 0.7rem 1.2rem;
  border-radius: 10px; transition: transform 0.25s; max-width: 90vw; z-index: 50;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.ok { border-color: #1f3a2a; }
.toast.err { border-color: #3a2530; }

.keyreveal { background: var(--panel-2); border: 1px dashed var(--accent); padding: 0.7rem; border-radius: 8px; margin-top: 0.5rem; font-family: monospace; word-break: break-all; font-size: 0.78rem; }

.key-reveal-card { background: var(--panel-2); border: 1px solid var(--accent); border-radius: 10px; padding: 0.9rem 1rem; margin-bottom: 0.9rem; }
.key-reveal-card #keyRevealValue { font-family: monospace; font-size: 0.82rem; }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: none; align-items: center; justify-content: center; z-index: 100; backdrop-filter: blur(2px); }
.modal-overlay.show { display: flex; }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 1.4rem; width: min(460px, 92vw); box-shadow: 0 20px 60px rgba(0,0,0,0.5); }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.2rem; }
.modal-head h3 { margin: 0; color: var(--text); font-size: 1.1rem; }
.modal-close { background: none; border: none; color: var(--dim); font-size: 1.6rem; line-height: 1; cursor: pointer; padding: 0 0.3rem; }
.modal-close:hover { color: var(--text); }
.modal-label { display: block; font-size: 0.78rem; color: var(--dim); margin: 0.7rem 0 0.3rem; }
.input.full { width: 100%; }
.modal-actions { display: flex; gap: 0.6rem; margin-top: 1rem; }
.modal-actions .btn { flex: 1; text-align: center; }

/* ---- Standardization & foolproofing pass ---- */

/* Keyboard focus visibility */
.btn:focus-visible, .tab:focus-visible, .input:focus-visible, .select:focus-visible,
.modal-close:focus-visible, .expandable:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.input:focus, .select:focus { border-color: var(--accent); }

/* Disabled / busy buttons can't be double-clicked */
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:disabled:hover { border-color: var(--border); }

/* Responsive: let bars wrap instead of overflowing on narrow screens */
.topbar { flex-wrap: wrap; gap: 0.5rem; }
.topbar-actions { flex-wrap: wrap; }
.row.gap { flex-wrap: wrap; }
.row.between { gap: 0.6rem; }
.tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tab { white-space: nowrap; }

/* Truncate long single-line labels instead of breaking layout */
.ellip { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.muted { opacity: 0.7; }
.err-inline { color: var(--red); }
td.ellip-cell { max-width: 220px; overflow: hidden; text-overflow: ellipsis; }

/* Modals stay usable on short viewports */
.modal { max-height: 88vh; overflow-y: auto; }

/* ---- Inline SVG icons ---- */
.icon { width: 1em; height: 1em; flex: 0 0 auto; vertical-align: -0.15em; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 0.45em; }
.brand { display: inline-flex; align-items: center; gap: 0.45em; }
.brand .icon { width: 1.15em; height: 1.15em; color: var(--accent); }
.modal-close { display: inline-flex; align-items: center; justify-content: center; }
.modal-close .icon { width: 1.1em; height: 1.1em; }
.key-reveal-card strong { display: inline-flex; align-items: center; gap: 0.4em; }

/* Health pill dot (replaces the unicode bullet). */
.pill { display: inline-flex; align-items: center; gap: 0.45em; }
.hdot { width: 8px; height: 8px; border-radius: 50%; background: var(--dim); flex: 0 0 auto; }
.pill.ok .hdot { background: var(--green); }
.pill.bad .hdot { background: var(--red); }

/* Chart: canvas must fill the container (default canvas is inline/300px). */
.chart-wrap { position: relative; }
#chart { display: block; width: 100%; height: 180px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 0.4rem 1rem; font-size: 0.8rem; color: var(--dim); margin-top: 0.6rem; }
.chart-legend .split-leg { display: inline-flex; align-items: center; }
.chart-tip .tip-row { display: flex; align-items: center; gap: 0.35rem; white-space: nowrap; }
.chart-tip .dot, .chart-legend .dot { width: 9px; height: 9px; border-radius: 2px; display: inline-block; margin-right: 4px; flex: 0 0 auto; }
.chart-tip {
  position: absolute; display: none; pointer-events: none; z-index: 5;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 0.35rem 0.55rem; font-size: 0.75rem; white-space: nowrap;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
}

/* Small inline number input (per-account threshold) */
.input.mini { width: 3.4rem; padding: 0.1rem 0.35rem; font-size: 0.75rem; text-align: center; }
.profile .sub .input.mini { margin: 0 2px; }

/* ---- Routing split slider ---- */
.routing { margin-bottom: 0.5rem; }
.split-track { position: relative; height: 28px; margin: 0.4rem 0 0.2rem; }
.split-bar { position: absolute; inset: 0; border-radius: 8px; overflow: hidden; background: var(--panel-2); }
.split-seg { position: absolute; top: 0; bottom: 0; opacity: 0.85; }
.split-handle { position: absolute; top: -4px; bottom: -4px; width: 16px; transform: translateX(-50%); cursor: ew-resize; z-index: 2; }
.split-handle::before {
  content: ""; position: absolute; left: 50%; top: 0; bottom: 0; width: 3px;
  transform: translateX(-50%); background: var(--text); border-radius: 3px;
  box-shadow: 0 0 0 2px var(--bg);
}
.split-handle:focus-visible { outline: none; }
.split-handle:focus-visible::before { background: var(--accent); box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.split-legend { display: flex; flex-wrap: wrap; gap: 0.4rem 1rem; font-size: 0.8rem; color: var(--dim); }
.split-leg { display: inline-flex; align-items: center; }
.split-leg b { color: var(--text); margin-left: 0.3rem; }
.split-leg .dot { width: 9px; height: 9px; border-radius: 2px; display: inline-block; margin-right: 5px; }

/* ---- Site-wide failure banner (full-width strip under the top bar) ---- */
.banner {
  background: #3a1a24; border-bottom: 1px solid #5a2836; color: #fecdd3;
  padding: 0.6rem 1.2rem; font-size: 0.85rem;
  display: flex; flex-direction: column; gap: 0.25rem;
}
.banner[hidden] { display: none; }
.banner .icon { width: 15px; height: 15px; color: var(--red); vertical-align: -2px; margin-right: 0.3rem; }
.banner a { color: var(--accent); font-weight: 600; }

/* ---- Groups ---- */
/* Pill-style sub-tab bar reused by the Overview (usage-by-group) and the
   Accounts (routing per group) sections. */
.grouptabs { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0.2rem 0 1rem; }
.grouptab {
  padding: 0.32rem 0.8rem; font-size: 0.82rem; border-radius: 20px; cursor: pointer;
  background: var(--panel-2); color: var(--dim); border: 1px solid var(--border);
}
.grouptab:hover { color: var(--text); border-color: var(--accent); }
.grouptab.active { color: var(--bg); background: var(--accent); border-color: var(--accent); font-weight: 600; }
.grouptab.add { color: var(--accent); background: transparent; }
.grouptab.add:hover { background: var(--panel-2); }

.group-panel { margin-bottom: 0.4rem; }
.group-panel .group-head { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: center; justify-content: space-between; margin-bottom: 0.7rem; }
.group-members { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 0.4rem 0 0.9rem; }
.member-toggle {
  display: inline-flex; align-items: center; gap: 0.45rem; padding: 0.3rem 0.7rem;
  border-radius: 20px; border: 1px solid var(--border); background: var(--panel-2);
  font-size: 0.82rem; cursor: pointer; user-select: none;
}
.member-toggle input { accent-color: var(--accent); margin: 0; }
.member-toggle.on { border-color: var(--accent); color: var(--text); }

/* Inline auto-reset checkbox in the account sub-line. */
.inline-toggle { display: inline-flex; align-items: center; gap: 0.35rem; cursor: pointer; }
.inline-toggle input { accent-color: var(--accent); margin: 0; }

/* Small group badges shown on each account row. */
.gchips { display: inline-flex; flex-wrap: wrap; gap: 0.3rem; margin-left: 0.1rem; }
.gchip { font-size: 0.68rem; padding: 1px 7px; border-radius: 20px; background: var(--panel-2); border: 1px solid var(--border); color: var(--dim); }
.gchip.default { border-style: dashed; }

/* ---- Add-account split button + dropdown ---- */
.add-account-split { position: relative; display: inline-flex; }
.add-account-split .btn.primary:first-child { border-top-right-radius: 0; border-bottom-right-radius: 0; border-right: 1px solid rgba(11, 14, 20, 0.35); }
.add-account-split .btn.caret {
  border-top-left-radius: 0; border-bottom-left-radius: 0; padding: 0.5rem 0.55rem;
  display: inline-flex; align-items: center;
}
.caret-glyph {
  width: 0; height: 0; display: block;
  border-left: 5px solid transparent; border-right: 5px solid transparent;
  border-top: 6px solid currentColor;
}
.dropdown-menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 60; min-width: 250px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45); padding: 0.3rem; display: flex; flex-direction: column;
}
.dropdown-menu[hidden] { display: none; }
.dropdown-item {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  background: none; border: none; border-radius: 8px; cursor: pointer;
  color: var(--text); text-align: left; padding: 0.55rem 0.7rem; font-size: 0.85rem;
}
.dropdown-item:hover { background: var(--panel); border-color: var(--accent); }
.dropdown-item .dim { font-size: 0.74rem; }

/* ---- Settings ---- */
.setting-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.2rem; }
.setting-title { font-weight: 600; }
#rotThreshold { width: 180px; accent-color: var(--accent); }

@media (max-width: 640px) {
  main { padding: 1rem; }
  .modal-actions { flex-direction: column; }
  .card .value { font-size: 1.35rem; }
  .setting-card .row.between { flex-direction: column; align-items: stretch; gap: 0.8rem; }
  #rotThreshold { width: 100%; }

  /* Stack list rows so sublines get full width instead of fragmenting
     beside the action buttons. */
  .profile, .key-item { flex-direction: column; align-items: stretch; gap: 0.7rem; }
  .profile .actions, .key-item .actions { justify-content: flex-end; }
  .account-quota { max-width: none; }

  /* Comfortable touch targets on mobile (44px min per the touch rule). */
  .btn { min-height: 44px; }
  .btn.small { min-height: 40px; padding: 0.45rem 0.8rem; font-size: 0.82rem; }
  .tab { padding: 0.85rem 1rem; }
  .modal-close { min-width: 44px; min-height: 44px; }

  /* Dropdown menu opens with room on narrow screens. */
  .dropdown-menu { min-width: 220px; }
}
