:root {
  --bg: #0f141b;
  --panel: #151b24;
  --panel-soft: #1b2330;
  --border: #2b3544;
  --border-strong: #3a4658;
  --text: #edf2f7;
  --muted: #9aa8ba;
  --quiet: #9aa8ba;
  --primary: #4d8dff;
  --primary-strong: #2f72e8;
  --good: #36c275;
  --warn: #f3b84a;
  --bad: #f06464;
  --shadow: 0 24px 70px rgba(0, 0, 0, .42);
  --radius: 10px;
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html,
body {
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

body {
  overflow: hidden;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel-soft);
  color: var(--text);
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: #222b38;
}

button:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: .55;
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: #111821;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 22px;
  line-height: 1.15;
  letter-spacing: 0;
}

h2 {
  font-size: 16px;
  line-height: 1.25;
  letter-spacing: 0;
}

.eyebrow {
  color: var(--quiet);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.runtime {
  display: flex;
  align-items: center;
  gap: 8px;
}

.health-pill {
  display: grid;
  grid-template-columns: 8px auto auto;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel);
  white-space: nowrap;
}

.health-pill span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
}

.health-pill em {
  color: var(--muted);
  font-style: normal;
}

.health-pill[data-state="online"] span,
.status-dot[data-state="online"] { background: var(--good); }
.health-pill[data-state="error"] span,
.status-dot[data-state="error"],
.status-dot[data-state="proxy-error"] { background: var(--bad); }
.health-pill[data-state="pending"] span,
.status-dot[data-state="connecting"],
.status-dot[data-state="login-required"] { background: var(--warn); }
.status-dot[data-state="stopped"],
.status-dot[data-state="disabled"] { background: var(--quiet); }

.workspace {
  display: grid;
  grid-template-columns: minmax(260px, .85fr) minmax(290px, 1fr) minmax(360px, 1.65fr);
  min-height: 0;
}

.pane {
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--border);
  background: var(--panel);
}

.conversation-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  border-right: 0;
}

.pane-header,
.conversation-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.icon-button,
.icon-text {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.icon-button {
  width: 40px;
  padding: 0;
}

.primary {
  border-color: transparent;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
}

.primary:hover:not(:disabled) {
  border-color: transparent;
  background: var(--primary-strong);
}

.secondary {
  background: transparent;
}

.danger {
  border-color: rgba(240, 100, 100, .45);
  background: rgba(240, 100, 100, .12);
  color: #ffb7b7;
}

.status-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.status-grid div {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #111821;
}

.status-grid strong {
  display: block;
  font-size: 20px;
  line-height: 1;
}

.status-grid span {
  color: var(--muted);
  font-size: 12px;
}

.account-list,
.chat-list {
  overflow: auto;
  min-height: 0;
  padding: 8px;
}



.account-card,
.chat-row {
  width: 100%;
  margin: 0 0 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #121923;
  color: inherit;
  text-align: left;
}

.account-card.is-selected,
.chat-row.is-selected {
  border-color: var(--primary);
  background: #172338;
}

.account-main,
.chat-main {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.account-title,
.chat-title {
  overflow: hidden;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-meta,
.chat-meta,
.chat-preview,
.message-meta,
.form-note,
.composer-actions p,
.empty-state {
  color: var(--muted);
  font-size: 12px;
}

.account-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin-top: 10px;
}

.account-actions button {
  min-height: 34px;
}

.status-line,
.badge-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  max-width: 100%;
  padding: 2px 7px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
}

.badge.unread {
  border-color: rgba(77, 141, 255, .45);
  color: #cfe0ff;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #111821;
}

.search-box input {
  width: 100%;
  min-height: 38px;
  border: 0;
  background: transparent;
  color: var(--text);
  outline: 0;
}

.filter-row {
  display: flex;
  gap: 6px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.segmented {
  min-height: 32px;
  padding: 0 10px;
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
}

.segmented.is-active {
  border-color: var(--primary);
  color: var(--text);
}

.chat-preview {
  overflow: hidden;
  margin-top: 6px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
  padding: 16px;
}

.message {
  max-width: min(72ch, 78%);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #121923;
}

.message.mine {
  align-self: flex-end;
  border-color: rgba(77, 141, 255, .38);
  background: #17315a;
}

.message-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message-meta {
  margin-top: 6px;
}

.message-attachments {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.attachment-link {
  display: grid;
  gap: 5px;
  color: #cfe0ff;
  font-size: 12px;
  overflow-wrap: anywhere;
}

.attachment-link img {
  display: block;
  width: min(320px, 100%);
  max-height: 260px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  object-fit: contain;
  background: #0f141b;
}

.composer {
  padding: 12px;
  border-top: 1px solid var(--border);
  background: #111821;
}

textarea,
input,
select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #0f141b;
  color: var(--text);
}

textarea {
  min-height: 86px;
  padding: 10px;
  resize: vertical;
}

input,
select {
  min-height: 40px;
  padding: 0 10px;
}

.composer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
}

.composer-buttons {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

.payment-preview {
  display: grid;
  gap: 8px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #111821;
}

.payment-preview p:not(.eyebrow) {
  color: var(--muted);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(4, 8, 13, .7);
  z-index: 20;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  width: min(760px, 100%);
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.modal-header,
.modal-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.modal-actions {
  border-top: 1px solid var(--border);
  border-bottom: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px;
  padding: 14px;
}

.form-grid label {
  display: grid;
  gap: 6px;
}

.form-grid label span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.form-grid .full {
  grid-column: 1 / -1;
}

.toggle-row {
  align-content: end;
  grid-template-columns: 20px 1fr;
  align-items: center;
  min-height: 64px;
}

.toggle-row input {
  width: 18px;
  min-height: 18px;
}

.spacer {
  flex: 1;
}

.toast-region {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: grid;
  gap: 8px;
  width: min(380px, calc(100vw - 32px));
  z-index: 30;
}

.toast {
  padding: 12px;
  border: 1px solid var(--border-strong);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius);
  background: #151b24;
  box-shadow: var(--shadow);
}

.toast.error {
  border-left-color: var(--bad);
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: 160px;
  padding: 24px;
  text-align: center;
}

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

@media (prefers-reduced-motion: no-preference) {
  button,
  .account-card,
  .chat-row,
  .toast {
    transition: background .14s ease, border-color .14s ease, opacity .14s ease, transform .14s ease;
  }

  .toast {
    animation: toast-in .16s ease-out;
  }

  @keyframes toast-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
  }
}

/* Flexible panel tracks keep scrollable content inside the available viewport. */
[hidden] { display: none !important; }
button { padding: 10px 14px; min-height: 44px; line-height: 1.35; overflow-wrap: anywhere; }
svg, .status-dot { flex-shrink: 0; }
input, textarea, select, .form-grid > *, .pane-header > *, .conversation-header > *,
.account-main > div, .chat-main > div, .composer-actions > * { min-width: 0; }
h2, .account-meta, .chat-meta, .message-meta, .composer-hint, .form-note,
.payment-preview, .toast { overflow-wrap: anywhere; }
.topbar { padding: 18px 24px; gap: 12px; }
.topbar .eyebrow { margin-bottom: 5px; color: #9dbbff; }
.workspace { padding: 16px; gap: 12px; }
.pane { border: 1px solid var(--border); border-radius: 16px; overflow: hidden; }
.account-pane, .chat-pane { display: grid; grid-template-rows: auto auto minmax(0, 1fr); }
.pane-header { padding: 18px 16px; }
.pane-header .eyebrow, .conversation-header .eyebrow { margin-bottom: 6px; }
.chat-pane .pane-header { flex-direction: column; align-items: stretch; }
.search-box:focus-within { border-color: var(--primary); box-shadow: 0 0 0 2px #4d8dff30; }
.search-box input { min-width: 0; }
.icon-button { flex: 0 0 44px; width: 44px; }
.account-list, .chat-list { padding: 10px; scrollbar-gutter: stable; overscroll-behavior: contain; }
.account-card, .chat-row { padding: 14px; margin-bottom: 10px; }
.account-actions button { min-height: 44px; padding: 8px 6px; font-size: 13px; }
.account-actions [data-action="select"] { grid-column: span 2; color: #cfe0ff; background: #203554; }
.badge { white-space: normal; overflow-wrap: anywhere; }
.account-main > .badge, .chat-main > .badge { flex-shrink: 0; }
.filter-row { flex-wrap: wrap; padding: 10px; }
.segmented { min-height: 44px; padding: 8px 12px; }
.segmented.is-active { background: #203554; }
.conversation-header { flex-wrap: wrap; padding: 18px; }
.conversation-header > .conversation-heading { flex: 1 1 100%; }
.conversation-header .badge-row { margin-top: 0; }
.message-list { min-height: 0; overscroll-behavior: contain; background: #111821; }
.message { flex-shrink: 0; min-width: 0; border-radius: 14px 14px 14px 4px; line-height: 1.6; }
.message.mine { border-radius: 14px 14px 4px 14px; }
.message-list > .empty-state { margin: auto; max-width: 360px; line-height: 1.8; }
.composer { padding: 16px; }
.composer textarea { display: block; resize: none; min-height: 76px; max-height: 160px; }
.composer-actions { flex-wrap: wrap; }
.composer-actions p { flex-basis: 100%; overflow-wrap: anywhere; line-height: 1.5; }
.composer-buttons { width: 100%; }
#sendButton { margin-left: auto; min-width: 88px; }
.empty-state { line-height: 1.7; }
.modal-header, .modal-actions { position: sticky; background: var(--panel); z-index: 1; flex-wrap: wrap; }
.modal-header { top: 0; }
.modal-actions { bottom: 0; }
.modal-header > div { min-width: 0; flex: 1; }
.form-grid { padding: 20px; gap: 18px; }
.form-grid input, .form-grid select { min-height: 44px; }
.view-nav { display: none; }
.back-button { display: none; }

@media (max-width: 1100px) {
  .app-shell { grid-template-rows: auto auto minmax(0, 1fr); }
  .view-nav { display: flex; gap: 6px; padding: 10px 16px 0; }
  .view-nav button { background: transparent; color: var(--muted); }
  .view-nav button[aria-pressed="true"] { background: #203554; color: var(--text); border-color: var(--primary); }
  .workspace { grid-template-columns: minmax(260px, .85fr) minmax(0, 1.4fr); }
  .account-pane { display: none; }
  .app-shell[data-view="accounts"] .account-pane { display: grid; }
  .app-shell[data-view="accounts"] .chat-pane { display: none; }
}

@media (max-width: 760px) {
  .topbar { padding: 12px 16px; flex-wrap: wrap; }
  h1 { font-size: 20px; }
  .runtime { gap: 6px; flex-wrap: wrap; }
  .health-pill { min-height: 30px; padding: 4px 8px; gap: 5px; font-size: 11px; }
  .health-pill em { max-width: 90px; overflow: hidden; text-overflow: ellipsis; }
  .view-nav { padding: 8px 10px; border-bottom: 1px solid var(--border); }
  .view-nav button { flex: 1; }
  .workspace { display: grid; grid-template-columns: minmax(0, 1fr); padding: 0; gap: 0; }
  .pane { border: 0; border-radius: 0; }
  .app-shell .workspace > .pane { display: none; }
  .app-shell[data-view="accounts"] .account-pane,
  .app-shell[data-view="chats"] .chat-pane,
  .app-shell[data-view="conversation"] .conversation-pane { display: grid; }
  .pane-header, .conversation-header { padding: 12px 16px; }
  .chat-pane .pane-header { gap: 10px; }
  .conversation-header { gap: 6px; }
  .conversation-header > .conversation-heading { flex-basis: calc(100% - 60px); }
  .back-button { display: inline-flex; }
  .conversation-header .badge-row { max-height: 55px; overflow: auto; }
  .message { max-width: 92%; }
  .composer { padding: 10px 12px max(10px, env(safe-area-inset-bottom)); }
  .composer textarea { min-height: 64px; height: 64px; }
  .composer-buttons { gap: 6px; }
  .composer-buttons button { padding: 10px; }
  #sendButton { min-width: 72px; }
  input, textarea, select { font-size: 16px; }
  .form-grid { grid-template-columns: minmax(0, 1fr); padding: 16px; }
  .modal-backdrop { padding: 8px; }
  .modal { max-height: calc(100dvh - 16px); }
  .modal-actions { gap: 8px; padding: 12px; }
  .toast-region { bottom: max(16px, env(safe-area-inset-bottom)); }
}
@media (max-height: 500px) and (max-width: 1100px) {
  .topbar { padding: 6px 12px; }
  .topbar .eyebrow, .conversation-header .badge-row { display: none; }
  .view-nav { padding-top: 4px; padding-bottom: 4px; }
  .conversation-header { padding: 6px 12px; }
  .composer textarea { height: 48px; min-height: 48px; }
  .composer-actions { margin-top: 4px; gap: 4px; }
}
/* Keep essential conversation controls visible even with very long account names. */
.conversation-header h2, .composer-actions p {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
@media (max-height: 500px) and (max-width: 1100px) {
  .conversation-header h2, .composer-actions p { -webkit-line-clamp: 1; }
}

.conversation-header .badge { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conversation-header .badge-row { width: 100%; }
.conversation-pane > * { min-width: 0; }

.auth-gate { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 24px; padding: 32px 16px; overflow: auto; }
.auth-intro { text-align: center; display: grid; gap: 12px; max-width: 400px; }
.auth-intro h1 { font-size: 28px; }
#authStatus { color: var(--muted); line-height: 1.7; }
#signIn { max-width: 100%; }
body:has(.app-shell[hidden]) { overflow: auto; }
.signed-in-user { font-size: 12px; color: var(--muted); max-width: 120px; overflow: hidden; text-overflow: ellipsis; }

.node-status { padding: 8px 16px; font-size: 12px; overflow-wrap: anywhere; color: var(--muted); }
select { width: 100%; min-width: 0; padding: 10px; background: #111827; color: #e5e7eb; border: 1px solid #374151; border-radius: 8px; }
