:root {
  --primary: #0057A4;
  --primary-dark: #003E7A;
  --primary-light: #1A6BB8;
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-alt: #eef2f7;
  --border: #dbe3ee;
  --text: #17233a;
  --text-muted: #5a6b85;
  --success: #15803d;
  --warning: #d97706;
  --error: #dc2626;
  --error-bg: #fdecec;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 34, 64, .08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font-family: inherit; }
a { color: var(--primary); }

/* ===== Login ===== */
.login-screen { display: none; min-height: 100vh; align-items: center; justify-content: center; padding: 24px; background: linear-gradient(160deg, var(--primary-dark), var(--primary)); }
.login-screen.active { display: flex; }
.login-card { background: var(--surface); border-radius: 16px; padding: 36px 32px; width: 100%; max-width: 380px; box-shadow: 0 20px 60px rgba(0,0,0,.25); text-align: center; }
.login-card img { height: 48px; margin-bottom: 18px; }
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-card p { color: var(--text-muted); font-size: 13px; margin: 0 0 22px; }
.login-card input { width: 100%; padding: 12px 14px; margin-bottom: 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; }
.login-card input:focus { outline: none; border-color: var(--primary); }
.btn-login { width: 100%; padding: 12px; border: none; border-radius: 8px; background: var(--primary); color: #fff; font-weight: 600; font-size: 14px; cursor: pointer; margin-top: 6px; }
.btn-login:hover { background: var(--primary-dark); }

/* ===== App shell ===== */
.app-screen { display: none; min-height: 100vh; }
.app-screen.active { display: block; }
.topbar { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; padding: 10px 20px; }
.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-left img { height: 26px; }
.topbar-nav { display: flex; gap: 4px; }
.topbar-nav button { border: none; background: transparent; padding: 8px 14px; border-radius: 8px; font-size: 13.5px; font-weight: 600; color: var(--text-muted); cursor: pointer; }
.topbar-nav button.active { background: var(--surface-alt); color: var(--primary); }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.btn-logout { border: none; background: transparent; color: var(--text-muted); cursor: pointer; font-size: 13px; padding: 6px 10px; border-radius: 6px; }
.btn-logout:hover { background: var(--surface-alt); }
main#mainContent { max-width: 1180px; margin: 0 auto; padding: 22px 20px 60px; }

h2.page-title { font-size: 19px; margin: 0 0 4px; }
p.page-sub { color: var(--text-muted); font-size: 13px; margin: 0 0 20px; }

/* ===== Cards / grids ===== */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 22px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat-card .stat-label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.stat-card .stat-value { font-size: 24px; font-weight: 700; }
.stat-card .stat-delta { font-size: 11.5px; color: var(--text-muted); margin-top: 2px; }
.stat-card.urgente { border-color: var(--error); background: var(--error-bg); }
.stat-card.urgente .stat-value { color: var(--error); }

.section-block { margin-bottom: 26px; }
.section-block h3 { font-size: 14.5px; margin: 0 0 10px; }

table.tbl { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
table.tbl th, table.tbl td { padding: 10px 12px; text-align: left; font-size: 13px; border-bottom: 1px solid var(--border); }
table.tbl th { background: var(--surface-alt); color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .02em; }
table.tbl tr:last-child td { border-bottom: none; }

/* Toggle switch */
.switch { position: relative; display: inline-block; width: 40px; height: 22px; vertical-align: middle; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; cursor: pointer; inset: 0; background: #c7d0dd; border-radius: 22px; transition: .15s; }
.switch .slider:before { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .15s; }
.switch input:checked + .slider { background: var(--success); }
.switch input:checked + .slider:before { transform: translateX(18px); }

.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 20px; text-transform: uppercase; letter-spacing: .03em; }
.badge-urgente { background: var(--error); color: #fff; }
.badge-manual { background: var(--warning); color: #fff; }
.badge-ia { background: var(--surface-alt); color: var(--primary); }
.badge-ok { background: #e7f5ec; color: var(--success); }

.feed-item { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.feed-item:last-child { border-bottom: none; }
.feed-item .feed-icon { width: 30px; height: 30px; border-radius: 50%; background: var(--surface-alt); display: flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 15px; }
.feed-item .feed-time { color: var(--text-muted); font-size: 11.5px; margin-left: auto; white-space: nowrap; }

/* ===== Bandeja ===== */
.bandeja-layout { display: grid; grid-template-columns: 320px 1fr 260px; gap: 16px; align-items: start; }
@media (max-width: 980px) { .bandeja-layout { grid-template-columns: 1fr; } }

.tabs { display: flex; gap: 4px; margin-bottom: 12px; background: var(--surface-alt); padding: 4px; border-radius: 10px; }
.tabs button { flex: 1; border: none; background: transparent; padding: 8px 10px; border-radius: 8px; font-size: 12.5px; font-weight: 600; color: var(--text-muted); cursor: pointer; }
.tabs button.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }

.conv-list { max-height: 640px; overflow-y: auto; }
.conv-item { padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.conv-item:hover { background: var(--surface-alt); }
.conv-item.active { background: #e7f0fb; }
.conv-item .conv-nombre { font-weight: 600; font-size: 13.5px; }
.conv-item .conv-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.conv-item .conv-meta { display: flex; align-items: center; gap: 6px; margin-top: 4px; font-size: 11px; color: var(--text-muted); }

.chat-panel { display: flex; flex-direction: column; height: 640px; }
.chat-alert { background: var(--error-bg); border: 1px solid #f3c2c2; color: #7f1d1d; border-radius: 8px; padding: 10px 12px; font-size: 12.5px; margin-bottom: 10px; }
.chat-messages { flex: 1; overflow-y: auto; padding: 6px 4px; display: flex; flex-direction: column; gap: 8px; }
.bubble { max-width: 78%; padding: 8px 12px; border-radius: 12px; font-size: 13px; line-height: 1.4; }
.bubble-entrante { align-self: flex-start; background: var(--surface-alt); }
.bubble-ia { align-self: flex-end; background: var(--primary); color: #fff; }
.bubble-humano { align-self: flex-end; background: var(--success); color: #fff; }
.bubble-sistema { align-self: center; background: #fff3cd; color: #7a5b00; font-style: italic; }
.bubble .bubble-meta { display: block; font-size: 10px; opacity: .75; margin-top: 4px; }

.reply-box { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 10px; }
.reply-box textarea { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px; resize: vertical; min-height: 60px; }
.reply-actions { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; gap: 8px; flex-wrap: wrap; }
.reply-actions label { font-size: 12px; color: var(--text-muted); display: flex; align-items: center; gap: 6px; }

.lead-ficha .lf-row { padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 12.5px; }
.lead-ficha .lf-row:last-child { border-bottom: none; }
.lead-ficha .lf-label { color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .02em; }

/* ===== Buttons ===== */
.btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer; }
.btn:hover { background: var(--surface-alt); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--error); border-color: var(--error); color: #fff; }
.btn-danger:hover { opacity: .9; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

/* ===== Cerebro IA ===== */
.prompt-box { background: #0f172a; color: #cbd5e1; font-family: 'JetBrains Mono', ui-monospace, monospace; font-size: 11.5px; line-height: 1.55; padding: 16px; border-radius: 10px; max-height: 420px; overflow-y: auto; white-space: pre-wrap; word-break: break-word; }
.guardrail-list { list-style: none; margin: 0; padding: 0; }
.guardrail-list li { padding: 8px 12px; background: var(--surface-alt); border-radius: 8px; margin-bottom: 6px; font-size: 12.5px; }

.playground-turn { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; font-size: 12.5px; }
.playground-turn select { padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; }
.playground-turn input { flex: 1; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 12.5px; }
.simulacion-banner { background: #fff3cd; color: #7a5b00; font-size: 12px; font-weight: 700; text-align: center; padding: 6px; border-radius: 8px; margin-bottom: 10px; letter-spacing: .02em; }
.rama-badge { font-family: 'JetBrains Mono', ui-monospace, monospace; font-size: 11px; padding: 3px 8px; border-radius: 6px; }
.rama-opt_out { background: #e5e7eb; color: #374151; }
.rama-emergencia { background: var(--error); color: #fff; }
.rama-ia_normal { background: #e7f0fb; color: var(--primary); }

/* ===== Toast / modal ===== */
#toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%) translateY(20px); background: #1e293b; color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 13px; opacity: 0; pointer-events: none; transition: .2s; z-index: 999; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.modal-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: none; align-items: center; justify-content: center; z-index: 100; padding: 20px; }
.modal-overlay.show { display: flex; }
.modal-box { background: #fff; border-radius: 12px; padding: 20px; width: 100%; max-width: 420px; }
.modal-box h3 { margin: 0 0 8px; font-size: 16px; }
.modal-box p { font-size: 13.5px; color: var(--text-muted); margin: 0 0 16px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

.selector-lista { max-height: 260px; overflow-y: auto; }
.selector-item { padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.selector-item:hover { background: var(--surface-alt); }
.selector-item input { margin-right: 8px; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--text-muted); font-size: 13.5px; }

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: var(--radius); }
.table-scroll table.tbl { min-width: 480px; }

/* ===== Responsive (celular) ===== */
@media (max-width: 680px) {
  .topbar { flex-wrap: wrap; row-gap: 8px; padding: 10px 14px; }
  .topbar-left { flex-wrap: wrap; row-gap: 8px; width: 100%; }
  .topbar-nav { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap; }
  .topbar-nav button { flex: 0 0 auto; white-space: nowrap; padding: 8px 12px; font-size: 12.5px; }
  .topbar-right { width: 100%; justify-content: space-between; }
  #topbarUser { display: none; }
  main#mainContent { padding: 16px 14px 50px; }
  h2.page-title { font-size: 17px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat-card .stat-value { font-size: 20px; }
  .chat-panel, .conv-list { height: auto; max-height: 60vh; }
  .bubble { max-width: 88%; }
  .modal-box { padding: 16px; }
}
@media (max-width: 400px) {
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .topbar-nav button { padding: 7px 9px; font-size: 12px; }
}
