:root {
  --bg: #0a0e14;
  --bg-elevated: #11151d;
  --bg-input: #161b25;
  --border: #232a37;
  --accent: #34d399;
  --accent-dim: #1f7a5c;
  --text: #e8edf2;
  --text-dim: #8b96a5;
  --text-faint: #566072;
  --danger: #f87171;
  --bubble-mine: linear-gradient(135deg, #1a7a52, #14603f);
  --bubble-theirs: #1c222c;
  --radius: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

body {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- Login screen ---------- */
#login-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(ellipse at top, #0f1720 0%, #0a0e14 60%);
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 28px;
  box-shadow: 0 20px 60px -20px rgba(0,0,0,0.6);
}

.login-icon {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 18px;
  font-size: 24px;
}

.login-card h1 {
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  margin: 0 0 4px;
  letter-spacing: -0.02em;
}

.login-sub {
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  margin: 0 0 24px;
}

.field {
  margin-bottom: 12px;
}

.field label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 6px;
  font-weight: 500;
}

#login-screen input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}

#login-screen input:focus {
  border-color: var(--accent-dim);
}

#login-btn {
  width: 100%;
  margin-top: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  color: #062419;
  border: none;
  padding: 13px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
}

#login-btn:active { transform: scale(0.98); }
#login-btn:hover { opacity: 0.92; }

#login-error {
  color: var(--danger);
  font-size: 12.5px;
  min-height: 16px;
  margin-top: 10px;
  text-align: center;
}

.hint {
  color: var(--text-faint);
  font-size: 11.5px;
  text-align: center;
  margin-top: 18px;
  line-height: 1.5;
}

/* ---------- Chat screen ---------- */
#chat-screen {
  display: none;
  flex: 1;
  flex-direction: column;
  height: 100%;
  background: var(--bg);
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
  flex-shrink: 0;
  transition: background 0.2s, box-shadow 0.2s;
}

.status-dot.offline {
  background: #e0a63f;
  box-shadow: 0 0 8px #e0a63f;
}

#status {
  font-size: 12.5px;
  color: var(--text-dim);
  font-weight: 500;
}

.topbar-actions {
  display: flex;
  gap: 6px;
}

.topbar-actions button {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11.5px;
  font-weight: 500;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.topbar-actions button:hover {
  background: #1c222c;
  color: var(--text);
}

#forget-btn:hover {
  color: var(--danger);
  border-color: #4a2828;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

#messages::-webkit-scrollbar { width: 6px; }
#messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 76%;
  margin-bottom: 8px;
  animation: fadeIn 0.2s ease-out;
}

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

.msg-row.mine { align-self: flex-end; align-items: flex-end; }
.msg-row.theirs { align-self: flex-start; align-items: flex-start; }

.msg {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.4;
  word-break: break-word;
  white-space: pre-wrap;
}

.msg-row.mine .msg {
  background: var(--bubble-mine);
  color: #eafff2;
  border-bottom-right-radius: 4px;
}

.msg-row.theirs .msg {
  background: var(--bubble-theirs);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.msg img, .msg video {
  max-width: 260px;
  max-height: 320px;
  border-radius: 12px;
  margin-top: 2px;
  display: block;
  cursor: pointer;
}

.msg video { background: #000; }

.msg.edited-tag::after {
  content: '(edited)';
  font-size: 10.5px;
  margin-left: 6px;
  font-style: italic;
  opacity: 0.75;
}

.msg-row.theirs .msg.edited-tag::after { color: var(--text-faint); }
.msg-row.mine .msg.edited-tag::after { color: rgba(6, 36, 25, 0.65); }

.msg.deleted-msg,
.msg-row.mine .msg.deleted-msg,
.msg-row.theirs .msg.deleted-msg {
  background: var(--bubble-theirs) !important;
  color: var(--text-faint) !important;
  font-style: italic;
}

.msg.placeholder {
  color: var(--text-faint);
  font-style: italic;
  font-size: 13px;
}

.meta {
  font-size: 10.5px;
  color: var(--text-faint);
  margin-top: 4px;
  padding: 0 4px;
}

#composer {
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  align-items: flex-end;
  flex-shrink: 0;
}

.icon-btn {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}

.icon-btn:hover { background: #1c222c; color: var(--text); }

#text-input {
  flex: 1;
  resize: none;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  padding: 11px 14px;
  font-size: 14.5px;
  max-height: 110px;
  outline: none;
  font-family: inherit;
  transition: border-color 0.15s;
}

#text-input:focus { border-color: var(--accent-dim); }

#send-btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  color: #062419;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 16px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s;
}

#send-btn:active { transform: scale(0.92); }

#file-input { display: none; }

/* ---------- Lightbox ---------- */
#lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 10, 0.92);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top, 16px) 16px calc(env(safe-area-inset-bottom, 16px) + 8px);
}

#lightbox.open { display: flex; }

#lightbox-content {
  max-width: 100%;
  max-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#lightbox-content img,
#lightbox-content video {
  max-width: 100%;
  max-height: 92vh;
  border-radius: 8px;
  object-fit: contain;
}

#lightbox-close {
  position: absolute;
  top: calc(env(safe-area-inset-top, 12px) + 10px);
  right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.12);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  z-index: 101;
}

/* ---------- Message context menu (edit/delete) ---------- */
#msg-menu {
  display: none;
  position: fixed;
  z-index: 110;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 10px 30px -10px rgba(0,0,0,0.6);
  flex-direction: column;
  min-width: 140px;
}

#msg-menu.open { display: flex; }

#msg-menu button {
  background: none;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 12px 16px;
  font-size: 14px;
  cursor: pointer;
}

#msg-menu button:hover { background: var(--bg-input); }
#msg-menu-delete { color: var(--danger); }

.msg-row.mine .msg { cursor: pointer; }

/* ---------- Emoji panel ---------- */
#emoji-panel {
  display: none;
  flex-wrap: wrap;
  gap: 2px;
  padding: 10px 12px;
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  max-height: 160px;
  overflow-y: auto;
  flex-shrink: 0;
}

#emoji-panel.open { display: flex; }

#emoji-panel button {
  background: none;
  border: none;
  font-size: 22px;
  padding: 6px;
  cursor: pointer;
  border-radius: 8px;
  line-height: 1;
}

#emoji-panel button:hover { background: var(--bg-input); }

#emoji-btn.active { color: var(--accent); }

/* ---------- Edit mode ---------- */
#composer.editing #text-input { border-color: var(--accent); }
#composer.editing #send-btn { background: linear-gradient(135deg, #4dc9ff, #2a86bf); }

#edit-banner {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: var(--bg-input);
  border-top: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--text-dim);
  flex-shrink: 0;
}

#edit-banner.open { display: flex; }
#edit-banner-cancel { background: none; border: none; color: var(--danger); font-size: 12.5px; cursor: pointer; }

/* ---------- Mobile ---------- */
@media (max-width: 480px) {
  .msg-row { max-width: 86%; }
  #composer { padding: 10px 10px calc(10px + env(safe-area-inset-bottom, 0px)); }
  #topbar { padding-top: calc(12px + env(safe-area-inset-top, 0px)); }
  .msg img, .msg video { max-width: 220px; }
  #msg-menu { min-width: 160px; }
  #msg-menu button { padding: 14px 18px; font-size: 15px; }
}
