:root {
  --ink: #1b1f23;
  --paper: #f6f4ef;
  --panel: #ffffff;
  --line: #e4e0d6;
  --accent: #2f5d50;
  --accent-soft: #dfe9e4;
  --bubble-me: #2f5d50;
  --bubble-them: #ffffff;
  --muted: #7a7568;
  --danger: #b3492f;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--paper); color: var(--ink); }

.hidden { display: none !important; }

/* ---------- Auth ---------- */
.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.auth-card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 40px; width: 360px; box-shadow: 0 20px 40px rgba(27,31,35,0.06); }
.brand { display: flex; align-items: center; gap: 8px; font-size: 22px; font-weight: 700; }
.brand-mark { color: var(--accent); font-size: 22px; }
.brand-tag { color: var(--muted); font-size: 13px; margin: 8px 0 24px; }
.tabs { display: flex; gap: 4px; background: var(--accent-soft); border-radius: 10px; padding: 4px; margin-bottom: 20px; }
.tab { flex: 1; border: none; background: transparent; padding: 8px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--muted); }
.tab.active { background: var(--panel); color: var(--ink); }
.auth-form label { display: block; font-size: 12px; color: var(--muted); margin: 12px 0 4px; }
.auth-form input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; background: var(--paper); }
.primary-btn { margin-top: 20px; width: 100%; background: var(--accent); color: white; border: none; padding: 11px; border-radius: 8px; font-weight: 600; cursor: pointer; }
.primary-btn:hover { opacity: 0.92; }
.ghost-btn { background: transparent; border: 1px solid var(--line); padding: 10px 16px; border-radius: 8px; cursor: pointer; }
.auth-error { color: var(--danger); font-size: 13px; margin-top: 14px; }

/* ---------- App shell ---------- */
.app-shell { display: flex; height: 100vh; }
.sidebar { width: 320px; border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; }
.sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--line); }
.me { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.avatar { display: inline-flex; align-items: center; justify-content: center; width: 34px; height: 34px; border-radius: 50%; background: var(--accent-soft); font-size: 16px; }
.sidebar-actions button, .sidebar-actions .icon-link { border: none; background: var(--accent-soft); width: 32px; height: 32px; border-radius: 8px; cursor: pointer; margin-left: 6px; font-size: 14px; display: inline-flex; align-items: center; justify-content: center; text-decoration: none; color: var(--ink); }
.conversation-list { overflow-y: auto; flex: 1; }
.sidebar-tabs { display: flex; border-bottom: 2px solid var(--line); }
.load-more-btn { text-align: center; padding: 10px; color: var(--accent); font-size: 13px; font-weight: 600; cursor: pointer; margin: 8px; background: var(--accent-soft); border-radius: 8px; }
.load-more-btn:hover { opacity: 0.85; }
.typing-indicator { padding: 6px 16px; font-size: 12px; color: var(--muted); font-style: italic; min-height: 24px; }
.sidebar-tab { flex: 1; border: none; background: transparent; padding: 10px 0; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -2px; }
.sidebar-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.conv-item { display: flex; gap: 10px; padding: 12px 16px; cursor: pointer; border-bottom: 1px solid var(--line); }
.conv-item:hover, .conv-item.active { background: var(--accent-soft); }
.conv-meta { flex: 1; min-width: 0; }
.conv-name { font-weight: 600; font-size: 14px; }
.conv-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chat-panel { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); gap: 12px; }
.empty-state .brand-mark { font-size: 40px; }

.chat-view { display: flex; flex-direction: column; height: 100%; }
.chat-header { display: flex; align-items: center; gap: 10px; padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--panel); }
.chat-header-meta { flex: 1; min-width: 0; }
.peer-name { font-weight: 700; }
.peer-status { font-size: 12px; color: var(--muted); }
.peer-status.online { color: var(--accent); font-weight: 600; }
.header-btn { width: 32px; height: 32px; border-radius: 8px; font-size: 15px; padding: 0; }

.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 8px; background: repeating-linear-gradient(135deg, var(--paper), var(--paper) 24px, #f1eee6 24px, #f1eee6 48px); }
.bubble { max-width: 60%; padding: 9px 12px; border-radius: 14px; font-size: 14px; line-height: 1.4; position: relative; }
.bubble.me { align-self: flex-end; background: var(--bubble-me); color: white; border-bottom-right-radius: 4px; }
.bubble.them { align-self: flex-start; background: var(--bubble-them); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.bubble .time { display: inline-flex; align-items: center; gap: 3px; font-size: 10px; opacity: 0.75; margin-top: 4px; }
.bubble img, .bubble video { max-width: 220px; border-radius: 8px; display: block; margin-bottom: 4px; }
.bubble.deleted { font-style: italic; opacity: 0.65; }
.bubble.deleted.them { background: var(--bubble-them); }
.bubble { cursor: pointer; }
.delete-actions { flex-direction: column; align-items: stretch; gap: 8px; }
.delete-actions button { width: 100%; text-align: center; }
.ticks { letter-spacing: -2px; font-size: 12px; }
.ticks.read { color: #6fc7ff; opacity: 1; }

.modal-card.wide { width: 400px; max-height: 80vh; display: flex; flex-direction: column; }
.rename-row { display: flex; gap: 8px; margin-top: 12px; }
.rename-row input { flex: 1; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); }
.member-list { margin-top: 16px; overflow-y: auto; flex: 1; border-top: 1px solid var(--line); }
.member-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.member-info { flex: 1; min-width: 0; }
.member-name { font-weight: 600; font-size: 14px; }
.member-phone { font-size: 12px; color: var(--muted); }
.admin-badge { font-size: 10px; background: var(--accent-soft); color: var(--accent); padding: 2px 7px; border-radius: 10px; font-weight: 700; margin-left: 6px; }
.member-actions { display: flex; gap: 6px; }
.member-actions button { border: none; background: var(--accent-soft); border-radius: 6px; padding: 5px 8px; font-size: 11px; cursor: pointer; }
.ghost-btn.danger { border-color: var(--danger); color: var(--danger); }

/* ---------- Status / Stories ---------- */
.status-bar { display: flex; gap: 14px; padding: 12px 16px; overflow-x: auto; border-bottom: 1px solid var(--line); }
.status-item { display: flex; flex-direction: column; align-items: center; gap: 4px; cursor: pointer; flex-shrink: 0; width: 56px; }
.status-ring { width: 48px; height: 48px; border-radius: 50%; display: flex; align-items: center; justify-content: center; border: 2.5px solid var(--accent-soft); position: relative; }
.status-ring.unseen { border-color: var(--accent); }
.status-ring.subscribed { border-color: #d9b64a; }
.status-ring .avatar { width: 42px; height: 42px; font-size: 18px; }
.status-ring .plus-badge { position: absolute; bottom: -2px; right: -2px; width: 16px; height: 16px; background: var(--accent); color: white; border-radius: 50%; font-size: 11px; display: flex; align-items: center; justify-content: center; border: 2px solid var(--panel); }
.status-item-label { font-size: 10px; color: var(--muted); text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 56px; }

.status-textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); font-size: 15px; resize: none; margin-top: 8px; font-family: inherit; }
.status-swatches { display: flex; gap: 8px; margin-top: 10px; }
.status-swatch { width: 26px; height: 26px; border-radius: 50%; cursor: pointer; border: 2px solid transparent; }
.status-swatch.selected { border-color: var(--ink); }
#statusImageArea label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
#statusImageArea input[type=text] { width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); }

.status-viewer { position: fixed; inset: 0; background: #111; z-index: 100; display: flex; flex-direction: column; }
.status-bars { display: flex; gap: 4px; padding: 8px 10px 0; }
.status-bar-track { flex: 1; height: 3px; background: rgba(255,255,255,0.3); border-radius: 2px; overflow: hidden; }
.status-bar-fill { height: 100%; width: 0%; background: white; }
.status-bar-fill.filled { width: 100%; }
.status-viewer-header { display: flex; align-items: center; gap: 10px; padding: 10px 16px; color: white; }
.status-viewer-meta { flex: 1; }
.status-viewer-meta .peer-name { color: white; }
.status-viewer-meta .peer-status { color: rgba(255,255,255,0.7); }
.status-viewer-header .header-btn { background: rgba(255,255,255,0.15); color: white; }
.status-viewer-content { position: relative; flex: 1; display: flex; align-items: center; justify-content: center; color: white; text-align: center; font-size: 22px; font-weight: 600; padding: 30px; word-break: break-word; }
.status-viewer-content img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 8px; }
.status-nav-zone { position: absolute; top: 60px; bottom: 60px; width: 35%; }
.status-nav-zone.left { left: 0; }
.status-nav-zone.right { right: 0; }
.status-viewers-footer { padding: 14px 16px; color: white; max-height: 30vh; overflow-y: auto; }
.status-viewers-footer .viewer-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; font-size: 13px; }

.connection-banner { padding: 8px 16px; font-size: 12px; text-align: center; background: #fdf1e7; color: var(--danger); border-top: 1px solid var(--line); }
.connection-banner.connecting { background: var(--accent-soft); color: var(--muted); }

.upload-progress { position: fixed; bottom: 0; left: 0; right: 0; z-index: 200; height: 42px; background: var(--panel); border-top: 2px solid var(--accent); overflow: hidden; display: flex; align-items: center; box-shadow: 0 -4px 16px rgba(0,0,0,0.12); }
.upload-progress-fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--accent); opacity: 0.18; transition: width 0.2s ease; width: 0%; }
.upload-progress-label { position: relative; font-size: 13px; padding: 0 16px; color: var(--accent); font-weight: 700; letter-spacing: 0.01em; }

.file-attachment { display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; background: rgba(0,0,0,0.07); border-radius: 8px; text-decoration: none; color: inherit; font-size: 13px; word-break: break-all; }
.attachment-preview { display: flex; align-items: center; justify-content: space-between; padding: 8px 16px; font-size: 13px; background: var(--accent-soft); border-top: 1px solid var(--line); }
.attachment-preview button { border: none; background: transparent; cursor: pointer; font-size: 14px; color: var(--muted); }

.ad-banner { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: #fbf6e8; border-bottom: 1px solid var(--line); font-size: 13px; }
.ad-banner .ad-badge { font-size: 9px; font-weight: 800; letter-spacing: 0.5px; color: #8c6b00; background: #f1e0a3; padding: 2px 6px; border-radius: 4px; flex-shrink: 0; }
.ad-banner .ad-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ad-banner a { color: var(--accent); font-weight: 600; text-decoration: none; flex-shrink: 0; }
.ad-banner .ad-dismiss { cursor: pointer; color: var(--muted); flex-shrink: 0; font-size: 15px; }
.ad-card { display: block; text-decoration: none; color: var(--ink); border-bottom: 1px solid var(--line); padding: 14px 16px; cursor: pointer; }
.ad-card:hover { background: var(--accent-soft); }
.ad-card-img { width: 100%; max-height: 160px; object-fit: cover; border-radius: 10px; margin-bottom: 10px; display: block; }
.ad-card video { width: 100%; max-height: 160px; border-radius: 10px; margin-bottom: 10px; display: block; }
.ad-card-meta { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.ad-card-biz { font-size: 11px; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: 0.05em; }
.ad-card-badge { font-size: 10px; background: #f1e0a3; color: #8c6b00; padding: 2px 7px; border-radius: 6px; font-weight: 700; }
.ad-card-title { font-weight: 700; font-size: 15px; margin: 2px 0 4px; }
.ad-card-desc { font-size: 13px; color: var(--muted); line-height: 1.4; }
.ad-card-cta { display: inline-block; margin-top: 10px; padding: 7px 16px; background: var(--accent); color: white; border-radius: 20px; font-size: 13px; font-weight: 600; }
.ads-empty { text-align: center; color: var(--muted); padding: 40px 20px; font-size: 14px; }
.ads-post-btn { display: block; margin: 14px 16px; padding: 11px; text-align: center; background: var(--accent); color: white; border-radius: 10px; font-weight: 600; font-size: 14px; text-decoration: none; }
.status-ring.sponsored { border-color: #d9b64a; border-style: dashed; }
.biz-status { font-size: 13px; color: var(--danger); margin: 12px 0 0; }
.biz-status.active { color: var(--accent); font-weight: 600; }

/* ---------- Mobile: single-pane navigation (tap a chat = full screen) ---------- */
.back-btn { display: none; border: none; background: transparent; font-size: 20px; line-height: 1; cursor: pointer; padding: 4px 10px 4px 0; color: var(--ink); }

@media (max-width: 768px) {
  .app-shell { position: relative; overflow: hidden; }
  .sidebar { width: 100%; }
  .chat-panel { display: none; width: 100%; max-width: 100vw; overflow: hidden; }
  .app-shell.chat-active .sidebar { display: none; }
  .app-shell.chat-active .chat-panel { display: flex; width: 100%; }
  .back-btn { display: inline-flex; }
  .modal-card, .modal-card.wide { width: 88vw; max-width: 400px; }
  .bubble { max-width: 78%; }
  .messages { padding: 12px 10px; }
}

.message-form { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); background: var(--panel); box-sizing: border-box; width: 100%; min-width: 0; overflow: hidden; }
.attach-btn { cursor: pointer; font-size: 18px; }
.message-form input[type=text] { flex: 1; min-width: 0; padding: 10px 14px; border-radius: 20px; border: 1px solid var(--line); background: var(--paper); font-size: 14px; }
.send-btn { flex-shrink: 0; border: none; background: var(--accent); color: white; width: 38px; height: 38px; border-radius: 50%; cursor: pointer; font-size: 15px; }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(27,31,35,0.4); display: flex; align-items: center; justify-content: center; }
.modal-card { background: var(--panel); border-radius: 14px; padding: 28px; width: 340px; }
.modal-card h3 { margin: 0 0 12px; }
.modal-card label { display: block; font-size: 12px; color: var(--muted); margin: 12px 0 4px; }
.modal-card input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ---------- Search bar ---------- */
.search-bar { display: flex; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.search-bar input { flex: 1; padding: 8px 12px; border-radius: 20px; border: 1px solid var(--line); background: var(--paper); font-size: 14px; }
.search-bar button { border: none; background: transparent; font-size: 16px; cursor: pointer; }
.search-results { padding: 10px 16px; font-size: 13px; color: var(--muted); }

/* ---------- Pinned bar ---------- */
.pinned-bar { display: flex; align-items: center; gap: 8px; padding: 8px 16px; background: var(--accent-soft); border-bottom: 1px solid var(--line); font-size: 13px; cursor: pointer; }
.pinned-bar .pin-icon { color: var(--accent); }

/* ---------- Poll bubble ---------- */
.poll-bubble { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; min-width: 200px; max-width: 280px; }
.poll-question { font-weight: 700; margin-bottom: 10px; font-size: 15px; }
.poll-option { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; cursor: pointer; }
.poll-bar-wrap { flex: 1; background: var(--accent-soft); border-radius: 6px; height: 8px; overflow: hidden; }
.poll-bar-fill { height: 100%; background: var(--accent); border-radius: 6px; transition: width 0.3s; }
.poll-option-label { font-size: 13px; min-width: 100px; }
.poll-votes { font-size: 11px; color: var(--muted); margin-left: 4px; }
.poll-option.voted .poll-option-label { font-weight: 700; color: var(--accent); }

/* ---------- Reaction bar ---------- */
.reaction-bar { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction-chip { font-size: 13px; background: var(--accent-soft); border-radius: 10px; padding: 2px 7px; cursor: pointer; border: 1px solid transparent; }
.reaction-chip.mine { border-color: var(--accent); }
.reaction-picker { display: flex; gap: 6px; padding: 6px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 20px; position: absolute; z-index: 50; box-shadow: 0 4px 16px rgba(0,0,0,0.12); }
.reaction-picker span { font-size: 20px; cursor: pointer; padding: 2px; border-radius: 6px; }
.reaction-picker span:hover { background: var(--accent-soft); }

/* ---------- Voice message ---------- */
.voice-bubble { display: flex; align-items: center; gap: 8px; min-width: 180px; max-width: 260px; }
.voice-play { flex-shrink: 0; border: none; background: var(--accent); color: white; width: 34px; height: 34px; border-radius: 50%; cursor: pointer; font-size: 14px; display: flex; align-items: center; justify-content: center; }
.bubble.me .voice-play { background: rgba(255,255,255,0.25); }
.voice-progress { flex: 1; height: 4px; background: rgba(0,0,0,0.15); border-radius: 2px; overflow: hidden; }
.bubble.me .voice-progress { background: rgba(255,255,255,0.3); }
.voice-progress-fill { height: 100%; width: 0%; background: white; border-radius: 2px; transition: width 0.1s linear; }
.bubble.them .voice-progress-fill { background: var(--accent); }
.voice-duration { font-size: 11px; opacity: 0.75; white-space: nowrap; flex-shrink: 0; }
.voice-recording { display: flex; align-items: center; gap: 8px; padding: 10px 16px; background: var(--accent-soft); border-top: 1px solid var(--line); }
.voice-recording .rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: blink 1s infinite; }
@keyframes blink { 0%,100%{opacity:1;} 50%{opacity:0;} }

/* ---------- Dropdown menu ---------- */
.dropdown-menu { position: absolute; right: 16px; top: 60px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 4px 20px rgba(0,0,0,0.12); z-index: 60; min-width: 180px; }
.dropdown-menu button { display: block; width: 100%; text-align: left; padding: 12px 16px; border: none; background: transparent; cursor: pointer; font-size: 14px; }
.dropdown-menu button:hover { background: var(--accent-soft); }
