:root {
  color-scheme: dark;
  --bg: #111820; --surface: #192733; --surface-2: #20303c; --raised: #263d4c; --line: #344755; --line-strong: #647987;
  --text: #eef4f9; --muted: #afbfcc; --accent: #a1efce; --accent-ink: #10231b; --mine: #244535;
  --danger: #e2606e; --danger-bg: #562d35; --warn: #f2c14e; --warn-bg: #3d321a; --ok-bg: #1d3a2c;
  --radius: 16px; --top: env(safe-area-inset-top, 0px); --bottom: env(safe-area-inset-bottom, 0px);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); min-height: 100dvh; overflow-x: hidden; }
main { max-width: 560px; margin: auto; padding: calc(28px + var(--top)) 16px calc(32px + var(--bottom)); }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
h1 { font-size: 28px; line-height: 1.15; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 15px; margin: 20px 0 8px; color: var(--muted); font-weight: 600; }
p { margin: 0; }
.note { color: var(--muted); font-size: 14px; }
.icon { width: 24px; height: 24px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

button, input, select, textarea { font: inherit; color: inherit; }
button { border: 1px solid var(--line-strong); border-radius: 12px; background: var(--raised); padding: 11px 16px; cursor: pointer; }
button:disabled { opacity: .45; cursor: default; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible, summary:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.danger { background: var(--danger-bg); border-color: #a86b77; }
input[type=url], input[type=password], textarea, select { background: var(--surface); border: 1px solid var(--line-strong); border-radius: 12px; padding: 11px 12px; max-width: 100%; }
input[type=url], input[type=password] { width: 100%; }
input[type=file] { max-width: 100%; }
label { display: block; font-size: 14px; color: var(--muted); }

/* ---------- Lobby ---------- */
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.logo { width: 52px; height: 52px; padding: 12px; border-radius: 16px; background: var(--ok-bg); color: var(--accent); fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.tagline { color: var(--muted); margin-top: 4px; }
#status { background: var(--surface-2); border-radius: var(--radius); padding: 14px 16px; margin: 0 0 16px; }
.choices { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 0 0 16px; }
.choice { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 18px 8px; border-radius: var(--radius); background: var(--surface); border-color: var(--line); font-weight: 600; font-size: 15px; }
.choice .icon { width: 28px; height: 28px; color: var(--accent); }
#join:not(:disabled) { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
#join:not(:disabled) .icon { color: var(--accent-ink); }
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin: 0 0 16px; background: var(--bg); }
summary { cursor: pointer; font-weight: 600; min-height: 28px; }
#invitation-form { display: flex; gap: 8px; margin: 12px 0 8px; }
#invitation-form input { flex: 1; min-width: 0; }
#invitation-load { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.about > .note { text-align: center; margin: 8px 0 12px; }
.about ul { margin: 12px 0 0; padding-left: 20px; color: var(--muted); font-size: 14px; }
.about li + li { margin-top: 6px; }
.settings p { margin: 0 0 10px; color: var(--muted); font-size: 14px; }
.settings label { margin: 10px 0 6px; }
.backup-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 12px 0; }
.backup-actions label { width: 100%; margin: 4px 0 0; }
#backup-status:empty { display: none; }
#clear-confirm { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 10px; }
#clear-confirm p { width: 100%; }
#settings-close { position: absolute; top: 8px; right: 8px; }

/* ---------- Messages (shared) ---------- */
.chat-section { border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg); margin: 0 0 16px; display: flex; flex-direction: column; overflow: hidden; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
#history-select { flex: 1; min-width: 0; padding: 7px 10px; font-size: 14px; border-radius: 10px; background: var(--surface); }
.icon-button { display: grid; place-items: center; width: 40px; height: 40px; padding: 0; border-radius: 50%; background: transparent; border-color: transparent; flex: none; }
#chat-log { list-style: none; margin: 0; padding: 12px 14px; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 6px; max-height: 360px; }
#chat-log:empty::before { content: "No messages yet"; color: var(--muted); font-size: 14px; text-align: center; padding: 24px 0; }
#chat-log li { align-self: flex-start; max-width: 82%; background: var(--surface-2); border-radius: 18px 18px 18px 6px; padding: 8px 12px 6px; overflow-wrap: anywhere; }
#chat-log li.outgoing { align-self: flex-end; background: var(--mine); border-radius: 18px 18px 6px 18px; }
.message-text { white-space: pre-wrap; margin: 0; }
.message-meta { display: block; text-align: right; font-size: 11px; color: #c1ccd4; margin-top: 2px; font-variant-numeric: tabular-nums; }
#chat-log li[data-status="delivered"].outgoing .message-meta { color: var(--accent); }
#chat-status { font-size: 12px; color: var(--muted); padding: 0 14px 6px; }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 8px 10px; border-top: 1px solid var(--line); background: var(--bg); }
.timer { position: relative; display: grid; place-items: center; width: 44px; height: 44px; border-radius: 50%; background: var(--surface); color: var(--muted); flex: none; margin: 0; }
.timer select { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; padding: 0; }
.timer:focus-within { outline: 3px solid var(--accent); outline-offset: 2px; }
#chat-input { flex: 1; min-width: 0; resize: none; border-radius: 22px; padding: 10px 16px; min-height: 44px; max-height: 160px; line-height: 1.4; field-sizing: content; }
.send { display: grid; place-items: center; width: 44px; height: 44px; padding: 0; border-radius: 50%; background: var(--accent); color: var(--accent-ink); border-color: var(--accent); flex: none; }
.send .icon { width: 20px; height: 20px; }

/* ---------- Session chrome (call + chat) ---------- */
.topbar { display: flex; align-items: center; gap: 10px; }
.verify { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; padding: 8px 14px; border-radius: 14px; background: var(--ok-bg); border: 1px solid #2f5f47; }
.verify .icon { color: var(--accent); }
.verify-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
#verify-digits { font-size: 24px; letter-spacing: .08em; font-variant-numeric: tabular-nums; }
#verify-digits:empty { display: none; }
#verify-label { font-size: 12px; color: var(--muted); }
.verify[data-kind="pending"] { background: var(--surface-2); border-color: var(--line); }
.verify[data-kind="pending"] .icon { color: var(--muted); }
.verify[data-kind="warn"] { background: var(--warn-bg); border-color: #7a6326; }
.verify[data-kind="warn"] .icon, .verify[data-kind="warn"] #verify-label { color: var(--warn); font-weight: 600; }
.menu { position: relative; margin-left: auto; flex: none; }
.menu summary { list-style: none; display: grid; place-items: center; width: 44px; height: 44px; border-radius: 50%; background: var(--surface-2); }
.menu summary::-webkit-details-marker { display: none; }
.menu-panel { position: absolute; right: 0; top: 52px; z-index: 30; min-width: 220px; display: flex; flex-direction: column; padding: 6px; border-radius: 14px; background: var(--surface-2); border: 1px solid var(--line); box-shadow: 0 12px 32px #0009; }
.menu-panel button { background: transparent; border: 0; text-align: left; padding: 12px; border-radius: 10px; }
.menu-panel button:hover:not(:disabled) { background: var(--raised); }
.danger-text { color: #ff9aa5; }
.controls { display: flex; justify-content: center; align-items: flex-start; gap: 14px; }
.round { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 0; border: 0; background: none; font-size: 12px; color: var(--text); }
.round .icon { box-sizing: content-box; padding: 15px; border-radius: 50%; background: #ffffff26; }
.round[aria-pressed="true"] .icon { background: var(--text); color: var(--bg); }
.round.end .icon { background: var(--danger); transform: rotate(135deg); padding: 18px; }
.round[aria-expanded="true"] .icon { background: var(--accent); color: var(--accent-ink); }
.cap { line-height: 1; text-shadow: 0 1px 3px #000a; }
.dialog { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px; background: #0009; }
.dialog-card { width: min(100%, 400px); background: var(--surface-2); border: 1px solid var(--line); border-radius: 20px; padding: 20px; }
.dialog-card p { color: var(--muted); margin: 10px 0 18px; font-size: 15px; }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* ---------- State visibility ---------- */
body:not([data-state="lobby"]) .lobby-only, body[data-state="lobby"] .session-only, body:not([data-state="call"]) .call-only,
body:not([data-state="call"]) .videos, body[data-state="lobby"][data-history="no"] .chat-section { display: none !important; }
body[data-state="lobby"] .composer, body[data-state="lobby"] #chat-status { display: none; }
body:not([data-state="lobby"]):not([data-panel="settings"]) #settings { display: none; }

/* ---------- Call ---------- */
body[data-state="call"] { overflow: hidden; background: #000; }
body[data-state="call"] main { max-width: none; padding: 0; }
body[data-state="call"] .videos { position: fixed; inset: 0; z-index: 1; background: #0b1117; }
body[data-state="call"] .remote { position: absolute; inset: 0; margin: 0; }
body[data-state="call"] #remote { width: 100%; height: 100%; object-fit: cover; display: block; }
body[data-state="call"] .self { position: absolute; z-index: 3; right: 14px; top: calc(var(--top) + 84px); width: 104px; aspect-ratio: 3 / 4; margin: 0; border-radius: 16px; overflow: hidden; border: 2px solid #ffffff40; box-shadow: 0 8px 24px #0008; background: var(--surface); }
body[data-state="call"] #local { width: 100%; height: 100%; object-fit: cover; display: block; transform: scaleX(-1); }
.videos figcaption { display: none; }
body[data-state="call"][data-media="audio"] .videos video, body[data-state="call"][data-media="audio"] .self { display: none; }
body[data-state="call"][data-media="audio"] .remote::before { content: ""; position: absolute; left: 50%; top: 40%; width: 128px; height: 128px; translate: -50% -50%; border-radius: 50%; background: radial-gradient(circle at 50% 38%, #a1efce55 0 22%, transparent 23%), radial-gradient(circle at 50% 100%, #a1efce55 0 42%, transparent 43%), var(--surface-2); box-shadow: 0 0 0 12px #a1efce14, 0 0 0 28px #a1efce0a; }
body[data-state="call"][data-media="audio"] .remote figcaption { display: block; position: absolute; left: 0; right: 0; top: calc(40% + 90px); text-align: center; font-size: 18px; color: var(--muted); background: none; }
body[data-state="call"] .topbar { position: fixed; z-index: 10; top: 0; left: 0; right: 0; padding: calc(var(--top) + 12px) 14px 28px; background: linear-gradient(#000a, #0000); }
body[data-state="call"] .verify { flex: 0 1 auto; background: #0d1a14d9; }
body[data-state="call"] .verify[data-kind="pending"] { background: #111820d9; }
body[data-state="call"] .verify[data-kind="warn"] { background: #3d321ae6; }
body[data-state="call"] .menu summary { background: #111820b3; }
body[data-state="call"] #status { position: fixed; z-index: 9; left: 14px; right: 134px; top: calc(var(--top) + 84px); margin: 0; padding: 6px 12px; width: fit-content; max-width: calc(100% - 148px); font-size: 13px; border-radius: 12px; background: #111820b3; }
body[data-state="call"] .controls { position: fixed; z-index: 10; left: 0; right: 0; bottom: 0; padding: 40px 12px calc(var(--bottom) + 18px); background: linear-gradient(#0000, #000b); }
body[data-state="call"] .chat-section { position: fixed; z-index: 20; left: 0; right: 0; bottom: 0; height: min(72dvh, 640px); margin: 0; border-radius: 22px 22px 0 0; border-bottom: 0; box-shadow: 0 -12px 40px #000a; padding-bottom: var(--bottom); translate: 0 105%; visibility: hidden; }
body[data-state="call"][data-sheet="open"] .chat-section { translate: 0 0; visibility: visible; }
body[data-state="call"] #chat-log { flex: 1; max-height: none; }
body[data-state="call"] #settings { position: fixed; z-index: 40; inset: auto 0 0 0; max-height: 85dvh; overflow-y: auto; margin: 0; border-radius: 22px 22px 0 0; background: var(--surface); padding-bottom: calc(var(--bottom) + 16px); }

/* ---------- Chat only ---------- */
body[data-state="chat"] main { display: flex; flex-direction: column; height: 100dvh; padding: calc(var(--top) + 10px) 0 0; max-width: 720px; }
body[data-state="chat"] .topbar { padding: 0 12px 10px; border-bottom: 1px solid var(--line); }
body[data-state="chat"] .controls { order: -1; position: fixed; z-index: 11; top: calc(var(--top) + 15px); right: max(12px, calc((100vw - 720px) / 2 + 12px)); }
body[data-state="chat"] .topbar { padding-right: 70px; min-height: 74px; }
body:not([data-state="lobby"]) .chat-head:has(#history-select option:only-child) #history-select { display: none; }
body[data-state="call"] .chat-head:has(#history-select option:only-child) #chat-close { margin-left: auto; }
body[data-state="chat"] .chat-head:has(#history-select option:only-child) { display: none; }
body[data-state="chat"] .round.end .icon { padding: 10px; }
body[data-state="chat"] .round .cap { display: none; }
body[data-state="chat"] #status { margin: 8px 12px 0; padding: 6px 12px; font-size: 13px; background: transparent; color: var(--muted); text-align: center; }
body[data-state="chat"] .chat-section { flex: 1; min-height: 0; margin: 0; border: 0; border-radius: 0; }
body[data-state="chat"] .chat-head { border-bottom: 0; padding: 6px 12px; }
body[data-state="chat"] #chat-heading { display: none; }
body[data-state="chat"] #chat-log { flex: 1; max-height: none; }
body[data-state="chat"] .composer { padding-bottom: calc(var(--bottom) + 8px); }
body[data-state="chat"] #settings { position: fixed; z-index: 40; inset: auto 0 0 0; max-height: 85dvh; overflow-y: auto; margin: 0; border-radius: 22px 22px 0 0; background: var(--surface); padding-bottom: calc(var(--bottom) + 16px); }

@media (min-width: 900px) {
  body[data-state="call"] .chat-section { left: auto; top: 0; width: 380px; height: auto; border-radius: 0; border: 0; border-left: 1px solid var(--line); translate: 105% 0; }
  body[data-state="call"][data-sheet="open"] :is(.controls, .topbar) { right: 380px; }
  body[data-state="call"][data-sheet="open"] .self { right: 394px; }
  body[data-state="call"] .self { width: 160px; }
}
@media (prefers-reduced-motion: no-preference) {
  .chat-section { transition: translate .28s cubic-bezier(.2, .8, .2, 1), visibility .28s; }
  .round .icon, .choice, button { transition: background-color .15s, transform .15s; }
  .round:active:not(:disabled) .icon { scale: .94; }
}
