:root {
  --bg: #0f1419;
  --surface: #1a2129;
  --surface-2: #232c36;
  --border: #2e3a46;
  --text: #e6edf3;
  --text-dim: #8b98a5;
  --accent: #2f81f7;
  --accent-hover: #4593f8;
  --mine: #1f4f8f;
  --theirs: #232c36;
  --danger: #f85149;
  --ok: #3fb950;
  --warn: #d29922;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  overflow: hidden; /* only the message list scrolls, never the page */
}

.screen {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

[hidden] { display: none !important; }

button {
  font: inherit;
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  cursor: pointer;
  color: var(--text);
  background: var(--surface-2);
}

button.primary {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
button.primary:hover:not(:disabled) { background: var(--accent-hover); }
button.primary:disabled { opacity: 0.45; cursor: not-allowed; }

button.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px 12px;
}
button.ghost:hover { color: var(--text); border-color: var(--text-dim); }
button.ghost.danger:hover { color: var(--danger); border-color: var(--danger); }
button.icon { padding: 8px 10px; }

input, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  outline: none;
  min-width: 0;
}
input:focus, select:focus { border-color: var(--accent); }

/* ---------- Join screen ---------- */

#join-screen {
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card h1 {
  margin: 0;
  font-size: 28px;
  text-align: center;
}

.tagline {
  margin: 0 0 8px;
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
}

.card label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.key-row {
  display: flex;
  gap: 8px;
}
.key-row input { flex: 1; }

.key-actions {
  display: flex;
  gap: 8px;
}
.key-actions button { flex: 1; font-size: 14px; }

.hint {
  margin: 4px 0 0;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.5;
}

.error {
  margin: 0;
  color: var(--danger);
  font-size: 14px;
}

/* ---------- Chat screen ---------- */

#chat-screen header {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-dim);
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--warn);
  flex: none;
}
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--danger); }

.fp {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 13px;
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 8px;
  cursor: help;
}

.header-actions {
  margin-left: auto;
  display: flex;
  gap: 6px;
}
.header-actions button { font-size: 13px; padding: 6px 10px; }

.hold.active {
  color: var(--ok);
  border-color: var(--ok);
}

#messages {
  flex: 1;
  min-height: 0; /* allow the list to shrink when the keyboard opens */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  list-style: none;
  margin: 0;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#messages li {
  max-width: 78%;
  padding: 9px 13px;
  border-radius: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  position: relative;
  animation: pop 0.15s ease-out;
}

@keyframes pop {
  from { transform: scale(0.96); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

#messages li.mine {
  align-self: flex-end;
  background: var(--mine);
  border-bottom-right-radius: 4px;
}

#messages li.theirs {
  align-self: flex-start;
  background: var(--theirs);
  border-bottom-left-radius: 4px;
}

#messages li.system {
  align-self: center;
  background: none;
  color: var(--text-dim);
  font-size: 13px;
  font-style: italic;
  max-width: 100%;
  animation: none;
}

.ticks {
  display: inline-block;
  margin-left: 7px;
  font-size: 12px;
  letter-spacing: -0.18em;
  color: var(--text-dim);
  vertical-align: middle;
  user-select: none;
}
.ticks.read { color: #4fc3f7; }

.ttl-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: 8px;
  font-size: 11px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  color: var(--warn);
  vertical-align: middle;
}

#messages li.expiring { animation: fadeout 0.4s ease-in forwards; }
@keyframes fadeout {
  to { opacity: 0; transform: scale(0.9); }
}

/* Inline (encrypted) images / GIFs */
#messages li.has-media { padding: 5px; }
#messages li.has-media.mine { border-bottom-right-radius: 14px; }
#messages li.has-media.theirs { border-bottom-left-radius: 14px; }
.media {
  display: block;
  max-width: min(260px, 64vw);
  max-height: 320px;
  width: auto;
  height: auto;
  border-radius: 10px;
  cursor: pointer;
}
.media + .caption { display: block; padding: 6px 8px 2px; }

/* Emoji picker */
.emoji-panel {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 74px;
  max-width: 420px;
  margin: 0 auto;
  max-height: 240px;
  overflow-y: auto;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 10px;
  box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.4);
  z-index: 10;
}
.emoji-cat {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin: 8px 2px 4px;
}
.emoji-cat:first-child { margin-top: 0; }
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(34px, 1fr));
  gap: 2px;
}
.emoji-grid button {
  background: none;
  border: none;
  border-radius: 6px;
  padding: 4px 0;
  font-size: 22px;
  line-height: 1.2;
  cursor: pointer;
}
.emoji-grid button:hover { background: var(--surface); }

/* Emoji screen effect */
.fx-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 9999;
}
.fx-emoji {
  position: absolute;
  bottom: 0;
  will-change: transform, opacity;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.35));
}
.fx-glow {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 75%, rgba(255, 209, 102, 0.28), transparent 62%);
  opacity: 0;
  animation: fxglow 1.3s ease-out forwards;
}
@keyframes fxglow {
  0% { opacity: 0; }
  18% { opacity: 1; }
  100% { opacity: 0; }
}

/* GIF search picker */
.gif-panel {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 74px;
  max-width: 420px;
  margin: 0 auto;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.4);
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#gif-search { width: 100%; }
.gif-results {
  column-count: 3;
  column-gap: 4px;
  max-height: 240px;
  overflow-y: auto;
}
.gif-results img {
  width: 100%;
  display: block;
  margin-bottom: 4px;
  border-radius: 6px;
  cursor: pointer;
  break-inside: avoid;
  background: var(--surface);
  transition: outline 0.1s;
}
.gif-results img:hover { outline: 2px solid var(--accent); }
.gif-status {
  font-size: 13px;
  color: var(--text-dim);
  text-align: center;
  padding: 8px 0;
}
.gif-status.error { color: var(--danger); }
.gif-attribution {
  font-size: 10.5px;
  color: var(--text-dim);
  text-align: center;
  line-height: 1.4;
}
.icon.gif {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

#chat-screen footer {
  position: relative;
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}

#chat-screen footer select { flex: none; }
#chat-screen footer input#msg-input { flex: 1; }
#chat-screen footer .icon { flex: none; font-size: 18px; padding: 8px 10px; }
#chat-screen footer .icon:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- Small screens ---------- */

@media (max-width: 560px) {
  .card { padding: 24px 18px; }
  #chat-screen header { gap: 8px; padding: 8px 10px; }
  .fp { order: 3; }
  .header-actions { margin-left: 0; width: 100%; }
  .header-actions button { flex: 1; }
  #messages li { max-width: 88%; }
  #chat-screen footer { padding: 10px; gap: 6px; }
  #chat-screen footer select { max-width: 76px; }
  #chat-screen footer .icon { padding: 8px 6px; }
  .emoji-panel, .gif-panel { bottom: 70px; }
  .gif-results { column-count: 2; }
}
