/* ============================================================================
   ChatPunk — Inbox de WhatsApp Business
   Hoja de estilos de produccion. Mobile-first, tema oscuro profesional.
   Breakpoints: 768px (2 columnas) y 1024px (3 columnas).
   ========================================================================== */

/* ----------------------------------------------------------------------------
   1. Variables y reset
---------------------------------------------------------------------------- */
:root {
    /* Paleta contratada */
    --bg: #0b141a;
    --bg-2: #111b21;
    --panel: #202c33;
    --text: #e9edef;
    --muted: #8696a0;
    --accent: #00a884;
    --in-bubble: #202c33;
    --out-bubble: #005c4b;
    --danger: #f15c6d;
    --ok: #06cf9c;

    /* Derivadas */
    --panel-2: #2a3942;
    --panel-3: #324650;
    --accent-strong: #06cf9c;
    --accent-dim: rgba(0, 168, 132, 0.18);
    --warning: #f7b955;
    --read-tick: #53bdeb;
    --border: rgba(134, 150, 160, 0.16);
    --border-strong: rgba(134, 150, 160, 0.3);
    --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-2: 0 6px 24px rgba(0, 0, 0, 0.45);
    --shadow-3: 0 16px 48px rgba(0, 0, 0, 0.6);

    /* Metricas */
    --radius-s: 6px;
    --radius-m: 10px;
    --radius-l: 16px;
    --sidebar-w: 320px;
    --contact-w: 300px;
    --header-h: 60px;
    --composer-max: 140px;

    /* Tipografia */
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
            Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji";

    /* Capas */
    --z-panel: 40;
    --z-banner: 60;
    --z-modal: 80;
    --z-toast: 100;

    color-scheme: dark;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.45;
    color: var(--text);
    background: var(--bg);
    overflow: hidden;
    height: 100dvh;
}

h1, h2, h3, h4, p, ul {
    margin: 0;
    padding: 0;
}

ul {
    list-style: none;
}

img {
    max-width: 100%;
    display: block;
}

button {
    font: inherit;
    color: inherit;
    cursor: pointer;
    border: 0;
    background: none;
    padding: 0;
}

a {
    color: var(--accent-strong);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* Foco visible y accesible en todos los controles */
:focus {
    outline: none;
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: var(--radius-s);
}

::selection {
    background: var(--accent-dim);
    color: var(--text);
}

::placeholder {
    color: var(--muted);
    opacity: 0.8;
}

/* Scrollbars discretos */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--panel-3) transparent;
}

*::-webkit-scrollbar {
    width: 7px;
    height: 7px;
}

*::-webkit-scrollbar-track {
    background: transparent;
}

*::-webkit-scrollbar-thumb {
    background: var(--panel-3);
    border-radius: 8px;
}

*::-webkit-scrollbar-thumb:hover {
    background: var(--border-strong);
}

/* ----------------------------------------------------------------------------
   2. Utilidades
---------------------------------------------------------------------------- */
.hidden {
    display: none !important;
}

.only-mobile {
    display: inline-flex;
}

.noscript-warn {
    position: fixed;
    inset: auto 0 0 0;
    z-index: var(--z-toast);
    margin: 0;
    padding: 12px 16px;
    text-align: center;
    background: var(--danger);
    color: #fff;
    font-weight: 600;
}

/* Estado vacio generico (lista vacia, sin chat seleccionado) */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    padding: 32px 24px;
    text-align: center;
    color: var(--muted);
}

.empty-state svg {
    opacity: 0.5;
}

.empty-state p {
    max-width: 320px;
    font-size: 14px;
}

/* Indicador de carga: aplicar .loading a cualquier contenedor */
.loading {
    position: relative;
    pointer-events: none;
    min-height: 64px;
}

.loading > * {
    opacity: 0.4;
}

.loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 26px;
    height: 26px;
    margin: -13px 0 0 -13px;
    border: 3px solid var(--panel-3);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ----------------------------------------------------------------------------
   3. Botones y controles de formulario
---------------------------------------------------------------------------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 9px 18px;
    border-radius: var(--radius-m);
    font-size: 14px;
    font-weight: 600;
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--border);
    transition: background 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
    user-select: none;
}

.btn:hover {
    background: var(--panel-3);
}

.btn:active {
    transform: scale(0.98);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #06251e;
}

.btn-primary:hover {
    background: var(--accent-strong);
    border-color: var(--accent-strong);
}

.btn-danger {
    background: transparent;
    border-color: var(--danger);
    color: var(--danger);
}

.btn-danger:hover {
    background: rgba(241, 92, 109, 0.12);
}

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--muted);
}

.btn-ghost:hover {
    background: var(--panel-2);
    color: var(--text);
}

.btn-block {
    width: 100%;
}

.btn-sm {
    padding: 5px 12px;
    font-size: 13px;
    border-radius: var(--radius-s);
}

/* Boton de icono (cabeceras, composer) */
.icon-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    color: var(--muted);
    flex: 0 0 auto;
    transition: background 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
    background: var(--panel-2);
    color: var(--text);
}

.icon-btn:active {
    background: var(--panel-3);
}

.icon-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Punto de estado del sistema sobre #btn-system */
.dot {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--ok);
    border: 2px solid var(--bg-2);
    transition: background 0.2s ease;
}

.dot.warning {
    background: var(--warning);
}

.dot.critical {
    background: var(--danger);
    animation: pulse-dot 1.2s ease-in-out infinite;
}

@keyframes pulse-dot {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.35); opacity: 0.7; }
}

/* Inputs, selects y textareas */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="search"],
input[type="url"],
input[type="number"],
select,
textarea {
    width: 100%;
    padding: 9px 12px;
    border-radius: var(--radius-m);
    border: 1px solid var(--border);
    background: var(--panel);
    color: var(--text);
    font: inherit;
    font-size: 14px;
    transition: border-color 0.15s ease, background 0.15s ease;
    appearance: none;
    -webkit-appearance: none;
}

input:hover,
select:hover,
textarea:hover {
    border-color: var(--border-strong);
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-dim);
}

select {
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' fill='none'%3E%3Cpath d='M1 1.5 6 6.5 11 1.5' stroke='%238696a0' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 30px;
    cursor: pointer;
}

textarea {
    resize: vertical;
    min-height: 70px;
}

label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
    margin-bottom: 6px;
}

.field {
    margin-bottom: 16px;
}

.form-row {
    display: flex;
    gap: 10px;
}

.form-row > * {
    flex: 1;
    min-width: 0;
}

.form-error {
    margin: 0 0 14px;
    padding: 9px 12px;
    border-radius: var(--radius-s);
    background: rgba(241, 92, 109, 0.12);
    border: 1px solid rgba(241, 92, 109, 0.4);
    color: var(--danger);
    font-size: 13px;
}

.help-text {
    font-size: 12px;
    color: var(--muted);
    margin-top: 5px;
}

/* ----------------------------------------------------------------------------
   4. Pagina de login
---------------------------------------------------------------------------- */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    background:
        radial-gradient(1100px 500px at 80% -10%, rgba(0, 168, 132, 0.08), transparent 60%),
        radial-gradient(800px 400px at 10% 110%, rgba(0, 168, 132, 0.05), transparent 60%),
        var(--bg);
    overflow: auto;
    padding: 16px;
}

.login-wrap {
    width: 100%;
    max-width: 380px;
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.login-card {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-l);
    padding: 34px 28px 28px;
    box-shadow: var(--shadow-2);
    animation: card-in 0.3s ease both;
}

@keyframes card-in {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.login-card h1 {
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 4px;
}

.login-sub {
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 24px;
}

.login-foot {
    text-align: center;
    color: var(--muted);
    font-size: 12px;
}

/* Marca */
.brand {
    display: flex;
    align-items: center;
    gap: 9px;
    font-weight: 700;
    font-size: 17px;
    letter-spacing: -0.01em;
    color: var(--text);
    min-width: 0;
}

.brand svg {
    color: var(--accent);
    flex: 0 0 auto;
}

.brand-lg {
    font-size: 22px;
    margin-bottom: 22px;
}

/* ----------------------------------------------------------------------------
   5. Layout principal (movil primero: una columna con data-view)
---------------------------------------------------------------------------- */
#app {
    display: flex;
    height: 100dvh;
    width: 100%;
    overflow: hidden;
    background: var(--bg);
}

/* En movil solo se ve una vista a la vez */
#app[data-view="list"] #chat {
    display: none;
}

#app[data-view="chat"] #sidebar {
    display: none;
}

/* ----------------------------------------------------------------------------
   6. Sidebar: lista de conversaciones
---------------------------------------------------------------------------- */
#sidebar {
    display: flex;
    flex-direction: column;
    width: 100%;
    min-width: 0;
    background: var(--bg-2);
    border-right: 1px solid var(--border);
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-height: var(--header-h);
    padding: 8px 10px 8px 16px;
    background: var(--panel);
    flex: 0 0 auto;
}

.sidebar-actions {
    display: flex;
    align-items: center;
    gap: 2px;
}

.sidebar-filters {
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}

.sidebar-filters input[type="search"] {
    background: var(--panel);
    border-radius: var(--radius-l);
}

.filter-row {
    display: flex;
    gap: 8px;
}

.filter-row select {
    flex: 1;
    min-width: 0;
    font-size: 13px;
    padding: 7px 26px 7px 10px;
    background-color: var(--panel);
}

/* Lista scrollable independiente */
#conversation-list {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

/* Item de conversacion */
.conv-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 14px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    transition: background 0.12s ease;
    animation: fade-in 0.18s ease both;
}

.conv-item:hover {
    background: var(--panel);
}

.conv-item.active {
    background: var(--panel-2);
    box-shadow: inset 3px 0 0 var(--accent);
}

.conv-item:focus-visible {
    outline-offset: -2px;
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Avatar circular con iniciales */
.avatar {
    flex: 0 0 auto;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: 700;
    color: #dff7ef;
    background: linear-gradient(135deg, #00786a, #00a884);
    text-transform: uppercase;
    user-select: none;
    overflow: hidden;
}

.conv-body {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.conv-top,
.conv-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-width: 0;
}

.conv-name {
    font-weight: 600;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.conv-preview {
    flex: 1;
    min-width: 0;
    font-size: 13px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-meta {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: var(--muted);
    white-space: nowrap;
}

/* Contador de no leidos */
.badge-unread {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 10px;
    background: var(--accent);
    color: #06251e;
    font-size: 11px;
    font-weight: 700;
    line-height: 1;
}

/* Chip de etiqueta (background dinamico via style="") */
.chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    max-width: 120px;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    line-height: 1.5;
    color: #fff;
    background: var(--panel-3);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
}

.chip.ok {
    background: var(--ok);
    color: #06251e;
    text-shadow: none;
}

.chip.warn {
    background: var(--warning);
    color: #2a1d00;
    text-shadow: none;
}

.chip.danger {
    background: var(--danger);
}

.conv-chips {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
    min-width: 0;
}

/* Indicador de ventana de 24h */
.win-indicator {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
}

.win-indicator::before {
    content: "";
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: currentColor;
}

.win-indicator.open {
    color: var(--ok);
}

.win-indicator.closed {
    color: var(--muted);
}

/* ----------------------------------------------------------------------------
   7. Zona de chat
---------------------------------------------------------------------------- */
#chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg);
}

#chat-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#chat-view.hidden + #chat-placeholder {
    display: flex;
}

/* Cabecera del hilo */
#chat-header {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: var(--header-h);
    padding: 8px 10px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}

#chat-avatar {
    width: 40px;
    height: 40px;
    font-size: 15px;
}

.chat-head-info {
    flex: 1;
    min-width: 0;
}

#chat-title {
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#chat-sub {
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-head-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 0 0 auto;
}

#conv-status {
    width: auto;
    font-size: 13px;
    padding: 6px 26px 6px 10px;
    background-color: var(--panel-2);
    border-color: transparent;
}

/* Interruptor del bot */
.bot-switch {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin: 0;
    cursor: pointer;
    user-select: none;
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
}

.bot-switch input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
}

.switch-track {
    position: relative;
    width: 36px;
    height: 20px;
    border-radius: 999px;
    background: var(--panel-3);
    transition: background 0.18s ease;
    flex: 0 0 auto;
}

.switch-track::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--muted);
    transition: transform 0.18s ease, background 0.18s ease;
}

.bot-switch input:checked + .switch-track {
    background: var(--accent-dim);
}

.bot-switch input:checked + .switch-track::after {
    transform: translateX(16px);
    background: var(--accent);
}

.bot-switch input:focus-visible + .switch-track {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.switch-text {
    line-height: 1;
}

/* Hilo de mensajes con fondo sutil */
#message-list {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 14px 4% 10px;
    display: flex;
    flex-direction: column;
    background:
        radial-gradient(circle at 25% 15%, rgba(0, 168, 132, 0.03), transparent 45%),
        radial-gradient(circle at 80% 85%, rgba(0, 168, 132, 0.025), transparent 45%),
        var(--bg);
}

/* Separador de dia */
.day-sep {
    align-self: center;
    margin: 14px 0 10px;
    padding: 4px 12px;
    border-radius: var(--radius-s);
    background: var(--panel);
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
    box-shadow: var(--shadow-1);
    user-select: none;
}

/* ----------------------------------------------------------------------------
   8. Burbujas de mensaje
---------------------------------------------------------------------------- */
.msg {
    display: flex;
    margin: 2px 0;
    animation: msg-in 0.18s ease both;
}

@keyframes msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.msg.in {
    justify-content: flex-start;
}

.msg.out {
    justify-content: flex-end;
}

.msg-bubble {
    position: relative;
    max-width: min(78%, 560px);
    padding: 7px 10px 6px;
    border-radius: var(--radius-m);
    background: var(--in-bubble);
    box-shadow: var(--shadow-1);
    font-size: 14.5px;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
}

.msg.out .msg-bubble {
    background: var(--out-bubble);
}

/* Cola de la burbuja */
.msg.in .msg-bubble::before,
.msg.out .msg-bubble::before {
    content: "";
    position: absolute;
    top: 0;
    width: 9px;
    height: 12px;
}

.msg.in .msg-bubble::before {
    left: -8px;
    background: var(--in-bubble);
    clip-path: polygon(100% 0, 100% 100%, 0 0);
    border-top-left-radius: 2px;
}

.msg.out .msg-bubble::before {
    right: -8px;
    background: var(--out-bubble);
    clip-path: polygon(0 0, 0 100%, 100% 0);
    border-top-right-radius: 2px;
}

.msg.in .msg-bubble {
    border-top-left-radius: 0;
}

.msg.out .msg-bubble {
    border-top-right-radius: 0;
}

/* Mensaje enviado por el bot: acento lateral diferenciado */
.msg.bot .msg-bubble {
    box-shadow: var(--shadow-1), inset -3px 0 0 rgba(6, 207, 156, 0.55);
}

/* Pie de la burbuja: hora + ticks */
.msg-foot {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 5px;
    margin-top: 2px;
    white-space: normal;
}

.msg-time {
    font-size: 11px;
    color: var(--muted);
    user-select: none;
    white-space: nowrap;
}

.msg.out .msg-time {
    color: rgba(233, 237, 239, 0.6);
}

.msg-status {
    font-size: 12px;
    line-height: 1;
    color: var(--muted);
    user-select: none;
    white-space: nowrap;
    letter-spacing: -0.08em;
}

.msg.out .msg-status {
    color: rgba(233, 237, 239, 0.6);
}

.msg-status.read {
    color: var(--read-tick);
}

.msg-status.failed {
    color: var(--danger);
    font-weight: 700;
    letter-spacing: normal;
}

/* Boton de reintento en mensajes fallidos */
.btn-retry {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: 6px;
    padding: 4px 10px;
    border-radius: var(--radius-s);
    border: 1px solid var(--danger);
    color: var(--danger);
    font-size: 12px;
    font-weight: 600;
    background: transparent;
    transition: background 0.15s ease;
}

.btn-retry:hover {
    background: rgba(241, 92, 109, 0.12);
}

.error-detail {
    display: block;
    margin-top: 4px;
    font-size: 12px;
    color: var(--danger);
}

/* Media dentro de la burbuja */
.msg-media {
    margin: -3px -6px 5px;
    border-radius: var(--radius-s);
    overflow: hidden;
}

.msg-media img {
    max-width: 280px;
    max-height: 320px;
    width: auto;
    height: auto;
    border-radius: var(--radius-s);
    cursor: pointer;
    background: var(--bg-2);
}

.msg-media a {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 9px 12px;
    border-radius: var(--radius-s);
    background: rgba(0, 0, 0, 0.22);
    color: var(--text);
    font-size: 13px;
    max-width: 280px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-media a:hover {
    background: rgba(0, 0, 0, 0.35);
    text-decoration: none;
}

/* Cita / contexto de respuesta */
.msg-context {
    margin-bottom: 5px;
    padding: 5px 8px;
    border-left: 3px solid var(--accent);
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.18);
    font-size: 13px;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

/* ----------------------------------------------------------------------------
   9. Aviso de ventana de 24h y composer
---------------------------------------------------------------------------- */
#window-notice {
    flex: 0 0 auto;
    margin: 0 12px 8px;
    padding: 9px 14px;
    border-radius: var(--radius-m);
    background: var(--panel);
    border: 1px solid var(--border);
    color: var(--muted);
    font-size: 13px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}

#window-notice.closed {
    border-color: rgba(247, 185, 85, 0.4);
    color: var(--warning);
}

#composer {
    flex: 0 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 6px;
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    background: var(--panel);
    border-top: 1px solid var(--border);
}

#composer-input {
    flex: 1;
    min-width: 0;
    min-height: 40px;
    max-height: var(--composer-max);
    padding: 10px 14px;
    border-radius: 20px;
    background: var(--panel-2);
    border-color: transparent;
    resize: none;
    overflow-y: auto;
    line-height: 1.4;
    font-size: 15px;
}

#composer-input:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

#composer .icon-btn {
    width: 40px;
    height: 40px;
}

.btn-send-main {
    background: var(--accent);
    color: #06251e;
}

.btn-send-main:hover {
    background: var(--accent-strong);
    color: #06251e;
}

.btn-send-main:disabled {
    background: var(--panel-3);
    color: var(--muted);
}

/* ----------------------------------------------------------------------------
   10. Panel de contacto (hoja deslizante en movil/tablet, columna en desktop)
---------------------------------------------------------------------------- */
#contact-panel {
    position: fixed;
    inset: auto 0 0 0;
    z-index: var(--z-panel);
    max-height: 86dvh;
    display: flex;
    flex-direction: column;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-bottom: 0;
    border-radius: var(--radius-l) var(--radius-l) 0 0;
    box-shadow: var(--shadow-3);
    transform: translateY(105%);
    transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

#contact-panel.open {
    transform: translateY(0);
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-height: 54px;
    padding: 8px 10px 8px 18px;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}

.panel-header h2 {
    font-size: 16px;
    font-weight: 700;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 18px;
    display: flex;
    flex-direction: column;
    gap: 22px;
}

.panel-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.panel-identity {
    align-items: flex-start;
}

#panel-name {
    font-size: 18px;
    font-weight: 700;
    word-break: break-word;
}

#panel-phone {
    color: var(--muted);
    font-size: 14px;
    direction: ltr;
    unicode-bidi: embed;
}

.panel-sec-title {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
    margin: 0;
}

.panel-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    min-height: 24px;
}

#panel-notes {
    min-height: 110px;
}

#btn-save-notes {
    align-self: flex-end;
}

/* ----------------------------------------------------------------------------
   11. Banners de sistema
---------------------------------------------------------------------------- */
#banner-root {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: var(--z-banner);
    display: flex;
    flex-direction: column;
    pointer-events: none;
}

.banner {
    pointer-events: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
    padding: 10px 16px;
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
    background: var(--panel-2);
    border-bottom: 1px solid var(--border-strong);
    color: var(--text);
    font-size: 13px;
    font-weight: 600;
    text-align: center;
    box-shadow: var(--shadow-1);
    animation: banner-in 0.25s ease both;
}

@keyframes banner-in {
    from { transform: translateY(-100%); }
    to { transform: translateY(0); }
}

.banner.warning {
    background: #4a3a14;
    border-bottom-color: var(--warning);
    color: #ffe1a8;
}

.banner.critical {
    background: #4d1f26;
    border-bottom-color: var(--danger);
    color: #ffd4da;
}

.banner .banner-close {
    background: transparent;
    border: 0;
    color: inherit;
    font-size: 16px;
    line-height: 1;
    padding: 4px 8px;
    border-radius: var(--radius-s);
    opacity: 0.8;
}

.banner .banner-close:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.1);
}

/* ----------------------------------------------------------------------------
   12. Toasts
---------------------------------------------------------------------------- */
#toast-root {
    position: fixed;
    left: 50%;
    bottom: calc(18px + env(safe-area-inset-bottom, 0px));
    transform: translateX(-50%);
    z-index: var(--z-toast);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: none;
    width: min(92vw, 420px);
}

.toast {
    pointer-events: auto;
    width: 100%;
    padding: 11px 16px;
    border-radius: var(--radius-m);
    background: var(--panel-2);
    border: 1px solid var(--border-strong);
    color: var(--text);
    font-size: 13.5px;
    box-shadow: var(--shadow-2);
    text-align: center;
    animation: toast-in 0.22s ease both;
}

.toast.ok {
    border-color: var(--ok);
}

.toast.error {
    border-color: var(--danger);
    color: #ffd4da;
}

.toast.leaving {
    animation: toast-out 0.2s ease both;
}

@keyframes toast-in {
    from { opacity: 0; transform: translateY(12px) scale(0.97); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toast-out {
    from { opacity: 1; }
    to { opacity: 0; transform: translateY(8px); }
}

/* ----------------------------------------------------------------------------
   13. Modales
---------------------------------------------------------------------------- */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: var(--z-modal);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    background: rgba(4, 10, 13, 0.72);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    animation: fade-in 0.18s ease both;
    padding: 0;
}

.modal {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 600px;
    max-height: 92dvh;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-l) var(--radius-l) 0 0;
    box-shadow: var(--shadow-3);
    animation: modal-in-mobile 0.25s cubic-bezier(0.32, 0.72, 0, 1) both;
    overflow: hidden;
}

@keyframes modal-in-mobile {
    from { transform: translateY(40px); opacity: 0.6; }
    to { transform: translateY(0); opacity: 1; }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 14px 14px 14px 20px;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}

.modal-header h2,
.modal-header h3 {
    font-size: 16px;
    font-weight: 700;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.modal-body {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 18px 20px;
    min-height: 0;
}

.modal-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    padding: 13px 20px;
    padding-bottom: calc(13px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
    flex: 0 0 auto;
    flex-wrap: wrap;
}

/* Listas dentro de modales (plantillas, reglas, etiquetas) */
.modal-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.modal-list-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-m);
    background: var(--panel);
}

.modal-list-item:hover {
    border-color: var(--border-strong);
}

.modal-list-item .grow {
    flex: 1;
    min-width: 0;
}

.modal-note {
    padding: 9px 12px;
    border-radius: var(--radius-s);
    background: rgba(247, 185, 85, 0.1);
    border: 1px solid rgba(247, 185, 85, 0.35);
    color: var(--warning);
    font-size: 12.5px;
    margin-bottom: 14px;
}

/* Vista previa de plantilla */
.tpl-preview {
    padding: 12px 14px;
    border-radius: var(--radius-m);
    background: var(--in-bubble);
    box-shadow: var(--shadow-1);
    font-size: 14px;
    white-space: pre-wrap;
    word-break: break-word;
}

/* ----------------------------------------------------------------------------
   14. Breakpoint tablet (>= 768px): dos columnas
---------------------------------------------------------------------------- */
@media (min-width: 768px) {
    .only-mobile {
        display: none !important;
    }

    /* Ambas columnas visibles ignorando data-view */
    #app[data-view="list"] #chat,
    #app[data-view="chat"] #sidebar {
        display: flex;
    }

    #sidebar {
        width: var(--sidebar-w);
        min-width: 280px;
        flex: 0 0 auto;
    }

    #chat {
        flex: 1;
    }

    #message-list {
        padding-left: 6%;
        padding-right: 6%;
    }

    /* Panel de contacto como cajon lateral derecho */
    #contact-panel {
        inset: 0 0 0 auto;
        width: min(86vw, 360px);
        max-height: none;
        border-radius: 0;
        border: 0;
        border-left: 1px solid var(--border);
        transform: translateX(105%);
    }

    #contact-panel.open {
        transform: translateX(0);
    }

    /* Modal centrado en pantallas medianas */
    .modal-backdrop {
        align-items: center;
        padding: 24px;
    }

    .modal {
        border-radius: var(--radius-l);
        max-height: 85dvh;
        animation: modal-in 0.22s ease both;
    }

    .modal-footer {
        padding-bottom: 13px;
    }

    #toast-root {
        left: auto;
        right: 20px;
        transform: none;
        align-items: flex-end;
    }
}

@keyframes modal-in {
    from { opacity: 0; transform: translateY(14px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ----------------------------------------------------------------------------
   15. Breakpoint escritorio (>= 1024px): tres columnas
---------------------------------------------------------------------------- */
@media (min-width: 1024px) {
    #sidebar {
        width: var(--sidebar-w);
    }

    /* Panel de contacto integrado como tercera columna */
    #contact-panel {
        position: static;
        display: none;
        width: var(--contact-w);
        flex: 0 0 var(--contact-w);
        transform: none;
        transition: none;
        border-left: 1px solid var(--border);
        box-shadow: none;
        padding-bottom: 0;
    }

    #contact-panel.open {
        display: flex;
        transform: none;
        animation: panel-in 0.2s ease both;
    }

    #message-list {
        padding-left: 7%;
        padding-right: 7%;
    }

    .msg-bubble {
        max-width: min(70%, 560px);
    }
}

@keyframes panel-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* ----------------------------------------------------------------------------
   16. Pantallas muy estrechas y ajustes finos
---------------------------------------------------------------------------- */
@media (max-width: 380px) {
    .chat-head-actions .bot-switch .switch-text {
        display: none;
    }

    #conv-status {
        max-width: 104px;
    }

    .msg-bubble {
        max-width: 86%;
    }
}

/* Soporte hover solo en dispositivos con puntero fino */
@media (hover: none) {
    .conv-item:hover {
        background: transparent;
    }

    .conv-item:active {
        background: var(--panel);
    }
}

/* ----------------------------------------------------------------------------
   17. Accesibilidad: movimiento reducido
---------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
