:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #222630;
  --border: #2c313c;
  --text: #f2f4f8;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --accent-strong: #2f6fe8;
  --danger: #e5484d;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

#app { min-height: 100%; }

.view[hidden] { display: none; }

.muted { color: var(--muted); }

.error {
  color: var(--danger);
  margin: 0;
  font-size: 0.95rem;
}

.icon {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

.icon-lg { width: 56px; height: 56px; }

/* ---------- Join screen ---------- */
.view-join {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(60% 80% at 20% 30%, rgba(79, 140, 255, 0.14), transparent 70%),
    radial-gradient(40% 60% at 80% 90%, rgba(229, 72, 77, 0.10), transparent 70%),
    var(--bg);
}

.join-layout {
  width: 100%;
  max-width: 1040px;
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  align-items: center;
  gap: 32px;
}

.join-photo {
  margin: 0;
  display: grid;
  place-items: center;
}

.join-photo img {
  width: 100%;
  max-width: 560px;
  height: auto;
  filter: drop-shadow(0 24px 40px rgba(0, 0, 0, 0.55));
}

.card {
  width: 100%;
  max-width: 440px;
  justify-self: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: var(--shadow);
}

.card h1 { margin: 0 0 8px; font-size: 1.5rem; }
.card p { margin: 0 0 20px; }

.field { margin-bottom: 16px; }

label {
  display: block;
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 6px;
}

input, select {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
}

input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button.primary {
  width: 100%;
  margin-top: 8px;
  padding: 13px 16px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

button.primary:hover { background: var(--accent-strong); }
button.primary:disabled { opacity: 0.6; cursor: wait; }

@media (max-width: 760px) {
  .view-join { padding: 16px; align-items: start; }
  .join-layout { grid-template-columns: 1fr; gap: 8px; }
  .join-photo img { max-height: 220px; width: auto; max-width: 100%; }
  .card { padding: 22px; }
}

/* ---------- Room ---------- */
.view-room {
  position: relative;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  overflow: hidden;
}

.topbar {
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.status {
  margin: 0;
  font-size: 0.95rem;
  color: var(--muted);
}

.notices {
  list-style: none;
  margin: 0;
  padding: 0;
}

.notices:empty { display: none; }

.notices li {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 16px;
  background: rgba(245, 165, 36, 0.12);
  border-bottom: 1px solid rgba(245, 165, 36, 0.3);
  color: #ffd58a;
  font-size: 0.9rem;
}

.notices li .icon { width: 20px; height: 20px; margin-top: 2px; }
.notices li span { flex: 1; }

.notices li button {
  background: none;
  border: 0;
  color: inherit;
  cursor: pointer;
  padding: 0;
  display: flex;
}

/* Stage: the video area. Layouts: solo (only me), pip (peer big + me small), split (side by side). */
.stage {
  position: relative;
  background: #000;
  overflow: hidden;
  min-height: 0;
}

.tile {
  position: absolute;
  overflow: hidden;
  background: #0b0d12;
}

.tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #000;
}

.tile-local video { transform: scaleX(-1); }

.placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  color: var(--muted);
  background: radial-gradient(circle at center, #1c202a, #0a0c10);
}

.placeholder[hidden] { display: none; }

.name-label {
  position: absolute;
  left: 10px;
  top: 10px;
  max-width: calc(100% - 20px);
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 0.8rem;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  backdrop-filter: blur(6px);
}

.name-label:empty { display: none; }

/* solo: my own video full screen */
.stage[data-layout="solo"] .tile-remote { display: none; }
.stage[data-layout="solo"] .tile-local { inset: 0; }
.stage[data-layout="solo"] .tile-local .name-label { display: none; }

/* pip: peer full screen, me in a corner (my label sits at the bottom of the small tile) */
.stage[data-layout="pip"] .tile-remote { inset: 0; }
.stage[data-layout="pip"] .tile-local .name-label { top: auto; bottom: 8px; left: 8px; }
.stage[data-layout="pip"] .tile-local {
  right: 12px;
  top: 12px;
  width: clamp(120px, 22%, 240px);
  aspect-ratio: 4 / 3;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: var(--shadow);
  z-index: 2;
}

/* split: two equal tiles */
.stage[data-layout="split"] .tile-remote { left: 0; top: 0; bottom: 0; width: 50%; }
.stage[data-layout="split"] .tile-local { right: 0; top: 0; bottom: 0; width: 50%; border-left: 2px solid #000; }

.stage:not([data-layout="solo"]) .waiting { display: none; }

.waiting {
  position: absolute;
  left: 50%;
  top: 16px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 0.95rem;
  backdrop-filter: blur(8px);
  z-index: 3;
  white-space: nowrap;
}

.waiting .icon { width: 20px; height: 20px; }

.tap-to-play {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  border: 0;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font: inherit;
  font-size: 1.05rem;
  cursor: pointer;
  z-index: 4;
}

.tap-to-play[hidden] { display: none; }
.tap-to-play .icon { width: 48px; height: 48px; }

/* Captions overlay like film subtitles on the big video */
.captions {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 12px 16px 14px;
  text-align: center;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.85), rgba(0, 0, 0, 0));
  pointer-events: none;
  z-index: 3;
}

.captions p { margin: 0; }

.caption-main {
  font-size: clamp(1.15rem, 2.6vw, 1.7rem);
  font-weight: 600;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.9);
  transition: opacity 0.6s ease;
}

.caption-main:empty, .caption-original:empty, .caption-interim:empty, .own-line:empty { display: none; }

.caption-main.faded, .caption-original.faded { opacity: 0.35; }

.caption-original {
  margin-top: 4px;
  font-size: 0.95rem;
  color: var(--muted);
  transition: opacity 0.6s ease;
}

.caption-interim {
  font-size: 0.95rem;
  color: var(--muted);
  font-style: italic;
}

.own-line {
  margin-bottom: 6px;
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.own-line strong { color: var(--text); font-weight: 600; }

.badge {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 500;
  vertical-align: middle;
  background: rgba(245, 165, 36, 0.2);
  color: #ffd58a;
}

/* Controls */
.controls {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.ctl {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.ctl:hover { background: #2b303b; }
.ctl:disabled { opacity: 0.45; cursor: not-allowed; }
.ctl.small { width: 36px; height: 36px; }
.ctl.small .icon { width: 18px; height: 18px; }

/* Media toggles: "off" state is red; neutral toggles: "on" state is blue */
.ctl:not(.neutral)[aria-pressed="false"] { background: #3a2a2c; border-color: #5a3438; color: #ffb4b7; }
.ctl.neutral[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }

.ctl .when-off { display: none; }
.ctl[aria-pressed="false"] .when-on { display: none; }
.ctl[aria-pressed="false"] .when-off { display: block; }

.ctl.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.ctl.danger:hover { background: #c93a3f; }

/* History panel */
.history {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(360px, 100%);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  z-index: 5;
}

.history[hidden] { display: none; }

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.history-header h2 { margin: 0; font-size: 1.05rem; }

.history > .muted { padding: 16px; margin: 0; }
.history > .muted[hidden] { display: none; }

.history-list {
  list-style: none;
  margin: 0;
  padding: 8px 16px 16px;
  overflow-y: auto;
  flex: 1;
}

.history-list li {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.history-list .who {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.history-list .main { margin: 2px 0 0; }
.history-list .orig { margin: 2px 0 0; font-size: 0.85rem; color: var(--muted); }

/* Mobile: portrait, big video fills, smaller PiP, split becomes stacked */
@media (max-width: 640px) {
  .stage[data-layout="pip"] .tile-local { width: 30%; max-width: 140px; aspect-ratio: 3 / 4; top: auto; bottom: 96px; }
  .stage[data-layout="split"] .tile-remote { left: 0; right: 0; top: 0; bottom: 50%; width: auto; }
  .stage[data-layout="split"] .tile-local { left: 0; right: 0; top: 50%; bottom: 0; width: auto; border-left: 0; border-top: 2px solid #000; }
  .waiting { font-size: 0.85rem; padding: 8px 14px; }
  .history { width: 100%; }
  .ctl { width: 48px; height: 48px; }
  .controls { gap: 8px; }
}

@media (orientation: portrait) and (max-width: 640px) {
  .view-room { grid-template-rows: auto auto 1fr auto; }
}
