/* ---------- Themes ----------
   Each theme sets the same variables. Default is "midnight": dark with an orange accent. */

:root,
[data-theme="midnight"] {
  --bg: #0f1115;
  --panel: #171a21;
  --panel2: #1f232c;
  --line: #2a2f3a;
  --text: #e9ecf2;
  --muted: #8a93a6;
  --accent: #ff7a1a;
  --accent-ink: #1a0d00;
  --mine: #ff7a1a;
  --mine-ink: #1a0d00;
  --other: #262b36;
  --other-ink: #e9ecf2;
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --glow: none;
  --grid: none;
}

/* 80s synthwave: magenta and cyan on a deep violet night */
[data-theme="synthwave"] {
  --bg: #130828;
  --panel: #1d0f3a;
  --panel2: #271450;
  --line: #4b2a8a;
  --text: #f5e9ff;
  --muted: #b59bd9;
  --accent: #ff2e9a;
  --accent-ink: #ffffff;
  --mine: #ff2e9a;
  --mine-ink: #ffffff;
  --other: #2b1857;
  --other-ink: #f5e9ff;
  --radius: 12px;
  --font: "Trebuchet MS", "Segoe UI", sans-serif;
  --glow: 0 0 12px rgba(255, 46, 154, 0.5);
  --grid: linear-gradient(transparent 95%, rgba(0, 240, 255, 0.12) 95%),
          linear-gradient(90deg, transparent 95%, rgba(0, 240, 255, 0.12) 95%);
}

/* Tron: black with cyan lines */
[data-theme="tron"] {
  --bg: #03080c;
  --panel: #061218;
  --panel2: #0a1d26;
  --line: #0f4a5c;
  --text: #d8fbff;
  --muted: #6fb8c8;
  --accent: #22e4ff;
  --accent-ink: #00131a;
  --mine: #22e4ff;
  --mine-ink: #00131a;
  --other: #0a1d26;
  --other-ink: #d8fbff;
  --radius: 4px;
  --font: "Arial Narrow", Arial, sans-serif;
  --glow: 0 0 10px rgba(34, 228, 255, 0.5);
  --grid: none;
}

/* Miami Vice: pastel pink and teal on a dusky purple */
[data-theme="miami"] {
  --bg: #1b1036;
  --panel: #241546;
  --panel2: #2e1d57;
  --line: #4a3380;
  --text: #fff1fa;
  --muted: #c8a8e0;
  --accent: #ff8fc8;
  --accent-ink: #2b0a22;
  --mine: #5fe0d0;
  --mine-ink: #06302b;
  --other: #3a2766;
  --other-ink: #fff1fa;
  --radius: 18px;
  --font: "Trebuchet MS", system-ui, sans-serif;
  --glow: none;
  --grid: none;
}

/* Retro terminal: amber on black. Scanlines are optional (Settings). */
[data-theme="terminal"] {
  --bg: #050a05;
  --panel: #0a120a;
  --panel2: #0f1c0f;
  --line: #1d4d1d;
  --text: #b8ffb0;
  --muted: #5fa85a;
  --accent: #ffb000;
  --accent-ink: #1a1100;
  --mine: #ffb000;
  --mine-ink: #1a1100;
  --other: #0f1c0f;
  --other-ink: #b8ffb0;
  --radius: 0;
  --font: "Courier New", ui-monospace, monospace;
  --glow: 0 0 6px rgba(255, 176, 0, 0.45);
  --grid: none;
}

/* ---------- Base ---------- */

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg) var(--grid);
  background-size: 40px 40px;
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: #ff6b6b; min-height: 1.2em; margin: 0; font-size: 14px; }

button, input, textarea, select {
  font: inherit;
  color: inherit;
}
button { cursor: pointer; }
input, textarea, select {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 11px;
  width: 100%;
}
input:focus, textarea:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
}

.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: var(--radius);
  padding: 10px 16px;
  font-weight: 600;
  box-shadow: var(--glow);
}
.primary:disabled { opacity: 0.5; }
.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  padding: 4px 2px;
}
.icon-btn {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  width: 36px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  font-size: 16px;
}

/* ---------- Auth ---------- */

.auth {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 20px;
}
.auth-card {
  width: min(380px, 100%);
  display: grid;
  gap: 12px;
}
.auth-card h1 { margin: 0; color: var(--accent); text-shadow: var(--glow); }
.auth-card label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.auth-card .link-btn { justify-self: center; }

/* ---------- App layout ---------- */

.app {
  height: 100%;
  display: grid;
  grid-template-columns: 300px 1fr;
}
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.side-head, .chat-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.side-head .brand { flex: 1; font-size: 18px; color: var(--accent); text-shadow: var(--glow); }
.side-actions, .chat-tools { display: flex; gap: 6px; }

.conv-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  overflow-y: auto;
  flex: 1;
}
.conv-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
  padding: 10px;
  border-radius: var(--radius);
  cursor: pointer;
}
.conv-item:hover { background: var(--panel2); }
.conv-item.active { background: var(--panel2); box-shadow: inset 3px 0 0 var(--accent); }
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview { grid-column: 1; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-meta { grid-row: 1 / span 2; grid-column: 2; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.badge {
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 999px;
  min-width: 22px;
  padding: 1px 7px;
  font-size: 12px;
  text-align: center;
  font-weight: 700;
}
.bell-off { font-size: 12px; color: var(--muted); }

.side-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-top: 1px solid var(--line);
  font-size: 14px;
}

.chat {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}
.empty {
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--muted);
}
.chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.chat-title { flex: 1; min-width: 0; }
.chat-title h2 { margin: 0; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title small { font-size: 13px; }
.back-btn { display: none; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.msg-row { display: flex; flex-direction: column; max-width: 74%; }
.msg-row.mine { align-self: flex-end; align-items: flex-end; }
.msg-row.other { align-self: flex-start; align-items: flex-start; }
.msg-row.grouped { margin-top: -2px; }
.msg-author { font-size: 12px; color: var(--muted); margin: 8px 6px 2px; }
.bubble {
  background: var(--other);
  color: var(--other-ink);
  padding: 8px 12px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
  position: relative;
}
.mine .bubble { background: var(--mine); color: var(--mine-ink); }
.bubble.deleted { font-style: italic; opacity: 0.7; }
.bubble .edited { font-size: 11px; opacity: 0.7; margin-left: 6px; }
.quote {
  border-left: 3px solid var(--accent);
  padding: 2px 8px;
  margin-bottom: 6px;
  font-size: 13px;
  opacity: 0.85;
}
.reactions { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.reaction {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 13px;
}
.reaction.mine { border-color: var(--accent); }
.msg-actions { display: flex; gap: 4px; margin-top: 2px; opacity: 0.85; }
.msg-actions button {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 12px;
  padding: 2px 4px;
}
.msg-actions button:hover { color: var(--accent); }
.day-sep {
  align-self: center;
  color: var(--muted);
  font-size: 12px;
  margin: 10px 0;
}

.typing { min-height: 22px; padding: 0 16px; font-size: 13px; }
.reply-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  background: var(--panel2);
  border-top: 1px solid var(--line);
  font-size: 13px;
}
.reply-bar span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.composer {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.composer textarea { resize: none; min-height: 42px; max-height: 140px; line-height: 1.4; }
.send-btn { align-self: flex-end; }

.popped {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 12px;
  color: var(--muted);
  background: var(--bg);
  z-index: 5;
}

/* ---------- Modal and forms ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 10;
}
.modal-box { width: min(480px, 100%); max-height: 90vh; overflow-y: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.modal-head h3 { margin: 0; }
.stack { display: grid; gap: 12px; }
.stack label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { flex: 1; }
.row > .shrink { flex: 0; }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius);
  background: var(--panel2);
}
.pill { font-size: 12px; color: var(--accent); border: 1px solid var(--accent); border-radius: 999px; padding: 0 7px; }
.check { display: flex; gap: 8px; align-items: center; width: auto; }
.check input { width: auto; }
.section-title { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin: 14px 0 6px; }
.theme-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
.theme-swatch {
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 8px;
  background: var(--panel2);
  text-align: left;
}
.theme-swatch.active { border-color: var(--accent); }
.swatch-dots { display: flex; gap: 4px; margin-top: 6px; }
.swatch-dots span { width: 14px; height: 14px; border-radius: 50%; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--accent);
  color: var(--text);
  padding: 10px 16px;
  border-radius: var(--radius);
  z-index: 20;
  box-shadow: var(--glow);
}

/* 80s terminal scanlines (toggle in Settings; only drawn when the class is on) */
.scanlines::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0) 0, rgba(0, 0, 0, 0) 2px, rgba(0, 0, 0, 0.18) 3px);
  z-index: 50;
}

/* Honour reduced motion: no transitions anywhere. */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------- Phone layout ---------- */

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .app.show-chat .sidebar { display: none; }
  .app:not(.show-chat) .chat { display: none; }
  .app:not(.show-chat) .sidebar { display: flex; }
  .back-btn { display: inline-grid; }
  .msg-row { max-width: 88%; }
}
