:root {
  --bg: #f5f5f3;
  --surface: #ffffff;
  --text: #0d0d0f;
  --muted: #6e6e73;
  --line: rgba(0, 0, 0, 0.08);
  --accent: #0d0d0f;
  --accent-text: #ffffff;
  --danger: #e5484d;
  --danger-soft: rgba(229, 72, 77, 0.1);
  --live: #30a46c;
  --shadow: rgba(0, 0, 0, 0.35);

  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a0a0b;
    --surface: #161618;
    --text: #f2f2f2;
    --muted: #8e8e93;
    --line: rgba(255, 255, 255, 0.09);
    --accent: #f2f2f2;
    --accent-text: #0a0a0b;
    --danger: #ff6369;
    --danger-soft: rgba(255, 99, 105, 0.14);
    --live: #3dd68c;
    --shadow: rgba(0, 0, 0, 0.8);
  }
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
svg { flex: none; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--text); outline-offset: 3px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---- Brand ---------------------------------------------------------- */

.brand {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 22px);
  left: 0; right: 0;
  display: flex; justify-content: center; align-items: center; gap: 8px;
  font-size: 15px; font-weight: 600; letter-spacing: -0.01em;
  z-index: 2;
  transition: opacity 0.3s var(--ease);
}
.brand-mark {
  position: relative;
  width: 16px; height: 16px; border-radius: 5px;
  background: var(--text);
}
.brand-mark::after {
  content: ""; position: absolute; inset: 4.5px; border-radius: 2px; background: var(--bg);
}
body[data-view="watching"] .brand { opacity: 0; pointer-events: none; }

/* ---- Layout & view transitions -------------------------------------- */

main {
  flex: 1;
  display: grid;
  place-items: center;
  padding: calc(env(safe-area-inset-top) + 84px) 16px calc(env(safe-area-inset-bottom) + 40px);
}

.view {
  width: 100%;
  max-width: 400px;
  display: flex; flex-direction: column; align-items: center;
  text-align: center;
  animation: view-in 0.55s var(--ease) both;
}
.view.is-leaving { animation: view-out 0.18s ease-in both; }

@keyframes view-in {
  from { opacity: 0; transform: translateY(14px) scale(0.985); filter: blur(6px); }
  to   { opacity: 1; transform: none; filter: none; }
}
@keyframes view-out {
  to { opacity: 0; transform: translateY(-8px) scale(0.99); filter: blur(3px); }
}

h1 {
  margin: 0 0 8px;
  font-size: clamp(28px, 7vw, 34px);
  font-weight: 600;
  letter-spacing: -0.03em;
}
.sub { margin: 0; color: var(--muted); font-size: 15px; line-height: 1.45; }

/* ---- PIN entry ------------------------------------------------------- */

.pin-field { position: relative; width: 100%; margin-top: 32px; }

.pin-field input {
  position: absolute; inset: 0; z-index: 1;
  width: 100%; height: 100%;
  opacity: 0; border: 0; outline: 0; background: transparent;
  color: transparent; caret-color: transparent;
  font-size: 16px; /* avoids iOS zoom on focus */
  cursor: text;
}
.pin-field input:disabled { cursor: progress; }

.pin-boxes { display: flex; gap: 8px; }
.pin-boxes span {
  position: relative;
  flex: 1;
  height: 64px;
  display: grid; place-items: center;
  border-radius: 14px;
  background: var(--surface);
  box-shadow: inset 0 0 0 1px var(--line);
  font-size: 28px; font-weight: 500;
  font-variant-numeric: tabular-nums;
  transition: box-shadow 0.25s var(--ease), opacity 0.3s var(--ease), transform 0.25s var(--ease);
}
.pin-boxes span:nth-child(4) { margin-left: 10px; }
.pin-boxes span.filled { animation: pop 0.32s var(--spring); }
.pin-boxes span.active { box-shadow: inset 0 0 0 1.5px var(--text); }
.pin-boxes span.active::after {
  content: ""; position: absolute;
  width: 2px; height: 26px; border-radius: 1px;
  background: var(--text);
  animation: blink 1.1s steps(1) infinite;
}

.pin-field.is-busy span { animation: breathe 1.2s var(--ease) infinite; }
.pin-field.is-busy span:nth-child(2) { animation-delay: 0.08s; }
.pin-field.is-busy span:nth-child(3) { animation-delay: 0.16s; }
.pin-field.is-busy span:nth-child(4) { animation-delay: 0.24s; }
.pin-field.is-busy span:nth-child(5) { animation-delay: 0.32s; }
.pin-field.is-busy span:nth-child(6) { animation-delay: 0.40s; }
.pin-field.shake { animation: shake 0.42s var(--ease); }

@keyframes pop { 0% { transform: scale(0.86); } 60% { transform: scale(1.05); } 100% { transform: none; } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes breathe { 50% { opacity: 0.45; transform: scale(0.96); } }
@keyframes shake {
  20% { transform: translateX(-8px); } 40% { transform: translateX(7px); }
  60% { transform: translateX(-4px); } 80% { transform: translateX(2px); }
}

.status {
  min-height: 20px;
  margin: 14px 0 0;
  font-size: 14px; color: var(--muted);
  transition: color 0.2s;
}
.status.error { color: var(--danger); }

.divider {
  display: flex; align-items: center; gap: 14px;
  width: 100%; margin: 28px 0;
  color: var(--muted); font-size: 13px;
}
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }

/* ---- Buttons --------------------------------------------------------- */

.btn {
  width: 100%; height: 54px;
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  border-radius: 999px;
  font-size: 16px; font-weight: 600; letter-spacing: -0.01em;
  transition: transform 0.2s var(--ease), background-color 0.2s, color 0.2s, box-shadow 0.3s var(--ease), opacity 0.2s;
}
.btn svg { width: 18px; height: 18px; }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover:not(:disabled) { box-shadow: 0 10px 28px -10px var(--shadow); transform: translateY(-1px); }

.host-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 20px; }

.btn-ghost {
  width: auto; height: 42px; padding: 0 18px;
  font-size: 14px; font-weight: 500;
  background: var(--surface); box-shadow: inset 0 0 0 1px var(--line);
}
.btn-ghost svg { width: 16px; height: 16px; }
.btn-ghost:hover { box-shadow: inset 0 0 0 1px var(--muted); }

.btn-stop { background: var(--danger-soft); color: var(--danger); }
.btn-stop:hover { background: var(--danger); color: #fff; }

.hint { margin: 14px 0 0; color: var(--muted); font-size: 13px; line-height: 1.5; }

/* ---- Hosting --------------------------------------------------------- */

.eyebrow {
  display: inline-flex; align-items: center; gap: 8px;
  margin: 0 0 18px;
  color: var(--live);
  font-size: 12px; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase;
}

.live-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--live);
  animation: pulse 2s var(--ease) infinite;
}
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 color-mix(in srgb, var(--live) 55%, transparent); }
  70%  { box-shadow: 0 0 0 8px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}

.pin-display {
  display: flex;
  margin: 0 0 14px;
  font-size: clamp(52px, 17vw, 84px);
  font-weight: 600; letter-spacing: -0.02em; line-height: 1;
  font-variant-numeric: tabular-nums;
}
.pin-display span { display: inline-block; animation: rise 0.7s var(--ease) both; animation-delay: calc(var(--i) * 50ms + 120ms); }
.pin-display .gap { width: 0.28em; }
@keyframes rise { from { opacity: 0; transform: translateY(0.35em); filter: blur(8px); } }

.surface-hint {
  margin: 16px 0 0; padding: 10px 14px;
  border-radius: 12px;
  background: var(--surface); box-shadow: inset 0 0 0 1px var(--line);
  color: var(--muted); font-size: 13px; line-height: 1.45;
  animation: fade-in 0.4s var(--ease) both;
}

.preview {
  width: 100%; aspect-ratio: 16 / 10;
  margin: 28px 0 14px;
  border-radius: 16px; overflow: hidden;
  background: #000;
  box-shadow: inset 0 0 0 1px var(--line), 0 20px 40px -24px var(--shadow);
}
.preview video { display: block; width: 100%; height: 100%; object-fit: contain; }

.viewers {
  margin: 0 0 24px;
  color: var(--muted); font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.viewers.bump { animation: bump 0.4s var(--spring); }

.viewer-list {
  width: 100%;
  margin: 0 0 24px; padding: 0;
  list-style: none;
}
.viewer-list:empty { display: none; }
.viewer-list li {
  display: flex; align-items: center; gap: 12px;
  height: 52px; padding: 0 8px 0 14px;
  border-radius: 14px;
  background: var(--surface);
  box-shadow: inset 0 0 0 1px var(--line);
  font-size: 14px; text-align: left;
  animation: row-in 0.4s var(--ease) both;
}
.viewer-list li + li { margin-top: 6px; }
.viewer-list li.is-leaving { animation: row-out 0.25s var(--ease) both; }
.viewer-list .device { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-list .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--live); flex: none; }
.viewer-list .remove {
  height: 34px; padding: 0 12px; border-radius: 999px;
  color: var(--muted); font-size: 13px; font-weight: 500;
  transition: background-color 0.2s, color 0.2s;
}
.viewer-list .remove:hover { background: var(--danger-soft); color: var(--danger); }
.viewer-list .remove:disabled { opacity: 0.5; cursor: progress; }
@keyframes row-in { from { opacity: 0; transform: translateY(6px); } }
@keyframes row-out { to { opacity: 0; transform: scale(0.97); } }

.host-stats {
  margin: -12px 0 24px;
  color: var(--muted);
  font: 500 12px/1.5 ui-monospace, "SF Mono", Menlo, monospace;
  animation: fade-in 0.3s var(--ease) both;
}
@keyframes bump { 40% { transform: scale(1.06); color: var(--text); } }

/* ---- Watching -------------------------------------------------------- */

.view.watch {
  position: fixed; inset: 0; z-index: 5;
  max-width: none;
  background: #000;
  animation: fade-in 0.4s var(--ease) both;
}
@keyframes fade-in { from { opacity: 0; } }

#remoteVideo {
  width: 100%; height: 100%;
  object-fit: contain;
  opacity: 0; transform: scale(0.98);
  transition: opacity 0.7s var(--ease), transform 0.7s var(--ease);
}
.watch.is-playing #remoteVideo { opacity: 1; transform: none; }

.loader {
  position: absolute; inset: 0;
  display: grid; place-content: center; justify-items: center; gap: 16px;
  color: rgba(255, 255, 255, 0.7); font-size: 14px;
  transition: opacity 0.4s var(--ease);
}
.loader p { margin: 0; }
.watch.is-playing .loader { opacity: 0; pointer-events: none; }

.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.15);
  border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.watch-controls {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 40px;
  background: linear-gradient(rgba(0, 0, 0, 0.55), transparent);
  color: #fff;
  opacity: 0; transform: translateY(-8px); pointer-events: none;
  transition: opacity 0.35s var(--ease), transform 0.35s var(--ease);
}
.watch.controls-visible .watch-controls { opacity: 1; transform: none; pointer-events: auto; }
.watch.controls-hidden { cursor: none; }

.watch-actions { display: flex; gap: 8px; }

.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: rgba(255, 255, 255, 0.14);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  transition: background-color 0.2s, transform 0.2s var(--ease);
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.24); }
.icon-btn:active { transform: scale(0.9); }
.icon-btn svg { width: 18px; height: 18px; }
#soundBtn .icon-sound { display: none; }
#soundBtn.on .icon-sound { display: block; }
#soundBtn.on .icon-muted { display: none; }

.pill {
  display: inline-flex; align-items: center; gap: 8px;
  height: 32px; padding: 0 14px; border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  font-size: 13px; font-weight: 500; font-variant-numeric: tabular-nums;
  --live: #3dd68c;
}

.stats {
  position: absolute; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 8px 14px; border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  color: rgba(255, 255, 255, 0.85);
  font: 500 12px/1.3 ui-monospace, "SF Mono", Menlo, monospace;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  animation: fade-in 0.3s var(--ease) both;
}

/* ---- Ended ----------------------------------------------------------- */

.ended-icon {
  width: 60px; height: 60px; margin-bottom: 24px;
  display: grid; place-items: center;
  border-radius: 18px;
  background: var(--surface); box-shadow: inset 0 0 0 1px var(--line);
  color: var(--muted);
}
.ended-icon svg { width: 26px; height: 26px; }
[data-view="ended"] .btn { margin-top: 32px; }

/* ---- Crop editor ----------------------------------------------------- */

.crop-editor {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center;
  padding: 24px 16px;
  background: rgba(0, 0, 0, 0.74);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  animation: fade-in 0.3s var(--ease) both;
}
.crop-panel {
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  max-width: 100%;
  animation: view-in 0.45s var(--ease) both;
}
.crop-stage {
  --ar: 1.6;
  position: relative;
  width: min(92vw, calc(72vh * var(--ar)));
  aspect-ratio: var(--ar);
  overflow: hidden;
  border-radius: 12px;
  background: #000;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.crop-stage video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: fill;
  pointer-events: none;
}
.crop-box {
  position: absolute;
  border-radius: 6px;
  outline: 2px solid #fff;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.58);
  cursor: move;
  transition: left 0.25s var(--ease), top 0.25s var(--ease), width 0.25s var(--ease), height 0.25s var(--ease);
}
.crop-box.is-dragging { transition: none; }
.crop-box .handle {
  position: absolute;
  width: 18px; height: 18px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.45);
  transform: translate(-50%, -50%);
}
.crop-box .handle[data-corner="nw"] { left: 0; top: 0; cursor: nwse-resize; }
.crop-box .handle[data-corner="ne"] { left: 100%; top: 0; cursor: nesw-resize; }
.crop-box .handle[data-corner="sw"] { left: 0; top: 100%; cursor: nesw-resize; }
.crop-box .handle[data-corner="se"] { left: 100%; top: 100%; cursor: nwse-resize; }

.crop-bar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px;
  width: 100%;
}
.crop-bar .spacer { flex: 1; }
.crop-bar .btn { width: auto; height: 40px; padding: 0 18px; font-size: 14px; }
.crop-bar .btn-ghost { background: rgba(255, 255, 255, 0.12); color: #fff; box-shadow: none; }
.crop-bar .btn-ghost:hover { background: rgba(255, 255, 255, 0.2); box-shadow: none; }
.crop-bar .btn-primary { background: #fff; color: #0a0a0b; }

.segmented {
  display: inline-flex; padding: 3px; border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
}
.segmented button {
  height: 34px; padding: 0 14px; border-radius: 999px;
  color: rgba(255, 255, 255, 0.75); font-size: 13px; font-weight: 500;
  transition: background-color 0.25s var(--ease), color 0.25s var(--ease);
}
.segmented button[aria-checked="true"] { background: #fff; color: #0a0a0b; }

/* ---- Toast ----------------------------------------------------------- */

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px); z-index: 10;
  max-width: calc(100vw - 32px);
  padding: 12px 18px; border-radius: 999px;
  background: var(--text); color: var(--bg);
  font-size: 14px; font-weight: 500;
  opacity: 0; transform: translate(-50%, 16px) scale(0.96);
  pointer-events: none;
  transition: opacity 0.3s var(--ease), transform 0.4s var(--spring);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---- Motion preferences ---------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
