:root {
  --bg: #f4f3f0;
  --surface: #ffffff;
  --surface-2: #f8f7f5;
  --border: #e3e0da;
  --text: #1d1c1a;
  --muted: #6d6a64;
  --accent: #e8452c;
  --accent-text: #ffffff;
  --accent-soft: #fde9e5;
  --bot: #eceae6;
  --team: #1f5f8b;
  --team-text: #ffffff;
  --ok: #1e7a46;
  --warn: #9a5b00;
  --warn-soft: #fff4e0;
  --danger: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 2px 8px rgba(0, 0, 0, 0.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141412;
    --surface: #1d1c1a;
    --surface-2: #232220;
    --border: #34322e;
    --text: #f1efea;
    --muted: #a19d95;
    --accent: #ff6a50;
    --accent-text: #1a0d0a;
    --accent-soft: #3a1f19;
    --bot: #2c2b28;
    --team: #4b9bd1;
    --team-text: #0b1620;
    --ok: #5cc98a;
    --warn: #f0b04a;
    --warn-soft: #33270f;
    --danger: #ff8a80;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141412; --surface: #1d1c1a; --surface-2: #232220; --border: #34322e; --text: #f1efea; --muted: #a19d95;
  --accent: #ff6a50; --accent-text: #1a0d0a; --accent-soft: #3a1f19; --bot: #2c2b28; --team: #4b9bd1; --team-text: #0b1620;
  --ok: #5cc98a; --warn: #f0b04a; --warn-soft: #33270f; --danger: #ff8a80; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
h2 { font-size: 18px; margin: 0 0 4px; }
.muted { color: var(--muted); }
.empty { color: var(--muted); padding: 24px 16px; text-align: center; }
.center { margin: auto; }

.brand { font-weight: 800; font-size: 20px; letter-spacing: -0.02em; white-space: nowrap; }
.brand span { color: var(--accent); }
.brand small { font-weight: 600; font-size: 13px; color: var(--muted); margin-left: 4px; }

input, textarea {
  width: 100%;
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
textarea { resize: vertical; }
input:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); outline-offset: 0; border-color: var(--accent); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 13px; font-weight: 600; margin: 12px 0 4px; }

.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(0.95); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.danger { color: var(--danger); }
.row { display: flex; gap: 8px; align-items: flex-end; }
.row.end { justify-content: flex-end; margin-top: 8px; }
.row.between { justify-content: space-between; align-items: center; margin-top: 8px; }
.msg { min-height: 1.2em; font-size: 14px; margin: 8px 0 0; }
.msg.error { color: var(--danger); }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 24px; box-shadow: var(--shadow);
}
.login-card .btn { width: 100%; margin-top: 12px; }
.login-or { text-align: center; font-size: 13px; margin: 16px 0 4px; }

/* Layout */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.tabs { display: flex; gap: 4px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs button {
  font: inherit; font-weight: 600; font-size: 14px; color: var(--muted); background: none; border: 0;
  padding: 8px 12px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.tabs button[aria-selected="true"] { color: var(--text); background: var(--surface-2); }
.badge {
  display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent);
  color: var(--accent-text); font-size: 11px; line-height: 18px; text-align: center;
}
.account { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.live { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.live.on { background: var(--ok); }
.tab { flex: 1; min-height: 0; }
.panel { overflow-y: auto; padding: 20px 16px 40px; max-width: 860px; width: 100%; margin: 0 auto; }

/* Chats */
.chats { display: grid; grid-template-columns: 320px 1fr; }
.chat-list { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.chat-list input { margin: 12px; width: auto; }
#chats { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#chats li button {
  width: 100%; text-align: left; font: inherit; color: inherit; background: none; border: 0;
  border-bottom: 1px solid var(--border); padding: 12px 14px; cursor: pointer; display: grid; gap: 2px;
}
#chats li button:hover { background: var(--surface-2); }
#chats li button[aria-current="true"] { background: var(--accent-soft); }
.chat-top { display: flex; justify-content: space-between; gap: 8px; }
.chat-top strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { font-size: 12px; color: var(--muted); white-space: nowrap; }
.chat-preview { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-tags { display: flex; gap: 6px; align-items: center; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); flex: none; }
.tag { font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 6px; background: var(--warn-soft); color: var(--warn); }

.conversation { display: flex; min-height: 0; min-width: 0; }
#chat-view { display: flex; flex-direction: column; flex: 1; min-height: 0; min-width: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.chat-head .who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-head .who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .who span { font-size: 13px; }
.back { display: none; }
.pill { font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.open { color: var(--ok); }
.pill.closed { color: var(--warn); background: var(--warn-soft); }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--surface); border: 1px solid var(--border); padding: 2px 10px; border-radius: 999px; margin: 6px 0; }
.bubble {
  max-width: min(560px, 82%); padding: 8px 12px; border-radius: 14px; white-space: pre-wrap; overflow-wrap: anywhere;
  background: var(--surface); border: 1px solid var(--border);
}
.bubble.user { align-self: flex-start; border-bottom-left-radius: 4px; }
.bubble.assistant { align-self: flex-end; background: var(--bot); border-color: transparent; border-bottom-right-radius: 4px; }
.bubble.equipo { align-self: flex-end; background: var(--team); color: var(--team-text); border-color: transparent; border-bottom-right-radius: 4px; }
.bubble .meta { display: block; font-size: 11px; opacity: 0.75; margin-top: 4px; }
.bubble.equipo .meta, .bubble.assistant .meta { text-align: right; }
.bubble img, .bubble video { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin-bottom: 4px; }
.bubble audio { display: block; max-width: 100%; margin-bottom: 4px; }
.file-link { display: flex; gap: 6px; align-items: center; font-weight: 600; color: inherit; margin-bottom: 4px; }
.missing { font-style: italic; opacity: 0.8; }
.transcript { display: block; font-style: italic; font-size: 14px; opacity: 0.85; margin-bottom: 4px; }
.transcript::before { content: "Transcripción: "; font-style: normal; font-weight: 600; font-size: 12px; }

.composer { background: var(--surface); border-top: 1px solid var(--border); padding: 10px 16px; }
.composer textarea { min-height: 44px; max-height: 160px; }
.composer .hint { font-size: 12px; margin: 6px 0 0; }
.note { font-size: 13px; background: var(--warn-soft); color: var(--warn); padding: 8px 10px; border-radius: 8px; margin: 0 0 8px; }

/* Cards */
.cards { display: grid; gap: 10px; margin-top: 14px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.card h3 { margin: 0 0 4px; font-size: 15px; }
.card .actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; align-items: center; }
.card.off { opacity: 0.6; }
.card dl { display: grid; grid-template-columns: max-content 1fr; gap: 2px 12px; margin: 8px 0 0; font-size: 14px; }
.card dt { color: var(--muted); }
.card dd { margin: 0; overflow-wrap: anywhere; }
.status-ok { color: var(--ok); font-weight: 700; font-size: 13px; }
.status-bad { color: var(--danger); font-weight: 700; font-size: 13px; }
.small-meta { font-size: 12px; color: var(--muted); }
.text-pre { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; }
.doc-text { max-height: 140px; overflow: hidden; position: relative; font-size: 14px; color: var(--muted); }
.switch { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; margin: 0; cursor: pointer; }
.switch input { width: auto; }

.sandbox { min-height: 240px; max-height: 50vh; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); margin-top: 12px; }
#tab-probar .composer { border: 1px solid var(--border); border-radius: var(--radius); margin-top: 8px; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); max-width: min(520px, calc(100% - 32px));
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 10;
}
.toast.error { background: var(--danger); color: #fff; }

/* Teléfono */
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .tabs { order: 3; flex-basis: 100%; }
  .account #user-email { display: none; }
  .account { margin-left: auto; }
  .chats { grid-template-columns: 1fr; }
  .chats.open .chat-list { display: none; }
  .chats:not(.open) .conversation { display: none; }
  .back { display: inline-block; }
  .bubble { max-width: 90%; }
  .chat-head { padding: 8px 12px; flex-wrap: wrap; row-gap: 6px; }
  .chat-head .pill { order: 3; flex-basis: 100%; text-align: center; }
  .composer { padding: 8px 12px; }
}
