:root {
  --bg: #101418;
  --panel: #1a2027;
  --text: #eef2f5;
  --dim: #95a2ae;
  --accent: #f2b544;
  --live: #4fd18b;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

/* --- top bar ---------------------------------------------------------------- */

#bar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) 10px calc(14px + env(safe-area-inset-left));
  background: rgba(16, 20, 24, 0.92);
  backdrop-filter: blur(8px);
}

#bar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.15rem;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon {
  border: 0;
  background: none;
  font-size: 2rem;
  line-height: 1;
  padding: 0 8px 4px 0;
}

.pill {
  border: 1px solid #36414c;
  background: var(--panel);
  border-radius: 999px;
  padding: 5px 13px;
  font-size: 0.9rem;
}

.pill[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #1b1405;
  font-weight: 650;
}

/* --- the cameras -------------------------------------------------------------- */

#grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 420px), 1fr));
  gap: 10px;
  padding: 4px calc(10px + env(safe-area-inset-right)) calc(14px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
}

.tile {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  border: 0;
  padding: 0;
  width: 100%;
}

.tile img,
.tile cam-stream,
.tile cam-stream video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  /* Photos and streams are all 16:9 (Porchlight re-encodes the video). */
  object-fit: contain;
  display: block;
}

/* The still photo shows at once; the live video fades in over it. */
.tile cam-stream { opacity: 0; transition: opacity 0.35s; }
.tile cam-stream.live { opacity: 1; }

.label {
  position: absolute;
  left: 8px;
  bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  font-size: 0.85rem;
  font-weight: 600;
  pointer-events: none;
}

.label::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--dim);
}

.tile.is-live .label::before { background: var(--live); }

/* ?debug=1 */
.debug {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  padding: 4px 8px;
  background: rgba(0, 0, 0, 0.7);
  color: #ffe08a;
  font: 12px/1.3 ui-monospace, Menlo, monospace;
  text-align: left;
  pointer-events: none;
}

/* One camera, as big as the screen allows. */
body.single #grid {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
  height: calc(100dvh - 58px - env(safe-area-inset-top));
}

/* As big as fits, still 16:9. */
body.single .tile {
  border-radius: 0;
  width: min(100vw, calc((100dvh - 58px - env(safe-area-inset-top)) * 16 / 9));
}

.icon-pill { padding: 5px 9px; line-height: 0; }
.icon-pill svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* Full screen: the camera covers the whole page (and the browser goes full
   screen where it can; iPhones can't, so this works without it). */
body.full #bar { display: none; }
body.single.full .tile {
  position: fixed;
  inset: 0;
  z-index: 20;
  width: 100vw;
  height: 100dvh;
  aspect-ratio: auto;
  border-radius: 0;
}
body.full .label { display: none; }

#exitfull {
  position: fixed;
  z-index: 30;
  right: calc(14px + env(safe-area-inset-right));
  top: calc(14px + env(safe-area-inset-top));
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  font-size: 1.4rem;
  line-height: 1;
  transition: opacity 0.25s;
}
body:not(.controls) #exitfull { opacity: 0; pointer-events: none; }

/* Phone upright (or rotation locked): lay the picture on its side so it
   fills the screen, and turn the phone to watch. If the phone does turn,
   the screen is landscape and none of this applies. */
@media (orientation: portrait) {
  body.full .tile img,
  body.full .tile cam-stream {
    inset: auto;
    top: 50%;
    left: 50%;
    width: 100dvh;
    height: 100vw;
    transform: translate(-50%, -50%) rotate(90deg);
  }
  /* the picture's top-right corner, turned with it */
  #exitfull {
    top: auto;
    bottom: calc(14px + env(safe-area-inset-bottom));
    transform: rotate(90deg);
  }
}

/* A phone on its side, one camera: the picture takes the whole screen and
   the top bar floats over it. Tap the picture to show or hide the bar. */
@media (orientation: landscape) and (max-height: 520px) {
  body.single #bar {
    position: fixed;
    left: 0;
    right: 0;
    background: linear-gradient(rgba(0, 0, 0, 0.65), transparent);
    backdrop-filter: none;
    transition: opacity 0.25s;
  }
  body.single:not(.controls) #bar { opacity: 0; pointer-events: none; }
  body.single #grid { height: 100dvh; }
  body.single .tile { width: min(100vw, calc(100dvh * 16 / 9)); }
  body.single .label { display: none; }
}

/* A TV and its remote (?tv=1): three across, a bright frame on the camera
   the remote is on; one camera takes the whole screen. */
body.tv #bar { display: none; }
body.tv #grid {
  grid-template-columns: repeat(3, 1fr);
  align-content: center;
  min-height: 100vh;
  gap: 30px;
  padding: 40px 56px;
}
body.tv .tile { outline: none; transition: transform 0.15s, box-shadow 0.15s; }
body.tv .tile:focus {
  transform: scale(1.05);
  box-shadow: 0 0 0 8px var(--accent), 0 12px 36px rgba(0, 0, 0, 0.7);
  z-index: 2;
}
body.tv .tile:not(:focus) { opacity: 0.8; }
body.tv .label { font-size: 1.15rem; padding: 5px 14px; left: 12px; bottom: 12px; }
body.tv.single #grid { display: block; padding: 0; height: 100vh; }
body.tv.single .tile {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  aspect-ratio: auto;
  border-radius: 0;
  transform: none;
  box-shadow: none;
}
body.tv.single .tile { opacity: 1; }
body.tv.single .label { display: flex; bottom: 28px; left: 32px; font-size: 1.3rem; }

/* Calendar board and TVs: nothing but the picture. */
body.kiosk #bar { display: none; }
body.kiosk #grid { padding: 0; gap: 4px; }
body.kiosk .tile { border-radius: 0; }
body.kiosk.single #grid { height: 100dvh; }
body.kiosk.single .tile { width: min(100vw, calc(100dvh * 16 / 9)); }

/* --- sign-in -------------------------------------------------------------------- */

#signin {
  max-width: 360px;
  margin: 12vh auto 0;
  padding: 0 24px;
  text-align: center;
}

.mark { width: 72px; height: 72px; fill: var(--accent); }
.mark .lens { fill: var(--bg); }

#signin h2 { margin: 10px 0 6px; font-size: 1.6rem; }
#signin p { color: var(--dim); }

.big {
  width: 100%;
  margin-top: 14px;
  padding: 14px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #1b1405;
  font-weight: 700;
  font-size: 1.05rem;
}

.big:disabled { opacity: 0.6; }

.msg { color: var(--dim); text-align: center; min-height: 1.4em; }
#signin .msg { color: #ffb4a8; }
#offline { padding: 30px 20px; }
