/* Mobile-first chat UI (Telegram/WhatsApp style) */
:root {
  --bg: #0e1621; --panel: #17212b; --panel2: #202b36; --line: #0b121a; --text: #e9edf1; --muted: #7f8b97;
  --accent: #2ea6ff; --mine: #2b5278; --theirs: #182533; --green: #4fce5d; --red: #e55;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--panel); overscroll-behavior: none; }
body { font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); overflow: hidden; }
[hidden] { display: none !important; }
/* Fixed to the screen edges: avoids the iOS standalone bug where 100vh/100dvh
   excludes the translucent status bar and leaves an empty strip at the bottom. */
#app { position: fixed; top: var(--app-top, 0px); left: 0; right: 0; bottom: 0; height: var(--app-h, auto); overflow: hidden; }
.pane { position: absolute; inset: 0; display: flex; flex-direction: column; background: var(--panel); min-height: 0; }
#chatPane { z-index: 1; background: var(--bg); transform: translateX(100%); transition: transform .22s ease; }
body.in-chat #chatPane { transform: none; }
h1 { font-size: 1.15rem; margin: 0; flex: 1; }
h2 { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 1rem 0 .4rem; }
ul { list-style: none; margin: 0; padding: 0; }
input, textarea { background: var(--panel2); color: inherit; border: 0; border-radius: 10px; padding: .6rem .8rem; width: 100%; font: inherit; font-size: 16px; outline: none; }
textarea { resize: vertical; }
button { background: var(--accent); color: #fff; border: 0; border-radius: 10px; padding: .6rem 1rem; font: inherit; cursor: pointer; }
button:disabled { opacity: .5; }
button.icon { background: none; color: var(--text); font-size: 1.3rem; padding: .3rem .6rem; line-height: 1; }
button.ghost { background: none; color: var(--accent); padding: .4rem .6rem; }
form { display: flex; gap: .5rem; }
form.col { flex-direction: column; }
label { font-size: .85rem; display: flex; gap: .4rem; align-items: center; margin-top: .6rem; color: var(--muted); }
label input { width: auto; }
.pad { padding: .6rem .8rem; }
.empty { color: var(--muted); text-align: center; padding: 2rem 1.5rem; font-size: .9rem; }
code { background: var(--panel2); padding: 0 .25rem; border-radius: 4px; }

/* Top bars */
.bar { display: flex; align-items: center; gap: .5rem; padding: .5rem .75rem; padding-top: calc(.5rem + env(safe-area-inset-top)); padding-left: max(.75rem, env(safe-area-inset-left)); padding-right: max(.75rem, env(safe-area-inset-right)); background: var(--panel); border-bottom: 1px solid var(--line); min-height: calc(56px + env(safe-area-inset-top)); flex: none; }
.badge { padding: .15rem .6rem; border-radius: 1rem; font-size: .72rem; max-width: 55vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
button.badge { border: 0; font: inherit; font-size: .72rem; cursor: pointer; }
.hint.error { color: #f5a3a3; }
.badge.on { background: #1f4d2c; color: #9be3a6; } .badge.off { background: #4d1f1f; color: #f5a3a3; }
.tabs { display: flex; background: var(--panel); border-bottom: 1px solid var(--line); flex: none; }
.tabs button { flex: 1; background: none; border-radius: 0; color: var(--muted); padding: .7rem 0; border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab { flex: 1; overflow-y: auto; position: relative; min-height: 0; padding-bottom: env(safe-area-inset-bottom); }

/* Chat list rows */
.chatlist li { display: flex; align-items: center; gap: .75rem; padding: .55rem .8rem; cursor: pointer; }
.chatlist li:active, .chatlist li.active { background: #2b5278; }
@media (hover: hover) { .chatlist li:hover:not(.active) { background: var(--panel2); } }
.avatar { width: 48px; height: 48px; border-radius: 50%; flex: none; display: grid; place-items: center; font-weight: 600; font-size: 1.15rem; color: #fff; position: relative; text-transform: uppercase; }
.avatar.sm { width: 38px; height: 38px; font-size: .95rem; }
.avatar .dot { position: absolute; right: 1px; bottom: 1px; width: 12px; height: 12px; border-radius: 50%; background: var(--green); border: 2px solid var(--panel); }
.row-main { flex: 1; min-width: 0; border-bottom: 1px solid var(--line); padding-bottom: .55rem; margin-bottom: -.55rem; }
.row-top, .row-bottom { display: flex; align-items: center; gap: .5rem; height: 28px; line-height: 28px; overflow: hidden; }
.row-top strong { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.row-top time { color: var(--muted); font-size: .75rem; flex: none; }
.row-bottom span.preview { flex: 1; color: var(--muted); font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-bottom .sender { color: var(--text); }
.unread { background: var(--accent); color: #fff; border-radius: 1rem; min-width: 22px; padding: 0 .4rem; font-size: .75rem; text-align: center; line-height: 22px; flex: none; }
.fab { position: absolute; right: 1rem; bottom: calc(1rem + env(safe-area-inset-bottom)); position: fixed; width: 56px; height: 56px; border-radius: 50%; font-size: 1.8rem; padding: 0; box-shadow: 0 4px 12px rgba(0,0,0,.4); z-index: 2; }
body.in-chat .fab { display: none; }
#lookupResults li { cursor: default; align-items: flex-start; }
.chans { display: flex; flex-wrap: wrap; gap: .3rem; margin-top: .3rem; }
.chip { background: var(--panel2); color: var(--accent); border-radius: 1rem; padding: .15rem .6rem; font-size: .8rem; cursor: pointer; }
a.link { color: var(--accent); cursor: pointer; font-size: .85rem; margin-left: .3rem; }

/* Conversation */
.who { flex: 1; display: flex; align-items: center; gap: .6rem; min-width: 0; cursor: pointer; }
.who-text { display: flex; flex-direction: column; min-width: 0; }
.who-text strong, .who-text small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who-text small { color: var(--muted); font-size: .78rem; }
#log { flex: 1; overflow-y: auto; padding: .6rem .6rem 1rem; display: flex; flex-direction: column; gap: 3px; min-height: 0;
  background-color: var(--bg); background-image: radial-gradient(rgba(255,255,255,.025) 1px, transparent 1px); background-size: 18px 18px; overscroll-behavior: contain; }
.msg { max-width: 85%; align-self: flex-start; background: var(--theirs); border-radius: 14px 14px 14px 4px; padding: .35rem .6rem .25rem; word-wrap: break-word; overflow-wrap: anywhere; line-height: 1.35; }
.msg.mine { align-self: flex-end; background: var(--mine); border-radius: 14px 14px 4px 14px; }
.msg.cont { margin-top: -1px; }
.msg .author { display: block; font-size: .82rem; font-weight: 600; cursor: pointer; margin-bottom: 1px; }
.msg .text { white-space: pre-wrap; }
.msg.action .text { font-style: italic; }
.msg.notice { border: 1px dashed #3a4a5c; }
.msg time { float: right; font-size: .68rem; color: var(--muted); margin: .45rem 0 0 .6rem; }
.msg.mine time { color: #8fb3d6; }
.msg .author.me { color: #8fb3d6; cursor: default; text-align: right; }
.event { align-self: center; background: rgba(0,0,0,.35); color: #b9c3cd; border-radius: 1rem; padding: .15rem .7rem; font-size: .76rem; margin: 4px 0; text-align: center; max-width: 90%; }
.event .nick { cursor: pointer; font-weight: 600; }
.day { align-self: center; background: rgba(0,0,0,.45); border-radius: 1rem; padding: .15rem .7rem; font-size: .76rem; margin: 8px 0; }
#sendForm { padding: .5rem; padding-bottom: max(.5rem, env(safe-area-inset-bottom)); background: var(--panel); flex: none; align-items: center; }
#sendInput { border-radius: 22px; }
button.send { width: 44px; height: 44px; border-radius: 50%; padding: 0; flex: none; font-size: 1.1rem; }
#placeholder { margin: auto; }
body.has-chat #placeholder { display: none; }

/* Bottom sheets */
.sheet { position: fixed; top: var(--app-top, 0px); left: 0; right: 0; bottom: 0; height: var(--app-h, auto); background: rgba(0,0,0,.5); z-index: 10; display: flex; align-items: flex-end; }
.sheet-body { background: var(--panel); width: 100%; max-height: 85%; overflow-y: auto; border-radius: 16px 16px 0 0; padding: .5rem 1rem calc(1rem + env(safe-area-inset-bottom)); animation: up .2s ease; }
@keyframes up { from { transform: translateY(100%); } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--panel); padding: .4rem 0; }
.hint { color: var(--muted); font-size: .8rem; }
dl { margin: 0; }
dt { color: var(--muted); font-size: .75rem; margin-top: .6rem; }
dd { margin: 0; word-break: break-word; }
.actions { display: flex; gap: .5rem; margin-top: 1rem; }
.actions button { flex: 1; }
.profile { display: flex; flex-direction: column; align-items: center; gap: .3rem; margin: .5rem 0; }
.profile .avatar { width: 72px; height: 72px; font-size: 1.8rem; }
.profile small { color: var(--muted); }
.sheet .chatlist li { padding-left: 0; padding-right: 0; }

/* Desktop / tablet: two panes side by side */
@media (min-width: 768px) {
  #app { display: grid; grid-template-columns: minmax(300px, 380px) 1fr; }
  .pane { position: relative; inset: auto; }
  #listPane { border-right: 1px solid var(--line); }
  #chatPane, body.in-chat #chatPane { transform: none; transition: none; }
  #backBtn { display: none; }
  .fab, body.in-chat .fab { display: block; position: absolute; }
  .sheet { align-items: center; justify-content: center; }
  .sheet-body { max-width: 440px; border-radius: 16px; }
  .msg { max-width: 65%; }
}
.chips { display: flex; gap: .4rem; padding: 0 .8rem .5rem; flex: none; }
.chips button { background: var(--panel2); color: var(--muted); border-radius: 1rem; padding: .3rem .8rem; font-size: .85rem; }
.chips button.active { background: var(--accent); color: #fff; }
.chips .unread { margin-left: .35rem; }

.tabs .unread { display: inline-block; margin-left: .35rem; line-height: 20px; min-width: 20px; }

/* Discreet mode: a working calculator covers the app until the unlock code is typed + "=" */
#decoy { position: fixed; inset: 0; z-index: 100; background: #000; color: #fff; display: flex; flex-direction: column; justify-content: flex-end;
  padding: env(safe-area-inset-top) 1rem calc(1rem + env(safe-area-inset-bottom)); user-select: none; -webkit-user-select: none; }
#calcDisplay { font-size: 4rem; font-weight: 300; text-align: right; padding: 1rem .5rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#calcKeys { display: grid; grid-template-columns: repeat(4, 1fr); gap: .7rem; max-width: 420px; width: 100%; margin: 0 auto; }
#calcKeys button { aspect-ratio: 1; border-radius: 50%; background: #333; font-size: 1.8rem; padding: 0; }
#calcKeys button.op { background: #ff9f0a; }
#calcKeys button[data-k="C"], #calcKeys button[data-k="±"], #calcKeys button[data-k="%"] { background: #a5a5a5; color: #000; }

/* Swipe-to-delete chat rows */
.chatlist li.swipe { padding: 0; position: relative; overflow: hidden; display: block; isolation: isolate; }
.chatlist li.swipe:active, .chatlist li.swipe.active { background: none; }
.chatlist li.swipe .row { height: 78px; box-sizing: border-box; display: flex; align-items: center; gap: .75rem; padding: .55rem .8rem; background: var(--panel); position: relative; z-index: 1; transition: transform .2s ease; touch-action: pan-y; -webkit-touch-callout: none; user-select: none; -webkit-user-select: none; }
.chatlist li.swipe .row:active, .chatlist li.swipe.active .row { background: #2b5278; }
@media (hover: hover) { .chatlist li.swipe:hover:not(.active) { background: none; } .chatlist li.swipe:hover:not(.active) .row { background: var(--panel2); } }
.swipe-del { position: absolute; top: 0; right: 0; bottom: 0; width: 88px; border-radius: 0; background: #e5484d; color: #fff; font-weight: 600; padding: 0; }
.row-top strong.offline { color: #ff5c5c; }

/* Welcome screen: choose nick + channel */
#welcome { position: fixed; top: var(--app-top, 0px); left: 0; right: 0; bottom: 0; height: var(--app-h, auto); z-index: 50; background: var(--bg); display: flex; overflow-y: auto;
  padding: calc(1rem + env(safe-area-inset-top)) 1rem calc(1rem + env(safe-area-inset-bottom)); }
#welcome[hidden] { display: none; }
.welcome-card { margin: auto; width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: .4rem; text-align: center; }
.welcome-card h1 { margin: .4rem 0 0; font-size: 1.4rem; }
.welcome-card label { margin-top: .8rem; }
.welcome-card .chips { justify-content: center; flex-wrap: wrap; padding: .2rem 0; }
.welcome-card #welcomeGo { margin-top: .8rem; }
.welcome-logo { width: 72px; height: 72px; margin: 0 auto; border-radius: 18px; }

/* Loading spinner */
.spinner { display: inline-block; width: 18px; height: 18px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: middle; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty.loading { display: flex; align-items: center; justify-content: center; gap: .6rem; padding: 1rem; }
button.busy { position: relative; color: transparent; }
button.busy::after { content: ''; position: absolute; left: 50%; top: 50%; width: 16px; height: 16px; margin: -10px 0 0 -10px; border: 2px solid rgba(255,255,255,.35); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }

/* Media messages */
.msg .media-img { display: block; max-width: min(260px, 100%); max-height: 320px; border-radius: 10px; margin-top: 2px; cursor: zoom-in; }
.msg .media-audio { display: block; width: 240px; max-width: 100%; height: 40px; margin-top: 2px; }
.msg .media-fallback { display: inline-block; margin-top: 2px; color: var(--accent); }
#micBtn.rec { color: var(--red); animation: pulse 1s infinite; }
#sendInput.rec::placeholder { color: var(--red); }
@keyframes pulse { 50% { opacity: .4; } }
#lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.92); display: flex; align-items: center; justify-content: center; }
#lightbox img { max-width: 100%; max-height: 100%; }
/* Calls */
#callScreen { position: fixed; inset: 0; z-index: 50; background: #0b121a; display: flex; flex-direction: column; align-items: center; justify-content: space-between; padding: 3rem 1rem max(2rem, env(safe-area-inset-bottom)); }
#callScreen[hidden] { display: none; }
#remoteVideo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
#localVideo { position: absolute; right: 1rem; top: 1rem; width: 28vw; max-width: 160px; border-radius: 10px; background: #000; z-index: 1; }
#remoteVideo:not(.on), #localVideo:not(.on) { display: none; }
.call-info { position: relative; display: flex; flex-direction: column; align-items: center; gap: .4rem; text-shadow: 0 1px 3px #000; }
.call-info .avatar { width: 96px; height: 96px; font-size: 2.4rem; }
#callName { font-size: 1.4rem; }
#callState { color: var(--muted); }
.call-actions { position: relative; display: flex; gap: 2rem; }
button.round { width: 64px; height: 64px; border-radius: 50%; padding: 0; font-size: 1.5rem; }
button.round.accept { background: var(--green); }
button.round.hangup { background: var(--red); }
button.round.ghost { background: rgba(255,255,255,.15); }
button.round.ghost.off { background: #fff; }
/* Hand-made SVG icons (sprite in index.html) */
.ic { width: 1.3em; height: 1.3em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; vertical-align: middle; flex: none; }
button.icon .ic { width: 24px; height: 24px; display: block; }
button.icon { display: inline-flex; align-items: center; justify-content: center; }
button.send, button.round { display: inline-flex; align-items: center; justify-content: center; }
button.send .ic { width: 22px; height: 22px; margin-left: 2px; }
button.round .ic { width: 28px; height: 28px; }
button.round.ghost.off { color: #000; }
#calcKeys button .ic { width: .9em; height: .9em; stroke-width: 2.2; }
.ic-label .ic { width: 1.05em; height: 1.05em; vertical-align: -.18em; margin-right: .3em; }

.atlas-tag { display: inline-block; margin-left: .4rem; padding: 0 .4rem; border-radius: .6rem; background: #1f3f5a; color: #8fd0ff; font-size: .62rem; font-weight: 600; vertical-align: middle; letter-spacing: .02em; }
.atlas-tag[hidden] { display: none; }
#randomBtn.wide { width: 100%; }
#randomBtn.waiting { opacity: .75; }

.mode-form { display: flex; flex-direction: column; gap: .5rem; }
.mode-form label { display: flex; align-items: center; gap: .5rem; }

/* Watch tab */
.row-form { display: flex; gap: .5rem; }
.row-form input { flex: 1; min-width: 0; }
.empty.small { padding: .25rem 1rem .5rem; font-size: .8rem; text-align: left; }
.section-title { margin: .5rem 0 0; font-size: .85rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.watch-log li { cursor: default; font-size: .9rem; }
.watch-log .on { color: #4caf50; }
.watch-log .off { color: #e57373; }
.toast { position: fixed; left: 50%; bottom: calc(1rem + env(safe-area-inset-bottom)); transform: translateX(-50%); max-width: 90vw; background: var(--panel2); color: var(--text); padding: .6rem 1rem; border-radius: 10px; box-shadow: 0 4px 16px #0006; z-index: 1000; font-size: .9rem; }
.restart-btn { width: 100%; margin-top: .25rem; }
