:root {
  --ink: #14181c;
  --mist-1: #1b222a;
  --mist-2: #29323c;
  --mist-3: #384250;
  --fog: #93a5ab;
  --vapor: #cfe8e6;
  --verified: #7fd1ae;
  --warn: #f5b942;
  --danger: #e8788a;
  --font-display: 'Source Serif 4', 'Iowan Old Style', Georgia, serif;
  --font-body: 'Public Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
body {
  margin: 0;
  font-family: var(--font-body);
  background: var(--ink);
  color: var(--vapor);
  height: 100vh;
  height: 100dvh;
}
a { color: var(--vapor); }
.screen { height: 100%; }
.hidden { display: none !important; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
.muted { color: var(--fog); font-size: 13px; line-height: 1.6; }

/* ---------- auth screen ---------- */

#auth-screen {
  display: flex; align-items: center; justify-content: center;
  padding: 20px; padding-bottom: max(20px, env(safe-area-inset-bottom));
}
.card {
  background: var(--mist-1);
  border: 1px solid var(--mist-3);
  border-radius: 16px;
  padding: 32px;
  width: 100%;
  max-width: 400px;
}
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.brand .mark { width: 30px; height: 30px; flex-shrink: 0; }
.brand h1 { font-family: var(--font-display); font-weight: 500; font-size: 28px; color: var(--vapor); margin: 0; }
.tagline { color: var(--fog); font-size: 14px; margin: 0 0 24px; line-height: 1.5; }

.tabs { display: flex; gap: 6px; margin-bottom: 18px; background: var(--mist-2); border-radius: 10px; padding: 4px; }
.tab-btn {
  flex: 1; background: transparent; color: var(--fog); border: none; border-radius: 7px;
  padding: 9px 0; font-size: 14px; font-weight: 600; cursor: pointer; font-family: var(--font-body);
}
.tab-btn.active { background: var(--mist-1); color: var(--vapor); }

.auth-form { display: flex; flex-direction: column; }
label { display: block; font-size: 13px; color: var(--fog); margin: 14px 0 6px; font-family: var(--font-body); }
label:first-of-type { margin-top: 0; }
.visibility-tag {
  font-size: 10px; text-transform: uppercase; letter-spacing: .4px; font-weight: 700;
  color: var(--verified); background: rgba(127,209,174,0.14); padding: 2px 6px; border-radius: 999px;
  font-family: var(--font-mono);
}
.visibility-tag.hidden-tag { color: var(--fog); background: var(--mist-2); }

input, textarea {
  width: 100%;
  background: var(--ink);
  border: 1px solid var(--mist-3);
  border-radius: 8px;
  color: var(--vapor);
  padding: 12px;
  font-size: 16px; /* keep >=16px: prevents iOS Safari auto-zoom on focus */
  font-family: var(--font-body);
  min-height: 44px;
}
textarea { min-height: 70px; resize: vertical; }
button {
  background: var(--vapor);
  color: var(--ink);
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  font-family: var(--font-body);
}
button:hover { opacity: 0.9; }
button:focus-visible { outline: 2px solid var(--verified); outline-offset: 2px; }
#login-submit, #signup-submit { width: 100%; margin-top: 20px; }
.form-error { color: var(--danger); font-size: 13px; margin: 10px 0 0; line-height: 1.4; }
.form-success { color: var(--verified); font-size: 13px; margin: 10px 0 0; line-height: 1.4; }
.link-btn {
  background: transparent; color: var(--fog); font-size: 13px; font-weight: 500;
  padding: 4px 0; min-height: 0; text-align: left; align-self: flex-start;
  text-decoration: underline;
}
.link-btn:hover { color: var(--vapor); }
.fineprint { color: var(--fog); font-size: 11px; margin-top: 16px; line-height: 1.5; }

.ghost-btn { background: transparent; border: 1px solid var(--mist-3); color: var(--fog); }

/* ---------- main app ---------- */

#main-screen { display: flex; flex-direction: column; height: 100%; }
header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--mist-3); background: var(--mist-1);
  flex-shrink: 0;
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me > div { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.me strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--vapor); }
.header-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.avatar { border-radius: 999px; object-fit: cover; background: var(--mist-2); flex-shrink: 0; }
.avatar-sm { width: 28px; height: 28px; }
.avatar-md { width: 40px; height: 40px; }
.avatar-upload { position: relative; cursor: pointer; display: block; }
.avatar-upload::after {
  content: '✎'; position: absolute; bottom: -2px; right: -2px;
  background: var(--vapor); color: var(--ink); font-size: 9px; width: 16px; height: 16px;
  border-radius: 999px; display: flex; align-items: center; justify-content: center;
}

.pill {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px;
  padding: 3px 9px; border-radius: 999px; background: var(--mist-2); color: var(--fog);
  width: fit-content; font-family: var(--font-mono); font-weight: 500;
}
.pill.offline { color: var(--fog); }
.pill.online { color: var(--verified); background: rgba(127,209,174,0.14); }
.pill.connecting { color: var(--warn); background: rgba(245,185,66,0.14); }
.pill.connected { color: var(--verified); background: rgba(127,209,174,0.14); }

.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: rgba(245,185,66,0.12); color: var(--warn);
  font-size: 13px; flex-shrink: 0; border-bottom: 1px solid var(--mist-3);
}
.banner .link-btn { color: var(--warn); }
.banner .link-btn:hover { color: var(--vapor); }

.layout { display: flex; flex: 1; min-height: 0; }
#peer-list-panel {
  width: 280px; border-right: 1px solid var(--mist-3); padding: 16px; overflow-y: auto; flex-shrink: 0;
}
#peer-list-panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--fog); margin: 0 0 12px; font-family: var(--font-mono); font-weight: 500; }
#peer-list { list-style: none; padding: 0; margin: 0; }
#peer-list li {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 10px; cursor: pointer; margin-bottom: 4px; font-size: 15px;
  min-height: 48px;
}
#peer-list li:hover, #peer-list li.active { background: var(--mist-2); }

#chat-panel { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat-placeholder { margin: auto; padding: 20px; text-align: center; }
#chat-active { display: flex; flex-direction: column; height: 100%; }
.chat-header {
  padding: 10px 16px; border-bottom: 1px solid var(--mist-3);
  display: flex; align-items: center; gap: 10px; flex-shrink: 0;
}
.chat-header strong { color: var(--vapor); }
.back-btn {
  display: none; background: transparent; color: var(--vapor); padding: 6px 10px; font-size: 18px;
  min-height: 40px; min-width: 40px;
}
#chat-log { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 75%; padding: 9px 13px; border-radius: 14px; font-size: 15px; line-height: 1.4; word-break: break-word; }
.msg.mine { align-self: flex-end; background: var(--vapor); color: var(--ink); border-bottom-right-radius: 4px; }
.msg.theirs { align-self: flex-start; background: var(--mist-2); color: var(--vapor); border-bottom-left-radius: 4px; }
.msg.unsent { align-self: flex-end; background: transparent; border: 1px dashed var(--mist-3); color: var(--fog); border-bottom-right-radius: 4px; }
.msg .badge { display: block; font-size: 10px; text-transform: uppercase; letter-spacing: .3px; margin-top: 3px; opacity: 0.85; font-family: var(--font-mono); }
.chat-input-row {
  display: flex; gap: 10px; padding: 12px 16px; flex-shrink: 0;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--mist-3);
}
.chat-input-row input { flex: 1; }

/* ---------- mobile: single-column, swap sidebar/chat ---------- */

@media (max-width: 720px) {
  header { padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); }
  .header-actions button { padding: 8px 12px; font-size: 13px; }

  #peer-list-panel { width: 100%; border-right: none; }
  #chat-panel { display: none; }
  body.chat-open #peer-list-panel { display: none; }
  body.chat-open #chat-panel { display: flex; }

  .back-btn { display: inline-flex; align-items: center; justify-content: center; }

  .msg { max-width: 85%; }
}
