:root {
  --bg: #0f1115;
  --panel: #181b22;
  --panel-2: #222736;
  --border: #2a2f3a;
  --text: #e6e8ee;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --danger: #e5484d;
  --good: #30a46c;
  --warn: #f5a524;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.icon-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; border-bottom: 1px solid var(--border);
}
.brand { color: var(--text); text-decoration: none; font-weight: 600; }
.brand-logo { height: 26px; display: block; }

main {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 20px;
}

.card {
  width: 100%; max-width: 460px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px;
}
h1 { font-size: 22px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 1px; color: var(--accent); }
.error { color: var(--danger); margin: 12px 0 0; }

button {
  font: inherit; padding: 10px 16px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
button svg, .pill svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
button:hover { background: #2a3040; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: transparent; color: #fff; }
button.primary:hover { filter: brightness(.92); }
button.danger { background: var(--danger); border-color: transparent; color: #fff; }
button.active { background: var(--good); border-color: transparent; color: #fff; }
button.wide { width: 100%; }

input {
  font: inherit; padding: 10px 12px; border-radius: 10px; width: 100%;
  border: 1px solid var(--border); background: #0f1218; color: var(--text);
}
input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
input[readonly] { color: var(--muted); }

.row { display: flex; gap: 8px; margin: 12px 0; }
.row input { flex: 1; min-width: 0; }
.field { display: block; margin: 16px 0 8px; font-size: 13px; color: var(--muted); }
.field input { margin-top: 4px; }
.check { display: flex; gap: 8px; align-items: center; margin: 8px 0 18px; }
.check input { width: auto; }
.lobby-camera-box { display: flex; gap: 14px; align-items: flex-start; margin: -7px 0 18px; }
.lobby-camera-box video { width: 168px; aspect-ratio: 16 / 10; object-fit: cover; transform: scaleX(-1); border-radius: 10px; background: #080a0d; }
.camera-preview-settings { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.camera-preview-settings label, .camera-menu > label, .layout-menu > label { color: var(--muted); font-size: 12px; }
select { width: 100%; padding: 9px 10px; border: 1px solid var(--border); border-radius: 9px; background: #0f1218; color: var(--text); font: inherit; }
select:focus { outline: 2px solid var(--accent); }
.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; margin: 22px 0 6px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.linkbox { display: flex; gap: 8px; margin: 10px 0; width: 100%; }
.linkbox input { flex: 1; min-width: 0; font-family: ui-monospace, Menlo, monospace; font-size: 13px; }

.pill { font-size: 12px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); display: flex; align-items: center; gap: 6px; min-width: 0; }
.pill svg { width: 14px; height: 14px; fill: currentColor; stroke: none; }
.pill #statusText { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pill #elapsedTime { padding-left: 7px; border-left: 1px solid currentColor; font-variant-numeric: tabular-nums; opacity: .9; }
.pill.good { border-color: var(--good); color: var(--good); }
.pill.warn { border-color: var(--warn); color: var(--warn); }
.pill.bad { border-color: var(--danger); color: var(--danger); }

/* ---- Call view ---- */
#view-call {
  width: 100%; max-width: 1400px; flex: 1; min-height: 0;
  display: flex; flex-direction: column; gap: 12px;
}
.stage {
  position: relative; flex: 1; min-height: 240px;
  background: #000; border-radius: 14px; overflow: hidden; border: 1px solid var(--border);
}
.stage:fullscreen { border-radius: 0; border: none; }
.remote-surface { position: absolute; inset: 0; transform-origin: center; transition: transform .12s ease-out; }
#remoteVideo { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }
.face-tiles { position: absolute; z-index: 7; top: 62px; right: 12px; width: clamp(120px, 16vw, 186px); display: flex; flex-direction: column; gap: 8px; }
.face-tiles.corner-tl { right: auto; left: 12px; }
.face-tiles.corner-br { top: auto; bottom: 12px; }
.face-tiles.corner-bl { top: auto; right: auto; bottom: 12px; left: 12px; }
.face-tile { position: relative; min-height: 90px; aspect-ratio: 16 / 10; overflow: hidden; border-radius: 10px; background: #171e2a; border: 2px solid rgba(255,255,255,.19); box-shadow: 0 8px 24px rgba(0,0,0,.4); }
.face-tile.speaking { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 35%, transparent), 0 8px 24px rgba(0,0,0,.4); }
.face-tile video { width: 100%; height: 100%; object-fit: cover; display: block; background: #080a0d; }
.self-face-tile video { transform: scaleX(-1); }
.face-avatar { position: absolute; inset: 0; display: grid; place-items: center; background: radial-gradient(circle at 30% 20%, color-mix(in srgb, var(--accent) 65%, #182336), #19202d 85%); color: white; font-size: 32px; font-weight: 650; letter-spacing: 1px; }
.face-name { position: absolute; left: 7px; bottom: 6px; padding: 2px 6px; border-radius: 5px; background: rgba(0,0,0,.65); font-size: 11px; line-height: 1.3; }
.stage.layout-faces .remote-surface { display: none; }
.stage.layout-faces .face-tiles { inset: 12px; width: auto; display: grid; grid-template-columns: minmax(0, 1fr) minmax(180px, 26%); gap: 12px; }
.stage.layout-faces .face-tile { aspect-ratio: auto; min-height: 0; }
.stage.layout-faces .remote-face-tile .face-avatar { font-size: clamp(44px, 8vw, 100px); }
.stage.layout-side .remote-surface { right: 26%; }
.stage.layout-side .face-tiles { inset: 12px 12px 12px auto; width: calc(26% - 24px); display: flex; }
.stage.layout-side .face-tile { flex: 1; aspect-ratio: auto; min-height: 0; }
.stage.layout-side .face-tiles.corner-tl, .stage.layout-side .face-tiles.corner-bl, .stage.layout-side .face-tiles.corner-br { left: auto; top: 12px; bottom: 12px; right: 12px; }
@media (min-width: 950px) {
  .stage.layout-screen.chat-open .face-tiles:not(.corner-tl):not(.corner-bl) { right: 365px; }
}

.placeholder {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  padding: 24px; text-align: center; color: var(--muted); font-size: 16px;
  background: radial-gradient(ellipse at center, #171a21 0%, #0f1115 70%);
  z-index: 3;
}
.placeholder .linkbox { max-width: 520px; }

.pip {
  position: absolute; right: 12px; bottom: 12px;
  width: 240px; max-width: 30%; border-radius: 8px; border: 2px solid var(--border); background: #000; overflow: hidden;
  z-index: 5;
}
.pip video { display: block; width: 100%; }

/* Pointer / drawing overlays */
.annot { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; touch-action: none; }
#remoteAnnot.active { pointer-events: auto; cursor: default; }
#remoteAnnot.active:not([data-tool="pointer"]) { cursor: crosshair; }
.link { color: var(--accent); text-decoration: none; }
.link:hover { text-decoration: underline; }
.host-identity { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.host-identity p { margin: 0; }
.badge {
  position: absolute; left: 12px; top: 12px;
  padding: 4px 10px; border-radius: 8px; background: rgba(0,0,0,.6); font-size: 13px;
  z-index: 6;
}
.unmute { position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 8; }

.annotation-palette, .annotation-style, .stage-utilities {
  position: absolute; z-index: 8; display: flex; padding: 6px; gap: 5px;
  border: 1px solid rgba(255,255,255,.13); border-radius: 12px;
  background: rgba(18,21,28,.9); box-shadow: 0 8px 28px rgba(0,0,0,.35); backdrop-filter: blur(12px);
}
.annotation-palette { left: 12px; top: 50%; flex-direction: column; transform: translateY(-50%); }
.annotation-palette button, .stage-utilities button {
  width: 38px; height: 38px; padding: 0; border-radius: 8px; background: transparent;
}
.annotation-palette button:hover, .stage-utilities button:hover { background: #303747; }
.annotation-palette button.active { background: var(--accent); }
.palette-separator { height: 1px; margin: 3px 4px; background: var(--border); }

.annotation-style { left: 61px; top: 50%; width: 150px; flex-direction: column; transform: translateY(-50%); padding: 10px; }
.color-row { display: flex; gap: 8px; }
.color-swatch { width: 28px; height: 28px; padding: 0; border: 2px solid transparent; border-radius: 50%; }
.color-swatch.red { background: #ff3b30; }
.color-swatch.yellow { background: #ffd60a; }
.color-swatch.blue { background: #3b82f6; }
.color-swatch.active { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }
.width-control, .fade-control { display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12px; }
.width-control { flex-direction: column; align-items: stretch; }
.width-control input { height: 18px; padding: 0; }
.fade-control input { width: auto; margin: 0; }

.stage-utilities { right: 12px; top: 12px; }
.stage-utilities button { width: auto; min-width: 36px; padding: 0 9px; }
.stage-utilities #btnZoomReset { min-width: 58px; font-size: 12px; font-variant-numeric: tabular-nums; }

.chat-panel {
  position: absolute; z-index: 10; right: 12px; top: 62px; bottom: 12px; width: min(340px, calc(100% - 24px));
  display: flex; flex-direction: column; overflow: hidden;
  border: 1px solid var(--border); border-radius: 12px; background: rgba(24,27,34,.97); box-shadow: 0 16px 42px rgba(0,0,0,.5);
}
.chat-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.chat-header button { width: 30px; height: 30px; padding: 0; border: 0; background: transparent; font-size: 22px; }
.chat-messages { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.chat-message { align-self: flex-start; max-width: 90%; padding: 8px 10px; border-radius: 10px 10px 10px 3px; background: var(--panel-2); overflow-wrap: anywhere; }
.chat-message.mine { align-self: flex-end; border-radius: 10px 10px 3px 10px; background: #24477c; }
.chat-message strong { display: block; margin-bottom: 2px; color: var(--muted); font-size: 11px; }
.chat-message span { white-space: pre-wrap; }
.chat-form { display: flex; gap: 7px; padding: 10px; border-top: 1px solid var(--border); }
.chat-form input { min-width: 0; }
.chat-form button { padding-inline: 12px; }
.count-badge { min-width: 18px; height: 18px; padding: 1px 5px; border-radius: 999px; background: var(--danger); color: white; font-size: 11px; line-height: 16px; }

.toolbar { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.toolbar button { min-height: 42px; }
.camera-control, .layout-control { position: relative; display: flex; }
.camera-control #btnCamera { border-radius: 10px 0 0 10px; }
.camera-options-button { min-width: 30px; padding-inline: 5px; border-radius: 0 10px 10px 0; border-left: 0; }
.camera-options-button svg { width: 15px; }
.camera-menu, .layout-menu { position: absolute; z-index: 20; bottom: calc(100% + 9px); left: 0; width: 254px; padding: 14px; display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); box-shadow: 0 12px 35px rgba(0,0,0,.55); }
.camera-menu .check { margin: 4px 0; font-size: 12px; }
.camera-menu .check input { flex: none; }
.camera-menu p { margin: 0; }
.layout-menu { width: 190px; }

@media (max-width: 600px) {
  main { padding: 12px; }
  .card { padding: 20px; }
  .topbar { padding-inline: 12px; gap: 10px; }
  .pill { max-width: 72%; }
  .toolbar { gap: 6px; }
  .toolbar button { flex: 1 1 30%; padding-inline: 9px; }
  .camera-control, .layout-control { flex: 1 1 30%; }
  .camera-control #btnCamera, .layout-control #btnLayout { flex: 1; }
  .camera-menu { left: auto; right: 0; max-width: calc(100vw - 24px); }
  .layout-menu { left: auto; right: 0; }
  .lobby-camera-box { flex-direction: column; }
  .lobby-camera-box video { width: 100%; max-width: 280px; }
  .annotation-palette { left: 8px; right: 8px; top: auto; bottom: 8px; flex-direction: row; transform: none; overflow-x: auto; }
  .annotation-palette button { flex: 0 0 36px; }
  .palette-separator { width: 1px; height: 30px; margin: 3px; flex: none; }
  .annotation-style { left: 8px; top: auto; bottom: 58px; width: 150px; transform: none; }
  .stage-utilities { right: 8px; top: 8px; gap: 2px; }
  .stage-utilities button { min-width: 32px; padding-inline: 7px; }
  .chat-panel { top: 54px; right: 8px; bottom: 8px; width: calc(100% - 16px); }
  .stage.chat-open .face-tiles { visibility: hidden; }
}

@media (max-width: 700px) and (orientation: portrait) {
  .stage.layout-screen .face-tiles, .stage.layout-faces .face-tiles, .stage.layout-side .face-tiles {
    inset: 8px 8px auto; width: auto; height: 100px; display: flex; flex-direction: row; gap: 7px;
  }
  .stage.layout-screen .face-tile, .stage.layout-faces .face-tile, .stage.layout-side .face-tile { flex: 1; min-width: 0; height: 100px; min-height: 0; aspect-ratio: auto; }
  .stage.layout-screen .remote-surface, .stage.layout-side .remote-surface { inset: 116px 0 0; }
  .stage.layout-faces .remote-surface { display: none; }
  .stage-utilities { top: 118px; }
}
