:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --user-bubble: #2563eb;
  --tutor-bubble: #ffffff;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* ---------- header ---------- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 42px; height: 42px;
  background: var(--accent);
  color: #fff;
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; font-weight: 700;
}
.brand h1 { margin: 0; font-size: 18px; }
.brand p { margin: 0; font-size: 12px; color: var(--muted); }
.header-actions { display: flex; align-items: center; gap: 8px; }
.badge {
  font-size: 11px;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 4px 10px;
  border-radius: 999px;
  font-weight: 600;
}
.ghost-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}
.ghost-btn:hover { background: var(--bg); }

/* ---------- layout ---------- */
main {
  flex: 1;
  display: flex;
  min-height: 0;
}
#chatPanel {
  flex: 1.1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
#canvasPanel {
  flex: 1;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  background: var(--panel);
  min-width: 0;
}
#canvasPanel.hidden { display: none; }

/* ---------- messages ---------- */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 15px;
  word-wrap: break-word;
}
.msg.user {
  align-self: flex-end;
  background: var(--user-bubble);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.user .katex { color: #fff; }
.msg.assistant {
  align-self: flex-start;
  background: var(--tutor-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.assistant p:first-child { margin-top: 0; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg img.attachment {
  max-width: 220px;
  max-height: 220px;
  border-radius: 8px;
  display: block;
  margin-top: 6px;
}
.msg .plot-chip {
  display: inline-block;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12.5px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  margin: 4px 0;
  cursor: pointer;
}
.msg.error { background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c; align-self: center; font-size: 13px; }
.typing { color: var(--muted); font-style: italic; }

.msg.assistant pre {
  background: #f1f5f9;
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
}
.msg.assistant table { border-collapse: collapse; margin: 8px 0; }
.msg.assistant td, .msg.assistant th { border: 1px solid var(--border); padding: 4px 10px; }

/* ---------- input bar ---------- */
#attachPreview {
  display: flex;
  gap: 8px;
  padding: 0 16px;
  flex-wrap: wrap;
}
.preview-item {
  position: relative;
  width: 64px; height: 64px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  margin-bottom: 6px;
}
.preview-item img { width: 100%; height: 100%; object-fit: cover; }
.preview-item .remove {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 11px;
  cursor: pointer;
  line-height: 1;
}
#inputBar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px 16px;
  background: var(--bg);
}
.icon-btn {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 42px; height: 42px;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--accent-soft); }
#input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 15px;
  font-family: inherit;
  max-height: 140px;
  outline: none;
  background: var(--panel);
}
#input:focus { border-color: var(--accent); }
.send-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  width: 42px; height: 42px;
  font-size: 17px;
  cursor: pointer;
  flex-shrink: 0;
}
.send-btn:disabled { opacity: 0.5; cursor: default; }

/* ---------- canvas ---------- */
#canvasToolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.tool-btn {
  background: none;
  border: 1px solid transparent;
  border-radius: 8px;
  width: 34px; height: 34px;
  font-size: 16px;
  cursor: pointer;
}
.tool-btn.active { background: var(--accent-soft); border-color: var(--accent); }
.tool-btn:hover { background: var(--bg); }
#penColor { width: 30px; height: 30px; border: none; background: none; cursor: pointer; padding: 0; }
#penSize { border: 1px solid var(--border); border-radius: 8px; padding: 5px; font-size: 13px; }
.spacer { flex: 1; }
.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.primary-btn:hover { filter: brightness(1.1); }
#canvasWrap {
  flex: 1;
  position: relative;
  min-height: 0;
  background:
    linear-gradient(#eef2f7 1px, transparent 1px),
    linear-gradient(90deg, #eef2f7 1px, transparent 1px);
  background-size: 24px 24px;
}
#canvasWrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
#drawLayer { touch-action: none; cursor: crosshair; }
.canvas-hint {
  margin: 0;
  padding: 6px 12px;
  font-size: 12px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

/* ---------- voice ---------- */
.icon-btn.recording {
  background: #fee2e2;
  border-color: #dc2626;
  animation: pulse 1.2s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.35); }
  50% { box-shadow: 0 0 0 7px rgba(220, 38, 38, 0); }
}
.speak-btn {
  display: block;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  opacity: 0.45;
  padding: 4px 0 0;
}
.speak-btn:hover { opacity: 1; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  main { flex-direction: column; }
  #canvasPanel {
    border-left: none;
    border-top: 1px solid var(--border);
    min-height: 45vh;
  }
}

/* ---------- curriculum drawer ---------- */
#drawerOverlay {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.35);
  z-index: 40;
}
#curriculumDrawer {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: min(400px, 92vw);
  background: var(--panel);
  z-index: 50;
  box-shadow: 4px 0 30px rgba(0,0,0,0.15);
  display: flex; flex-direction: column;
  overflow-y: auto;
}
#drawerOverlay.hidden, #curriculumDrawer.hidden { display: none; }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 4px;
}
.drawer-head h2 { margin: 0; font-size: 17px; }
.drawer-sub {
  margin: 0 16px 10px; font-size: 12px; color: var(--muted);
}
.unit {
  border-top: 1px solid var(--border);
}
.unit-head {
  width: 100%;
  display: flex; align-items: center; justify-content: space-between;
  background: none; border: none; cursor: pointer;
  padding: 12px 16px;
  font-size: 14.5px; font-weight: 600; color: var(--text);
  text-align: left;
}
.unit-head:hover { background: var(--bg); }
.unit-head .strand { display: block; font-size: 11px; font-weight: 400; color: var(--muted); }
.unit-topics { padding: 0 8px 8px; }
.unit-topics.collapsed { display: none; }
.topic-item {
  width: 100%;
  display: flex; align-items: center; gap: 8px;
  background: none; border: none; cursor: pointer;
  padding: 8px 10px;
  font-size: 13.5px; color: var(--text);
  text-align: left; border-radius: 8px;
}
.topic-item:hover { background: var(--accent-soft); }
.topic-item .tick { color: #059669; font-weight: 700; width: 14px; flex-shrink: 0; }

/* ---------- chapter-mode: progress bars + topic state dots ---------- */
.unit-head { flex-wrap: wrap; gap: 8px; }
.unit-progress { display: flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 500; color: var(--muted); }
.unit-bar { width: 54px; height: 6px; border-radius: 999px; background: var(--border); overflow: hidden; }
.unit-bar-fill { display: block; height: 100%; width: 0%; background: var(--accent); transition: width 0.3s ease; }
.unit-pct { min-width: 32px; text-align: right; }

.state-dot {
  width: 11px; height: 11px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: transparent;
  flex-shrink: 0;
}
.state-dot.practising { border-color: #d97706; background: #fde68a; }
.state-dot.mastered { border-color: #059669; background: #059669; }

.ghost-btn.active-mode { background: var(--accent); color: #fff; border-color: var(--accent); }

.continue-card {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 12px 16px 0;
  padding: 10px 14px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: 10px;
  font-size: 13.5px;
  color: var(--text);
}
.continue-card.hidden { display: none; }
.continue-card span:first-child { flex: 1; }

/* ---------- live voice mode ---------- */
.ghost-btn.live-on {
  background: #fee2e2;
  border-color: #dc2626;
  color: #b91c1c;
  font-weight: 600;
}
.live-status {
  position: fixed;
  top: 64px; left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 16px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 4px 14px rgba(0,0,0,0.08);
}
.live-status.hidden { display: none; }
.live-status.listening { color: #059669; border-color: #059669; }
.live-status.speaking { color: var(--accent); border-color: var(--accent); }
.live-status.connecting { color: var(--muted); }

/* ---------- enhancements ---------- */
select#voiceSelect { max-width: 110px; }
.speak-btn.inline { display: inline-block; margin-right: 6px; }
.topic-row { display: flex; align-items: center; }
.topic-row .topic-item { flex: 1; }
.topic-practice {
  background: none; border: none; cursor: pointer;
  font-size: 14px; opacity: 0.5; padding: 4px 8px;
}
.topic-practice:hover { opacity: 1; }
header { flex-wrap: wrap; gap: 6px; }

/* ---------- mobile ---------- */
@media (max-width: 700px) {
  header { padding: 8px 10px; }
  .brand p { display: none; }
  .brand h1 { font-size: 16px; }
  .logo { width: 34px; height: 34px; font-size: 19px; border-radius: 9px; }
  .header-actions { gap: 4px; }
  .ghost-btn { padding: 5px 8px; font-size: 12px; }
  .badge { display: none; }
  select#voiceSelect { max-width: 84px; }
  #messages { padding: 12px; }
  .msg { max-width: 92%; font-size: 14.5px; }
  #inputBar { padding: 8px 10px 12px; gap: 6px; }
  .icon-btn { width: 38px; height: 38px; font-size: 16px; }
  .send-btn { width: 38px; height: 38px; }
  /* 16px stops iOS from zooming the page when the input is focused */
  #input { font-size: 16px; padding: 9px 12px; }
  #canvasPanel { min-height: 42vh; }
  #canvasToolbar { padding: 6px 8px; gap: 4px; }
  .primary-btn { padding: 7px 10px; font-size: 12px; }
  .canvas-hint { display: none; }
  .live-status { top: 54px; font-size: 12px; padding: 5px 12px; }
}

/* ---------- help modal ---------- */
#helpOverlay {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.4);
  z-index: 60;
}
#helpModal {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: min(560px, 94vw);
  max-height: 86vh;
  background: var(--panel);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  z-index: 70;
  display: flex; flex-direction: column;
  overflow: hidden;
}
#helpOverlay.hidden, #helpModal.hidden { display: none; }
.help-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px 8px;
  border-bottom: 1px solid var(--border);
}
.help-head h2 { margin: 0; font-size: 18px; }
.help-body {
  padding: 6px 20px 20px;
  overflow-y: auto;
  font-size: 14px;
  line-height: 1.55;
}
.help-body h3 { margin: 16px 0 4px; font-size: 14.5px; }
.help-body p { margin: 0; color: #334155; }

/* ---------- math board ---------- */
#mathBoard {
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: #fffbeb;
  overflow-x: auto;
  flex-shrink: 0;
  min-height: 44px;
}
#mathBoard.hidden { display: none; }
.board-line {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: #fff;
  border: 1px solid #fde68a;
  border-radius: 8px;
  font-size: 16px;
  white-space: nowrap;
  transition: box-shadow 0.4s, border-color 0.4s;
}
.board-line.board-new {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.board-note {
  font-size: 11px;
  color: var(--muted);
  font-family: "Segoe UI", system-ui, sans-serif;
}

/* ---------- canvas-first layout: chat 20%, canvas 80% ---------- */
@media (min-width: 901px) {
  #chatPanel { flex: 0 0 20%; min-width: 280px; }
  #canvasPanel { flex: 1 1 80%; }
  .msg { max-width: 96%; font-size: 13.5px; }
}

/* ---------- start lesson overlay ---------- */
#startOverlay {
  position: fixed; inset: 0;
  background: rgba(244, 246, 251, 0.92);
  backdrop-filter: blur(3px);
  z-index: 80;
  display: flex; align-items: center; justify-content: center;
}
#startOverlay.hidden { display: none; }
#startLessonBtn {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 24px;
  padding: 44px 64px;
  font-size: 22px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 18px 50px rgba(37, 99, 235, 0.35);
  transition: transform 0.15s;
}
#startLessonBtn:hover { transform: scale(1.03); }
.start-icon { font-size: 44px; }
.start-sub { font-size: 13px; font-weight: 400; opacity: 0.85; }

/* ---------- parameter slider strip ---------- */
#paramStrip {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  background: #f0f9ff;
  flex-shrink: 0;
}
#paramStrip.hidden { display: none; }
#paramStrip input[type=range] { flex: 1; max-width: 420px; accent-color: var(--accent); }
.param-label { font-weight: 700; font-size: 15px; color: var(--accent); min-width: 80px; }

/* ---------- attendance calendar ---------- */
#calOverlay { position: fixed; inset: 0; background: rgba(15,23,42,0.4); z-index: 60; }
#calModal {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: min(460px, 94vw); max-height: 86vh;
  background: var(--panel); border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  z-index: 70; display: flex; flex-direction: column; overflow: hidden;
}
#calOverlay.hidden, #calModal.hidden { display: none; }
.cal-body { padding: 10px 18px 18px; overflow-y: auto; }
.cal-schedule { display: flex; align-items: center; gap: 8px; font-size: 14px; margin-bottom: 12px; }
.cal-schedule input { border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; font-size: 14px; }
.cal-month-title { font-weight: 700; font-size: 14px; margin: 10px 0 6px; }
.cal-month { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-dow { text-align: center; font-size: 11px; color: var(--muted); }
.cal-day {
  aspect-ratio: 1; border-radius: 8px; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 12px; color: var(--muted);
}
.cal-day.attended { background: #bbf7d0; color: #14532d; font-weight: 600; }
.cal-day.attended small { font-size: 9px; }
.cal-day.today { outline: 2px solid var(--accent); }
.cal-legend { font-size: 11.5px; color: var(--muted); margin: 10px 0 0; }

/* ---------- formula bubbles in chat ---------- */
.msg.boardmsg {
  background: #fffbeb;
  border: 1px solid #fde68a;
  align-self: stretch;
  max-width: 100%;
  text-align: center;
  padding: 8px 10px;
}
.msg.boardmsg .katex-display { margin: 4px 0; }
.msg.boardmsg .board-note { display: block; font-size: 11px; color: var(--muted); }
.msg.boardmsg.board-new { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- adjustable pane divider ---------- */
#paneDivider {
  flex: 0 0 6px;
  cursor: col-resize;
  background: var(--border);
  transition: background 0.15s;
  touch-action: none;
}
#paneDivider:hover, #paneDivider:active { background: var(--accent); }
@media (max-width: 900px) { #paneDivider { display: none; } }

/* ---------- guided tour ---------- */
.tour-overlay { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45); z-index: 90; }
.tour-tip {
  position: fixed; z-index: 95; width: 320px;
  background: #fff; border-radius: 16px; padding: 18px 20px;
  box-shadow: 0 18px 50px rgba(0,0,0,0.3);
}
.tour-count { font-size: 11px; font-weight: 700; color: var(--accent); margin-bottom: 4px; }
.tour-tip h3 { margin: 0 0 6px; font-size: 16px; color: var(--text); }
.tour-tip p { margin: 0 0 14px; font-size: 13.5px; color: #475569; line-height: 1.5; }
.tour-nav { display: flex; gap: 8px; justify-content: flex-end; }
.tour-glow { position: relative; z-index: 92; box-shadow: 0 0 0 4px var(--accent), 0 0 0 9px rgba(37, 99, 235, 0.3) !important; border-radius: 10px; }
