:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273449;
  --line: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --brand: #2563eb;
  --brand-2: #3b82f6;
  --me: #2563eb;
  --them: #334155;
  --ok: #22c55e;
  --warn: #f59e0b;
  --bad: #ef4444;
  --radius: 16px;
  --maxw: 560px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background:
    radial-gradient(1200px 600px at 50% -10%, #1d2a4a 0%, transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
}

body {
  display: flex;
  justify-content: center;
}

.screen {
  width: 100%;
  max-width: var(--maxw);
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(env(safe-area-inset-bottom) + 16px);
  gap: 16px;
}

/* Only the active screen is shown */
.screen[hidden] { display: none; }

/* ---------- Home ---------- */
.hero { text-align: center; margin-top: 8vh; }
.logo { width: 84px; height: 84px; border-radius: 22px; }
.hero h1 { margin: 12px 0 4px; font-size: 30px; letter-spacing: -0.5px; }
.tagline { color: var(--muted); margin: 0; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: 13px; color: var(--muted); padding-left: 4px; }

input[type="text"], .codebox {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 12px;
  padding: 13px 14px;
  font-size: 16px;
  outline: none;
  font-family: inherit;
}
input[type="text"]:focus, .codebox:focus { border-color: var(--brand-2); }

.home-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }

.btn {
  border: none;
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: filter .15s, opacity .15s;
}
.btn:active { filter: brightness(.9); }
.btn[disabled] { opacity: .5; pointer-events: none; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
.btn.small { padding: 10px 14px; font-size: 14px; }

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

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: -4px 0 4px;
}
.topbar h2 { font-size: 18px; margin: 0; }
.icon-btn {
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--text);
  width: 42px; height: 42px;
  border-radius: 12px;
  font-size: 20px;
  cursor: pointer;
  display: grid; place-items: center;
  flex: none;
}
.icon-btn:active { filter: brightness(.9); }

/* ---------- Steps / connect ---------- */
.steps { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 20px; }
.steps > li {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  line-height: 1.5;
}
.steps b { color: var(--text); }

.qr-wrap {
  background: #fff;
  border-radius: 12px;
  width: 220px; height: 220px;
  margin: 14px auto;
  display: grid; place-items: center;
  color: #475569; font-size: 13px; gap: 8px;
  overflow: hidden;
}
.qr-wrap img { width: 100%; height: 100%; image-rendering: pixelated; }

.code-row { display: flex; gap: 8px; align-items: flex-start; margin-top: 8px; }
.codebox {
  flex: 1;
  resize: none;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px;
  word-break: break-all;
}
.btn-row { display: flex; gap: 8px; margin-top: 10px; }
.btn-row .btn { flex: 1; }

/* ---------- Spinner ---------- */
.spinner {
  width: 28px; height: 28px;
  border: 3px solid #cbd5e1;
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Chat ---------- */
.chat { padding-bottom: calc(env(safe-area-inset-bottom) + 8px); }
.chat-bar { position: sticky; top: 0; }
.peer { display: flex; flex-direction: column; line-height: 1.2; flex: 1; min-width: 0; }
.peer-name { font-weight: 600; }
.conn-state { font-size: 12px; color: var(--muted); }
.conn-state.ok { color: var(--ok); }
.conn-state.bad { color: var(--bad); }

.messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 4px 2px;
}
.msg {
  max-width: 78%;
  padding: 9px 12px;
  border-radius: 16px;
  line-height: 1.4;
  word-wrap: break-word;
  position: relative;
  animation: pop .12s ease-out;
  box-shadow: 0 1px 2px rgba(0,0,0,.25);
}
@keyframes pop { from { transform: scale(.96); opacity: .4; } }
.msg.me { align-self: flex-end; background: linear-gradient(135deg, #2f6bff, #2563eb); color: #fff; border-bottom-right-radius: 5px; }
.msg.them { align-self: flex-start; background: var(--them); color: var(--text); border-bottom-left-radius: 5px; }
.msg .time { display: block; font-size: 10px; opacity: .7; margin-top: 3px; text-align: right; }
.msg a.msglink { text-decoration: underline; word-break: break-all; }
.msg.me a.msglink { color: #e6efff; }
.msg.them a.msglink { color: #7cc0ff; }
.msg .ticks { font-size: 11px; letter-spacing: -2px; margin-left: 2px; opacity: .85; }
.msg.me .ticks { color: rgba(255,255,255,.75); }
.msg.me .ticks.read { color: #5ad1ff; opacity: 1; }
.msg audio { display: block; margin-top: 4px; max-width: 230px; }
.msg-img {
  display: block;
  max-width: 240px;
  max-height: 300px;
  border-radius: 10px;
  cursor: pointer;
  background: rgba(0,0,0,.2);
}
.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: inherit;
  background: rgba(255,255,255,.12);
  padding: 9px 11px;
  border-radius: 10px;
  max-width: 240px;
}
.file-icon { font-size: 26px; flex: none; }
.file-meta { display: flex; flex-direction: column; min-width: 0; }
.file-name { font-weight: 600; word-break: break-word; }
.file-size { font-size: 11px; opacity: .75; }
.msg.system {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 8px;
}
.msg.deleted { font-style: italic; opacity: .65; box-shadow: none; }
.msg .edited { font-size: 10px; opacity: .7; margin-right: 4px; }

/* kebab options button on your own bubbles */
.msg-menu-btn {
  position: absolute;
  top: 50%; left: -28px;
  transform: translateY(-50%);
  width: 24px; height: 24px;
  border: none; border-radius: 50%;
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px; line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition: opacity .12s;
}
.msg.me:hover .msg-menu-btn { opacity: .9; }
@media (hover: none) { .msg.me .msg-menu-btn { opacity: .5; } }

/* popup action menu */
.msg-menu {
  position: fixed;
  z-index: 70;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  min-width: 180px;
  box-shadow: 0 10px 28px rgba(0,0,0,.45);
}
.msg-menu[hidden] { display: none; }
.msg-menu button {
  background: transparent;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 11px 12px;
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
  font-family: inherit;
}
.msg-menu button:hover { background: var(--surface); }
#mmDelete { color: #fca5a5; }

/* edit banner above composer */
.edit-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-left: 3px solid var(--brand);
  border-radius: 10px;
  padding: 8px 12px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--muted);
}
.edit-banner[hidden] { display: none; }
.edit-banner button {
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
}

.composer {
  display: flex;
  gap: 8px;
  align-items: center;
  padding-top: 8px;
}
.composer input { flex: 1; }
.mic.recording { background: var(--bad); border-color: var(--bad); animation: pulse 1s infinite; }
@keyframes pulse { 50% { filter: brightness(1.3); } }
.send { background: linear-gradient(135deg, #3b82f6, #2563eb); border-color: var(--brand); color: #fff; box-shadow: 0 2px 8px rgba(37,99,235,.4); }

.rec-indicator {
  text-align: center;
  color: var(--bad);
  font-size: 13px;
  padding: 6px;
}

/* ---------- Scanner modal ---------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.85);
  display: grid; place-items: center;
  z-index: 50;
  padding: 16px;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  width: 100%; max-width: 420px;
  text-align: center;
  display: flex; flex-direction: column; gap: 12px;
}
#scanVideo {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 12px;
  background: #000;
}
#scanStatus { margin: 0; color: var(--muted); font-size: 14px; }

/* ---------- Emoji picker ---------- */
.emoji.icon-btn { font-size: 20px; }
.emoji-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  margin-top: 8px;
  overflow: hidden;
  animation: emojiUp .16s ease-out;
}
.emoji-panel[hidden] { display: none; }
@keyframes emojiUp { from { opacity: 0; transform: translateY(8px); } }

.emoji-tabs {
  display: flex;
  gap: 2px;
  padding: 6px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
.emoji-tabs button {
  flex: none;
  background: transparent;
  border: none;
  font-size: 20px;
  width: 40px; height: 36px;
  border-radius: 9px;
  cursor: pointer;
  opacity: .65;
  transition: background .12s, opacity .12s;
}
.emoji-tabs button:hover { opacity: 1; }
.emoji-tabs button.active { background: var(--surface-2); opacity: 1; }

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  padding: 8px;
  height: 200px;
  overflow-y: auto;
}
.emoji-grid button {
  background: transparent;
  border: none;
  font-size: 24px;
  line-height: 1;
  aspect-ratio: 1;
  border-radius: 9px;
  cursor: pointer;
  transition: background .1s, transform .1s;
}
.emoji-grid button:hover { background: var(--surface-2); }
.emoji-grid button:active { transform: scale(1.25); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: 28px;
  transform: translateX(-50%);
  background: #000c;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 60;
  max-width: 90vw;
  text-align: center;
}
.toast[hidden] { display: none; }
