:root{
  --bg:#f6f1e7; --panel:#fffdf8; --ink:#1d2622; --muted:#6c756f; --line:#e6dfd1;
  --brand:#1f3a34; --brand-2:#2f5a4f; --accent:#d9734e; --accent-ink:#fff;
  --mine:#dcefe6; --theirs:#ffffff; --danger:#d64545; --ok:#2f9e6a;
  --radius:18px; --shadow:0 10px 30px rgba(31,58,52,.12);
  --font:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
  color-scheme:light;
}
@media (prefers-color-scheme:dark){:root{
  --bg:#121816; --panel:#1a2220; --ink:#e8eee9; --muted:#93a19a; --line:#26312d;
  --brand:#8fd3b8; --brand-2:#6dbb9f; --accent:#e88a63; --mine:#1f4038; --theirs:#212b28;
  --shadow:0 10px 30px rgba(0,0,0,.4); color-scheme:dark;
}}
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
html,body{margin:0;height:100%;background:var(--bg);color:var(--ink);font:16px/1.4 var(--font)}
body{overscroll-behavior:none}
button,input,textarea{font:inherit;color:inherit}
button{cursor:pointer}
.hidden{display:none!important}
svg{width:22px;height:22px;fill:none;stroke:currentColor;stroke-width:1.9;stroke-linecap:round;stroke-linejoin:round}

/* ---------- botones ---------- */
.primary{background:var(--accent);color:var(--accent-ink);border:0;border-radius:12px;padding:.75rem 1.1rem;font-weight:600}
.primary:disabled{opacity:.6}
.ghost{background:transparent;border:1px solid var(--line);border-radius:12px;padding:.6rem 1rem}
.danger-text{color:var(--danger)}
.icon-btn{display:inline-grid;place-items:center;width:42px;height:42px;border-radius:50%;border:0;background:transparent;color:var(--brand);flex:none}
.icon-btn:hover{background:color-mix(in srgb,var(--brand) 10%,transparent)}

/* ---------- acceso ---------- */
.auth{min-height:100%;display:grid;place-items:center;padding:24px;
  background:radial-gradient(1200px 600px at 10% -10%,color-mix(in srgb,var(--accent) 22%,transparent),transparent),
             radial-gradient(900px 500px at 110% 110%,color-mix(in srgb,var(--brand-2) 25%,transparent),transparent),var(--bg)}
.auth-card{width:100%;max-width:380px;background:var(--panel);border-radius:24px;padding:28px;box-shadow:var(--shadow)}
.brand{display:flex;align-items:center;gap:12px}
.brand img{width:48px;height:48px;border-radius:14px}
.brand h1{margin:0;font-size:2rem;letter-spacing:-.02em;color:var(--brand)}
.tagline{color:var(--muted);margin:.5rem 0 1.25rem}
.tabs{display:flex;background:var(--bg);border-radius:12px;padding:4px;margin-bottom:16px}
.tabs button{flex:1;border:0;background:transparent;padding:.55rem;border-radius:9px;font-weight:600;color:var(--muted)}
.tabs button.active{background:var(--panel);color:var(--ink);box-shadow:0 1px 3px rgba(0,0,0,.08)}
form label{display:block;font-size:.85rem;color:var(--muted);margin-bottom:12px}
form input,.field{display:block;width:100%;margin-top:4px;padding:.75rem .85rem;border-radius:12px;border:1px solid var(--line);background:var(--bg);font-size:16px;outline:none}
form input:focus,.field:focus{border-color:var(--brand-2)}
#auth-form .primary{width:100%;margin-top:6px}
.auth:not(.register) .reg-only{display:none}
.error{color:var(--danger);font-size:.9rem;min-height:1.2em;margin:4px 0}

/* ---------- layout ---------- */
.app{display:grid;grid-template-columns:340px 1fr;height:100dvh;overflow:hidden}
.sidebar{display:flex;flex-direction:column;border-right:1px solid var(--line);background:var(--panel);min-height:0;padding-top:env(safe-area-inset-top)}
.side-head{display:flex;align-items:center;gap:10px;padding:12px 12px 6px}
.side-head h2{flex:1;margin:0;font-size:1.5rem;letter-spacing:-.02em}
.avatar-btn{border:0;padding:0;background:none}
.search{padding:4px 12px 8px}
.search input{width:100%;padding:.6rem .9rem;border-radius:12px;border:0;background:var(--bg);outline:none}
.room-list{list-style:none;margin:0;padding:0 6px 12px;overflow-y:auto;flex:1}
.room-list li{display:flex;gap:12px;align-items:center;padding:10px;border-radius:14px;cursor:pointer}
.room-list li:hover{background:var(--bg)}
.room-list li.active{background:color-mix(in srgb,var(--brand-2) 14%,transparent)}
.room-meta{flex:1;min-width:0}
.room-top{display:flex;justify-content:space-between;gap:8px}
.room-top strong{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.room-top time{color:var(--muted);font-size:.78rem;flex:none}
.room-bottom{display:flex;justify-content:space-between;gap:8px;color:var(--muted);font-size:.9rem}
.room-bottom span{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.badge{background:var(--accent);color:#fff;border-radius:999px;min-width:22px;height:22px;padding:0 7px;font-size:.75rem;font-weight:700;display:grid;place-items:center;flex:none}
.empty-side{padding:30px 20px;text-align:center;color:var(--muted)}

.avatar{position:relative;width:46px;height:46px;border-radius:50%;display:grid;place-items:center;color:#fff;font-weight:700;flex:none;font-size:1.05rem}
.avatar.small{width:36px;height:36px;font-size:.9rem}
.avatar.big{width:110px;height:110px;font-size:2.6rem}
.avatar.online::after{content:"";position:absolute;right:1px;bottom:1px;width:12px;height:12px;border-radius:50%;background:var(--ok);border:2px solid var(--panel)}

/* ---------- chat ---------- */
.chat{position:relative;display:flex;flex-direction:column;min-height:0;min-width:0;
  background:var(--bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Ccircle cx='2' cy='2' r='1' fill='%23888' fill-opacity='.10'/%3E%3C/svg%3E")}
.chat-empty{margin:auto;text-align:center;color:var(--muted)}
.chat-empty img{width:72px;border-radius:20px;opacity:.85}
.chat-head{display:flex;align-items:center;gap:8px;padding:8px 10px;padding-top:calc(8px + env(safe-area-inset-top));background:var(--panel);border-bottom:1px solid var(--line)}
.chat-head .back{display:none}
.chat-title{flex:1;min-width:0;display:flex;flex-direction:column}
.chat-title strong{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.chat-title small{color:var(--muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.messages{flex:1;overflow-y:auto;padding:14px 12px 8px;display:flex;flex-direction:column;gap:2px}
.day{align-self:center;background:var(--panel);color:var(--muted);font-size:.78rem;padding:4px 10px;border-radius:999px;margin:12px 0 8px;box-shadow:0 1px 2px rgba(0,0,0,.06)}
.msg{max-width:min(78%,560px);padding:7px 11px 6px;border-radius:var(--radius);background:var(--theirs);box-shadow:0 1px 1px rgba(0,0,0,.06);align-self:flex-start;word-wrap:break-word;overflow-wrap:anywhere;position:relative}
.msg.mine{align-self:flex-end;background:var(--mine)}
.msg.first{margin-top:8px}
.msg .who{font-size:.8rem;font-weight:700;margin-bottom:2px}
.msg .text{white-space:pre-wrap}
.msg .text a{color:var(--brand-2)}
.msg .meta{float:right;margin:6px 0 -2px 10px;font-size:.7rem;color:var(--muted);display:flex;gap:3px;align-items:center}
.msg .meta svg{width:15px;height:15px}
.msg .meta .read{color:#3a86ff}
.msg.pending{opacity:.6}
.msg img.media,.msg video.media{display:block;max-width:100%;max-height:340px;border-radius:12px;margin:2px 0 4px;cursor:zoom-in;background:var(--line)}
.msg audio{width:240px;max-width:100%;margin:2px 0}
.file-card{display:flex;gap:10px;align-items:center;padding:8px;border-radius:12px;background:color-mix(in srgb,var(--brand) 7%,transparent);text-decoration:none;color:inherit;margin:2px 0 4px}
.file-card .ext{width:40px;height:44px;border-radius:8px;background:var(--brand-2);color:#fff;display:grid;place-items:center;font-size:.7rem;font-weight:700;flex:none;text-transform:uppercase}
.file-card small{color:var(--muted);display:block}
.typing{padding:2px 16px 4px;color:var(--muted);font-size:.85rem;font-style:italic}
.composer{display:flex;align-items:flex-end;gap:6px;padding:8px 8px calc(8px + env(safe-area-inset-bottom));background:var(--panel);border-top:1px solid var(--line)}
.composer textarea{flex:1;resize:none;border:0;outline:none;background:var(--bg);border-radius:22px;padding:.65rem 1rem;max-height:140px;font-size:16px;line-height:1.35}
.send-btn{width:42px;height:42px;border-radius:50%;border:0;background:var(--accent);color:#fff;display:grid;place-items:center;flex:none}
.recording{display:flex;align-items:center;gap:12px;padding:10px 14px calc(10px + env(safe-area-inset-bottom));background:var(--panel);border-top:1px solid var(--line)}
.recording #rec-time{flex:1;font-variant-numeric:tabular-nums}
.rec-dot{width:12px;height:12px;border-radius:50%;background:var(--danger);animation:pulse 1s infinite}
@keyframes pulse{50%{opacity:.3}}
.drop-hint{position:absolute;inset:12px;border:3px dashed var(--accent);border-radius:20px;display:grid;place-items:center;font-size:1.3rem;font-weight:700;color:var(--accent);background:color-mix(in srgb,var(--bg) 85%,transparent);pointer-events:none}

/* ---------- llamada ---------- */
.call{position:fixed;inset:0;z-index:50;background:#0d1714;color:#fff;display:flex;flex-direction:column;align-items:center;justify-content:space-between;padding:calc(40px + env(safe-area-inset-top)) 20px calc(36px + env(safe-area-inset-bottom))}
#remote-video{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;background:#0d1714}
#local-video{position:absolute;right:16px;top:calc(16px + env(safe-area-inset-top));width:110px;aspect-ratio:3/4;object-fit:cover;border-radius:14px;box-shadow:0 6px 20px rgba(0,0,0,.4);transform:scaleX(-1);z-index:2;background:#222}
.call.video-on.connected .call-info{opacity:0}
.call-info{position:relative;z-index:1;text-align:center;margin-top:8vh;transition:opacity .3s}
.call-info .avatar{margin:0 auto 16px}
.call-info h2{margin:0;font-size:1.8rem}
.call-info p{color:#b9c9c2;margin:.4rem 0 0}
.call-controls{position:relative;z-index:3;display:flex;gap:18px;justify-content:center}
.round{width:64px;height:64px;border-radius:50%;border:0;background:rgba(255,255,255,.16);color:#fff;display:grid;place-items:center;backdrop-filter:blur(10px)}
.round svg{width:28px;height:28px}
.round.off{background:#fff;color:#0d1714}
.round.danger{background:var(--danger)}
.round.accept{background:var(--ok)}
.round.accept svg{animation:wiggle 1s infinite}
@keyframes wiggle{25%{transform:rotate(-12deg)}75%{transform:rotate(12deg)}}

/* ---------- modal ---------- */
.modal-wrap{position:fixed;inset:0;z-index:40;background:rgba(10,20,16,.45);display:grid;place-items:center;padding:16px}
.modal{width:100%;max-width:440px;max-height:min(86dvh,700px);display:flex;flex-direction:column;background:var(--panel);border-radius:22px;box-shadow:var(--shadow);overflow:hidden}
.modal header{display:flex;align-items:center;padding:12px 12px 4px 20px}
.modal header h3{flex:1;margin:0}
#modal-body{padding:8px 20px 20px;overflow-y:auto}
.pick-list{list-style:none;margin:8px 0;padding:0}
.pick-list li{display:flex;align-items:center;gap:12px;padding:8px 6px;border-radius:12px;cursor:pointer}
.pick-list li:hover{background:var(--bg)}
.pick-list li .check{margin-left:auto;width:24px;height:24px;border-radius:50%;border:2px solid var(--line);display:grid;place-items:center}
.pick-list li.sel .check{background:var(--accent);border-color:var(--accent);color:#fff}
.pick-list li .check svg{width:14px;height:14px;stroke-width:3}
.row{display:flex;gap:10px;align-items:center;justify-content:space-between;padding:10px 0;border-bottom:1px solid var(--line)}
.row:last-child{border:0}
.section-title{font-size:.8rem;text-transform:uppercase;letter-spacing:.06em;color:var(--muted);margin:16px 0 4px}
.code{font-family:ui-monospace,Menlo,monospace;background:var(--bg);padding:.5rem .7rem;border-radius:10px;user-select:all;word-break:break-all}
.help{color:var(--muted);font-size:.88rem}

.viewer{position:fixed;inset:0;z-index:60;background:rgba(0,0,0,.92);display:grid;place-items:center;cursor:zoom-out}
.viewer img{max-width:96vw;max-height:94dvh;object-fit:contain}
.toast{position:fixed;left:50%;bottom:calc(90px + env(safe-area-inset-bottom));transform:translateX(-50%);z-index:70;background:var(--ink);color:var(--bg);padding:.65rem 1rem;border-radius:12px;box-shadow:var(--shadow);max-width:90vw;text-align:center}

/* ---------- móvil ---------- */
@media (max-width:760px){
  .app{grid-template-columns:1fr}
  .app .chat{display:none}
  .app.in-room .sidebar{display:none}
  .app.in-room .chat{display:flex}
  .chat-head .back{display:inline-grid}
  .msg{max-width:84%}
}
#file-input{display:none!important}
