:root {
  --bg: #f3f5f9; --panel: #ffffff; --text: #1b2230; --muted: #6b7587; --line: #e3e7ef;
  --accent: #2f6fed; --accent-text: #ffffff; --mine: #e4edff; --theirs: #ffffff; --tr: #f4f6fb;
  --danger: #c93636; --shadow: 0 2px 12px rgba(20, 30, 60, .12);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131a; --panel: #171c26; --text: #e8ecf4; --muted: #93a0b6; --line: #262d3b;
    --accent: #5b92ff; --accent-text: #0b1220; --mine: #1f3159; --theirs: #1c2230; --tr: #161b26;
    --danger: #ff7b7b; --shadow: 0 2px 14px rgba(0, 0, 0, .5);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
.muted { color: var(--muted); }
.err { color: var(--danger); font-size: 14px; }

.center { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.card { background: var(--panel); border-radius: 18px; box-shadow: var(--shadow); padding: 32px 26px; max-width: 420px; width: 100%; text-align: center; }
.card h1 { margin: 6px 0 10px; font-size: 24px; }
.card p { margin: 0 0 20px; }
.logo { font-size: 44px; }
.primary { background: var(--accent); color: var(--accent-text); border-radius: 12px; padding: 10px 16px; font-weight: 600; }
.primary.big { padding: 14px 18px; width: 100%; }
.primary:disabled { opacity: .55; cursor: default; }

/* Layout */
.chat { height: 100dvh; display: flex; flex-direction: column; max-width: 860px; margin: 0 auto; background: var(--bg); }
.top { display: flex; align-items: center; justify-content: space-between; padding: calc(8px + env(safe-area-inset-top)) 14px 8px; background: var(--panel); border-bottom: 1px solid var(--line); position: relative; z-index: 5; }
.title { font-weight: 700; font-size: 17px; }
.logo-sm { margin-right: 4px; }
.top-right { display: flex; align-items: center; gap: 10px; position: relative; }
.conn { width: 9px; height: 9px; border-radius: 50%; background: #3cbf6b; }
.conn.off { background: #e0a030; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: var(--accent-text); font-weight: 700; display: grid; place-items: center; }
.menu { position: absolute; right: 0; top: 44px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); min-width: 220px; padding: 6px; z-index: 20; }
.menu button { display: block; width: 100%; text-align: left; padding: 10px 12px; border-radius: 8px; }
.menu button:hover { background: var(--tr); }
.menu-name { padding: 8px 12px; color: var(--muted); font-size: 14px; border-bottom: 1px solid var(--line); margin-bottom: 4px; word-break: break-all; }

.list { flex: 1; overflow-y: auto; padding: 12px 12px 6px; -webkit-overflow-scrolling: touch; }
.earlier { display: block; margin: 0 auto 12px; padding: 7px 14px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); color: var(--muted); font-size: 14px; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.day { text-align: center; color: var(--muted); font-size: 12.5px; margin: 14px 0 8px; }
.day span { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; }

.msg { display: flex; margin: 3px 0; }
.msg.mine { justify-content: flex-end; }
.bubble { max-width: min(86%, 560px); background: var(--theirs); border: 1px solid var(--line); border-radius: 16px; padding: 7px 11px 6px; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.mine .bubble { background: var(--mine); border-color: transparent; border-bottom-right-radius: 5px; }
.theirs .bubble { border-bottom-left-radius: 5px; }
.who { font-size: 12.5px; font-weight: 700; color: var(--accent); margin-bottom: 1px; }
.body { word-wrap: break-word; overflow-wrap: anywhere; }
.body p, .body div { margin: 0; }
.body ul, .body ol { margin: 2px 0; padding-left: 22px; }
.body blockquote { margin: 3px 0; padding: 1px 0 1px 10px; border-left: 3px solid var(--accent); color: var(--muted); }
.body a { color: var(--accent); text-decoration: underline; }
.body code { background: var(--tr); padding: 0 4px; border-radius: 4px; }
.tr { margin-top: 6px; padding-top: 6px; border-top: 1px dashed var(--line); font-size: .97em; }
.tr .lang { display: inline-block; font-size: 11px; font-weight: 700; letter-spacing: .04em; color: var(--muted); border: 1px solid var(--line); border-radius: 5px; padding: 0 5px; margin-bottom: 2px; }
.tr.pending { color: var(--muted); }
.tr.failed { color: var(--danger); font-size: 14px; }
.tr.failed button { text-decoration: underline; color: inherit; padding: 0 4px; }
.dots::after { content: "…"; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }
.meta { display: flex; justify-content: flex-end; gap: 8px; align-items: center; font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.meta .del { opacity: .6; padding: 0 2px; }
.meta .del:hover { opacity: 1; color: var(--danger); }
.pic { display: block; max-width: 100%; width: 320px; border-radius: 10px; margin: 3px 0; background: var(--tr); cursor: zoom-in; object-fit: cover; }
.pic.missing { height: 140px; }

/* Composer */
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 6px 10px calc(8px + env(safe-area-inset-bottom)); position: relative; }
.toolbar { display: flex; align-items: center; gap: 2px; overflow-x: auto; scrollbar-width: none; padding-bottom: 4px; }
.toolbar::-webkit-scrollbar { display: none; }
.toolbar button { min-width: 34px; height: 34px; border-radius: 8px; font-size: 16px; flex: none; }
.toolbar button:hover { background: var(--tr); }
.toolbar button.on { background: var(--mine); color: var(--accent); }
.toolbar .sep { width: 1px; height: 20px; background: var(--line); margin: 0 5px; flex: none; }
.input-row { display: flex; gap: 8px; align-items: flex-end; }
.editor { flex: 1; min-height: 42px; max-height: 38dvh; overflow-y: auto; background: var(--bg); border: 1px solid var(--line); border-radius: 14px; padding: 9px 12px; outline: none; word-wrap: break-word; overflow-wrap: anywhere; -webkit-user-select: text; user-select: text; }
.editor:focus { border-color: var(--accent); }
.editor:empty::before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; }
.editor ul, .editor ol { margin: 2px 0; padding-left: 22px; }
.editor blockquote { margin: 3px 0; padding-left: 10px; border-left: 3px solid var(--accent); color: var(--muted); }
.send { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: var(--accent-text); font-size: 18px; flex: none; }
.send:disabled { opacity: .5; }

.attach-strip { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 8px; }
.thumb { position: relative; flex: none; }
.thumb img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; display: block; border: 1px solid var(--line); }
.thumb button { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; background: var(--text); color: var(--bg); font-size: 11px; line-height: 20px; }
.thumb.busy img { opacity: .5; }

.emoji-panel { position: absolute; left: 10px; right: 10px; bottom: 100%; margin-bottom: 4px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); z-index: 10; }
.emoji-tabs { display: flex; border-bottom: 1px solid var(--line); padding: 4px; gap: 2px; overflow-x: auto; }
.emoji-tabs button { font-size: 20px; padding: 4px 8px; border-radius: 8px; flex: none; }
.emoji-tabs button.on { background: var(--mine); }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); max-height: 220px; overflow-y: auto; padding: 6px; }
.emoji-grid button { font-size: 24px; height: 40px; border-radius: 8px; }
.emoji-grid button:hover { background: var(--tr); }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-card { background: var(--panel); border-radius: 16px; width: 100%; max-width: 460px; max-height: 88dvh; overflow-y: auto; padding: 16px; box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.x { width: 32px; height: 32px; border-radius: 50%; }
.x:hover { background: var(--tr); }
.x.light { color: #fff; position: absolute; top: calc(10px + env(safe-area-inset-top)); right: 12px; background: rgba(255,255,255,.15); }
.members-list { display: grid; gap: 6px; margin-bottom: 12px; }
.member { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; font-size: 15px; }
.member small { color: var(--muted); display: block; }
.member button { color: var(--danger); font-size: 14px; }
.members-form { display: grid; gap: 8px; }
.members-form input { padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 60; display: grid; place-items: center; }
.lightbox img { max-width: 100vw; max-height: 88dvh; object-fit: contain; }
.lightbox .dl { position: absolute; bottom: calc(14px + env(safe-area-inset-bottom)); color: #fff; background: rgba(255,255,255,.15); padding: 8px 14px; border-radius: 999px; text-decoration: none; font-size: 14px; }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(110px + env(safe-area-inset-bottom)); background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 10px; z-index: 70; font-size: 14px; max-width: 90vw; }

@media (min-width: 861px) { .chat { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }
