/* =========================================================================
   Mensajero — interfaz tipo Telegram
   ========================================================================= */

:root {
  --bg:            #0e1621;
  --bg-sidebar:    #17212b;
  --bg-panel:      #1c2733;
  --bg-hover:      #202b36;
  --bg-active:     #2b5278;
  --bg-chat:       #0e1621;
  --bubble-in:     #182533;
  --bubble-out:    #2b5278;
  --text:          #e9edf1;
  --text-dim:      #8797a8;
  --accent:        #4e8ef7;
  --accent-2:      #62a0ff;
  --danger:        #e17076;
  --border:        #223040;
  --radius:        14px;
  --shadow:        0 8px 30px rgba(0, 0, 0, .35);
  --sidebar-w:     360px;
}

:root[data-theme="light"] {
  --bg:            #eef2f6;
  --bg-sidebar:    #ffffff;
  --bg-panel:      #ffffff;
  --bg-hover:      #f1f4f8;
  --bg-active:     #d9e7fb;
  --bg-chat:       #e9eef4;
  --bubble-in:     #ffffff;
  --bubble-out:    #d5e7fb;
  --text:          #14202c;
  --text-dim:      #6b7c8f;
  --border:        #dfe6ee;
  --shadow:        0 8px 30px rgba(20, 32, 44, .12);
}

* { box-sizing: border-box; }

/* El atributo hidden siempre gana, aunque la clase declare display */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

button { font: inherit; color: inherit; }

.icon-btn {
  display: grid; place-items: center;
  width: 40px; height: 40px;
  border: 0; border-radius: 50%;
  background: transparent; color: var(--text-dim);
  cursor: pointer; flex: none;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

/* ------------------------------------------------------------------ acceso */

.auth-screen {
  min-height: 100dvh;
  display: grid; place-items: center;
  padding: 24px;
  background: radial-gradient(1200px 600px at 50% -10%, #23415f 0%, var(--bg) 60%);
}

.auth-card {
  width: min(400px, 100%);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: var(--shadow);
  text-align: center;
}
.auth-logo { margin-bottom: 12px; }
.auth-card h1 { margin: 0; font-size: 26px; letter-spacing: -.3px; }
.auth-sub { margin: 6px 0 22px; color: var(--text-dim); font-size: 14px; }

.tabs { display: flex; gap: 4px; background: var(--bg-hover); padding: 4px; border-radius: 12px; margin-bottom: 20px; }
.tab {
  flex: 1; padding: 9px; border: 0; border-radius: 9px;
  background: transparent; color: var(--text-dim); cursor: pointer; font-weight: 600; font-size: 14px;
  transition: background .15s, color .15s;
}
.tab.active { background: var(--accent); color: #fff; }

.field { display: block; text-align: left; margin-bottom: 14px; }
.field > span { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; font-weight: 600; letter-spacing: .3px; }
.field input, .modal input, .modal textarea {
  width: 100%; padding: 12px 14px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  font: inherit; outline: none;
  transition: border-color .15s;
}
.field input:focus, .modal input:focus { border-color: var(--accent); }

.btn-primary {
  width: 100%; padding: 13px; margin-top: 6px;
  background: var(--accent); color: #fff;
  border: 0; border-radius: 10px;
  font-weight: 600; cursor: pointer;
  transition: background .15s, transform .1s;
}
.btn-primary:hover { background: var(--accent-2); }
.btn-primary:active { transform: scale(.99); }
.btn-primary:disabled { opacity: .6; cursor: default; }

.auth-error {
  background: rgba(225, 112, 118, .12);
  color: var(--danger);
  border-radius: 9px; padding: 10px 12px; font-size: 13px; margin-bottom: 12px;
}

/* --------------------------------------------------------------- estructura */

.app { display: flex; height: 100dvh; overflow: hidden; }

.sidebar {
  width: var(--sidebar-w); flex: none;
  display: flex; flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  position: relative;
}

.sidebar-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); }

.search-wrap { position: relative; flex: 1; }
.search-icon { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; color: var(--text-dim); }
#searchInput {
  width: 100%; padding: 10px 12px 10px 38px;
  background: var(--bg-hover); color: var(--text);
  border: 1px solid transparent; border-radius: 20px;
  font: inherit; outline: none;
}
#searchInput:focus { border-color: var(--accent); background: var(--bg); }

.chat-list { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 6px 0 80px; }

.list-label { padding: 12px 16px 6px; font-size: 12px; font-weight: 700; color: var(--text-dim); text-transform: uppercase; letter-spacing: .6px; }

.chat-item {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 12px; margin: 1px 6px;
  border-radius: 12px; cursor: pointer;
  transition: background .12s;
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-active); }
.chat-item.active .chat-preview, .chat-item.active .chat-time { color: rgba(255,255,255,.75); }
:root[data-theme="light"] .chat-item.active .chat-preview { color: var(--text-dim); }

.avatar {
  width: 48px; height: 48px; flex: none;
  border-radius: 50%;
  display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 18px;
  position: relative;
  user-select: none;
}
.avatar.sm { width: 38px; height: 38px; font-size: 15px; }
.avatar .dot {
  position: absolute; right: 0; bottom: 1px;
  width: 12px; height: 12px; border-radius: 50%;
  background: #4dcb5f; border: 2.5px solid var(--bg-sidebar);
}

.chat-item-body { flex: 1; min-width: 0; }
.chat-item-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { font-size: 12px; color: var(--text-dim); flex: none; }
.chat-item-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.chat-preview { font-size: 14px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  min-width: 22px; height: 22px; padding: 0 7px;
  background: var(--accent); color: #fff;
  border-radius: 11px; font-size: 12px; font-weight: 700;
  display: grid; place-items: center; flex: none;
}

.fab {
  position: absolute; right: 18px; bottom: 18px;
  width: 54px; height: 54px; border: 0; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; cursor: pointer;
  box-shadow: 0 4px 16px rgba(78, 142, 247, .45);
  transition: transform .15s, background .15s;
}
.fab:hover { background: var(--accent-2); transform: scale(1.05); }
.fab svg { width: 26px; height: 26px; }

/* ------------------------------------------------------------ conversación */

.chat-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg-chat); position: relative; }

.empty-state { flex: 1; display: grid; place-items: center; }
.empty-bubble {
  background: var(--bubble-in); color: var(--text-dim);
  padding: 9px 18px; border-radius: 20px; font-size: 14px;
}

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

.chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 14px;
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  flex: none;
}
.chat-head .avatar { width: 40px; height: 40px; font-size: 16px; }
.chat-head-info { flex: 1; min-width: 0; }
.chat-head-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head-sub { font-size: 13px; color: var(--text-dim); }
.chat-head-sub.online { color: var(--accent); }
.back-btn { display: none; }

.messages {
  flex: 1; overflow-y: auto;
  padding: 18px 5% 12px;
  display: flex; flex-direction: column; gap: 2px;
  scroll-behavior: smooth;
}

.day-sep { align-self: center; margin: 14px 0 8px; padding: 4px 12px; background: rgba(0,0,0,.28); border-radius: 12px; font-size: 12px; color: #cfd9e4; }
:root[data-theme="light"] .day-sep { background: rgba(20,32,44,.08); color: var(--text-dim); }

.sys-msg { align-self: center; margin: 8px 0; padding: 5px 14px; background: rgba(0,0,0,.25); border-radius: 14px; font-size: 13px; color: #cfd9e4; text-align: center; max-width: 80%; }
:root[data-theme="light"] .sys-msg { background: rgba(20,32,44,.07); color: var(--text-dim); }

.msg { max-width: min(560px, 78%); display: flex; flex-direction: column; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.in  { align-self: flex-start; }
.msg + .msg { margin-top: 2px; }
.msg.first { margin-top: 10px; }

.bubble {
  position: relative;
  padding: 7px 12px 6px;
  border-radius: 14px;
  background: var(--bubble-in);
  word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap;
}
.msg.out .bubble { background: var(--bubble-out); }
.msg.in.first  .bubble { border-top-left-radius: 6px; }
.msg.out.first .bubble { border-top-right-radius: 6px; }

.msg-sender { font-size: 13px; font-weight: 700; color: var(--accent-2); margin-bottom: 2px; }

.msg-reply {
  border-left: 3px solid var(--accent);
  padding: 3px 8px; margin-bottom: 5px;
  background: rgba(255,255,255,.07); border-radius: 5px;
  font-size: 13px; color: var(--text-dim);
}
.msg-reply b { display: block; color: var(--accent-2); }

.msg-meta { float: right; margin: 6px 0 -2px 10px; font-size: 11px; color: var(--text-dim); display: inline-flex; align-items: center; gap: 3px; }
.msg.out .msg-meta { color: rgba(255,255,255,.6); }
:root[data-theme="light"] .msg.out .msg-meta { color: var(--text-dim); }
.msg-meta svg { width: 15px; height: 15px; stroke-width: 2.4; }
.msg-meta .read { color: #62d16a; }

.msg-img { display: block; max-width: 100%; border-radius: 10px; margin: 2px 0 4px; cursor: pointer; }

.msg-file { display: flex; align-items: center; gap: 10px; padding: 6px 4px; text-decoration: none; color: inherit; }
.msg-file .file-ico { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); display: grid; place-items: center; flex: none; color: #fff; }
.msg-file .file-meta { min-width: 0; }
.msg-file .file-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-file .file-size { font-size: 12px; color: var(--text-dim); }

.msg-actions { display: none; gap: 2px; margin: 2px 4px 0; }
.msg:hover .msg-actions { display: flex; }
.msg-actions button { border: 0; background: transparent; color: var(--text-dim); font-size: 12px; cursor: pointer; padding: 2px 6px; border-radius: 6px; }
.msg-actions button:hover { background: var(--bg-hover); color: var(--text); }

.typing-row { align-self: flex-start; padding: 8px 14px; color: var(--text-dim); font-size: 13px; display: flex; align-items: center; gap: 6px; }
.typing-dots { display: inline-flex; gap: 3px; }
.typing-dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--text-dim); animation: blink 1.2s infinite; }
.typing-dots i:nth-child(2) { animation-delay: .2s; }
.typing-dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25 } 30% { opacity: 1 } }

/* -------------------------------------------------------------- redactor */

.reply-bar { display: flex; align-items: center; gap: 8px; padding: 8px 14px; background: var(--bg-sidebar); border-top: 1px solid var(--border); }
.reply-info { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 9px; font-size: 13px; color: var(--text-dim); }
.reply-info b { display: block; color: var(--accent-2); }
.reply-info span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg-sidebar); border-top: 1px solid var(--border);
  flex: none;
}
#composerInput {
  flex: 1; resize: none; max-height: 160px;
  padding: 11px 14px;
  background: var(--bg-hover); color: var(--text);
  border: 1px solid transparent; border-radius: 20px;
  font: inherit; outline: none;
}
#composerInput:focus { border-color: var(--accent); background: var(--bg); }

.send-btn {
  width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; cursor: pointer;
  transition: background .15s, transform .1s;
}
.send-btn:hover { background: var(--accent-2); }
.send-btn:active { transform: scale(.93); }

/* --------------------------------------------------------------- modales */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, .55);
  display: grid; place-items: center; padding: 20px;
  backdrop-filter: blur(2px);
}
.modal {
  width: min(440px, 100%); max-height: 82dvh;
  display: flex; flex-direction: column;
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 14px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 16px 20px 20px; overflow-y: auto; }
.modal-body .field { margin-bottom: 14px; }

.people-list { display: flex; flex-direction: column; gap: 2px; margin: 4px -8px 0; }
.person {
  display: flex; align-items: center; gap: 11px;
  padding: 8px; border-radius: 10px; cursor: pointer;
  transition: background .12s;
}
.person:hover { background: var(--bg-hover); }
.person.checked { background: var(--bg-active); }
.person-info { flex: 1; min-width: 0; }
.person-name { font-weight: 600; }
.person-sub { font-size: 13px; color: var(--text-dim); }

.menu-item { display: flex; align-items: center; gap: 12px; padding: 12px 8px; border-radius: 10px; cursor: pointer; }
.menu-item:hover { background: var(--bg-hover); }
.menu-item.danger { color: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #2b3947; color: #fff; padding: 11px 20px; border-radius: 22px;
  box-shadow: var(--shadow); z-index: 90; font-size: 14px;
}

/* --------------------------------------------------------------- responsive */

@media (max-width: 820px) {
  :root { --sidebar-w: 100%; }
  .sidebar { border-right: 0; }
  .app.chat-open .sidebar { display: none; }
  .app:not(.chat-open) .chat-pane { display: none; }
  .back-btn { display: grid; }
  .messages { padding-left: 10px; padding-right: 10px; }
  .msg { max-width: 86%; }
}

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: #38495c; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }
