:root {
  --bg: #0c1a26;
  --panel: #14293a;
  --text: #f4f7fa;
  --muted: #9fb3c4;
  --primary: #1f8f5f;
  --danger: #c0392b;
  --warn: #e0a526;
  --radius: 18px;
  --gap: 20px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, "Segoe UI", Roboto, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

main {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--gap);
  padding: 24px;
  text-align: center;
}

h1 { margin: 0; font-size: 2rem; }

.clock { font-size: clamp(5rem, 22vw, 16rem); font-weight: 600; line-height: 1; letter-spacing: 0.02em; }
.date { font-size: clamp(1.4rem, 3.5vw, 3rem); color: var(--muted); }
.status { font-size: 1.05rem; color: var(--muted); max-width: 60ch; }

button.big {
  font: inherit;
  font-size: clamp(1.3rem, 2.6vw, 2.4rem);
  font-weight: 600;
  padding: 0.7em 1.6em;
  border: 0;
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  min-width: 9em;
}
button.big:disabled { opacity: 0.45; cursor: default; }
button.big:focus-visible { outline: 4px solid var(--warn); outline-offset: 3px; }
button.primary { background: var(--primary); }
button.danger { background: var(--danger); }

.overlay {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--gap);
  background: var(--bg);
  text-align: center;
}
.ring-name { font-size: clamp(2.4rem, 7vw, 6rem); font-weight: 600; }
.ring-hint { font-size: clamp(1.2rem, 2.5vw, 2rem); color: var(--muted); }

.call { position: fixed; inset: 0; background: #000; }
.call #remote { width: 100%; height: 100%; object-fit: contain; background: #000; }
.call .self {
  position: absolute; right: 16px; top: 16px;
  width: min(24vw, 240px); aspect-ratio: 16 / 9; object-fit: cover;
  border-radius: 12px; border: 2px solid rgba(255, 255, 255, 0.5); background: #111;
  transform: scaleX(-1);
}
.who {
  position: absolute; left: 16px; top: 16px;
  padding: 0.4em 0.8em; border-radius: 12px;
  background: rgba(0, 0, 0, 0.55); font-size: clamp(1rem, 2vw, 1.6rem);
}
.station .call #hangupBtn { position: absolute; left: 50%; bottom: 28px; transform: translateX(-50%); }
.center { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }

.caller .call .bar {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; gap: 12px; justify-content: center;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.caller .call .bar button.big { min-width: 0; flex: 1; max-width: 220px; font-size: 1.2rem; }

/* Trang người con: màn "Bố mẹ đang gọi" phủ lên mọi thứ, kể cả khi đang xem camera. */
.caller .incoming { z-index: 8; padding: 24px; }
.caller .incoming .ring-name { animation: ring-pulse 1.2s ease-in-out infinite; }
.caller .bar-inline { display: flex; gap: 14px; width: 100%; max-width: 460px; }
.caller .bar-inline button.big { flex: 1; min-width: 0; padding: 0.9em 0.4em; }
/* Nhà có nhiều máy bố mẹ: chọn máy để xem camera. */
.caller .pick-title { font-size: clamp(1.6rem, 5vw, 2.6rem); font-weight: 600; margin-bottom: 18px; text-align: center; }
.caller .pick-list { display: flex; flex-direction: column; gap: 12px; width: 100%; max-width: 460px; margin-bottom: 18px; }
.caller .pick-list button.big { width: 100%; }
@keyframes ring-pulse { 50% { opacity: 0.55; } }
@media (prefers-reduced-motion: reduce) { .caller .incoming .ring-name { animation: none; } }
.caller main button.small { font-size: 1.05rem; padding: 0.7em 1.1em; }
/* Khung báo trên trang người gọi nằm sát đáy, dưới các nút và dòng chú thích, để không đè lên chữ nào. */
.caller .notice { bottom: calc(16px + env(safe-area-inset-bottom)); width: min(92vw, 60ch); }
.caller .call ~ .notice, .caller .call:not([hidden]) ~ .notice { bottom: calc(96px + env(safe-area-inset-bottom)); }

/*
 * Màn hình máy bố mẹ: chữ số và nút thật lớn, tính theo kích thước màn hình
 * (lấy số nhỏ hơn giữa bề ngang và bề cao để không tràn ở màn hình nào).
 */
.station .clock { font-size: min(22vw, 32vh); font-weight: 700; }
.station .date { font-size: min(4vw, 6vh); color: var(--text); }

/* Nút gọi từng người con: mỗi người một nút lớn ghi tên, xếp thành hàng, tự xuống dòng khi nhiều người. */
.station .family {
  display: flex; flex-wrap: wrap; justify-content: center;
  gap: min(1.6vw, 2.4vh);
  max-width: 96vw;
}
.station .family button.big {
  font-size: min(4.2vw, 6.5vh);
  padding: 0.42em 1em;
  min-width: 4.5em;
}
.station .family button.child { background: #1d4f73; }
/* Chấm xanh: người này đang mở sẵn trang, gọi là đổ chuông ngay. */
.station .family button.child.online::before {
  content: ''; display: inline-block; vertical-align: middle;
  width: 0.34em; height: 0.34em; margin-right: 0.35em; border-radius: 50%;
  background: #5fe39a;
}
.station .overlay button.big { margin-top: 4vh; }
.station .status { font-size: min(1.6vw, 2.6vh); }
.station button.big {
  font-size: min(5.5vw, 9vh);
  font-weight: 700;
  padding: 0.45em 1.3em;
  border-radius: 0.35em;
  border: 0.06em solid rgba(255, 255, 255, 0.85);
  min-width: 6em;
}
.station .ring-name { font-size: min(9vw, 14vh); font-weight: 700; }
.station .ring-hint { font-size: min(3.6vw, 6vh); color: var(--text); }
.station .who { font-size: min(3vw, 5vh); padding: 0.3em 0.7em; }
.station .notice { font-size: min(3vw, 5vh); top: 3vh; bottom: auto; max-width: 92vw; }
/* Nút kết thúc trên máy bố mẹ: nhỏ, tròn, mờ nhẹ để không che hình người con. */
.station .call #hangupBtn {
  bottom: 3vh;
  width: min(7vw, 11vh);
  height: min(7vw, 11vh);
  min-width: 0;
  padding: 0;
  border-radius: 50%;
  border-width: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.85;
}
.station .call #hangupBtn svg {
  width: 52%;
  height: 52%;
  fill: #fff;
  transform: rotate(135deg); /* ống nghe úp xuống: biểu tượng gác máy */
}
.station .call .self { width: min(20vw, 34vh); }

/* Chế độ chờ của máy bố mẹ: nền đen, chỉ còn đồng hồ mờ, dịch vị trí chậm để không hằn hình. */
body.station #idle { transition: opacity 1.2s ease, transform 2.5s ease; }
body.station.standby { background: #000; cursor: none; }
body.station.standby #idle {
  opacity: var(--standby-opacity, 0.45);
  transform: translate(var(--drift-x, 0vw), var(--drift-y, 0vh));
}
body.station.standby #family,
body.station.standby #noFamily,
body.station.standby #status,
body.station.standby .date { display: none; }
body.station.standby.standby-date .date { display: block; }
/* Máy đang thiếu micro hoặc camera: cảnh báo vẫn hiện trong chế độ chờ, để người nhà đi ngang thấy được. */
body.station.standby.device-missing #status { display: block; }
body.station.device-missing #status { color: var(--warn); }
body.station.standby .badge { opacity: 0.7; }

.hint { font-size: 0.95rem; color: var(--muted); max-width: 34ch; }

/* Chỉ báo nhỏ ở góc màn hình máy bố mẹ khi có người đang xem. */
.badge {
  position: fixed; left: 16px; top: 16px; z-index: 4;
  padding: 0.35em 0.8em; border-radius: 999px;
  background: var(--panel); color: var(--warn);
  font-size: clamp(0.95rem, 1.6vw, 1.4rem);
}

.banner {
  padding: 0.8em 1.2em; border-radius: var(--radius);
  background: var(--warn); color: #1c1400; font-size: 1.3rem; font-weight: 600;
}

.notice {
  position: fixed; left: 50%; bottom: 110px; transform: translateX(-50%);
  max-width: min(90vw, 60ch);
  padding: 0.7em 1.1em; border-radius: 12px;
  background: rgba(20, 41, 58, 0.95); border: 1px solid var(--warn);
  font-size: 1.1rem; text-align: center; z-index: 5;
}

.stats {
  position: fixed; left: 12px; bottom: 12px; margin: 0;
  padding: 8px 10px; border-radius: 8px;
  background: rgba(0, 0, 0, 0.7); color: #c8f7c5;
  font: 12px/1.4 ui-monospace, Consolas, monospace; z-index: 6;
}
/* Trên điện thoại, đáy màn hình là chỗ của các nút: đưa bảng số đo lên dưới dòng trạng thái. */
.caller .stats { top: 84px; bottom: auto; }

/* ---------- Trang nhập mã mời và trang quản trị ---------- */
body.pair main, body.admin main { justify-content: flex-start; padding-top: 6vh; height: auto; min-height: 100%; }
body.admin main { align-items: stretch; max-width: 720px; margin: 0 auto; text-align: left; }
body.admin a, body.pair a { color: #8fd3b0; }

.card {
  width: 100%; max-width: 520px;
  display: flex; flex-direction: column; gap: 14px;
  padding: 20px; border-radius: var(--radius);
  background: var(--panel);
}
body.admin .card { max-width: none; margin-bottom: 16px; }
.card h2 { margin: 0; font-size: 1.25rem; }
.lead { margin: 0; font-size: 1.05rem; line-height: 1.5; color: var(--text); }
.muted { margin: 0; color: var(--muted); }

.card label { display: flex; flex-direction: column; gap: 6px; font-size: 1rem; color: var(--muted); }
.card label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text); }
.card input:not([type="checkbox"]), .card select {
  font: inherit; font-size: 1.15rem; padding: 0.6em 0.7em;
  border-radius: 12px; border: 1px solid #3a566b; background: #0c1a26; color: var(--text);
}
.card input[type="checkbox"] { width: 22px; height: 22px; }
.card input:focus-visible, .card select:focus-visible { outline: 3px solid var(--warn); outline-offset: 2px; }

.code-input { text-align: center; font-size: 2.2rem !important; letter-spacing: 0.12em; font-variant-numeric: tabular-nums; }
.form-error { padding: 0.6em 0.8em; border-radius: 12px; border: 1px solid var(--danger); color: #ffd7d2; }
.button-link { display: inline-block; text-align: center; text-decoration: none; color: var(--text) !important; }
a.big { font-size: 1.3rem; font-weight: 600; padding: 0.7em 1.6em; border-radius: var(--radius); background: var(--primary); }
.quiet-link { color: var(--muted); font-size: 0.95rem; }

.invite { border: 2px solid var(--warn); }
.invite-code { font-size: clamp(2.4rem, 10vw, 4rem); font-weight: 700; letter-spacing: 0.1em; text-align: center; font-variant-numeric: tabular-nums; }
.invite-text { margin: 0; padding: 12px; border-radius: 12px; background: #0f2231; font-size: 0.98rem; line-height: 1.5; word-break: break-word; user-select: all; }
.invite .actions button.small { flex: 1; font-size: 1.05rem; padding: 0.75em 0.9em; }

.tag { font-size: 0.8rem; font-weight: 600; padding: 0.15em 0.6em; border-radius: 999px; background: #24455c; color: var(--muted); vertical-align: middle; }
.tag.me { background: var(--primary); color: #fff; }
.device { display: flex; gap: 12px; align-items: center; justify-content: space-between; padding: 10px 12px; border-radius: 12px; background: #0f2231; }
.device-info { font-size: 0.98rem; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }
button.small {
  font: inherit; font-size: 0.98rem; font-weight: 600; padding: 0.5em 0.9em;
  border: 0; border-radius: 10px; background: #24455c; color: var(--text); cursor: pointer; white-space: nowrap;
}
button.small.danger { background: var(--danger); }
a.small.button-link { font-size: 0.98rem; font-weight: 600; padding: 0.5em 0.9em; border-radius: 10px; background: #24455c; white-space: nowrap; }
#recordingsList .device { flex-wrap: wrap; margin-bottom: 8px; }
/* Đặt tên và loại cho một máy bố mẹ (ngay dưới dòng thiết bị). */
.device-form { margin: -4px 0 10px; flex-wrap: wrap; background: transparent; padding-top: 0; }
.device-form input { flex: 1 1 200px; min-width: 0; }
.device-form select { flex: 1 1 220px; min-width: 0; }
button.small:focus-visible { outline: 3px solid var(--warn); outline-offset: 2px; }
