:root {
    --bg: #efeae2;
    --bg-pattern: rgba(0, 0, 0, 0.035);
    --header: #075e54;
    --header-text: #ffffff;
    --bubble-out: #d9fdd3;
    --bubble-in: #ffffff;
    --text: #111b21;
    --muted: #667781;
    --tick-read: #53bdeb;
    --danger: #d93025;
    --panel: #f0f2f5;
    --input-bg: #ffffff;
    --system-bg: #ffeecd;
    --system-text: #54656f;
    --day-bg: rgba(255, 255, 255, 0.92);
    --shadow: 0 1px 0.5px rgba(11, 20, 26, 0.13);
    --accent: #00a884;
}

:root[data-theme="dark"],
:root[data-theme="black"] {
    --bg: #0b141a;
    --bg-pattern: rgba(255, 255, 255, 0.025);
    --header: #202c33;
    --bubble-out: #005c4b;
    --bubble-in: #202c33;
    --text: #e9edef;
    --muted: #8696a0;
    --panel: #202c33;
    --input-bg: #2a3942;
    --system-bg: #182229;
    --system-text: #8696a0;
    --day-bg: #182229;
    --shadow: 0 1px 0.5px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

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

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.hidden { display: none !important; }

/* ---------------- Header ---------------- */
#header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--header);
    color: var(--header-text);
    flex-shrink: 0;
}

#avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    display: grid;
    place-items: center;
    font-weight: 600;
    flex-shrink: 0;
}

#contact-info { display: flex; flex-direction: column; min-width: 0; flex: 1; }
#contact-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#contact-phone { font-size: 12px; opacity: 0.8; white-space: nowrap; }

.pill {
    font-size: 11px;
    padding: 3px 8px;
    border-radius: 999px;
    white-space: nowrap;
    background: rgba(255, 255, 255, 0.18);
}
.pill.closed { background: rgba(217, 48, 37, 0.85); }

.icon-btn {
    background: transparent;
    border: none;
    color: inherit;
    font-size: 18px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    line-height: 1;
}
.icon-btn:hover { background: rgba(127, 127, 127, 0.15); }
.icon-btn:disabled { opacity: 0.4; cursor: default; }
#resync-btn.spinning { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------- Banner ---------------- */
.banner {
    padding: 6px 12px;
    font-size: 12px;
    background: var(--system-bg);
    color: var(--system-text);
    text-align: center;
    flex-shrink: 0;
}
.banner.error { background: #fde7e9; color: var(--danger); }

/* ---------------- Chat ---------------- */
#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 12px 6%;
    position: relative;
    background-color: var(--bg);
    background-image: radial-gradient(var(--bg-pattern) 1px, transparent 1px);
    background-size: 18px 18px;
}
#chat-container.dragging { outline: 3px dashed var(--accent); outline-offset: -8px; }

#drop-hint {
    position: sticky;
    bottom: 12px;
    margin: 0 auto;
    width: fit-content;
    background: var(--accent);
    color: #fff;
    padding: 8px 14px;
    border-radius: 999px;
    font-size: 13px;
}

#load-older-wrap { text-align: center; margin-bottom: 8px; }
#load-older {
    border: none;
    background: var(--day-bg);
    color: var(--muted);
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 12px;
    cursor: pointer;
    box-shadow: var(--shadow);
}

#messages { display: flex; flex-direction: column; gap: 3px; }

.day-sep {
    align-self: center;
    margin: 10px 0 6px;
    padding: 4px 10px;
    border-radius: 7px;
    font-size: 12px;
    color: var(--muted);
    background: var(--day-bg);
    box-shadow: var(--shadow);
}

.system-msg {
    align-self: center;
    background: var(--system-bg);
    color: var(--system-text);
    padding: 5px 12px;
    border-radius: 7px;
    font-size: 12px;
    margin: 6px 0;
    max-width: 90%;
    text-align: center;
}

.message {
    max-width: min(75%, 520px);
    padding: 6px 8px 4px 9px;
    border-radius: 8px;
    box-shadow: var(--shadow);
    word-wrap: break-word;
    overflow-wrap: anywhere;
    line-height: 1.35;
}
.message.out { align-self: flex-end; background: var(--bubble-out); }
.message.in  { align-self: flex-start; background: var(--bubble-in); }
.message.group-start.out { border-top-right-radius: 0; margin-top: 6px; }
.message.group-start.in  { border-top-left-radius: 0; margin-top: 6px; }
.message.pending { opacity: 0.7; }
.message.failed { outline: 1px solid var(--danger); }

.sender {
    font-size: 12px;
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 2px;
}

.body { white-space: pre-wrap; }
.body a { color: #027eb5; }
:root[data-theme="dark"] .body a, :root[data-theme="black"] .body a { color: #53bdeb; }

.meta {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    color: var(--muted);
    margin-top: 2px;
    white-space: nowrap;
}
.tick { font-size: 12px; letter-spacing: -3px; padding-right: 3px; }
.tick.read { color: var(--tick-read); }
.tick.failed { color: var(--danger); letter-spacing: 0; }

.error-text { font-size: 12px; color: var(--danger); margin-top: 3px; }

/* ---------------- Media ---------------- */
.media {
    display: block;
    max-width: 100%;
    max-height: 280px;
    border-radius: 6px;
    margin-bottom: 4px;
    cursor: zoom-in;
    background: rgba(0, 0, 0, 0.05);
}
.media.sticker { max-height: 140px; background: transparent; cursor: default; }
audio.media { width: 260px; max-width: 100%; cursor: default; height: 40px; }
video.media { cursor: default; }

.doc {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.05);
    color: inherit;
    text-decoration: none;
    margin-bottom: 4px;
    min-width: 200px;
}
.doc:hover { background: rgba(0, 0, 0, 0.09); }
.doc-icon { font-size: 26px; }
.doc-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; display: block; }
.doc-size { font-size: 11px; color: var(--muted); }

.location { display: flex; gap: 8px; align-items: center; color: inherit; text-decoration: none; }
.location-icon { font-size: 26px; }

/* ---------------- Vista previa de archivo ---------------- */
#file-preview {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--panel);
    border-top: 1px solid rgba(127, 127, 127, 0.2);
}
#file-preview-thumb { width: 48px; height: 48px; border-radius: 6px; overflow: hidden; display: grid; place-items: center; font-size: 26px; background: rgba(0,0,0,0.06); flex-shrink: 0; }
#file-preview-thumb img { width: 100%; height: 100%; object-fit: cover; }
#file-preview-info { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
#file-preview-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#file-preview-size { color: var(--muted); font-size: 11px; }
#file-caption { border: none; border-radius: 6px; padding: 6px 8px; background: var(--input-bg); color: var(--text); outline: none; margin-top: 2px; }

/* ---------------- Entrada ---------------- */
#input-area {
    position: relative;
    display: flex;
    align-items: flex-end;
    gap: 6px;
    padding: 8px 10px;
    background: var(--panel);
    flex-shrink: 0;
}

#attach-btn { color: var(--muted); font-size: 20px; padding: 8px; }

#message-input {
    flex: 1;
    resize: none;
    border: none;
    outline: none;
    border-radius: 8px;
    padding: 9px 12px;
    font: inherit;
    color: var(--text);
    background: var(--input-bg);
    max-height: 140px;
    line-height: 1.35;
}

.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: var(--accent);
    color: #fff;
    font-size: 17px;
    cursor: pointer;
    flex-shrink: 0;
}
.send-btn:disabled { opacity: 0.5; cursor: default; }

#attach-menu {
    position: absolute;
    bottom: 58px;
    left: 10px;
    background: var(--input-bg);
    border-radius: 12px;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
    padding: 6px;
    display: flex;
    flex-direction: column;
    z-index: 10;
    min-width: 190px;
}
.attach-option {
    display: flex;
    align-items: center;
    gap: 10px;
    background: transparent;
    border: none;
    padding: 9px 12px;
    border-radius: 8px;
    font: inherit;
    color: var(--text);
    cursor: pointer;
    text-align: left;
}
.attach-option:hover { background: rgba(127, 127, 127, 0.12); }
.attach-icon { width: 22px; text-align: center; font-size: 17px; }

/* ---------------- Plantillas ---------------- */
#tpl-btn { color: var(--muted); font-size: 19px; padding: 8px; }
#tpl-btn.attention { color: var(--accent); background: rgba(0, 168, 132, 0.15); }

#tpl-modal, #mf-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 40;
}
#tpl-box, #mf-box {
    background: var(--panel);
    width: min(560px, 100%);
    max-height: 88vh;
    border-radius: 14px 14px 0 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
#tpl-head, #mf-head { display: flex; align-items: center; gap: 4px; padding: 10px 12px; }
#tpl-title, #mf-title { flex: 1; font-size: 15px; }
#mf-search { margin: 0 12px 8px; }
#mf-list { display: flex; flex-direction: column; gap: 6px; padding: 0 12px 12px; overflow-y: auto; }
#tpl-create-view { padding: 0 12px 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.tpl-lbl { font-size: 12px; color: var(--muted); display: block; margin-bottom: 2px; }
.tpl-row { display: flex; gap: 8px; }
.tpl-row > div { flex: 1; min-width: 0; }
.tpl-hint { font-size: 12px; color: var(--muted); }
#nt-examples { display: flex; flex-direction: column; gap: 6px; }
#tpl-create-view textarea.tpl-input { resize: vertical; font-family: inherit; }
#tpl-list-view, #tpl-form-view { padding: 0 12px 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
#tpl-search, #mf-search, .tpl-input {
    border: 1px solid rgba(127, 127, 127, 0.3);
    border-radius: 8px;
    padding: 8px 10px;
    font: inherit;
    color: var(--text);
    background: var(--input-bg);
    outline: none;
    width: 100%;
}
#tpl-list { display: flex; flex-direction: column; gap: 6px; }
.tpl-item {
    width: 100%;
    text-align: left;
    border: none;
    background: var(--input-bg);
    color: var(--text);
    font: inherit;
    border-radius: 10px;
    padding: 9px 12px;
    cursor: pointer;
    box-shadow: var(--shadow);
}
.tpl-item:hover { outline: 2px solid var(--accent); }
.tpl-item-name { font-weight: 600; display: block; }
.tpl-item-meta { font-size: 11px; color: var(--muted); }
.tpl-item-body { display: block; font-size: 12px; color: var(--muted); margin-top: 3px; white-space: pre-wrap; max-height: 3.9em; overflow: hidden; }
.tpl-empty { text-align: center; color: var(--muted); padding: 18px 8px; }
.link-btn { align-self: flex-start; border: none; background: none; color: var(--accent); font: inherit; cursor: pointer; padding: 2px 0; }
.tpl-var label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
#tpl-preview-label { font-size: 12px; color: var(--muted); }
#tpl-preview { align-self: stretch; max-width: none; white-space: pre-wrap; }
.primary-btn {
    border: none;
    border-radius: 999px;
    background: var(--accent);
    color: #fff;
    font: inherit;
    font-weight: 600;
    padding: 10px 16px;
    cursor: pointer;
}
.primary-btn:disabled { opacity: 0.5; cursor: default; }

/* ---------------- Visor de imagen ---------------- */
#lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    display: grid;
    place-items: center;
    z-index: 50;
    cursor: zoom-out;
}
#lightbox img { max-width: 94vw; max-height: 90vh; border-radius: 4px; }

@media (max-width: 520px) {
    #chat-container { padding: 10px 8px; }
    .pill { max-width: 42vw; overflow: hidden; text-overflow: ellipsis; }
    .message { max-width: 88%; }
}

/* Grabación de notas de voz */
#rec-bar { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel); border-top: 1px solid rgba(127, 127, 127, 0.2); }
#rec-dot { width: 10px; height: 10px; border-radius: 50%; background: #e53935; animation: rec-pulse 1s infinite; }
#rec-time { font-variant-numeric: tabular-nums; font-weight: 600; }
#rec-label { flex: 1; color: var(--muted); font-size: 13px; }
@keyframes rec-pulse { 50% { opacity: 0.25; } }
#mic-btn.recording { color: #e53935; }
