:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-text: #082f49;
  --danger: #f87171;
  --mine: #0e7490;
  --theirs: #334155;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--panel);
}
.brand { color: var(--accent); font-weight: 700; font-size: 1.25rem; text-decoration: none; }
.room-badge { display: flex; align-items: center; gap: 0.5rem; color: var(--muted); white-space: nowrap; }
.room-badge strong { color: var(--text); letter-spacing: 0.15em; }

main { max-width: 1100px; margin: 0 auto; padding: 1rem; }
[hidden] { display: none !important; }

/* Lobby */
.lobby { text-align: center; padding-top: 3rem; }
.lobby h1 { margin: 0 0 0.5rem; }
.lobby p { color: var(--muted); margin: 0 0 1.5rem; }
.join-form { display: flex; justify-content: center; gap: 0.5rem; margin-bottom: 1rem; }
.join-form input {
  width: 8rem;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.3em;
  font-size: 1.5rem;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--panel-2);
  background: var(--panel);
  color: var(--text);
}
.note { color: var(--danger) !important; }

/* Buttons */
button, .button {
  font: inherit;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  border: 0;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}
button:disabled { opacity: 0.5; cursor: default; }
button.small { padding: 0.2rem 0.6rem; font-size: 0.8rem; }
button.link { background: none; color: var(--accent); font-weight: 400; padding: 0; text-decoration: underline; }
button.danger { background: var(--danger); color: #450a0a; }
button.off { background: var(--panel-2); color: var(--text); }

/* Room */
.status { color: var(--muted); margin: 0 0 0.75rem; min-height: 1.4em; }
.videos {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
.video-box {
  min-width: 0; /* a video's intrinsic size must not widen the grid */
  margin: 0;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  aspect-ratio: 4 / 3;
}
.video-box video { width: 100%; height: 100%; object-fit: cover; display: block; }
.video-box.remote { cursor: pointer; }
.video-box.remote:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Focus layout: the other person full width, you as a small picture-in-picture in the corner */
.videos.focus { display: block; position: relative; }
.videos.focus .video-box.remote { aspect-ratio: 16 / 9; width: 100%; max-height: 60vh; } /* keeps the chat in view */
.videos.focus .video-box.local {
  position: absolute;
  right: 0.75rem;
  bottom: 0.75rem;
  width: 24%;
  max-width: 14rem;
  min-width: 6rem;
  aspect-ratio: 4 / 3;
  border: 2px solid rgba(255, 255, 255, 0.7);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  z-index: 2;
}
.videos.focus .video-box.local figcaption { font-size: 0.7rem; padding: 0.1rem 0.4rem; }

.video-box figcaption {
  position: absolute;
  left: 0.5rem;
  bottom: 0.5rem;
  padding: 0.15rem 0.5rem;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 6px;
  font-size: 0.85rem;
}
.controls { display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: center; align-items: center; margin: 0.75rem 0; }
.ctl { display: inline-flex; align-items: center; gap: 0.4rem; background: var(--panel-2); color: var(--text); padding: 0.45rem 0.8rem; }
.ctl .icon { font-size: 1.05rem; line-height: 1; }
.ctl.off { background: var(--panel); color: var(--muted); box-shadow: inset 0 0 0 1px var(--panel-2); }
.ctl.off .icon { opacity: 0.6; }
.ctl.danger { background: var(--danger); color: #450a0a; }
.ctl[aria-expanded="true"], .ctl[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }
.controls input[type="range"] { width: 6rem; accent-color: var(--accent); }
.devices { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; justify-content: center; margin: 0 0 0.75rem; }
.control { display: flex; align-items: center; gap: 0.4rem; color: var(--muted); font-size: 0.85rem; }
.control select {
  font: inherit;
  max-width: 14rem;
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--panel-2);
  background: var(--panel);
  color: var(--text);
}

/* Chat */
.chat {
  background: var(--panel);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  height: 16rem;
}
.messages {
  list-style: none;
  margin: 0;
  padding: 0.75rem;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.messages li {
  max-width: 75%;
  padding: 0.4rem 0.7rem;
  border-radius: 12px;
  background: var(--theirs);
  word-wrap: break-word;
  white-space: pre-wrap;
}
.messages li.mine { background: var(--mine); align-self: flex-end; }
.messages li.system { background: none; color: var(--muted); align-self: center; font-size: 0.85rem; }
.chat-form { display: flex; gap: 0.5rem; padding: 0.5rem; border-top: 1px solid var(--panel-2); }

/* Pictures in the chat: a small preview that opens over the whole window */
.messages li.picture { padding: 0.25rem; flex: 0 0 auto; }
.messages li.picture.sending { opacity: 0.6; }
.messages li.picture.failed { opacity: 0.4; outline: 1px solid var(--danger); }
button.thumb { display: block; background: none; padding: 0; border-radius: 9px; overflow: hidden; cursor: zoom-in; }
button.thumb img { display: block; max-width: min(11rem, 100%); max-height: 9rem; min-width: 3rem; min-height: 3rem; object-fit: cover; }
.viewer {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.94);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}
.viewer img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }
.viewer-close {
  position: absolute;
  top: max(0.75rem, env(safe-area-inset-top));
  right: 0.75rem;
  background: rgba(30, 41, 59, 0.85);
  color: var(--text);
  padding: 0.4rem 0.75rem;
  font-size: 1.1rem;
  line-height: 1;
}

/* Emoji picker */
.emoji-wrap { position: relative; }
.emoji-toggle { background: var(--panel-2); color: var(--text); padding: 0.4rem 0.6rem; font-size: 1.1rem; line-height: 1; }
.emoji-toggle[aria-expanded="true"] { background: var(--accent); }
.emoji-panel {
  position: absolute;
  bottom: calc(100% + 0.5rem);
  left: 0;
  width: 18rem;
  max-width: calc(100vw - 2rem);
  max-height: 14rem;
  overflow-y: auto;
  padding: 0.5rem;
  background: var(--panel-2);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  z-index: 10;
}
.emoji-panel h4 { margin: 0.25rem 0.25rem 0.15rem; font-size: 0.7rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 0.1rem; margin-bottom: 0.35rem; }
.emoji-grid button {
  background: none;
  padding: 0.2rem 0;
  font-size: 1.3rem;
  line-height: 1.3;
  border-radius: 6px;
}
.emoji-grid button:hover, .emoji-grid button:focus-visible { background: var(--panel); outline: none; }
.messages li { font-size: 1rem; }
.chat-form input {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--panel-2);
  background: var(--bg);
  color: var(--text);
}

/* Phones: the room fills the screen with no page scroll, the two videos side by side on
   top, one row of icon buttons, and the chat taking all the height that is left. */
@media (max-width: 640px) {
  .join-form { flex-direction: column; align-items: center; }

  body.in-room { height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }
  body.in-room .top { padding: 0.5rem 0.75rem; flex: 0 0 auto; }
  body.in-room .brand { font-size: 1.05rem; }
  .room-badge { font-size: 0.85rem; }
  .room { flex: 1; min-height: 0; display: flex; flex-direction: column; width: 100%; padding: 0.5rem 0.75rem; }
  .status { font-size: 0.8rem; margin: 0 0 0.5rem; min-height: 0; }
  .videos { gap: 0.5rem; flex: 0 0 auto; }
  .video-box { aspect-ratio: 1 / 1; border-radius: 8px; }
  .videos.focus .video-box.remote { aspect-ratio: 4 / 3; }
  .videos.focus .video-box.local { width: 30%; right: 0.5rem; bottom: 0.5rem; min-width: 5rem; }
  .video-box figcaption { font-size: 0.75rem; left: 0.35rem; bottom: 0.35rem; }
  .controls { gap: 0.35rem; margin: 0.5rem 0; flex-wrap: nowrap; }
  .ctl { padding: 0.5rem; }
  .ctl .label { display: none; }                  /* icons only */
  .ctl .icon { font-size: 1.2rem; }
  .controls input[type="range"] { width: 3.5rem; }
  .devices { flex-direction: column; align-items: stretch; gap: 0.35rem; margin-bottom: 0.5rem; }
  .control { justify-content: space-between; }
  .control select { max-width: 60%; }
  .chat { flex: 1; min-height: 0; height: auto; }
  .emoji-panel { width: 16rem; max-height: 11rem; }
}

/* Phones held sideways: the other person's video (with you in the corner) on the left, the
   chat full height on the right, a slim header. The side-by-side layout is not offered here. */
@media (max-width: 1024px) and (max-height: 520px) and (orientation: landscape) {
  body.in-room { height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }
  body.in-room .top { padding: 0.15rem 0.75rem; flex: 0 0 auto; }
  body.in-room .brand { font-size: 0.95rem; }
  body.in-room .room-badge { font-size: 0.8rem; }
  body.in-room .room-badge button.small { padding: 0.1rem 0.5rem; font-size: 0.7rem; }

  .room {
    flex: 1; min-height: 0; width: 100%; max-width: none; padding: 0.4rem 0.6rem;
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    column-gap: 0.6rem; row-gap: 0.3rem;
  }
  .status { grid-column: 1; grid-row: 1; font-size: 0.75rem; margin: 0; min-height: 0; }
  .videos { grid-column: 1; grid-row: 2; min-height: 0; }
  .controls { grid-column: 1; grid-row: 3; margin: 0; gap: 0.3rem; flex-wrap: nowrap; }
  .ctl { padding: 0.35rem 0.45rem; }
  .ctl .label { display: none; }
  .ctl .icon { font-size: 1.05rem; }
  .controls input[type="range"] { width: 3rem; }
  .devices { grid-column: 1; grid-row: 4; flex-direction: column; align-items: stretch; gap: 0.25rem; margin: 0; }
  .control { justify-content: space-between; font-size: 0.75rem; }
  .control select { max-width: 60%; padding: 0.25rem 0.4rem; font-size: 0.8rem; }
  .chat { grid-column: 2; grid-row: 1 / -1; height: auto; min-height: 0; }
  .messages { padding: 0.5rem; }
  .chat-form { padding: 0.35rem; }
  .emoji-panel { width: 15rem; max-height: 9rem; }

  /* always the picture-in-picture arrangement, filling the height the grid gives it */
  .videos, .videos.focus { display: block; position: relative; height: 100%; }
  .video-box.remote, .videos.focus .video-box.remote { aspect-ratio: auto; width: 100%; height: 100%; max-height: none; border-radius: 8px; }
  .video-box.local, .videos.focus .video-box.local {
    position: absolute; right: 0.4rem; bottom: 0.4rem; width: 28%; min-width: 4.5rem; max-width: 10rem;
    aspect-ratio: 4 / 3; border: 2px solid rgba(255, 255, 255, 0.7); box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5); z-index: 2;
  }
  .video-box.local figcaption { font-size: 0.65rem; padding: 0.1rem 0.35rem; }
  .video-box.remote { cursor: default; }
}

/* Maximized chat (the 💬 button): the room fills the window, both videos shrink to small
   thumbnails in a row, and the chat takes all the height that is left. */
body.chat-max { height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }
body.chat-max .top { flex: 0 0 auto; }
body.chat-max .room { flex: 1; min-height: 0; width: 100%; display: flex; flex-direction: column; }
body.chat-max .videos { display: flex; justify-content: center; gap: 0.5rem; flex: 0 0 auto; height: auto; }
body.chat-max .videos .video-box.local,
body.chat-max .videos .video-box.remote {
  position: relative; right: auto; bottom: auto;
  width: auto; height: 5.5rem; aspect-ratio: 4 / 3;
  min-width: 0; max-width: none; max-height: none;
  border: 0; box-shadow: none; border-radius: 8px;
}
body.chat-max .videos .video-box.remote { cursor: pointer; }
body.chat-max .video-box figcaption { font-size: 0.65rem; padding: 0.05rem 0.35rem; left: 0.25rem; bottom: 0.25rem; max-width: calc(100% - 0.5rem); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
body.chat-max .chat { flex: 1; min-height: 0; height: auto; }

/* ...and on a phone held sideways: a narrow column of thumbnails and controls, the chat beside it */
@media (max-width: 1024px) and (max-height: 520px) and (orientation: landscape) {
  body.chat-max .room { display: grid; grid-template-columns: 8.5rem minmax(0, 1fr); }
  body.chat-max .videos { flex-direction: column; justify-content: flex-start; overflow: hidden; }
  body.chat-max .videos .video-box.local,
  body.chat-max .videos .video-box.remote { width: 100%; height: auto; flex: 0 1 auto; min-height: 0; }
  body.chat-max .controls { flex-wrap: wrap; }
}

/* Footer with the legal links, and the terms page */
.site-footer {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: baseline;
  gap: 0.25rem 1rem;
  padding: 1rem;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}
.site-footer nav { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.25rem 1rem; }
.site-footer a, .agree a, .legal a:not(.button) { color: var(--accent); }
.lobby p.agree { font-size: 0.85rem; margin: 1.5rem 0 0; }

/* in a room the page has no spare height: one slim line of links */
body.in-room .site-footer { padding: 0.25rem 0.75rem 0.4rem; font-size: 0.7rem; }
body.in-room .site-footer .footer-note { display: none; }

.legal { max-width: 46rem; text-align: left; padding-bottom: 2rem; }
.legal h1 { margin: 1rem 0 0.25rem; }
.legal h2 { margin: 2rem 0 0.5rem; font-size: 1.15rem; scroll-margin-top: 1rem; }
.legal p, .legal li { color: var(--text); line-height: 1.6; }
.legal ul { padding-left: 1.25rem; }
.legal .updated { color: var(--muted); font-size: 0.85rem; }
