:root {
  --blue: #3a76f0;
  --blue-hover: #2f67db;
  --blue-soft: rgba(58, 118, 240, 0.16);
  --radius: 18px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --mono: "SF Mono", "Consolas", "Liberation Mono", Menlo, monospace;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  --sidebar-w: 340px;
  --topbar-h: 58px;
}

html[data-theme="dark"], :root {
  --bg: #101114;
  --bg-raised: #17181c;
  --bg-chat: #1c1d22;
  --bg-hover: #22232a;
  --bg-input: #101114;
  --text: #e8eaed;
  --text-dim: #9aa0a8;
  --text-faint: #6d737c;
  --border: #2a2c33;
  --incoming: #2c2e36;
  --incoming-text: #e8eaed;
  --outgoing: #3a76f0;
  --outgoing-text: #ffffff;
  --card: #22242b;
  --danger: #f16a6a;
  --ok: #4caf7a;
  --scrollbar: #3a3c44;
  color-scheme: dark;
}

html[data-theme="light"] {
  --bg: #f4f5f7;
  --bg-raised: #ffffff;
  --bg-chat: #e9eef6;
  --bg-hover: #eef0f4;
  --bg-input: #f4f5f7;
  --text: #1a1c20;
  --text-dim: #5c6370;
  --text-faint: #8a9099;
  --border: #e2e5eb;
  --incoming: #ffffff;
  --incoming-text: #1a1c20;
  --outgoing: #3a76f0;
  --outgoing-text: #ffffff;
  --card: #ffffff;
  --danger: #c62828;
  --ok: #2e7d4f;
  --scrollbar: #c5c9d1;
  --shadow: 0 8px 32px rgba(20, 30, 50, 0.12);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

button, input { font-family: inherit; }

/* ---------- Login ---------- */
.login-body {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(58, 118, 240, 0.18), transparent 60%),
    var(--bg);
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 40px 36px 32px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-logo { margin-bottom: 16px; }
.login-card h1 {
  margin: 0 0 6px;
  font-size: 24px;
  font-weight: 650;
  letter-spacing: -0.02em;
}
.login-sub { margin: 0 0 24px; color: var(--text-dim); font-size: 14px; }
.login-error {
  background: rgba(241, 106, 106, 0.12);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 16px;
  font-size: 14px;
}
.login-form { text-align: left; }
.login-form label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.login-form input[type="password"] {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.login-form input[type="password"]:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-soft);
}
.login-form button {
  width: 100%;
  margin-top: 16px;
  padding: 12px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--blue);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.login-form button:hover { background: var(--blue-hover); }

/* ---------- App chrome ---------- */
.app-body { display: flex; flex-direction: column; height: 100%; overflow: hidden; }

.topbar {
  height: var(--topbar-h);
  flex: 0 0 var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 16px;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand strong { display: block; font-size: 15px; letter-spacing: -0.01em; }
.brand-sub { display: block; font-size: 11px; color: var(--text-faint); text-transform: lowercase; }
.topbar-stats {
  color: var(--text-dim);
  font-size: 13px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.topbar-stats b { color: var(--text); font-weight: 600; }
.dot { color: var(--text-faint); }

.icon-btn, .text-btn {
  background: transparent;
  border: 0;
  color: var(--text-dim);
  cursor: pointer;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn { width: 36px; height: 36px; }
.icon-btn:hover, .text-btn:hover { background: var(--bg-hover); color: var(--text); }
.text-btn { padding: 6px 10px; font-size: 13px; }
.logout-form { margin: 0; }

.shell {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  display: flex;
  flex-direction: column;
  background: var(--bg-raised);
  border-right: 1px solid var(--border);
  min-width: 0;
}
.search-wrap { padding: 12px 12px 8px; }
.search-wrap input {
  width: 100%;
  border: 0;
  border-radius: 18px;
  padding: 9px 14px;
  background: var(--bg-input);
  color: var(--text);
  outline: none;
  font-size: 14px;
  border: 1px solid var(--border);
}
.search-wrap input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-soft);
}
.chat-list { flex: 1; overflow-y: auto; }
.chat-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  cursor: pointer;
  border: 0;
  width: 100%;
  background: transparent;
  color: inherit;
  text-align: left;
  font: inherit;
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--blue-soft); }
.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  flex: 0 0 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 650;
  font-size: 16px;
  color: #fff;
  background: #5a6a88;
  user-select: none;
}
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .row1, .chat-item .row2 {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
}
.chat-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-time { color: var(--text-faint); font-size: 12px; flex-shrink: 0; }
.chat-preview {
  color: var(--text-dim);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}
.chat-count {
  font-size: 11px;
  color: var(--text-faint);
  flex-shrink: 0;
}
.empty-chats { padding: 32px 20px; text-align: center; color: var(--text-dim); }
.muted { color: var(--text-faint); font-size: 13px; }

/* ---------- Conversation ---------- */
.conversation {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background:
    radial-gradient(800px 400px at 80% 0%, rgba(58, 118, 240, 0.06), transparent 50%),
    var(--bg-chat);
}
.conv-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 18px;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
  min-height: 58px;
}
.conv-head .avatar { width: 36px; height: 36px; flex-basis: 36px; font-size: 14px; }
.conv-title { font-weight: 650; line-height: 1.25; }
.conv-contact {
  font-size: 12.5px;
  color: var(--text-dim);
  line-height: 1.3;
  margin-top: 1px;
  display: flex;
  flex-wrap: wrap;
  gap: 0 8px;
  align-items: baseline;
}
.conv-contact .contact-item { white-space: nowrap; }
.conv-contact .contact-sep { color: var(--text-faint); opacity: 0.7; }
.conv-meta { font-size: 12px; color: var(--text-faint); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px 28px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.placeholder {
  margin: auto;
  text-align: center;
  max-width: 360px;
  color: var(--text-dim);
}
.placeholder h2 { color: var(--text); margin: 16px 0 8px; font-size: 22px; font-weight: 650; }
.placeholder p { margin: 0; }
.placeholder-icon { display: flex; justify-content: center; }

.day-sep {
  align-self: center;
  margin: 14px 0 10px;
  padding: 4px 12px;
  border-radius: 12px;
  background: var(--bg-raised);
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: min(72%, 560px);
  margin: 2px 0;
}
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; }
.sender-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--blue);
  margin: 6px 12px 2px;
}
.bubble {
  padding: 8px 12px 6px;
  border-radius: 18px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  line-height: 1.4;
  box-shadow: 0 1px 1px rgba(0,0,0,0.08);
}
.msg.in .bubble {
  background: var(--incoming);
  color: var(--incoming-text);
  border-bottom-left-radius: 6px;
}
.msg.out .bubble {
  background: var(--outgoing);
  color: var(--outgoing-text);
  border-bottom-right-radius: 6px;
}
.msg.in.stack .bubble { border-top-left-radius: 8px; }
.msg.out.stack .bubble { border-top-right-radius: 8px; }
.msg.in.stack { margin-top: 1px; }
.msg.out.stack { margin-top: 1px; }

.bubble .body:empty { display: none; }
.time {
  display: block;
  text-align: right;
  font-size: 11px;
  margin-top: 4px;
  opacity: 0.72;
  font-variant-numeric: tabular-nums;
}

/* Attachments */
.att-list { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.att-list:first-child { margin-top: 0; }
.att-image {
  display: block;
  max-width: 280px;
  max-height: 320px;
  border-radius: 12px;
  overflow: hidden;
  cursor: zoom-in;
  margin: 2px 0 4px;
}
.att-image img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 320px;
  object-fit: cover;
  background: rgba(0,0,0,0.15);
}
.att-video {
  max-width: 320px;
  border-radius: 12px;
  margin: 2px 0 4px;
  background: #000;
}
.att-video video { display: block; width: 100%; max-height: 320px; border-radius: 12px; }

.att-audio {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 220px;
  padding: 6px 4px 2px;
}
.att-audio .mic {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255,255,255,0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.msg.in .att-audio .mic { background: var(--blue-soft); color: var(--blue); }
.att-audio audio { flex: 1; height: 32px; width: 180px; }

.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(0,0,0,0.12);
  text-decoration: none;
  color: inherit;
  min-width: 200px;
  max-width: 320px;
}
.msg.in .file-card { background: rgba(0,0,0,0.06); }
html[data-theme="light"] .msg.in .file-card { background: #f0f2f6; }
.file-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: rgba(255,255,255,0.16);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.msg.in .file-icon { background: var(--blue-soft); color: var(--blue); }
.file-meta { min-width: 0; flex: 1; }
.file-name {
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-size { font-size: 12px; opacity: 0.75; }
.file-dl { font-size: 16px; opacity: 0.8; }

.conv-foot { padding: 6px 16px 10px; text-align: center; }

/* Lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 32px;
}
.lightbox[hidden] { display: none; }
.lightbox img {
  max-width: 96vw;
  max-height: 92vh;
  border-radius: 8px;
  box-shadow: var(--shadow);
}
.lightbox-close {
  position: absolute;
  top: 16px;
  right: 20px;
  background: transparent;
  border: 0;
  color: #fff;
  font-size: 36px;
  cursor: pointer;
  line-height: 1;
}

/* Scrollbars */
.chat-list, .messages {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar) transparent;
}

/* Mobile */
@media (max-width: 760px) {
  .topbar-stats { display: none; }
  .sidebar { width: 100%; flex-basis: 100%; }
  .conversation { display: none; }
  .shell.show-chat .sidebar { display: none; }
  .shell.show-chat .conversation { display: flex; width: 100%; }
  .msg { max-width: 88%; }
  .brand-sub { display: none; }
}

@media (min-width: 761px) {
  .back-btn { display: none !important; }
}

/* Sender + snapshot tag — always visible on incoming */
.sender-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 6px 12px 2px;
  flex-wrap: wrap;
}
.sender-row .sender-label { margin: 0; }
.snap-tag {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-faint);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1px 7px;
  letter-spacing: 0.01em;
}
.msg.out .snap-tag { align-self: flex-end; margin: 0 4px 2px auto; }

/* Pending attachment chips */
.att-pending {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(0,0,0,0.12);
  font-size: 13px;
  min-width: 180px;
}
html[data-theme="light"] .msg.in .att-pending { background: #f0f2f6; }
.att-ico { font-size: 16px; line-height: 1; }
.att-pending span:last-child { opacity: 0.95; }

/* Voice bubble */
.att-audio.voice {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  min-width: 240px;
  padding: 4px 2px 2px;
}
.att-audio.voice .voice-meta { flex: 1; min-width: 0; }
.voice-title { font-size: 13px; font-weight: 650; }
.voice-dur { font-size: 11px; opacity: 0.75; margin: 1px 0 4px; }
.att-audio.voice audio { width: 100%; height: 32px; }

.chat-item.unlinked { opacity: 0.95; }
.chat-item.unlinked .chat-name { font-style: italic; }


/* Devices */
.device-panel {
  padding: 10px 12px 4px;
  border-bottom: 1px solid var(--border);
}
.device-head {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin: 0 2px 8px;
}
.device-list { display: flex; flex-direction: column; gap: 4px; max-height: 180px; overflow-y: auto; }
.device-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  padding: 7px 8px;
  border-radius: 12px;
  cursor: pointer;
}
.device-item:hover { background: var(--bg-hover); }
.device-item.active { background: var(--blue-soft); }
.device-item .d-av {
  width: 28px; height: 28px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; color: #fff; flex: 0 0 28px;
}
.device-item .d-meta { flex: 1; min-width: 0; }
.device-item .d-name {
  font-weight: 600; font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.device-item .d-sub { font-size: 11px; color: var(--text-faint); }
.device-item .d-rename {
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  border-radius: 8px;
  padding: 4px 6px;
  font-size: 13px;
}
.device-item .d-rename:hover { background: var(--bg-raised); color: var(--text); }
.device-empty { font-size: 12px; color: var(--text-faint); padding: 4px 2px 8px; }

/* Export / wipe / modal */
.danger-btn { color: var(--danger) !important; }
.danger-btn:hover { background: rgba(241, 106, 106, 0.12) !important; }
.login-hint {
  background: rgba(58, 118, 240, 0.12);
  color: var(--text);
  border: 1px solid rgba(58, 118, 240, 0.35);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  text-align: left;
  margin: 0 0 14px;
  line-height: 1.4;
}
.d-actions { display: flex; gap: 2px; flex: 0 0 auto; }
.device-item .d-wipe:hover { color: var(--danger); }
.modal {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal[hidden] { display: none !important; }
.modal-card {
  width: 100%; max-width: 420px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 22px 20px 16px;
  box-shadow: var(--shadow);
}
.modal-card h3 { margin: 0 0 8px; font-size: 18px; }
.modal-text { margin: 0 0 14px; color: var(--text-dim); font-size: 14px; line-height: 1.45; }
.modal-label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; }
.modal-card input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  font-size: 15px;
  margin-bottom: 14px;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
@media (max-width: 900px) {
  .topbar-right .text-btn { padding: 6px 8px; font-size: 12px; }
}
