/* CharChat: mobile first, messaging-app feel. Light and dark follow the system. */
:root {
  --bg: #ece5dd; --panel: #ffffff; --panel-2: #f6f7f8; --header: #0b7a66; --header-2: #09695a; --header-text: #ffffff;
  --text: #111b21; --muted: #667781; --in: #ffffff; --out: #d9fdd3; --accent: #00a884; --accent-2: #008f70;
  --tick: #53bdeb; --border: #e6e9eb; --danger: #d93025;
  --shadow: 0 1px 1.5px rgba(11, 20, 26, .14); --shadow-lg: 0 6px 24px rgba(11, 20, 26, .22);
  --dots: rgba(11, 20, 26, .045); --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a; --panel: #111b21; --panel-2: #1a252c; --header: #1c2b33; --header-2: #17242b; --header-text: #e9edef;
    --text: #e9edef; --muted: #8696a0; --in: #202c33; --out: #005c4b; --border: #222d34; --danger: #f28b82;
    --accent: #00a884; --accent-2: #06cf9c; --dots: rgba(255, 255, 255, .035); --shadow: 0 1px 1.5px rgba(0, 0, 0, .4);
    --shadow-lg: 0 6px 24px rgba(0, 0, 0, .55);
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font: 16px/1.42 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text);
  background: var(--panel); overscroll-behavior: none; -webkit-text-size-adjust: 100%; -webkit-font-smoothing: antialiased;
}
#app {
  height: 100vh; height: 100dvh; max-width: 520px; margin: 0 auto; position: relative; overflow: hidden;
  background: var(--panel); display: flex; flex-direction: column;
}
@media (min-width: 600px) { body { background: #cfd6da; } #app { box-shadow: 0 0 40px rgba(0, 0, 0, .28); } }
@media (min-width: 600px) and (prefers-color-scheme: dark) { body { background: #05090c; } }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
img, video { max-width: 100%; }
.muted { color: var(--muted); }
.icon { display: block; flex: 0 0 auto; }

.screen { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.header {
  flex: 0 0 auto; display: flex; align-items: center; gap: 4px; min-height: 58px; color: var(--header-text);
  background: linear-gradient(180deg, var(--header), var(--header-2)); box-shadow: 0 1px 4px rgba(0, 0, 0, .25); position: relative; z-index: 2;
  padding: env(safe-area-inset-top) calc(8px + env(safe-area-inset-right)) 0 calc(4px + env(safe-area-inset-left));
}
.header .title { flex: 1; margin: 0 0 0 8px; font-size: 20px; font-weight: 600; letter-spacing: .1px; }
.icon-btn { min-width: 44px; min-height: 44px; border: 0; background: transparent; color: inherit; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; }
.icon-btn:active { background: rgba(255, 255, 255, .16); }
.icon-btn.small { min-width: 36px; min-height: 36px; color: var(--muted); }
.icon-btn.small:active { background: rgba(127, 127, 127, .18); }
.text-btn { min-height: 44px; padding: 0 14px; border: 0; background: transparent; color: inherit; font-weight: 600; border-radius: 22px; }
.text-btn:active { background: rgba(255, 255, 255, .16); }
.header-main { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; min-height: 52px; }
.avatar-btn { border: 0; background: transparent; padding: 0; border-radius: 50%; display: inline-flex; }
.avatar-btn:active { opacity: .8; }
.header-text { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; border: 0; background: transparent; color: inherit; text-align: left; padding: 4px 0; min-height: 44px; justify-content: center; }
.header-text .name { font-weight: 600; font-size: 17px; line-height: 1.2; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.header-text .status { font-size: 12.5px; opacity: .82; line-height: 1.3; }
.scroller { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overflow-anchor: none; }
.scroller.plain { background: var(--panel); }

.avatar { flex: 0 0 auto; border-radius: 50%; overflow: hidden; display: inline-flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; box-shadow: 0 0 0 2px rgba(255, 255, 255, .18); }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-slot { display: inline-flex; }

/* login */
.login { align-items: center; justify-content: center; padding: 24px; background: linear-gradient(160deg, var(--header), var(--header-2)); }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; background: var(--panel); padding: 30px 24px; border-radius: 22px; box-shadow: var(--shadow-lg); text-align: center; }
.login-card h1 { margin: 0; font-size: 26px; }
.logo { font-size: 46px; }
input, textarea, select { width: 100%; font-size: 16px; padding: 11px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); color: var(--text); }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.form-error { color: var(--danger); margin: 0; font-size: 14px; text-align: left; }
.btn { min-height: 46px; border-radius: 23px; padding: 0 20px; border: 1px solid var(--border); background: var(--panel); font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 8px; transition: transform .08s ease, background .15s ease; }
.btn:active { transform: scale(.98); background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:active { background: var(--accent-2); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn:disabled { opacity: .55; }

/* chat list */
.row { display: flex; align-items: center; gap: 14px; min-height: 76px; padding: 10px 16px 10px calc(16px + env(safe-area-inset-left)); cursor: pointer; position: relative; }
.row::after { content: ""; position: absolute; left: 78px; right: 0; bottom: 0; height: 1px; background: var(--border); }
.row:active, .row:focus-visible { background: var(--panel-2); outline: none; }
.row .avatar { box-shadow: none; }
.row-main { flex: 1; min-width: 0; }
.row-top, .row-bottom { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.row .name { font-weight: 600; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .time { font-size: 12px; color: var(--muted); flex: 0 0 auto; }
.row .time.unread { color: var(--accent); font-weight: 600; }
.preview { color: var(--muted); font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { flex: 0 0 auto; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; }
.empty { text-align: center; padding: 72px 24px; }
.fab { position: absolute; right: calc(20px + env(safe-area-inset-right)); bottom: calc(22px + env(safe-area-inset-bottom)); width: 58px; height: 58px; border-radius: 18px; border: 0; background: var(--accent); color: #fff; font-size: 32px; line-height: 1; box-shadow: 0 4px 14px rgba(0, 168, 132, .45), 0 2px 6px rgba(0, 0, 0, .25); }
.fab:active { background: var(--accent-2); transform: scale(.96); }

/* chat */
.chat-body { flex: 1; min-height: 0; position: relative; display: flex; }
.chat-body .scroller { flex: 1; }
.chat-bg {
  background-color: var(--bg); padding: 8px calc(10px + env(safe-area-inset-right)) 8px calc(10px + env(safe-area-inset-left));
  background-image: radial-gradient(var(--dots) 1.2px, transparent 1.4px), radial-gradient(var(--dots) 1.2px, transparent 1.4px);
  background-size: 26px 26px; background-position: 0 0, 13px 13px;
}
.messages { display: flex; flex-direction: column; gap: 2px; min-height: 100%; justify-content: flex-end; padding-bottom: 4px; }
.day { align-self: center; background: var(--panel); color: var(--muted); font-size: 12.5px; font-weight: 500; padding: 4px 12px; border-radius: 10px; margin: 12px 0 8px; box-shadow: var(--shadow); }
.bubble { max-width: 82%; padding: 7px 10px 5px; border-radius: 14px; box-shadow: var(--shadow); overflow-wrap: anywhere; position: relative; font-size: 15.5px; -webkit-user-select: text; user-select: text; }
.bubble.in { align-self: flex-start; background: var(--in); margin-right: 14%; }
.bubble.out { align-self: flex-end; background: var(--out); margin-left: 14%; cursor: pointer; }
.bubble.in:not(.bubble.in + .bubble.in), .bubble.out:not(.bubble.out + .bubble.out) { margin-top: 6px; }
.bubble.in:not(.bubble.in + .bubble.in) { border-top-left-radius: 4px; }
.bubble.out:not(.bubble.out + .bubble.out) { border-top-right-radius: 4px; }
.bubble.in:not(.bubble.in + .bubble.in)::before, .bubble.out:not(.bubble.out + .bubble.out)::before {
  content: ""; position: absolute; top: 0; width: 9px; height: 13px;
}
.bubble.in:not(.bubble.in + .bubble.in)::before { left: -8px; background: var(--in); clip-path: polygon(100% 0, 0 0, 100% 100%); }
.bubble.out:not(.bubble.out + .bubble.out)::before { right: -8px; background: var(--out); clip-path: polygon(0 0, 100% 0, 0 100%); }
.bubble .text { white-space: pre-wrap; line-height: 1.38; }
.bubble .meta { float: right; margin: 7px 0 -3px 10px; font-size: 11px; color: var(--muted); user-select: none; -webkit-user-select: none; display: inline-flex; align-items: center; gap: 1px; }
.edited-label { font-style: italic; opacity: .85; margin-right: 2px; }
.tick { font-size: 12px; letter-spacing: -2px; margin-left: 3px; }
.tick.read { color: var(--tick); }
.media-bubble { padding: 3px 3px 5px; }
.media-bubble .meta { display: flex; justify-content: flex-end; float: none; margin: 3px 6px 0 0; }
.media { display: block; border-radius: 11px; max-height: 62vh; width: auto; background: rgba(127, 127, 127, .15); }
img.media { cursor: zoom-in; }
.media-pending, .media-failed { width: 230px; max-width: 100%; height: 150px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; border-radius: 11px; background: rgba(127, 127, 127, .14); color: var(--muted); font-size: 14px; text-align: center; padding: 8px; }
.media-failed { color: var(--danger); height: 80px; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid rgba(127, 127, 127, .35); border-top-color: var(--accent); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.typing { display: flex; gap: 5px; padding: 13px 15px; margin-top: 6px; }
.typing .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing .dot:nth-child(2) { animation-delay: .2s; }
.typing .dot:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-2px); } }
@media (prefers-reduced-motion: reduce) { .spinner, .typing .dot { animation: none; } .btn { transition: none; } }
.jump { position: absolute; right: 14px; bottom: 12px; min-width: 42px; height: 42px; border-radius: 21px; border: 0; background: var(--panel); color: var(--muted); box-shadow: var(--shadow-lg); font-size: 18px; padding: 0 12px; }
.edit-bar { flex: 0 0 auto; display: flex; align-items: center; gap: 10px; background: var(--panel); border-top: 1px solid var(--border); padding: 8px calc(8px + env(safe-area-inset-right)) 8px calc(14px + env(safe-area-inset-left)); border-left: 4px solid var(--accent); }
.edit-icon { color: var(--accent); display: inline-flex; }
.edit-body { flex: 1; min-width: 0; display: flex; flex-direction: column; font-size: 13.5px; }
.edit-body strong { color: var(--accent); font-size: 13px; }
.edit-text { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer { flex: 0 0 auto; display: flex; align-items: flex-end; gap: 8px; background: var(--bg); padding: 6px calc(8px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(8px + env(safe-area-inset-left)); }
.composer textarea { flex: 1; resize: none; max-height: 120px; border: 0; border-radius: 24px; padding: 12px 18px; line-height: 1.3; overflow-y: auto; background: var(--panel); box-shadow: var(--shadow); }
.send { flex: 0 0 auto; width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: inline-flex; align-items: center; justify-content: center; box-shadow: var(--shadow); padding-left: 2px; }
.send:active { background: var(--accent-2); transform: scale(.95); }
.send:disabled { opacity: .45; }

/* bottom sheet (message actions) */
.sheet-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .4); display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: 100%; max-width: 520px; background: var(--panel); border-radius: 20px 20px 0 0; padding: 8px 8px calc(10px + env(safe-area-inset-bottom)); box-shadow: var(--shadow-lg); animation: rise .18s ease-out; }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }
.sheet-preview { margin: 6px 12px 8px; padding: 8px 12px; border-radius: 12px; background: var(--panel-2); color: var(--muted); font-size: 14px; border-left: 3px solid var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-item { width: 100%; min-height: 52px; display: flex; align-items: center; gap: 16px; border: 0; background: transparent; padding: 0 16px; border-radius: 12px; text-align: left; font-size: 16px; }
.sheet-item:active { background: var(--panel-2); }
.sheet-item.danger { color: var(--danger); }

/* editor */
.form { display: flex; flex-direction: column; padding: 18px calc(18px + env(safe-area-inset-right)) calc(28px + env(safe-area-inset-bottom)) calc(18px + env(safe-area-inset-left)); }
.form label { font-size: 13px; color: var(--muted); margin: 16px 0 5px; font-weight: 500; }
.form textarea { min-height: 64px; resize: vertical; }
.form .btn { margin-top: 18px; }
.photo-row { display: flex; align-items: center; gap: 18px; }
.photo-preview { width: 104px; height: 104px; border-radius: 50%; overflow: hidden; flex: 0 0 auto; border: 0; padding: 0; background: transparent; box-shadow: 0 2px 10px rgba(0, 0, 0, .22); }
.photo-preview .avatar { box-shadow: none; }
.photo-preview img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-actions { display: flex; flex-direction: column; gap: 8px; }
.photo-row .btn, .photo-actions .btn { margin: 0; }
.hint { color: var(--muted); font-size: 13px; margin: 4px 0 8px; }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 14px; margin: 16px 0 4px; }
.check input { width: 20px; height: 20px; margin: 1px 0 0; padding: 0; flex: 0 0 auto; accent-color: var(--accent); }
.danger-zone { display: flex; flex-direction: column; gap: 4px; margin-top: 22px; border-top: 1px solid var(--border); padding-top: 6px; }
.gallery-block { margin-top: 28px; padding-top: 18px; border-top: 1px solid var(--border); }
.gallery-block h2 { margin: 0 0 4px; font-size: 18px; }
.gallery-actions { display: flex; gap: 10px; }
.gallery-actions .btn { flex: 1; margin-top: 12px; }
.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 14px; }
.tile { position: relative; aspect-ratio: 1; border: 0; padding: 0; border-radius: 12px; overflow: hidden; background: var(--panel-2); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; color: var(--muted); font-size: 12px; }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile.main { box-shadow: 0 0 0 3px var(--accent); }
.tile.failed { color: var(--danger); }
.tile-badge { position: absolute; top: 6px; right: 6px; width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: #fff; display: inline-flex; align-items: center; justify-content: center; box-shadow: var(--shadow); }
.tile-label { font-size: 12px; }

/* full-screen viewer */
.viewer { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .96); display: flex; flex-direction: column; align-items: center; justify-content: center; }
.viewer-top { position: absolute; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 12px 12px; background: linear-gradient(180deg, rgba(0, 0, 0, .65), transparent); z-index: 1; }
.viewer-close { width: 44px; height: 44px; border-radius: 50%; border: 0; background: transparent; color: #fff; display: inline-flex; align-items: center; justify-content: center; }
.viewer-close:active { background: rgba(255, 255, 255, .16); }
.viewer-caption { color: #fff; font-weight: 600; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-img { max-width: 100%; max-height: 100%; object-fit: contain; flex: 1 1 auto; min-height: 0; width: 100%; }
.viewer-actions { flex: 0 0 auto; display: flex; gap: 10px; justify-content: center; padding: 10px 12px calc(14px + env(safe-area-inset-bottom)); width: 100%; background: linear-gradient(0deg, rgba(0, 0, 0, .7), transparent); }
.viewer-action { min-height: 46px; padding: 0 18px; border-radius: 23px; border: 0; background: rgba(255, 255, 255, .16); color: #fff; font-weight: 600; display: inline-flex; align-items: center; gap: 8px; }
.viewer-action:active { background: rgba(255, 255, 255, .28); }
.viewer-action.danger { color: #ff8a80; }
.toast { position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60; max-width: 86vw; background: #323232; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; box-shadow: var(--shadow-lg); }
