:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --text: #1b1f27;
  --text-2: #4d5565;
  --border: #d5d9e0;
  --accent: #3157d5;
  --accent-text: #ffffff;
  --accent-soft: #e3e9fb;
  --danger: #c4312f;
  --ok: #1f7a4d;
  --warn-bg: #fff4d6;
  --warn-text: #6b4a00;
  --focus: #f0a400;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 24, 33, .08), 0 6px 24px rgba(20, 24, 33, .08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101318;
    --surface: #191d24;
    --surface-2: #232833;
    --text: #eef0f4;
    --text-2: #a8b0bf;
    --border: #333a47;
    --accent: #7d9bff;
    --accent-text: #0d1430;
    --accent-soft: #1f2a4a;
    --danger: #ff6b66;
    --ok: #5fd39a;
    --warn-bg: #3a2f12;
    --warn-text: #ffd98a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 24px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}
[hidden] { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip-link { position: absolute; left: 8px; top: -48px; background: var(--surface); color: var(--text); padding: 8px 12px; border-radius: 8px; z-index: 10; }
.skip-link:focus { top: 8px; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
main:focus { outline: none; }

/* The camera feed is only read by the face tracker; it is never shown or sent. */
.camera-feed { position: fixed; left: 0; top: 0; width: 2px; height: 2px; opacity: 0; pointer-events: none; }

/* ---------------------------------------------------------------- chrome */

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
}
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: -.01em; }
.pill {
  font-size: 14px; padding: 4px 12px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-2);
}
.pill[data-state="connected"] { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.pill[data-state="full"], .pill[data-state="reconnecting"] { background: var(--warn-bg); color: var(--warn-text); }

main { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.panel { width: 100%; max-width: 1080px; margin: 0 auto; padding: 8px 16px 32px; }
.panel.narrow { max-width: 620px; padding-top: 6vh; }
h1 { font-size: clamp(28px, 5vw, 40px); line-height: 1.15; letter-spacing: -.02em; margin: 0 0 12px; }
h2 { font-size: 18px; margin: 0 0 12px; }
.lead { font-size: 18px; color: var(--text-2); margin: 0 0 16px; }
.facts { padding-left: 20px; color: var(--text-2); margin: 0 0 28px; }
.facts li { margin: 4px 0; }
.hint { color: var(--text-2); font-size: 14px; margin: 8px 0 0; }
.warn { background: var(--warn-bg); color: var(--warn-text); padding: 8px 12px; border-radius: 10px; font-size: 14px; margin: 8px 0 0; }
.row { display: flex; gap: 12px; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 550; cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.big { min-height: 52px; padding: 12px 28px; font-size: 18px; }
.btn.small { min-height: 36px; padding: 4px 12px; font-size: 14px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------------------------------------------------------------- avatar tiles */

.tile-slot { width: 100%; height: 100%; }
.avatar-holder, .tile-slot { position: relative; }
.avatar-holder { width: 100%; height: 100%; }
.avatar-holder, #remote-avatar {
  background: var(--avatar-bg, var(--surface-2));
  border-radius: inherit;
  overflow: hidden;
  transition: background-color .3s;
}
.avatar-svg { display: block; width: 100%; height: 100%; }

/* ---------------------------------------------------------------- lobby */

.lobby-grid { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 24px; align-items: start; }
.lobby-preview { display: flex; flex-direction: column; gap: 16px; }
.tile-slot.square { aspect-ratio: 1; max-height: 52vh; border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.device-row .btn { width: 100%; }
.invite label { font-weight: 600; font-size: 14px; display: block; margin-bottom: 6px; }
.invite-row { display: flex; gap: 8px; }
.invite-row input {
  flex: 1; min-width: 0; min-height: 44px; padding: 8px 12px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-size: 14px;
}
#join-btn { width: 100%; }

.customize { background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; font-weight: 600; font-size: 14px; }
.field input {
  min-height: 44px; padding: 8px 12px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font: inherit; font-weight: 400;
}
fieldset { border: 0; padding: 0; margin: 0 0 14px; display: flex; flex-wrap: wrap; gap: 6px; }
legend { font-weight: 600; font-size: 14px; padding: 0; margin-bottom: 6px; }
fieldset label { position: relative; cursor: pointer; }
fieldset input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.swatch {
  display: block; width: 36px; height: 36px; border-radius: 50%; background: var(--c);
  border: 2px solid var(--border); box-shadow: inset 0 0 0 3px var(--surface);
}
input:checked + .swatch, input:checked + .sr-only + .swatch { border-color: var(--accent); box-shadow: inset 0 0 0 3px var(--surface), 0 0 0 2px var(--accent); }
.chip {
  display: inline-flex; align-items: center; min-height: 36px; padding: 4px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg); font-size: 14px;
}
input:checked + .chip { background: var(--accent-soft); border-color: var(--accent); color: var(--text); font-weight: 600; }
fieldset input:focus-visible + .swatch, fieldset input:focus-visible + .sr-only + .swatch,
fieldset input:focus-visible + .chip { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------------------------------------------------------------- call */

.call {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  grid-template-rows: minmax(0, 1fr) auto;
  grid-template-areas: "stage chat" "controls chat";
  gap: 12px; padding: 0 16px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.stage { grid-area: stage; position: relative; min-height: 0; }
.tile { position: relative; border-radius: var(--radius); box-shadow: var(--shadow); }
.tile.remote { position: absolute; inset: 0; }
.tile.remote::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  box-shadow: inset 0 0 0 0 var(--ok); transition: box-shadow .15s;
}
.tile.remote.speaking::after { box-shadow: inset 0 0 0 4px var(--ok); }
.tile.self {
  position: absolute; right: 12px; bottom: 12px; width: clamp(110px, 22%, 220px); aspect-ratio: 1;
  border: 3px solid var(--surface); z-index: 2;
}
.tile-label {
  position: absolute; left: 10px; bottom: 10px; display: flex; gap: 6px; align-items: center;
  padding: 4px 10px; border-radius: 999px; font-size: 14px; font-weight: 600;
  background: rgba(15, 18, 24, .72); color: #fff; max-width: calc(100% - 20px);
}
.tile-label span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile.self .tile-label { font-size: 12px; left: 6px; bottom: 6px; }
.muted-badge { background: var(--danger); color: #fff; border-radius: 999px; padding: 0 8px; font-size: 12px; }
.waiting {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; text-align: center; padding: 24px; border-radius: inherit;
  background: color-mix(in srgb, var(--bg) 55%, transparent); backdrop-filter: blur(2px);
}
.waiting p { margin: 0; font-size: 18px; font-weight: 600; max-width: 32ch; }
.sound-btn { position: absolute; left: 50%; top: 16px; transform: translateX(-50%); z-index: 3; }

.controls { grid-area: controls; display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }
.ctl {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-width: 72px; min-height: 60px; padding: 6px 10px; border-radius: 14px; position: relative;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-size: 13px; font-weight: 550; cursor: pointer;
}
.ctl svg { width: 24px; height: 24px; fill: currentColor; }
.ctl:hover { background: var(--surface-2); }
.ctl[aria-pressed="true"] { background: var(--danger); border-color: var(--danger); color: #fff; }
.ctl.danger { color: var(--danger); }
.ctl:disabled { opacity: .5; cursor: not-allowed; }
.unread {
  position: absolute; top: 4px; right: 8px; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: 12px; line-height: 20px;
}
.chat-toggle, .chat-toggle-close { display: none; }

.chat {
  grid-area: chat; display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
}
.chat-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.chat-head h2 { margin: 0; }
.chat-log { list-style: none; margin: 0; padding: 8px 12px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 8px; }
.chat-log li { max-width: 85%; padding: 8px 12px; border-radius: 14px; background: var(--surface-2); overflow-wrap: anywhere; }
.chat-log li.mine { align-self: flex-end; background: var(--accent); color: var(--accent-text); }
.chat-log li.sys { align-self: center; background: none; color: var(--text-2); font-size: 14px; text-align: center; padding: 2px 8px; }
.chat-log .who { display: block; font-size: 12px; font-weight: 700; opacity: .8; }
.chat-log .text { white-space: pre-wrap; }
.chat-log time { display: block; font-size: 11px; opacity: .65; margin-top: 2px; }
.chat-log li.sys time { display: inline; margin-left: 6px; }
.chat-form { display: flex; gap: 8px; padding: 10px 12px 12px; border-top: 1px solid var(--border); }
.chat-form input {
  flex: 1; min-width: 0; min-height: 44px; padding: 8px 12px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit;
}

/* ---------------------------------------------------------------- small screens */

@media (max-width: 860px) {
  .lobby-grid { grid-template-columns: minmax(0, 1fr); }
  .tile-slot.square { max-height: 44vh; margin: 0 auto; width: min(100%, 44vh); }

  .call { grid-template-columns: minmax(0, 1fr); grid-template-areas: "stage" "controls"; padding: 0 12px 12px; }
  .ctl { min-width: 0; flex: 1 1 0; padding: 6px 4px; font-size: 12px; }
  .chat-toggle { display: inline-flex; }
  .chat-toggle-close { display: inline-flex; }
  .chat {
    display: none; position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
    height: min(70dvh, 560px); border-radius: var(--radius) var(--radius) 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.chat-open .chat { display: flex; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ---------------------------------------------------------------- custom SVG avatars */

.custom-svg { border: 1px dashed var(--border); border-radius: 12px; padding: 14px; margin: 0 0 18px; }
.custom-svg h3 { font-size: 16px; margin: 0 0 4px; }
.custom-svg .hint { margin: 0 0 10px; }
.custom-svg .row { gap: 8px; }
.svg-paste { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; font-weight: 600; font-size: 14px; }
.svg-paste textarea {
  font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; padding: 8px 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); resize: vertical;
}
.svg-paste .btn { align-self: flex-start; }
.svg-report:empty { display: none; }
.svg-report { margin-top: 10px; font-size: 14px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); }
.svg-report.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); }
.svg-report.error { background: var(--warn-bg); color: var(--warn-text); }
.svg-report p { margin: 0 0 4px; font-weight: 600; }
.svg-report ul { margin: 4px 0 0; padding-left: 18px; }
.svg-report code { font-size: 12px; }
.ai-help { margin-top: 12px; }
.ai-help summary { cursor: pointer; font-weight: 600; font-size: 14px; padding: 4px 0; }
.ai-help ol { padding-left: 20px; margin: 6px 0 10px; font-size: 14px; }
.ai-help .hint { margin-top: 8px; }
.avatar-holder.drop-target { outline: 3px dashed var(--accent); outline-offset: -6px; }
.customize.using-custom .builtin-only { display: none; }

/* ---------------------------------------------------------------- cast mode */

.lead.small { font-size: 16px; }
.btn .icon { width: 22px; height: 22px; fill: currentColor; }
.cast-panel { display: flex; flex-direction: column; gap: 10px; }
.cast-panel .hint { margin: 0; }
.cast-status:empty { display: none; }
.cast-status { margin: 0; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-size: 15px; }
.cast-status[data-tone="ok"] { background: color-mix(in srgb, var(--ok) 16%, transparent); }
.cast-status[data-tone="warn"] { background: var(--warn-bg); color: var(--warn-text); }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 600; cursor: pointer; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); margin: 0; }
#landing .row { margin-top: 4px; }
