*{box-sizing:border-box;margin:0;padding:0;-webkit-tap-highlight-color:transparent}

/* ── THEMES ── */
:root{
  --bg:#0f0f0f;--surface:#181818;--surface2:#222;
  --border:#2c2c2c;--accent:#4f8cff;--accent-dim:#1a2e55;
  --text:#f0f0f0;--subtext:#7a7a7a;--subtext2:#444;
  --bubble-me:#1a3460;--bubble-other:#1e1e1e;
  --reply-bg:#ffffff0f;--danger:#ff4f4f;
  --radius:18px;--safe-bottom:env(safe-area-inset-bottom,0px);
  --safe-top:env(safe-area-inset-top,0px);
}
[data-theme="light"]{
  --bg:#f0f2f5;--surface:#fff;--surface2:#f5f5f5;
  --border:#e0e0e0;--accent:#2563eb;--accent-dim:#dbeafe;
  --text:#111;--subtext:#888;--subtext2:#bbb;
  --bubble-me:#2563eb;--bubble-other:#fff;
  --reply-bg:#0000000a;--danger:#dc2626;
}
[data-theme="light"] .msg-row.me .bubble{color:#fff}
[data-theme="light"] .bubble{border-color:#e5e7eb}

html,body{height:100%;background:var(--bg);color:var(--text);
  font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;font-size:15px;overflow:hidden}

/* ── SETUP ── */
#setup-modal{position:fixed;inset:0;background:rgba(0,0,0,.9);display:flex;
  align-items:center;justify-content:center;z-index:200;padding:24px}
.modal-box{background:var(--surface);border:1px solid var(--border);border-radius:22px;
  padding:30px 24px;width:100%;max-width:360px;text-align:center}
.modal-box h2{font-size:22px;margin-bottom:8px}
.modal-box p{color:var(--subtext);font-size:14px;margin-bottom:20px;line-height:1.5}
.device-options{display:flex;gap:10px;margin-bottom:20px}
.device-btn{flex:1;padding:16px 10px;background:var(--bg);border:2px solid var(--border);
  border-radius:16px;color:var(--text);font-size:14px;cursor:pointer;transition:.2s}
.device-btn:hover,.device-btn.selected{border-color:var(--accent);background:var(--accent-dim)}
.device-btn .icon{font-size:30px;margin-bottom:6px}
.pin-section{margin-bottom:16px}
.pin-section label{font-size:13px;color:var(--subtext);display:block;margin-bottom:6px}
.pin-input{width:100%;background:var(--bg);border:1px solid var(--border);border-radius:10px;
  padding:10px 14px;color:var(--text);font-size:16px;text-align:center;
  letter-spacing:6px;outline:none;font-family:monospace}
.pin-input:focus{border-color:var(--accent)}
.setup-btn{width:100%;padding:14px;background:var(--accent);border:none;border-radius:14px;
  color:#fff;font-size:16px;font-weight:600;cursor:pointer;transition:.2s}
.setup-btn:hover{opacity:.9}
.setup-btn:disabled{opacity:.4;cursor:default}

/* ── PIN LOCK ── */
#pin-lock{position:fixed;inset:0;background:var(--bg);display:none;flex-direction:column;
  align-items:center;justify-content:center;z-index:190;gap:16px;padding:24px}
#pin-lock h2{font-size:22px}
#pin-lock p{color:var(--subtext);font-size:14px}
.pin-dots{display:flex;gap:12px;margin:8px 0}
.pin-dot{width:14px;height:14px;border-radius:50%;border:2px solid var(--border);
  transition:.2s;background:transparent}
.pin-dot.filled{background:var(--accent);border-color:var(--accent)}
.numpad{display:grid;grid-template-columns:repeat(3,1fr);gap:10px;max-width:280px;width:100%}
.num-btn{padding:18px;background:var(--surface);border:1px solid var(--border);
  border-radius:14px;color:var(--text);font-size:20px;cursor:pointer;transition:.15s;
  font-weight:500}
.num-btn:active{transform:scale(.93);background:var(--accent-dim)}
.pin-error{color:var(--danger);font-size:13px;min-height:18px}

/* ── LAYOUT ── */
#app{display:flex;flex-direction:column;height:100dvh}
.app-shell{display:grid;grid-template-columns:330px 1fr;height:100dvh;background:var(--bg)}
.screen{min-height:100dvh}
.auth-screen{display:flex;align-items:center;justify-content:center;background:var(--bg);padding:24px}
.auth-input{letter-spacing:0;text-align:left;margin-bottom:10px;font-family:inherit}
.setup-btn.secondary{background:var(--surface2);border:1px solid var(--border);color:var(--text);margin-top:8px}
.setup-btn.small{padding:9px 12px;border-radius:10px;font-size:14px;margin:0}
.setup-btn.left{text-align:left}
.form-error{min-height:18px;margin-top:10px;color:var(--danger);font-size:13px}
.chat-list{border-right:1px solid var(--border);background:var(--surface);overflow-y:auto}
.list-header{position:static;justify-content:space-between;min-height:58px}
.list-header h1{font-size:22px;margin-bottom:0;line-height:1.15}
.header-actions{display:flex;align-items:center;gap:6px}
.search-input{width:100%;background:var(--bg);border:1px solid var(--border);border-radius:10px;
  color:var(--text);padding:10px 12px;font:inherit;outline:none}
.search-input:focus{border-color:var(--accent)}
.search-results{padding-top:8px}
.panel{display:grid;gap:8px}
.person-row,.chat-item,.invite-row{display:flex;align-items:center;gap:10px;padding:10px;border-radius:8px}
.person-row:hover,.chat-item:hover,.chat-item.active{background:var(--surface2)}
.invite-copy{width:100%;border:0;background:transparent;color:inherit;font:inherit;text-align:left;cursor:pointer}
.invite-copy:hover{background:var(--surface2)}
.person-main,.chat-main{min-width:0;flex:1}
.person-name,.chat-name{font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.person-meta,.chat-last{color:var(--subtext);font-size:12px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.avatar{width:34px;height:34px;border-radius:50%;border:1px solid var(--border);background:var(--surface2);
  color:var(--text);display:flex;align-items:center;justify-content:center;flex-shrink:0;
  font-size:13px;font-weight:700;overflow:hidden;cursor:pointer}
.avatar img{width:100%;height:100%;object-fit:cover;display:block}
.avatar.large{width:54px;height:54px;font-size:18px;background:var(--bg)}
.avatar.xlarge{width:86px;height:86px;font-size:28px;margin:4px auto 12px}
.header-avatar{width:38px;height:38px;font-size:14px}
.profile-edit-row{display:flex;align-items:center;gap:10px}
.profile-card{text-align:center;display:grid;gap:6px}
.group-card{display:grid;gap:8px}
.group-members{display:grid;gap:4px}
.profile-actions{display:grid;grid-template-columns:1fr 44px;gap:8px;margin-top:12px}
.online-dot{width:8px;height:8px;border-radius:50%;background:var(--subtext2);flex-shrink:0}
.online-dot.on{background:#3ecf8e;box-shadow:0 0 6px #3ecf8e88}
.mini-btn{border:1px solid var(--border);background:var(--bg);color:var(--text);border-radius:8px;padding:7px 9px;cursor:pointer}
.mini-btn.danger{color:var(--danger)}
.person-actions{display:flex;align-items:center;gap:6px;flex-shrink:0}
.pick-btn{min-width:34px;font-weight:700}
.pick-btn.active{background:var(--accent);border-color:var(--accent);color:#fff}
.selected-users{display:flex;flex-wrap:wrap;gap:6px;min-height:34px;align-items:center}
.selected-chip{border:1px solid var(--border);background:var(--surface2);color:var(--text);
  border-radius:999px;padding:7px 10px;font:inherit;font-size:13px;cursor:pointer;
  max-width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.selected-chip span{color:var(--subtext);padding-left:4px}
.tool-hint{color:var(--subtext);font-size:12px;line-height:1.35}
.chat-view{display:flex;flex-direction:column;min-width:0;height:100dvh}
.mobile-only{display:none}
.tools-overlay{position:fixed;inset:0;background:rgba(0,0,0,.35);z-index:220;
  display:none;align-items:stretch;justify-content:flex-start}
.tools-overlay.open{display:flex}
.tools-drawer{width:min(86vw,360px);height:100%;background:var(--surface);
  border-right:1px solid var(--border);padding:calc(12px + max(var(--safe-top), 0px)) 12px calc(12px + var(--safe-bottom));
  box-shadow:14px 0 34px rgba(0,0,0,.35);overflow:auto;touch-action:pan-y}
.tools-head{display:flex;align-items:center;gap:10px;margin-bottom:14px}
.tools-head h3{font-size:17px;flex:1}
.tool-section{display:grid;gap:9px;padding:12px 0;border-top:1px solid var(--border)}
.tool-section:first-of-type{border-top:none}
.tool-row{display:grid;grid-template-columns:1fr auto;gap:8px}
.section-title{font-size:12px;color:var(--subtext);text-transform:uppercase;letter-spacing:.04em}
.tool-footer{margin-top:auto;padding-top:14px;border-top:1px solid var(--border)}
.logout-btn{width:100%;padding:11px 12px;background:transparent;border:1px solid var(--border);
  color:var(--subtext);border-radius:10px;font:inherit;cursor:pointer;text-align:left}
.logout-btn:hover{color:var(--text);background:var(--surface2)}
.toggle-row{display:grid;grid-template-columns:1fr 46px;align-items:center;gap:12px;
  padding:10px 11px;border:1px solid var(--border);border-radius:10px;background:var(--surface2);
  color:var(--text);font:inherit;text-align:left;cursor:pointer}
.toggle-row strong{display:block;font-size:14px;font-weight:600}
.toggle-row small{display:block;margin-top:2px;color:var(--subtext);font-size:12px}
.toggle-row i{width:46px;height:26px;border-radius:999px;background:var(--bg);border:1px solid var(--border);
  position:relative;transition:.18s}
.toggle-row i::after{content:"";position:absolute;top:3px;left:3px;width:18px;height:18px;border-radius:50%;
  background:var(--subtext);transition:.18s}
.toggle-row.active i{background:var(--accent);border-color:var(--accent)}
.toggle-row.active i::after{left:23px;background:#fff}
.toggle-row.muted{opacity:.65}
.admin-storage-action{position:relative;z-index:5;touch-action:manipulation}
.admin-overlay{position:fixed;inset:0;background:rgba(0,0,0,.55);z-index:250;
  display:none;align-items:flex-end;justify-content:center}
.admin-overlay.open{display:flex}
.profile-overlay{position:fixed;inset:0;background:rgba(0,0,0,.55);z-index:245;
  display:none;align-items:flex-end;justify-content:center}
.profile-overlay.open{display:flex}
.admin-sheet,.profile-sheet{width:100%;max-width:520px;max-height:72vh;overflow:auto;background:var(--surface);
  border:1px solid var(--border);border-radius:18px 18px 0 0;
  padding:14px 14px calc(14px + var(--safe-bottom));box-shadow:0 -12px 32px rgba(0,0,0,.45);
  touch-action:pan-y;overscroll-behavior:contain}
.admin-head{display:flex;align-items:center;gap:10px;margin-bottom:12px}
.admin-head h3{font-size:16px;flex:1}
.admin-head .bar-close{width:38px;height:38px;border-radius:50%;background:var(--surface2)}
.action-list{display:grid;gap:8px}
.action-list button{width:100%;padding:12px;background:var(--surface2);border:1px solid var(--border);
  color:var(--text);border-radius:10px;font:inherit;cursor:pointer;text-align:left}
.action-list button.danger{color:var(--danger)}

/* ── HEADER ── */
header{background:var(--surface);border-bottom:1px solid var(--border);
  padding:12px 14px;display:flex;align-items:center;gap:10px;
  position:sticky;top:0;z-index:10;flex-shrink:0}
.status-dot{width:9px;height:9px;border-radius:50%;background:var(--subtext2);
  transition:background .3s;flex-shrink:0}
.status-dot.online{background:#3ecf8e;box-shadow:0 0 6px #3ecf8e88}
header h1{font-size:17px;font-weight:600;flex:1}
.hdr-btn{background:none;border:none;color:var(--subtext);font-size:18px;
  cursor:pointer;padding:4px 6px;border-radius:8px;transition:.15s}
.hdr-btn:hover{color:var(--text);background:var(--surface2)}
.device-tag{font-size:12px;color:var(--subtext);background:var(--surface2);
  padding:3px 10px;border-radius:20px}
.lock-icon{font-size:13px;color:#3ecf8e}

/* ── MESSAGES ── */
#messages{flex:1;overflow-y:auto;padding:12px 10px 6px;
  display:flex;flex-direction:column;gap:3px;scroll-behavior:smooth}
#messages::-webkit-scrollbar{width:3px}
#messages::-webkit-scrollbar-thumb{background:var(--border);border-radius:3px}

.msg-row{display:flex;flex-direction:column;position:relative;transition:transform .15s}
.msg-row.me{align-items:flex-end}
.msg-row.other{align-items:flex-start}

.bubble{max-width:80%;padding:9px 13px;border-radius:var(--radius);
  line-height:1.5;word-break:break-word;position:relative}
.msg-row.me .bubble{background:var(--bubble-me);border-bottom-right-radius:5px;color:#cfe0ff}
.msg-row.other .bubble{background:var(--bubble-other);border:1px solid var(--border);
  border-bottom-left-radius:5px}
.bubble.deleted{opacity:.45;font-style:italic}

/* Forward label */
.fwd-label{font-size:11px;color:var(--accent);margin-bottom:4px;font-weight:500}

/* Reply quote */
.reply-quote{background:var(--reply-bg);border-left:3px solid var(--accent);
  border-radius:6px;padding:5px 9px;margin-bottom:7px;cursor:pointer}
.reply-quote .rq-name{font-weight:600;color:var(--accent);margin-bottom:1px;font-size:12px}
.reply-quote .rq-text{color:var(--subtext);white-space:nowrap;overflow:hidden;
  text-overflow:ellipsis;max-width:220px;font-size:13px}

/* Image */
.bubble img{max-width:100%;border-radius:10px;display:block;cursor:zoom-in;max-height:300px;object-fit:cover}

/* Voice */
.voice-player{display:flex;align-items:center;gap:8px;min-width:190px}
.play-btn{width:34px;height:34px;border-radius:50%;background:var(--accent);
  border:none;cursor:pointer;display:flex;align-items:center;justify-content:center;
  flex-shrink:0;color:#fff;transition:transform .15s}
.play-btn:active{transform:scale(.9)}
.voice-duration{font-size:13px;color:var(--subtext);min-width:36px}
audio{display:none}

/* File */
.file-card{display:flex;align-items:center;gap:10px;text-decoration:none;
  color:inherit;padding:2px 0}
.file-icon{font-size:28px;flex-shrink:0}
.file-info .fname{font-size:14px;font-weight:500;white-space:nowrap;
  overflow:hidden;text-overflow:ellipsis;max-width:180px}
.file-info .fsize{font-size:12px;color:var(--subtext);margin-top:2px}

/* Meta + read receipts */
.meta{font-size:11px;color:var(--subtext);margin-top:2px;padding:0 3px;
  display:flex;align-items:center;gap:4px}
.msg-row.me .meta{justify-content:flex-end}
.edited-tag{font-size:10px;color:var(--subtext2)}
.ticks{font-size:13px;line-height:1;color:var(--subtext)}
.ticks.read{color:var(--accent)}

/* ── CONTEXT MENU ── */
#ctx-menu{position:fixed;background:var(--surface);border:1px solid var(--border);
  border-radius:14px;padding:6px;z-index:100;box-shadow:0 8px 28px rgba(0,0,0,.5);
  min-width:170px;display:none}
#ctx-menu button{display:flex;align-items:center;gap:8px;width:100%;padding:9px 12px;
  background:none;border:none;color:var(--text);font-size:14px;cursor:pointer;
  border-radius:8px;text-align:left;transition:.15s}
#ctx-menu button:hover{background:var(--surface2)}
#ctx-menu button.danger{color:var(--danger)}
#ctx-menu .sep{height:1px;background:var(--border);margin:4px 0}

/* ── TYPING ── */
#typing{font-size:12px;color:var(--subtext);padding:2px 14px;min-height:18px;
  font-style:italic;flex-shrink:0}

/* ── REPLY BAR ── */
#reply-bar{background:var(--surface);border-top:1px solid var(--border);
  padding:8px 14px;display:none;align-items:center;gap:8px;flex-shrink:0}
#reply-bar.active{display:flex}
.reply-preview .rp-name{font-size:12px;color:var(--accent);font-weight:600}
.reply-preview .rp-text{font-size:13px;color:var(--subtext);white-space:nowrap;
  overflow:hidden;text-overflow:ellipsis;max-width:220px}
.bar-close{background:none;border:none;color:var(--subtext);font-size:20px;
  cursor:pointer;padding:0 4px;line-height:1;flex-shrink:0}

/* ── EDIT BAR ── */
#edit-bar{background:var(--surface);border-top:1px solid var(--border);
  padding:8px 14px;display:none;align-items:center;gap:8px;flex-shrink:0}
#edit-bar.active{display:flex}
.edit-label{font-size:13px;color:var(--accent);flex:1}

/* ── INPUT BAR ── */
#inputbar{background:var(--surface);border-top:1px solid var(--border);
  padding:10px 10px calc(10px + var(--safe-bottom));
  display:flex;gap:6px;align-items:flex-end;flex-shrink:0}
#msg-input{flex:1;background:var(--bg);border:1px solid var(--border);
  border-radius:22px;padding:10px 14px;color:var(--text);font-size:15px;
  outline:none;resize:none;max-height:120px;line-height:1.4;
  transition:border-color .2s;font-family:inherit}
#msg-input:focus{border-color:var(--accent)}
#msg-input::placeholder{color:var(--subtext)}
.icon-btn{width:42px;height:42px;background:none;border:1px solid var(--border);
  border-radius:50%;cursor:pointer;display:flex;align-items:center;justify-content:center;
  color:var(--subtext);flex-shrink:0;transition:.2s;font-size:18px}
.icon-btn:hover{border-color:var(--accent);color:var(--accent)}
.icon-btn:active{transform:scale(.9)}
#record-btn.recording{background:var(--danger)!important;border-color:var(--danger)!important;
  color:#fff!important;animation:pulse 1s infinite}
#send-btn{width:42px;height:42px;background:var(--accent);border:none;border-radius:50%;
  cursor:pointer;display:flex;align-items:center;justify-content:center;
  flex-shrink:0;transition:.15s}
#send-btn:active{transform:scale(.9)}
#send-btn:disabled{background:var(--border);cursor:default;opacity:.5}

/* ── SETTINGS PANEL ── */
#settings-panel{position:fixed;inset:0;background:rgba(0,0,0,.6);z-index:150;
  display:none;align-items:flex-end;justify-content:center}
#settings-panel.open{display:flex}
.settings-sheet{background:var(--surface);border-radius:22px 22px 0 0;
  padding:20px 20px calc(20px + var(--safe-bottom));width:100%;max-width:480px}
.settings-sheet h3{font-size:17px;font-weight:600;margin-bottom:16px;text-align:center}
.settings-row{display:flex;align-items:center;justify-content:space-between;
  padding:12px 0;border-bottom:1px solid var(--border)}
.settings-row:last-child{border-bottom:none}
.settings-row label{font-size:15px;color:var(--text)}
.settings-row span{font-size:13px;color:var(--subtext);margin-top:2px}
/* Toggle */
.toggle{position:relative;width:48px;height:26px;flex-shrink:0}
.toggle input{opacity:0;width:0;height:0}
.toggle-slider{position:absolute;inset:0;background:var(--border);border-radius:13px;
  cursor:pointer;transition:.3s}
.toggle-slider:before{content:'';position:absolute;width:20px;height:20px;left:3px;bottom:3px;
  background:#fff;border-radius:50%;transition:.3s}
.toggle input:checked+.toggle-slider{background:var(--accent)}
.toggle input:checked+.toggle-slider:before{transform:translateX(22px)}

/* ── LIGHTBOX ── */
#lightbox{position:fixed;inset:0;background:rgba(0,0,0,.93);z-index:300;
  display:none;align-items:center;justify-content:center;cursor:zoom-out}
#lightbox img{max-width:95vw;max-height:95vh;border-radius:8px;object-fit:contain}

/* ── CONFIRM ── */
.confirm-overlay{position:fixed;inset:0;background:rgba(0,0,0,.58);z-index:360;
  display:none;align-items:center;justify-content:center;padding:22px}
.confirm-overlay.open{display:flex}
.confirm-sheet{width:100%;max-width:360px;background:var(--surface);border:1px solid var(--border);
  border-radius:14px;padding:18px;box-shadow:0 18px 44px rgba(0,0,0,.45)}
.confirm-sheet h3{font-size:17px;margin-bottom:8px}
.confirm-sheet p{color:var(--subtext);font-size:14px;line-height:1.45;white-space:pre-line}
.confirm-actions{display:grid;grid-template-columns:1fr 1fr;gap:8px;margin-top:18px}
.confirm-actions .mini-btn{min-height:42px;background:var(--surface2)}

/* ── TOAST ── */
#upload-toast{position:fixed;bottom:110px;left:50%;transform:translateX(-50%);
  background:var(--surface);border:1px solid var(--border);border-radius:12px;
  padding:10px 16px;font-size:13px;z-index:150;display:none;align-items:center;gap:8px;
  white-space:nowrap}
#upload-toast.show{display:flex}

/* ── EMPTY ── */
#empty-state{flex:1;display:flex;flex-direction:column;align-items:center;
  justify-content:center;color:var(--subtext);gap:8px;pointer-events:none}
#empty-state svg{opacity:.15}

@keyframes fadeUp{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:none}}
@keyframes pulse{0%,100%{box-shadow:0 0 0 0 rgba(255,79,79,.4)}50%{box-shadow:0 0 0 8px rgba(255,79,79,0)}}

@media (max-width:760px){
  .app-shell{display:block;width:100vw;overflow:hidden}
  .chat-list,.chat-view{height:100dvh;width:100vw}
  .chat-list{border-right:none}
  .list-header,.chat-view>header{padding-top:calc(12px + max(var(--safe-top), 28px))}
  .chat-view{display:none}
  .app-shell.chat-open .chat-list{display:none}
  .app-shell.chat-open .chat-view{display:flex}
  .mobile-only{display:inline-flex}
  .search-input{min-width:0}
  .setup-btn.small{min-height:42px}
}
