:root {
  --bg: #f5f4fb;
  --surface: #ffffff;
  --surface-2: #f0eef8;
  --text: #17152b;
  --muted: #6b6880;
  --border: #e4e1f0;
  --accent: #6c4cff;
  --accent-2: #00b89f;
  --accent-text: #ffffff;
  --mine: #efeaff;
  --ok: #16a34a;
  --ok-rgb: 22, 163, 74;
  --danger: #e5484d;
  --glow: rgba(108, 76, 255, .28);
  --glow-strong: rgba(108, 76, 255, .5);
  --radius: 16px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0e17;
    --surface: #1a1826;
    --surface-2: #22202f;
    --text: #eeecf7;
    --muted: #a19db8;
    --border: #2c2940;
    --accent: #8b74ff;
    --accent-2: #2ed3b7;
    --mine: #2a2350;
    --ok: #3ddc84;
    --ok-rgb: 61, 220, 132;
    --danger: #ff6369;
    --glow: rgba(139, 116, 255, .22);
    --glow-strong: rgba(139, 116, 255, .45);
  }
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

/* Пока app.js не подставил тексты на нужном языке, страницу не показываем. */
.boot body { visibility: hidden; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
body[data-stage="room"] { height: 100vh; height: 100dvh; overflow: hidden; }

[hidden] { display: none !important; }

a { color: var(--accent); }

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.show-sm { display: none; }

/* ---------- Кнопки и формы ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 16px;
  border: 1px solid transparent;
  border-radius: 12px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color .15s, border-color .15s, color .15s, transform .1s;
}
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover:not(:disabled) { background: var(--surface); }
.btn.small { min-height: 32px; padding: 5px 12px; font-size: 14px; border-radius: 10px; }
.btn.ok, .btn.ok:hover { background: var(--ok); border-color: var(--ok); color: #fff; }

input, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

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

/* ---------- Шапка ---------- */

.bar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.logo {
  font-weight: 800;
  font-size: 19px;
  letter-spacing: -0.02em;
  color: var(--text);
  text-decoration: none;
}
.logo span {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.spacer { flex: 1; }

.lang {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 13px;
}
.lang button {
  min-width: 34px;
  min-height: 28px;
  padding: 2px 8px;
  border: 0;
  border-radius: 999px;
  background: none;
  color: inherit;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.lang button[aria-pressed="true"] { background: var(--text); color: var(--bg); }
.lang span { opacity: .5; }

.btn.burn {
  min-height: 36px;
  padding: 6px 14px;
  font-size: 14px;
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border-color: color-mix(in srgb, var(--danger) 40%, transparent);
}
.btn.burn:hover:not(:disabled) { background: color-mix(in srgb, var(--danger) 18%, transparent); }
.btn.burn.armed, .btn.burn.armed:hover { background: var(--danger); border-color: var(--danger); color: #fff; animation: armed .8s ease-in-out infinite; }
@keyframes armed { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--danger) 25%, transparent); } }

/* ---------- Первый экран: QR и PIN ---------- */

.pair, .gate {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 32px 16px 28px;
  display: grid;
  gap: 22px;
  justify-items: center;
  text-align: center;
}

.hero h1 {
  margin: 0 0 10px;
  font-size: clamp(34px, 8vw, 52px);
  line-height: 1.05;
  font-weight: 800;
  letter-spacing: -0.035em;
}
.lead { margin: 0; font-size: 17px; color: var(--muted); }

.pair-card {
  width: min(100%, 360px);
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: 22px 20px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 28px;
  animation: glow 3.2s ease-in-out infinite;
  transform-origin: 50% 50%;
  transition: transform .28s cubic-bezier(.4, 0, .2, 1), opacity .28s ease, filter .28s ease;
}
@keyframes glow {
  0%, 100% { box-shadow: 0 12px 40px -14px var(--glow); }
  50% { box-shadow: 0 16px 70px -8px var(--glow-strong); }
}
/* Момент «Voilà!»: QR схлопывается в центр, на его месте раскрывается чат. */
.pair.leaving .pair-card, .pair-card.leaving { transform: scale(.85); opacity: 0; filter: blur(8px); }
.pair > :not(.pair-card) { transition: opacity .2s ease; }
.pair.leaving > :not(.pair-card) { opacity: 0; }

.qr-frame {
  position: relative;
  width: 248px;
  aspect-ratio: 1;
  margin-bottom: 8px;
  border-radius: 20px;
  overflow: hidden;
  background: #fff;
}
.qr { display: block; width: 100%; height: 100%; }
.qr-wait {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  font-size: 14px;
  color: #6b6880;
  background: linear-gradient(110deg, #f3f1fb 30%, #fff 50%, #f3f1fb 70%) 0 0 / 300% 100%;
  animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer { to { background-position: -150% 0; } }

.pin-label { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.pin {
  margin: 0;
  font-family: var(--mono);
  font-size: 42px;
  font-weight: 700;
  letter-spacing: .04em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
/* Истёкший код размыт, поверх — «🔄 Обновить код». QR при этом работает: в нём ссылка, а не код. */
.pin-box { display: grid; place-items: center; }
.pin-box > * { grid-area: 1 / 1; }
.pin { transition: filter .35s ease, opacity .35s ease; }
.pin-box.expired .pin { filter: blur(7px); opacity: .4; user-select: none; }
/* filter и opacity поднимают размытый код в свой слой — кнопку ставим выше, иначе клик уходит в код. */
.pin-refresh { position: relative; z-index: 1; box-shadow: 0 6px 20px -6px var(--glow-strong); }
.pin-timer { margin: 0; min-height: 20px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.pin-timer.soon { color: var(--danger); }
.pair-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 4px; }
.pin-hint { margin: 6px 0 0; font-size: 13px; color: var(--muted); }

.join { display: grid; gap: 8px; justify-items: center; width: 100%; }
.join label { font-size: 14px; color: var(--muted); }
.join-row { display: flex; gap: 8px; }
.join-row input {
  width: 150px;
  font-family: var(--mono);
  font-size: 20px;
  letter-spacing: .12em;
  text-align: center;
}

.foot { margin: 0; max-width: 460px; font-size: 12.5px; color: var(--muted); }
.foot.small { max-width: none; padding: 0 16px 8px; font-size: 12px; text-align: center; }

/* ---------- Комната ---------- */

.room {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}
.room.entering { animation: enter .3s cubic-bezier(.2, .8, .2, 1); }
@keyframes enter { from { opacity: 0; transform: scale(.96); } }

.status-row { display: flex; justify-content: center; padding: 10px 16px 2px; }
.badge {
  display: inline-flex;
  align-items: center;
  padding: 5px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ok);
  background: rgba(var(--ok-rgb), .12);
  border: 1px solid rgba(var(--ok-rgb), .3);
}
/* Зелёная волна вокруг статуса при подключении: на iOS вибрации нет, это главный отклик. */
.badge.pulse { animation: wave .9s ease-out 2, flash .9s ease-out; }
@keyframes wave {
  from { box-shadow: 0 0 0 0 rgba(var(--ok-rgb), .55); }
  to { box-shadow: 0 0 0 20px rgba(var(--ok-rgb), 0); }
}
@keyframes flash { 0% { background: rgba(var(--ok-rgb), .45); transform: scale(1.06); } }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  overscroll-behavior: contain;
}
.load-more { display: flex; margin: 0 auto 12px; }
.list { display: flex; flex-direction: column; gap: 10px; }
#pending:not(:empty) { margin-top: 10px; }

.msg {
  max-width: min(85%, 640px);
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 14px 11px;
}
.msg.mine { align-self: flex-end; background: var(--mine); border-color: transparent; }
.msg.system {
  align-self: center;
  background: none;
  border: 0;
  padding: 0;
  font-size: 13px;
  color: var(--muted);
}
.msg-head { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-body.mono {
  font-family: var(--mono);
  font-size: 15px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--surface-2);
}
.msg-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 9px; }
.url-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; width: 100%; }
.url-host { font-family: var(--mono); font-size: 13px; color: var(--muted); margin-right: 2px; overflow-wrap: anywhere; }

.preview-link { display: block; }
.preview {
  display: block;
  max-width: 100%;
  max-height: 320px;
  border-radius: 12px;
  margin: 2px 0 8px;
}
.file-row { display: flex; align-items: center; gap: 10px; }
.file-icon { font-size: 26px; line-height: 1; }
.file-meta { min-width: 0; }
.file-name { font-weight: 600; overflow-wrap: anywhere; }
.file-size { font-size: 13px; color: var(--muted); }
.file-gone { color: var(--muted); font-style: italic; }

.upload-progress {
  display: block;
  width: 100%;
  min-width: 180px;
  height: 6px;
  margin-top: 10px;
  border: 0;
  border-radius: 999px;
  overflow: hidden;
  background: var(--border);
  accent-color: var(--accent);
  -webkit-appearance: none;
  appearance: none;
}
.upload-progress::-webkit-progress-bar { background: var(--border); }
.upload-progress::-webkit-progress-value { background: var(--accent); border-radius: 999px; transition: width .15s; }
.upload-progress::-moz-progress-bar { background: var(--accent); border-radius: 999px; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.composer textarea { resize: none; min-height: 44px; max-height: 200px; overflow-y: hidden; }
.composer .btn { min-height: 44px; }
.attach, .mic { font-size: 20px; padding: 8px 12px; }
.mic.active { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 15%, transparent); animation: blink 1.2s ease-in-out infinite; }
@keyframes blink { 50% { opacity: .6; } }
.expires { white-space: nowrap; }

/* ---------- Пригласить ещё устройство ---------- */

.invite {
  width: min(400px, calc(100vw - 24px));
  border: 0;
  padding: 18px;
  border-radius: 28px;
  background: var(--bg);
  color: var(--text);
  text-align: center;
}
.invite::backdrop { background: rgba(10, 8, 20, .55); backdrop-filter: blur(4px); }
.invite form { margin-top: 12px; }
#invite-slot { display: grid; justify-items: center; }

/* ---------- Dropzone на всё окно ---------- */

.drop {
  position: fixed;
  inset: 0;
  z-index: 50;
  padding: 16px;
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  pointer-events: none;
}
.drop-inner {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  border: 3px dashed var(--accent);
  border-radius: 28px;
  font-size: clamp(22px, 4vw, 34px);
  font-weight: 800;
  color: var(--accent);
}

/* ---------- Служебное ---------- */

.toast, .conn {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  z-index: 60;
}
.toast { bottom: calc(90px + env(safe-area-inset-bottom)); }
.conn { top: 64px; background: var(--danger); color: #fff; }

/* Скрытое поле для копирования без Clipboard API. 16px — чтобы iOS не зумил страницу. */
.clip { position: fixed; top: 0; left: 0; width: 1px; height: 1px; opacity: 0; font-size: 16px; pointer-events: none; }

.print-frame { position: fixed; right: 0; bottom: 0; width: 0; height: 0; border: 0; }
.print-area { display: none; }
@media print {
  body.printing > :not(#print-area) { display: none !important; }
  body.printing .print-area { display: block; }
  .print-area img { display: block; max-width: 100%; max-height: 100vh; margin: 0 auto; }
}

@media (prefers-reduced-motion: reduce) {
  .pair-card, .mic.active, .btn.burn.armed, .qr-wait { animation: none; }
  .badge.pulse { animation: wave .9s ease-out 1; }
  .room.entering { animation: none; }
  .pair-card, .pair > :not(.pair-card), .pin { transition: none; }
}

@media (max-width: 600px) {
  .bar { padding: 8px 12px; gap: 8px; }
  .logo { font-size: 17px; }
  .hide-sm { display: none; }
  .show-sm { display: inline; }
  .pair, .gate { padding-top: 22px; gap: 18px; }
  .lead { font-size: 16px; }
  .qr-frame { width: 224px; }
  .pin { font-size: 36px; }
  .messages, .composer { padding-left: 12px; padding-right: 12px; }
  .composer #send { padding: 8px 12px; }
  .attach, .mic { padding: 8px 10px; }
  .msg { max-width: 92%; }
}
