/* ===== Theme tokens ===== */
:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-2: #f5f5f5;
  --border: #ececec;
  --border-strong: #dcdcdc;
  --text: #0a0a0a;
  --text-soft: #555;
  --muted: #888;
  --hover: #f3f3f3;
  --accent: #0a0a0a;
  --accent-text: #ffffff;
  --accent-soft: #ededed;
  --bubble-me: #0a0a0a;
  --bubble-me-text: #ffffff;
  --bubble-peer: #f1f1f1;
  --bubble-peer-text: #0a0a0a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.04);
  --qr-fg: #0a0a0a;
  --qr-bg: #ffffff;
  --radius: 14px;
  --radius-sm: 10px;
  color-scheme: light;

  /* Icon colors */
  --icon-color: #0a0a0a;
  --icon-hover: #555;
  
  /* Telegram-like pattern */
  --bg-pattern: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 200'%3E%3Ccircle cx='30' cy='30' r='2' fill='%23000000' fill-opacity='0.02'/%3E%3Ccircle cx='70' cy='80' r='1.5' fill='%23000000' fill-opacity='0.02'/%3E%3Ccircle cx='120' cy='150' r='2.5' fill='%23000000' fill-opacity='0.02'/%3E%3Ccircle cx='180' cy='40' r='1.8' fill='%23000000' fill-opacity='0.02'/%3E%3C/svg%3E");
}

:root[data-theme="dark"] {
  --bg: #0e0e0e;
  --surface: #161616;
  --surface-2: #1d1d1d;
  --border: #262626;
  --border-strong: #333333;
  --text: #f2f2f2;
  --text-soft: #b9b9b9;
  --muted: #7a7a7a;
  --hover: #1f1f1f;
  --accent: #f2f2f2;
  --accent-text: #0a0a0a;
  --accent-soft: #2a2a2a;
  --bubble-me: #f2f2f2;
  --bubble-me-text: #0a0a0a;
  --bubble-peer: #1f1f1f;
  --bubble-peer-text: #f2f2f2;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.3);
  --qr-fg: #f2f2f2;
  --qr-bg: #161616;

  /* Icon colors dark */
  --icon-color: #f2f2f2;
  --icon-hover: #b9b9b9;
  
  --bg-pattern: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 200'%3E%3Ccircle cx='30' cy='30' r='2' fill='%23ffffff' fill-opacity='0.03'/%3E%3Ccircle cx='70' cy='80' r='1.5' fill='%23ffffff' fill-opacity='0.02'/%3E%3Ccircle cx='120' cy='150' r='2.5' fill='%23ffffff' fill-opacity='0.02'/%3E%3Ccircle cx='180' cy='40' r='1.8' fill='%23ffffff' fill-opacity='0.03'/%3E%3C/svg%3E");
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg) var(--bg-pattern) repeat;
  background-size: 32px 32px;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter",
               "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: background .2s ease, color .2s ease;
}
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.small { font-size: 13px; }
.muted { color: var(--muted); }

/* ===== Onboarding ===== */
.onboard {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 16px;
  animation: fade .35s ease;
}
.card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 28px 20px 22px;
  box-shadow: var(--shadow);
  animation: rise .4s ease;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 6px;
  position: relative;
}
.brand h1 {
  font-size: 18px; margin: 0;
  font-weight: 600; letter-spacing: -0.01em;
  flex: 1;
}
.header-actions { display: flex; gap: 4px; }
.logo {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text);
  border-radius: 9px;
  transition: background .2s ease, color .2s ease;
}
.qr-wrap {
  margin: 18px auto 14px;
  width: 200px; height: 200px;
  padding: 10px;
  background: var(--qr-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: grid; place-items: center;
  transition: transform .2s ease, background .2s ease, border-color .2s ease;
}
.qr-wrap:hover { transform: translateY(-1px); }
.qr-wrap img { width: 100%; height: 100%; display: block; }

.code-row {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 8px 8px 14px;
  transition: background .2s ease, border-color .2s ease;
}
.code {
  flex: 1;
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-align: center;
  user-select: all;
  cursor: pointer;
  padding: 6px 0;
}
.icon-btn {
  width: 40px; 
  height: 40px;
  display: grid; 
  place-items: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 50%;
  color: var(--icon-color);
  transition: all .2s ease;
}
.icon-btn:hover { 
  background: var(--hover); 
  color: var(--icon-hover);
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn.active { background: var(--accent-soft); color: var(--accent); }

.divider {
  display: flex; align-items: center; gap: 10px;
  color: var(--muted);
  margin: 18px 0 12px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.divider::before, .divider::after {
  content: ""; flex: 1; height: 1px; background: var(--border);
}

.join-form { 
  display: flex; 
  flex-direction: column;
  gap: 10px;
}
.join-form input {
  width: 100%;
  padding: 14px 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: center;
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  outline: none;
  transition: border-color .15s ease, background .15s ease;
  color: var(--text);
}
.join-form input:focus { border-color: var(--border-strong); background: var(--surface); }
.join-form button {
  width: 100%;
  padding: 14px 20px;
}

.primary {
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: var(--radius);
  padding: 12px 18px;
  font-weight: 600;
  letter-spacing: 0.01em;
  transition: transform .12s ease, opacity .15s ease, background .2s ease, color .2s ease;
}
.primary:hover { opacity: .92; }
.primary:active { transform: scale(.98); }

.error {
  color: #e5484d;
  font-size: 13px;
  min-height: 18px;
  margin: 10px 0 0;
  text-align: center;
  animation: fade .2s ease;
}

/* ===== Messenger layout ===== */
.app {
  height: 100vh;
  display: grid;
  grid-template-columns: 320px 1fr;
  background: var(--bg) var(--bg-pattern) repeat;
  background-size: 32px 32px;
  overflow: hidden;
}
.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex; flex-direction: column;
  min-height: 0;
  overflow: hidden;
  transition: background .2s ease, border-color .2s ease;
}
.me { padding: 16px; border-bottom: 1px solid var(--border); }
.me-row { display: flex; align-items: center; gap: 8px; }
.me-code {
  flex: 1;
  font-family: "SF Mono", ui-monospace, monospace;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.18em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  text-align: center;
  cursor: pointer;
  transition: background .15s ease, border-color .2s ease;
  color: var(--text);
}
.me-code:hover { background: var(--hover); }
.qr-small {
  width: 100%;
  margin-top: 10px;
  border-radius: var(--radius-sm);
  background: var(--qr-bg);
  padding: 8px;
  border: 1px solid var(--border);
  display: none;
}
.qr-small.show { display: block; animation: rise .2s ease; }

.rooms-head {
  padding: 14px 16px 8px;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.rooms {
  list-style: none;
  margin: 0; padding: 0 8px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}
.room {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex; flex-direction: column; gap: 2px;
  transition: background .15s ease;
}
.room:hover { background: var(--hover); }
.room.active { background: var(--accent-soft); }
.room .peer-line {
  display: flex; justify-content: space-between; align-items: center;
  font-weight: 600; font-size: 14px;
}
.room .peer-code { color: var(--muted); font-weight: 500; font-size: 12px; letter-spacing: 0.12em; font-family: "SF Mono", monospace; }
.room .preview {
  font-size: 13px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.room .preview.kind-file::before { content: "📎 "; }
.room .preview.kind-image::before { content: "🖼 "; }

.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); }
.ghost {
  width: 100%;
  background: transparent;
  border: 1px dashed var(--border-strong);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 10px;
  font-weight: 500;
  transition: background .15s ease, border-color .15s ease;
}
.ghost:hover { background: var(--hover); border-color: var(--border-strong); }

/* ===== Chat panel ===== */
.chat {
  display: flex; 
  flex-direction: column;
  min-width: 0; 
  min-height: 0;
  height: 100vh;
  overflow: hidden;
  background: var(--bg) var(--bg-pattern) repeat;
  background-size: 32px 32px;
}
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  transition: background .2s ease, border-color .2s ease;
  flex-shrink: 0;
}
.peer { display: flex; flex-direction: column; }
.peer-name { font-weight: 600; }
.peer-code { font-size: 12px; color: var(--muted); font-family: "SF Mono", monospace; letter-spacing: 0.12em; }
.spacer { flex: 1; }
.only-mobile { display: none; }

.messages {
  flex: 1; 
  min-height: 0;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex; 
  flex-direction: column; 
  gap: 6px;
  scroll-behavior: smooth;
}
.msg {
  max-width: min(560px, 78%);
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 14.5px;
  line-height: 1.35;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  animation: pop .18s ease;
  position: relative;
}
.msg.me {
  align-self: flex-end;
  background: var(--bubble-me);
  color: var(--bubble-me-text);
  border-bottom-right-radius: 6px;
}
.msg.peer {
  align-self: flex-start;
  background: var(--bubble-peer);
  color: var(--bubble-peer-text);
  border-bottom-left-radius: 6px;
}
.msg .time {
  display: block;
  font-size: 10.5px;
  opacity: .55;
  margin-top: 3px;
}

/* Статус сообщения (галочки) */
.msg .status {
  display: inline-flex;
  align-items: center;
  gap: 1px;
  font-size: 13px;
  margin-left: 4px;
  opacity: 0.85;
}
.status.sent::after { content: "✓"; color: var(--muted); }
.status.delivered::after { content: "✓✓"; color: var(--muted); }
.status.read::after { content: "✓✓"; color: #34b7f1; font-weight: bold; }

.msg .file {
  display: flex; align-items: center; gap: 10px;
  text-decoration: none;
  color: inherit;
}
.msg .file .icon {
  width: 36px; height: 36px; border-radius: 9px;
  background: rgba(255,255,255,0.12);
  display: grid; place-items: center;
  flex-shrink: 0;
}
.msg.peer .file .icon { background: rgba(255,255,255,0.08); }
:root[data-theme="light"] .msg.peer .file .icon { background: rgba(0,0,0,0.06); }
.msg .file .name {
  font-size: 13.5px; font-weight: 500;
  max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.msg img.media {
  display: block; max-width: 100%; max-height: 320px;
  border-radius: 10px; margin-top: 4px;
  cursor: zoom-in;
  transition: transform .15s ease;
}
.msg img.media:hover { transform: scale(1.01); }

/* Голосовое сообщение как в Telegram */
.voice-message {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  min-width: 260px;
}
.voice-message .play-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  display: grid;
  place-items: center;
  flex-shrink: 0;
  cursor: pointer;
  transition: transform .2s ease;
}
.voice-message .play-btn:hover { transform: scale(1.08); }
.voice-message .waveform {
  flex: 1;
  height: 36px;
  display: flex;
  align-items: center;
  gap: 3px;
}
.voice-message .bar {
  width: 3px;
  background: currentColor;
  border-radius: 999px;
  transition: height .15s ease;
}
.voice-message.playing .bar { animation: voiceBar 1.1s infinite ease-in-out; }
.voice-message .duration {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  min-width: 40px;
  text-align: right;
}
.voice-message .bar:nth-child(1) { height: 16px; }
.voice-message .bar:nth-child(2) { height: 24px; animation-delay: 80ms; }
.voice-message .bar:nth-child(3) { height: 32px; animation-delay: 160ms; }
.voice-message .bar:nth-child(4) { height: 22px; animation-delay: 240ms; }
.voice-message .bar:nth-child(5) { height: 28px; animation-delay: 320ms; }

.empty-chat {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 40px 20px;
  animation: fade .3s ease;
}
.empty-chat h2 { font-weight: 600; margin: 0 0 6px; color: var(--text); }

/* ===== Composer ===== */
.composer {
  display: flex; 
  align-items: flex-end; 
  gap: 8px;
  padding: 12px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  transition: background .2s ease, border-color .2s ease;
  flex-shrink: 0;
  margin-top: auto;
}
.composer textarea {
  flex: 1; 
  resize: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: 10px 12px;
  max-height: 120px;
  min-height: 40px;
  outline: none;
  transition: border-color .15s ease, background .15s ease;
  line-height: 1.4;
  color: var(--text);
  font-size: 14px;
}
.composer textarea:focus { border-color: var(--border-strong); background: var(--surface); }
.composer .send { 
  padding: 10px 12px; 
  border-radius: 12px;
  align-self: flex-end;
}
.composer .send svg { display: block; }

.composer-actions {
  display: flex;
  gap: 4px;
  align-items: center;
}

.icon-mic, .icon-theme, .icon-send {
  width: 24px;
  height: 24px;
  stroke: currentColor;
  stroke-width: 2.2;
  fill: none;
}

/* Audio player */
.audio-player {
  min-width: 200px;
}
.audio-player audio {
  outline: none;
  border-radius: 10px;
  background: var(--surface-2);
}
.msg .audio-player audio {
  max-width: 260px;
}

/* Loading */
.msg[data-temp-id] {
  opacity: 0.7;
  animation: pulse 1s ease-in-out infinite;
}

/* Recording button */
#audioRecordBtn.active {
  background: #e5484d;
  color: white;
  animation: pulse-red 1.2s ease-in-out infinite;
}

/* ===== Toast ===== */
.toast {
  position: fixed; left: 50%; bottom: 30px;
  transform: translateX(-50%) translateY(20px);
  background: var(--accent);
  color: var(--accent-text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 1000;
  box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== Animations ===== */
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pop { from { opacity: 0; transform: translateY(4px) scale(.98); } to { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes pulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 0.5; }
}
@keyframes pulse-red {
  0%, 100% { background: #e5484d; }
  50% { background: #ff4d4d; }
}
@keyframes voiceBar {
  0%, 100% { height: 12px; }
  50% { height: 36px; }
}

/* ===== Mobile Optimizations ===== */
@media (max-width: 760px) {
  .onboard { padding: 16px; align-items: center; }
  .card { padding: 20px 16px; max-width: 100%; margin: 0; }
  
  .join-form { gap: 12px; }
  .join-form input { font-size: 16px; letter-spacing: 0.15em; padding: 12px; }
  .join-form button { padding: 12px; }
  
  .qr-wrap { width: 160px; height: 160px; margin: 12px auto; }
  .code { font-size: 16px; letter-spacing: 0.12em; }
  
  .app { 
    grid-template-columns: 1fr;
    position: fixed;
    width: 100%;
    height: 100%;
  }
  
  .sidebar {
    position: fixed; 
    inset: 0;
    z-index: 10;
    transform: translateX(0);
    transition: transform .25s ease;
    width: 85%;
    max-width: 320px;
  }
  
  .app.chat-open .sidebar { transform: translateX(-100%); }
  .app:not(.chat-open) .chat { display: flex; }
  .only-mobile { display: flex; }
  
  .chat {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  
  .messages {
    padding: 12px;
    -webkit-overflow-scrolling: touch;
  }
  
  .composer {
    position: relative;
    bottom: 0; left: 0; right: 0;
    padding: 10px 12px;
  }
  
  .composer textarea { font-size: 15px; padding: 10px; }
  .msg { max-width: 88%; }
  
  body, .app, .chat, .sidebar { overflow-x: hidden; }
  .me-code { font-size: 13px; letter-spacing: 0.12em; }
  
  .voice-message { min-width: 220px; }
  .icon-btn { width: 38px; height: 38px; }
}