/* ---------------------------------------------------------------------------
   AI Conversations - styles

   One centred column holding the header, three stacked AI seats as an
   accordion, and the moderator underneath. 60% of the viewport on desktop,
   full width on mobile. Only one accordion panel is open at a time, and it is
   the seat that is speaking.
   --------------------------------------------------------------------------- */

:root {
  --bg: #101319;
  --panel: #181c25;
  --panel-2: #1f2430;
  --line: #2a3140;
  --text: #e6e9ef;
  --muted: #8f9aae;
  --accent: #5b8def;
  --ok: #3fb96b;
  --warn: #d9a53b;
  --bad: #e0565b;
  --radius: 10px;

  /* One place to change the column width. */
  --shell-width: 60%;

  /* Tells the browser this page is dark, so it renders NATIVE widgets - the
     dropdown popup, scrollbars, focus rings - in their dark variants. Without
     it the <select> popup is drawn by the OS in light mode while inheriting
     this page's light text colour, which is white text on a white list: the
     options are there, they are just invisible. */
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* The hidden attribute must win over every layout rule below.
 *
 * The browser's own [hidden]{display:none} lives in the UA stylesheet, so ANY
 * author rule setting display beats it - regardless of specificity. `.modal`
 * sets display:grid, which silently made hidden a no-op on both modals: they
 * rendered permanently, position:fixed and inset:0, over the whole app, with
 * the seat dialog on top because it is declared last. Its <select> was empty
 * because openSeatModal() had never run - and openSeatModal() is the only
 * caller of loadCatalog(), so the model list was never fetched either.
 *
 * !important here rather than a .modal[hidden] rule, so the same trap cannot
 * be reopened by any future component that toggles `hidden` and also sets
 * display. Toggle visibility with el.hidden and this always holds. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  padding-block: 18px;
}

/* The centred column. Everything the moderator looks at shares this width. */
.shell {
  width: var(--shell-width);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

h1 { font-size: 16px; margin: 0; font-weight: 600; }
h2 { font-size: 15px; margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.tiny { font-size: 12px; }
.spacer { flex: 1; }
.err { color: var(--bad); margin: 6px 0 0; font-size: 13px; }

/* --- buttons / inputs --- */
button {
  font: inherit;
  padding: 7px 13px;
  border-radius: 7px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button:hover { filter: brightness(1.1); }
button:disabled { opacity: .45; cursor: not-allowed; filter: none; }
button.ghost { background: transparent; border-color: var(--line); color: var(--text); }
button.ghost:hover { background: var(--panel-2); filter: none; }
button.danger { background: transparent; border-color: var(--bad); color: var(--bad); }
button.danger:hover { background: rgba(224,86,91,.12); filter: none; }
button.tiny { padding: 4px 9px; font-size: 12px; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 7px 9px;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { resize: vertical; }
label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; }

/* The dropdown popup is drawn outside the page, so it does not inherit the
   select's colours - each option needs them set explicitly or the list renders
   unreadably. Belt and braces alongside color-scheme above, because which of
   the two actually applies varies by browser and platform. */
select option,
select optgroup {
  background-color: var(--panel-2);
  color: var(--text);
}
select optgroup { font-weight: 600; color: var(--muted); }
select option { font-weight: 400; color: var(--text); padding: 4px; }
select option:disabled,
select optgroup:disabled { color: var(--muted); font-style: italic; }
#fModel { min-height: 34px; }

/* --- login gate --- */
.gate { min-height: 80vh; display: grid; place-items: center; }
.gate-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 26px; width: 320px;
}
.gate-card h1 { margin-bottom: 4px; }
.gate-card p { margin: 0 0 16px; font-size: 13px; }
.gate-card input { margin-bottom: 10px; }
.gate-card button { width: 100%; }

/* --- top bar --- */
.topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 11px 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.topbar-controls { display: flex; align-items: center; gap: 9px; margin-left: auto; flex-wrap: wrap; }
.inline { display: flex; align-items: center; gap: 6px; margin: 0; white-space: nowrap; font-size: 12.5px; }
.inline input[type="number"] { width: 62px; }
.inline input[type="checkbox"] { width: auto; }

.status-chip, .cost-chip {
  padding: 4px 10px; border-radius: 20px; font-size: 12px;
  border: 1px solid var(--line); background: var(--panel-2); white-space: nowrap;
}
.status-chip.running { border-color: var(--ok); color: var(--ok); }
.status-chip.waiting { border-color: var(--warn); color: var(--warn); }
.status-chip.paused  { border-color: var(--muted); color: var(--muted); }
.cost-chip { font-variant-numeric: tabular-nums; }

/* --- the accordion --- */
.accordion { display: flex; flex-direction: column; gap: 8px; }

.acc-item {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
/* The seat that is speaking, or has just spoken. */
.acc-item.is-next { border-color: var(--accent); }
.acc-item.is-off  { opacity: .55; }

/* Everything on ONE line: caret, position, name, model, three controls.
   `min-width: 0` on the flex children is what lets the model id ellipsis
   instead of forcing the row to overflow. */
.acc-head {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px;
  cursor: pointer;
  user-select: none;
  min-width: 0;
}
.acc-head:hover { background: var(--panel-2); }
.acc-head:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.acc-caret {
  color: var(--muted); font-size: 11px; flex: 0 0 auto;
  transition: transform .15s ease;
}
.acc-item.open .acc-caret { transform: rotate(90deg); }

.acc-order {
  flex: 0 0 auto; font-size: 11px; color: var(--muted);
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 4px; padding: 1px 7px;
}
.acc-order.open-source { border-color: var(--ok); color: var(--ok); }

.acc-name {
  flex: 0 1 auto; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.acc-model {
  flex: 1 1 auto; min-width: 0;
  font-size: 11.5px; color: var(--muted);
  font-family: ui-monospace, Menlo, Consolas, monospace;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
/* A seated model that is no longer in the catalogue. Every turn it takes will
   fail at the vendor, so it is worth saying before the round starts. */
.acc-model.model-gone { color: var(--bad); }

.acc-controls { display: flex; align-items: center; gap: 7px; flex: 0 0 auto; }
.switch { display: flex; align-items: center; gap: 5px; margin: 0; font-size: 12px; color: var(--muted); white-space: nowrap; }
.switch input { width: auto; }

/* Closed by default; the open one is set from JS. */
.acc-body { display: none; border-top: 1px solid var(--line); }
.acc-item.open .acc-body { display: block; }

.acc-foot {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; border-top: 1px solid var(--line);
}

/* --- chat areas --- */
.chat { overflow-y: auto; padding: 12px; max-height: 46vh; min-height: 120px; }
.chat .empty { color: var(--muted); font-size: 13px; text-align: center; margin-top: 20px; }

.msg { margin-bottom: 12px; }
.msg-head { display: flex; align-items: baseline; gap: 7px; margin-bottom: 3px; }
.msg-who { font-size: 12px; font-weight: 600; }
.msg-stats { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; margin-left: auto; }
.msg-body { white-space: pre-wrap; word-wrap: break-word; }

.msg.from-mod .msg-who { color: var(--accent); }
.msg.from-ai .msg-who { color: var(--text); }

/* A turn that was attempted and failed. Kept in the transcript on purpose -
   the moderator needs to see that a seat was asked and what came back. */
.msg.is-error .msg-body {
  color: var(--bad); background: rgba(224,86,91,.08);
  border-left: 2px solid var(--bad); padding: 6px 9px; border-radius: 4px;
  font-size: 13px;
}
.msg.is-pending .msg-body { color: var(--muted); font-style: italic; }

/* In a seat pane the moderator's turns are context for that seat's replies, so
   they are dimmed. The other two AIs' answers are not rendered here at all -
   see renderSeats(). They are still sent to the model; this is display only. */
.chat .msg.is-other { opacity: .62; }
.chat .msg.is-other .msg-body { font-size: 13px; }

/* --- moderator pane --- */
.moderator {
  display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden;
}
.mod-head {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 14px; border-bottom: 1px solid var(--line);
}
.mod-input { display: flex; gap: 9px; padding: 10px 14px; border-top: 1px solid var(--line); }
.mod-input textarea { flex: 1; }
.mod-send { display: flex; flex-direction: column; gap: 6px; align-items: stretch; }
.mod-send button { padding-inline: 20px; }
.mod-send .switch { justify-content: center; }

/* A web search note. Reference material rather than a voice in the room, so it
   is styled as a block of evidence and not as a chat message. */
.msg.is-note .msg-who { color: var(--warn); }
.msg.is-note .msg-body {
  background: var(--panel-2);
  border-left: 2px solid var(--warn);
  padding: 8px 10px;
  border-radius: 4px;
  font-size: 13px;
}

/* --- modals --- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: grid; place-items: center; padding: 20px; z-index: 50;
}
.modal-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  width: 100%; max-width: 620px; max-height: 88vh; overflow-y: auto; padding: 18px 20px 20px;
}
.modal-head { display: flex; align-items: center; margin-bottom: 12px; }
.modal-head h2 { margin-right: auto; }
.modal-foot { display: flex; justify-content: flex-end; margin-top: 16px; }
.field { margin-bottom: 14px; }
.field.narrow { max-width: 170px; }
.row { display: flex; gap: 14px; align-items: flex-start; }
.row .field { flex: 1; }
.hint { font-size: 12px; color: var(--muted); margin: 4px 0 0; }

/* --- empty state / first-run nudge --- */
.empty-state-title { font-weight: 600; margin: 0 0 6px; }
#seatNoKeysReasons { margin: 12px 0 0; padding-left: 18px; }
#seatNoKeysReasons li { margin-bottom: 3px; }

/* Points at the API keys button on a first run. Nothing works before a key is
   entered, so it is worth being loud about exactly once. */
.nudge {
  font-size: 12px; color: var(--warn); white-space: nowrap;
  animation: nudge-pulse 1.8s ease-in-out infinite;
}
@keyframes nudge-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .45; } }
@media (prefers-reduced-motion: reduce) { .nudge { animation: none; } }

button.needs-attention { border-color: var(--warn); color: var(--warn); }

/* --- key slots --- */
.key-row { border: 1px solid var(--line); border-radius: 8px; padding: 12px; margin-bottom: 10px; }
.key-row-head { display: flex; align-items: center; gap: 9px; margin-bottom: 9px; }
.key-row-head strong { margin-right: auto; }
.key-source { font-size: 11px; color: var(--muted); }
.key-controls { display: flex; gap: 8px; }
.key-controls input { flex: 1; }
.key-state { font-size: 12px; padding: 2px 8px; border-radius: 4px; border: 1px solid var(--line); }
.key-state.valid   { border-color: var(--ok);   color: var(--ok); }
.key-state.invalid { border-color: var(--bad);  color: var(--bad); }
.key-state.untested{ border-color: var(--warn); color: var(--warn); }
.key-state.missing { border-color: var(--line); color: var(--muted); }
.key-detail { font-size: 12px; color: var(--muted); margin: 7px 0 0; }

/* --- toast --- */
.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 16px; z-index: 100; max-width: 80vw;
}
.toast.bad { border-color: var(--bad); color: var(--bad); }

/* ---------------------------------------------------------------------------
   Narrow screens.

   Two breakpoints, because the accordion header and the column width fail at
   different sizes: the six items on one line run out of room well before 60%
   of the viewport stops being a sensible column.
   --------------------------------------------------------------------------- */

/* The header row gives up on one line before the layout does. */
@media (max-width: 1100px) {
  .acc-head { flex-wrap: wrap; row-gap: 6px; }
  .acc-model { flex-basis: 100%; order: 5; }
  .acc-controls { margin-left: auto; }
}

@media (max-width: 900px) {
  :root { --shell-width: 100%; }
  body { padding-block: 0; }
  .shell { gap: 0; }
  .topbar, .acc-item, .moderator { border-radius: 0; border-left: 0; border-right: 0; }
  .accordion { gap: 0; }
  .acc-item + .acc-item { border-top: 0; }
  .chat { max-height: 50vh; }
  .topbar-controls { margin-left: 0; }
  .acc-controls { flex-wrap: wrap; }
}
