/* Phòng thu — giao diện ghi âm/ghi hình.
 *
 * Nguyên tắc màu: đỏ (--rec) chỉ mang MỘT nghĩa là "đang ghi". Lỗi mạng dùng
 * hổ phách, xoá dùng đỏ sẫm (--danger) — để người dùng liếc qua là biết máy
 * có đang ghi hay không mà không phải đọc chữ.
 */

:root {
  --bg: #eef1f6;
  --surface: #ffffff;
  --surface-2: #f4f6fa;
  --ink: #172033;
  --ink-2: #3a4459;
  --muted: #5e6880;
  --line: #dbe0ea;
  --line-strong: #c3cad8;
  --rec: #e23d3d;
  --rec-soft: rgba(226, 61, 61, .14);
  --danger: #b42318;
  --safe: #12806f;
  --warn: #a8570a;
  --warn-soft: #fbf0e2;
  --focus: #2f5bd3;
  --tape-idle: #c0c8d7;
  --tape-live: #172033;
  --video-bg: #0f1420;
  --shadow: 0 1px 2px rgba(23, 32, 51, .05), 0 18px 40px -22px rgba(23, 32, 51, .28);
  --radius-l: 24px;
  --radius-m: 16px;
  --radius-s: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141a26;
    --surface: #1c2332;
    --surface-2: #242c3d;
    --ink: #e8ecf4;
    --ink-2: #c4cbda;
    --muted: #97a1b7;
    --line: #2f3849;
    --line-strong: #435068;
    --rec: #f0524f;
    --rec-soft: rgba(240, 82, 79, .18);
    --danger: #ff7a70;
    --safe: #45c6ae;
    --warn: #eaa650;
    --warn-soft: #3a2e1d;
    --focus: #8aa6ff;
    --tape-idle: #3d475c;
    --tape-live: #e8ecf4;
    --video-bg: #0b0f18;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 18px 40px -22px rgba(0, 0, 0, .7);
    color-scheme: dark;
  }
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, p { margin: 0; }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.75;
      stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg .fill { fill: currentColor; stroke: none; }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

[hidden] { display: none !important; }

kbd {
  font: inherit; font-size: .92em; font-weight: 500;
  padding: 1px 6px; border-radius: 6px;
  border: 1px solid var(--line-strong); border-bottom-width: 2px;
  background: var(--surface);
}

/* ── Thanh trên ────────────────────────────────────────────────────────── */

.topbar {
  max-width: 1240px; margin: 0 auto;
  padding: 20px 24px;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand__name { font-weight: 600; font-size: 17px; letter-spacing: -.01em; }

/* Đèn "on air": tắt khi rảnh, sáng đỏ và thở nhẹ khi đang ghi. */
.brand__lamp {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--line-strong);
  box-shadow: 0 0 0 3px var(--surface);
  transition: background .2s;
}
.brand__lamp--big { width: 18px; height: 18px; background: var(--rec); box-shadow: 0 0 0 6px var(--rec-soft); }
body[data-phase="recording"] .brand__lamp { background: var(--rec); animation: breathe 2.4s ease-in-out infinite; }
body[data-phase="paused"] .brand__lamp { background: var(--rec); opacity: .45; }

@keyframes breathe {
  0%, 100% { box-shadow: 0 0 0 3px var(--rec-soft); }
  50% { box-shadow: 0 0 0 7px var(--rec-soft); }
}

.topbar__right { display: flex; align-items: center; gap: 12px; min-width: 0; }

.conn { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--muted); min-width: 0; }
.conn__text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conn__dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line-strong); flex: none; }
.conn[data-state="ok"] .conn__dot { background: var(--safe); }
.conn[data-state="error"] .conn__dot { background: var(--warn); }
.conn[data-state="error"] { color: var(--warn); }

.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  border: 1px solid var(--line); background: var(--surface);
}
.icon-btn:hover { border-color: var(--line-strong); }

.notice {
  max-width: 1192px; margin: 0 auto 20px;
  padding: 12px 16px; border-radius: var(--radius-s);
  background: var(--warn-soft); color: var(--warn);
  font-size: 14px;
}
.notice a { color: inherit; font-weight: 600; }
@media (max-width: 1240px) { .notice { margin-left: 24px; margin-right: 24px; } }

/* ── Bố cục ─────────────────────────────────────────────────────────────── */

.layout {
  max-width: 1240px; margin: 0 auto;
  padding: 0 24px 56px;
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(320px, 1fr);
  gap: 32px;
  align-items: start;
}

/* ── Bàn ghi ───────────────────────────────────────────────────────────── */

.stage {
  background: var(--surface);
  border-radius: var(--radius-l);
  box-shadow: var(--shadow);
  padding: 24px 28px 28px;
  display: flex; flex-direction: column; align-items: center;
}

.modes {
  display: inline-flex; gap: 4px; padding: 4px;
  border-radius: 999px; background: var(--surface-2);
}
.mode {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  white-space: nowrap;
  height: 40px; padding: 0 16px; border-radius: 999px;
  border: 0; background: transparent; color: var(--muted);
  font-weight: 500; font-size: 14.5px;
  transition: background .15s, color .15s;
}
.mode:hover:not(:disabled) { color: var(--ink); }
.mode[aria-checked="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(23, 32, 51, .12); }
.mode:disabled:not([aria-checked="true"]) { opacity: .4; }

.monitor { width: 100%; margin-top: 22px; display: flex; flex-direction: column; gap: 10px; }

.monitor__video {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius-m);
  background: var(--video-bg);
  overflow: hidden;
}
.monitor__video video { width: 100%; height: 100%; object-fit: contain; display: block; }

.tape {
  position: relative;
  height: 168px;
  border-radius: var(--radius-m);
  background: var(--surface-2);
  overflow: hidden;
}
body:not([data-mode="audio"]) .tape { height: 56px; }
.tape canvas { position: absolute; inset: 0; width: 100%; height: 100%; }

/* Khung chờ khi camera/màn hình chưa bật */
.monitor__placeholder {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  padding: 24px; text-align: center;
  color: #aab3c5; font-size: 14.5px;
}
.monitor__placeholder p { max-width: 42ch; }

/* Nút bật xem trước. JS chuyển nó giữa dải sóng, khung chờ và góc khung hình. */
.arm {
  height: 38px; padding: 0 18px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: var(--surface);
  font-size: 14px; font-weight: 500; color: var(--ink-2);
  white-space: nowrap;
}
.arm:hover { border-color: var(--ink-2); color: var(--ink); }
.tape > .arm { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
.arm.is-corner {
  position: absolute; left: auto; top: 10px; right: 10px; transform: none;
  height: 32px; padding: 0 14px; font-size: 13px;
}
.monitor__video > .arm.is-corner { background: rgba(15, 20, 32, .6); border-color: transparent; color: #fff; backdrop-filter: blur(6px); }

/* Xem trước camera lật ngang như gương — người dùng quen thấy mình như vậy.
   Chỉ lật phần HIỂN THỊ, bản ghi vẫn đúng chiều. */
body[data-mode="camera"] #preview { transform: scaleX(-1); }

/* Khi không gửi được lên máy chủ */
.rescue {
  margin: 14px auto 0; max-width: 460px;
  padding: 14px 16px; border-radius: var(--radius-s);
  background: var(--warn-soft); color: var(--warn);
  font-size: 14px;
}
.rescue__actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 10px; }

.readout { margin-top: 22px; text-align: center; }

.clock {
  font-size: clamp(64px, 9.5vw, 116px);
  font-weight: 200;
  line-height: 1;
  letter-spacing: -.035em;
  font-variant-numeric: tabular-nums;
  transition: color .2s;
}
body[data-phase="paused"] .clock { color: var(--muted); }

.sync {
  margin-top: 12px; min-height: 22px;
  display: flex; align-items: center; justify-content: center; gap: 7px;
  font-size: 14px; color: var(--muted);
}
.sync svg { width: 17px; height: 17px; }
.sync[data-tone="ok"] { color: var(--safe); }
.sync[data-tone="warn"] { color: var(--warn); }
.sync[data-tone="busy"] { color: var(--ink-2); }
.sync[data-tone="busy"]::before {
  content: ""; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--line-strong); border-top-color: var(--ink-2);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.transport {
  margin-top: 26px;
  width: 100%;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 28px;
}
.side-btn {
  display: flex; align-items: center; gap: 8px;
  height: 44px; padding: 0 16px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface);
  font-weight: 500; font-size: 14.5px; color: var(--ink-2);
}
.side-btn:hover:not(:disabled) { border-color: var(--line-strong); color: var(--ink); }
#pause { justify-self: end; }
#discard { justify-self: start; }
body[data-phase="idle"] .side-btn,
body[data-phase="finishing"] .side-btn { visibility: hidden; }
#pause svg .fill { color: var(--rec); }

.rec-btn {
  width: 92px; height: 92px; border-radius: 50%;
  display: grid; place-items: center;
  border: 3px solid var(--line-strong);
  background: var(--surface);
  transition: border-color .2s, transform .12s;
}
.rec-btn:hover:not(:disabled) { border-color: var(--ink-2); }
.rec-btn:active:not(:disabled) { transform: scale(.96); }
.rec-btn:focus-visible { outline-offset: 5px; }
.rec-btn__core {
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--rec);
  transition: width .22s cubic-bezier(.3, .7, .3, 1), height .22s cubic-bezier(.3, .7, .3, 1),
              border-radius .22s cubic-bezier(.3, .7, .3, 1), opacity .2s;
}
/* Đang ghi: lõi tròn thu thành ô vuông "dừng" — đúng quy ước của máy ghi âm. */
body[data-phase="recording"] .rec-btn,
body[data-phase="paused"] .rec-btn { border-color: var(--rec); }
body[data-phase="recording"] .rec-btn__core,
body[data-phase="paused"] .rec-btn__core { width: 32px; height: 32px; border-radius: 7px; }
body[data-phase="finishing"] .rec-btn__core,
body[data-phase="starting"] .rec-btn__core { opacity: .35; }

.hint { margin-top: 14px; font-size: 12.5px; color: var(--muted); }
body:not([data-phase="idle"]) .hint { visibility: hidden; }
@media (hover: none) { .hint { display: none; } }

.fields {
  width: 100%;
  margin-top: 24px; padding-top: 22px;
  border-top: 1px solid var(--line);
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px 18px;
}
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field--wide { grid-column: 1 / -1; }
.field__label { font-size: 13px; font-weight: 500; color: var(--ink-2); }
.field__help { font-size: 12.5px; color: var(--muted); }

.field input, .field select {
  height: 44px; width: 100%;
  padding: 0 14px;
  border: 1px solid var(--line); border-radius: var(--radius-s);
  background: var(--surface);
  font-size: 15px;
  transition: border-color .15s, box-shadow .15s;
}
.field select { appearance: none; padding-right: 36px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 19px) 19px, calc(100% - 14px) 19px;
  background-size: 5px 5px; background-repeat: no-repeat; }
.field input:hover, .field select:hover { border-color: var(--line-strong); }
.field input:focus, .field select:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 22%, transparent); }
.field input::placeholder { color: var(--muted); opacity: .8; }

.check { display: flex; align-items: center; gap: 10px; font-size: 14.5px; color: var(--ink-2); align-self: end; min-height: 44px; }
.check input { width: 18px; height: 18px; accent-color: var(--ink); margin: 0; }

/* Mỗi ô cài đặt chỉ hiện ở chế độ nó có tác dụng. */
body[data-mode="audio"]  .fields [data-show]:not([data-show~="audio"]),
body[data-mode="camera"] .fields [data-show]:not([data-show~="camera"]),
body[data-mode="screen"] .fields [data-show]:not([data-show~="screen"]) { display: none; }

/* ── Thư viện ──────────────────────────────────────────────────────────── */

.library { min-width: 0; }

.library__head { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin: 6px 0 14px; flex-wrap: wrap; }
.library__head h2 { font-size: 22px; font-weight: 600; letter-spacing: -.015em; }
.count { color: var(--muted); font-weight: 400; margin-left: 4px; font-variant-numeric: tabular-nums; }

.search {
  display: flex; align-items: center; gap: 8px;
  height: 40px; padding: 0 14px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line);
  color: var(--muted); flex: 1 1 200px; max-width: 280px;
}
.search:focus-within { border-color: var(--focus); }
.search svg { width: 17px; height: 17px; }
.search input { border: 0; outline: 0; background: transparent; width: 100%; font-size: 14.5px; color: var(--ink); }
.search input::placeholder { color: var(--muted); }

.list {
  list-style: none; margin: 0; padding: 0;
  background: var(--surface); border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.list:empty { display: none; }

.row + .row { border-top: 1px solid var(--line); }
.row[data-open] { background: var(--surface-2); }

.row__main {
  width: 100%; border: 0; background: transparent; text-align: left;
  display: grid; grid-template-columns: 38px minmax(0, 1fr) auto; align-items: center; gap: 14px;
  padding: 14px 18px;
}
.row__main:hover { background: var(--surface-2); }
.row__main:focus-visible { outline-offset: -3px; }

.row__icon {
  width: 38px; height: 38px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--surface-2); color: var(--ink-2);
}
.row[data-open] .row__icon { background: var(--surface); }
.row__icon svg { width: 18px; height: 18px; }

.row__text { display: flex; flex-direction: column; min-width: 0; }
.row__title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row__meta { font-size: 13px; color: var(--muted); display: flex; gap: 12px; min-width: 0; }
.row__who { color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row__who:empty { display: none; }
.row__flag { color: var(--warn); }

.row__nums { display: flex; flex-direction: column; align-items: flex-end; font-variant-numeric: tabular-nums; }
.row__dur { font-size: 14px; color: var(--ink-2); }
.row__size { font-size: 12.5px; color: var(--muted); }

.row__panel { padding: 2px 18px 18px 70px; }
.row__player audio { width: 100%; height: 40px; display: block; }
.row__player video { width: 100%; max-height: 380px; border-radius: var(--radius-s); background: var(--video-bg); display: block; }
.row__actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.rename { display: flex; gap: 8px; margin-top: 12px; }
.rename input {
  flex: 1; min-width: 0; height: 36px; padding: 0 12px;
  border: 1px solid var(--focus); border-radius: 10px; background: var(--surface);
}
.rename input:focus { outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 22%, transparent); }

/* Hàng "đang ghi" tạm thời ở đầu danh sách */
.row--live .row__icon { background: var(--rec-soft); color: var(--rec); }
.row--live .row__main { cursor: default; }
.row--live .row__main:hover { background: transparent; }

.chip {
  display: inline-flex; align-items: center; gap: 7px;
  height: 34px; padding: 0 13px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: var(--surface);
  font-size: 13.5px; font-weight: 500; color: var(--ink-2); text-decoration: none;
}
.chip svg { width: 16px; height: 16px; }
.chip:hover { border-color: var(--ink-2); color: var(--ink); }
.chip--danger:hover { border-color: var(--danger); color: var(--danger); }
.chip--solid-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.chip--solid-danger:hover { color: #fff; filter: brightness(1.08); }
.confirm-text { font-size: 13.5px; color: var(--ink-2); align-self: center; margin-right: 4px; }

.empty {
  padding: 44px 28px; text-align: center; color: var(--muted);
  border: 1.5px dashed var(--line-strong); border-radius: 18px;
  font-size: 14.5px;
}
.empty p { max-width: 34ch; margin: 0 auto; }
.empty__title { color: var(--ink); font-weight: 500; font-size: 16px; margin-bottom: 6px !important; }

/* ── Cổng nhập mã ──────────────────────────────────────────────────────── */

.gate {
  position: fixed; inset: 0; z-index: 20;
  background: var(--bg);
  display: grid; place-items: center; padding: 16px;
}
.gate__card {
  width: min(420px, 100%);
  background: var(--surface); border-radius: var(--radius-l); box-shadow: var(--shadow);
  padding: 36px 32px 32px;
  display: flex; flex-direction: column; gap: 12px;
}
.gate__card h2 { font-size: 24px; font-weight: 600; letter-spacing: -.02em; margin-top: 14px; }
.gate__lead { color: var(--muted); margin-bottom: 8px; }
.gate__card input {
  height: 48px; padding: 0 16px; border-radius: var(--radius-s);
  border: 1px solid var(--line-strong); background: var(--surface); font-size: 16px;
}
.gate__card input:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 22%, transparent); }
.gate__error { color: var(--danger); font-size: 14px; min-height: 0; }
.gate__error:empty { display: none; }

.primary {
  height: 46px; padding: 0 20px; border-radius: var(--radius-s);
  border: 0; background: var(--ink); color: var(--surface);
  font-weight: 600; font-size: 15px;
}
.primary:hover { filter: brightness(1.15); }
.ghost {
  height: 46px; padding: 0 18px; border-radius: var(--radius-s);
  border: 1px solid var(--line); background: transparent; font-weight: 500; color: var(--ink-2);
}
.ghost:hover { border-color: var(--line-strong); color: var(--ink); }

/* ── Cài đặt ───────────────────────────────────────────────────────────── */

.sheet {
  width: min(480px, calc(100% - 32px));
  border: 0; border-radius: var(--radius-l); padding: 0;
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
}
.sheet::backdrop { background: rgba(12, 16, 26, .45); backdrop-filter: blur(3px); }
.sheet form { padding: 28px; display: flex; flex-direction: column; gap: 18px; }
.sheet h2 { font-size: 21px; font-weight: 600; letter-spacing: -.015em; }
.sheet__invite { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; }
.sheet__foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

/* ── Thông báo nổi ─────────────────────────────────────────────────────── */

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  max-width: calc(100% - 32px);
  padding: 12px 18px; border-radius: var(--radius-s);
  background: var(--ink); color: var(--surface);
  font-size: 14px; box-shadow: var(--shadow);
  animation: toast-in .22s ease-out;
}
.toast[data-tone="warn"] { background: var(--warn); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ── Màn hình hẹp ──────────────────────────────────────────────────────── */

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); gap: 36px; }
}

@media (max-width: 600px) {
  .topbar { padding: 14px 16px; }
  .layout { padding: 0 16px 40px; }
  .notice { margin-left: 16px; margin-right: 16px; }
  .stage { padding: 18px 16px 22px; border-radius: 20px; }
  /* Ba nhãn phải nằm trên một hàng ở màn 360px — bỏ biểu tượng, chia đều. */
  .modes { display: flex; width: 100%; }
  .mode { flex: 1; padding: 0 8px; font-size: 14px; }
  .mode svg { display: none; }
  .search { flex-basis: 100%; max-width: none; }
  .tape { height: 132px; }
  .transport { gap: 14px; }
  .side-btn { padding: 0 12px; }
  .side-btn span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .side-btn { width: 48px; height: 48px; justify-content: center; padding: 0; }
  .fields { grid-template-columns: 1fr; }
  .row__main { padding: 13px 14px; gap: 12px; }
  .row__panel { padding: 2px 14px 16px; }
  .conn__text { max-width: 36vw; }
}

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