:root {
  color-scheme: dark;
  --bg: #111;
  --panel: #1d1d1f;
  --line: #333;
  --text: #f2f2f2;
  --dim: #9a9a9a;
  --accent: #4caf50;
  --danger: #e5534b;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea {
  font: inherit;
  color: inherit;
}

button {
  background: #2a2a2d;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
}
button:disabled { opacity: 0.35; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

input, textarea {
  background: #0c0c0d;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  width: 100%;
}
textarea { resize: none; }

[hidden] { display: none !important; }

/* Login */

#login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
#login form {
  width: min(360px, 100%);
  display: grid;
  gap: 14px;
}
#login h1 { margin: 0 0 8px; text-align: center; font-weight: 700; letter-spacing: 0.05em; }
.parents { display: grid; grid-auto-flow: column; gap: 10px; }
.parents button { padding: 18px; font-size: 1.1rem; }
.parents button.selected { border-color: var(--accent); background: #1f3a21; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; text-align: center; }

/* Main */

#main {
  height: 100%;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px 0;
  padding-top: max(6px, env(safe-area-inset-top));
  min-height: 36px;
}
.status { color: var(--dim); font-size: 0.85rem; }
.me { padding: 4px 12px; border-radius: 999px; font-size: 0.85rem; }

#screens {
  flex: 1;
  overflow-y: auto;
  display: grid;
  gap: 8px;
  padding: 8px;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr));
  align-content: start;
}
#screens[data-count="2"], #screens[data-count="3"], #screens[data-count="4"] {
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
}
@media (orientation: portrait) and (max-width: 600px) {
  #screens[data-count="3"], #screens[data-count="4"],
  #screens[data-count="5"], #screens[data-count="6"],
  #screens[data-count="7"], #screens[data-count="8"] {
    grid-template-columns: 1fr 1fr;
  }
}

.tile {
  margin: 0;
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  background: #333;
  border: 3px solid var(--kid);
  aspect-ratio: 16 / 9;
  cursor: pointer;
}
.tile img.screen {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
.tile figcaption {
  position: absolute;
  left: 6px;
  top: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.65);
  border-radius: 999px;
  padding: 2px 10px 2px 2px;
  font-weight: 600;
  font-size: 0.85rem;
}
.tile figcaption:not(:has(.picture:not([hidden]))) { padding-left: 10px; }
.tile figcaption .picture {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--kid);
}
.tile.no-agent figcaption::after { content: "🔕"; font-size: 0.75rem; }
.tile figcaption .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
.badges {
  position: absolute;
  bottom: 6px;
  left: 6px;
  right: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.badge {
  background: rgba(255, 213, 79, 0.92);
  color: #000;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.8rem;
  font-weight: 600;
}

.empty { text-align: center; color: var(--dim); margin: auto; }

.actions {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  padding: 8px 8px calc(8px + var(--safe-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.actions button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 2px;
  font-size: 0.72rem;
  border: none;
  background: transparent;
}
.actions button span { font-size: 1.5rem; }

/* Bottom sheet */

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 88%;
  overflow-y: auto;
  background: var(--panel);
  border-radius: 18px 18px 0 0;
  padding: 8px 16px calc(16px + var(--safe-bottom));
  max-width: 640px;
  margin: 0 auto;
  animation: up 0.18s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.grab { width: 40px; height: 5px; border-radius: 3px; background: #555; margin: 0 auto 10px; }
.sheet-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.sheet-head h2 { margin: 0; font-size: 1.25rem; }
.sheet-head .sub { margin: 2px 0 0; color: var(--dim); font-size: 0.85rem; }
.sheet-head .picture { width: 48px; height: 48px; border-radius: 50%; object-fit: cover; }
.close { margin-left: auto; border-radius: 50%; width: 38px; height: 38px; padding: 0; }
#sheet-body { display: grid; gap: 14px; }
.preview { width: 100%; border-radius: 10px; background: #333; aspect-ratio: 16 / 9; object-fit: contain; }
.note { margin: 0; color: var(--dim); font-size: 0.9rem; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 1; }
.buttons button { padding: 12px 6px; }
.compose, .timer { display: grid; gap: 10px; }
.compose .row button.primary { flex: 0 0 auto; min-width: 100px; }
.quick button { font-size: 0.9rem; }
.timer h3 { margin: 6px 0 0; font-size: 1rem; }
.timer .or { flex: 0 0 auto; color: var(--dim); font-size: 0.85rem; }
.timers { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.timers li {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #2a2a2d;
  border-radius: 10px;
  padding: 6px 6px 6px 12px;
}
.timers li span { flex: 1; font-weight: 600; }
.timers li small { color: var(--dim); }
.timers li button { padding: 6px 10px; }

.switch { display: flex; align-items: center; gap: 10px; flex: 0 0 auto; cursor: pointer; }
.switch input { display: none; }
.switch span {
  width: 44px;
  height: 26px;
  border-radius: 13px;
  background: #444;
  position: relative;
  transition: background 0.15s;
}
.switch span::after {
  content: "";
  position: absolute;
  left: 3px;
  top: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}
.switch input:checked + span { background: var(--danger); }
.switch input:checked + span::after { transform: translateX(18px); }

/* Viewer */

.viewer {
  position: fixed;
  inset: 0;
  background: #000;
  overflow: hidden;
  touch-action: none;
  display: grid;
  place-items: center;
  z-index: 11;
}
.viewer img {
  max-width: 100%;
  max-height: 100%;
  transform-origin: center;
  user-select: none;
  -webkit-user-drag: none;
}
.viewer .close {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: 12px;
  background: rgba(40, 40, 40, 0.8);
}
.viewer .hint {
  position: absolute;
  bottom: calc(10px + var(--safe-bottom));
  margin: 0;
  color: var(--dim);
  font-size: 0.8rem;
  pointer-events: none;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + var(--safe-bottom));
  transform: translate(-50%, 20px);
  background: #333;
  padding: 10px 16px;
  border-radius: 999px;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.alerts {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 8px 0;
  padding: 8px 8px 8px 14px;
  background: #1f3a21;
  border-radius: 12px;
  font-size: 0.9rem;
}
.alerts span { flex: 1; }
.alerts button { padding: 8px 14px; }

/* Calls */

/* The kid's screen on top, as large as fits in half the height, and the parent's
   camera and the controls below. */
.call {
  position: fixed;
  inset: 0;
  background: #000;
  display: flex;
  flex-direction: column;
  z-index: 10;
}
.call-screen {
  position: relative;
  flex: none;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 25%;
  padding-top: env(safe-area-inset-top);
  background: #111;
  cursor: zoom-in;
}
.call-screen img {
  display: block;
  max-width: 100%;
  max-height: calc(50dvh - env(safe-area-inset-top));
}
.call-screen img:not([src]), .call-screen img[src=""] { display: none; }
/* Absolutely placed with auto margins, so the video keeps its shape at the largest
   size that fits. */
.call-self video {
  position: absolute;
  inset: 0;
  margin: auto;
  max-width: 100%;
  max-height: 100%;
}
#call-screen-name {
  position: absolute;
  left: 10px;
  bottom: 8px;
  margin: 0;
  padding: 2px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.6);
  font-weight: 700;
}
#call-screen-name:empty { display: none; }
.call-me {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 12px 20px calc(16px + var(--safe-bottom));
}
/* The camera's whole frame, in its own shape, since that is what the kids see. */
.call-self {
  position: relative;
  flex: 1;
  min-height: 60px;
  align-self: stretch;
}
.call-self video {
  border-radius: 14px;
  transform: scaleX(-1);
}
.call-info { text-align: center; }
.call-info h2 { margin: 0 0 2px; font-size: 1.3rem; }
.call-info p { margin: 0; color: var(--dim); min-height: 1.2em; }
#call-kids { list-style: none; padding: 0; margin: 8px 0 0; display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
#call-kids li { padding: 4px 12px; border-radius: 999px; background: #333; color: var(--dim); }
#call-kids li.joined { background: #1f3a21; color: #fff; }
#call-kids li.shown { box-shadow: 0 0 0 2px #fff; }
#call-kids.pick li { cursor: pointer; }
.call-buttons { display: flex; gap: 16px; width: 100%; max-width: 420px; }
.call-buttons button { flex: 1; padding: 14px; font-size: 1.1rem; border-radius: 999px; }
#call-mute.on { background: #fff; color: #000; }
.hangup { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 700; }

.ring {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: grid;
  place-items: center;
  z-index: 12;
}
.ring-box { text-align: center; width: min(360px, 90vw); display: grid; gap: 16px; }
.ring-icon { font-size: 4rem; margin: 0; animation: wiggle 0.5s infinite alternate; }
@keyframes wiggle { from { transform: rotate(-12deg); } to { transform: rotate(12deg); } }
.ring-box h2 { margin: 0; font-size: 1.6rem; }
.ring-box button { padding: 16px; font-size: 1.1rem; border-radius: 999px; }
