/* Токены и общий вид взяты из share_my_notes — чтобы вещи одного автора
   выглядели роднёй. Всё, что ниже раздела «Лента», написано под диалог. */
:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1d2129;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --self: #dbeafe;
  --other: #ffffff;
  --code-bg: #f0f1f4;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #b45309;
  --warn-bg: #fef3c7;
}
:root[data-theme="dark"] {
  --bg: #0f1115; --card: #1a1d24; --text: #e7e9ee; --muted: #9aa3b2;
  --border: #2a2f3a; --accent: #3b82f6; --self: #1e3a5f; --other: #232733;
  --code-bg: #12151b; --warn: #fbbf24; --warn-bg: #3a2f12;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    --bg: #0f1115; --card: #1a1d24; --text: #e7e9ee; --muted: #9aa3b2;
    --border: #2a2f3a; --accent: #3b82f6; --self: #1e3a5f; --other: #232733;
    --code-bg: #12151b; --warn: #fbbf24; --warn-bg: #3a2f12;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.screen { height: 100dvh; display: flex; flex-direction: column; }

#splash-screen.splash { align-items: center; justify-content: center; gap: 14px; }
.spinner {
  width: 42px; height: 42px; border-radius: 50%;
  border: 4px solid rgba(128,128,128,0.3);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Вход ── */
#auth-screen { align-items: center; justify-content: center; padding: 16px; }
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px 24px; width: 100%; max-width: 360px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.06);
}
.card h1 { margin: 0 0 4px; font-size: 22px; }
.step { display: flex; flex-direction: column; gap: 8px; margin-top: 18px; }
label { font-size: 13px; color: var(--muted); }
input, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 11px 12px; outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }
#code { letter-spacing: 6px; font-size: 20px; text-align: center; }

button {
  font: inherit; cursor: pointer; border: none; border-radius: 10px;
  padding: 11px 14px; background: var(--accent); color: var(--accent-text);
}
button:disabled { opacity: 0.5; cursor: default; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); padding: 7px 11px; }
button.danger { background: var(--danger); color: #fff; }
button.link { background: transparent; color: var(--accent); padding: 4px; text-align: left; }
.msg { min-height: 18px; font-size: 13px; margin: 12px 0 0; }
.msg.error { color: var(--danger); }
.msg.ok { color: var(--ok); }

/* ── Шапка ── */
.app-header {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--card); border-bottom: 1px solid var(--border);
}
.header-info { display: flex; align-items: center; gap: 8px; min-width: 0; }
.header-info strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn { font-size: 11px; color: var(--muted); }
.conn.online { color: var(--ok); }
.conn.offline { color: var(--danger); }
.header-actions { display: flex; gap: 6px; flex-shrink: 0; }
.badge {
  display: inline-block; min-width: 18px; padding: 0 5px;
  background: var(--accent); color: var(--accent-text);
  border-radius: 9px; font-size: 12px; text-align: center;
}

/* ── Полоса состояния Cursor ──
   Отдельная от индикатора связи: браузер может прекрасно общаться с
   сервером, пока сам Cursor не запущен. Путать эти две вещи нельзя. */
.channel-bar {
  padding: 7px 14px; font-size: 13px; text-align: center;
  background: var(--warn-bg); color: var(--warn);
  border-bottom: 1px solid var(--border);
}

/* ── Лента ── */
.messages {
  flex: 1; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 10px;
  overscroll-behavior: contain;
}
.bubble {
  position: relative; max-width: min(78%, 620px);
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 14px;
  background: var(--other); align-self: flex-start;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.bubble.self { background: var(--self); align-self: flex-end; }
.bubble .text { line-height: 1.45; white-space: pre-wrap; }
.bubble .text a { color: var(--accent); text-decoration: underline; word-break: break-all; }
.bubble .text strong { font-weight: 650; }
.bubble .foot {
  display: flex; align-items: center; gap: 8px; justify-content: flex-end;
  flex-wrap: wrap;
  margin-top: 4px; font-size: 11px; color: var(--muted);
}
.bubble .foot .state { display: inline-flex; align-items: center; gap: 6px; }
.bubble .foot .pending { color: var(--warn); }
.bubble .foot .failed { color: var(--danger); }
.bubble .retry-btn {
  background: transparent; border: 1px solid var(--danger); color: var(--danger);
  padding: 1px 7px; font-size: 12px; border-radius: 8px; line-height: 1.3;
}
.bubble .retry-btn:disabled { opacity: 0.5; }
.bubble .copy-btn {
  background: transparent; border: 1px solid var(--border); color: var(--accent);
  padding: 2px 8px; font-size: 12px; border-radius: 8px;
}
.bubble .copy-btn.copied { color: var(--ok); border-color: var(--ok); }

/* Код: внутри пузыря живёт своей жизнью, с горизонтальным скроллом —
   длинная команда не должна распирать ленту. */
.bubble code.inline {
  background: var(--code-bg); border-radius: 5px; padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em;
}
.bubble pre.code {
  margin: 8px 0; padding: 10px 12px; background: var(--code-bg);
  border: 1px solid var(--border); border-radius: 10px;
  overflow-x: auto; white-space: pre;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px; line-height: 1.4;
}
.bubble .code-wrap { display: block; margin: 8px 0; }
.bubble .code-bar { display: flex; justify-content: flex-end; margin-bottom: 2px; }
.bubble .code-bar .copy-btn { font-size: 11px; padding: 1px 7px; }
.bubble .code-wrap pre.code { margin: 0; }

.day-sep { align-self: center; font-size: 12px; color: var(--muted); padding: 2px 10px; }
.empty-hint { margin: auto; text-align: center; color: var(--muted); font-size: 14px; line-height: 1.5; }

.to-bottom {
  position: absolute; right: 18px; bottom: 84px; z-index: 5;
  width: 38px; height: 38px; padding: 0; border-radius: 50%;
  background: var(--card); color: var(--text); border: 1px solid var(--border);
  box-shadow: 0 3px 12px rgba(0,0,0,0.15); font-size: 18px;
}

/* ── «Cursor думает» ── */
.busy-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 14px; font-size: 13px; color: var(--muted);
  background: var(--card); border-top: 1px solid var(--border);
}
.busy-bar.stale { color: var(--warn); }
.busy-bar .dots { display: inline-flex; gap: 4px; }
.busy-bar .dots i {
  width: 6px; height: 6px; border-radius: 50%; background: var(--accent);
  animation: busy-blink 1.2s infinite ease-in-out;
}
.busy-bar .dots i:nth-child(2) { animation-delay: 0.2s; }
.busy-bar .dots i:nth-child(3) { animation-delay: 0.4s; }
.busy-bar.stale .dots i { background: var(--warn); animation: none; opacity: 0.5; }
@keyframes busy-blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

/* ── Вложения ── */
.attach-list {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 8px 10px 0; background: var(--card);
}
.attach-chip {
  display: inline-flex; align-items: center; gap: 6px;
  max-width: 100%; padding: 4px 8px; font-size: 12px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
}
.attach-chip .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 180px; }
.attach-chip .s { color: var(--muted); flex-shrink: 0; }
.attach-chip button {
  background: transparent; border: none; color: var(--muted);
  padding: 0 2px; font-size: 14px; line-height: 1;
}
.attach-chip.uploading { opacity: 0.6; }

.bubble .img-preview {
  display: block; margin-top: 6px; max-width: 100%; max-height: 340px;
  border-radius: 10px; cursor: zoom-in; background: var(--code-bg);
}
.bubble .file-card {
  display: flex; align-items: center; gap: 10px; margin-top: 6px;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px;
  background: rgba(127, 127, 127, 0.06);
}
.bubble .file-card::before { content: "📄"; font-size: 20px; flex-shrink: 0; }
.bubble .file-info { min-width: 0; flex: 1; }
.bubble .file-name { font-weight: 600; word-break: break-all; font-size: 13px; }
.bubble .file-meta { font-size: 12px; color: var(--muted); }
.bubble .dl-btn {
  flex-shrink: 0; background: var(--accent); color: var(--accent-text);
  border: none; padding: 5px 11px; font-size: 12px; border-radius: 8px;
}
.bubble .dl-btn:disabled { opacity: 0.6; }

/* ── Композер ── */
.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); background: var(--card);
}
.composer textarea { flex: 1; resize: none; max-height: 160px; }
.composer button { flex-shrink: 0; }
.composer .attach {
  background: transparent; border: 1px solid var(--border);
  font-size: 18px; padding: 8px 10px; line-height: 1;
}

/* ── Модалка и панель ── */
.modal {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.45); padding: 20px;
}
.modal-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 22px; width: 100%; max-width: 360px; box-shadow: 0 10px 40px rgba(0,0,0,0.25);
}
.modal-card h2 { margin: 0 0 6px; }
.modal-card button { margin-top: 10px; width: 100%; }

.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 9; }
.panel {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 10;
  width: min(360px, 86vw); background: var(--card); border-left: 1px solid var(--border);
  padding: 14px; padding-top: calc(14px + env(safe-area-inset-top));
  display: flex; flex-direction: column; gap: 10px;
  box-shadow: -8px 0 24px rgba(0,0,0,0.12);
}
.panel-head { display: flex; align-items: center; justify-content: space-between; }
.devices-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.device {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 11px 4px; border-bottom: 1px solid var(--border);
}
.device .d-name { font-weight: 600; }
.device .d-sub { font-size: 12px; color: var(--muted); }
.device.current .d-name::after {
  content: " (это устройство)"; font-weight: 400; color: var(--muted); font-size: 12px;
}
.device .d-actions { display: flex; gap: 6px; flex-shrink: 0; }
.device .d-actions button {
  background: transparent; border: 1px solid var(--border); color: var(--text);
  padding: 5px 9px; font-size: 13px;
}
.device .revoke { color: var(--danger); }
