:root {
  --primary: #128c7e;
  --primary-dark: #075e54;
  --accent: #34b7f1;
  --bg-chat: #e5ddd5;
  --bg-panel: #ffffff;
  --bg-sidebar: #f7f7f7;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --text-dark: #111b21;
  --text-light: #667781;
  --border: #e9edef;
  --radius: 12px;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* ===== Tema oscuro: manual (data-theme) o automático (prefers-color-scheme) ===== */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-chat: #0b141a; --bg-panel: #1f2c34; --bg-sidebar: #111b21;
    --bubble-out: #005c4b; --bubble-in: #1f2c34;
    --text-dark: #e9edef; --text-light: #8696a0; --border: #2a3942;
  }
}
:root[data-theme="dark"] {
  --bg-chat: #0b141a; --bg-panel: #1f2c34; --bg-sidebar: #111b21;
  --bubble-out: #005c4b; --bubble-in: #1f2c34;
  --text-dark: #e9edef; --text-light: #8696a0; --border: #2a3942;
}
body { background: var(--bg-chat); color: var(--text-dark); }
* { box-sizing: border-box; }
body { margin:0; font-family: -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; }

/* ===== Auth screen ===== */
.auth-body {
  min-height: 100vh; display:flex; align-items:center; justify-content:center;
  background: linear-gradient(135deg, var(--primary-dark), var(--accent));
  padding: 20px;
}
.auth-card {
  background: var(--bg-panel); width: 100%; max-width: 420px; border-radius: 16px;
  padding: 32px 28px; box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.brand { text-align:center; margin-bottom: 20px; }
.brand-logo { width:64px; height:64px; border-radius:16px; }
.brand h1 { margin: 10px 0 4px; color: var(--primary-dark); }
.tagline { color: var(--text-light); margin:0; font-size: 14px; }

.tabs { display:flex; border-bottom: 2px solid var(--border); margin-bottom: 18px; }
.tab-btn {
  flex:1; padding: 10px; background:none; border:none; font-size:15px; font-weight:600;
  color: var(--text-light); cursor:pointer; border-bottom: 3px solid transparent;
}
.tab-btn.active { color: var(--primary); border-color: var(--primary); }
.tab-panel { display:none; }
.tab-panel.active { display:block; }

.method-switch { display:flex; gap:8px; margin-bottom:14px; }
.method-btn {
  flex:1; padding:8px; border-radius:20px; border:1px solid var(--border); background:#fff;
  color: var(--text-light); cursor:pointer; font-size:13px;
}
.method-btn.active { background: var(--primary); color:#fff; border-color: var(--primary); }
.method-form { display:none; flex-direction:column; gap:12px; }
.method-form.active { display:flex; }

input[type=text], input[type=email], input[type=password], input[type=tel] {
  padding: 12px 14px; border-radius: 10px; border: 1px solid var(--border); font-size: 16px; width:100%;
  background: var(--bg-panel); color: var(--text-dark);
}
input:focus { outline: 2px solid var(--primary); border-color: var(--primary); }

.btn-primary {
  background: var(--primary); color:#fff; border:none; padding: 12px; border-radius: 10px;
  font-size: 15px; font-weight:600; cursor:pointer;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary {
  background: #fff; color: var(--primary); border: 1px solid var(--primary); padding:10px; border-radius:10px;
  cursor:pointer; font-weight:600;
}
.error-msg { color:#d32f2f; font-size:13px; text-align:center; min-height: 18px; margin-top: 10px; }

/* ===== Chat layout ===== */
.chat-app { display:flex; height:100vh; height:100dvh; background: var(--bg-sidebar); overflow:hidden; }
.sidebar {
  width: 340px; min-width: 280px; background: var(--bg-sidebar); border-right:1px solid var(--border);
  display:flex; flex-direction:column;
}
.sidebar-header {
  background: var(--primary-dark); color:#fff; padding: 14px 16px; display:flex; align-items:center; justify-content:space-between;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
}
.sidebar-header .me { display:flex; align-items:center; gap:10px; cursor:pointer; border-radius:8px; padding:4px 6px; margin:-4px -6px; transition:background .15s; }
.sidebar-header .me:hover { background: rgba(255,255,255,.1); }
.avatar { width:40px; height:40px; border-radius:50%; background:var(--accent); color:#fff; display:flex; align-items:center; justify-content:center; font-weight:700; flex-shrink:0; }
.icon-btn { background:none; border:none; color:#fff; font-size:18px; cursor:pointer; padding:8px; border-radius:8px; transition: background .15s; }
.icon-btn:hover { background: rgba(255,255,255,.15); }
.search-box { padding: 10px; }
.search-box input { width:100%; padding:10px 14px; border-radius:20px; border:1px solid var(--border); font-size:15px; background: var(--bg-panel); color: var(--text-dark); }
.conversation-list { flex:1; overflow-y:auto; -webkit-overflow-scrolling: touch; }
.conversation-item {
  display:flex; gap:12px; padding: 12px 16px; cursor:pointer; border-bottom:1px solid var(--border); align-items:center;
  transition: background .12s;
}
.conversation-item:hover, .conversation-item.active { background: var(--border); }
.conv-info { flex:1; min-width:0; }
.conv-name { font-weight:600; color: var(--text-dark); }
.conv-preview { font-size: 13px; color: var(--text-light); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.conv-name.unread, .conv-preview.unread { font-weight:700; color: var(--text-dark); }
.unread-badge { background: var(--primary); color:#fff; font-size:11px; font-weight:700; min-width:20px; height:20px; border-radius:10px; display:flex; align-items:center; justify-content:center; padding:0 6px; }
.online-dot { width:10px; height:10px; border-radius:50%; background:#4caf50; position:relative; left:-14px; top:14px; border:2px solid var(--bg-sidebar); }

.chat-main { flex:1; display:flex; flex-direction:column; background: var(--bg-chat); min-width:0; }
.chat-header { background: var(--bg-panel); padding: 12px 18px; border-bottom:1px solid var(--border); display:flex; align-items:center; gap:12px; flex-shrink:0; }
.chat-header .peer-name { font-weight:600; }
.chat-header .peer-status { font-size:12px; color: var(--text-light); }
.messages { flex:1; overflow-y:auto; -webkit-overflow-scrolling: touch; padding: 20px; display:flex; flex-direction:column; gap:8px; }
.msg-row { display:flex; animation: msg-in .15s ease-out; }
.msg-row.out { justify-content:flex-end; }
@keyframes msg-in { from { opacity:0; transform: translateY(6px); } to { opacity:1; transform: translateY(0); } }
.bubble {
  max-width: 65%; padding: 8px 12px; border-radius: var(--radius); font-size:14.5px; line-height:1.4;
  box-shadow: 0 1px 1px rgba(0,0,0,.1); position:relative; word-wrap: break-word;
}
.msg-row.in .bubble { background: var(--bubble-in); border-top-left-radius:2px; color: var(--text-dark); }
.msg-row.out .bubble { background: var(--bubble-out); border-top-right-radius:2px; color: var(--text-dark); }
.bubble .sender { font-size:12px; font-weight:700; color: var(--primary); display:block; margin-bottom:2px; }
.bubble .time { font-size: 10px; color: var(--text-light); text-align:right; margin-top:4px; }
.bubble .attachment { display:block; margin-top:6px; }
.bubble img.attachment-img, .bubble video.attachment-img { max-width: 240px; border-radius:8px; display:block; }
.bubble .file-chip { display:flex; align-items:center; gap:8px; background:rgba(0,0,0,.06); padding:8px 10px; border-radius:8px; text-decoration:none; color:var(--text-dark); }

.typing-indicator { font-size:12px; color: var(--text-light); padding: 0 20px 6px; font-style:italic; min-height: 18px; flex-shrink:0; }

.composer {
  display:flex; gap:10px; padding: 10px 14px; background: var(--bg-panel); border-top:1px solid var(--border);
  align-items:center; flex-shrink:0;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
}
.composer input[type=text] {
  flex:1; padding:12px 16px; border-radius: 24px; border:1px solid var(--border); font-size:16px;
  background: var(--bg-sidebar); color: var(--text-dark);
}
.composer button, .composer .icon-btn.attach-btn {
  background: var(--primary); border:none; color:#fff; width:42px; height:42px; border-radius:50%; cursor:pointer;
  font-size:18px; flex-shrink:0; display:flex; align-items:center; justify-content:center; transition: transform .1s;
}
.composer button:active { transform: scale(.92); }
.attach-btn { background: transparent !important; color: var(--text-light) !important; font-size:22px; }
.attach-btn:hover { background: var(--border) !important; }

.empty-state { flex:1; display:flex; align-items:center; justify-content:center; color: var(--text-light); flex-direction:column; gap:10px; text-align:center; padding: 20px; }
.empty-state h2 { font-weight:600; color: var(--text-dark); }

.modal-overlay { position:fixed; inset:0; background:rgba(0,0,0,.5); display:none; align-items:center; justify-content:center; z-index:50; padding:16px; }
.modal-overlay.active { display:flex; animation: fade-in .15s ease-out; }
@keyframes fade-in { from { opacity:0; } to { opacity:1; } }
.modal-box {
  background: var(--bg-panel); color: var(--text-dark); border-radius:14px; padding:20px; width:100%; max-width:400px;
  max-height:85vh; overflow-y:auto; box-shadow: 0 20px 60px rgba(0,0,0,.35); animation: modal-in .18s ease-out;
}
@keyframes modal-in { from { opacity:0; transform: translateY(12px) scale(.98); } to { opacity:1; transform: translateY(0) scale(1); } }
.user-result { display:flex; align-items:center; gap:10px; padding:10px; cursor:pointer; border-radius:8px; }
.user-result:hover { background: var(--bg-sidebar); }

/* ===== Banner de notificaciones ===== */
.notif-banner {
  display:none; align-items:center; gap:10px; background: var(--primary); color:#fff;
  padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  font-size: 13px; position:fixed; top:0; left:0; right:0; z-index:60;
}
.notif-banner.visible { display:flex; }
.notif-banner button {
  background:#fff; color: var(--primary); border:none; padding:6px 12px; border-radius:16px; font-weight:700;
  font-size:12px; cursor:pointer; white-space:nowrap;
}
.notif-banner .dismiss { background:transparent; color:#fff; font-size:16px; padding:2px 6px; font-weight:400; margin-left:auto; }
body.has-banner .chat-app { padding-top: 52px; }

/* ===== Aviso de nueva versión disponible ===== */
.update-toast {
  display:none; align-items:center; gap:12px; justify-content:center; background:#1f2c34; color:#fff;
  padding: 12px 16px; padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  font-size: 13px; position:fixed; bottom:0; left:0; right:0; z-index:70; box-shadow: 0 -4px 16px rgba(0,0,0,.25);
}
.update-toast.visible { display:flex; animation: toast-in .25s ease-out; }
@keyframes toast-in { from { transform: translateY(100%); } to { transform: translateY(0); } }
.update-toast button {
  background: var(--primary); color:#fff; border:none; padding:8px 14px; border-radius:16px; font-weight:700;
  font-size:12px; cursor:pointer; white-space:nowrap;
}

/* ===== Loading skeleton ===== */
.spinner { width:28px; height:28px; border:3px solid var(--border); border-top-color: var(--primary); border-radius:50%; animation: spin .7s linear infinite; margin: 20px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Admin panel ===== */
.admin-body { background:#f3f4f6; margin:0; font-family: -apple-system, Roboto, sans-serif; }
.admin-header { background: var(--primary-dark); color:#fff; padding:16px 24px; display:flex; justify-content:space-between; align-items:center; }
.admin-wrap { padding: 24px; max-width: 1100px; margin: 0 auto; }
.stat-grid { display:grid; grid-template-columns: repeat(auto-fit,minmax(160px,1fr)); gap:16px; margin-bottom:24px; }
.stat-card { background:#fff; border-radius:12px; padding:18px; text-align:center; box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.stat-card .num { font-size:28px; font-weight:700; color: var(--primary-dark); }
.stat-card .label { color: var(--text-light); font-size:13px; }
.admin-section { background:#fff; border-radius:12px; padding:20px; margin-bottom:20px; box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.admin-section h2 { margin-top:0; font-size:17px; }
table { width:100%; border-collapse: collapse; }
th, td { text-align:left; padding: 10px 8px; border-bottom:1px solid var(--border); font-size:14px; }
.badge { padding:3px 8px; border-radius:12px; font-size:11px; font-weight:600; }
.badge.online { background:#e6f7ee; color:#2e7d32; }
.badge.banned { background:#fdecea; color:#c62828; }
.badge.admin { background:#e3f2fd; color:#1565c0; }
.settings-form { display:flex; flex-direction:column; gap:12px; max-width:500px; }
.settings-row { display:flex; justify-content:space-between; align-items:center; gap:12px; }
.small-btn { padding:6px 10px; border-radius:6px; border:none; cursor:pointer; font-size:12px; font-weight:600; }
.small-btn.ban { background:#fdecea; color:#c62828; }
.small-btn.unban { background:#e6f7ee; color:#2e7d32; }

/* ===== Botón "volver" (solo visible en móvil) ===== */
.back-btn { display:none; font-size:20px; color: var(--text-dark); margin-right:4px; }

@media (max-width: 720px) {
  .sidebar { position:absolute; inset:0; z-index:10; width:100%; }
  .sidebar.hidden { display:none; }
  .chat-main.hidden { display:none; }
  .back-btn { display:inline-block; }
  .bubble { max-width: 80%; }
}

/* ===== Modal de adjuntar archivo ===== */
.attach-box { max-width: 340px; }
.attach-options { display:flex; gap:12px; margin-top: 14px; }
.attach-option {
  flex:1; display:flex; flex-direction:column; align-items:center; gap:8px;
  background: var(--bg-sidebar); border:1px solid var(--border); border-radius:12px;
  padding: 16px 8px; font-size: 28px; cursor:pointer; color: var(--text-dark);
}
.attach-option span { font-size: 12px; color: var(--text-light); }
.attach-option:hover { background: var(--border); }

/* ===== Menú de ajustes ===== */
.settings-box { max-width: 480px; }
.settings-header { display:flex; justify-content:space-between; align-items:center; margin-bottom: 10px; }
.settings-tabs { display:flex; gap:4px; border-bottom: 2px solid var(--border); margin-bottom: 16px; overflow-x:auto; }
.settings-tab {
  padding: 8px 10px; background:none; border:none; color: var(--text-light); font-weight:600;
  font-size: 13px; cursor:pointer; border-bottom: 3px solid transparent; white-space:nowrap;
}
.settings-tab.active { color: var(--primary); border-color: var(--primary); }
.settings-panel { display:none; flex-direction:column; gap:14px; }
.settings-panel.active { display:flex; }
.settings-panel label { display:flex; flex-direction:column; gap:6px; font-size:13px; color: var(--text-light); }
.settings-panel input[type=text], .settings-panel select {
  padding: 10px 12px; border-radius:8px; border:1px solid var(--border); font-size:16px;
  background: var(--bg-panel); color: var(--text-dark);
}
.settings-hint { font-size:12px; color: var(--text-light); margin:0; }
.btn-secondary.danger { border-color:#d32f2f; color:#d32f2f; }
.device-row { display:flex; justify-content:space-between; align-items:center; padding:10px 0; border-bottom:1px solid var(--border); font-size:14px; }
hr { border:none; border-top:1px solid var(--border); margin: 4px 0; }

/* ===== Toggle switches para notificaciones/sonido (encima de checkbox nativo) ===== */
.settings-row input[type=checkbox] { width:42px; height:24px; appearance:none; background:var(--border); border-radius:12px; position:relative; cursor:pointer; transition:.2s; flex-shrink:0; }
.settings-row input[type=checkbox]::before { content:''; position:absolute; width:20px; height:20px; border-radius:50%; background:#fff; top:2px; left:2px; transition:.2s; box-shadow:0 1px 3px rgba(0,0,0,.3); }
.settings-row input[type=checkbox]:checked { background: var(--primary); }
.settings-row input[type=checkbox]:checked::before { left:20px; }
