:root {
  color-scheme: dark;
  --bg: #07111f;
  --panel: #0e1b2c;
  --panel-2: #13243a;
  --text: #e8f1ff;
  --muted: #8fa6c3;
  --accent: #5ee7ff;
  --accent-2: #8f7aff;
  --danger: #ff6b8a;
  --ok: #51d88a;
  --line: #28496f;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; }
body { margin: 0; background: radial-gradient(circle at top left, #122b4a, var(--bg) 45%); color: var(--text); }
button, textarea, input, select { font: inherit; }
button { border: 0; border-radius: 14px; padding: 12px 16px; color: #04101c; background: linear-gradient(135deg, var(--accent), var(--accent-2)); font-weight: 750; cursor: pointer; }
button.secondary { background: #1d3350; color: var(--text); border: 1px solid #2b4b72; }
button.danger { background: linear-gradient(135deg, var(--danger), #ffb86b); }
button.danger-outline { color: var(--danger); border-color: rgba(255,107,138,.55); }
button:disabled { opacity: .45; cursor: not-allowed; }
textarea, input, select { width: 100%; border: 1px solid var(--line); border-radius: 14px; background: #081525; color: var(--text); padding: 12px; min-width: 0; }
textarea { resize: vertical; }
label { display: block; margin: 18px 0 8px; color: var(--muted); font-weight: 650; }

.login-screen { height: 100dvh; overflow: auto; display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at top left, #183862, var(--bg) 50%); }
.login-screen[hidden], .app-shell[hidden] { display: none; }
.login-card { width: min(440px, 100%); border: 1px solid #27476b; background: rgba(7, 17, 31, .9); border-radius: 28px; padding: 28px; box-shadow: 0 30px 80px rgba(0,0,0,.35); }
.login-brand { margin-bottom: 18px; }
.login-error { min-height: 20px; color: var(--danger); font-weight: 700; margin: 12px 0 0; }
.login-note { font-size: 13px; margin: 14px 0 0; }

.app-shell {
  height: 100dvh;
  min-height: 0;
  width: 100%;
  overflow: hidden;
  display: grid;
  grid-template-columns: auto 360px minmax(420px, 1fr) 410px;
  gap: 1px;
  background: rgba(255,255,255,.08);
}
.tool-rail, .chat-pane, .conversation, .agent-panel {
  min-width: 0;
  min-height: 0;
  background: rgba(7, 17, 31, .86);
  backdrop-filter: blur(16px);
}
.tool-rail {
  width: 208px;
  height: 100dvh;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: width .18s ease;
  overflow: hidden;
}
.tool-rail.collapsed { width: 76px; }
.rail-toggle, .tool-item { width: 100%; min-height: 46px; display: flex; align-items: center; gap: 12px; justify-content: flex-start; background: transparent; color: var(--text); border: 1px solid transparent; overflow: hidden; }
.rail-toggle { justify-content: center; background: #10213a; border-color: #203c61; flex: 0 0 auto; }
.rail-brand { display: flex; align-items: center; gap: 12px; margin: 8px 0 16px; white-space: nowrap; flex: 0 0 auto; }
.tool-icon { width: 28px; height: 28px; display: grid; place-items: center; text-align: center; font-size: 20px; flex: 0 0 28px; }
.whatsapp-icon svg { width: 28px; height: 28px; display: block; }
.whatsapp-icon .wa-bg { fill: #25d366; }
.whatsapp-icon .wa-phone { fill: #07111f; }
.tool-label, .rail-brand strong { white-space: nowrap; }
.tool-rail.collapsed .tool-label, .tool-rail.collapsed .rail-brand strong { display: none; }
.tool-item.active { background: linear-gradient(135deg, rgba(94,231,255,.18), rgba(143,122,255,.14)); border-color: rgba(94,231,255,.35); }
.tool-item.disabled { opacity: .45; cursor: not-allowed; }
.rail-spacer { flex: 1 1 auto; min-height: 0; }

.chat-pane {
  height: 100dvh;
  padding: 22px;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto auto auto auto minmax(0, 1fr);
  gap: 0;
}
.agent-panel {
  height: 100dvh;
  padding: 22px;
  overflow-y: auto;
  overflow-x: hidden;
  border-left: 1px solid rgba(255,255,255,.08);
}
.brand { display: flex; gap: 14px; align-items: center; margin-bottom: 24px; }
.brand-mark { width: 50px; height: 50px; display: grid; place-items: center; border-radius: 18px; background: linear-gradient(135deg, #243bff, #5ee7ff); font-size: 25px; font-weight: 900; flex: 0 0 auto; }
h1, h2, p { margin-top: 0; }
h1 { font-size: 22px; margin-bottom: 4px; }
p { color: var(--muted); line-height: 1.45; }
.eyebrow { color: var(--accent); text-transform: uppercase; letter-spacing: .12em; font-size: 11px; font-weight: 800; }
.pane-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; min-width: 0; }
.live-pill { color: var(--ok); background: rgba(81,216,138,.12); border: 1px solid rgba(81,216,138,.32); border-radius: 999px; padding: 6px 10px; font-size: 12px; white-space: nowrap; }
.pill { border: 1px solid #2b4b72; color: var(--muted); border-radius: 999px; padding: 6px 10px; font-size: 12px; white-space: nowrap; }
.chat-controls { margin: 0 0 12px; border: 1px solid rgba(40,73,111,.5); border-radius: 18px; padding: 12px; background: rgba(19,36,58,.36); overflow: hidden; }
.chat-controls label { margin-top: 10px; }
.filter-row { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; min-width: 0; }
.inline-filter { display: flex; align-items: center; gap: 8px; margin: 10px 0 12px; color: var(--muted); min-width: 0; }
.inline-filter input { width: auto; flex: 0 0 auto; }
.runtime-status, .draft-meta { margin: 0 0 12px; padding: 12px; border: 1px solid #244361; background: rgba(19,36,58,.72); color: var(--muted); border-radius: 14px; font-size: 13px; line-height: 1.45; overflow-wrap: anywhere; }
.pane-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; min-width: 0; }
.chat-list { min-height: 0; overflow-y: auto; overflow-x: hidden; display: grid; align-content: start; gap: 10px; padding-right: 4px; overscroll-behavior: contain; }
.chat-card { position: relative; text-align: left; background: var(--panel); color: var(--text); border: 1px solid #243d5e; display: grid; grid-template-columns: 44px minmax(0, 1fr); gap: 12px; align-items: center; width: 100%; min-width: 0; transition: transform .15s ease, border-color .15s ease, opacity .15s ease; }
.chat-card:hover { transform: translateY(-1px); border-color: #3d6d9f; }
.chat-card.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(94,231,255,.18); }
.chat-card.ignored { opacity: .55; }
.chat-card-copy { min-width: 0; display: block; }
.chat-card strong { display: block; margin-bottom: 5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-right: 30px; }
.chat-card em { color: var(--danger); font-size: 10px; font-style: normal; text-transform: uppercase; letter-spacing: .08em; margin-left: 4px; }
.chat-card small { color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { position: absolute; right: 10px; top: 10px; min-width: 24px; border-radius: 999px; background: var(--accent); color: #04101c; text-align: center; padding: 2px 7px; }
.avatar { width: 42px; height: 42px; border-radius: 15px; display: grid; place-items: center; background: linear-gradient(135deg, #1d3350, #294b77); border: 1px solid #345d8b; color: var(--text); font-weight: 850; object-fit: cover; flex: 0 0 auto; }
.avatar-large { width: 54px; height: 54px; border-radius: 18px; font-size: 18px; }

.conversation { height: 100dvh; display: grid; grid-template-rows: auto minmax(0, 1fr); padding: 22px 24px; overflow: hidden; }
.conversation-header { min-width: 0; display: flex; align-items: center; justify-content: space-between; gap: 14px; border-bottom: 1px solid #243d5e; padding-bottom: 18px; overflow: hidden; }
.conversation-title { display: flex; align-items: center; gap: 14px; min-width: 0; overflow: hidden; }
.conversation-title h2 { margin-bottom: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-title small { color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; flex: 0 0 auto; }
.chat-actions button { padding: 8px 10px; border-radius: 10px; font-size: 12px; }
.message-thread { min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 22px 4px 22px 0; display: flex; flex-direction: column; gap: 12px; overscroll-behavior: contain; }
.message { max-width: min(76%, 720px); min-width: 0; padding: 12px 14px; border-radius: 18px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; }
.message.inbound { align-self: flex-start; background: var(--panel); border-top-left-radius: 4px; }
.message.outbound { align-self: flex-end; background: linear-gradient(135deg, #173a4f, #243168); border-top-right-radius: 4px; }
.sender-label { color: var(--accent); font-size: 12px; font-weight: 800; margin-bottom: 6px; }
.message time { display: block; color: var(--muted); font-size: 11px; margin-top: 8px; }
.media-label { display: inline-block; margin: 0 8px 6px 0; border: 1px solid var(--line); border-radius: 999px; padding: 2px 8px; color: var(--accent); font-size: 11px; text-transform: uppercase; }
.transcription { margin: 10px 0 0; color: var(--text); border-left: 3px solid var(--accent); padding-left: 10px; }
.inline-action { margin-top: 10px; padding: 8px 10px; border-radius: 10px; font-size: 12px; }
.empty-state { color: var(--muted); border: 1px dashed var(--line); border-radius: 18px; padding: 24px; text-align: center; }
.empty-state.compact { padding: 14px; font-size: 13px; }

.settings-card { margin: 14px 0 18px; border: 1px solid #244361; background: rgba(19,36,58,.5); border-radius: 18px; padding: 14px; overflow: hidden; }
.agent-overview { display: grid; gap: 14px; width: 100%; }
.agent-facts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 12px; }
.agent-facts div { border: 1px solid #244361; border-radius: 14px; padding: 10px; background: rgba(8,21,37,.55); min-width: 0; }
.agent-facts strong, .agent-facts span { display: block; overflow-wrap: anywhere; }
.agent-facts strong { color: var(--accent); font-size: 12px; text-transform: uppercase; letter-spacing: .08em; margin-bottom: 4px; }
.style-guide-preview { max-height: 48dvh; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; border: 1px solid #244361; border-radius: 14px; background: #081525; padding: 14px; color: var(--text); }
code { color: var(--accent); }
.settings-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.settings-grid label { margin-top: 8px; font-size: 12px; min-width: 0; }
.settings-grid input { margin-top: 6px; padding: 9px; }
#save-chat-settings { width: 100%; margin-top: 10px; }
.audit-panel { margin-top: 18px; }
.audit-list { margin-top: 10px; display: grid; gap: 8px; max-height: 210px; overflow: auto; overscroll-behavior: contain; }
.audit-event { border: 1px solid #244361; background: rgba(19,36,58,.55); border-radius: 14px; padding: 10px 12px; overflow: hidden; }
.audit-event strong, .audit-event small { display: block; overflow-wrap: anywhere; }
.audit-event small { color: var(--muted); margin-top: 4px; font-size: 12px; }
.safety-note { margin-top: 16px; color: var(--ok); font-size: 13px; }

@media (max-width: 1180px) {
  html, body { overflow: auto; }
  .app-shell { height: auto; min-height: 100dvh; overflow: visible; grid-template-columns: 76px 330px minmax(0, 1fr); }
  .tool-rail, .chat-pane, .conversation { height: 100dvh; }
  .agent-panel { grid-column: 2 / -1; height: min(70dvh, 720px); border-left: 0; border-top: 1px solid rgba(255,255,255,.08); }
}
@media (max-width: 820px) {
  html, body { overflow: auto; }
  .app-shell { grid-template-columns: 1fr; height: auto; overflow: visible; }
  .tool-rail { width: auto; height: auto; flex-direction: row; }
  .tool-rail.collapsed { width: auto; }
  .tool-label, .rail-brand strong { display: none; }
  .chat-pane { height: 55dvh; }
  .conversation { height: 65dvh; }
  .agent-panel { grid-column: auto; height: auto; max-height: none; }
  .settings-grid, .filter-row { grid-template-columns: 1fr; }
}
