:root {
  --bg: #f4f6fa; --panel: #ffffff; --text: #16202e; --muted: #6b7686; --line: #e1e6ee;
  --accent: #2f6df6; --accent-text: #fff; --mine: #2f6df6; --mine-text: #fff; --theirs: #ffffff;
  --err: #d6314a; --warn: #b7791f; --ok: #1f9d55; --shadow: 0 1px 2px rgba(20,30,50,.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b; --panel: #171e28; --text: #e6ebf2; --muted: #8b97a8; --line: #263041;
    --accent: #4b83ff; --mine: #2b5fd9; --theirs: #1d2634; --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; max-width: 100vw; overflow-x: hidden; }
html { touch-action: manipulation; }
body { background: var(--bg); color: var(--text); font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
.center { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.spinner { width: 32px; height: 32px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; width: 100%; max-width: 380px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.card h1 { font-size: 22px; margin: 0 0 4px; }
label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
/* font-size: 16px обязателен: iOS Safari приближает страницу при фокусе на поле со шрифтом < 16px */
input, textarea { font: inherit; font-size: 16px; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; width: 100%; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button { font: inherit; cursor: pointer; border: 0; border-radius: 10px; }
button.primary { background: var(--accent); color: var(--accent-text); padding: 12px 16px; font-weight: 600; }
button.primary:disabled { opacity: .6; cursor: default; }
button.ghost { background: transparent; color: var(--accent); padding: 8px 10px; }
.error { color: var(--err); margin: 0; font-size: 14px; }

#chat-view { height: 100dvh; display: flex; flex-direction: column; max-width: 860px; margin: 0 auto; background: var(--bg); position: relative; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--line); }
.topbar .title { font-weight: 600; flex: 1; }
.pill { font-size: 13px; padding: 4px 10px; border-radius: 999px; background: #fdecc8; color: #7a4b00; }
@media (prefers-color-scheme: dark) { .pill { background: #4a3a14; color: #ffd98a; } }
.banner { padding: 8px 14px; font-size: 14px; background: #e8f0ff; color: #234; border-bottom: 1px solid var(--line); }
@media (prefers-color-scheme: dark) { .banner { background: #1b2a47; color: #cfe0ff; } }

#messages { flex: 1; overflow-y: auto; overflow-anchor: none; padding: 12px 12px 8px; overscroll-behavior: contain; scroll-behavior: auto; }
.center-btn { display: block; margin: 0 auto 8px; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.day { text-align: center; color: var(--muted); font-size: 12px; margin: 14px 0 6px; }
.msg { display: flex; flex-direction: column; margin: 3px 0; max-width: 82%; min-width: 0; }
.msg.mine { margin-left: auto; align-items: flex-end; }
.msg.theirs { margin-right: auto; align-items: flex-start; }
.bubble { padding: 8px 12px; border-radius: 16px; background: var(--theirs); border: 1px solid var(--line); box-shadow: var(--shadow); word-break: break-word; overflow-wrap: anywhere; white-space: pre-wrap; }
.mine .bubble { background: var(--mine); color: var(--mine-text); border-color: transparent; border-bottom-right-radius: 5px; }
.theirs .bubble { border-bottom-left-radius: 5px; }
.bubble a { color: inherit; text-decoration: underline; }
.meta { font-size: 12px; color: var(--muted); margin: 2px 6px 0; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.meta .st-ok { color: var(--ok); } .meta .st-warn { color: var(--warn); } .meta .st-err { color: var(--err); }
.meta button { background: none; color: var(--accent); padding: 0; font-size: 12px; text-decoration: underline; }
.attach img { max-width: min(320px, 100%); max-height: 320px; border-radius: 10px; display: block; cursor: zoom-in; background: var(--line); }
.attach audio { width: min(260px, 100%); display: block; }
.attach video { max-width: min(320px, 100%); border-radius: 10px; display: block; }
.attach .chip { display: inline-flex; gap: 8px; align-items: center; text-decoration: none; padding: 6px 2px; }
.attach .note { font-size: 14px; opacity: .85; }
.bubble .txt:not(:empty) { margin-top: 6px; }
.attach + .txt, .txt:first-child { margin-top: 0; }
.editor textarea { min-width: 220px; width: 100%; background: var(--panel); color: var(--text); }
.editor .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
.editor .row button { padding: 6px 12px; background: var(--panel); color: var(--accent); }

.jump { position: absolute; right: 14px; bottom: 76px; width: 42px; height: 42px; border-radius: 50%; background: var(--panel); color: var(--accent); border: 1px solid var(--line); box-shadow: 0 2px 8px rgba(0,0,0,.18); font-size: 20px; }
#jump-count:not(:empty) { position: absolute; top: -6px; right: -4px; background: var(--accent); color: #fff; border-radius: 999px; font-size: 11px; padding: 1px 6px; }
.dropzone { position: absolute; inset: 56px 10px 10px; border: 3px dashed var(--accent); border-radius: 16px; background: rgba(47,109,246,.12); display: flex; align-items: center; justify-content: center; font-weight: 600; z-index: 5; pointer-events: none; }
.toast { position: absolute; left: 50%; transform: translateX(-50%); bottom: 90px; background: #222; color: #fff; padding: 8px 14px; border-radius: 10px; font-size: 14px; z-index: 6; max-width: 90%; text-align: center; }
.files { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px 12px 0; background: var(--panel); border-top: 1px solid var(--line); }
.file-chip { display: inline-flex; align-items: center; gap: 8px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 4px 8px; font-size: 14px; max-width: 100%; }
.file-chip img { width: 36px; height: 36px; object-fit: cover; border-radius: 6px; }
.file-chip button { background: none; color: var(--muted); font-size: 18px; padding: 0 4px; }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px; padding-bottom: max(8px, env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
.files:not([hidden]) + .composer { border-top: 0; }
.composer textarea { resize: none; max-height: 160px; min-height: 42px; line-height: 1.35; border-radius: 20px; padding: 9px 14px; }
.icon { background: transparent; color: var(--muted); width: 42px; height: 42px; display: grid; place-items: center; flex: none; }
.icon:hover { color: var(--accent); }
.send { background: var(--accent); color: #fff; width: 42px; height: 42px; display: grid; place-items: center; border-radius: 50%; flex: none; }
.recorder { position: absolute; left: 0; right: 0; bottom: 0; display: flex; align-items: center; gap: 12px; padding: 12px 14px; padding-bottom: max(12px, env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); z-index: 7; }
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--err); animation: blink 1s infinite; }
#rec-time { flex: 1; font-variant-numeric: tabular-nums; }
@keyframes blink { 50% { opacity: .25; } }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.88); display: flex; align-items: center; justify-content: center; z-index: 20; cursor: zoom-out; padding: 12px; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
