:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #e0e2e7;
  --text: #1c1e21;
  --text-dim: #6b7076;
  --accent: #2f6feb;
  --accent-dark: #1e4fa8;
  --green: #1a8a4a;
  --amber: #c77d00;
  --red: #c0392b;
  --bot-bubble: #eef0f3;
  --me-bubble: #d9e8ff;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

button {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: var(--radius);
  padding: 10px 14px;
  min-height: 44px;
}

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

input, textarea {
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  min-height: 44px;
  width: 100%;
}

a { color: var(--accent); }

#app { height: 100%; }

.view { height: 100%; }
.view[hidden] { display: none !important; }

/* ---------- Login ---------- */

#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-form {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-form h1 {
  font-size: 1.15rem;
  margin: 0 0 4px;
  text-align: center;
}

.login-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.9rem;
  color: var(--text-dim);
}

.login-form button {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  margin-top: 6px;
}

.login-form button:hover { background: var(--accent-dark); }

.error-text { color: var(--red); font-size: 0.9rem; margin: 0; }

/* ---------- App shell ---------- */

.app-view {
  display: flex;
  height: 100%;
  overflow: hidden;
}

.list-pane {
  width: 320px;
  min-width: 320px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.list-header h1 { font-size: 1.05rem; margin: 0; }

.list-header-actions { display: flex; gap: 6px; }

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  padding: 6px 10px;
}

.conversation-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.conversation-list li {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.conversation-list li:hover { background: var(--bg); }
.conversation-list li.selected { background: #e8f0fe; }
.conversation-list li.unread .conv-name { font-weight: 700; }

.conv-row-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.conv-name {
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-time { font-size: 0.75rem; color: var(--text-dim); flex-shrink: 0; }

.conv-preview {
  font-size: 0.85rem;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-badges { display: flex; align-items: center; gap: 6px; margin-top: 2px; }

.badge-unread {
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  font-size: 0.7rem;
  padding: 1px 7px;
  min-width: 18px;
  text-align: center;
}

.chip-human {
  background: #fdecea;
  color: var(--red);
  border-radius: 999px;
  font-size: 0.7rem;
  padding: 1px 8px;
}

.dot-closed {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--amber);
  display: inline-block;
}

.empty-state {
  color: var(--text-dim);
  text-align: center;
  padding: 40px 20px;
}

/* ---------- Conversation pane ---------- */

.conversation-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

.conversation-view {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.conversation-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.back-btn {
  display: none;
  flex-shrink: 0;
  background: transparent;
  padding: 6px 8px;
}

.conversation-title { flex: 1; min-width: 0; }
.conversation-title h2 {
  font-size: 1rem;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.call-link { font-size: 0.85rem; text-decoration: none; }

.mode-area {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.mode-status {
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  padding: 3px 9px;
  border-radius: 999px;
}
.mode-status.is-human {
  background: #fdecea;
  color: var(--red);
}
.mode-status.is-bot {
  background: #eaf3ea;
  color: var(--green);
}

.mode-toggle {
  font-size: 0.85rem;
  white-space: nowrap;
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 8px 12px;
  min-height: 36px;
}

.human-banner {
  background: #fff4e5;
  color: #7a4a00;
  padding: 10px 16px;
  font-size: 0.85rem;
  border-bottom: 1px solid #f0d9a8;
}

.messages {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg {
  max-width: 75%;
  padding: 8px 12px;
  border-radius: 14px;
  font-size: 0.92rem;
  line-height: 1.35;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.entrante {
  align-self: flex-start;
  background: var(--bot-bubble);
  border-bottom-left-radius: 4px;
}

.msg.saliente {
  align-self: flex-end;
  background: var(--me-bubble);
  border-bottom-right-radius: 4px;
}

.msg .msg-meta {
  display: block;
  font-size: 0.68rem;
  color: var(--text-dim);
  margin-top: 4px;
}

.msg .chip-tu {
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  font-size: 0.65rem;
  padding: 0 6px;
  margin-right: 4px;
}

.day-separator {
  align-self: center;
  font-size: 0.75rem;
  color: var(--text-dim);
  margin: 6px 0;
}

.window-countdown {
  font-size: 0.8rem;
  color: var(--amber);
  padding: 6px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

.send-area {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

.send-area textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
}

.send-area button {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.send-area button:hover { background: var(--accent-dark); }

.window-closed {
  padding: 16px;
  border-top: 1px solid var(--border);
  background: #fff8ec;
}

.window-closed-text, .window-closed p { margin: 0 0 10px; font-size: 0.9rem; }

.window-closed-actions { display: flex; gap: 10px; align-items: center; }

.plantilla-btn { background: var(--bg); border: 1px solid var(--border); }

.window-closed-note { font-size: 0.75rem; color: var(--text-dim); margin-top: 8px; }

/* ---------- Dialog / toast ---------- */

.confirm-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 20px;
  max-width: 340px;
  width: 90%;
}

.confirm-dialog::backdrop { background: rgba(0,0,0,0.4); }

.confirm-title { font-weight: 700; margin: 0 0 8px; }

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}

.dialog-actions button.primary {
  background: var(--accent);
  color: #fff;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 0.88rem;
  max-width: 90%;
  text-align: center;
  z-index: 50;
}

.toast[hidden] { display: none; }

/* ---------- Responsive: mobile ---------- */

@media (max-width: 767px) {
  .app-view { flex-direction: column; }

  .list-pane { width: 100%; min-width: 0; border-right: none; }

  .app-view.showing-conversation .list-pane { display: none; }
  .app-view:not(.showing-conversation) .conversation-pane { display: none; }

  .back-btn { display: inline-block; }

  .mode-area { width: 100%; justify-content: space-between; }

  .msg { max-width: 88%; }
}

@media (min-width: 768px) {
  .conversation-empty { display: flex; align-items: center; justify-content: center; height: 100%; }
}
