:root {
  --bg: #0f1419; --panel: #1a2029; --line: #2a323d; --text: #e6e9ee; --muted: #8b95a3;
  --me: #2f6fed; --them: #262e39; --accent: #2f6fed; --online: #3ddc84; --offline: #ff5c5c; --pending: #f5a623;
  color-scheme: dark;
}
* { box-sizing: border-box; }
/* The client swaps welcome ⇄ app with the hidden attribute. Our display rules below would otherwise beat the browser's default. */
[hidden] { display: none !important; }
body { margin: 0; font: 15px/1.4 system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); }
input, button { font: inherit; font-size: 16px; border-radius: 8px; border: 1px solid var(--line); padding: 10px 12px; }
input { background: var(--bg); color: var(--text); }
button { background: var(--accent); color: #fff; border: none; cursor: pointer; padding-inline: 16px; }
button:disabled { opacity: .5; cursor: default; }
button.link { background: none; color: var(--muted); padding: 4px 8px; font-size: 14px; }
button.link:hover { color: var(--text); }
button.primary { padding: 6px 14px; font-size: 14px; }
.muted { color: var(--muted); }

/* ---- Welcome ---- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.auth form { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; display: flex; flex-direction: column; gap: 12px; }
.auth h1 { margin: 0; font-size: 22px; }
.hint { margin: 0 0 8px; color: var(--muted); font-size: 13px; }
.auth label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.error { margin: 0; color: #ff7b7b; font-size: 13px; }
.error:empty { display: none; }
.auth .link { align-self: flex-start; padding-inline: 0; margin-top: -4px; }
.open-app { align-self: center; margin-top: 4px; font-size: 13px; color: var(--muted); text-decoration: none; }
.auth input[name="code"] { font-family: ui-monospace, Menlo, monospace; font-size: 20px; letter-spacing: 2px; text-align: center; text-transform: uppercase; }

/* ---- Invite popup ---- */
dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text); padding: 24px; width: min(360px, calc(100vw - 48px)); }
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0; font-size: 18px; }
.code-big { font-family: ui-monospace, Menlo, monospace; font-size: 30px; letter-spacing: 4px; text-align: center; padding: 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); user-select: all; -webkit-user-select: all; }
.row { display: flex; gap: 8px; }
.row button { flex: 1; }
button.secondary { background: var(--them); }
dialog .link { align-self: center; }

/* ---- App shell: two panes on wide screens, one at a time on phones ---- */
.app { display: grid; grid-template-columns: 320px 1fr; height: 100dvh; max-width: 1100px; margin: 0 auto; border-inline: 1px solid var(--line); }
.list, .thread { display: flex; flex-direction: column; min-height: 0; }
.list { border-right: 1px solid var(--line); }
header { display: flex; align-items: center; gap: 8px; padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top)); border-bottom: 1px solid var(--line); }
header strong { font-size: 17px; }
.list header strong { flex: 1; }
.status::before { content: ''; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--pending); }
.status[data-state="online"]::before { background: var(--online); }
.status[data-state="offline"]::before { background: var(--offline); }

/* chat list */
#chats { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#chats li { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer; }
#chats li:hover, #chats li.active { background: var(--panel); }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--them); display: grid; place-items: center; font-weight: 600; position: relative; flex-shrink: 0; }
.avatar::after { content: ''; position: absolute; right: -1px; bottom: -1px; width: 11px; height: 11px; border-radius: 50%; border: 2px solid var(--bg); background: var(--muted); }
.avatar.online::after { background: var(--online); }
.chat-main { flex: 1; min-width: 0; }
.chat-main .name { display: flex; justify-content: space-between; gap: 8px; }
.chat-main .time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.chat-main .preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { margin: 0; padding: 32px 24px; color: var(--muted); text-align: center; font-size: 14px; line-height: 1.5; }
.logout { align-self: center; margin: 8px 0 12px; font-size: 13px; }
.pick { align-self: center; justify-self: center; }

/* thread */
.thread header .peer { display: flex; flex-direction: column; line-height: 1.2; }
.thread header .muted { font-size: 12px; }
.back { display: none; font-size: 26px; line-height: 1; padding: 0 8px 0 0; }
.earlier { align-self: center; margin-top: 8px; }
#messages { flex: 1; list-style: none; margin: 0; padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
#messages li { max-width: 78%; padding: 8px 12px; border-radius: 16px; background: var(--them); align-self: flex-start; border-bottom-left-radius: 4px; word-wrap: break-word; white-space: pre-wrap; }
#messages li.me { background: var(--me); align-self: flex-end; border-bottom-left-radius: 16px; border-bottom-right-radius: 4px; }
#messages li .meta { display: flex; justify-content: flex-end; align-items: center; gap: 6px; margin-top: 2px; font-size: 10px; color: var(--muted); }
#messages li.me .meta { color: rgba(255,255,255,.7); }
#messages li .meta .edited { font-style: italic; }
.typing { height: 18px; padding: 0 16px; font-size: 12px; color: var(--muted); font-style: italic; }
#compose { display: flex; gap: 8px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
#compose input { flex: 1; border-radius: 18px; }
#compose button { border-radius: 18px; }

/* search */
.search-row { padding: 8px 12px; border-bottom: 1px solid var(--line); }
.search-row input { width: 100%; border-radius: 18px; padding: 7px 12px; font-size: 14px; }
#search-results { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#search-results li { padding: 10px 16px; border-bottom: 1px solid var(--line); cursor: pointer; }
#search-results li .name { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; }
#search-results li .preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#search-results li.none { color: var(--muted); text-align: center; cursor: default; }

/* unread */
.badge { background: var(--online); color: #08260f; font-size: 12px; font-weight: 700; border-radius: 10px; padding: 1px 7px; min-width: 20px; text-align: center; margin-left: 6px; }
#chats li.unread .name span:first-child, #chats li.unread .preview { color: var(--text); font-weight: 600; }
.chat-main .preview { display: flex; gap: 4px; align-items: center; }
.chat-main .preview .ticks { flex-shrink: 0; }

/* ticks: ✓ sent, ✓✓ delivered, blue ✓✓ read, 🕓 still sending */
.ticks { font-size: 11px; letter-spacing: -2px; color: var(--muted); }
#messages li.me .ticks { color: rgba(255,255,255,.75); }
.ticks.read { color: #5ac8fa !important; }
#messages li.pending { opacity: .7; }
#messages li.deleted .text { font-style: italic; color: var(--muted); }
#messages li.me.deleted { background: var(--them); }

/* day separators and the "unread" line */
#messages li.day { align-self: center; max-width: none; background: var(--panel); color: var(--muted); font-size: 12px; padding: 4px 10px; border-radius: 10px; white-space: nowrap; }
#messages li.unread-divider { align-self: stretch; max-width: none; background: none; border-radius: 0; text-align: center; color: var(--online); font-size: 12px; padding: 4px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }

/* quoted reply inside a bubble, and the reply/edit strip above the composer */
.quote { border-left: 3px solid var(--online); background: rgba(0,0,0,.22); padding: 4px 8px; border-radius: 6px; margin-bottom: 4px; font-size: 13px; cursor: pointer; }
.quote strong { display: block; font-size: 12px; color: var(--online); }
.quote span { display: block; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
.compose-bar { display: flex; align-items: center; gap: 8px; padding: 8px 16px; border-top: 1px solid var(--line); background: var(--panel); }
.compose-bar-body { flex: 1; min-width: 0; border-left: 3px solid var(--online); padding-left: 8px; font-size: 13px; }
.compose-bar-body strong { display: block; color: var(--online); font-size: 12px; }
.compose-bar-body span { display: block; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* reactions */
.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.chip { background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 1px 7px; font-size: 13px; color: var(--text); }
.chip.mine { border-color: var(--accent); }

/* message menu */
.menu { position: fixed; z-index: 10; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.5); padding: 6px; min-width: 190px; }
.menu-reactions { display: flex; gap: 2px; padding: 2px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu-reactions:empty { display: none; }
.menu-reactions button { background: none; border: none; font-size: 22px; padding: 4px 5px; border-radius: 8px; }
.menu-reactions button.mine { background: var(--them); }
.menu-actions { display: flex; flex-direction: column; }
.menu-actions button { background: none; border: none; color: var(--text); text-align: left; padding: 8px 10px; border-radius: 8px; font-size: 14px; }
.menu-actions button:hover { background: var(--them); }
.menu-actions button.danger { color: #ff7b7b; }
#messages li.msg { -webkit-touch-callout: none; }
#messages li.flash { outline: 2px solid var(--accent); }

/* media */
.avatar.small { width: 32px; height: 32px; font-size: 14px; }
.avatar-btn { background: none; border: none; padding: 0; cursor: pointer; }
.avatar.has-photo { background-size: cover; background-position: center; color: transparent; }
#messages li.media { padding: 4px; }
#messages li.media .text:empty { display: none; }
#messages li.media .text { padding: 4px 8px 0; }
#messages li.media .meta { padding: 0 8px 4px; }
.photo { display: block; max-width: min(100%, 320px); max-height: 320px; border-radius: 12px; cursor: zoom-in; object-fit: cover; }
#messages li audio { display: block; width: 240px; max-width: 100%; margin: 4px 0; }
#messages li video { display: block; max-width: min(100%, 320px); border-radius: 12px; }
.file { display: flex; align-items: center; gap: 8px; padding: 8px 10px; color: inherit; text-decoration: none; background: rgba(0,0,0,.2); border-radius: 10px; }
.file small { color: var(--muted); }
.viewer { background: #000; border: none; padding: 0; max-width: 100vw; max-height: 100vh; width: 100vw; height: 100vh; }
.viewer::backdrop { background: #000; }
.viewer img { width: 100%; height: 100%; object-fit: contain; cursor: zoom-out; }
#compose .icon { font-size: 20px; padding: 6px; }
#compose.recording #text, #compose.recording #attach, #compose.recording #mic, #compose.recording button[type=submit] { display: none; }
.rec-bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-top: 1px solid var(--line); }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--offline); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }
.rec-bar #rec-time { flex: 1; font-variant-numeric: tabular-nums; }
.uploading { padding: 4px 16px; font-size: 12px; color: var(--muted); }

/* jump to latest */
.thread { position: relative; }
.to-bottom { position: absolute; right: 16px; bottom: 100px; width: 40px; height: 40px; border-radius: 50%; padding: 0; background: var(--panel); border: 1px solid var(--line); color: var(--text); font-size: 18px; box-shadow: 0 2px 8px rgba(0,0,0,.4); }
.to-bottom span:not(:empty) { position: absolute; top: -8px; right: -8px; background: var(--online); color: #08260f; font-size: 11px; font-weight: 700; border-radius: 10px; padding: 1px 6px; }

/* forward picker */
.pick-list { list-style: none; margin: 0; padding: 0; max-height: 50vh; overflow-y: auto; }
.pick-list li { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.pick-list li:hover { background: var(--them); }

/* Nothing selected yet: hide the thread pane, show the placeholder */
.app:not(.open) .thread { display: none; }
.app.open .pick { display: none; }

@media (max-width: 700px) {
  .app { grid-template-columns: 1fr; border: none; }
  .list { border: none; }
  .app.open .list, .app:not(.open) .pick { display: none; }
  .back { display: block; }
  #messages li { max-width: 85%; }
}
