:root {
  --bg: #f4f3ef; --raised: #ffffff; --fill: #efeee9; --fill-2: #e6e4dd;
  --label: #1c1c1e; --label-2: #5f5f66; --label-3: #a1a1a8; --sep: rgba(60,60,67,.16);
  --blue: #2f62e6; --blue-deep: #1f44b8; --blue-soft: #e8eefd; --green: #1f9d55; --green-soft: #e3f5ea;
  --amber: #b7791f; --amber-soft: #fdf3e1; --red: #d93a3a; --red-soft: #fde8e8;
  --radius: 14px; --shadow: 0 1px 2px rgba(20,20,30,.05), 0 8px 24px rgba(20,20,30,.06);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--label); font: 15px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
h1 { font-size: 1.6rem; font-weight: 750; letter-spacing: -.02em; margin: 0; }
h2 { font-size: 1.02rem; font-weight: 700; margin: 0 0 10px; }
.muted { color: var(--label-2); } .small { font-size: .85rem; } .hidden { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { width: 100%; background: var(--raised); border: 1px solid var(--sep); border-radius: 10px; padding: 9px 11px; outline: none; transition: border-color .15s, box-shadow .15s; }
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-soft); }
textarea { resize: vertical; }
label { display: block; font-size: .88rem; font-weight: 600; color: var(--label-2); margin: 0 0 12px; }
label > input, label > select, label > textarea { margin-top: 5px; font-weight: 400; color: var(--label); }
.btn { border: 0; border-radius: 10px; padding: 9px 16px; background: var(--fill); font-weight: 600; cursor: pointer; transition: transform .08s, background .15s; }
.btn:hover { background: var(--fill-2); } .btn:active { transform: scale(.97); }
.btn.primary { background: var(--blue); color: #fff; } .btn.primary:hover { background: var(--blue-deep); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.block { width: 100%; padding: 12px; }

/* Login */
.login-body { display: grid; place-items: center; padding: 20px; min-height: 100dvh; background: radial-gradient(1200px 600px at 20% -10%, #e5ecff 0, transparent 60%), var(--bg); }
.login-card { width: 100%; max-width: 370px; background: var(--raised); border-radius: 22px; padding: 32px 28px; box-shadow: var(--shadow); text-align: center; }
.login-card form { text-align: left; margin-top: 18px; }
.login-logo { border-radius: 14px; }
.login-card h1 { margin-top: 10px; }
.form-error { color: var(--red); min-height: 1.2em; font-size: .9rem; margin: 10px 0 0; }

/* Estructura */
.shell { display: flex; height: 100dvh; }
.sidebar { width: 236px; flex: none; display: flex; flex-direction: column; gap: 6px; padding: 18px 12px; background: rgba(255,255,255,.65); border-right: 1px solid var(--sep); backdrop-filter: blur(20px); }
.brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 16px; }
.brand img { border-radius: 9px; } .brand strong { display: block; font-size: 1.05rem; } .brand small { color: var(--label-2); font-size: .78rem; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item { text-align: left; border: 0; background: transparent; padding: 9px 12px; border-radius: 10px; cursor: pointer; font-weight: 550; color: var(--label); }
.nav-item:hover { background: var(--fill); } .nav-item.active { background: var(--blue); color: #fff; }
.nav-item.subtle { color: var(--label-2); }
.content { flex: 1; min-width: 0; overflow: auto; position: relative; }
.view { padding: 24px 28px 40px; max-width: 1080px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head p { margin: 6px 0 0; }
.card { background: var(--raised); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 16px; }
.card-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.card-row h2 { margin: 0; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 14px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0 14px; }
.toggles { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 24px; }
.toggle { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--sep); margin: 0; color: var(--label); font-weight: 500; }
.save-bar { position: sticky; bottom: 0; display: flex; justify-content: flex-end; align-items: center; gap: 14px; padding: 12px 0; background: linear-gradient(transparent, var(--bg) 35%); }

/* Interruptor estilo iOS */
.switch { position: relative; display: inline-flex; align-items: center; gap: 8px; margin: 0; cursor: pointer; flex: none; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch > span { width: 46px; height: 28px; border-radius: 28px; background: var(--fill-2); position: relative; transition: background .2s; }
.switch > span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.switch input:checked + span { background: var(--green); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:focus-visible + span { box-shadow: 0 0 0 3px var(--blue-soft); }
.switch em { font-style: normal; font-size: .85rem; color: var(--label-2); font-weight: 600; }

.segmented { display: inline-flex; background: var(--fill); border-radius: 10px; padding: 3px; gap: 2px; }
.segmented button { border: 0; background: transparent; padding: 6px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--label-2); }
.segmented button.active { background: var(--raised); color: var(--label); box-shadow: 0 1px 3px rgba(0,0,0,.1); }

/* Conversaciones */
.view.chats { display: flex; padding: 0; max-width: none; height: 100%; }
.chat-list-pane { width: 340px; flex: none; border-right: 1px solid var(--sep); display: flex; flex-direction: column; background: var(--raised); }
.pane-head { padding: 18px 16px 10px; display: flex; flex-direction: column; gap: 10px; }
.chat-list { overflow: auto; flex: 1; }
.chat-item { width: 100%; display: flex; gap: 11px; align-items: center; text-align: left; border: 0; background: transparent; padding: 11px 16px; cursor: pointer; border-bottom: 1px solid var(--sep); }
.chat-item:hover { background: var(--fill); } .chat-item.active { background: var(--blue-soft); }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .top { display: flex; justify-content: space-between; gap: 8px; }
.chat-item strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item time { color: var(--label-3); font-size: .78rem; flex: none; }
.chat-item .bottom { display: flex; justify-content: space-between; gap: 8px; align-items: center; margin-top: 2px; }
.chat-item .preview { color: var(--label-2); font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--blue); color: #fff; border-radius: 10px; padding: 0 7px; font-size: .75rem; font-weight: 700; flex: none; }
.chip { display: inline-block; font-size: .72rem; font-weight: 650; padding: 1px 8px; border-radius: 8px; background: var(--fill); color: var(--label-2); }
.chip.paused { background: var(--amber-soft); color: var(--amber); }
.avatar { width: 42px; height: 42px; border-radius: 50%; flex: none; display: grid; place-items: center; font-weight: 700; color: #fff; background: linear-gradient(135deg, #7b93f5, #3a5bd6); }
.empty-list { padding: 28px 18px; color: var(--label-2); text-align: center; }
.thread-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; background: #ecebe5; }
.thread-empty { flex: 1; display: grid; place-items: center; text-align: center; color: var(--label-2); }
.big-emoji { font-size: 2.6rem; }
.thread { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread-head { display: flex; align-items: center; gap: 12px; padding: 12px 18px; background: rgba(255,255,255,.85); border-bottom: 1px solid var(--sep); backdrop-filter: blur(20px); }
.thread-title { flex: 1; min-width: 0; } .thread-title strong { display: block; } .thread-title small { color: var(--label-2); }
.thread-messages { flex: 1; overflow: auto; padding: 18px clamp(12px, 4vw, 48px); display: flex; flex-direction: column; gap: 6px; }
.day-sep { align-self: center; background: rgba(255,255,255,.8); color: var(--label-2); font-size: .78rem; font-weight: 600; padding: 3px 10px; border-radius: 8px; margin: 8px 0; }
.bubble { max-width: min(560px, 78%); padding: 8px 12px 6px; border-radius: 16px; background: var(--raised); box-shadow: 0 1px 1px rgba(0,0,0,.06); align-self: flex-start; word-wrap: break-word; }
.bubble.out { align-self: flex-end; background: #dfe8ff; }
.bubble.out.operator { background: #d9f3e2; }
.bubble.out.system { background: var(--amber-soft); }
.bubble p { margin: 0; white-space: pre-wrap; }
.bubble .who { font-size: .72rem; font-weight: 700; color: var(--blue-deep); margin-bottom: 2px; }
.bubble.operator .who { color: var(--green); }
.bubble .foot { display: flex; justify-content: flex-end; gap: 6px; font-size: .72rem; color: var(--label-3); margin-top: 3px; }
.bubble .ticks.read { color: #34a0e4; } .bubble .ticks.failed { color: var(--red); }
.bubble img { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; margin: 2px 0 4px; }
.bubble audio { width: 260px; max-width: 100%; margin: 2px 0; }
.bubble .note { font-size: .84rem; color: var(--label-2); background: rgba(0,0,0,.04); border-radius: 8px; padding: 5px 8px; margin-top: 5px; }
.bubble .err { font-size: .8rem; color: var(--red); margin-top: 4px; }
.bubble a.file { display: inline-block; font-weight: 600; color: var(--blue-deep); }
.composer { display: flex; gap: 10px; padding: 12px 18px calc(12px + env(safe-area-inset-bottom)); background: rgba(255,255,255,.9); border-top: 1px solid var(--sep); }
.composer textarea { min-height: 42px; max-height: 160px; border-radius: 20px; padding: 10px 14px; }
.icon-btn { border: 0; background: transparent; font-size: 1.6rem; cursor: pointer; padding: 0 6px; color: var(--blue); }
.mobile-only { display: none; }

/* Prueba del agente */
.sandbox-row { display: flex; gap: 10px; } .sandbox-row input { flex: 1; }
.sandbox-out { margin-top: 14px; display: flex; flex-direction: column; gap: 6px; background: #ecebe5; border-radius: 12px; padding: 14px; }

/* Filas y estado */
.rows { display: flex; flex-direction: column; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 11px 0; border-bottom: 1px solid var(--sep); }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row select { width: auto; min-width: 190px; }
.inline-form { display: flex; gap: 10px; margin-top: 12px; }
.status-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; margin-bottom: 16px; }
.stat { background: var(--raised); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; }
.stat h3 { margin: 0 0 6px; font-size: .82rem; color: var(--label-2); font-weight: 650; text-transform: uppercase; letter-spacing: .03em; }
.stat .value { font-size: 1.25rem; font-weight: 700; }
.stat .ok { color: var(--green); } .stat .warn { color: var(--amber); } .stat .bad { color: var(--red); }
.stat dl { margin: 6px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; font-size: .86rem; }
.stat dt { color: var(--label-2); } .stat dd { margin: 0; font-weight: 600; word-break: break-word; }

.toast { position: fixed; top: 16px; left: 50%; transform: translateX(-50%); background: rgba(28,28,30,.92); color: #fff; padding: 10px 18px; border-radius: 14px; font-weight: 600; z-index: 50; box-shadow: var(--shadow); max-width: 90vw; }
.toast.error { background: var(--red); }

@media (max-width: 900px) {
  .grid3, .grid4, .toggles { grid-template-columns: 1fr 1fr; }
  .shell { flex-direction: column; }
  .sidebar { width: auto; flex-direction: row; align-items: center; padding: 8px 10px; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--sep); }
  .brand small, .brand div { display: none; }
  .brand { padding: 0 6px 0 0; }
  .nav { flex-direction: row; flex: none; }
  .nav-item { white-space: nowrap; padding: 7px 10px; }
  .view { padding: 18px 16px 32px; }
  .view.chats { position: relative; }
  .chat-list-pane { width: 100%; }
  .thread-pane { position: absolute; inset: 0; }
  .view.chats:not(.thread-open) .thread-pane { display: none; }
  .view.chats.thread-open .chat-list-pane { display: none; }
  .mobile-only { display: inline-block; }
}
@media (max-width: 560px) {
  .grid3, .grid4, .toggles { grid-template-columns: 1fr; }
  .inline-form, .sandbox-row { flex-direction: column; }
  .bubble { max-width: 88%; }
}
