/* ── Reset & base ────────────────────────────────────────────────────── */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --green-dark: #2d5429;
  --green-mid: #3a6b35;
  --green-body: #5a9e35;
  --green-light: #8dc63f;
  --green-pale: #e8f5d0;
  --bg: #f4f9ee;
  --surface: #ffffff;
  --border: #c8e0a8;
  --text: #1e2e1a;
  --text-muted: #5a6e52;
  --red: #c0392b;
  --red-pale: #fdecea;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow: 0 2px 12px rgba(45, 84, 41, 0.1);
  --font: "Segoe UI", system-ui, sans-serif;
  --font-mono: "Fira Code", "Cascadia Code", "Consolas", monospace;
}

html {
  height: 100%;
}
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  margin: 0;
}

/* ── Login page ──────────────────────────────────────────────────────── */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 2rem 2.4rem 2.4rem;
  width: 100%;
  max-width: 480px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 2rem;
}

.brand-logo {
  width: 64px;
  height: 64px;
  object-fit: contain;
}

.brand-text {
  display: flex;
  flex-direction: column;
}

.brand-title {
  font-size: 28px;
  font-weight: 700;
  color: var(--green-dark);
  line-height: 1;
}

.brand-grove {
  color: var(--green-body);
}

.brand-tagline {
  font-size: 12px;
  color: var(--text-muted);
  letter-spacing: 0.08em;
  margin-top: 3px;
}

.field {
  margin-bottom: 1.2rem;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--green-dark);
  margin-bottom: 6px;
}

.field .hint {
  font-weight: 400;
  color: var(--text-muted);
}

.field input[type="text"] {
  width: 100%;
  padding: 9px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  font-family: var(--font);
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s;
}

.field input[type="text"]:focus {
  outline: none;
  border-color: var(--green-body);
}

/* Turtle grid */
.turtle-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
}

.turtle-option {
  aspect-ratio: 1;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
  transition:
    border-color 0.15s,
    background 0.15s,
    box-shadow 0.15s;
  background: var(--bg);
  user-select: none;
  overflow: hidden;
}

.turtle-option:hover {
  border-color: var(--green-body);
  background: var(--green-pale);
}
.turtle-option.chosen {
  border-color: var(--green-dark);
  border-width: 3px;
  background: var(--green-pale);
  box-shadow: 0 0 0 3px rgba(141, 198, 63, 0.4);
}

/* Role toggle */
.role-toggle {
  display: flex;
  gap: 8px;
  margin-bottom: 1.2rem;
}

.role-btn {
  flex: 1;
  padding: 8px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.role-btn.active {
  border-color: var(--green-dark);
  background: var(--green-pale);
  color: var(--green-dark);
}

/* Join button */
.btn-join {
  width: 100%;
  padding: 11px;
  background: var(--green-dark);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-join:hover {
  background: var(--green-mid);
}

.error-msg {
  margin-top: 0.8rem;
  color: var(--red);
  font-size: 13px;
  text-align: center;
}

/* ── Topbar (shared) ─────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  height: 52px;
  background: var(--green-dark);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 17px;
  flex-shrink: 0;
}

.topbar-logo {
  width: 30px;
  height: 30px;
  object-fit: contain;
}

.trainer-badge {
  background: var(--green-light);
  color: var(--green-dark);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 20px;
}

.topbar-info {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

.hdr-name {
  font-size: 14px;
  font-weight: 600;
}

.mode-badge {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  background: rgba(255, 255, 255, 0.15);
  padding: 2px 8px;
  border-radius: 20px;
}

.mode-badge.collaborative {
  background: var(--green-light);
  color: var(--green-dark);
}

.topbar-roster {
  display: flex;
  gap: 6px;
  align-items: center;
}

.peer-icon {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  title: attr(data-name);
}

/* ── Student page ────────────────────────────────────────────────────── */
.student-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.student-main {
  display: flex;
  flex: 1;
  min-height: 0;
}

.editor-panel {
  width: 500px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 12px;
  gap: 10px;
}

.editor-toolbar {
  display: flex;
  gap: 8px;
}

/* ── Run button base ─────────────────────────────────────────────────── */
.btn-primary {
  padding: 7px 14px;
  background: var(--green-dark);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition:
    background 0.15s,
    border-color 0.15s;
}
.btn-primary:hover {
  background: var(--green-mid);
}

/* ── Pause state: orange ─────────────────────────────────────────────── */
.btn-primary.btn-pause {
  background: #e67e22;
}
.btn-primary.btn-pause:hover {
  background: #ca6f1e;
}

/* ── Resume state: bright green ──────────────────────────────────────── */
.btn-primary.btn-resume {
  background: #27ae60;
}
.btn-primary.btn-resume:hover {
  background: #1e8449;
}

.btn-secondary {
  padding: 7px 14px;
  background: var(--surface);
  color: var(--text-muted);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.btn-secondary:hover {
  border-color: var(--green-body);
  color: var(--green-dark);
}

.btn-ghost {
  padding: 7px 14px;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  cursor: pointer;
}
.btn-ghost:hover {
  background: var(--green-pale);
}

.btn-danger {
  padding: 7px 14px;
  background: var(--red);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  opacity: 0.85;
  transition: opacity 0.15s;
}
.btn-danger:hover {
  opacity: 1;
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

.code-editor {
  flex: 1;
  min-height: 0;
  resize: none;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
  background: #f8fbf4;
  tab-size: 2;
}
.code-editor:focus {
  outline: none;
  border-color: var(--green-body);
}

.error-box {
  padding: 8px 12px;
  background: var(--red-pale);
  border: 1px solid #f5c6c2;
  border-radius: var(--radius);
  color: var(--red);
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}

.help-panel {
  overflow-y: auto;
  max-height: 220px;
}

.cmd-table {
  width: 100%;
  font-size: 11.5px;
  border-collapse: collapse;
}
.cmd-table th,
.cmd-table td {
  padding: 4px 6px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.cmd-table th {
  color: var(--green-dark);
  font-weight: 600;
}
.cmd-table td:first-child {
  font-family: var(--font-mono);
  color: var(--green-mid);
}
.cmd-table td:nth-child(2) {
  font-family: var(--font-mono);
  color: var(--text-muted);
}

.canvas-panel {
  flex: 1;
  min-width: 0;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: #fff;
}
.student-main {
  height: calc(100vh - 52px);
}

#turtle-canvas {
  position: absolute;
  top: 0;
  left: 0;
  border: none;
  display: block;
}

.cursor-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* ── Trainer mosaic ──────────────────────────────────────────────────── */
.trainer-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.mode-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}
.mode-label {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.7);
}

.mode-btn {
  padding: 5px 14px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border: 1.5px solid rgba(255, 255, 255, 0.25);
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}
.mode-btn.active {
  background: var(--green-light);
  border-color: var(--green-light);
  color: var(--green-dark);
  font-weight: 700;
}

.trainer-actions {
  margin-left: 12px;
}

.mosaic {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 10px;
  padding: 10px;
  overflow: hidden;
  background: var(--bg);
  height: calc(100vh - 52px);
  box-sizing: border-box;
}

.mosaic-empty {
  grid-column: 1 / -1;
  grid-row: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 16px;
}

.student-panel {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  cursor: pointer;
  transition:
    border-color 0.15s,
    box-shadow 0.15s;
  position: relative;
}
.student-panel:hover {
  border-color: var(--green-body);
  box-shadow: var(--shadow);
}

.panel-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--green-dark);
  background: var(--green-pale);
  flex-shrink: 0;
}

.panel-turtle {
  font-size: 18px;
}

.panel-canvas-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

.panel-canvas-wrap canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
}

.panel-cursor {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.2);
  width: min(90vw, 700px);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 15px;
  color: var(--green-dark);
}

.modal-header .btn-ghost {
  margin-left: auto;
}

#modal-canvas {
  width: 100%;
  height: auto;
  display: block;
  position: relative;
}

/* Modal fixes */
.modal-overlay[hidden] {
  display: none !important;
}
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
}
.modal-card {
  background: #fff;
  border-radius: 12px;
  width: min(90vw, 720px);
  height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 501;
}
.modal-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header span {
  font-weight: 600;
  font-size: 15px;
  color: var(--green-dark);
  flex: 1;
}
.modal-body {
  flex: 1;
  min-height: 0;
  position: relative;
}
.modal-body canvas {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
}
.modal-body .cursor-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* Mosaic fill fix */
.trainer-page {
  height: 100vh;
  overflow: hidden;
}
.mosaic {
  height: calc(100vh - 52px);
}

/* === FINAL PANEL OVERRIDES === */
.mosaic {
  display: grid !important;
  grid-template-columns: repeat(3, 1fr) !important;
  grid-template-rows: repeat(2, 1fr) !important;
  height: calc(100vh - 52px) !important;
  gap: 8px !important;
  padding: 8px !important;
  box-sizing: border-box !important;
  overflow: hidden !important;
  align-content: stretch !important;
}
.student-panel {
  display: flex !important;
  flex-direction: column !important;
  min-height: 0 !important;
  overflow: hidden !important;
}
.panel-canvas-wrap {
  flex: 1 !important;
  min-height: 0 !important;
  position: relative !important;
  overflow: hidden !important;
  background: #fff !important;
}
.panel-canvas-wrap canvas {
  position: absolute !important;
  top: 0 !important;
  left: 0 !important;
  width: 100% !important;
  height: 100% !important;
}
.panel-cursor {
  position: absolute !important;
  inset: 0 !important;
  pointer-events: none !important;
  width: 100% !important;
  height: 100% !important;
}

.speed-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  margin-left: 8px;
}
.speed-label input[type="range"] {
  width: 80px;
  accent-color: #8dc63f;
  cursor: pointer;
}

.topbar-btn {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border: 1.5px solid rgba(255, 255, 255, 0.25);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.topbar-btn:hover {
  background: rgba(255, 255, 255, 0.22);
}

/* ── Help detail slide-out panel ─────────────────────────────────────────── */
.help-panel {
  overflow-y: auto;
  max-height: 220px;
  position: relative;
}

.cmd-table td:first-child button.cmd-link {
  background: none;
  border: none;
  padding: 0;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--green-mid);
  cursor: pointer;
  text-align: left;
  text-decoration: underline dotted;
  text-underline-offset: 2px;
}
.cmd-table td:first-child button.cmd-link:hover {
  color: var(--green-dark);
}
.cmd-table tr.cmd-active td {
  background: #f0f7ec;
}

/* The slide-out sits below the help table, inside the same aside */
#help-detail {
  display: none;
  border-top: 2px solid var(--green-body);
  background: #f8fbf4;
  padding: 12px 14px;
  font-size: 12px;
  overflow-y: auto;
  max-height: 340px;
  flex-shrink: 0;
}
#help-detail.visible {
  display: block;
}
.hd-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 8px;
}
.hd-name {
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 700;
  color: var(--green-dark);
}
.hd-short {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 6px;
}
.hd-close {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  padding: 0 2px;
}
.hd-close:hover {
  color: var(--red);
}
.hd-desc {
  color: var(--text);
  margin-bottom: 8px;
  line-height: 1.5;
}
.hd-syntax {
  font-family: var(--font-mono);
  font-size: 11.5px;
  background: #e8f0e5;
  border-radius: 4px;
  padding: 4px 8px;
  margin-bottom: 10px;
  color: var(--green-dark);
}
.hd-examples-title {
  font-weight: 700;
  font-size: 11px;
  color: var(--text-muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 6px;
}
.hd-example {
  margin-bottom: 8px;
}
.hd-example-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 3px;
}
.hd-example-code {
  font-family: var(--font-mono);
  font-size: 11.5px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 8px;
  white-space: pre;
  overflow-x: auto;
  color: var(--text);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 6px;
}
.hd-example-code pre {
  margin: 0;
  flex: 1;
  white-space: pre;
}
.hd-load-btn {
  background: var(--green-dark);
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 10px;
  padding: 3px 7px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  align-self: flex-start;
  margin-top: 1px;
}
.hd-load-btn:hover {
  background: var(--green-mid);
}
