:root {
  --bg: #f4f0e8;
  --surface: #fffaf1;
  --surface-strong: #ffffff;
  --ink: #211b18;
  --muted: #756b62;
  --line: rgba(45, 35, 28, 0.14);
  --line-strong: rgba(45, 35, 28, 0.24);
  --brand: #8f1f2a;
  --brand-dark: #66151d;
  --gold: #c79738;
  --green: #2f8d58;
  --blue: #2d6fa8;
  --board-light: #f0d7aa;
  --board-dark: #9a5e35;
  --shadow: 0 18px 50px rgba(45, 35, 28, 0.16);
}

* { box-sizing: border-box; }

body {
  min-height: 100vh;
  margin: 0;
  background:
    linear-gradient(135deg, rgba(143, 31, 42, 0.08), transparent 34%),
    linear-gradient(225deg, rgba(47, 141, 88, 0.08), transparent 32%),
    var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", Arial, sans-serif;
}

.chess-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.chess-topbar {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 12px 22px;
  background: #1f1815;
  color: #fff;
  box-shadow: 0 2px 18px rgba(20, 14, 11, 0.18);
}

.brand-mini {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: #fff;
  text-decoration: none;
  font-weight: 850;
  letter-spacing: 0;
}

.brand-mini img {
  width: 42px;
  height: 42px;
  object-fit: contain;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
}

.session-pill {
  min-width: 190px;
  text-align: center;
  padding: 9px 14px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: rgba(255, 255, 255, 0.86);
  font-size: 0.88rem;
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 288px minmax(520px, 1fr) 318px;
  min-height: 0;
  padding: 12px;
  gap: 12px;
}

.chess-shell.student-view .workspace {
  grid-template-columns: minmax(560px, 1fr) minmax(280px, 360px);
  align-items: stretch;
}

.chess-shell.student-view .status-row,
.chess-shell.student-view .board-wrap,
.chess-shell.student-view .student-lock {
  width: min(100%, calc(100vh - 260px), 780px);
}

.tool-panel,
.info-panel {
  overflow: auto;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 250, 241, 0.94);
  box-shadow: 0 10px 28px rgba(45, 35, 28, 0.08);
}

.panel-section {
  padding: 16px 0;
  border-bottom: 1px solid var(--line);
}

.panel-section:first-child { padding-top: 0; }
.panel-section:last-child { border-bottom: 0; padding-bottom: 0; }

.panel-section h2 {
  margin: 0 0 12px;
  color: #2b211d;
  font-size: 1rem;
  font-weight: 850;
  letter-spacing: 0;
}

.panel-section label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

textarea,
input,
select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--surface-strong);
  color: var(--ink);
  outline: none;
  font: inherit;
}

textarea {
  resize: vertical;
  font-family: Consolas, "Courier New", monospace;
  font-size: 0.84rem;
  line-height: 1.45;
}

textarea:focus,
input:focus,
select:focus {
  border-color: rgba(143, 31, 42, 0.52);
  box-shadow: 0 0 0 3px rgba(143, 31, 42, 0.10);
}

.icon-btn,
.seg-btn,
.tool-btn,
.primary-btn,
.piece-pick {
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: var(--surface-strong);
  cursor: pointer;
  transition: background .18s ease, border-color .18s ease, color .18s ease, transform .18s ease;
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: inline-grid;
  place-items: center;
  color: #fff;
  text-decoration: none;
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.16);
}

.icon-btn:hover {
  background: rgba(199, 151, 56, 0.24);
  border-color: rgba(199, 151, 56, 0.5);
}

.segmented {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 7px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f0e5d4;
}

.seg-btn {
  height: 40px;
  background: transparent;
  font-size: 0.95rem;
}

.seg-btn:hover,
.tool-btn:hover {
  border-color: rgba(143, 31, 42, 0.35);
  background: #fff4e3;
}

.seg-btn.active {
  border-color: rgba(143, 31, 42, 0.55);
  background: var(--brand);
  color: #fff;
}

.piece-bank {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 7px;
}

.piece-pick {
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  background: #f7ecdc;
  color: #201916;
  font-size: 1.75rem;
}

.piece-pick:hover,
.piece-pick.active {
  border-color: var(--gold);
  background: #fff8e9;
  box-shadow: inset 0 0 0 2px rgba(199, 151, 56, 0.35);
}

.compact-grid,
.two-actions,
.game-box {
  display: grid;
  gap: 9px;
}

.compact-grid,
.two-actions {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.tool-btn,
.primary-btn {
  min-height: 42px;
  padding: 9px 12px;
  font-weight: 800;
}

.primary-btn {
  width: 100%;
  margin-top: 8px;
  border-color: var(--brand);
  background: var(--brand);
  color: #fff;
}

.primary-btn:hover {
  background: var(--brand-dark);
  border-color: var(--brand-dark);
}

.board-stage {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.54);
  box-shadow: var(--shadow);
}

.status-row {
  width: min(84vh, 100%);
  max-width: 920px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 250, 241, 0.9);
  color: var(--muted);
  font-size: 0.92rem;
}

.board-status-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.zoom-board-btn {
  width: 38px;
  height: 38px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  color: var(--brand);
  cursor: pointer;
}

.zoom-board-btn:hover {
  border-color: rgba(143, 31, 42, 0.38);
  background: #fff4e3;
}

.status-row strong {
  color: var(--ink);
}

.live-dot {
  width: 10px;
  height: 10px;
  display: inline-block;
  margin-right: 8px;
  border-radius: 50%;
  background: #a29a92;
  box-shadow: 0 0 0 4px rgba(162, 154, 146, 0.14);
}

.live-dot.online {
  background: var(--green);
  box-shadow: 0 0 0 4px rgba(47, 141, 88, 0.16);
}

.board-wrap {
  position: relative;
  width: min(84vh, 100%);
  max-width: 920px;
  aspect-ratio: 1;
  padding: 12px;
  border-radius: 12px;
  background: #2a1b14;
  box-shadow: 0 24px 56px rgba(45, 35, 28, 0.28);
}

.chess-board {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 7px;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  border: 0;
  padding: 0;
  font-size: clamp(1.7rem, 6.7vh, 4.6rem);
  line-height: 1;
  cursor: pointer;
  user-select: none;
}

.square[draggable="true"] {
  cursor: grab;
}

.square[draggable="true"]:active {
  cursor: grabbing;
}

.square.light { background: var(--board-light); }
.square.dark { background: var(--board-dark); }

.square.selected {
  outline: 4px solid rgba(47, 141, 88, 0.92);
  outline-offset: -4px;
}

.square.highlight {
  box-shadow: inset 0 0 0 999px rgba(248, 212, 89, 0.36);
}

.square.circle::after {
  content: "";
  position: absolute;
  inset: 20%;
  border: 5px solid rgba(45, 111, 168, 0.72);
  border-radius: 50%;
}

.square::before {
  position: absolute;
  left: 6px;
  bottom: 5px;
  color: rgba(34, 25, 19, 0.46);
  font-size: 0.64rem;
  font-weight: 900;
}

.square.rank-file::before { content: attr(data-square); }

.piece.white {
  color: #fff8e9;
  text-shadow: 0 2px 2px rgba(0, 0, 0, 0.42);
}

.piece.black {
  color: #17110e;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.24);
}

.board-overlay {
  pointer-events: none;
  position: absolute;
  inset: 12px;
}

.student-lock {
  width: min(84vh, 100%);
  max-width: 920px;
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 250, 241, 0.88);
  color: var(--muted);
  text-align: center;
  font-size: 0.92rem;
}

.chess-shell.board-zoom .board-stage {
  position: fixed;
  z-index: 2000;
  inset: 0;
  border: 0;
  border-radius: 0;
  padding: 18px;
  background:
    linear-gradient(135deg, rgba(143, 31, 42, 0.12), transparent 34%),
    linear-gradient(225deg, rgba(47, 141, 88, 0.12), transparent 32%),
    var(--bg);
}

.chess-shell.board-zoom .status-row,
.chess-shell.board-zoom .student-lock {
  width: min(96vh, 96vw);
  max-width: 1040px;
}

.chess-shell.board-zoom .board-wrap {
  width: min(96vh, 96vw);
  max-width: 1040px;
}

.chess-shell.board-zoom .zoom-board-btn {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

.student-lock.enabled {
  border-color: rgba(47, 141, 88, 0.3);
  color: var(--green);
  background: rgba(47, 141, 88, 0.08);
}

.toggle-row {
  display: flex !important;
  align-items: center;
  gap: 9px;
}

.toggle-row input {
  width: 18px;
  min-height: 18px;
  accent-color: var(--brand);
}

.students-list,
.pending-list,
.exercise-list,
.progress-list,
.tournament-box {
  display: grid;
  gap: 9px;
}

.student-item,
.pending-item,
.exercise-item,
.progress-item,
.tournament-item,
.live-exercise-box {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-strong);
  color: var(--ink);
}

.student-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.pending-item strong,
.progress-item strong,
.tournament-item strong {
  color: var(--brand-dark);
}

.pending-actions {
  display: flex;
  gap: 8px;
  margin-top: 9px;
}

.ok-btn,
.bad-btn {
  flex: 1;
  min-height: 34px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  cursor: pointer;
  font-weight: 850;
}

.ok-btn { background: var(--green); }
.bad-btn { background: var(--brand); }

.exercise-form {
  display: grid;
  gap: 9px;
  margin-bottom: 14px;
}

.exercise-item h3 {
  margin: 0 0 5px;
  color: #2b211d;
  font-size: 0.98rem;
}

.exercise-item p {
  margin: 0 0 9px;
  color: var(--muted);
  font-size: 0.86rem;
}

.exercise-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.live-exercise-box {
  border-color: rgba(199, 151, 56, 0.34);
  background: #fff5df;
  color: #59442a;
}

.teacher-only.hidden,
.student-only.hidden,
.hidden { display: none !important; }

@media (max-width: 1220px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .chess-shell.student-view .workspace {
    grid-template-columns: 1fr;
  }

  .board-stage {
    order: -1;
  }

  .status-row,
  .board-wrap,
  .student-lock,
  .chess-shell.student-view .status-row,
  .chess-shell.student-view .board-wrap,
  .chess-shell.student-view .student-lock {
    width: min(92vw, 760px);
  }
}

@media (max-width: 680px) {
  .chess-topbar {
    align-items: flex-start;
    flex-wrap: wrap;
    padding: 12px;
  }

  .session-pill {
    order: 3;
    width: 100%;
  }

  .workspace {
    padding: 10px;
    gap: 10px;
  }

  .tool-panel,
  .info-panel,
  .board-stage {
    border-radius: 9px;
    padding: 12px;
  }

  .board-wrap {
    padding: 7px;
  }

  .board-overlay {
    inset: 7px;
  }

  .square {
    font-size: clamp(1.35rem, 9vw, 2.8rem);
  }

  .compact-grid,
  .exercise-actions {
    grid-template-columns: 1fr;
  }
}
