:root {
  --bg-deep: #0d0b14;
  --bg-panel: #14111d;
  --bg-panel-2: #1b1628;
  --bg-elevated: #221c33;
  --line: rgba(255, 255, 255, 0.08);
  --line-soft: rgba(255, 255, 255, 0.04);
  --text-primary: #f3f0f8;
  --text-muted: #948da9;
  --text-faint: #635c78;
  --ember: #ff5c7a;
  --ember-soft: rgba(255, 92, 122, 0.15);
  --glow: #7c9eff;
  --glow-soft: rgba(124, 158, 255, 0.15);
  --danger: #ff5c5c;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 9px;
  --font-display: "Fraunces", serif;
  --font-body: "Inter", -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg-deep);
  color: var(--text-primary);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; }

.hidden { display: none !important; }

.screen { height: 100vh; width: 100vw; }

/* ============ AUTH SCREEN ============ */

#auth-screen {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse 70% 50% at 50% 0%, rgba(255, 92, 122, 0.12), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(124, 158, 255, 0.10), transparent),
    var(--bg-deep);
  overflow: hidden;
}

.ember-field { position: absolute; inset: 0; pointer-events: none; }
.ember-field span {
  position: absolute;
  bottom: -20px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: radial-gradient(circle, #ffb37a 0%, #ff5c7a 60%, transparent 100%);
  opacity: 0;
  animation: rise 9s linear infinite;
  filter: blur(0.3px);
}
.ember-field span:nth-child(1) { left: 8%; animation-delay: 0.5s; }
.ember-field span:nth-child(2) { left: 22%; animation-delay: 3s; width: 3px; height: 3px; }
.ember-field span:nth-child(3) { left: 45%; animation-delay: 1.5s; }
.ember-field span:nth-child(4) { left: 63%; animation-delay: 5s; width: 4px; height: 4px; }
.ember-field span:nth-child(5) { left: 80%; animation-delay: 2.2s; }
.ember-field span:nth-child(6) { left: 92%; animation-delay: 6.5s; width: 3px; height: 3px; }

@keyframes rise {
  0% { transform: translateY(0) translateX(0); opacity: 0; }
  10% { opacity: 0.9; }
  50% { transform: translateY(-45vh) translateX(15px); }
  90% { opacity: 0; }
  100% { transform: translateY(-95vh) translateX(-10px); opacity: 0; }
}

.auth-card {
  position: relative;
  z-index: 1;
  width: 380px;
  max-width: 90vw;
  background: linear-gradient(180deg, var(--bg-panel-2), var(--bg-panel));
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 36px 32px 28px;
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.6);
}

.brand { text-align: center; margin-bottom: 24px; }
.brand-mark { font-size: 34px; line-height: 1; margin-bottom: 6px; filter: drop-shadow(0 0 14px rgba(255, 92, 122, 0.5)); }
.brand h1 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 30px;
  margin: 0;
  letter-spacing: 0.3px;
}
.tagline { color: var(--text-muted); font-size: 13.5px; margin: 6px 0 0; }

.auth-tabs {
  display: flex;
  background: var(--bg-deep);
  border-radius: 999px;
  padding: 4px;
  margin-bottom: 22px;
  border: 1px solid var(--line);
}
.tab-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 9px 0;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 600;
  transition: all 0.2s ease;
}
.tab-btn.active {
  background: linear-gradient(135deg, var(--ember), #ff8c6a);
  color: #1a0d10;
}

.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12.5px;
  color: var(--text-muted);
  font-weight: 500;
}
.auth-form input {
  background: var(--bg-deep);
  border: 1px solid var(--line);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  padding: 11px 13px;
  font-size: 14.5px;
  outline: none;
  transition: border-color 0.15s ease;
}
.auth-form input:focus { border-color: var(--ember); }

.form-hint { color: var(--text-faint); font-size: 11.5px; margin: -6px 0 0; }
.form-error { color: var(--danger); font-size: 12.5px; margin: -4px 0 0; min-height: 0; }
.form-error:empty { display: none; }

.btn-primary {
  margin-top: 4px;
  border: none;
  background: linear-gradient(135deg, var(--ember), #ff8c6a);
  color: #1a0d10;
  font-weight: 700;
  font-size: 14.5px;
  padding: 12px;
  border-radius: var(--radius-sm);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 8px 20px -6px rgba(255, 92, 122, 0.5); }
.btn-primary:active { transform: translateY(0); }

.auth-footnote {
  text-align: center;
  color: var(--text-faint);
  font-size: 11.5px;
  margin: 22px 0 0;
  line-height: 1.5;
}

/* ============ APP SCREEN ============ */

#app-screen { display: flex; height: 100vh; }

.sidebar {
  width: 300px;
  min-width: 300px;
  background: var(--bg-panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 18px 14px;
  border-bottom: 1px solid var(--line-soft);
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: linear-gradient(135deg, var(--ember), var(--glow));
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px; color: #0d0b14;
  flex-shrink: 0;
}
.avatar.small { width: 34px; height: 34px; font-size: 13px; }
.me-name { font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 140px; }
.me-sub { font-size: 10.5px; color: var(--text-faint); letter-spacing: 0.3px; text-transform: uppercase; }

.icon-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  color: var(--text-muted);
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
  transition: all 0.15s ease;
  flex-shrink: 0;
}
.icon-btn:hover { color: var(--text-primary); border-color: var(--ember); }
.icon-btn.tiny { width: 22px; height: 22px; font-size: 10px; }

.add-contact { display: flex; gap: 8px; padding: 14px 18px 6px; }
.add-contact input {
  flex: 1;
  background: var(--bg-deep);
  border: 1px solid var(--line);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: 13.5px;
  outline: none;
}
.add-contact input:focus { border-color: var(--glow); }
#add-contact-error { padding: 0 18px; margin: 4px 0 0; }

.contact-list { flex: 1; overflow-y: auto; padding: 10px 10px; }
.empty-hint { color: var(--text-faint); font-size: 12.5px; padding: 20px 12px; line-height: 1.6; text-align: center; }

.contact {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 10px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background 0.15s ease;
  margin-bottom: 2px;
  position: relative;
}
.contact:hover { background: var(--bg-elevated); }
.contact.active { background: var(--bg-elevated); box-shadow: inset 2px 0 0 var(--ember); }
.contact-name { font-weight: 600; font-size: 13.5px; }
.contact-preview { font-size: 11.5px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 170px; }
.contact-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint);
  position: absolute; right: 14px; top: 14px;
}
.contact-dot.online { background: #4ade80; box-shadow: 0 0 6px #4ade80; }

.avatar-wrap { position: relative; flex-shrink: 0; }
.unread-badge {
  position: absolute; top: -4px; right: -6px;
  background: linear-gradient(135deg, var(--ember), #d9436a);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 999px;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--bg-panel);
  line-height: 1;
}

/* ============ CHAT PANEL ============ */

.chat-panel { flex: 1; display: flex; flex-direction: column; background: var(--bg-deep); min-width: 0; }

.chat-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text-faint); text-align: center;
}
.ember-big { font-size: 46px; margin-bottom: 10px; opacity: 0.5; }
.chat-empty h2 { font-family: var(--font-display); font-weight: 500; color: var(--text-muted); margin: 0 0 6px; }
.chat-empty p { font-size: 13px; margin: 0; }

.chat-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-panel);
}
.chat-header-user { display: flex; align-items: center; gap: 12px; }
.chat-header-name { font-weight: 600; font-size: 15px; }
.chat-header-status { font-size: 11.5px; color: var(--text-faint); }
.chat-header-status.online { color: #4ade80; }
.chat-header-note {
  font-size: 11px; color: var(--text-faint);
  border: 1px solid var(--line); padding: 5px 11px; border-radius: 999px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 22px 26px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg-row { display: flex; margin-bottom: 10px; max-width: 100%; }
.msg-row.mine { justify-content: flex-end; }
.msg-row.theirs { justify-content: flex-start; }

.msg-group { max-width: 62%; display: flex; flex-direction: column; gap: 2px; }
.msg-row.mine .msg-group { align-items: flex-end; }
.msg-row.theirs .msg-group { align-items: flex-start; }

.bubble {
  position: relative;
  padding: 10px 14px 8px;
  border-radius: var(--radius-md);
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  white-space: pre-wrap;
  cursor: default;
}
.msg-row.mine .bubble {
  background: linear-gradient(135deg, var(--ember), #d9436a);
  color: #1c0a10;
  border-bottom-right-radius: 4px;
}
.msg-row.theirs .bubble {
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.bubble .reply-quote {
  display: block;
  font-size: 12px;
  padding: 6px 9px;
  border-radius: 8px;
  margin-bottom: 6px;
  opacity: 0.85;
  border-left: 2px solid currentColor;
}
.msg-row.mine .reply-quote { background: rgba(0,0,0,0.12); }
.msg-row.theirs .reply-quote { background: rgba(0,0,0,0.2); }

.bubble .media-wrap { margin: -2px -6px 4px; border-radius: 10px; overflow: hidden; cursor: zoom-in; max-width: 280px; min-width: 160px; min-height: 90px; }
.bubble .media-wrap img, .bubble .media-wrap video { display: block; width: 100%; max-height: 320px; object-fit: cover; }
.bubble .media-wrap.loading, .bubble .media-wrap.media-error {
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.18);
  cursor: default;
  min-height: 110px;
}
.media-spinner { font-size: 12px; color: var(--text-faint); text-align: center; padding: 10px; }
.media-error .media-spinner { color: var(--danger); }

.msg-meta {
  display: flex; align-items: center; gap: 5px;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-faint);
  padding: 0 3px;
}
.fuse-ring {
  width: 13px; height: 13px; border-radius: 50%;
  background: conic-gradient(var(--ember) calc(var(--fuse, 1) * 360deg), rgba(255,255,255,0.1) 0deg);
  flex-shrink: 0;
  position: relative;
}
.fuse-ring::after {
  content: ""; position: absolute; inset: 2px; border-radius: 50%; background: var(--bg-deep);
}

.receipt {
  font-size: 11px;
  letter-spacing: -1px;
  color: var(--text-faint);
}
.receipt.seen { color: var(--glow); }

.msg-row:hover .msg-actions { opacity: 1; }
.msg-actions { opacity: 0; transition: opacity 0.15s ease; display: flex; gap: 2px; align-self: center; margin: 0 6px; }
.msg-actions button {
  background: none; border: none; color: var(--text-faint); font-size: 13px; padding: 4px;
}
.msg-actions button:hover { color: var(--glow); }

.day-divider {
  text-align: center; color: var(--text-faint); font-size: 11px; margin: 14px 0 10px;
  text-transform: uppercase; letter-spacing: 0.5px;
}

.typing-indicator { font-size: 12px; color: var(--text-faint); padding: 0 26px 6px; font-style: italic; min-height: 18px; }

/* reply preview above composer */
.reply-preview {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 22px;
  background: var(--bg-panel);
  border-top: 1px solid var(--line);
}
.reply-preview-bar { width: 3px; align-self: stretch; background: var(--glow); border-radius: 2px; }
.reply-preview-body { flex: 1; min-width: 0; }
.reply-preview-label { font-size: 11px; color: var(--glow); font-weight: 600; }
.reply-preview-text { font-size: 12.5px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* composer */
.composer {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 14px 22px 18px;
  background: var(--bg-panel);
  border-top: 1px solid var(--line-soft);
}
.composer textarea {
  flex: 1;
  resize: none;
  background: var(--bg-deep);
  border: 1px solid var(--line);
  color: var(--text-primary);
  border-radius: var(--radius-md);
  padding: 11px 14px;
  font-size: 14px;
  outline: none;
  max-height: 120px;
  line-height: 1.4;
}
.composer textarea:focus { border-color: var(--glow); }

.btn-send {
  background: linear-gradient(135deg, var(--ember), #ff8c6a);
  border: none; color: #1a0d10;
  width: 40px; height: 40px; border-radius: 50%;
  font-size: 15px;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.15s ease;
  flex-shrink: 0;
}
.btn-send:hover { transform: scale(1.06); }
.btn-send:disabled { opacity: 0.4; cursor: default; transform: none; }

.upload-preview {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-elevated); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 6px 10px; font-size: 12px; color: var(--text-muted);
  max-width: 160px;
}
.upload-preview span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* lightbox */
.lightbox {
  position: fixed; inset: 0; background: rgba(5, 4, 8, 0.92);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.lightbox #lightbox-close { position: absolute; top: 20px; right: 24px; }
#lightbox-content img, #lightbox-content video { max-width: 88vw; max-height: 88vh; border-radius: 10px; }

/* toast */
.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elevated); border: 1px solid var(--line);
  padding: 11px 18px; border-radius: 999px; font-size: 13px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
  z-index: 200;
}

/* scrollbars */
.messages::-webkit-scrollbar, .contact-list::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb, .contact-list::-webkit-scrollbar-thumb {
  background: var(--bg-elevated); border-radius: 4px;
}

/* responsive */
@media (max-width: 760px) {
  #app-screen { position: relative; }
  .sidebar {
    position: absolute; inset: 0; z-index: 5; width: 100%; min-width: 0;
    transition: transform 0.25s ease;
  }
  .sidebar.collapsed { transform: translateX(-100%); }
  .chat-panel { width: 100%; }
  .msg-group { max-width: 80%; }
  #back-btn { display: inline-flex !important; }
}
