:root {
  --bg: #0c1220;
  --bg2: #111827;
  --card: #151c2e;
  --text: #eef2ff;
  --muted: #93a4c3;
  --accent: #4f8cff;
  --accent2: #2dd4a8;
  --danger: #f07178;
  --warn: #f0c14b;
  --ok: #2dd4a8;
  --border: #243049;
  --radius: 16px;
  --shadow: 0 10px 36px rgba(0, 0, 0, 0.35);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Noto Sans SC", sans-serif;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--text);
  min-height: 100%;
  -webkit-tap-highlight-color: transparent;
}
body {
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 14px calc(16px + var(--safe-bottom));
}
body.has-session { padding: 0; max-width: none; }
body.has-session #viewHome,
body.has-session .modal { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

.topbar { margin-bottom: 12px; }
.topbar-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 10px;
}
.logo {
  margin: 0; font-size: 1.25rem; font-weight: 750; letter-spacing: 0.01em;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.pill {
  display: inline-flex; align-items: center;
  font-size: 0.75rem; font-weight: 600;
  padding: 5px 10px; border-radius: 999px;
  background: #1a2438; color: var(--muted);
  border: 1px solid var(--border);
  max-width: 42vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pill.ok { color: var(--ok); border-color: rgba(45, 212, 168, 0.35); background: rgba(45, 212, 168, 0.08); }
.pill.err { color: var(--danger); border-color: rgba(240, 113, 120, 0.35); background: rgba(240, 113, 120, 0.08); }

.me-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
}
.me-row { display: flex; gap: 6px; align-items: center; }
.name-input {
  flex: 1; min-width: 0;
  border: 0; background: transparent; color: var(--text);
  font-size: 1.05rem; font-weight: 650; padding: 6px 2px;
  outline: none;
}
.name-input::placeholder { color: var(--muted); font-weight: 500; }
.details-panel {
  margin-top: 8px; padding-top: 8px;
  border-top: 1px solid var(--border);
}
.one-liner {
  margin: 8px 0 10px;
  font-size: 0.8rem; color: var(--muted); line-height: 1.4;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.peers-card { min-height: 180px; }
.section-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 6px;
}
.section-head h2 {
  margin: 0; font-size: 0.95rem; font-weight: 650; color: var(--muted);
}

.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.grow { flex: 1 1 auto; min-width: 0; }
.block { width: 100%; }
label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 6px; }

input[type="text"], textarea {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text);
  font-size: 1rem;
}
textarea {
  font-size: 0.8rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  resize: vertical;
}
input[type="file"] { display: none; }

button, .btn, .file-btn {
  appearance: none; border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 0.95rem; font-weight: 650;
  cursor: pointer;
  background: var(--accent); color: #fff;
  min-height: 44px;
}
button.secondary, .file-btn.secondary { background: #243049; color: var(--text); }
button.danger { background: var(--danger); }
button.ghost {
  background: transparent; color: var(--muted);
  border: 1px solid var(--border); min-height: 36px;
}
button.sm, .ghost.sm {
  padding: 6px 10px; font-size: 0.78rem; min-height: 34px; border-radius: 10px;
}
button:disabled { opacity: 0.45; cursor: not-allowed; }
.file-btn {
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; margin: 0;
}

.device-id {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9rem; color: var(--accent2); word-break: break-all;
}
.meta-line {
  font-size: 0.82rem; color: var(--muted);
  margin-top: 6px; word-break: break-all;
}
.code {
  font-family: ui-monospace, monospace;
  background: var(--bg2);
  padding: 2px 6px; border-radius: 6px; color: var(--text);
}
.muted-id { font-size: 0.75rem; color: var(--muted); margin-left: 6px; }

.sas {
  font-size: 2.6rem; font-weight: 750;
  letter-spacing: 0.28em; text-align: center;
  padding: 18px 8px 10px;
  font-family: ui-monospace, monospace;
  color: var(--warn);
}
.pin-input {
  font-size: 1.8rem !important;
  letter-spacing: 0.35em; text-align: center;
  font-family: ui-monospace, monospace; font-weight: 700;
  padding: 14px !important;
}

.progress-wrap {
  height: 6px; background: var(--bg2); border-radius: 999px;
  overflow: hidden; margin: 6px 0;
}
.progress-bar {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  transition: width 0.15s ease;
}
.status { font-size: 0.82rem; color: var(--muted); min-height: 1.2em; margin: 4px 0 0; }
.status.ok { color: var(--ok); }
.status.err { color: var(--danger); }
.hint {
  font-size: 0.78rem; color: var(--muted);
  line-height: 1.45; margin: 6px 0 0;
}

.peer-list { margin: 0; padding: 0; list-style: none; }
.peer-list li {
  display: flex; justify-content: space-between; align-items: center;
  gap: 10px; padding: 14px 0;
  border-bottom: 1px solid var(--border);
}
.peer-list li:last-child { border-bottom: 0; }
.peer-list .peer-info { min-width: 0; flex: 1; }
.peer-list .peer-info strong {
  display: block; color: var(--text); font-size: 1.05rem; font-weight: 650;
}
.peer-list .peer-sub {
  display: block; margin-top: 3px; font-size: 0.78rem; color: var(--muted);
}
.peer-list .empty {
  display: block; padding: 28px 8px; text-align: center;
  color: var(--muted); font-size: 0.92rem; line-height: 1.5;
  border: 0;
}
.trusted li {
  list-style: none; display: flex; justify-content: space-between;
  align-items: center; gap: 8px; padding: 10px 0;
  border-bottom: 1px solid var(--border); font-size: 0.9rem;
}
.trusted ul { margin: 0; padding: 0; }

.lan-banner {
  border: 1px dashed rgba(79, 140, 255, 0.45);
  background: rgba(79, 140, 255, 0.08);
  border-radius: 12px; padding: 10px 12px;
  text-align: center; margin-bottom: 4px;
}
.lan-label { font-size: 0.8rem; color: var(--muted); margin-bottom: 4px; }
.lan-url {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.95rem; font-weight: 700; color: var(--accent2);
  word-break: break-all; line-height: 1.4;
}

.hidden { display: none !important; }
a.download {
  color: var(--accent2); font-weight: 600;
  word-break: break-all; display: inline-block; margin-top: 6px;
}

details.advanced { padding: 10px 14px; }
details.advanced summary {
  cursor: pointer; font-size: 0.88rem; color: var(--muted);
  font-weight: 650; list-style: none;
}
details.advanced summary::-webkit-details-marker { display: none; }
details.advanced[open] summary { margin-bottom: 10px; color: var(--text); }
.qr-wrap {
  margin-top: 8px; display: flex; justify-content: center;
  background: #fff; border-radius: 8px; padding: 8px;
}
.qr-wrap:empty { display: none; padding: 0; }
.qr-wrap img, .qr-wrap canvas { max-width: 200px; height: auto; }

/* Modals */
.modal {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(6, 10, 18, 0.72);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  backdrop-filter: blur(4px);
}
.modal-card {
  width: min(420px, 100%);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 22px 18px;
  box-shadow: var(--shadow);
}

/* Session / chat-first */
.session {
  display: flex; flex-direction: column;
  min-height: 100dvh; max-width: 720px; margin: 0 auto;
  background: var(--bg);
}
.session-bar {
  display: flex; justify-content: space-between; align-items: center;
  gap: 10px; padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--card);
  position: sticky; top: 0; z-index: 5;
}
.session-label { font-size: 0.72rem; color: var(--ok); font-weight: 650; }
.chat {
  flex: 1; overflow-y: auto;
  padding: 14px 14px 8px;
  background: var(--bg);
  min-height: 40vh;
}
.chat .msg {
  margin-bottom: 10px; line-height: 1.4; word-break: break-word;
  max-width: 92%;
}
.chat .msg.me {
  margin-left: auto; background: rgba(79, 140, 255, 0.18);
  color: var(--text); padding: 10px 12px; border-radius: 14px 14px 4px 14px;
}
.chat .msg.them {
  background: var(--card); border: 1px solid var(--border);
  padding: 10px 12px; border-radius: 14px 14px 14px 4px;
}
.chat .msg.sys {
  text-align: center; color: var(--muted); font-size: 0.78rem;
  max-width: 100%;
}
.chat .msg .who {
  display: block; font-size: 0.72rem; font-weight: 650;
  color: var(--muted); margin-bottom: 2px;
}
.chat .msg.me .who { color: var(--accent2); }
.composer {
  padding: 10px 14px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--card);
}
.composer-row { display: flex; gap: 8px; align-items: center; }
.file-row { margin-top: 8px; }

@media (min-width: 640px) {
  body:not(.has-session) { padding-top: 24px; }
  .logo { font-size: 1.35rem; }
}
