:root {
  color-scheme: light;
  --bg: #f6f4ef;
  --surface: #fffdf8;
  --surface-2: #ebe7dc;
  --ink: #20231f;
  --muted: #646b60;
  --line: #d8d2c4;
  --teal: #0f766e;
  --teal-dark: #0b4f4a;
  --red: #b42318;
  --amber: #b7791f;
  --blue: #1d4ed8;
  --shadow: 0 18px 50px rgba(32, 35, 31, 0.12);
  font-family: Inter, "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  background:
    linear-gradient(90deg, rgba(15, 118, 110, 0.08) 1px, transparent 1px),
    linear-gradient(0deg, rgba(32, 35, 31, 0.06) 1px, transparent 1px),
    var(--bg);
  background-size: 48px 48px;
  color: var(--ink);
}

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

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.hidden {
  display: none !important;
}

.lobby-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.lobby-panel {
  width: min(460px, 100%);
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.94);
  box-shadow: var(--shadow);
}

.brand-row,
.room-heading,
.room-title-row,
.room-meta,
.class-clock,
.call-controls,
.panel-tabs,
.board-toolbar,
.member-actions,
.lesson-actions {
  display: flex;
  align-items: center;
}

.brand-row {
  gap: 14px;
  margin-bottom: 24px;
}

.brand-mark {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #f0c35b;
  color: var(--ink);
  font-weight: 900;
}

.brand-mark.small {
  width: 40px;
  height: 40px;
  font-size: 14px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 32px;
  line-height: 1.1;
}

.brand-row p {
  margin-top: 4px;
  color: var(--muted);
}

.mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}

.mode-switch button,
.panel-tabs button {
  min-height: 38px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
}

.mode-switch button.active,
.panel-tabs button.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 4px rgba(32, 35, 31, 0.12);
}

.lobby-form {
  display: grid;
  gap: 16px;
  margin-top: 20px;
}

.field,
.notes-box {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 14px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

input,
select {
  height: 44px;
  padding: 0 12px;
}

textarea {
  resize: vertical;
  min-height: 160px;
  padding: 12px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.16);
}

.primary-action,
.chat-form button,
.lesson-actions button {
  min-height: 44px;
  border: 0;
  border-radius: 6px;
  background: var(--teal);
  color: #fff;
  font-weight: 800;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  min-height: 76px;
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.96);
}

.room-heading {
  min-width: 0;
  gap: 12px;
}

.room-heading h2 {
  max-width: min(48vw, 560px);
  overflow: hidden;
  font-size: 20px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-title-row {
  gap: 8px;
}

.room-meta {
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 5px;
  color: var(--muted);
  font-size: 13px;
}

.text-button {
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--teal-dark);
  font-weight: 700;
}

.pill,
.tile-badge {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  padding: 0 8px;
  border-radius: 999px;
  background: rgba(15, 118, 110, 0.12);
  color: var(--teal-dark);
  font-size: 12px;
  font-weight: 800;
}

.class-clock {
  justify-content: flex-end;
  gap: 10px;
  min-width: 170px;
}

.class-clock span {
  color: var(--muted);
  font-size: 13px;
}

.class-clock strong {
  font-variant-numeric: tabular-nums;
  font-size: 22px;
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(340px, 380px);
}

.video-area {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr auto;
  padding: 14px;
}

.video-grid {
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  grid-auto-rows: minmax(210px, 1fr);
  gap: 12px;
  overflow: auto;
  overscroll-behavior: contain;
}

.video-grid.has-pinned {
  grid-template-columns: minmax(0, 1fr) minmax(160px, 210px);
  grid-template-rows: repeat(5, minmax(0, 1fr));
  grid-auto-rows: minmax(104px, 1fr);
}

.video-tile {
  position: relative;
  min-height: 210px;
  overflow: hidden;
  border: 1px solid rgba(32, 35, 31, 0.16);
  border-radius: 8px;
  background: #111410;
  box-shadow: 0 10px 28px rgba(32, 35, 31, 0.16);
  outline: none;
  transition: border-color 0.16s ease, box-shadow 0.16s ease, transform 0.16s ease;
}

.video-tile:focus-visible {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.22), 0 10px 28px rgba(32, 35, 31, 0.16);
}

.video-grid.has-pinned .video-tile.pinned {
  grid-column: 1;
  grid-row: 1 / -1;
  min-height: 0;
}

.video-grid.has-pinned .video-tile.unpinned {
  grid-column: 2;
  min-height: 104px;
}

.video-grid.has-pinned .video-tile.pinned video {
  object-fit: contain;
}

.video-grid.has-pinned .video-tile.unpinned .tile-footer {
  left: 6px;
  right: 6px;
  bottom: 6px;
  padding: 6px;
}

.video-tile video {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  background: #111410;
}

.video-tile.local video {
  transform: scaleX(-1);
}

.video-grid.has-pinned .video-tile.local.pinned video {
  object-fit: contain;
}

.video-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.82);
  font-size: 42px;
  font-weight: 900;
}

.tile-footer {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px;
  border-radius: 6px;
  background: rgba(17, 20, 16, 0.76);
  color: #fff;
}

.pin-button {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 6px;
  background: rgba(17, 20, 16, 0.72);
  color: #fff;
  font-size: 13px;
  font-weight: 900;
  opacity: 0;
  transition: opacity 0.16s ease, background 0.16s ease;
}

.video-tile:hover .pin-button,
.video-tile:focus-within .pin-button,
.video-tile.pinned .pin-button {
  opacity: 1;
}

.pin-button:hover {
  background: rgba(15, 118, 110, 0.9);
}

body.video-fullscreen {
  overflow: hidden;
  background: #000;
}

body.video-fullscreen .topbar,
body.video-fullscreen .side-panel,
body.video-fullscreen .call-controls {
  display: none !important;
}

body.video-fullscreen .app-shell,
body.video-fullscreen .workspace,
body.video-fullscreen .video-area,
body.video-fullscreen .video-grid {
  display: block;
  width: 100dvw;
  height: 100dvh;
  min-height: 100dvh;
  padding: 0;
  overflow: hidden;
}

body.video-fullscreen .video-tile {
  display: none;
}

body.video-fullscreen .video-tile.fullscreen {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: block;
  width: 100dvw;
  height: 100dvh;
  min-height: 100dvh;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  background: #000;
}

body.video-fullscreen .video-tile.fullscreen video {
  object-fit: cover;
}

body.video-fullscreen .video-tile.fullscreen.screen-sharing video {
  object-fit: contain;
}

body.video-fullscreen .video-tile.fullscreen .pin-button {
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  z-index: 55;
  opacity: 1;
  background: rgba(180, 35, 24, 0.9);
}

body.video-fullscreen .video-tile.fullscreen .tile-footer {
  left: calc(env(safe-area-inset-left, 0px) + 12px);
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
  z-index: 54;
}

.tile-name {
  min-width: 0;
  overflow: hidden;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tile-badges {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 5px;
}

.tile-badge {
  min-height: 20px;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
}

.tile-badge.warning {
  background: rgba(180, 35, 24, 0.9);
}

.tile-badge.attention {
  background: rgba(183, 121, 31, 0.9);
}

.call-controls {
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  padding-top: 12px;
}

.control-button,
.danger-button,
.member-actions button,
.board-toolbar button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font-weight: 800;
}

.control-button,
.danger-button {
  min-width: 94px;
  padding: 0 12px;
}

.control-button.active {
  border-color: var(--teal);
  background: rgba(15, 118, 110, 0.12);
  color: var(--teal-dark);
}

.danger-button {
  border-color: rgba(180, 35, 24, 0.28);
  background: rgba(180, 35, 24, 0.1);
  color: var(--red);
}

.side-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  border-left: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.92);
}

.panel-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 10px;
  border-bottom: 1px solid var(--line);
}

.panel-view {
  min-height: 0;
  display: none;
  padding: 12px;
}

.panel-view.active {
  display: grid;
}

#chat-panel {
  grid-template-rows: 1fr auto;
  gap: 10px;
}

.chat-list,
.member-list {
  min-height: 0;
  overflow: auto;
}

.chat-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-message {
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.chat-message.system {
  background: rgba(15, 118, 110, 0.08);
  color: var(--teal-dark);
}

.chat-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 12px;
}

.chat-text {
  overflow-wrap: anywhere;
  line-height: 1.45;
}

.chat-form {
  display: grid;
  grid-template-columns: 1fr 70px;
  gap: 8px;
}

.member-actions {
  flex-wrap: wrap;
  gap: 7px;
  margin-bottom: 10px;
}

.member-actions button,
.board-toolbar button {
  min-width: 72px;
  padding: 0 10px;
}

.member-list {
  display: grid;
  align-content: start;
  gap: 8px;
}

.member-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.member-name {
  min-width: 0;
  overflow: hidden;
  font-weight: 900;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.member-sub {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.member-status {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 5px;
}

.board-toolbar {
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.swatch {
  width: 32px;
  min-width: 32px !important;
  height: 32px;
  min-height: 32px !important;
  padding: 0 !important;
}

.swatch[data-color="#0f766e"] {
  background: #0f766e;
}

.swatch[data-color="#b42318"] {
  background: #b42318;
}

.swatch[data-color="#1d4ed8"] {
  background: #1d4ed8;
}

.swatch.active {
  outline: 3px solid rgba(32, 35, 31, 0.22);
  outline-offset: 2px;
}

.brush-size {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
}

.brush-size input {
  width: 82px;
  height: auto;
  padding: 0;
}

#whiteboard {
  width: 100%;
  aspect-ratio: 4 / 3;
  display: block;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  touch-action: none;
}

#lesson-panel {
  align-content: start;
  gap: 12px;
}

.lesson-actions {
  align-items: stretch;
  gap: 8px;
}

.lesson-actions input {
  min-width: 0;
}

.lesson-actions button {
  min-width: 104px;
  padding: 0 12px;
}

#toast-root {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 20;
  display: grid;
  gap: 8px;
  width: min(360px, calc(100vw - 32px));
}

.toast {
  padding: 11px 12px;
  border: 1px solid rgba(32, 35, 31, 0.14);
  border-radius: 8px;
  background: rgba(32, 35, 31, 0.92);
  color: #fff;
  box-shadow: var(--shadow);
}

body[data-role="student"] .teacher-only {
  display: none !important;
}

@media (max-width: 960px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .class-clock {
    justify-content: space-between;
    width: 100%;
  }

  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(420px, 58dvh) minmax(360px, 42dvh);
  }

  .video-grid.has-pinned {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: minmax(260px, 1fr);
    grid-auto-rows: 128px;
  }

  .video-grid.has-pinned .video-tile.pinned {
    grid-column: 1 / -1;
    grid-row: 1;
    min-height: 260px;
  }

  .video-grid.has-pinned .video-tile.unpinned {
    grid-column: auto;
    min-height: 128px;
  }

  .side-panel {
    border-top: 1px solid var(--line);
    border-left: 0;
  }
}

@media (max-width: 560px) {
  .lobby-shell,
  .video-area {
    padding: 10px;
  }

  .lobby-panel {
    padding: 18px;
  }

  h1 {
    font-size: 28px;
  }

  .topbar {
    padding: 12px;
  }

  .room-heading h2 {
    max-width: 70vw;
    font-size: 18px;
  }

  .video-grid {
    grid-template-columns: 1fr;
    grid-auto-rows: minmax(220px, 1fr);
    gap: 8px;
  }

  .video-grid.has-pinned {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: minmax(240px, 1fr);
    grid-auto-rows: 112px;
  }

  .video-grid.has-pinned .video-tile.pinned {
    min-height: 240px;
  }

  .video-grid.has-pinned .video-tile.unpinned {
    min-height: 112px;
  }

  .video-grid.has-pinned .video-tile.unpinned .tile-badges {
    display: none;
  }

  .video-grid.has-pinned .video-tile.unpinned .tile-name {
    font-size: 12px;
  }

  .pin-button {
    min-height: 30px;
    padding: 0 8px;
    opacity: 1;
  }

  .tile-footer {
    left: 8px;
    right: 8px;
    bottom: 8px;
    padding: 7px;
  }

  .control-button,
  .danger-button {
    flex: 1 1 42%;
    min-width: 0;
  }

  .panel-tabs {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .panel-tabs button {
    min-width: 0;
    padding: 0 4px;
    font-size: 14px;
  }
}

@media (hover: none) {
  .pin-button {
    opacity: 1;
  }
}
