/* ==========================================================================
   GeoGebra Ink — Giao diện lớp học: nền trắng, chữ lớn, nút to (>= 48px)
   ========================================================================== */

:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --ink: #111827;
  --ink-2: #374151;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-2: #d1d5db;
  --brand: #1d4ed8;
  --brand-soft: #e8efff;
  --brand-ink: #1e3a8a;
  --danger: #dc2626;
  --ok: #15803d;
  --radius: 16px;
  --radius-sm: 12px;
  --shadow: 0 6px 24px rgba(17, 24, 39, 0.08);
  --shadow-lg: 0 18px 48px rgba(17, 24, 39, 0.18);
  --toolbar-h: 84px;
  --header-h: 60px;
  --font: "Be Vietnam Pro", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 17px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

/* ── Thanh tiêu đề ─────────────────────────────────────────── */
.topbar {
  height: var(--header-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.brand-logo {
  width: 40px; height: 40px; border-radius: 12px; flex-shrink: 0;
  background: linear-gradient(135deg, #2563eb, #7c3aed);
  display: grid; place-items: center; color: #fff;
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.35);
}

.brand-name { font-weight: 800; font-size: 19px; letter-spacing: -0.01em; white-space: nowrap; }
.brand-name span { color: var(--brand); }

.figure-title {
  font-size: 15px; font-weight: 600; color: var(--ink-2);
  background: var(--bg); border: 1px solid var(--line);
  padding: 8px 14px; border-radius: 999px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55vw;
}

/* ── Sân khấu: hình GeoGebra + khung đối chiếu Colab bên cạnh ── */
.stage-wrap {
  flex: 1;
  min-height: 0;
  padding: 12px;
  display: flex;
  gap: 12px;
}

/* Khi mở bảng "Thêm tiện ích" trên màn hình rộng: thu gọn sân khấu sang trái để nhìn rõ cả hình lẫn phép tính */
@media (min-width: 960px) {
  body.sheet-open .stage-wrap {
    padding-right: 452px;
  }
  body.sheet-open .scrim {
    display: none !important;
  }
}

.side-verify {
  width: 310px;
  flex-shrink: 0;
  background: var(--surface);
  border: 1.5px solid #c7d2fe;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}

@media (min-width: 960px) {
  body.sheet-open .side-verify {
    display: none !important;
  }
}

.side-verify-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 16px;
  color: var(--brand-ink);
}

.side-verify-close {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 18px;
  font-weight: 700;
}

.side-verify-code {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: #0f172a;
  color: #e2e8f0;
  font-family: "Consolas", "Courier New", monospace;
  font-size: 14.5px;
  white-space: pre-wrap;
  word-break: break-word;
}

.side-verify-out {
  padding: 12px;
  border-radius: 12px;
  background: #f8fafc;
  border: 1.5px solid var(--line);
  font-size: 16px;
  line-height: 1.5;
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  isolation: isolate;
}

#ggbHost { position: absolute; inset: 0; z-index: 1; }
#ggbHost > div, #ggbHost iframe { border: 0 !important; }

#formulaLayer { position: absolute; inset: 0; z-index: 3; pointer-events: none; }

.ink-canvas {
  position: absolute; inset: 0; width: 100%; height: 100%;
  z-index: 4; pointer-events: none; touch-action: none;
}

#liveCanvas { z-index: 5; }

/* Khi đang viết/tẩy/com-pa: lớp mực nhận thao tác, hình GeoGebra đứng yên */
.stage.is-drawing #inkCanvas { pointer-events: auto; cursor: crosshair; }
.stage.is-erasing #inkCanvas { cursor: cell; }

#toolLayer { position: absolute; inset: 0; z-index: 6; pointer-events: none; }

/* Nhãn chế độ đang dùng */
.mode-chip {
  position: absolute; top: 12px; left: 12px; z-index: 8;
  min-height: 48px; padding: 0 16px;
  display: inline-flex; align-items: center; gap: 8px;
  border: 1px solid var(--line-2); border-radius: 999px;
  background: rgba(255, 255, 255, 0.94);
  font-weight: 700; font-size: 15px; color: var(--ink-2);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
  backdrop-filter: blur(6px);
}
.mode-chip.is-pen { background: #eef4ff; border-color: #bfd3ff; color: var(--brand-ink); }
.mode-chip.is-eraser { background: #fff1f2; border-color: #fecdd3; color: #9f1239; }

/* Nút phóng to / thu nhỏ trên hình */
.view-dock {
  position: absolute; right: 12px; top: 12px; z-index: 8;
  display: flex; flex-direction: column; gap: 8px;
}

.round-btn {
  width: 52px; height: 52px; border-radius: 50%;
  border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.96);
  display: grid; place-items: center; font-size: 22px; font-weight: 700; color: var(--ink-2);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  transition: transform 0.12s ease, background 0.15s ease;
}
.round-btn:hover { background: #f3f4f6; }
.round-btn:active { transform: scale(0.94); }
.round-btn.is-on { background: var(--brand); color: #fff; border-color: var(--brand); }

/* Thanh trượt tham số (ví dụ: chiều cao h) */
.param-dock {
  position: absolute; left: 12px; bottom: 12px; z-index: 8;
  display: flex; flex-direction: column; gap: 8px;
  max-width: min(420px, calc(100% - 24px));
}

.param-item {
  display: flex; align-items: center; gap: 12px;
  background: rgba(255, 255, 255, 0.96); border: 1px solid var(--line-2);
  border-radius: 14px; padding: 8px 14px; min-height: 56px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}
.param-item label { font-weight: 700; font-size: 15px; white-space: nowrap; }
.param-item output { font-weight: 800; color: var(--brand); min-width: 44px; text-align: right; font-variant-numeric: tabular-nums; }

input[type="range"] { -webkit-appearance: none; appearance: none; flex: 1; min-width: 120px; height: 36px; background: transparent; }
input[type="range"]::-webkit-slider-runnable-track { height: 8px; border-radius: 99px; background: linear-gradient(90deg, #93b4ff, #2563eb); }
input[type="range"]::-moz-range-track { height: 8px; border-radius: 99px; background: linear-gradient(90deg, #93b4ff, #2563eb); }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 28px; height: 28px; margin-top: -10px; border-radius: 50%; background: #fff; border: 3px solid #2563eb; box-shadow: 0 2px 6px rgba(0,0,0,0.2); }
input[type="range"]::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: #fff; border: 3px solid #2563eb; }

.stage-loading {
  position: absolute; inset: 0; z-index: 9;
  display: grid; place-items: center; background: #fff;
  font-weight: 600; color: var(--muted); text-align: center; padding: 24px;
}
.spinner {
  width: 44px; height: 44px; margin: 0 auto 14px; border-radius: 50%;
  border: 4px solid #dbe6ff; border-top-color: var(--brand);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Thanh công cụ chính (8 nút lớn) ───────────────────────── */
.toolbar {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 8px;
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.tool-btn {
  position: relative;
  min-height: 64px;
  border: 1.5px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  font-size: 15px; font-weight: 700; color: var(--ink-2);
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
  user-select: none;
}
.tool-btn svg { width: 28px; height: 28px; }
.tool-btn:hover { background: #f3f4f6; }
.tool-btn:active { transform: scale(0.96); }
.tool-btn.is-active { background: var(--brand-soft); border-color: #bcd0ff; color: var(--brand-ink); }
.tool-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.tool-btn.primary { background: var(--brand); color: #fff; }
.tool-btn.primary:hover { background: #1e40af; }

.color-dot {
  width: 26px; height: 26px; border-radius: 50%;
  border: 3px solid #fff; box-shadow: 0 0 0 2px var(--line-2);
}
.size-dot { display: grid; place-items: center; width: 28px; height: 28px; }
.size-dot i { display: block; border-radius: 50%; background: currentColor; }

/* ── Bảng chọn nhỏ (Màu / Độ dày) ──────────────────────────── */
.popover {
  position: fixed; z-index: 40;
  background: #fff; border: 1px solid var(--line); border-radius: 18px;
  box-shadow: var(--shadow-lg); padding: 14px;
  display: flex; gap: 12px;
  animation: pop 0.14s ease-out;
}
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }

.swatch {
  width: 60px; height: 60px; border-radius: 50%;
  border: 4px solid #fff; box-shadow: 0 0 0 2px var(--line-2);
}
.swatch.is-active { box-shadow: 0 0 0 4px var(--ink); }

.size-option {
  width: 76px; height: 76px; border-radius: 16px;
  border: 2px solid var(--line); background: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  font-size: 14px; font-weight: 700; color: var(--ink-2);
}
.size-option i { display: block; border-radius: 99px; background: var(--ink); }
.size-option.is-active { border-color: var(--brand); background: var(--brand-soft); color: var(--brand-ink); }

/* ── Bảng "Thêm" ───────────────────────────────────────────── */
.scrim { position: fixed; inset: 0; z-index: 50; background: rgba(17, 24, 39, 0.35); animation: fade 0.15s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  position: fixed; z-index: 51;
  right: 0; top: 0; bottom: 0; width: min(460px, 100vw);
  background: #fff; box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  animation: slideIn 0.2s ease-out;
}
@keyframes slideIn { from { transform: translateX(30px); opacity: 0; } to { transform: none; opacity: 1; } }

.sheet-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 22px; }

.close-btn {
  width: 52px; height: 52px; border-radius: 14px; border: 1px solid var(--line);
  background: #fff; font-size: 24px; display: grid; place-items: center;
}

.tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.tabs.tabs-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.tab {
  min-height: 50px; border-radius: 12px; border: 1.5px solid var(--line); background: #fff;
  font-weight: 700; font-size: 13.5px; color: var(--ink-2); padding: 4px 2px;
}
.tab.is-active { background: var(--brand); border-color: var(--brand); color: #fff; }

/* ── Ô mã kiểm chứng kiểu Google Colab ─────────────────────── */
.colab-cell {
  background: #f8fafc;
  border: 1.5px solid #cbd5e1;
  border-radius: 16px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.colab-cell-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.colab-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  background: #e0e7ff;
  color: #1e3a8a;
}

.colab-badge.is-pyodide {
  background: #dcfce7;
  color: #166534;
}

.colab-editor {
  width: 100%;
  min-height: 98px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1.5px solid #334155;
  background: #0f172a;
  color: #f8fafc;
  font-family: "Consolas", "Courier New", monospace;
  font-size: 16.5px;
  line-height: 1.5;
  resize: vertical;
  outline: none;
}

.colab-editor:focus {
  border-color: #60a5fa;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
}

.colab-student-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.colab-student-row label {
  font-size: 14.5px;
  font-weight: 700;
  color: var(--ink-2);
}

.colab-ans-input {
  min-height: 50px;
  font-family: "Consolas", "Courier New", monospace;
  font-size: 16px;
}

.colab-run-btn {
  min-height: 54px;
  font-size: 18px;
  letter-spacing: 0.01em;
}

.colab-output {
  margin-top: 12px;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1.5px solid var(--line-2);
  background: #ffffff;
  font-size: 17px;
  line-height: 1.55;
}

.colab-output-placeholder {
  color: var(--muted);
  font-size: 15.5px;
}

.colab-res-label {
  font-size: 14px;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 4px;
}

.colab-res-value {
  font-size: 20px;
  font-weight: 800;
  color: var(--ink);
  word-break: break-word;
}

.colab-check-banner {
  margin-top: 10px;
  padding: 12px 14px;
  border-radius: 12px;
  font-weight: 800;
  font-size: 17.5px;
}

.colab-check-ok {
  background: #dcfce7;
  border: 1.5px solid #86efac;
  color: #15803d;
}

.colab-check-bad {
  background: #fee2e2;
  border: 1.5px solid #fca5a5;
  color: #b91c1c;
}

.colab-engine-note {
  margin-top: 8px;
  font-size: 13.5px;
  color: var(--muted);
}

.sheet-body { flex: 1; overflow-y: auto; padding: 16px; }
.panel { display: none; }
.panel.is-active { display: block; }

.group { margin-bottom: 22px; }
.group h3 { margin: 0 0 10px; font-size: 18px; }
.group p.hint { margin: -4px 0 12px; color: var(--muted); font-size: 15px; line-height: 1.5; }

.btn {
  min-height: 52px; padding: 10px 18px; border-radius: 12px;
  border: 1.5px solid var(--line-2); background: #fff;
  font-weight: 700; font-size: 16px; color: var(--ink);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn:hover { background: #f9fafb; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: #1e40af; }
.btn.block { width: 100%; }
.btn.is-on { background: var(--brand-soft); border-color: var(--brand); color: var(--brand-ink); }

.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn-grid.three { grid-template-columns: repeat(3, 1fr); }

.example-card {
  display: flex; align-items: center; gap: 14px; width: 100%;
  min-height: 64px; padding: 10px 14px; margin-bottom: 10px;
  border: 1.5px solid var(--line); border-radius: 14px; background: #fff; text-align: left;
}
.example-card strong { display: block; font-size: 16.5px; }
.example-card span { color: var(--muted); font-size: 14.5px; }
.example-card .ex-icon { width: 44px; height: 44px; border-radius: 12px; background: var(--brand-soft); display: grid; place-items: center; font-size: 22px; flex-shrink: 0; }
.example-card.is-active { border-color: var(--brand); background: #f5f8ff; }

.field {
  width: 100%; min-height: 54px; padding: 12px 14px;
  border: 1.5px solid var(--line-2); border-radius: 12px; background: #fff;
  font-size: 17px; outline: none;
}
.field:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
textarea.field { min-height: 84px; resize: vertical; font-family: "Consolas", "Courier New", monospace; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.chip {
  min-height: 44px; padding: 6px 14px; border-radius: 999px;
  border: 1.5px solid var(--line-2); background: #fff; font-weight: 600; font-size: 15px;
}
.chip.is-active { background: var(--brand); border-color: var(--brand); color: #fff; }

.preview-box {
  min-height: 80px; margin: 10px 0; padding: 14px;
  border: 1.5px dashed var(--line-2); border-radius: 14px; background: #fcfcfd;
  display: grid; place-items: center; overflow-x: auto; font-size: 20px;
}
.preview-box.is-error { color: var(--danger); font-size: 15px; }

.result-box {
  margin-top: 10px; padding: 14px; border-radius: 14px;
  background: #f0f6ff; border: 1.5px solid #c9dafd;
  font-size: 19px; font-weight: 600; min-height: 56px; overflow-x: auto;
}
.result-box small { display: block; color: var(--muted); font-size: 14px; font-weight: 600; margin-bottom: 4px; }

.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 60px; padding: 8px 14px; margin-bottom: 10px;
  border: 1.5px solid var(--line); border-radius: 14px;
}
.switch-row strong { font-size: 16px; display: block; }
.switch-row span { color: var(--muted); font-size: 14px; }

.switch { position: relative; width: 58px; height: 34px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track { position: absolute; inset: 0; border-radius: 99px; background: #d1d5db; transition: 0.2s; }
.switch .track::before { content: ""; position: absolute; width: 28px; height: 28px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: 0.2s; box-shadow: 0 1px 4px rgba(0,0,0,0.25); }
.switch input:checked + .track { background: var(--brand); }
.switch input:checked + .track::before { transform: translateX(24px); }

/* ── Thẻ công thức đặt trên hình ───────────────────────────── */
.formula-card {
  position: absolute; pointer-events: auto; touch-action: none;
  background: rgba(255, 255, 255, 0.97); border: 1.5px solid #c7d2fe; border-radius: 12px;
  padding: 10px 14px; box-shadow: 0 4px 16px rgba(30, 58, 138, 0.12);
  cursor: grab; user-select: none; transform-origin: top left;
}
.formula-card:active { cursor: grabbing; }
.formula-card svg { display: block; }
.formula-card .fc-actions {
  position: absolute; top: -18px; right: -14px; display: none; gap: 6px;
}
.formula-card:hover .fc-actions, .formula-card.is-selected .fc-actions { display: flex; }
.fc-actions button {
  width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line-2);
  background: #fff; font-weight: 800; font-size: 16px; display: grid; place-items: center;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
}
.fc-actions .fc-del { color: var(--danger); }

/* ── Dụng cụ: thước, thước đo góc, com-pa ──────────────────── */
.tool-ruler, .tool-protractor {
  position: absolute; left: 0; top: 0; pointer-events: auto; touch-action: none;
  transform-origin: 0 0; cursor: move; user-select: none;
}
.tool-ruler svg, .tool-protractor svg { display: block; overflow: visible; filter: drop-shadow(0 4px 10px rgba(0,0,0,0.15)); }

.tool-handle {
  position: absolute; width: 48px; height: 48px; border-radius: 50%;
  background: var(--brand); color: #fff; border: 3px solid #fff;
  display: grid; place-items: center; font-size: 20px; font-weight: 800;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25); cursor: grab; touch-action: none;
}
.tool-close {
  position: absolute; width: 40px; height: 40px; border-radius: 50%;
  background: #fff; color: var(--danger); border: 1.5px solid var(--line-2);
  font-size: 18px; font-weight: 800; display: grid; place-items: center;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}
.tool-badge {
  position: absolute; padding: 4px 10px; border-radius: 99px;
  background: #111827; color: #fff; font-size: 14px; font-weight: 700; white-space: nowrap;
}

/* ── Thông báo nhanh ───────────────────────────────────────── */
.toast-wrap {
  position: fixed; left: 50%; bottom: calc(var(--toolbar-h) + 24px); transform: translateX(-50%);
  z-index: 80; display: flex; flex-direction: column; gap: 8px; align-items: center;
  pointer-events: none; width: max-content; max-width: calc(100vw - 32px);
}
.toast {
  background: #111827; color: #fff; padding: 12px 18px; border-radius: 14px;
  font-weight: 600; font-size: 15.5px; box-shadow: var(--shadow-lg);
  animation: pop 0.18s ease-out; text-align: center;
}
.toast.ok { background: #14532d; }
.toast.err { background: #991b1b; }

/* ── Điện thoại & máy tính bảng ───────────────────────────── */
@media (max-width: 700px) {
  .figure-title { display: none; }
  .stage-wrap { padding: 8px; }
  .tool-btn { min-height: 58px; font-size: 13.5px; }
  .tool-btn svg { width: 24px; height: 24px; }
  .toolbar { gap: 4px; padding-left: 6px; padding-right: 6px; }
}

@media (max-width: 520px) {
  .toolbar { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .tool-btn { min-height: 54px; flex-direction: row; gap: 6px; font-size: 14px; }
  .param-item { min-height: 50px; padding: 6px 12px; }
  .round-btn { width: 48px; height: 48px; font-size: 20px; }
  .sheet { width: 100vw; }
}

@media (min-width: 1100px) {
  .toolbar { max-width: 1100px; margin: 0 auto 10px; width: calc(100% - 24px); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow); padding: 8px; }
}

@media print {
  body * { visibility: hidden; }
}
