/* Fonts: we used to @import Inter + JetBrains Mono from fonts.googleapis.com
   but dropped it so the app has zero third-party network dependencies at page
   load — schools often filter third-party font CDNs. The font stacks below
   put -apple-system first, so iOS renders in SF Pro which looks identical to
   Inter. System UI fonts are always available. */

/* ============================================================
   DESIGN TOKENS — "Chalkboard & paper" palette
   Dark mode = chalkboard green surface with chalk-cream text.
   Light mode = paper cream surface with ink-black text.
   Accents map to real classroom pens: pencil (warmth/progress),
   blue pen (info/tutor), red pen (errors), eraser (muted/disabled).
   ============================================================ */

:root {
  /* Surfaces — deep slate chalkboard ramp */
  --bg-deep: #040907;        /* Board -1 */
  --bg-base: #08130f;        /* Board (primary surface) */
  --bg-elevated: #0c1b15;    /* Board +1 */
  --bg-secondary: #12261e;
  --bg-tertiary: #193126;
  --bg-glass: rgba(8, 19, 15, 0.72);
  --bg-glass-thick: rgba(8, 19, 15, 0.88);

  /* Text — chalk on board */
  --text-primary: #f4f1e8;   /* Chalk */
  --text-secondary: #c5c1b3;
  --text-tertiary: #8a8778;
  --text-quaternary: #5a5a52;

  /* Accent — Pencil (warmth, progress, highlights) */
  --accent: #d4a017;
  --accent-soft: rgba(212, 160, 23, 0.18);
  --accent-glow: rgba(212, 160, 23, 0.30);
  --accent-bright: #e6b030;

  /* Semantic — pens */
  --ai-cyan: #3a6fb0;        /* Blue pen — tutor strokes / info */
  --ai-cyan-soft: rgba(58, 111, 176, 0.12);
  --danger: #c44536;         /* Red pen */
  --danger-soft: rgba(196, 69, 54, 0.15);
  --success: #d4a017;        /* Pencil doubles as "progress" success */
  --success-soft: rgba(212, 160, 23, 0.15);
  --warning: #d4a017;
  --warning-soft: rgba(212, 160, 23, 0.15);

  /* Borders — chalk at low opacity */
  --border: rgba(244, 241, 232, 0.10);
  --border-light: rgba(244, 241, 232, 0.14);
  --border-strong: rgba(244, 241, 232, 0.22);

  /* Radius — iPadOS uses generous radii */
  --radius-xs: 6px;
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-xl: 28px;
  --radius-pill: 100px;

  /* Blur */
  --blur-sm: 12px;
  --blur: 24px;
  --blur-lg: 40px;

  /* Shadows */
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.6);
  --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.7);
  --shadow-glow: 0 0 24px var(--accent-glow);

  /* Easing */
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-in-out: cubic-bezier(0.45, 0, 0.55, 1);

  /* Canvas — the chalkboard */
  --canvas-bg: #08130f;

  /* Safe areas */
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);

  /* Unified corner inset — all floating UI elements use this */
  --corner: 16px;
}

/* Smooth crossfade when swapping themes (data-theme attr change triggers
   re-evaluation of every var(...) reference; this transition eases that). */
body,
#desk,
.rail,
.login-card,
.login-card input,
#comm-status,
.menu-drawer,
#sidebar,
.wb-card,
.auth-card,
.theme-picker-card,
.theme-picker-name {
  transition: background-color 0.28s ease, color 0.28s ease, border-color 0.28s ease;
}

/* ============================================================
   RESET & BASE
   ============================================================ */

*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, 'SF Pro Display', 'SF Pro Text', 'Inter', BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-deep);
  color: var(--text-primary);
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  display: flex;
  height: 100%;
  width: 100%;
  position: relative;
  overflow: hidden;
}

/* Per-theme canvas grid overrides. The default above applies to "dots"
   themes (chalkboard, paper, slate); these rules override for others. */
html[data-theme="whiteboard"] #paper-surface {
  background-image: none;
}
html[data-theme="legal-pad"] #paper-surface {
  /* Fallbacks inside calc() and var() so the canvas still renders usable
     lines if JS hasn't set the tokens yet (e.g. brief moment during FOUC). */
  background-image: linear-gradient(
    to bottom,
    transparent calc(var(--canvas-grid-size, 24px) - 1px),
    var(--canvas-grid-color, rgba(196, 69, 54, 0.15)) calc(var(--canvas-grid-size, 24px) - 1px),
    var(--canvas-grid-color, rgba(196, 69, 54, 0.15)) var(--canvas-grid-size, 24px)
  );
  background-size: 100% var(--canvas-grid-size, 24px);
}
html[data-theme="graph-paper"] #paper-surface {
  background-image:
    linear-gradient(var(--canvas-grid-color, rgba(58, 111, 176, 0.15)) 1px, transparent 1px),
    linear-gradient(90deg, var(--canvas-grid-color, rgba(58, 111, 176, 0.15)) 1px, transparent 1px);
  background-size: var(--canvas-grid-size, 18px) var(--canvas-grid-size, 18px);
}

/* ============================================================
   DESK / PAPER — bounded whiteboard surface (2026-04-22)
   ============================================================ */

#desk {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--desk-bg, var(--bg-deep, #040907));
}

#paper-viewport {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: none;
}

#paper-surface {
  position: relative;
  background: var(--canvas-bg, #f4f1e8);
  aspect-ratio: 8.5 / 11;
  /* Fit the largest 8.5:11 box inside the viewport minus margins. Using
     viewport units for the cross-axis budget because CSS can't reference
     parent height from inside a width property (100% always = parent width).
     #desk fills the full viewport so 100vh == parent height.
     Width picks the smaller of: width budget, OR width implied by
     height budget back-converted through 8.5/11. Aspect-ratio then
     drives height. flex-shrink:0 prevents flex auto-shrink.
     Explicit height: calc(100%-48px) alone won with aspect-ratio
     ignored, breaking portrait iPad. */
  width: min(calc(100vw - 110px), calc((100vh - 110px) * 8.5 / 11));
  height: auto;
  flex-shrink: 0;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35), 0 1px 0 rgba(255, 255, 255, 0.04) inset;
  border-radius: 3px;
  background-image: radial-gradient(circle, var(--canvas-grid-color, rgba(255, 255, 255, 0.035)) 1px, transparent 1px);
  background-size: var(--canvas-grid-size, 28px) var(--canvas-grid-size, 28px);
  transform-origin: center center;
  will-change: transform;
  /* Clip child canvases to the paper's rounded corners — strokes near
     the edge never spill past the sheet. Ink effectively lives only on
     the paper, regardless of zoom. */
  overflow: hidden;
}

#whiteboard {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#ai-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  /* AI ink is passive — never intercept pointer events from the student
     canvas beneath it. Regression introduced in Task 1 when the two
     canvas rules were merged; restoring this unblocks drawing. */
  pointer-events: none;
}

#ai-math-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#ai-math-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
  overflow: hidden;
}

.ai-math-el {
  position: absolute;
  pointer-events: none;
  transition: opacity 0.35s var(--ease-out);
}

.ai-math-el .katex {
  white-space: nowrap;
}

.ai-math-el.ai-text-el {
  font-family: -apple-system, 'SF Pro Display', 'Inter', sans-serif;
  font-weight: 500;
  letter-spacing: -0.01em;
}

/* Tool buttons */
.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 27px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s var(--ease-out);
  -webkit-tap-highlight-color: transparent;
  position: relative;
}

.tool-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-primary);
}

.tool-btn:active {
  transform: scale(0.92);
}

.tool-btn.active {
  background: var(--accent-soft);
  color: var(--accent-bright);
}

.tool-btn.active::after {
  content: '';
  position: absolute;
  bottom: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--accent-bright);
}

.tool-btn.danger {
  color: var(--danger);
}

.tool-btn.danger:hover {
  background: var(--danger-soft);
}

/* Color buttons */
.color-btn {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid transparent;
  background: var(--swatch);
  cursor: pointer;
  transition: all 0.2s var(--ease-spring);
  -webkit-tap-highlight-color: transparent;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.25);
  flex-shrink: 0;
}

.color-btn:hover {
  transform: scale(1.15);
}

.color-btn:active {
  transform: scale(0.95);
}

.color-btn.active {
  border-color: var(--text-primary);
  transform: scale(1.15);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.15), inset 0 0 0 1px rgba(0, 0, 0, 0.25);
}

/* Stroke size slider */
.stroke-size-control {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px;
}

#brush-size {
  width: 64px;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--bg-tertiary);
  border-radius: 2px;
  outline: none;
}

#brush-size::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text-primary);
  border: none;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}

.stroke-preview {
  width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stroke-preview::after {
  content: '';
  display: block;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--text-primary);
  transition: all 0.15s var(--ease-out);
}

.unread-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  background: var(--danger);
  color: white;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  animation: badge-in 0.3s var(--ease-spring) both;
}

@keyframes badge-in {
  from { transform: scale(0); }
  to { transform: scale(1); }
}

/* ============================================================
   CHAT SIDEBAR — iPadOS slide-over panel
   ============================================================ */

#sidebar {
  width: 380px;
  max-width: 85vw;
  height: 100%;
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  background: var(--bg-glass-thick);
  backdrop-filter: blur(var(--blur-lg));
  -webkit-backdrop-filter: blur(var(--blur-lg));
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform 0.4s var(--ease-out), opacity 0.4s var(--ease-out);
  z-index: 30;
  position: relative;
}

#sidebar.collapsed {
  transform: translateX(100%);
  opacity: 0;
  pointer-events: none;
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 380px;
}

/* Sidebar header */
#sidebar-header {
  padding: 16px 18px;
  padding-top: calc(16px + var(--sat));
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.ai-avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), var(--accent-bright));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  flex-shrink: 0;
}

#sidebar-header h2 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.02em;
  line-height: 1.2;
}

#connection-status {
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--text-tertiary);
  font-weight: 500;
  margin-top: 1px;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--danger);
  transition: background 0.3s var(--ease-out), box-shadow 0.3s var(--ease-out);
}

#connection-status.connected .status-dot {
  background: var(--success);
  box-shadow: 0 0 8px rgba(48, 209, 88, 0.4);
}

#close-sidebar {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius-xs);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s var(--ease-out);
}

#close-sidebar:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-primary);
}

/* ============================================================
   CHAT MESSAGES
   ============================================================ */

#chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.message {
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  font-size: 14px;
  line-height: 1.55;
  max-width: 90%;
  word-wrap: break-word;
  animation: msg-in 0.35s var(--ease-out) both;
}

@keyframes msg-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.message.user {
  background: var(--accent);
  color: white;
  align-self: flex-end;
  border-bottom-right-radius: var(--radius-xs);
}

.message.assistant {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  align-self: flex-start;
  border-bottom-left-radius: var(--radius-xs);
}

.message.system {
  background: transparent;
  color: var(--text-tertiary);
  font-size: 12px;
  text-align: center;
  align-self: center;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.message.voice {
  border-left: 3px solid var(--warning);
}

.message pre {
  background: var(--bg-deep);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 8px 0;
  font-size: 13px;
  border: 1px solid var(--border);
}

.message code {
  font-family: 'SF Mono', 'JetBrains Mono', monospace;
  font-size: 13px;
}

.message p { margin: 4px 0; }
.message strong { color: var(--accent-bright); }
.message.user strong { color: white; }
.message em { color: var(--text-secondary); }
.message.user em { color: rgba(255,255,255,0.8); }
.message ul, .message ol { padding-left: 18px; margin: 4px 0; }
.message li { margin: 2px 0; }

/* Thinking indicator */
.thinking-indicator {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: var(--text-tertiary);
  font-size: 13px;
  font-weight: 500;
}

.thinking-dots {
  display: flex;
  gap: 4px;
}

/* SP-2 · Multi-tool vocabulary — pencil / chalk / highlighter / marker.
   Applied via data-tool on .ai-math-el. Default (no attr or "chalk") uses
   the existing styling. Other tools tweak weight / opacity / background. */
.ai-math-el[data-tool="pencil"]     { opacity: 0.62; filter: saturate(0.6); }
.ai-math-el[data-tool="marker"]     { font-weight: 700; text-shadow: 0 0 1px currentColor; }
.ai-math-el[data-tool="highlighter"]{ background: rgba(212, 160, 23, 0.22); padding: 2px 4px; border-radius: 3px; }

.thinking-dots span {
  display: block;
  width: 6px;
  height: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: thinking-pulse 1.2s ease-in-out infinite;
}

.thinking-dots span:nth-child(2) { animation-delay: 0.15s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.30s; }

@keyframes thinking-pulse {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

/* ============================================================
   CHAT INPUT AREA
   ============================================================ */

#chat-input-area {
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--sab));
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: rgba(10, 10, 15, 0.6);
  flex-shrink: 0;
}

#action-row {
  display: flex;
  gap: 6px;
}

#action-row button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: var(--radius-pill);
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  transition: all 0.2s var(--ease-out);
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

#action-row button:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-primary);
}

#action-row button:active {
  transform: scale(0.96);
}

#tts-btn.active {
  background: var(--accent-soft);
  border-color: rgba(212, 160, 23, 0.25);
  color: var(--accent-bright);
}

#voice-btn.recording {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--danger);
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 69, 58, 0.3); }
  50% { box-shadow: 0 0 0 8px rgba(255, 69, 58, 0); }
}

#chat-input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

#chat-input {
  flex: 1;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 10px 16px;
  border-radius: var(--radius-lg);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  resize: none;
  transition: border-color 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out);
  -webkit-user-select: text;
  user-select: text;
}

#chat-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

#chat-input::placeholder {
  color: var(--text-quaternary);
}

#send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s var(--ease-out);
  box-shadow: var(--shadow-glow);
}

#send-btn:hover {
  background: var(--accent-bright);
  transform: scale(1.05);
}

#send-btn:active {
  transform: scale(0.95);
}

/* ============================================================
   SCROLLBAR
   ============================================================ */

::-webkit-scrollbar {
  width: 4px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.18);
}

/* ============================================================
   PRESSURE CURSOR — shows current brush on canvas
   ============================================================ */

.cursor-dot {
  position: absolute;
  pointer-events: none;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, 0.5);
  z-index: 5;
  transform: translate(-50%, -50%);
  transition: width 0.05s, height 0.05s;
}

/* ============================================================
   RESPONSIVE — iPad portrait / smaller screens
   ============================================================ */

@media (max-width: 1024px) {
  #sidebar {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    box-shadow: var(--shadow-xl);
  }
}

@media (max-width: 700px) {
  .tool-btn {
    width: 36px;
    height: 36px;
  }

  .color-btn {
    width: 22px;
    height: 22px;
  }

  #brush-size {
    width: 48px;
  }

  /* NOTE: Top-rail / right-rail / menu-btn / page-pill mobile polish
     lives in the @media (max-width: 700px) block at the END of this
     file (search "RESPONSIVE — fall back to full-bleed"). That block
     comes after the original .rail / .rail-horizontal definitions in
     source order, so its same-specificity overrides actually win. */

  #sidebar {
    width: 100%;
    max-width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    max-height: 60vh;
  }

  #sidebar.collapsed {
    transform: translateY(100%);
  }
}

/* ============================================================
   FLOATING COMM NODE — always-visible voice interface
   ============================================================ */

#comm-node {
  position: static;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

#comm-status {
  background: var(--bg-glass-thick);
  backdrop-filter: blur(var(--blur));
  -webkit-backdrop-filter: blur(var(--blur));
  border: 1px solid var(--border-light);
  border-radius: var(--radius-pill);
  padding: 3px 8px;
  box-shadow: var(--shadow-md);
  transition: all 0.3s var(--ease-out);
  width: auto;
  min-width: 48px;
  text-align: center;
  overflow: hidden;
  white-space: nowrap;
}

#comm-status-text {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--text-tertiary);
  white-space: nowrap;
  transition: color 0.3s var(--ease-out);
}

#comm-node.listening #comm-status {
  border-color: var(--danger);
  background: rgba(255, 69, 58, 0.12);
}

#comm-node.listening #comm-status-text {
  color: var(--danger);
}

#comm-node.sending #comm-status,
#comm-node.thinking #comm-status,
#comm-node.vocalizing #comm-status {
  border-color: var(--accent);
  background: var(--accent-soft);
}

#comm-node.sending #comm-status-text,
#comm-node.thinking #comm-status-text,
#comm-node.vocalizing #comm-status-text {
  color: var(--accent-bright);
}

#comm-node.speaking #comm-status {
  border-color: var(--success);
  background: var(--success-soft);
}

#comm-node.speaking #comm-status-text {
  color: var(--success);
}

/* Push-to-talk button */
#comm-ptt {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--bg-glass-thick);
  backdrop-filter: blur(var(--blur));
  -webkit-backdrop-filter: blur(var(--blur));
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  border: 1.5px solid var(--border-light);
  transition: all 0.2s var(--ease-out);
  -webkit-tap-highlight-color: transparent;
  position: relative;
  touch-action: none;
}

#comm-ptt:hover {
  transform: scale(1.05);
  border-color: var(--border-strong);
  color: var(--text-primary);
}

#comm-ptt:active,
#comm-ptt.recording,
#comm-node.listening #comm-ptt {
  transform: scale(0.95);
  background: rgba(255, 69, 58, 0.15);
  border-color: var(--danger);
  color: var(--danger);
}

.comm-mic-icon {
  position: relative;
  z-index: 1;
}

/* Animated ring around PTT when listening */
.comm-ptt-ring {
  position: absolute;
  top: -6px;
  left: -6px;
  right: -6px;
  bottom: -6px;
  border-radius: 50%;
  border: 2px solid transparent;
  transition: all 0.3s var(--ease-out);
}

#comm-node.listening .comm-ptt-ring {
  border-color: var(--danger);
  animation: ptt-pulse 1.5s infinite;
}

@keyframes ptt-pulse {
  0%, 100% { transform: scale(1); opacity: 0.6; }
  50% { transform: scale(1.15); opacity: 0; }
}

/* TTS toggle (small) */
#comm-tts-toggle {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-glass);
  backdrop-filter: blur(var(--blur-sm));
  -webkit-backdrop-filter: blur(var(--blur-sm));
  color: var(--accent-bright);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}

#comm-tts-toggle:hover {
  background: rgba(255, 255, 255, 0.08);
}

#comm-tts-toggle.muted {
  color: var(--text-quaternary);
  border-color: var(--border);
}

/* ============================================================
   PENCIL-SPECIFIC — when Apple Pencil is active
   ============================================================ */

body.pencil-active #whiteboard {
  cursor: none;
}

/* ============================================================
   ANIMATION UTILITIES
   ============================================================ */

/* ============================================================
   RECORD BUTTON
   ============================================================ */

.record-btn {
  position: absolute;
  top: 20px;
  left: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--bg-glass-thick);
  backdrop-filter: blur(var(--blur));
  -webkit-backdrop-filter: blur(var(--blur));
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border-light);
  z-index: 25;
  transition: all 0.2s;
  padding-top: var(--sat);
}

.record-btn:hover {
  color: var(--text-primary);
}

.record-btn.recording {
  background: var(--danger);
  border-color: var(--danger);
  color: white;
  animation: pulse 1.5s infinite;
}

/* ============================================================
   SETTINGS PANEL
   ============================================================ */

.settings-panel {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: none;
  align-items: flex-end;
  justify-content: center;
}

.settings-panel.open {
  display: flex;
}

.settings-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  animation: fade-in 0.2s ease;
}

.settings-sheet {
  position: relative;
  width: 100%;
  max-width: 440px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  padding: 0 0 calc(24px + var(--sab));
  animation: slide-up 0.3s var(--ease-out);
  max-height: 70vh;
  overflow-y: auto;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}

.settings-header h3 {
  font-size: 17px;
  font-weight: 600;
}

.settings-close-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: rgba(255,255,255,0.06);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.settings-body {
  padding: 16px 24px;
}

.setting-item {
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
}

.setting-item:last-child {
  border-bottom: none;
}

.setting-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.setting-toggle-group {
  display: flex;
  gap: 4px;
  background: rgba(255,255,255,0.04);
  border-radius: var(--radius-sm);
  padding: 3px;
}

.setting-toggle {
  flex: 1;
  padding: 8px 16px;
  border: none;
  border-radius: calc(var(--radius-sm) - 2px);
  background: transparent;
  color: var(--text-tertiary);
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s;
}

.setting-toggle.active {
  background: var(--accent);
  color: white;
}

.setting-action {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--danger);
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s;
}

.setting-action:hover {
  background: rgba(255, 69, 58, 0.1);
}

/* ============================================================
   LOGIN SCREEN — Premium onboarding
   ============================================================ */

#login-screen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-deep);
  padding: 24px;
  overflow: hidden;
}

/* Animated background orbs */
#login-screen::before,
#login-screen::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  filter: blur(100px);
  pointer-events: none;
  opacity: 0.5;
}

#login-screen::before {
  width: 500px;
  height: 500px;
  background: radial-gradient(circle, rgba(212, 160, 23, 0.25), transparent 70%);
  top: -15%;
  right: -10%;
  animation: login-orb1 12s ease-in-out infinite alternate;
}

#login-screen::after {
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(34, 211, 238, 0.15), transparent 70%);
  bottom: -10%;
  left: -5%;
  animation: login-orb2 15s ease-in-out infinite alternate;
}

@keyframes login-orb1 {
  0% { transform: translate(0, 0) scale(1); }
  100% { transform: translate(-8vw, 12vh) scale(1.15); }
}

@keyframes login-orb2 {
  0% { transform: translate(0, 0) scale(1); }
  100% { transform: translate(10vw, -8vh) scale(1.1); }
}

/* Dot grid on login too */
#login-screen .login-grid {
  position: absolute;
  inset: 0;
  background-image: radial-gradient(circle, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
  background-size: 28px 28px;
  pointer-events: none;
}

.login-card {
  width: 100%;
  max-width: 380px;
  text-align: center;
  position: relative;
  z-index: 1;
  background: rgba(20, 20, 28, 0.6);
  backdrop-filter: blur(40px);
  -webkit-backdrop-filter: blur(40px);
  border: 1px solid var(--border-light);
  border-radius: 28px;
  padding: 48px 40px 40px;
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.04),
    0 24px 80px rgba(0, 0, 0, 0.5),
    0 0 120px rgba(212, 160, 23, 0.06);
}

.login-logo {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--accent), var(--accent-bright));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  font-weight: 700;
  color: white;
  margin: 0 auto 20px;
  font-family: -apple-system, 'SF Pro Display', sans-serif;
  box-shadow: 0 8px 32px rgba(212, 160, 23, 0.3);
}

.login-card h1 {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin-bottom: 6px;
}

.login-sub {
  font-size: 15px;
  color: var(--text-tertiary);
  margin-bottom: 36px;
}

.login-card input {
  width: 100%;
  padding: 16px 20px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-primary);
  font-size: 16px;
  font-family: inherit;
  outline: none;
  margin-bottom: 12px;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
  -webkit-user-select: text;
  user-select: text;
}

.login-card input:focus {
  border-color: var(--accent);
  background: rgba(255, 255, 255, 0.06);
  box-shadow: 0 0 0 3px rgba(212, 160, 23, 0.15);
}

.login-card input::placeholder {
  color: var(--text-quaternary);
}

.login-submit {
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), var(--accent-bright));
  color: white;
  font-size: 16px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  margin-top: 8px;
  transition: all 0.25s var(--ease-out);
  box-shadow: 0 4px 24px rgba(212, 160, 23, 0.25);
  position: relative;
  overflow: hidden;
}

.login-submit:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 32px rgba(212, 160, 23, 0.35);
}

.login-submit:active {
  transform: translateY(0);
}

.login-submit:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.login-secondary {
  width: 100%;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 15px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  margin-top: 10px;
  transition: all 0.2s;
}

.login-secondary:hover {
  border-color: rgba(255, 255, 255, 0.15);
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.03);
}

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 16px;
  min-height: 18px;
}

/* Floating math symbols on login */
.login-math {
  position: absolute;
  font-family: 'Georgia', serif;
  color: rgba(212, 160, 23, 0.06);
  font-size: 48px;
  pointer-events: none;
  z-index: 0;
  animation: float 8s ease-in-out infinite alternate;
}

.login-math:nth-child(1) { top: 10%; left: 8%; font-size: 56px; animation-duration: 7s; }
.login-math:nth-child(2) { top: 15%; right: 12%; font-size: 40px; color: rgba(34, 211, 238, 0.05); animation-duration: 9s; }
.login-math:nth-child(3) { bottom: 18%; left: 15%; font-size: 44px; color: rgba(251, 113, 133, 0.04); animation-duration: 6s; }
.login-math:nth-child(4) { bottom: 12%; right: 8%; font-size: 52px; animation-duration: 10s; }
.login-math:nth-child(5) { top: 45%; left: 5%; font-size: 36px; color: rgba(34, 211, 238, 0.05); animation-duration: 8s; }

@keyframes float {
  0% { transform: translateY(0) rotate(0deg); }
  100% { transform: translateY(-20px) rotate(6deg); }
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slide-up {
  from { transform: translateY(16px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* Kill KaTeX display mode's huge margins — our layout engine handles spacing */
.ai-math-el .katex-display {
  margin: 0 !important;
  padding: 0 !important;
}

/* ============================================================
   MENU DRAWER
   ============================================================ */

/* Whiteboard entrance animations */
#whiteboard-area {
  animation: wbFadeIn 0.35s ease-out both;
}
@keyframes wbFadeIn { from { opacity:0; } to { opacity:1; } }

.page-nav {
  animation: slideUp 0.4s cubic-bezier(0.16,1,0.3,1) both;
  animation-delay: 0.2s;
}
@keyframes slideUp { from { opacity:0; transform:translateY(20px); } to { opacity:1; transform:translateY(0); } }
@keyframes fadeScale { from { opacity:0; transform:scale(0.8); } to { opacity:1; transform:scale(1); } }

.menu-btn {
  position: absolute;
  top: var(--corner);
  left: var(--corner);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border-light);
  background: var(--bg-glass-thick);
  backdrop-filter: blur(var(--blur));
  animation: fadeScale 0.3s ease-out both;
  animation-delay: 0.1s;
  -webkit-backdrop-filter: blur(var(--blur));
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-md);
  z-index: 25;
  transition: all 0.2s;
}
.menu-btn:hover { color: var(--text-primary); }

.menu-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 998;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}
.menu-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.menu-drawer {
  position: fixed;
  top: 0;
  left: 0;
  width: 280px;
  height: 100%;
  background: var(--bg-glass-thick, rgba(20, 20, 28, 0.95));
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-right: 1px solid var(--border-light, rgba(255,255,255,0.08));
  z-index: 999;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.35s cubic-bezier(0.16, 1, 0.3, 1);
  padding: calc(20px + env(safe-area-inset-top, 0px)) 0 env(safe-area-inset-bottom, 20px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.menu-drawer.open {
  transform: translateX(0);
}

/* User info */
.menu-user {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border-light, rgba(255,255,255,0.08));
}
.menu-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent, #d4a017);
  color: white;
  font-size: 18px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.menu-user-info {
  min-width: 0;
}
.menu-user-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary, #f0f0f2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.menu-user-email {
  font-size: 12px;
  color: var(--text-secondary, #8e8e96);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Section */
.menu-section {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-light, rgba(255,255,255,0.08));
}
.menu-section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary, #8e8e96);
  margin-bottom: 10px;
}

/* Setting row */
.menu-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 0;
}
.menu-setting span {
  font-size: 14px;
  color: var(--text-primary, #f0f0f2);
}
.menu-toggle-group {
  display: flex;
  gap: 2px;
  background: rgba(255,255,255,0.06);
  border-radius: 8px;
  padding: 2px;
}
.menu-toggle {
  padding: 5px 12px;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  background: transparent;
  color: var(--text-secondary, #8e8e96);
  cursor: pointer;
  transition: all 0.2s;
}
.menu-toggle.active {
  background: var(--accent, #d4a017);
  color: white;
}

/* ── Tutor Settings collapsible sections ──────────────────────────
   Two <details> wrappers group Voice vs Tutoring settings so the
   panel stays tidy as more options get added. Each <summary> is the
   clickable header; .ts-section-body holds the controls. Functionality
   is unchanged — only wrapped. */
.ts-section {
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  margin: 0 -4px;            /* subtle bleed so the header feels full-width */
  padding: 4px;
}
.ts-section:last-of-type {
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  margin-bottom: 20px;
}
.ts-section > summary {
  list-style: none;          /* hide the default triangle */
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 4px;
  user-select: none;
  -webkit-user-select: none;
}
.ts-section > summary::-webkit-details-marker { display: none; }
.ts-section-title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.2px;
  color: var(--text-primary, #f0f0f2);
}
.ts-chevron {
  font-size: 20px;
  line-height: 1;
  color: var(--text-tertiary, #636366);
  transform: rotate(90deg);
  transition: transform 0.18s ease;
}
.ts-section[open] > summary > .ts-chevron {
  transform: rotate(-90deg);  /* points up when expanded */
}
.ts-section-body {
  padding: 4px 4px 16px 4px;
}

/* Menu items */
.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 4px;
  border: none;
  background: none;
  color: var(--text-primary, #f0f0f2);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  border-radius: 8px;
  transition: background 0.15s;
}
.menu-item:active {
  background: rgba(255,255,255,0.06);
}
.menu-item svg {
  color: var(--text-secondary, #8e8e96);
  flex-shrink: 0;
}
.menu-item-badge {
  font-size: 11px;
  color: var(--text-secondary, #8e8e96);
  margin-left: auto;
}

/* Bottom */
.menu-spacer { flex: 1; }
.menu-bottom {
  padding: 12px 20px;
  border-top: 1px solid var(--border-light, rgba(255,255,255,0.08));
}
.menu-signout {
  color: var(--danger, #FF453A) !important;
}
.menu-signout svg {
  color: var(--danger, #FF453A) !important;
}
.menu-version {
  font-size: 11px;
  color: var(--text-secondary, #8e8e96);
  opacity: 0.5;
  margin-top: 8px;
  text-align: center;
}

/* ============================================================
   PAGE NAVIGATION
   ============================================================ */

.page-pill {
  position: absolute;
  bottom: 18px;
  left: 20px;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: var(--bg-glass-thick, rgba(255, 255, 255, 0.04));
  backdrop-filter: blur(var(--blur, 8px));
  -webkit-backdrop-filter: blur(var(--blur, 8px));
  border: 1px solid var(--border-light, rgba(255, 255, 255, 0.06));
  border-radius: 14px;
  color: var(--text-tertiary, #a89c7d);
  font-size: 12px;
  font-family: ui-sans-serif, system-ui;
  box-shadow: var(--shadow-md, 0 4px 12px rgba(0, 0, 0, 0.15));
}
.page-pill .page-chev {
  width: 22px; height: 22px;
  background: none; border: none; cursor: pointer;
  color: var(--text-secondary, #bbb0a0); font-size: 16px; line-height: 1;
  padding: 0; border-radius: 6px;
}
.page-pill .page-chev:hover:not(:disabled) { background: rgba(255, 255, 255, 0.06); }
.page-pill .page-chev:disabled { opacity: 0.3; cursor: default; }
.page-pill .page-indicator { min-width: 54px; text-align: center; }
.page-pill .page-plus {
  margin-left: 4px;
  padding-left: 8px;
  border-left: 1px solid var(--border, rgba(255, 255, 255, 0.08));
  background: none; border-top: none; border-right: none; border-bottom: none;
  color: var(--text-secondary, #bbb0a0);
  font-size: 16px; cursor: pointer; line-height: 1;
}
.page-pill .page-plus:hover { color: var(--accent-bright, #f5d373); }

/* ============================================================
   PAGE FLIP — physical slide with subtle Y-axis tilt (2026-04-22)
   ============================================================ */

#paper-viewport {
  perspective: 1400px;
  perspective-origin: center center;
}

#paper-surface {
  transform-style: preserve-3d;
  will-change: transform;
}

#paper-surface.page-flip-left {
  animation: paperFlipOutLeft 0.35s cubic-bezier(0.4, 0, 0.2, 1) forwards,
             paperFlipInRight 0.35s cubic-bezier(0.4, 0, 0.2, 1) 0.35s forwards;
}
#paper-surface.page-flip-right {
  animation: paperFlipOutRight 0.35s cubic-bezier(0.4, 0, 0.2, 1) forwards,
             paperFlipInLeft 0.35s cubic-bezier(0.4, 0, 0.2, 1) 0.35s forwards;
}

@keyframes paperFlipOutLeft {
  from { transform: translateX(0) rotateY(0deg); opacity: 1; }
  to   { transform: translateX(-120%) rotateY(-12deg); opacity: 0.7; }
}
@keyframes paperFlipInRight {
  from { transform: translateX(120%) rotateY(12deg); opacity: 0.7; }
  to   { transform: translateX(0) rotateY(0deg); opacity: 1; }
}
@keyframes paperFlipOutRight {
  from { transform: translateX(0) rotateY(0deg); opacity: 1; }
  to   { transform: translateX(120%) rotateY(12deg); opacity: 0.7; }
}
@keyframes paperFlipInLeft {
  from { transform: translateX(-120%) rotateY(-12deg); opacity: 0.7; }
  to   { transform: translateX(0) rotateY(0deg); opacity: 1; }
}

/* ============================================================
   THEME PICKER — thumbnail grid
   ============================================================ */
.theme-picker-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.theme-picker-card {
  display: flex;
  flex-direction: column;
  padding: 0;
  border: 2px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  background: var(--bg-elevated);
  font-family: inherit;
  transition: transform 0.12s, border-color 0.12s;
  text-align: left;
}
.theme-picker-card:hover { transform: translateY(-1px); }
.theme-picker-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
.theme-picker-swatch {
  position: relative;
  min-height: 72px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.theme-picker-stroke {
  font-family: 'Caveat', 'Comic Sans MS', cursive;
  font-size: 20px;
  line-height: 1;
  font-weight: 500;
}
.theme-picker-dots {
  display: flex;
  gap: 5px;
  margin-top: 8px;
}
.theme-picker-dots span {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1px solid rgba(0,0,0,0.08);
}
.theme-picker-name {
  padding: 6px 10px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  background: var(--bg-glass);
  border-top: 1px solid var(--border);
}

/* ============================================================
   RAILS — anchored toolbars on desk edges
   ============================================================ */

.rail {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 52px;
  background: var(--bg-glass-thick, rgba(255, 255, 255, 0.04));
  backdrop-filter: blur(var(--blur, 8px));
  -webkit-backdrop-filter: blur(var(--blur, 8px));
  border: 1px solid var(--border-light, rgba(255, 255, 255, 0.06));
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 0;
  gap: 8px;
  z-index: 20;
  max-height: calc(100% - 96px);
  overflow: hidden;
  box-shadow: var(--shadow-md, 0 4px 16px rgba(0, 0, 0, 0.2));
}

/* Keep the comm-status pill tight inside the 52px rail — the default
   #comm-status rule uses white-space:nowrap + width:auto, which grows
   with longer state labels ("Listening...", "Vocalizing...") and
   pushed the rail into overflow-scroll. Override scoped to rails
   only, so the pill wraps within rail width and rails stay clean. */
.rail #comm-status {
  width: auto;
  max-width: 44px;
  min-width: 0;
  padding: 2px 5px;
  white-space: normal;
  word-break: break-word;
  line-height: 1.15;
  overflow-wrap: anywhere;
}
.rail #comm-status-text {
  font-size: 8.5px;
  white-space: normal;
  letter-spacing: 0;
}

#rail-right { right: 12px; }

.rail .rail-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.rail-sep {
  width: 28px;
  height: 1px;
  background: var(--border, rgba(255, 255, 255, 0.08));
  margin: 2px 0;
}

/* ─── Horizontal top rail for drawing tools ──────────────────────
   Models .rail but flows horizontally across the top of the desk.
   Houses pen/highlighter/eraser/focus, color swatches, brush-size
   slider, and undo/clear. Paper width formula above accounts for
   the ~110px vertical budget this bar consumes (48px height +
   20px top offset + ~42px bottom pill area).
   ────────────────────────────────────────────────────────────── */
.rail-horizontal {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  height: 48px;
  background: var(--bg-glass-thick, rgba(255, 255, 255, 0.04));
  backdrop-filter: blur(var(--blur, 8px));
  -webkit-backdrop-filter: blur(var(--blur, 8px));
  border: 1px solid var(--border-light, rgba(255, 255, 255, 0.06));
  border-radius: 16px;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 10px;
  gap: 8px;
  z-index: 20;
  max-width: calc(100vw - 120px);
  overflow-x: auto;
  box-shadow: var(--shadow-md, 0 4px 16px rgba(0, 0, 0, 0.2));
}

.rail-horizontal .rail-section {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
}

.rail-hsep {
  width: 1px;
  height: 24px;
  background: var(--border, rgba(255, 255, 255, 0.08));
  margin: 0 2px;
}

.rail-horizontal .stroke-size-control.horizontal {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  padding: 0 4px;
}
.rail-horizontal .stroke-size-control.horizontal input[type=range] {
  -webkit-appearance: none;
  width: 70px;
  height: 4px;
  accent-color: var(--accent, #d4a017);
}

.rail-horizontal .tool-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary, #c9bd98);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, border-color 0.15s;
}
.rail-horizontal .tool-btn:hover { background: rgba(255, 255, 255, 0.05); }
.rail-horizontal .tool-btn.active {
  background: var(--accent-soft, rgba(212, 160, 23, 0.22));
  color: var(--accent-bright, #f5d373);
  border-color: var(--accent, rgba(212, 160, 23, 0.45));
}

.rail-horizontal .color-btn {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--swatch);
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.rail-horizontal .color-btn.active { border-color: var(--accent, #d4a017); }
/* Primary pen color dot inverts with theme polarity. The data-color="#ffffff"
   swatch is the "default ink" — white reads on dark themes, black reads on
   light themes. The data-attribute selector targets the first color-btn
   (the "white" one) and swaps its visible swatch in light themes. The
   actual stored color value flips in app.js (currentColor white↔black). */
html[data-theme-mode="light"] .rail-horizontal .color-btn[data-color="#ffffff"] {
  background: #1C1C1E;
  border-color: rgba(0,0,0,0.18);
}

.rail .tool-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary, #c9bd98);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, border-color 0.15s;
}
.rail .tool-btn:hover { background: rgba(255, 255, 255, 0.05); }
.rail .tool-btn.active {
  background: var(--accent-soft, rgba(212, 160, 23, 0.22));
  color: var(--accent-bright, #f5d373);
  border-color: var(--accent, rgba(212, 160, 23, 0.45));
}

.rail .color-btn {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--swatch);
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.rail .color-btn.active { border-color: var(--accent, #d4a017); }

.rail .stroke-size-control.vertical {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
}
.rail .stroke-size-control.vertical input[type=range] {
  -webkit-appearance: slider-vertical;
  width: 6px;
  height: 50px;
  accent-color: var(--accent, #d4a017);
}

/* Zoom HUD (Task 8) */
.zoom-hud {
  position: absolute;
  bottom: 18px;
  right: 20px;
  z-index: 20;
  padding: 4px 10px;
  background: var(--bg-glass-thick, rgba(0, 0, 0, 0.35));
  border: 1px solid var(--border-light, rgba(255, 255, 255, 0.06));
  border-radius: 12px;
  color: var(--text-secondary, #d4c9a8);
  font-size: 11px;
  font-family: ui-monospace, monospace;
  cursor: pointer;
  transition: opacity 0.2s ease;
  user-select: none;
}
.zoom-hud:hover { color: var(--accent-bright, #f5d373); }

/* Admin-only debug-log quick-toggle pill (stacked above zoom-hud) */
#debug-log-btn {
  position: absolute;
  bottom: 52px;
  right: 20px;
  z-index: 20;
  padding: 3px 10px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  color: var(--text-tertiary, #a89c7d);
  font-size: 10px;
  font-family: ui-monospace, monospace;
  font-weight: 700;
  letter-spacing: 1px;
  cursor: pointer;
  transition: opacity 0.2s ease, color 0.2s ease;
  user-select: none;
}
#debug-log-btn:hover { color: var(--accent-bright, #f5d373); }

/* ============================================================
   RESPONSIVE — fall back to full-bleed on phones / narrow windows
   ============================================================ */

@media (max-width: 700px) {
  #desk { background: var(--canvas-bg); }
  #paper-viewport { position: static; display: block; }
  #paper-surface {
    position: absolute;
    inset: 0;
    aspect-ratio: auto;
    max-width: none;
    height: 100%;
    width: 100%;
    box-shadow: none;
    border-radius: 0;
    transform: none !important; /* pan/zoom disabled on phones */
  }

  /* Right rail flips to a horizontal pill at the bottom on phone. Trim
     padding/width so it doesn't crowd the page-nav pill on the other side. */
  .rail {
    position: fixed;
    top: auto;
    transform: none;
    flex-direction: row;
    width: auto;
    max-width: calc(100% - 16px);
    overflow-x: auto;
    max-height: 48px;
    padding: 4px 8px;
    gap: 4px;
  }
  #rail-right { right: 8px; bottom: 8px; left: auto; }
  .rail-sep { width: 1px; height: 22px; margin: 0 2px; }
  .rail .tool-btn { width: 32px; height: 32px; border-radius: 8px; }
  .rail .tool-btn svg { width: 15px; height: 15px; }
  #comm-ptt { width: 36px; height: 36px; }
  #comm-ptt svg { width: 16px; height: 16px; }
  .rail #comm-status { max-width: 38px; padding: 2px 4px; }
  .rail #comm-status-text { font-size: 8px; }

  /* Top rail — drop it BELOW the menu-btn corner (top:60px) and let it
     span the full viewport width with flex-wrap. Without this, the rail
     defaulted to top:10px and overlapped the back/settings buttons that
     sit at top:16px in the left corner. The wrap lets colors flow to a
     second row when the tools+colors+slider+undo+clear don't fit one
     iPhone-wide line.

     IMPORTANT — these properties must override the earlier definitions
     at .rail-horizontal (line ~1908). Same selector specificity, so this
     block wins ONLY because it's LATER in source order. If a new
     .rail-horizontal rule lands below this in style.css, mobile breaks. */
  .rail-horizontal {
    top: 60px;
    left: 8px;
    right: 8px;
    transform: none;
    max-width: none;
    height: auto;
    padding: 6px 8px;
    gap: 5px;
    border-radius: 14px;
    flex-wrap: wrap;
    justify-content: center;
    row-gap: 6px;
  }
  .rail-horizontal .rail-section { gap: 4px; }
  .rail-horizontal .tool-btn { width: 30px; height: 30px; border-radius: 8px; }
  .rail-horizontal .tool-btn svg { width: 16px; height: 16px; }
  .rail-horizontal .color-btn { width: 20px; height: 20px; }
  .rail-horizontal .stroke-size-control.horizontal {
    padding: 0 2px;
    gap: 6px;
  }
  .rail-horizontal .stroke-size-control.horizontal input[type=range] {
    width: 50px;
  }
  /* Separators read awkward on a wrapping toolbar — gaps between sections
     already convey the grouping at this size. */
  .rail-hsep { display: none; }

  /* Corner buttons (back-home, settings, bug-report) — shrink so they fit
     above the top rail without crowding it. Inline-styled `left:` offsets
     in HTML for settings/bug-report still apply (calc(--corner + 48px)
     etc.); the smaller width keeps proportions sane. */
  .menu-btn { width: 34px; height: 34px; }
  .menu-btn svg { width: 16px; height: 16px; }

  /* Page nav pill — bottom-LEFT, smaller hit targets so the comm rail
     (bottom-right) has room across the same line. */
  .page-pill { bottom: 8px; left: 8px; padding: 4px 6px; gap: 2px; }
  .page-pill .page-chev { width: 28px; height: 28px; }
  .page-pill .page-indicator { min-width: 38px; font-size: 12px; }
  .page-pill .page-plus { width: 28px; height: 28px; font-size: 16px; }

  .zoom-hud { display: none; }
  #debug-log-btn { display: none !important; }

  /* ── MOBILE TOOLBAR STRIP-DOWN ──────────────────────────────────────
     The full toolbar (pen / highlighter / eraser / focus + 6 colors +
     stroke slider + undo + clear + 4 right-rail extras) is overkill for
     a phone visit. Pre-existing data has shown most TikTok arrivals
     don't draw — they're scrolling, exploring, or sending the link to
     a desktop. Strip the UI to the essentials:
       TOP:  back · settings · pen · eraser · trash · (3 colors)
       BOTTOM-RIGHT: just the microphone
     Everything else stays in markup (and stays clickable at >700px) —
     just hidden on phone. The pen/eraser/trash + voice mic covers the
     bare-minimum tutoring flow if a curious visitor actually tries it. */

  /* Top rail — hide highlighter, focus, undo, stroke slider, and the
     last 3 color swatches (keep white, red, orange).
     The stroke-control selector includes the .horizontal modifier so it
     matches the defaults-block rule's specificity (.rail-horizontal
     .stroke-size-control.horizontal). Without the modifier, the earlier
     rule with display:flex outranks ours and the slider stays visible. */
  .rail-horizontal [data-tool="highlighter"],
  .rail-horizontal [data-tool="focus"],
  .rail-horizontal #undo-btn,
  .rail-horizontal .stroke-size-control.horizontal { display: none; }
  .rail-horizontal .colors .color-btn:nth-of-type(n+4) { display: none; }

  /* Right rail — hide everything except the comm-ptt (mic) + status pill.
     Use IDs for the four buttons because :nth-of-type counts by tag
     name (all <div>), so :nth-of-type(2) on .rail-section would match
     the .rail-sep div between the two sections — exactly the wrong one. */
  #rail-right .rail-sep,
  #hint-btn,
  #snapshot-btn,
  #send-work-btn,
  #chat-toggle-btn { display: none; }

  /* With only the mic remaining in the right rail, drop the rail's
     padding so it reads as a single mic button at bottom-right rather
     than a stretched pill. */
  #rail-right { padding: 4px; max-height: 44px; }

  /* Top rail no longer needs to wrap — pen + eraser + trash + 3 colors
     fits one row even on a 360px viewport. Switch back to nowrap so
     the row reads as a single clean pill. */
  .rail-horizontal { flex-wrap: nowrap; }
}
