:root {
  --bg-primary: #0e1621;
  --bg-secondary: #17212b;
  --bg-tertiary: #242f3d;
  --bg-bubble-out: #2b5278;
  --bg-bubble-in: #182533;
  --text-main: #f5f5f5;
  --text-muted: #708499;
  --accent: #2481cc;
  --accent-hover: #1d6fa8;
  --danger: #ef4444;
  --border-color: #242f3d;
  --status-read: #4faeeb;
  --status-delivered: #8e9ca8;
}
body.light-theme {
  --bg-primary: #ffffff;
  --bg-secondary: #f4f4f5;
  --bg-tertiary: #e4e4e7;
  --bg-bubble-out: #eeffde;
  --bg-bubble-in: #ffffff;
  --text-main: #18181b;
  --text-muted: #71717a;
  --accent: #2481cc;
  --accent-hover: #1d6fa8;
  --danger: #dc2626;
  --border-color: #e4e4e7;
  --status-read: #3390ec;
  --status-delivered: #9ca3af;
}

* { box-sizing: border-box; margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; width: 100%; overflow: hidden; background: var(--bg-primary); color: var(--text-main); }

.hidden { display: none !important; }
.full-width { width: 100%; }

.app-layout { display: flex; flex-direction: row; width: 100vw; height: 100vh; height: 100dvh; overflow: hidden; position: relative; }

.sidebar { width: 360px; min-width: 300px; max-width: 420px; flex-shrink: 0; background: var(--bg-secondary); border-right: 1px solid var(--border-color); display: flex; flex-direction: column; height: 100%; z-index: 10; }
.sidebar-header { height: 60px; padding: 0 12px 0 16px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border-color); flex-shrink: 0; }
.user-profile-badge { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 16px; flex-shrink: 0; text-transform: uppercase; user-select: none; }
.user-meta { display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.user-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-status { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.header-actions { display: flex; gap: 2px; flex-shrink: 0; }
.icon-btn { background: transparent; border: none; font-size: 17px; cursor: pointer; padding: 8px; border-radius: 8px; color: var(--text-muted); transition: background 0.2s, color 0.2s; display: flex; align-items: center; justify-content: center; }
.icon-btn:hover { background: var(--bg-tertiary); color: var(--text-main); }
.icon-btn.danger-hover:hover { background: rgba(239,68,68,0.2); color: var(--danger); }
.mobile-back-btn { display: none; }

.push-banner { background: var(--bg-tertiary); padding: 8px 14px; display: flex; align-items: center; justify-content: space-between; font-size: 13px; border-bottom: 1px solid var(--border-color); flex-shrink: 0; }
.offline-banner { background: rgba(239,68,68,0.15); color: var(--danger); padding: 8px 14px; font-size: 13px; text-align: center; border-bottom: 1px solid var(--border-color); flex-shrink: 0; }
.btn-sm { padding: 4px 10px; font-size: 12px; }

.chat-list { flex: 1; overflow-y: auto; }
.chat-item { display: flex; align-items: center; padding: 10px 16px; gap: 12px; cursor: pointer; border-bottom: 1px solid rgba(128,128,128,0.06); transition: background 0.15s; }
.chat-item:hover { background: var(--bg-tertiary); }
.chat-item.active { background: var(--accent); color: #fff; }
.chat-item.active .chat-item-last-msg, .chat-item.active .chat-item-name, .chat-item.active .chat-item-time { color: #fff; }
.chat-item-info { flex: 1; min-width: 0; }
.chat-item-top { display: flex; justify-content: space-between; margin-bottom: 4px; gap: 8px; }
.chat-item-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-time { font-size: 11px; color: var(--text-muted); flex-shrink: 0; }
.chat-item-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.chat-item-last-msg { font-size: 13px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.unread-badge { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }

.chat-area { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg-primary); height: 100%; position: relative; }
.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-muted); text-align: center; padding: 20px; }
.empty-icon { font-size: 54px; margin-bottom: 12px; }
.empty-state h3 { color: var(--text-main); margin-bottom: 6px; }

.active-chat { display: flex; flex-direction: column; height: 100%; min-height: 0; flex: 1; }
.chat-header { height: 60px; padding: 0 20px; display: flex; align-items: center; gap: 12px; border-bottom: 1px solid var(--border-color); background: var(--bg-secondary); flex-shrink: 0; }
.chat-info { min-width: 0; flex: 1; }
.chat-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-subtitle { font-size: 12px; color: var(--accent); }

.selection-header { justify-content: space-between; background: var(--bg-tertiary) !important; }
.selection-info { display: flex; align-items: center; gap: 12px; font-weight: 600; font-size: 15px; }
.selection-actions { display: flex; gap: 8px; }

.message-feed-wrapper { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; position: relative; }
.message-feed { width: 100%; max-width: 820px; margin: 0 auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 10px; flex: 1; }

.new-messages-btn { position: sticky; bottom: 12px; left: 50%; transform: translateX(-50%); align-self: center; background: var(--accent); color: #fff; border: none; border-radius: 16px; padding: 8px 16px; font-size: 13px; font-weight: 600; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.3); z-index: 5; }

.date-separator { align-self: center; background: var(--bg-tertiary); color: var(--text-muted); font-size: 11px; font-weight: 600; padding: 4px 12px; border-radius: 10px; margin: 6px 0; user-select: none; }

.message-row { display: flex; align-items: center; position: relative; width: 100%; touch-action: pan-y; }
.message-row.outgoing { justify-content: flex-end; }
.message-row.incoming { justify-content: flex-start; }

.msg-select-check { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--text-muted); margin-right: 12px; display: flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0; transition: all 0.2s; }
.message-row.selected .msg-select-check { background: var(--accent); border-color: var(--accent); }
.message-row.selected .msg-select-check::after { content: '✓'; color: #fff; font-size: 12px; font-weight: bold; }

.message-bubble { max-width: 72%; padding: 8px 12px; border-radius: 18px; position: relative; word-wrap: break-word; font-size: 14.5px; line-height: 1.4; box-shadow: 0 1px 2px rgba(0,0,0,0.2); transition: transform 0.2s cubic-bezier(0.175,0.885,0.32,1.275), background 0.2s; -webkit-touch-callout: none; user-select: none; cursor: pointer; }
.message-bubble.outgoing { background: var(--bg-bubble-out); border-bottom-right-radius: 4px; }
.message-bubble.incoming { background: var(--bg-bubble-in); border-bottom-left-radius: 4px; }
.message-row.selected .message-bubble { outline: 2px solid var(--accent); }
.message-bubble img.chat-image { max-width: 100%; max-height: 360px; border-radius: 12px; margin-top: 4px; display: block; cursor: zoom-in; object-fit: cover; }

.voice-msg-player { display: flex; align-items: center; gap: 10px; min-width: 220px; padding: 4px 0; }
.voice-play-btn { width: 38px; height: 38px; border-radius: 50%; background: var(--accent); color: #fff; border: none; display: flex; align-items: center; justify-content: center; font-size: 14px; cursor: pointer; flex-shrink: 0; }
.voice-wave-container { flex: 1; display: flex; flex-direction: column; gap: 4px; }
.voice-progress-bar { width: 100%; height: 4px; background: rgba(128,128,128,0.25); border-radius: 2px; position: relative; cursor: pointer; }
.voice-progress-fill { height: 100%; background: var(--status-read); width: 0%; border-radius: 2px; }
.voice-meta { display: flex; justify-content: space-between; font-size: 11px; color: var(--text-muted); }

.message-meta { display: inline-flex; align-items: center; gap: 4px; float: right; margin-left: 10px; margin-top: 4px; font-size: 11px; color: var(--text-muted); user-select: none; }
.msg-status { font-size: 12px; line-height: 1; font-weight: bold; }
.msg-status.sending, .msg-status.queued { color: var(--text-muted); opacity: 0.7; }
.msg-status.sent { color: var(--text-delivered, var(--status-delivered)); }
.msg-status.delivered { color: var(--status-delivered); }
.msg-status.read { color: var(--status-read); }
.msg-status.failed { color: var(--danger); }
.message-edited { font-size: 10px; color: var(--text-muted); margin-left: 4px; }

.reply-quote { border-left: 3px solid var(--accent); padding: 2px 8px; margin-bottom: 6px; background: rgba(128,128,128,0.12); border-radius: 0 4px 4px 0; font-size: 12px; cursor: pointer; }
.reply-quote-sender { font-weight: 600; color: var(--accent); }
.reply-quote-text { color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }

.file-link { display: flex; align-items: center; gap: 8px; color: var(--accent); text-decoration: none; font-size: 14px; }
.file-link:hover { text-decoration: underline; }

.swipe-reply-icon { position: absolute; right: -42px; width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 14px; opacity: 0; transform: scale(0.5); transition: opacity 0.15s, transform 0.15s; pointer-events: none; }

.input-panel { background: var(--bg-secondary); border-top: 1px solid var(--border-color); flex-shrink: 0; padding: 8px 16px; padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
.action-banner { display: flex; align-items: center; padding: 6px 12px; background: var(--bg-tertiary); gap: 10px; border-radius: 8px; margin-bottom: 8px; max-width: 820px; margin-left: auto; margin-right: auto; }
.action-banner-border { width: 3px; height: 32px; background: var(--accent); border-radius: 2px; }
.action-banner-content { flex: 1; overflow: hidden; font-size: 12px; }
.action-banner-title { font-weight: 600; color: var(--accent); }
.action-banner-text { color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.action-banner-close { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 16px; }

.input-bar { display: flex; align-items: flex-end; gap: 8px; max-width: 820px; margin: 0 auto; }
.input-bar textarea { flex: 1; background: var(--bg-primary); border: 1px solid var(--border-color); color: var(--text-main); padding: 10px 14px; border-radius: 20px; outline: none; resize: none; font-size: 14px; line-height: 20px; max-height: 140px; }
.send-btn { background: var(--accent) !important; color: #fff !important; }
.send-btn:hover { background: var(--accent-hover) !important; }

.voice-panel { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; max-width: 820px; margin: 0 auto; }
.recording-indicator { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--danger); font-size: 14px; }
.rec-dot { width: 10px; height: 10px; background: var(--danger); border-radius: 50%; animation: pulse 1s infinite; }
@keyframes pulse { 0% { opacity: 1; } 50% { opacity: 0.3; } 100% { opacity: 1; } }

.btn { border: none; padding: 10px 16px; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px; transition: opacity 0.2s; }
.btn:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-cancel { background: var(--bg-tertiary); color: var(--text-main); }
.btn-danger-soft { background: rgba(239,68,68,0.15); color: var(--danger); text-align: left; }

.modal-overlay, .auth-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.65); display: flex; align-items: center; justify-content: center; z-index: 100; backdrop-filter: blur(4px); }
.modal-card, .auth-card { background: var(--bg-secondary); border-radius: 14px; width: 90%; max-width: 440px; padding: 24px; box-shadow: 0 10px 30px rgba(0,0,0,0.5); border: 1px solid var(--border-color); max-height: 90vh; overflow-y: auto; }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal-hint { font-size: 14px; color: var(--text-muted); margin-bottom: 16px; }
.modal-actions { display: flex; flex-direction: column; gap: 10px; }
.close-btn { background: none; border: none; font-size: 18px; color: var(--text-muted); cursor: pointer; }
.small-label { font-size: 13px; color: var(--text-muted); margin: 10px 0 6px 0; display: block; }

.input-group { margin-bottom: 14px; }
.input-group label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.input-group input, .modal-body input { width: 100%; background: var(--bg-primary); border: 1px solid var(--border-color); color: var(--text-main); padding: 10px 12px; border-radius: 8px; outline: none; font-size: 14px; }
.input-row { display: flex; gap: 10px; }

.auth-logo { text-align: center; margin-bottom: 20px; }
.logo-circle { width: 54px; height: 54px; background: var(--accent); border-radius: 50%; margin: 0 auto 10px; display: flex; align-items: center; justify-content: center; font-size: 22px; font-weight: bold; color: #fff; }
.auth-tabs { display: flex; border-bottom: 1px solid var(--border-color); margin-bottom: 16px; }
.auth-tabs button { flex: 1; background: none; border: none; padding: 10px; color: var(--text-muted); font-size: 14px; cursor: pointer; border-bottom: 2px solid transparent; }
.auth-tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.auth-error { background: rgba(239,68,68,0.15); color: var(--danger); padding: 8px; border-radius: 6px; font-size: 13px; margin-top: 12px; text-align: center; }

.context-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 190; backdrop-filter: blur(2px); }
.context-menu { position: fixed; background: var(--bg-secondary); border: 1px solid var(--border-color); border-radius: 12px; padding: 6px; z-index: 200; box-shadow: 0 8px 24px rgba(0,0,0,0.5); min-width: 190px; }
.context-menu-item { padding: 9px 14px; font-size: 13.5px; cursor: pointer; border-radius: 8px; display: flex; align-items: center; gap: 10px; }
.context-menu-item:hover { background: var(--bg-tertiary); }
.context-menu-item.danger { color: var(--danger); }

.members-checklist { max-height: 180px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.member-checkbox-item { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 6px; background: var(--bg-primary); }

.image-viewer-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.92); display: flex; align-items: center; justify-content: center; z-index: 300; backdrop-filter: blur(8px); }
.image-viewer-overlay img { max-width: 92vw; max-height: 92vh; object-fit: contain; border-radius: 8px; box-shadow: 0 10px 40px rgba(0,0,0,0.8); }
.image-viewer-close { position: absolute; top: 20px; right: 24px; background: rgba(255,255,255,0.2); color: #fff; border: none; font-size: 24px; width: 44px; height: 44px; border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; }

.toast-container { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 400; pointer-events: none; }
.toast { background: var(--bg-tertiary); color: var(--text-main); border-left: 3px solid var(--accent); border-radius: 8px; padding: 10px 16px; font-size: 13px; box-shadow: 0 6px 18px rgba(0,0,0,0.4); max-width: 90vw; animation: fadeIn 0.2s; }
.toast.error { border-left-color: var(--danger); }
.toast.success { border-left-color: #22c55e; }

.modal-search-results { max-height: 200px; overflow-y: auto; display: flex; flex-direction: column; }
.contacts-list { max-height: 240px; overflow-y: auto; display: flex; flex-direction: column; }
.forward-chat-list { max-height: 320px; overflow-y: auto; display: flex; flex-direction: column; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }

@media (max-width: 768px) {
  .sidebar { width: 100%; max-width: none; }
  .chat-area { display: none; position: fixed; inset: 0; z-index: 50; }
  .chat-area.active { display: flex; }
  .mobile-back-btn { display: flex; }
  .message-bubble { max-width: 82%; }
  .context-menu { position: fixed !important; left: 50% !important; top: auto !important; bottom: calc(20px + env(safe-area-inset-bottom)) !important; transform: translateX(-50%) !important; width: 92% !important; max-width: 380px !important; border-radius: 16px !important; padding: 8px !important; animation: slideUpSheet 0.25s cubic-bezier(0.2,0.9,0.3,1); }
  .context-menu-item { padding: 13px 16px !important; font-size: 15px !important; }
}
@keyframes slideUpSheet { from { transform: translate(-50%,100%); opacity: 0; } to { transform: translate(-50%,0); opacity: 1; } }

/* ===== Layout fix v7 ===== */
.app-layout { position: fixed; inset: 0; height: auto; }

/* ===== Overflow fix v8 ===== */
@media (max-width: 768px) {
  .input-bar textarea { min-width: 0; }
  .sidebar { width: 100vw; max-width: 100vw; }
  .chat-area { max-width: 100vw; }
  .chat-header { padding: 0 10px; gap: 8px; }
  .message-feed { padding: 12px 10px; }
  .input-panel { padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); }
  .header-actions .icon-btn { padding: 5px; }
  .user-name { max-width: 34vw; }
}

/* ===== Anti-zoom v9 ===== */
* { touch-action: manipulation; }
input, textarea, select { font-size: 16px; }
