/* ── Visualizer Layout ── */
.visualizer-main {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: var(--space-lg);
  padding: var(--space-lg) var(--space-xl);
  min-height: calc(100vh - 65px);
  align-items: start;
}

/* ── Controls Panel ── */
.controls-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  position: sticky;
  top: var(--space-lg);
  /* Scroll inside the panel when its content exceeds viewport height */
  max-height: calc(100vh - var(--space-xl));
  overflow-y: auto;
  overflow-x: clip; /* prevent horizontal scrollbar from tooltip overflow */
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.controls-panel::-webkit-scrollbar       { width: 6px; }
.controls-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.controls-panel::-webkit-scrollbar-track { background: transparent; }

.controls-panel h2 {
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding-bottom: var(--space-sm);
  border-bottom: 1px solid var(--border-subtle);
}

/* ── Section headers within the controls panel ── */
.section-header {
  font-family: var(--font-display);
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border-subtle);
  margin: var(--space-sm) 0 0;
}

/* `hidden` attribute must override `display:flex` on control-group */
.control-group[hidden] { display: none; }

/* ── Curve Type Tabs ── */
.curve-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.curve-tab {
  background: var(--bg-elevated);
  border: none;
  border-right: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1.3;
  padding: 7px 4px;
  text-align: center;
  transition: background 0.15s, color 0.15s;
}

.curve-tab:last-child { border-right: none; }

.curve-tab:hover:not(.active) {
  background: var(--accent-dim);
  color: var(--text-primary);
}

.curve-tab.active {
  background: var(--accent);
  color: #fff;
}

/* ── Layer chip type badge ── */
.chip-badge {
  display: inline-block;
  background: var(--accent-dim);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--accent-hover);
  font-size: 0.6rem;
  font-family: var(--font-mono);
  padding: 0 3px;
  vertical-align: middle;
  margin-left: 2px;
  line-height: 1.4;
}

.preset-select {
  background: var(--accent-dim);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 0.9rem;
  padding: var(--space-sm) var(--space-md);
  width: 100%;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23818cf8' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

.preset-select:focus { outline: none; border-color: var(--accent); }

.slider-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2px;
}

.slider-value {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--accent);
}

.slider-value.clickable {
  cursor: pointer;
  user-select: none;
  padding: 1px 6px;
  border-radius: var(--radius-sm);
  transition: background 0.12s ease;
}
.slider-value.clickable:hover  { background: var(--accent-dim); }
.slider-value.clickable:active { background: var(--accent); color: var(--bg-base); }

/* Approximate physical pen-hole number, shown muted/smaller next to the % */
.pen-hole-hint {
  color: var(--text-muted);
  font-size: 0.72em;
}

/* ── Color label row (label + auto-cycle toggle) ── */
.color-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-xs);
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

.toggle-label input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  position: relative;
  width: 30px;
  height: 17px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 17px;
  transition: background 0.2s, border-color 0.2s;
  flex-shrink: 0;
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 11px;
  height: 11px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform 0.2s, background 0.2s;
}

.toggle-label input:checked + .toggle-track {
  background: var(--accent-dim);
  border-color: var(--accent);
}

.toggle-label input:checked + .toggle-track .toggle-thumb {
  transform: translateX(13px);
  background: var(--accent);
}

.toggle-text {
  font-size: 0.75rem;
  color: var(--text-muted);
  transition: color 0.2s;
}

.toggle-label:has(input:checked) .toggle-text {
  color: var(--accent-hover);
}

/* ── Color Swatches ── */
.color-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: var(--space-xs);
}

.color-swatch {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  transition: transform 0.12s, box-shadow 0.12s;
  /* Ring indicator: gap + outline, visible on any bg */
  box-shadow: 0 0 0 0px var(--bg-surface), 0 0 0 0px transparent;
}

.color-swatch:hover {
  transform: scale(1.25);
}

.color-swatch.active {
  box-shadow: 0 0 0 2px var(--bg-surface), 0 0 0 4px var(--text-primary);
  transform: scale(1.15);
}

.divider {
  height: 1px;
  background: var(--border-subtle);
}

/* ── Undo button ── */
.btn-undo {
  background: rgba(251, 146, 60, 0.12);
  color: #fb923c;
  border: 1px solid rgba(251, 146, 60, 0.35);
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.25s, transform 0.25s, background 0.15s;
  pointer-events: none;
}

.btn-undo.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.btn-undo:hover {
  background: rgba(251, 146, 60, 0.22);
  border-color: #fb923c;
  color: #fb923c;
}

/* ── Info Panel ── */
.info-panel {
  background: var(--bg-elevated);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-sm) var(--space-md);
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.info-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  /* text-secondary, not muted: muted fails WCAG contrast on bg-elevated */
  color: var(--text-secondary);
}

.info-value {
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 500;
  color: var(--text-primary);
}

.info-value.highlight { color: var(--accent); }

/* ── Canvas Area ── */
.canvas-area {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  /* Establish stacking context so toolbar z-index is meaningful */
  position: relative;
  z-index: 0;
}

/* Top toolbar must paint above the canvas-wrapper's stacking context */
.canvas-toolbar-top {
  position: relative;
  z-index: 10;
}

.canvas-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  max-width: 700px;
  margin: 0 auto;
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: 0 0 60px var(--border-subtle), 0 0 0 1px var(--border);
}

#spiro-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* ── Canvas Toolbars (above / below canvas) ── */
.canvas-toolbar {
  display: flex;
  gap: var(--space-md);
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-sm) var(--space-md);
}

.toolbar-group {
  display: flex;
  gap: var(--space-sm);
  flex-wrap: wrap;
  align-items: center;
}

.toolbar-group-right { margin-left: auto; }

.canvas-toolbar .btn {
  /* Tighter padding so toolbar stays compact */
  padding: 8px 14px;
  font-size: 0.88rem;
}

.canvas-toolbar .btn-primary {
  /* Make Draw stand out as the hero action */
  padding: 8px 20px;
  font-weight: 600;
}

/* ── Layer Strip ── */
.layer-strip {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-sm) var(--space-md);
  display: flex;
  gap: var(--space-sm);
  flex-wrap: wrap;
  align-items: center;
  min-height: 56px;
}

.layer-strip-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-right: var(--space-xs);
}

.layer-chip {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 10px 4px 6px;
  font-size: 0.78rem;
  cursor: pointer;
  transition: border-color 0.15s;
}

.layer-chip:hover { border-color: var(--accent); }
.layer-chip.hidden { opacity: 0.4; }

.layer-chip-swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.layer-chip-label { color: var(--text-secondary); }

.layer-chip-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1;
  padding: 0 0 0 4px;
}

.layer-chip-remove:hover { color: #f87171; }

/* ── Educational Section ── */
.educational-section {
  padding: var(--space-2xl) var(--space-xl);
  max-width: 860px;
  margin: 0 auto;
}

.educational-section h2 {
  font-family: var(--font-display);
  font-size: 1.8rem;
  font-weight: 700;
  margin-bottom: var(--space-lg);
  color: var(--text-primary);
}

.educational-section h3 {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 600;
  margin: var(--space-xl) 0 var(--space-md);
  color: var(--heading-accent);
}

.educational-section p {
  color: var(--text-secondary);
  margin-bottom: var(--space-md);
  max-width: 65ch;
}

.math-formula {
  background: var(--bg-elevated);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  padding: var(--space-md) var(--space-lg);
  color: var(--text-primary);
  margin: var(--space-md) 0;
  overflow-x: auto;
}

.faq-item {
  border-bottom: 1px solid rgba(99, 102, 241, 0.12);
  padding: var(--space-md) 0;
}

.faq-question {
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 500;
  text-align: left;
  width: 100%;
  padding: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-md);
}

.faq-question::after {
  content: '+';
  color: var(--accent);
  font-size: 1.2rem;
  flex-shrink: 0;
}

.faq-item.open .faq-question::after { content: '−'; }

.faq-answer {
  display: none;
  color: var(--text-secondary);
  padding-top: var(--space-md);
  font-size: 0.95rem;
}

.faq-item.open .faq-answer { display: block; }

/* ── Responsive ── */
@media (max-width: 900px) {
  .visualizer-main {
    grid-template-columns: 1fr;
    padding: var(--space-md);
  }

  .controls-panel { position: static; }
  .canvas-wrapper { max-width: 100%; }
}

@media (max-width: 600px) {
  /* Art first on phones: canvas above the controls */
  .visualizer-main { display: flex; flex-direction: column; }
  .canvas-area { order: -1; }

  .educational-section {
    padding: var(--space-xl) var(--space-md);
  }

  .educational-section h2 { font-size: 1.4rem; }
  .educational-section h3 { font-size: 1rem; }

  /* Ensure math formulas scroll horizontally rather than overflow */
  .math-formula {
    font-size: 0.78rem;
    padding: var(--space-sm) var(--space-md);
  }

  /* Layer strip: smaller chips on narrow screens */
  .layer-chip { font-size: 0.72rem; padding: 3px 8px 3px 5px; }
}

@media (max-width: 480px) {
  .info-panel { grid-template-columns: 1fr 1fr; }

  /* LCM row — allow font to shrink so value fits */
  .info-value { font-size: 0.9rem; }
  #info-lcm { font-size: 0.72rem !important; }

  .canvas-toolbar { gap: var(--space-xs); padding: var(--space-xs) var(--space-sm); }
  .canvas-toolbar .btn { font-size: 0.8rem; padding: 6px 10px; }
  .canvas-toolbar .btn-primary { padding: 6px 14px; }
  .toolbar-group { gap: var(--space-xs); }
}

/* ── Fullscreen / zen mode ─────────────────────────────────────────── */
.canvas-area:fullscreen {
  background: #050510;
  display: flex;
  align-items: center;
  justify-content: center;
}

.canvas-area:fullscreen .canvas-toolbar,
.canvas-area:fullscreen .layer-strip {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: auto;
  opacity: 0.2;
  transition: opacity 0.25s ease;
  z-index: 2;
}

.canvas-area:fullscreen .canvas-toolbar-top    { top: 16px; }
.canvas-area:fullscreen .layer-strip           { display: none; }
.canvas-area:fullscreen .canvas-toolbar-bottom { bottom: 16px; }

.canvas-area:fullscreen .canvas-toolbar:hover,
.canvas-area:fullscreen .canvas-toolbar:focus-within { opacity: 1; }

/* Selected-for-editing layer chip */
.layer-chip.selected {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ── Personal gallery ──────────────────────────────────────────────── */
.gallery-section {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--space-lg) var(--space-lg) 0;
}

.gallery-section h2 {
  font-family: var(--font-display);
  font-size: 1.1rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: var(--space-xs);
}

.gallery-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: var(--space-md);
}

.gallery-strip {
  display: flex;
  gap: var(--space-md);
  overflow-x: auto;
  padding-bottom: var(--space-sm);
}

.gallery-card {
  position: relative;
  flex: 0 0 auto;
  border-radius: var(--radius-md);
  overflow: hidden;
  cursor: pointer;
  border: 1px solid var(--bg-elevated);
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.gallery-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.gallery-thumb {
  display: block;
  width: 120px;
  height: 120px;
}

.gallery-delete {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 50%;
  background: rgba(10, 10, 26, 0.75);
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.gallery-card:hover .gallery-delete { opacity: 1; }
.gallery-delete:hover { color: #f87171; }

/* Links inside educational prose must be distinguishable without color */
.educational-section p a {
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
}

.gallery-name {
  font-size: 0.68rem;
  color: var(--text-secondary);
  padding: 3px 6px 5px;
  max-width: 120px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: var(--bg-surface);
}
