*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

  :root {
    --bg:       #0d0d0d;
    --surface:  #141414;
    --surface2: #1a1a1a;
    --border:   #2a2a2a;
    --muted:    #555;
    --dim:      #888;
    --text:     #e0e0e0;
    --accent:   #4fc3f7;
    --online:   #4caf50;
    --mine:     #1e3a4a;
    --theirs:   #1e1e1e;
    --danger:   #e57373; 
  }

  body { font-family: 'Courier New', monospace; background: var(--bg); color: var(--text); height: 100vh; overflow: hidden; }

  /* ── LOGIN ── */
  #loginScreen { display: flex; align-items: center; justify-content: center; height: 100vh; }
  #loginBox {
    display: flex; flex-direction: column; gap: 12px;
    width: 320px; padding: 40px 32px;
    background: var(--surface); border: 1px solid var(--border);
  }
  #loginBox h2 { font-size: 22px; letter-spacing: 0.3em; color: var(--accent); }
  .subtitle { font-size: 11px; color: var(--muted); letter-spacing: 0.15em; margin-top: -6px; margin-bottom: 8px; }
  #loginBox input {
    background: var(--bg); border: 1px solid var(--border); color: var(--text);
    padding: 10px 12px; font-family: inherit; font-size: 13px; outline: none; transition: border-color 0.2s;
  }
  #loginBox input:focus { border-color: var(--accent); }
  #loginBox button {
    background: var(--accent); color: #000; border: none; padding: 10px;
    font-family: inherit; font-size: 13px; font-weight: bold; letter-spacing: 0.15em;
    cursor: pointer; transition: opacity 0.2s;
  }
  #loginBox button:hover    { opacity: 0.85; }
  #loginBox button:disabled { opacity: 0.4; cursor: default; }
  #loginStatus { font-size: 11px; color: var(--dim); min-height: 16px; letter-spacing: 0.05em; }
  #loginNotice { font-size: 11px; color: var(--muted); line-height: 1.6; padding: 10px; border: 1px solid var(--border); }
  #loginNotice strong { color: var(--dim); }

  /* ── APP ── */
  #appContainer { display: none; height: 100vh; }

  /* ── SIDEBAR ── */
  #sidebar {
    width: 260px; min-width: 260px; display: flex; flex-direction: column;
    background: var(--surface); border-right: 1px solid var(--border);
  }
  #sidebarHeader {
    display: flex; align-items: flex-start; justify-content: space-between;
    padding: 20px 16px 16px; border-bottom: 1px solid var(--border);
  }
  #sidebarTitle { font-size: 14px; letter-spacing: 0.3em; color: var(--accent); }
  #sidebarMeta  { font-size: 10px; color: var(--muted); letter-spacing: 0.05em; margin-top: 4px; }
  #exportBtn {
    background: none; border: 1px solid var(--border); color: var(--dim);
    width: 28px; height: 28px; font-size: 18px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: border-color 0.2s, color 0.2s; flex-shrink: 0;
  }
  #exportBtn:hover { border-color: var(--accent); color: var(--accent); }
  
  #meshLogToggle {
    background: none; border: 1px solid var(--border); color: var(--dim);
    width: 28px; height: 28px; font-size: 18px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: border-color 0.2s, color 0.2s; flex-shrink: 0;
  }
  #meshLogToggle:hover { border-color: var(--accent); color: var(--accent); }
  
  #addContactBtn {
    background: none; border: 1px solid var(--border); color: var(--dim);
    width: 28px; height: 28px; font-size: 18px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: border-color 0.2s, color 0.2s; flex-shrink: 0;
  }
  #addContactBtn:hover { border-color: var(--accent); color: var(--accent); }
  #contactList { list-style: none; flex: 1; overflow-y: auto; }
  #contactList::-webkit-scrollbar { width: 4px; }
  #contactList::-webkit-scrollbar-thumb { background: var(--border); }
  .contactItem {
    display: flex; align-items: center; gap: 10px; padding: 12px 16px; cursor: pointer;
    border-bottom: 1px solid var(--border); transition: background 0.15s;
  }
  .contactItem:hover  { background: var(--surface2); }
  .contactItem.active { background: var(--surface2); border-left: 2px solid var(--accent); }
  .contactAvatar {
    width: 34px; height: 34px; background: var(--surface2); border: 1px solid var(--border);
    display: flex; align-items: center; justify-content: center;
    font-size: 14px; color: var(--accent); flex-shrink: 0;
  }
  .contactInfo { flex: 1; min-width: 0; }
  .contactName { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .contactId   { font-size: 10px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .contactStatus {
    width: 7px; height: 7px; border-radius: 50%;
    background: #111; /* default off — JS controls online colour */
    flex-shrink: 0;
  }
  #syncStatus { font-size: 10px; color: var(--muted); padding: 6px 16px; border-top: 1px solid var(--border); letter-spacing: 0.05em; min-height: 26px; }
  #connectionStatus {
    display: flex; align-items: center; gap: 8px; padding: 12px 16px;
    border-top: 1px solid var(--border); font-size: 10px; color: var(--muted); letter-spacing: 0.05em;
  }
  #connDot { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); flex-shrink: 0; transition: background 0.3s; }
  #connDot.connected { background: var(--online); }

  /* ── CHAT ── */
  #chatContainer { flex: 1; display: flex; flex-direction: column; min-width: 0; }
  #chatHeader { padding: 16px 20px; border-bottom: 1px solid var(--border); min-height: 60px; display: flex; flex-direction: column; justify-content: center; }
  #emptyChat      { font-size: 12px; color: var(--muted); letter-spacing: 0.1em; }
  #chatHeaderName { font-size: 14px; letter-spacing: 0.1em; }
  #chatHeaderId   { font-size: 10px; color: var(--muted); margin-top: 3px; }
  #chatMessages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 8px; }
  #chatMessages::-webkit-scrollbar { width: 4px; }
  #chatMessages::-webkit-scrollbar-thumb { background: var(--border); }
  .message { position: relative; max-width: 65%; padding: 9px 14px 22px 14px; font-size: 13px; line-height: 1.5; word-break: break-word; }
.message.no-reaction-pad { padding-bottom: 9px; }
  .message.mine    { background: var(--mine);   align-self: flex-end;   color: var(--accent); }
  .message.theirs  { background: var(--theirs); align-self: flex-start; border: 1px solid var(--border); }
  .message.invalid { border-color: var(--danger) !important; opacity: 0.6; }
  .msgMeta { font-size: 10px; color: var(--muted); margin-top: 3px; }
  #chatInputContainer { display: flex; border-top: 1px solid var(--border); }
  #chatInput {
    flex: 1; background: var(--surface); border: none; color: var(--text);
    padding: 14px 16px; font-family: inherit; font-size: 13px; outline: none;
  }
  #sendButton {
    background: none; border: none; border-left: 1px solid var(--border); color: var(--accent);
    padding: 0 20px; font-family: inherit; font-size: 12px; letter-spacing: 0.15em; cursor: pointer; transition: background 0.2s;
  }
  #sendButton:hover { background: var(--surface2); }
  .chat-action-btn {
    background: none; border: none; border-left: 1px solid var(--border); color: var(--muted);
    padding: 0 14px; font-family: inherit; font-size: 11px; letter-spacing: 0.1em; cursor: pointer; transition: color 0.2s;
  }
  .chat-action-btn:hover { color: var(--accent); }

  /* ── MODALS ── */
  .overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.75); align-items: center; justify-content: center; z-index: 100; }
  .overlay.open { display: flex; }
  .modal-box {
    background: var(--surface); border: 1px solid var(--border);
    padding: 28px; width: 420px; display: flex; flex-direction: column; gap: 12px;
    max-height: 90vh; overflow-y: auto;
  }
  .modal-box h3 { font-size: 14px; letter-spacing: 0.3em; color: var(--accent); margin-bottom: 4px; }
  .modal-box input {
    background: var(--bg); border: 1px solid var(--border); color: var(--text);
    padding: 10px 12px; font-family: inherit; font-size: 13px; outline: none; transition: border-color 0.2s;
  }
  .modal-box input:focus { border-color: var(--accent); }
  .hint { font-size: 11px; color: var(--muted); line-height: 1.6; }
  .modal-btns { display: flex; gap: 8px; margin-top: 4px; }
  .modal-btns button {
    flex: 1; padding: 10px; font-family: inherit; font-size: 12px;
    letter-spacing: 0.15em; cursor: pointer; border: 1px solid var(--border); transition: all 0.2s;
  }
  .btn-cancel  { background: none; color: var(--dim); }
  .btn-confirm { background: var(--accent); color: #000; border-color: var(--accent); font-weight: bold; }
  .btn-alt     { background: none; color: var(--accent); }
  .btn-cancel:hover  { color: var(--text); border-color: var(--dim); }
  .btn-confirm:hover { opacity: 0.85; }
  .btn-alt:hover     { background: var(--surface2); }

  /* ── QR TABS ── */
  #myKeyBox {
    background: var(--bg); border: 1px solid var(--border); padding: 10px 12px;
    font-size: 11px; display: flex; flex-direction: column; gap: 4px;
    color: var(--muted); cursor: pointer; transition: border-color 0.2s;
  }
  #myKeyBox:hover { border-color: var(--accent); }
  #myKeyDisplay { color: var(--text); word-break: break-all; font-size: 10px; letter-spacing: 0.03em; line-height: 1.5; }

  .qr-tabs { display: flex; border: 1px solid var(--border); margin-bottom: 4px; }
  .qr-tab {
    flex: 1; padding: 8px 4px; background: none; border: none; border-right: 1px solid var(--border);
    color: var(--muted); font-family: inherit; font-size: 11px; letter-spacing: 0.1em; cursor: pointer; transition: all 0.2s;
  }
  .qr-tab:last-child { border-right: none; }
  .qr-tab.active { background: var(--surface2); color: var(--accent); }
  .qr-tab:hover:not(.active) { color: var(--text); }

  .qr-panel { display: none; flex-direction: column; align-items: center; gap: 10px; }
  .qr-panel.active { display: flex; }

  #myQrCode { background: white; padding: 12px; margin: 0 auto; }

  #qrReader { width: 100%; border: 1px solid var(--border); }
  #qrReader video { border-radius: 0 !important; }
  #qrReader img   { display: none !important; }

  #scanResult { font-size: 11px; color: var(--online); min-height: 16px; letter-spacing: 0.05em; text-align: center; }

  .scan-btn {
    width: 100%; padding: 9px; background: none; border: 1px solid var(--border);
    color: var(--accent); font-family: inherit; font-size: 11px; letter-spacing: 0.15em; cursor: pointer; transition: all 0.2s;
  }
  .scan-btn:hover   { background: var(--surface2); border-color: var(--accent); }
  .scan-btn.active  { color: var(--danger); border-color: var(--danger); }

  #exportStatus { font-size: 11px; color: var(--dim); min-height: 16px; }

  /* ── CONTACT ACTION MENU ── */
  #chatHeaderRow {
    display: flex; align-items: center; justify-content: space-between;
    gap: 10px;
  }
  #chatHeaderInfo { flex: 1; min-width: 0; }
  #contactMenuBtn {
    background: none; border: 1px solid var(--border); color: var(--dim);
    width: 28px; height: 28px; font-size: 16px; cursor: pointer; flex-shrink: 0;
    display: none; align-items: center; justify-content: center;
    transition: border-color 0.2s, color 0.2s;
  }
  #contactMenuBtn:hover { border-color: var(--accent); color: var(--accent); }
  #contactMenuBtn.visible { display: flex; }
  .contact-dropdown {
    position: absolute; top: 52px; right: 16px;
    background: var(--surface); border: 1px solid var(--border);
    display: none; flex-direction: column; z-index: 50; min-width: 140px;
  }
  .contact-dropdown.open { display: flex; }
  .contact-dropdown button {
    background: none; border: none; border-bottom: 1px solid var(--border);
    color: var(--text); padding: 10px 14px; font-family: inherit; font-size: 11px;
    letter-spacing: 0.12em; text-align: left; cursor: pointer; transition: background 0.15s;
  }
  .contact-dropdown button:last-child { border-bottom: none; }
  .contact-dropdown button:hover { background: var(--surface2); color: var(--accent); }
  .contact-dropdown button.danger:hover { color: var(--danger); }

  /* ── BLOCKED CONTACTS ── */
  .contactItem.blocked { opacity: 0.38; }
  .contactItem.blocked .contactName::after {
    content: ' · blocked';
    font-size: 9px; color: var(--danger); letter-spacing: 0.08em;
  }

  /* ── MOBILE ── */
  @media (max-width: 600px) {
    #appContainer { height: 100dvh; }
    #sidebar { width: 100%; min-width: unset; }
    #chatContainer { display: none; height: 100dvh; overflow: hidden; }
    #appContainer.chatOpen #sidebar       { display: none; }
    #appContainer.chatOpen #chatContainer { display: flex; width: 100%; }
    #chatMessages { flex: 1; min-height: 0; }
    #chatInputContainer { flex-shrink: 0; }
    #loginBox  { width: 90vw; padding: 32px 20px; }
    .modal-box { width: 92vw; padding: 20px 14px; }
    #backBtn   { display: block !important; }
  }

  .unreadBadge {
    background: var(--accent);
    color: black;
    font-size: 10px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 6px;
  }

  #chatMessages a {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: all 0.15s ease;
    word-break: break-word;
  }

  #chatMessages a:hover {
    border-bottom: 1px solid var(--accent);
    filter: brightness(1.2);
  }

  .contactPreview {
    font-size: 10px;
    color: var(--muted);
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
/* ── in-page log console ── */
#meshLog {
  display: none;
  position: fixed;
  bottom: 40px; left: 0;
  width: 440px; max-width: 100vw;
  max-height: 400px;
  background: #000;
  border: 1px solid #333;
  border-left: none;
  border-bottom: none;
  font-family: monospace;
  font-size: 11px;
  z-index: 9999;
  flex-direction: column;
  box-shadow: 2px -2px 12px rgba(0,0,0,.8);
}
#meshLog.open { display: flex; }
#meshLogHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 8px;
  background: #111;
  color: #555;
  letter-spacing: .12em;
  font-size: 10px;
  border-bottom: 1px solid #222;
  user-select: none;
}
#meshLogHeader span { color: #444; }
#meshLogHeader button {
  background: none; border: none; cursor: pointer;
  color: #444; font-family: monospace; font-size: 10px;
  padding: 0 4px; letter-spacing: .08em;
}
#meshLogHeader button:hover { color: #aaa; }
#meshLogBody {
  overflow-y: auto;
  flex: 1;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.mlog-line       { color: #888; white-space: pre; line-height: 1.5; }
.mlog-line.info  { color: #ccc; }
.mlog-line.warn  { color: #f80; }
.mlog-line.err   { color: #f44; }
#meshLogToggle {

  background: #111;
  border: none; border-right: 1px solid #333; border-top: 1px solid #333;
  color: #444;
  font-family: monospace; font-size: 10px;
  letter-spacing: .12em;
  padding: 3px 10px;
  cursor: pointer;
  z-index: 9999;
}
#meshLogToggle:hover { color: #aaa; background: #1a1a1a; }

/* ── show blocked toggle ── */
#showBlockedToggle {
  padding: 4px 8px;
}
#showBlockedBtn {
  width: 100%;
  background: none;
  border: 1px solid #2a2a2a;
  color: #555;
  font-family: monospace;
  font-size: 10px;
  letter-spacing: .1em;
  padding: 4px 8px;
  cursor: pointer;
  text-align: left;
}
#showBlockedBtn:hover { color: #aaa; border-color: #444; }

#mediaButtons {
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  flex-shrink: 0;
}
#audioBtn,
#imageBtn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 14px;
  padding: 0 10px;
  cursor: pointer;
  flex: 1;
  transition: color 0.1s, background 0.1s;
  line-height: 1;
}
#audioBtn {
  border-bottom: 1px solid var(--border);
}
#audioBtn:hover,
#imageBtn:hover {
  color: var(--accent);
  background: var(--surface2);
}
#audioBtn.recording {
  color: #f44;
  border-color: #f44;
  animation: pulse 1s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.4; }
}
.message.mine img,
.message.theirs img {
  max-width: 200px;
  display: block;
  border-radius: 2px;
}
.reaction-trigger {
  display: block;
  text-align: right;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
  margin-top: 4px;
  opacity: 0.5;
  transition: opacity 0.15s;
  user-select: none;
}
.reaction-trigger:hover {
  opacity: 1;
}
.reaction-picker {
  display: flex;
  gap: 4px;
  margin-top: 4px;
  justify-content: flex-end;
}
.reaction-picker button {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 14px;
  padding: 2px 7px;
  cursor: pointer;
  font-family: inherit;
  transition: border-color 0.15s;
}
.reaction-picker button:hover {
  border-color: var(--accent);
}
.reaction-row {
  font-size: 14px;
  margin-top: 2px;
  opacity: 0.85;
}

/* ── reactions ── */
.reaction-row {
  /* positioned absolutely inside .message — see JS */
}
.reaction-emoji {
  font-size: 13px;
  background: none;
  border: none;
  cursor: default;
  padding: 0 2px;
  line-height: 1;
  opacity: 0.75;
}
.mine-emoji {
  cursor: pointer;
  opacity: 0.9;
  transition: opacity 0.15s, transform 0.1s;
}
.mine-emoji:hover {
  opacity: 1;
  transform: scale(1.2);
}
.reaction-trigger-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
  padding: 0 2px;
  opacity: 0.35;
  transition: opacity 0.15s, color 0.15s;
  line-height: 1;
  font-family: inherit;
}
.reaction-trigger-btn:hover {
  opacity: 1;
  color: var(--accent);
}
.reaction-picker-popup {
  display: flex;
  gap: 2px;
  margin-top: 2px;
}
.reaction-picker-popup button {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 14px;
  padding: 2px 7px;
  cursor: pointer;
  font-family: inherit;
  transition: border-color 0.15s, transform 0.1s;
  line-height: 1.4;
}
.reaction-picker-popup button:hover {
  border-color: var(--accent);
  transform: scale(1.15);
}