:root {
  --bg: #07130d;
  --bg-streak: #0c1d14;
  --card-top: #122318;
  --card-bottom: #0c1610;
  --card: #0f1c14;
  --border: #21362a;
  --text: #e8f2ec;
  --muted: #8fa89a;
  --accent: #34d058;
  --accent-hover: #2bb84c;
  --accent-grad: linear-gradient(135deg, #9be15d 0%, #2bbf52 100%);
  --on-accent: #052310; /* dark text on bright green */
  --error: #f8716b;
  --ok: #46d97f;
  --ring: #35d15a;
  --radius: 16px;
  --streak: rgba(255, 255, 255, 0.012);
  --card-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04), 0 10px 34px rgba(0, 0, 0, 0.4);
}

/* Light theme — soft green. Toggled via data-theme on <html>. */
:root[data-theme="light"] {
  --bg: #e9f3ea;
  --bg-streak: #dcecde;
  --card-top: #ffffff;
  --card-bottom: #f3f8f3;
  --card: #ffffff;
  --border: #cde1d1;
  --text: #122a1c;
  --muted: #5d7568;
  --accent: #2aa84c;
  --accent-hover: #23933f;
  --accent-grad: linear-gradient(135deg, #4cc566 0%, #1f9a45 100%);
  --on-accent: #ffffff;
  --error: #d6453b;
  --ok: #1f9a45;
  --ring: #2aa84c;
  --streak: rgba(0, 70, 25, 0.035);
  --card-shadow: 0 1px 2px rgba(16, 40, 26, 0.06), 0 12px 30px rgba(16, 60, 30, 0.09);
}

* { box-sizing: border-box; }

/* Ensure the `hidden` attribute wins even over elements with an explicit display. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
  /* Subtle vertical streaks + a soft glow from the top, over a deep-green base. */
  background-color: var(--bg);
  background-image:
    repeating-linear-gradient(90deg, var(--streak) 0 2px, transparent 2px 64px),
    radial-gradient(120% 80% at 50% -10%, var(--bg-streak), var(--bg) 70%);
  background-attachment: fixed;
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.container {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.25rem;
}

.card {
  width: 100%;
  max-width: 380px;
  background: linear-gradient(160deg, var(--card-top), var(--card-bottom));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.75rem;
  box-shadow: var(--card-shadow);
}

.brand {
  margin: 0 0 0.25rem;
  font-size: 1.5rem;
  letter-spacing: -0.01em;
}

.muted { color: var(--muted); font-size: 0.9rem; line-height: 1.45; }

.tabs {
  display: flex;
  gap: 0.25rem;
  margin: 1.25rem 0 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.25rem;
}
.tab {
  flex: 1;
  padding: 0.5rem;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  cursor: pointer;
}
.tab.is-active {
  background: var(--card);
  color: var(--accent);
  box-shadow: inset 0 0 0 1px rgba(53, 209, 90, 0.4);
}

form { display: grid; gap: 0.9rem; margin-top: 0.5rem; }

label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}

input {
  width: 100%;
  padding: 0.65rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 0; }

/* Radios/checkboxes must not inherit the full-width text-input styling above. */
input[type='radio'],
input[type='checkbox'] {
  width: auto;
  min-width: 0;
  padding: 0;
  margin: 0;
  flex: 0 0 auto;
  accent-color: var(--accent);
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  padding: 0.7rem 1rem;
  border: 1px solid transparent;
}
button.primary {
  background: var(--accent-grad);
  color: var(--on-accent);
  font-weight: 600;
  box-shadow: 0 4px 18px rgba(43, 191, 82, 0.35);
}
button.primary:hover { filter: brightness(1.06); }
button.primary:disabled { opacity: 0.6; cursor: progress; }
button.secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
  margin-top: 1rem;
}
button.secondary:hover { border-color: var(--accent); }

.error {
  margin: 0;
  color: var(--error);
  font-size: 0.85rem;
}

button.small { padding: 0.4rem 0.7rem; font-size: 0.85rem; }

/* ---- Chat layout (Phase 2) ---------------------------------------------- */
/* Fixed to the viewport so it fills the screen exactly; only the message list
   scrolls internally (avoids a whole-page scrollbar). */
.chat {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: 300px 1fr;
}

.sidebar {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--card);
}
.sidebar-head {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.6rem;
  padding: 0.9rem;
  border-bottom: 1px solid var(--border);
}
.me-row { display: flex; align-items: center; gap: 0.55rem; min-width: 0; }
.me-row > div:not(.avatar) { min-width: 0; }
.head-actions { display: flex; align-items: center; gap: 0.4rem; }
.head-actions #logout-btn { flex: 1; }
.head-actions .icon-btn { width: 36px; height: 36px; font-size: 1rem; flex: 0 0 auto; }
.me { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Avatars (green ring, mockup style) */
.avatar {
  width: 40px;
  height: 40px;
  flex: 0 0 auto;
  border-radius: 50%;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: var(--bg);
  border: 2px solid var(--ring);
  color: var(--text);
  font-weight: 600;
  font-size: 1rem;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.room { border-color: var(--border); }
.status { font-size: 0.78rem; color: var(--muted); }
.status.ok { color: var(--ok); }

#new-chat {
  display: flex;
  gap: 0.4rem;
  margin: 0;
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
}
#new-chat input { padding: 0.5rem; }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.conv:hover { background: var(--bg); }
.conv.is-active {
  background: linear-gradient(90deg, rgba(53, 209, 90, 0.1), transparent);
  border-left: 3px solid var(--accent);
}
.conv-text { display: grid; gap: 0.1rem; min-width: 0; flex: 1; }
.conv-name { font-weight: 600; font-size: 0.95rem; }
.conv-preview {
  color: var(--muted);
  font-size: 0.8rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread { display: flex; flex-direction: column; min-height: 0; position: relative; }
.thread-head {
  padding: 0.7rem 1.1rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  min-height: 3.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.thread-head .icon-btn { width: 38px; height: 38px; font-size: 1rem; }

.new-room { margin: 0 0.75rem 0.5rem; width: calc(100% - 1.5rem); }

/* User-search results dropdown (new chat) */
.user-results { list-style: none; margin: 0; padding: 0; max-height: 260px; overflow-y: auto; }
.user-results:not(:empty) { border-bottom: 1px solid var(--border); }
.user-result {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.9rem;
  cursor: pointer;
}
.user-result:hover { background: var(--bg); }
.user-result .avatar { width: 32px; height: 32px; font-size: 0.85rem; border-width: 2px; }
.user-result-text { display: grid; min-width: 0; }
.ur-name { font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ur-sub { color: var(--muted); font-size: 0.75rem; }
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.empty {
  position: absolute;
  inset: 0;
  margin: 0;
  display: grid;
  place-items: center;
  text-align: center;
  pointer-events: none;
}

.msg { max-width: 72%; padding: 0.5rem 0.7rem; border-radius: 12px; position: relative; }
.msg-del {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 20px;
  height: 20px;
  padding: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.62rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0.35;
}
.msg:hover .msg-del { opacity: 1; }
.msg-del:hover { color: var(--error); border-color: var(--error); }
.msg.recalled .msg-body { font-style: italic; opacity: 0.7; }
.msg-react {
  position: absolute;
  top: -7px;
  right: 18px;
  width: 20px;
  height: 20px;
  padding: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--card);
  border: 1px solid var(--border);
  font-size: 0.62rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0.35;
}
.msg:hover .msg-react { opacity: 1; }
.msg.mine .msg-react { right: auto; left: -7px; }

.reactions { display: flex; flex-wrap: wrap; gap: 0.2rem; margin-top: 0.3rem; }
.reaction-chip {
  font-size: 0.75rem;
  padding: 0.1rem 0.4rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  line-height: 1.4;
}
.reaction-chip.mine { border-color: var(--accent); background: rgba(53, 209, 90, 0.15); }
.msg.mine .reaction-chip { color: var(--text); }

.quick-react {
  position: fixed;
  z-index: 90;
  display: flex;
  gap: 0.1rem;
  padding: 0.25rem;
  background: linear-gradient(160deg, var(--card-top), var(--card-bottom));
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--card-shadow);
}
.quick-react button { background: none; border: 0; cursor: pointer; font-size: 1.25rem; padding: 0.15rem 0.3rem; border-radius: 8px; }
.quick-react button:hover { background: var(--bg); }

.seen-label { font-size: 0.62rem; color: var(--muted); text-align: right; margin-top: 0.15rem; }
.msg.mine .seen-label { color: rgba(5, 35, 16, 0.6); }
.msg.ephemeral .msg-meta::after { content: ' ⏳'; }

.typing-row {
  padding: 0.25rem 1rem 0;
  font-size: 0.78rem;
  color: var(--muted);
  font-style: italic;
  min-height: 1rem;
}

.check-row { display: flex; align-items: center; gap: 0.5rem; font-size: 0.9rem; color: var(--text); }
.disappear-row { display: grid; gap: 0.35rem; font-size: 0.85rem; color: var(--muted); }
.disappear-row select { width: 100%; padding: 0.5rem; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; color: var(--text); }
.msg-body { white-space: pre-wrap; word-wrap: break-word; line-height: 1.35; }
.msg-meta { font-size: 0.68rem; color: var(--muted); margin-top: 0.2rem; text-align: right; }
.msg.mine { align-self: flex-end; background: var(--accent-grad); color: var(--on-accent); }
.msg.mine .msg-meta { color: rgba(5, 35, 16, 0.6); }
.msg.theirs { align-self: flex-start; background: var(--card); border: 1px solid var(--border); }
.msg.pending { opacity: 0.6; }
.msg.failed { outline: 1px solid var(--error); }

.composer {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
  padding: 0.75rem;
  border-top: 1px solid var(--border);
}
.composer #composer-input { flex: 1; }

.icon-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 1.1rem;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  padding: 0;
}
.icon-btn:hover { border-color: var(--accent); }

/* ---- Encrypted media (Phase 4) ------------------------------------------ */
.media-spoiler {
  width: 220px;
  max-width: 60vw;
  min-height: 130px;
  display: grid;
  place-content: center;
  gap: 0.25rem;
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: 12px;
  background:
    repeating-linear-gradient(45deg, rgba(53, 209, 90, 0.06) 0 10px, transparent 10px 20px),
    var(--bg);
  color: var(--text);
  cursor: pointer;
  backdrop-filter: blur(6px);
}
.media-spoiler .lock { font-size: 1.6rem; }
.media-spoiler .label { font-weight: 600; }
.media-spoiler .hint { font-size: 0.75rem; color: var(--muted); }
.media-spoiler.busy { opacity: 0.6; cursor: progress; }
.msg.mine .media-spoiler { color: var(--text); }

/* Overlays: passcode prompt, lightbox, capture */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.72);
}
.pass-modal { max-width: 320px; display: grid; gap: 1rem; }
.pass-modal h2 { margin: 0; font-size: 1.1rem; }
.pass-modal .row,
.capture-box .row { display: flex; gap: 0.5rem; justify-content: flex-end; }
.pass-modal .row button,
.capture-box .row button { flex: 1; }

.lightbox { background: rgba(0, 0, 0, 0.9); }
.lightbox-media {
  max-width: 92vw;
  max-height: 86vh;
  border-radius: 10px;
  box-shadow: 0 12px 50px rgba(0, 0, 0, 0.6);
}
.lightbox-close {
  position: fixed;
  top: 16px;
  right: 18px;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 1.1rem;
}

.capture-box {
  width: min(520px, 94vw);
  background: linear-gradient(160deg, var(--card-top), var(--card-bottom));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: grid;
  gap: 0.75rem;
}
.capture-preview {
  width: 100%;
  border-radius: 12px;
  background: #000;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}
.capture-status { text-align: center; color: var(--muted); font-size: 0.85rem; }

/* ---- Room modals (Phase 5) ---------------------------------------------- */
.room-modal { max-width: 360px; display: grid; gap: 1rem; }
.room-modal h2 { margin: 0; font-size: 1.15rem; }
.room-modal .row { display: flex; gap: 0.5rem; justify-content: flex-end; }
.room-modal .row button { flex: 1; }
.member-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; max-height: 40vh; overflow-y: auto; }
.member-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.65rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.link-btn {
  background: none;
  border: 0;
  color: var(--error);
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  font-size: 0.85rem;
}

/* ---- Settings modal ----------------------------------------------------- */
.settings-modal { max-width: 380px; display: grid; gap: 0.9rem; }
.settings-modal h2 { margin: 0; font-size: 1.15rem; }
.settings-modal .row { display: flex; gap: 0.5rem; justify-content: flex-end; }
.settings-modal .row button { flex: 1; }
.avatar-row { display: flex; align-items: center; gap: 0.9rem; }
.avatar-preview {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 2px solid var(--ring);
  overflow: hidden;
  display: grid;
  place-items: center;
  background: var(--bg);
  font-size: 1.5rem;
  font-weight: 600;
}
.avatar-preview img { width: 100%; height: 100%; object-fit: cover; }
.avatar-actions { display: flex; flex-direction: column; gap: 0.35rem; align-items: flex-start; }
.unlock-policy {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem 0.75rem;
  display: grid;
  gap: 0.5rem;
}
.unlock-policy legend { padding: 0 0.4rem; color: var(--muted); font-size: 0.8rem; }
.unlock-policy .radio {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.9rem;
  color: var(--text);
}
.unlock-policy .mins { width: 64px; padding: 0.3rem 0.4rem; }
.unlock-policy input[type='radio'] { accent-color: var(--accent); }
.notif-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}
.notif-status { font-size: 0.8rem; }

/* ---- Emoji picker ------------------------------------------------------- */
.emoji-panel {
  position: fixed;
  z-index: 80;
  width: 300px;
  max-width: 92vw;
  background: linear-gradient(160deg, var(--card-top), var(--card-bottom));
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--card-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.emoji-tabs {
  display: flex;
  gap: 0.1rem;
  padding: 0.4rem;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.emoji-tab {
  background: none;
  border: 0;
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.3rem 0.35rem;
  border-radius: 6px;
  flex: 0 0 auto;
}
.emoji-tab.is-active { background: var(--bg); }
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 0.1rem;
  padding: 0.4rem;
  max-height: 240px;
  overflow-y: auto;
}
.emoji-grid button {
  background: none;
  border: 0;
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
  padding: 0.25rem;
  border-radius: 6px;
}
.emoji-grid button:hover { background: var(--bg); }

/* ---- GIF picker --------------------------------------------------------- */
.gif-btn { width: auto; padding: 0 0.55rem; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.03em; }
.gif-panel {
  position: fixed;
  z-index: 80;
  width: 320px;
  max-width: 92vw;
  background: linear-gradient(160deg, var(--card-top), var(--card-bottom));
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--card-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.gif-search { padding: 0.5rem; border-bottom: 1px solid var(--border); }
.gif-search input { width: 100%; }
.gif-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.3rem;
  padding: 0.5rem;
  max-height: 320px;
  overflow-y: auto;
}
.gif-grid button {
  padding: 0;
  border: 0;
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg);
  cursor: pointer;
}
.gif-grid img { width: 100%; display: block; }
.gif-status { padding: 0.6rem; text-align: center; color: var(--muted); font-size: 0.85rem; }
.gif-attribution { padding: 0.3rem; text-align: center; color: var(--muted); font-size: 0.62rem; border-top: 1px solid var(--border); }

@media (max-width: 640px) {
  .chat { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; }
}

/* ---- Theme toggle ------------------------------------------------------- */
.theme-toggle {
  position: fixed;
  top: 10px;
  right: 14px;
  z-index: 50;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}
.theme-toggle:hover { border-color: var(--accent); }

/* On narrow screens the chat header occupies the top-right; move the toggle above
   the composer so it never overlaps the Log out button. */
@media (max-width: 640px) {
  .theme-toggle { top: auto; bottom: 84px; right: 14px; }
}
