:root {
  --red: #c62828;
  --red-dark: #9f1d1d;
  --gold: #f2b632;
  --gold-dark: #c99526;
  --ink: #1e2328;
  --muted: #5d656e;
  --line: #d5d9de;
  --bg: #eceef1;
  --panel: #ffffff;
  --tile: #dde1e7;
  --tile-hover: #cfd5dd;
  --tile-current: #fff3d1;
  --chip: #23272b;
  --chip-me: #c62828;
  --ok: #2e7d32;
  --radius: 8px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 2px 8px rgb(0 0 0 / 0.06);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; background: var(--bg); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 1.05rem; }
a { color: var(--red); }
[hidden] { display: none !important; }
hr { width: 100%; border: 0; border-top: 1px solid var(--line); margin: 6px 0; }

/* ---- Top bar ---------------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: #fff;
  border-bottom: 3px solid var(--gold);
  box-shadow: var(--shadow);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.1rem; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 6px;
  background: var(--red);
  color: #fff;
  font-size: 0.8rem;
  letter-spacing: 0.02em;
}
.topbar .spacer { flex: 1; }
.topbar nav { display: flex; gap: 4px; flex-wrap: wrap; }
.topbar nav a {
  padding: 6px 10px;
  border-radius: 6px;
  color: var(--ink);
  text-decoration: none;
  font-weight: 600;
}
.topbar nav a.active, .topbar nav a:hover { background: var(--tile); }
.toplink { color: var(--muted); font-size: 0.9rem; }

/* ---- Controls --------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 6px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: #f4f5f7; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.gold { background: var(--gold); border-color: var(--gold-dark); color: #3a2a00; }
.btn.gold:hover { background: #f5c24f; }
.btn.red { background: var(--red); border-color: var(--red-dark); color: #fff; }
.btn.red:hover { background: var(--red-dark); }
.btn.small { min-height: 30px; padding: 3px 10px; font-size: 0.85rem; }
.btn.icon { padding: 3px 8px; min-width: 30px; }

input, select, textarea {
  font: inherit;
  color: inherit;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  min-width: 0;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--gold-dark); outline-offset: 1px; }
label { display: grid; gap: 4px; font-weight: 600; font-size: 0.9rem; color: var(--muted); }
label.inline { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; }

.error { color: var(--red); font-weight: 600; min-height: 1.2em; margin: 0; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }

/* ---- Panels / cards --------------------------------------------------- */
.panel, .card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 0;
}
.panel { padding: 12px; display: grid; gap: 10px; align-content: start; }
.panel-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.panel-head .spacer { flex: 1; }
.card-head {
  padding: 10px 16px;
  background: var(--gold);
  color: #fff;
  font-weight: 700;
  font-size: 1.15rem;
  border-radius: var(--radius) var(--radius) 0 0;
  text-shadow: 0 1px 1px rgb(0 0 0 / 0.15);
}
.card-body { padding: 18px 16px; display: grid; gap: 14px; }

/* ---- Login ------------------------------------------------------------ */
.center { display: grid; place-items: start center; padding: 48px 16px; }
.login { width: min(480px, 100%); }
.login .btn { justify-self: center; min-width: 140px; }
#unit-slot { display: grid; }

/* ---- Exercise layout -------------------------------------------------- */
.ex-layout {
  display: grid;
  gap: 12px;
  padding: 12px;
  grid-template-columns: minmax(0, 1.35fr) minmax(320px, 1fr);
  grid-template-areas: 'viewer board' 'viewer chat';
  align-items: start;
}
.viewer-panel { grid-area: viewer; position: sticky; top: 12px; }
.board-panel { grid-area: board; }
.chat-panel { grid-area: chat; }
@media (max-width: 900px) {
  .ex-layout { grid-template-columns: minmax(0, 1fr); grid-template-areas: 'viewer' 'board' 'chat'; }
  .viewer-panel { position: static; }
}

.timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 1.35rem;
  padding: 2px 10px;
  border-radius: 6px;
  background: var(--tile);
}
.pill {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--tile);
  color: var(--muted);
}
.pill.running { background: #dff3e1; color: var(--ok); }
.pill.paused { background: #fff3d1; color: #8a6100; }
.pill.ended { background: #fde2e2; color: var(--red-dark); }
.me-badge { font-weight: 700; padding: 4px 10px; border-radius: 6px; background: var(--chip-me); color: #fff; }

/* ---- Viewer ----------------------------------------------------------- */
.stage {
  position: relative;
  background: #111;
  border-radius: 6px;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  display: grid;
  place-items: center;
}
.stage img, .stage video { width: 100%; height: 100%; object-fit: contain; display: block; background: #111; }
.stage .placeholder { color: #ddd; text-align: center; padding: 24px; max-width: 46ch; line-height: 1.5; }
.stage .placeholder strong { display: block; font-size: 1.4rem; margin-bottom: 8px; color: #fff; }
.stage-nav {
  position: absolute;
  top: 50%;
  translate: 0 -50%;
  width: 46px;
  height: 64px;
  border: 0;
  border-radius: 6px;
  background: rgb(0 0 0 / 0.45);
  color: #fff;
  font-size: 1.8rem;
  cursor: pointer;
}
.stage-nav:hover { background: var(--red); }
.stage-nav.prev { left: 8px; }
.stage-nav.next { right: 8px; }
.viewer-foot { display: flex; justify-content: space-between; gap: 10px; color: var(--muted); font-size: 0.9rem; }
.notes { margin: 0; white-space: pre-wrap; line-height: 1.5; padding: 10px 12px; background: #f7f8fa; border-left: 4px solid var(--red); border-radius: 4px; }
.links { display: flex; flex-wrap: wrap; gap: 8px; }
.links .btn { border-color: var(--red); color: var(--red); }
.links .btn:hover { background: var(--red); color: #fff; }

/* ---- Board ------------------------------------------------------------ */
.board { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); }
.tile {
  display: grid;
  align-content: start;
  gap: 6px;
  min-height: 92px;
  padding: 8px;
  border: 1px solid #c8ced6;
  border-radius: 6px;
  background: var(--tile);
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
}
.tile:hover { background: var(--tile-hover); }
.tile.current { background: var(--tile-current); border-color: var(--gold-dark); box-shadow: inset 0 0 0 1px var(--gold-dark); }
.tile.viewing { outline: 2px dashed var(--red); outline-offset: -3px; }
.tile.drop-target { background: #fff3d1; }
.tile-name { font-weight: 700; font-size: 0.88rem; overflow-wrap: anywhere; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  padding: 3px 8px;
  border-radius: 4px;
  background: var(--chip);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  border: 0;
  font-family: inherit;
}
.chip.me { background: var(--chip-me); }
.chip.offline { opacity: 0.45; }
.chip.selected { outline: 3px solid var(--gold); }
button.chip { cursor: pointer; }

/* ---- Chat ------------------------------------------------------------- */
.chat-form { display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; gap: 6px; }
@media (max-width: 520px) {
  .chat-form { grid-template-columns: minmax(0, 1fr) auto; }
  .chat-form select { grid-column: 1 / -1; }
}
.log {
  height: 340px;
  overflow-y: auto;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfbfc;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.5;
}
.log .line { margin: 0; overflow-wrap: anywhere; }
.log .t { color: var(--muted); }
.log .event { color: var(--muted); }
.log .from { font-weight: 700; }
.log .private { background: #fff3d1; }
.log .instructor .from { color: var(--red); }

.banner {
  position: fixed;
  left: 50%;
  bottom: 16px;
  translate: -50% 0;
  z-index: 10;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 600;
  box-shadow: var(--shadow);
}

.chat-form.no-to { grid-template-columns: minmax(0, 1fr) auto auto; }
@media (max-width: 520px) { .chat-form.no-to { grid-template-columns: minmax(0, 1fr) auto; } }
.recipients { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.to-chip {
  padding: 5px 12px;
  border: 1px solid #c8ced6;
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
}
.to-chip:hover { background: #f4f5f7; }
.to-chip[aria-pressed='true'] { background: var(--chip); border-color: var(--chip); color: #fff; }

/* ---- Instructor message popup ---------------------------------------- */
.popup-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgb(0 0 0 / 0.45);
  animation: popup-fade 0.15s ease-out;
}
.popup {
  width: min(560px, 100%);
  max-height: calc(100vh - 32px);
  overflow: auto;
  background: #fff;
  border-radius: 10px;
  border-top: 6px solid var(--red);
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.35);
}
.popup-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  font-weight: 800;
  font-size: 1.1rem;
  color: var(--red);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.popup-close {
  border: 0;
  background: none;
  font-size: 1.8rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 0 4px;
}
.popup-body { display: grid; gap: 14px; padding: 0 16px 16px; }
.popup-msg + .popup-msg { border-top: 1px solid var(--line); padding-top: 12px; }
.popup-meta { color: var(--muted); font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.popup-text { margin: 4px 0 0; font-size: 1.45rem; font-weight: 700; line-height: 1.35; overflow-wrap: anywhere; }
.popup-timer { height: 5px; background: var(--tile); }
.popup-timer > div { height: 100%; background: var(--red); width: 100%; }
@keyframes popup-countdown { from { width: 100%; } to { width: 0; } }
@keyframes popup-fade { from { opacity: 0; } to { opacity: 1; } }

/* ---- Admin ------------------------------------------------------------ */
.admin-main { padding: 16px; display: grid; gap: 16px; max-width: 1400px; margin: 0 auto; }
.grid-2 { display: grid; gap: 16px; grid-template-columns: minmax(260px, 340px) minmax(0, 1fr); align-items: start; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: minmax(0, 1fr); } }
.form-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; }
.form-row > label { flex: 1 1 180px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.table-wrap { overflow-x: auto; }
.code-big { font-size: 2rem; font-weight: 800; letter-spacing: 0.08em; font-variant-numeric: tabular-nums; }
.loc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.loc-list li { display: flex; align-items: center; gap: 4px; border-radius: 6px; }
.loc-list li button.name {
  flex: 1;
  text-align: left;
  border: 0;
  background: none;
  font: inherit;
  padding: 7px 8px;
  border-radius: 6px;
  cursor: pointer;
  color: inherit;
}
.loc-list li.active button.name, .loc-list li button.name:hover { background: var(--tile); }
.loc-list li.active button.name { font-weight: 700; }
.count { color: var(--muted); font-size: 0.8rem; font-weight: 500; }
.media-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.media-card { border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: #fff; display: grid; }
.media-card .thumb { aspect-ratio: 4 / 3; background: #111; }
.media-card .thumb img, .media-card .thumb video { width: 100%; height: 100%; object-fit: cover; display: block; }
.media-card .meta { padding: 8px; display: grid; gap: 6px; }
.media-card .actions { display: flex; gap: 4px; }
.media-card .actions .spacer { flex: 1; }
.progress { height: 6px; border-radius: 3px; background: var(--tile); overflow: hidden; }
.progress > div { height: 100%; background: var(--gold-dark); width: 0; transition: width 0.15s; }
.link-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; }
.units-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
.units-list li { display: flex; align-items: center; gap: 8px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #aab; flex: none; }
.dot.on { background: var(--ok); }
.muted { color: var(--muted); }
.stack { display: grid; gap: 12px; }
.empty { color: var(--muted); padding: 18px; text-align: center; border: 1px dashed var(--line); border-radius: 6px; }
