:root {
  color-scheme: light;
  --bg: #edf1f5;
  --surface: #ffffff;
  --surface-soft: #f5f7fa;
  --ink: #17212f;
  --muted: #5a6878;
  --line: #c8d1dd;
  --green: #127457;
  --blue: #2959a8;
  --amber: #9b6918;
  --red: #ad3537;
  --shadow: 0 14px 34px rgba(33, 45, 61, 0.10);
  --soft-shadow: 0 6px 18px rgba(33, 45, 61, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: linear-gradient(180deg, #f9fbfd 0, var(--bg) 320px), var(--bg);
  color: var(--ink);
  font: 14px/1.5 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

h1, h2, h3, p {
  margin: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.chat-app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 14px;
  width: min(1680px, calc(100vw - 28px));
  height: 100vh;
  margin: 0 auto;
  padding: 14px 0 16px;
}

/* Topbar ------------------------------------------------------------------ */

.chat-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 12px 16px;
  border: 1px solid rgba(200, 209, 221, 0.92);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.86);
  box-shadow: var(--soft-shadow);
}

.chat-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.eyebrow {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

h1 {
  font-size: 17px;
  font-weight: 650;
}

.chat-nav {
  display: flex;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

.chat-nav a {
  padding: 6px 14px;
  border-radius: 6px;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
}

.chat-nav a.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--soft-shadow);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.session-total {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  color: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.session-total b {
  color: var(--ink);
  font-size: 13px;
}

button {
  border: 1px solid var(--ink);
  border-radius: 7px;
  background: var(--ink);
  color: #fff;
  font: inherit;
  font-weight: 600;
  padding: 8px 16px;
  cursor: pointer;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.ghost-button {
  border-color: var(--line);
  background: var(--surface);
  color: var(--ink);
  font-weight: 550;
  padding: 7px 13px;
}

.ghost-button:hover {
  border-color: var(--ink);
}

/* Panes ------------------------------------------------------------------- */

.pane-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  min-height: 0;
}

.agent-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--soft-shadow);
  overflow: hidden;
}

.pane-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-soft);
}

.pane-head h2 {
  font-size: 14px;
  font-weight: 650;
}

.pane-model {
  color: var(--muted);
  font-size: 11px;
  word-break: break-word;
}

.pane-totals {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  color: var(--muted);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.pane-totals b {
  color: var(--ink);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
}

.pane-totals .free {
  color: var(--green);
}

.pane-body {
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.agent-pane[data-agent="cascade"] .brand-dot { background: var(--green); }
.agent-pane[data-agent="openai"] .brand-dot { background: var(--blue); }
.agent-pane[data-agent="claude"] .brand-dot { background: var(--amber); }

.brand-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 7px;
  border-radius: 50%;
  vertical-align: middle;
}

/* Messages ---------------------------------------------------------------- */

.msg {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg.user {
  align-items: flex-end;
}

.msg.user .bubble {
  max-width: 88%;
  border-radius: 12px 12px 3px 12px;
  background: var(--ink);
  color: #fff;
}

.msg.agent .bubble {
  border: 1px solid var(--line);
  border-radius: 12px 12px 12px 3px;
  background: var(--surface-soft);
}

.msg.agent.error .bubble {
  border-color: rgba(173, 53, 55, 0.4);
  background: rgba(173, 53, 55, 0.06);
  color: var(--red);
}

.bubble {
  padding: 10px 13px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.bubble p + p {
  margin-top: 8px;
}

.msg-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  font-size: 10.5px;
  font-variant-numeric: tabular-nums;
}

.msg-meta span {
  padding: 2px 7px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
}

.msg-meta span b {
  color: var(--ink);
  font-weight: 650;
}

.msg-meta .cost-free {
  border-color: rgba(18, 116, 87, 0.35);
  background: rgba(18, 116, 87, 0.08);
  color: var(--green);
}

.msg-meta .cost-free b {
  color: var(--green);
}

.msg-meta .escalated {
  border-color: rgba(155, 105, 24, 0.4);
  background: rgba(155, 105, 24, 0.09);
  color: var(--amber);
}

.msg-meta .escalated b {
  color: var(--amber);
}

.route-trace {
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface);
}

.route-trace > summary {
  padding: 7px 11px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}

.route-trace[open] > summary {
  border-bottom: 1px solid var(--line);
}

.route-trace ol {
  margin: 0;
  padding: 8px 11px 10px 26px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.route-trace li {
  font-size: 11.5px;
}

.route-trace li b {
  display: block;
}

.route-trace li small {
  color: var(--muted);
}

.route-chip {
  display: inline-block;
  margin-right: 6px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.route-chip.local { background: rgba(18, 116, 87, 0.12); color: var(--green); }
.route-chip.validate { background: rgba(41, 89, 168, 0.12); color: var(--blue); }
.route-chip.escalate { background: rgba(155, 105, 24, 0.14); color: var(--amber); }

.pane-empty {
  margin: auto 0;
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  color: var(--muted);
  text-align: center;
  font-size: 12.5px;
}

.thinking {
  display: flex;
  gap: 5px;
  padding: 12px 13px;
}

.thinking i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: pulse 1.1s ease-in-out infinite;
}

.thinking i:nth-child(2) { animation-delay: 0.16s; }
.thinking i:nth-child(3) { animation-delay: 0.32s; }

@keyframes pulse {
  0%, 100% { opacity: 0.25; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(-2px); }
}

/* Composer ---------------------------------------------------------------- */

.composer {
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.prompt-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.prompt-chip {
  padding: 5px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 11.5px;
  font-weight: 500;
}

.prompt-chip:hover {
  border-color: var(--ink);
  color: var(--ink);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

textarea,
input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--ink);
  font: inherit;
  resize: none;
  max-height: 180px;
}

textarea:focus,
input[type="password"]:focus {
  outline: 2px solid rgba(41, 89, 168, 0.35);
  outline-offset: 1px;
}

#send-button {
  flex: none;
  padding: 10px 22px;
}

.composer-hint {
  color: var(--muted);
  font-size: 11px;
}

.composer-hint.error {
  color: var(--red);
}

/* Modal ------------------------------------------------------------------- */

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(23, 33, 47, 0.45);
}

.modal[hidden] {
  display: none;
}

.modal-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(460px, 100%);
  padding: 20px;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.modal-card h2 {
  font-size: 16px;
}

.modal-card label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
}

.modal-card .checkbox-row {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-weight: 500;
}

.modal-card .checkbox-row input {
  width: auto;
}

.modal-note {
  color: var(--muted);
  font-size: 11.5px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

@media (max-width: 1100px) {
  .chat-app {
    height: auto;
    min-height: 100vh;
  }

  .pane-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .agent-pane {
    max-height: 70vh;
  }

  .chat-topbar {
    flex-wrap: wrap;
  }
}
