:root {
  --bg: #edf0f3;
  --bg-glow: radial-gradient(1100px 460px at 50% -80px, rgba(40, 85, 129, 0.08), transparent 70%);
  --paper: #fdfcf9;
  --paper-soft: #f5f7f9;
  --navy: #142235;
  --navy-soft: #1d314b;
  --accent: #1e4d7b;
  --accent-hover: #163b5e;
  --accent-soft: rgba(30, 77, 123, 0.09);
  --ink: #142133;
  --muted: #586778;
  --border: rgba(20, 34, 53, 0.11);
  --border-strong: rgba(20, 34, 53, 0.18);
  --danger: #c43638;
  --danger-hover: #a5292b;
  --ok: #127850;
  --warn: #a65f0d;
  --shadow-card: 0 1px 2px rgba(15, 23, 42, 0.04), 0 12px 28px -10px rgba(15, 23, 42, 0.08);
  --shadow-btn: 0 1px 2px rgba(15, 23, 42, 0.06);
  --room-bg: #0c1017;
  --room-panel: #131924;
  --room-surface: #1a2230;
  --room-border: rgba(255, 255, 255, 0.085);
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--bg);
  background-image: var(--bg-glow);
  background-attachment: fixed;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------- Top Navigation (Portal / Admin / Passkey) ---------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(253, 252, 249, 0.86);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  color: var(--ink);
  padding: 12px clamp(16px, 3vw, 32px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
}
.mac-inset .topbar {
  padding-left: 84px;
  -webkit-app-region: drag;
}
.mac-inset .topbar a, .mac-inset .topbar button, .mac-inset .topbar input {
  -webkit-app-region: no-drag;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 11px;
  font-weight: 700;
  font-size: 15.5px;
  letter-spacing: -0.02em;
  color: var(--navy);
  text-decoration: none;
}
.brand:hover { text-decoration: none; }
.brand small {
  font-weight: 500;
  color: var(--muted);
  font-size: 12px;
  margin-left: 4px;
  letter-spacing: 0;
}
.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: linear-gradient(145deg, #1b3554, #102136);
  color: #e6f2ff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(20, 34, 53, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.16);
  flex: none;
}
.brand-mark svg { width: 16px; height: 16px; display: block; }

.nav-links {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.nav-links a, .nav-btn {
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 550;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  font-family: var(--sans);
  transition: background 140ms ease, color 140ms ease;
}
.nav-links a:hover, .nav-btn:hover {
  background: rgba(20, 34, 53, 0.055);
  color: var(--navy);
  text-decoration: none;
}
.nav-links a.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 650;
}

/* ---------------- Studio Header & Layout ---------------- */
.container {
  max-width: 1040px;
  margin: clamp(20px, 4vh, 42px) auto;
  padding: 0 clamp(14px, 3vw, 24px) 48px;
}
.studio-hero {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 14px 24px;
  margin-bottom: 24px;
}
.studio-eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 6px;
}
.studio-eyebrow i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #1a9b68;
}
.studio-hero h1 {
  margin: 0;
  font-size: clamp(22px, 3.2vw, 28px);
  font-weight: 720;
  letter-spacing: -0.028em;
  color: var(--navy);
  line-height: 1.25;
}
.studio-hero p {
  margin: 5px 0 0;
  font-size: 13.5px;
  color: var(--muted);
}
.studio-pills {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px;
}
.studio-pill {
  font-size: 12px;
  font-weight: 550;
  color: #3d4f63;
  background: rgba(253, 252, 249, 0.82);
  border: 1px solid var(--border);
  padding: 5px 11px;
  border-radius: 999px;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px;
  align-items: stretch;
}
.card {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: clamp(20px, 3vw, 28px);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
}
.card-tag {
  display: inline-flex;
  align-items: center;
  align-self: flex-start;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.04em;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 3px 9px;
  border-radius: 999px;
  margin-bottom: 10px;
}
.card-tag.teacher {
  color: #165e44;
  background: rgba(18, 120, 80, 0.1);
}
.card h2 {
  margin: 0 0 6px;
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--navy);
}
.card p.lead {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 13.5px;
  line-height: 1.55;
}

.field {
  margin-bottom: 15px;
}
.field label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: #3c4d60;
  margin-bottom: 6px;
}
input[type="text"], input[type="email"], input[type="password"], input[type="number"], select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  font-size: 14px;
  font-family: var(--sans);
  background: #fff;
  color: var(--ink);
  transition: border-color 140ms ease, box-shadow 140ms ease, background 140ms ease;
}
input::placeholder, textarea::placeholder { color: #8c9baa; }
input:hover:not(:focus), select:hover:not(:focus), textarea:hover:not(:focus) {
  border-color: rgba(20, 34, 53, 0.3);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3.5px rgba(30, 77, 123, 0.13);
}
#join-code {
  font-family: var(--mono);
  letter-spacing: 0.06em;
  font-size: 15px;
}

.check-row {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  background: var(--paper-soft);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
}
.check-row input {
  width: auto;
  margin-top: 2px;
  accent-color: var(--accent);
}

.row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.btn {
  appearance: none;
  border: 1px solid var(--border-strong);
  background: #fff;
  color: var(--ink);
  padding: 10px 16px;
  min-height: 42px;
  border-radius: 10px;
  font-size: 13.5px;
  font-weight: 620;
  cursor: pointer;
  font-family: var(--sans);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  box-shadow: var(--shadow-btn);
  transition: background 140ms ease, border-color 140ms ease, transform 90ms ease, box-shadow 140ms ease;
}
.btn:hover:not(:disabled) { background: #f4f6f9; border-color: rgba(20, 34, 53, 0.26); text-decoration: none; }
.btn:active:not(:disabled) { transform: translateY(0.5px); }
.btn.block { width: 100%; min-height: 44px; font-size: 14px; }
.btn.primary {
  background: linear-gradient(180deg, #245b8f, var(--accent));
  border-color: #19426a;
  color: #fff;
  box-shadow: 0 1px 2px rgba(20, 34, 53, 0.16), inset 0 1px 0 rgba(255, 255, 255, 0.14);
}
.btn.primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #1f507f, var(--accent-hover));
  border-color: #143658;
}
.btn.danger {
  background: var(--danger);
  border-color: #a92b2d;
  color: #fff;
}
.btn.danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn.ok {
  background: var(--ok);
  border-color: #0e6341;
  color: #fff;
}
.btn.sm { padding: 6px 11px; min-height: 32px; font-size: 12.5px; border-radius: 8px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; box-shadow: none; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--accent-soft);
  color: var(--accent);
}
.badge.ok { background: #e1f4eb; color: var(--ok); }
.badge.warn { background: #fef2de; color: var(--warn); }
.badge.danger { background: #fde8e8; color: var(--danger); }

.status-msg {
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
  min-height: 20px;
}
.status-msg.err { color: var(--danger); }
.status-msg.ok { color: var(--ok); }

.studio-footer {
  margin-top: 22px;
  padding: 14px 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(253, 252, 249, 0.72);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px 18px;
  font-size: 12.5px;
  color: var(--muted);
}
.studio-footer a { font-weight: 600; }

#teacher-pair {
  margin-top: 14px;
  padding: 14px;
  border-radius: 12px;
  background: var(--paper-soft);
  border: 1px solid var(--border);
}
#teacher-pair-code {
  font-family: var(--mono);
  font-size: 15px;
  letter-spacing: 0.05em;
  color: var(--navy);
  padding: 5px 10px;
  border-radius: 8px;
  background: #fff;
  border: 1px solid var(--border);
}

/* ---------------- Tables (Admin) ---------------- */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}
th, td {
  text-align: left;
  padding: 12px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
th {
  font-size: 11.5px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 650;
  background: var(--paper-soft);
}
tbody tr:hover { background: rgba(30, 77, 123, 0.025); }
tbody tr:last-child td { border-bottom: 0; }

/* ---------------- Live Classroom Dark Base ---------------- */
body.classroom-body {
  background: var(--room-bg);
  background-image: none;
  color: #edf2f7;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.room-header {
  height: 56px;
  padding: 0 18px;
  background: rgba(12, 16, 23, 0.94);
  border-bottom: 1px solid var(--room-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}
.room-meta {
  display: flex;
  align-items: center;
  gap: 11px;
  font-size: 13.5px;
}
.room-code-pill {
  font-family: var(--mono);
  background: rgba(255, 255, 255, 0.06);
  color: #a9bfd6;
  border: 1px solid var(--room-border);
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 11.5px;
  cursor: pointer;
}
.room-code-pill:hover { background: rgba(255, 255, 255, 0.11); color: #eef5fc; }

.room-main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 330px;
}
.stage-col {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 10px 14px 12px;
  gap: 8px;
}
.stage-tabs {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.stage-viewport {
  flex: 1;
  min-height: 0;
  position: relative;
  background: transparent;
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.video-grid {
  width: 100%;
  height: 100%;
  display: grid;
  gap: 10px;
}
.video-tile {
  position: relative;
  background: #121823;
  border: 1px solid var(--room-border);
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.video-tile video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #080b11;
}
.tile-label {
  position: absolute;
  left: 12px;
  bottom: 12px;
  background: rgba(11, 15, 23, 0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: #edf3f9;
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 500;
}

/* Whiteboard stage */
.wb-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background: #fdfcf9;
  border-radius: 14px;
  border: 1px solid var(--room-border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.wb-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: #f2f4f7;
  border-bottom: 1px solid #d7dfe8;
  flex-wrap: wrap;
}
.wb-toolbar .btn.sm.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.wb-canvas-box {
  flex: 1;
  position: relative;
  overflow: hidden;
}
#wb-canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
  touch-action: none;
}
.wb-laser {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(220, 38, 38, 0.85);
  box-shadow: 0 0 12px rgba(220, 38, 38, 0.9);
  pointer-events: none;
  transform: translate(-50%, -50%);
  display: none;
}

/* Right Classroom Sidebar */
.side-col {
  background: var(--room-panel);
  border-left: 1px solid var(--room-border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.side-tabs {
  display: flex;
  border-bottom: 1px solid var(--room-border);
  padding: 0 6px;
  gap: 2px;
}
.side-tabs button {
  background: transparent;
  color: #8b9bb0;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 11px 8px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: color 140ms ease, border-color 140ms ease;
}
.side-tabs button:hover { color: #d8e4f0; }
.side-tabs button.active {
  color: #f3f7fb;
  border-bottom-color: #6ee7b7;
}
.side-pane {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 12px;
  overflow-y: auto;
}
.side-pane[hidden] { display: none; }

.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}
.chat-item {
  background: rgba(255, 255, 255, 0.045);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 10px;
  padding: 8px 11px;
  font-size: 13px;
  line-height: 1.48;
  word-break: break-word;
}
.chat-item .meta {
  font-size: 11px;
  color: #899bb1;
  margin-bottom: 3px;
}
.chat-item a {
  color: #7cd4ff;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.chat-input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--room-border);
}
.chat-input-row input {
  flex: 1;
  min-width: 0;
  background: #0d121b;
  border: 1px solid rgba(255, 255, 255, 0.11);
  color: #eef3f8;
  border-radius: 9px;
  padding: 8px 11px;
  font-size: 13px;
}
.chat-input-row input:focus {
  border-color: rgba(110, 231, 183, 0.55);
  box-shadow: 0 0 0 3px rgba(110, 231, 183, 0.12);
}
.chat-input-row button {
  flex: 0 0 auto;
  white-space: nowrap;
  min-height: 36px;
  padding: 6px 14px;
}

.peer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 11px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.045);
  border: 1px solid rgba(255, 255, 255, 0.06);
  margin-bottom: 7px;
  font-size: 12.5px;
}

/* Bottom Classroom Control Bar */
.room-controls {
  height: 68px;
  padding: 0 18px;
  background: rgba(12, 16, 23, 0.96);
  border-top: 1px solid var(--room-border);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-shrink: 0;
}
.ctrl-btn {
  background: var(--room-surface);
  color: #edf3f9;
  border: 1px solid rgba(255, 255, 255, 0.09);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 560;
  cursor: pointer;
  font-family: var(--sans);
  transition: background 140ms ease, border-color 140ms ease, color 140ms ease, transform 90ms ease;
}
.ctrl-btn:hover:not(:disabled) { background: #232d3f; border-color: rgba(255, 255, 255, 0.16); }
.ctrl-btn:active:not(:disabled) { transform: translateY(0.5px); }
.ctrl-btn.active { background: #234c78; border-color: rgba(114, 178, 240, 0.45); }
.ctrl-btn.off { background: rgba(220, 68, 70, 0.14); border-color: rgba(239, 108, 110, 0.34); color: #ffb3b3; }
.ctrl-btn.danger { background: rgba(212, 56, 59, 0.9); border-color: rgba(255, 120, 120, 0.35); color: #fff; }
.ctrl-btn.danger:hover:not(:disabled) { background: #bf2c2f; }

/* ---------------- Replay Player (Legacy Compat) ---------------- */
.player-shell {
  position: relative;
  background: #090d14;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid #26354a;
  aspect-ratio: 16 / 9;
  user-select: none;
}
.player-shell video { width: 100%; height: 100%; display: block; background: #000; }
.watermark-layer { position: absolute; inset: 0; pointer-events: none; overflow: hidden; z-index: 5; }
.wm-tag { position: absolute; font-family: var(--mono); font-size: 13px; font-weight: 600; color: rgba(255, 255, 255, 0.24); white-space: nowrap; transform: rotate(-16deg); transition: left 2.2s ease, top 2.2s ease; }

/* ---------------- Modal & Floating HUD Toast ---------------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 11, 17, 0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal-backdrop[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 440px;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 18px;
  padding: 26px 28px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}
.ns-dialog-backdrop { z-index: 80; }
.ns-dialog-card { max-width: 400px; padding: 22px 24px; }
.ns-dialog-card h3 { margin: 0 0 8px; font-size: 17px; font-weight: 700; color: var(--navy); letter-spacing: -0.02em; }
.ns-dialog-card p { margin: 0 0 18px; font-size: 13.5px; line-height: 1.55; color: var(--muted); white-space: pre-line; }
.ns-dialog-actions { display: flex; justify-content: flex-end; gap: 9px; }
.ns-dialog-actions .btn { min-width: 82px; min-height: 38px; padding: 8px 15px; }

.toast-box {
  position: fixed;
  top: 66px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  max-width: min(460px, calc(100vw - 24px));
}
.toast-msg {
  background: rgba(18, 25, 36, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: #f2f7fc;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  text-align: center;
}

@media (max-width: 860px) {
  .grid-2 { grid-template-columns: 1fr; }
  .room-main { grid-template-columns: 1fr; }
  .side-col { display: none; }
}
