:root {
  --bg: #0f1214;
  --panel: #1a1f23;
  --panel-2: #242b30;
  --line: #333c43;
  --text: #e8ecee;
  --muted: #9aa5ad;
  --accent: #6fb3a2;
  --accent-ink: #0c1a17;
  --danger: #d9655b;
  --warn: #e0a84e;
  --radius: 10px;
  font-family: system-ui, "Segoe UI", sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); overflow: hidden; }
button, input { font: inherit; color: inherit; }
button {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 14px; cursor: pointer; white-space: nowrap;
}
button:hover { border-color: var(--muted); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { border-color: var(--danger); color: var(--danger); }
button.record.active { background: var(--danger); border-color: var(--danger); color: #fff; }
button.off { color: var(--warn); border-color: var(--warn); }
button.small { padding: 5px 10px; font-size: 0.85rem; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
[hidden] { display: none !important; }

.screen { position: fixed; inset: 0; }

/* Join */
#join { display: grid; place-items: center; padding: 16px; overflow: auto; }
.join-card {
  width: min(440px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 24px; display: flex; flex-direction: column; gap: 14px;
}
.join-card h1 { margin: 0; font-size: 1.4rem; }
.join-card p { margin: 0; line-height: 1.45; }
.join-card label { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; color: var(--muted); }
.join-card input {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; color: var(--text);
}
.join-preview { aspect-ratio: 16 / 9; background: #000; border-radius: var(--radius); overflow: hidden; }
.join-preview video { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); }

/* Stage */
#stage { position: absolute; inset: 0; }
.tile { position: absolute; overflow: hidden; background: #000; }
.tile video { position: absolute; left: 0; top: 0; max-width: none; will-change: left, top, width, height; }
.nameplate {
  position: absolute; left: 10px; bottom: 10px; padding: 3px 9px; border-radius: 6px;
  background: rgba(0, 0, 0, 0.55); font-size: 0.85rem; pointer-events: none;
}
.nameplate:empty { display: none; }

/* Eye contact mode: other person fills the window, anchored to the top */
.mode-eye #remoteTile { inset: 0; }
.mode-eye #localTile {
  right: 16px; bottom: 76px; width: clamp(140px, 16vw, 260px); aspect-ratio: 16 / 9;
  z-index: 2;
}
/* Side by side: two equal tiles across the top of the window */
.mode-side #remoteTile { left: 0; top: 0; width: 50%; height: 100%; border-right: 1px solid #000; }
.mode-side #localTile { left: 50%; top: 0; width: 50%; height: 100%; }
#stage.hide-self.mode-eye #localTile { display: none; }
#stage.hide-self.mode-side #localTile { display: none; }
#stage.hide-self.mode-side #remoteTile { width: 100%; }
/* Your own picture is mirrored, as in a mirror. The name stays readable. */
#localTile.mirror { transform: scaleX(-1); }
#localTile.mirror .nameplate { transform: scaleX(-1); left: auto; right: 10px; }

.waiting {
  position: absolute; left: 50%; bottom: 96px; transform: translateX(-50%);
  width: min(460px, calc(100% - 32px)); background: rgba(15, 18, 20, 0.86); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 6px;
  font-size: 0.9rem; line-height: 1.4; z-index: 3;
}
.waiting button { align-self: flex-start; }

/* Bars */
#topbar {
  position: absolute; top: 10px; right: 12px; display: flex; gap: 8px; align-items: center; z-index: 5;
  font-variant-numeric: tabular-nums;
}
#topbar > span { background: rgba(0, 0, 0, 0.55); padding: 4px 10px; border-radius: 6px; font-size: 0.9rem; }
#topbar .conn:empty { display: none; }
.rec { color: #fff; display: inline-flex; gap: 6px; align-items: center; }
.rec i { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); animation: pulse 1.4s infinite; }
@keyframes pulse { 50% { opacity: 0.25; } }
.timer.warn { color: var(--warn); }
.timer.over { color: var(--danger); }

#controls {
  position: absolute; left: 0; right: 0; bottom: 14px; margin: 0 auto; width: fit-content;
  display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; max-width: calc(100% - 24px);
  background: rgba(15, 18, 20, 0.82); border: 1px solid var(--line); border-radius: 12px; padding: 8px; z-index: 6;
  transition: opacity 0.4s;
}
body.idle #controls, body.idle #topbar .conn { opacity: 0; pointer-events: none; }
body.idle { cursor: none; }

.panel {
  position: absolute; right: 16px; top: 52px; width: min(320px, calc(100% - 32px)); z-index: 7;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
}
.panel h2 { margin: 0; font-size: 1rem; }
.panel label { display: flex; flex-direction: column; gap: 6px; font-size: 0.88rem; color: var(--muted); }
.panel label.check { flex-direction: row; align-items: center; color: var(--text); }
.panel input[type=number] { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; width: 90px; }
.panel input[type=range] { width: 100%; accent-color: var(--accent); }
.panel output { color: var(--text); float: right; }

.dialog { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); display: grid; place-items: center; z-index: 20; padding: 16px; }
.dialog-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: min(420px, 100%); }
.dialog-card p { margin: 0 0 16px; line-height: 1.5; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }
.toast {
  position: fixed; left: 50%; top: 52px; transform: translateX(-50%); z-index: 21;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; font-size: 0.9rem;
  max-width: calc(100% - 32px);
}

@media (max-width: 640px) {
  #controls button { padding: 7px 10px; font-size: 0.85rem; }
  .mode-side #remoteTile { width: 100%; height: 50%; border-right: 0; }
  .mode-side #localTile { left: 0; top: 50%; width: 100%; height: 50%; }
}
@media (prefers-reduced-motion: reduce) { .rec i { animation: none; } }

/* Manual framing */
body.manual .tile { cursor: grab; touch-action: none; }
body.manual .tile.dragging { cursor: grabbing; }
.hint { margin: 0; font-size: 0.85rem; color: var(--muted); line-height: 1.4; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
#manualOnly, #followOnly { display: flex; flex-direction: column; gap: 12px; }

/* Pop-out framing preview */
#pipSettings { display: flex; flex-direction: column; gap: 8px; }
#pipCanvas { width: 100%; height: auto; max-height: 220px; object-fit: contain; background: #000; border-radius: 6px; cursor: grab; touch-action: none; align-self: center; }
#pipCanvas.dragging { cursor: grabbing; }
.panel select { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; color: var(--text); }
.panel { max-height: calc(100% - 68px); overflow-y: auto; }

/* Microphone and camera toggles */
.icon-btn { width: 44px; height: 44px; padding: 0; border-radius: 50%; display: inline-grid; place-items: center; }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn .slash { display: none; stroke-width: 2.2; }
.icon-btn.off { background: var(--danger); border-color: var(--danger); color: #fff; }
.icon-btn.off .slash { display: inline; }
#controls { align-items: center; }
button.timer { background: rgba(0, 0, 0, 0.55); border: 0; padding: 4px 10px; border-radius: 6px; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.panel h3 { margin: 4px 0 -4px; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; }
#timerMenu .row button { flex: 1 1 40%; }

/* Join screen: host tools */
.small-text { font-size: 0.85rem; }
.host-row { display: flex; gap: 8px; flex-wrap: wrap; }
.host-row button { flex: 1 1 auto; }
