/* ── Fonts ─────────────────────────────────────────────────────────── */
/* Fredoka (SIL Open Font License), self-hosted so no visitor data goes to a font CDN. */
@font-face {
  font-family: 'Fredoka';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('/fonts/fredoka-latin.woff2') format('woff2');
}

/* ── Design tokens ─────────────────────────────────────────────────── */
:root {
  color-scheme: light;

  /* Brand */
  --brand-orange: #f97316;
  --brand-pink: #ec4899;
  --brand-pink-strong: #be185d;
  --brand-indigo: #6366f1;
  --brand-gradient: linear-gradient(135deg, #f97316, #ec4899 55%, #6366f1);
  --brand-gradient-flat: linear-gradient(90deg, #f97316, #ec4899 50%, #6366f1);
  --brand-gradient-text: linear-gradient(90deg, #ea580c, #db2777 55%, #4f46e5);

  /* Text */
  --text-strong: #1e1b4b;
  --text: #1f2937;
  --text-muted: #4b5563;
  --text-subtle: #6b7280;

  /* Surfaces and lines */
  --surface: #ffffff;
  --surface-soft: #f8f9ff;
  --surface-sunken: #eef0f8;
  --border: #e4e7f2;
  --border-strong: #cdd3e6;

  /* Interactive accent: links, secondary buttons, focus */
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --accent-border: #c7d2fe;
  --focus-ring: #4f46e5;
  --focus-glow: 0 0 0 3px rgba(79, 70, 229, 0.22);

  /* Status */
  --success-text: #065f46;
  --success-bg: #ecfdf5;
  --success-line: #10b981;
  --info-text: #1e3a8a;
  --info-bg: #eff6ff;
  --info-line: #3b82f6;
  --warning-text: #854d0e;
  --warning-bg: #fefce8;
  --warning-line: #eab308;
  --danger-text: #b91c1c;
  --danger-bg: #fef2f2;
  --danger-line: #ef4444;

  /* Shape */
  --radius-sm: 0.6rem;
  --radius-md: 0.9rem;
  --radius-lg: 1.25rem;
  --radius-pill: 999px;

  /* Elevation */
  --shadow-sm: 0 1px 2px rgba(30, 27, 75, 0.06), 0 2px 6px rgba(30, 27, 75, 0.05);
  --shadow-md: 0 2px 4px rgba(30, 27, 75, 0.04), 0 12px 28px rgba(79, 70, 229, 0.1);
  --shadow-lg: 0 24px 60px rgba(30, 27, 75, 0.28);
  --shadow-button: 0 3px 0 rgba(157, 23, 77, 0.3), 0 6px 14px rgba(236, 72, 153, 0.22);
  --shadow-button-pressed: 0 1px 0 rgba(157, 23, 77, 0.3), 0 2px 6px rgba(236, 72, 153, 0.2);

  /* Type */
  --font-display: 'Fredoka', ui-rounded, 'Segoe UI', system-ui, sans-serif;
  --font-body: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: ui-monospace, 'Cascadia Code', 'Fira Code', Consolas, monospace;

  /* Code editor: the highlight overlay and the textarea must share these exactly. */
  --code-font-size: 1rem;
  --code-line-height: 1.4;
  --code-pad: 0.75rem;

  font-family: var(--font-body);
  color: var(--text);
  accent-color: var(--brand-pink);
  -webkit-text-size-adjust: 100%;
}

/* ── Base ──────────────────────────────────────────────────────────── */
*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  overflow-x: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  font-size: 1rem;
  line-height: 1.55;
  background-color: #eef2ff;
  /* Dotted "graph paper" over a warm-to-cool wash. */
  background-image:
    radial-gradient(circle, rgba(99, 102, 241, 0.13) 1px, transparent 1.5px),
    radial-gradient(circle at 15% 10%, #fff7ed 0%, #eef2ff 45%, #e0f2fe 100%);
  background-size: 24px 24px, 100% 100%;
}

/* Mode switch: body[data-mode] is "ai" when an API key (session or server) is available, otherwise "examples". */
body[data-mode="examples"] .ai-only,
body[data-mode="ai"] .examples-only {
  display: none !important;
}

h1,
h2,
h3,
h4 {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.2;
  color: var(--text-strong);
}

h1 {
  margin: 0 0 0.5rem;
}

h2 {
  margin: 0 0 0.25rem;
  font-size: 1.4rem;
}

h3 {
  margin: 1rem 0 0.5rem;
  font-size: 1.1rem;
}

a {
  color: var(--accent);
  text-underline-offset: 0.15em;
}

code,
pre,
kbd {
  font-family: var(--font-mono);
}

:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Skip-link and landmark targets receive focus programmatically; no ring needed there. */
[tabindex="-1"]:focus {
  outline: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
  z-index: 9999;
}

.skip-link:focus {
  position: fixed;
  top: 0;
  left: 0;
  width: auto;
  height: auto;
  padding: 0.6rem 1rem;
  background: var(--accent);
  color: #ffffff;
  font-weight: 600;
  text-decoration: none;
  border-radius: 0 0 var(--radius-sm) 0;
}

.noscript-notice {
  max-width: 1200px;
  margin: 1rem auto 0;
  padding: 0.75rem 1rem;
  border-left: 4px solid var(--warning-line);
  border-radius: var(--radius-sm);
  background: var(--warning-bg);
  color: var(--warning-text);
  font-weight: 600;
}

/* ── Buttons ───────────────────────────────────────────────────────── */
/* Primary is the default; .secondary-button and .danger-button are the only variants. */
button,
.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 44px;
  padding: 0.55rem 1.15rem;
  border: 2px solid transparent;
  border-radius: var(--radius-pill);
  background: var(--brand-gradient);
  color: #ffffff;
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.1;
  letter-spacing: 0.01em;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: var(--shadow-button);
  transition: filter 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease,
    border-color 0.15s ease;
}

button:hover,
.button-link:hover {
  filter: brightness(1.06);
}

button:active,
.button-link:active {
  transform: translateY(1px);
  box-shadow: var(--shadow-button-pressed);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  filter: grayscale(0.35);
  transform: none;
  box-shadow: none;
}

.secondary-button {
  background: var(--surface);
  color: var(--accent);
  border-color: var(--accent-border);
  box-shadow: 0 2px 0 rgba(79, 70, 229, 0.12);
}

.secondary-button:hover {
  background: var(--accent-soft);
  filter: none;
}

.secondary-button:active {
  box-shadow: none;
}

.danger-button {
  background: var(--surface);
  color: var(--danger-text);
  border-color: #fecaca;
  box-shadow: none;
}

.danger-button:hover {
  background: var(--danger-bg);
  filter: none;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.9rem;
  align-items: center;
}

.chip,
.saved-project-button {
  min-height: 40px;
  padding: 0.4rem 0.95rem;
  border-color: #fbcfe8;
  background: #fdf2f8;
  color: var(--brand-pink-strong);
  box-shadow: none;
  font-size: 0.95rem;
}

.chip:hover,
.saved-project-button:hover {
  background: #fce7f3;
  filter: none;
}

.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.9rem;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  min-height: 44px;
  padding: 0 0.25rem;
  color: var(--text-strong);
  font-family: var(--font-display);
  font-weight: 600;
  cursor: pointer;
}

.toggle input {
  width: 1.2rem;
  height: 1.2rem;
  margin: 0;
}

.loading {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  color: var(--text-subtle);
  font-size: 0.95rem;
  min-height: 1.25rem;
}

.loading.is-active::before {
  content: '';
  width: 0.95rem;
  height: 0.95rem;
  border-radius: 50%;
  border: 2px solid var(--accent-border);
  border-top-color: var(--accent);
  animation: spin-loader 0.75s linear infinite;
}

#generate-button.is-busy {
  position: relative;
}

#generate-button.is-busy::after {
  content: '';
  position: absolute;
  inset: -2px;
  border-radius: var(--radius-pill);
  box-shadow: 0 0 0 3px rgba(236, 72, 153, 0.2);
  animation: pulse-loader 1.2s ease-in-out infinite;
}

@keyframes spin-loader {
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse-loader {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.55;
  }

  50% {
    transform: scale(1.03);
    opacity: 1;
  }
}

/* ── Form fields ───────────────────────────────────────────────────── */
.field {
  display: grid;
  gap: 0.35rem;
}

label {
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text-strong);
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="url"],
select,
textarea {
  width: 100%;
  min-height: 44px;
  padding: 0.6rem 0.8rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:hover,
select:hover,
textarea:hover {
  border-color: var(--border-strong);
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--focus-glow);
}

textarea {
  resize: vertical;
}

.field-help {
  margin: 0;
  font-size: 0.875rem;
  color: var(--text-subtle);
}

/* ── Callouts (hints, notes, warnings, errors) ─────────────────────── */
.callout {
  margin: 0 0 0.75rem;
  padding: 0.65rem 0.9rem;
  border-left: 4px solid;
  border-radius: var(--radius-sm);
  line-height: 1.5;
}

ul.callout {
  padding-left: 2rem;
}

.callout-info {
  color: var(--info-text);
  background: var(--info-bg);
  border-left-color: var(--info-line);
}

.callout-success {
  color: var(--success-text);
  background: var(--success-bg);
  border-left-color: var(--success-line);
}

.callout-warning {
  color: var(--warning-text);
  background: var(--warning-bg);
  border-left-color: var(--warning-line);
}

.callout-danger {
  color: var(--danger-text);
  background: var(--danger-bg);
  border-left-color: var(--danger-line);
}

.callout a {
  color: inherit;
  font-weight: 600;
}

.error {
  margin: 0.75rem 0 0;
  color: var(--danger-text);
  min-height: 1.2rem;
}

#code-error:not(:empty) {
  padding: 0.55rem 0.8rem;
  border-left: 4px solid var(--danger-line);
  border-radius: var(--radius-sm);
  background: var(--danger-bg);
  font-size: 0.95rem;
  line-height: 1.45;
}

.token-status {
  margin: 0.75rem 0 0;
  min-height: 1.1rem;
  color: var(--accent);
  font-weight: 500;
}

.token-status:empty {
  margin: 0;
}

.token-status a {
  color: inherit;
}

/* ── Panels ────────────────────────────────────────────────────────── */
.panel {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  box-shadow: var(--shadow-md);
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.9rem;
}

.panel-header h2 {
  margin: 0 0 0.15rem;
}

.panel-header .panel-subtitle {
  margin: 0;
}

.panel-subtitle {
  margin: 0 0 0.75rem;
  color: var(--text-muted);
}

/* ── Brand ─────────────────────────────────────────────────────────── */
.brand-mark {
  display: block;
  flex-shrink: 0;
}

.brand-wordmark {
  padding-bottom: 0.06em;
  font-family: var(--font-display);
  font-weight: 700;
  letter-spacing: -0.01em;
  background: var(--brand-gradient-text);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

@media (forced-colors: active) {
  .brand-wordmark {
    background: none;
    color: CanvasText;
  }
}

/* ── Intro page ────────────────────────────────────────────────────── */
.intro-page {
  min-height: 100vh;
  padding: clamp(1.5rem, 5vw, 3.5rem) 1.25rem 2rem;
}

.intro-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(320px, 0.95fr);
  gap: clamp(1.5rem, 4vw, 3rem);
  max-width: 1200px;
  margin: 0 auto;
  align-items: center;
}

.intro-title {
  display: flex;
  align-items: center;
  gap: 0.2em;
  margin: 0;
  font-size: clamp(2.75rem, 6vw, 4.5rem);
  line-height: 1;
}

.intro-title .brand-mark {
  width: 1.05em;
  height: 1.05em;
  filter: drop-shadow(0 6px 10px rgba(26, 92, 53, 0.25));
}

.intro-lead {
  max-width: 40rem;
  margin: 1.25rem 0 0;
  font-size: 1.22rem;
  line-height: 1.6;
  color: var(--text);
}

.intro-body {
  max-width: 40rem;
  margin: 0.9rem 0 0;
  color: var(--text-muted);
}

.intro-steps {
  display: grid;
  gap: 0.6rem;
  max-width: 40rem;
  margin: 1.75rem 0 0;
  padding: 0;
  list-style: none;
}

.intro-step {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 0.7rem 0.95rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.75);
}

.intro-step strong {
  font-family: var(--font-display);
  font-weight: 600;
  color: var(--text-strong);
}

.intro-step-number {
  display: inline-grid;
  flex-shrink: 0;
  place-items: center;
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 50%;
  color: #ffffff;
  font-family: var(--font-display);
  font-weight: 700;
}

.intro-step:nth-child(1) .intro-step-number {
  background: #ea580c;
}

.intro-step:nth-child(2) .intro-step-number {
  background: #db2777;
}

.intro-step:nth-child(3) .intro-step-number {
  background: #4f46e5;
}

.intro-actions {
  margin-top: 1.5rem;
}

.intro-setup {
  padding: 1.5rem;
  border-color: rgba(236, 72, 153, 0.2);
  box-shadow: 0 24px 48px rgba(79, 70, 229, 0.16);
}

.intro-setup h2 {
  margin: 0 0 1rem;
  font-size: 1.5rem;
}

.intro-choices {
  display: grid;
  gap: 0.9rem;
}

.intro-choice {
  padding: 1rem 1.1rem 1.15rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-soft);
}

.intro-choice h3 {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin: 0 0 0.4rem;
  font-size: 1.2rem;
}

.choice-icon {
  display: inline-grid;
  place-items: center;
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 0.65rem;
  background: var(--accent-soft);
  font-size: 1.1rem;
}

.intro-showcase {
  max-width: 1200px;
  margin: clamp(2rem, 5vw, 3rem) auto 0;
}

.intro-showcase h2 {
  margin: 0 0 0.2rem;
  font-size: 1.5rem;
}

.examples-grid.intro-showcase-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1rem;
}

/* ── Studio ────────────────────────────────────────────────────────── */
.studio {
  padding-bottom: 1rem;
}

.topbar {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 1.25rem 0.7rem;
  background: rgba(255, 255, 255, 0.88);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.topbar::before {
  content: '';
  position: absolute;
  inset: 0 0 auto;
  height: 4px;
  background: var(--brand-gradient-flat);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin: 0;
  font-size: 1.6rem;
}

.brand .brand-mark {
  width: 2.25rem;
  height: 2.25rem;
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.ai-connection-badge {
  padding: 0.35rem 0.9rem 0.35rem 0.75rem;
  border-color: var(--border);
  background: var(--surface);
  color: var(--text-muted);
  box-shadow: none;
  font-size: 0.92rem;
}

.ai-connection-badge::before {
  content: '';
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  background: #9ca3af;
}

.ai-connection-badge[data-state="on"] {
  border-color: #a7f3d0;
  color: var(--success-text);
}

.ai-connection-badge[data-state="on"]::before {
  background: var(--success-line);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.2);
}

.ai-connection-badge:hover {
  background: var(--accent-soft);
  filter: none;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-areas:
    'canvas code'
    'explain explain';
  gap: 1.25rem;
  max-width: 1440px;
  margin: 0 auto;
  padding: 1.25rem;
}

.canvas-panel {
  grid-area: canvas;
}

.code-panel {
  grid-area: code;
}

.explain-panel {
  grid-area: explain;
}

/* Canvas */
.canvas-wrapper {
  position: relative;
  display: block;
  line-height: 0;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: #ffffff;
}

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

#turtle-sprite-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#explain-loading:not([hidden]) {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 1rem;
  color: var(--text-strong);
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(2px);
}

#explain-loading p {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 600;
  line-height: 1.3;
}

.explain-spinner {
  display: block;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--brand-pink);
  animation: spin-loader 0.75s linear infinite;
}

/* Code editor: a transparent textarea over a highlighted <pre>. */
.code-editor-wrapper {
  position: relative;
  display: block;
  aspect-ratio: 4 / 3;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-soft);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.code-editor-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: var(--focus-glow);
}

#code-highlight,
.code-editor-wrapper #code-editor {
  margin: 0;
  padding: var(--code-pad);
  font-family: var(--font-mono);
  font-size: var(--code-font-size);
  line-height: var(--code-line-height);
  white-space: pre;
  overflow: auto;
  border-radius: var(--radius-md);
}

#code-highlight {
  position: absolute;
  inset: 0;
  z-index: 0;
  word-wrap: normal;
  pointer-events: none;
  color: var(--text);
  scrollbar-width: none;
}

#code-highlight::-webkit-scrollbar {
  display: none;
}

.code-editor-wrapper #code-editor {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  border: none;
  background: transparent;
  color: transparent;
  caret-color: var(--text-strong);
  resize: none;
  outline: none;
  box-shadow: none;
  transition: none;
}

.code-comment {
  color: var(--text-subtle);
  font-style: italic;
}

.code-line {
  display: block;
}

.code-error-line,
.code-active-line {
  display: block;
  margin: 0 calc(-1 * var(--code-pad));
  padding: 0 var(--code-pad);
  border-radius: 2px;
}

.code-error-line {
  background: rgba(239, 68, 68, 0.13);
  outline: 1px solid rgba(239, 68, 68, 0.3);
}

.code-active-line {
  background: rgba(234, 179, 8, 0.22);
  outline: 1px solid rgba(234, 179, 8, 0.5);
}

.help-button {
  flex-shrink: 0;
  width: 2.5rem;
  height: 2.5rem;
  min-height: 0;
  padding: 0;
  border-radius: 50%;
  border-color: var(--accent-border);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 1.1rem;
  font-weight: 700;
  box-shadow: none;
}

.help-button:hover {
  background: #e0e7ff;
  filter: none;
}

/* Tabs (segmented control) */
.tab-list {
  display: flex;
  gap: 0.25rem;
  width: fit-content;
  max-width: 100%;
  margin-bottom: 1rem;
  padding: 0.3rem;
  overflow-x: auto;
  border-radius: var(--radius-pill);
  background: var(--surface-sunken);
}

.tab-button {
  min-height: 40px;
  padding: 0.45rem 1.05rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  box-shadow: none;
  font-size: 0.98rem;
  white-space: nowrap;
}

.tab-button:hover {
  background: rgba(255, 255, 255, 0.6);
  color: var(--text-strong);
  filter: none;
}

.tab-button:active {
  transform: none;
  box-shadow: none;
}

.tab-button.active {
  background: var(--surface);
  color: var(--brand-pink-strong);
  box-shadow: var(--shadow-sm);
}

.tab-content {
  display: none;
  max-height: 400px;
  overflow-y: auto;
  border-radius: var(--radius-sm);
}

.tab-content.active {
  display: block;
}

/* Explain tab */
.explain-title {
  margin: 0 0 0.25rem;
  font-size: 1.3rem;
}

.explain-description {
  margin: 0 0 0.5rem;
  color: var(--text-muted);
}

.ai-overview {
  margin: 0.25rem 0 0.75rem;
}

.explain-error-message {
  margin: 0 0 0.25rem;
  line-height: 1.5;
}

.suggestions h3 {
  margin-top: 0.5rem;
}

.suggestions ul {
  margin: 0;
  padding-left: 1.3rem;
}

.suggestions li + li {
  margin-top: 0.3rem;
}

.suggestions li::marker {
  color: var(--brand-pink);
}

.saved-projects {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* Debug tab */
.debug-controls {
  margin-bottom: 0.75rem;
}

.debug-log {
  max-height: 300px;
  overflow-y: auto;
  padding: 0.75rem;
  border-radius: var(--radius-sm);
  background: var(--text-strong);
  color: #d1d5db;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.debug-log-entry {
  margin: 0.5rem 0;
  padding: 0.5rem;
  border-left: 3px solid;
  border-radius: 0.25rem;
}

.debug-log-entry.request {
  border-left-color: #3b82f6;
  background: rgba(59, 130, 246, 0.1);
  color: #93c5fd;
}

.debug-log-entry.response {
  border-left-color: #10b981;
  background: rgba(16, 185, 129, 0.1);
  color: #86efac;
}

.debug-log-entry.error {
  border-left-color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
  color: #fca5a5;
}

.debug-log-entry.info {
  border-left-color: #f59e0b;
  background: rgba(245, 158, 11, 0.1);
  color: #fbbf24;
}

.debug-timestamp {
  display: block;
  margin-bottom: 0.25rem;
  font-size: 0.75rem;
  opacity: 0.7;
}

.debug-data {
  margin: 0.25rem 0 0;
  overflow: auto;
  font-size: 0.8rem;
}

.debug-empty {
  margin: 0;
  padding: 1rem;
  color: #9ca3af;
  text-align: center;
}

/* Community tab */
.community-submit-form {
  display: grid;
  gap: 0.75rem;
  max-width: 36rem;
}

.community-submit-form .button-row {
  margin-top: 0.25rem;
}

.community-gallery-list {
  display: grid;
  gap: 0.75rem;
  margin-top: 1rem;
}

.community-gallery-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.community-gallery-summary {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.community-gallery-pager {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
}

.page-button {
  min-width: 40px;
  min-height: 40px;
  padding: 0.3rem 0.7rem;
  border-color: var(--accent-border);
  background: var(--surface);
  color: var(--accent);
  box-shadow: none;
}

.page-button:hover {
  background: var(--accent-soft);
  filter: none;
}

.page-button.is-active {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

.community-gallery-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
}

.community-card {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-height: 100%;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.community-card h3 {
  margin: 0;
}

.community-card-body {
  display: grid;
  gap: 0.45rem;
}

.community-description,
.community-explanation,
.community-meta {
  margin: 0;
}

.community-explanation {
  display: -webkit-box;
  overflow: hidden;
  color: var(--text);
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
}

.community-meta {
  color: var(--text-muted);
  font-size: 0.92rem;
}

.community-card-actions {
  display: flex;
  justify-content: flex-start;
  margin-top: auto;
}

.community-tags {
  margin: 0;
  color: var(--brand-pink-strong);
  font-weight: 600;
}

.community-card summary,
.admin-entry summary {
  color: var(--accent);
  font-weight: 600;
  cursor: pointer;
}

.community-card pre,
.admin-entry pre {
  max-height: 180px;
  margin: 0.5rem 0 0;
  padding: 0.5rem 0.65rem;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-soft);
  font-size: 0.88rem;
}

/* ── Ready-made drawing cards (picker and intro showcase) ──────────── */
.examples-heading {
  margin: 1.5rem 0 0.25rem;
}

.examples-section .panel-subtitle {
  margin-bottom: 0.75rem;
}

.examples-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.75rem;
}

.example-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.45rem;
  min-height: 0;
  padding: 0.45rem 0.45rem 0.6rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text-strong);
  box-shadow: var(--shadow-sm);
  text-align: center;
}

.example-card:hover,
.example-card:focus-visible {
  border-color: #f9a8d4;
  filter: none;
  transform: translateY(-3px);
  box-shadow: 0 10px 20px rgba(236, 72, 153, 0.18);
}

.example-card:active {
  transform: translateY(-1px);
}

.example-thumbnail {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 4 / 3;
  border: 1px solid var(--surface-sunken);
  border-radius: var(--radius-sm);
}

.example-title {
  font-size: 0.95rem;
  line-height: 1.2;
}

.examples-ai-cta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
  margin: 1.25rem 0 0;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
}

/* ── Modals ────────────────────────────────────────────────────────── */
.modal {
  width: min(640px, calc(100vw - 2rem));
  max-width: 100%;
  max-height: calc(100dvh - 2rem);
  padding: 0;
  overflow-y: auto;
  border: none;
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}

.modal::backdrop {
  background: rgba(30, 27, 75, 0.5);
  backdrop-filter: blur(2px);
}

.modal-body {
  padding: 1.25rem 1.25rem 1.5rem;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.45rem;
}

.prompt-ai-section .button-row {
  margin-top: 0.75rem;
}

/* AI setup modal */
.token-modal {
  width: min(560px, calc(100vw - 2rem));
}

.token-form {
  display: grid;
  gap: 0.5rem;
}

.token-form .modal-header {
  margin-bottom: 0;
}

.token-step-caption {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: 0.2rem 0 0;
}

.token-step-caption span + span::before {
  content: '·';
  margin-right: 0.35rem;
}

.token-steps {
  display: flex;
  gap: 0.5rem;
  margin: 0.25rem 0 0.5rem;
}

.token-step-indicator {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  background: var(--surface-sunken);
  color: var(--text-muted);
  font-family: var(--font-display);
  font-weight: 700;
}

.token-step-indicator.active {
  background: var(--brand-gradient);
  color: #ffffff;
}

.token-step {
  display: grid;
  gap: 0.75rem;
}

.token-step .button-row {
  margin-top: 0.25rem;
}

.token-cta-row {
  display: flex;
}

/* Help modal */
.help-modal {
  width: min(760px, calc(100vw - 2rem));
}

.help-group-title {
  margin: 1.5rem 0 0.35rem;
  font-size: 1.2rem;
}

.help-canvas .help-group-title {
  margin-top: 0.25rem;
}

.help-canvas {
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.help-diagrams {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 1rem;
  align-items: center;
  margin: 0.5rem 0 1rem;
}

.help-diagram {
  display: block;
  width: 100%;
  height: auto;
}

.help-diagram text {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 500;
  fill: var(--text-muted);
}

.help-map .map-canvas {
  fill: var(--surface);
  stroke: var(--border-strong);
  stroke-width: 2;
}

.help-map .map-grid {
  fill: none;
  stroke: var(--border);
  stroke-width: 1;
}

.help-map .map-axis {
  fill: none;
  stroke: #a5b4fc;
  stroke-width: 2;
}

.help-map .map-size {
  font-weight: 600;
  fill: var(--text-strong);
}

.help-compass .compass-ring {
  fill: var(--surface-soft);
  stroke: var(--border);
  stroke-width: 2;
}

.help-compass .compass-arrow {
  stroke: var(--accent);
  stroke-width: 3;
  stroke-linecap: round;
}

.help-compass .compass-arrowhead {
  fill: var(--accent);
}

.help-compass .compass-turn {
  fill: none;
  stroke: var(--brand-pink);
  stroke-width: 3;
  stroke-linecap: round;
}

.help-compass .compass-turnhead {
  fill: var(--brand-pink);
}

.help-compass text {
  font-size: 18px;
  font-weight: 700;
  fill: var(--accent);
}

.help-compass .compass-turn-label {
  font-size: 15px;
  fill: var(--brand-pink-strong);
}

.help-facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 0.5rem 1.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.help-facts strong {
  color: var(--text-strong);
  font-family: var(--font-display);
  font-weight: 600;
}

.help-facts code {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: var(--surface-sunken);
  color: var(--text-strong);
  font-size: 0.85rem;
}

.help-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.25rem;
}

.help-section-title {
  margin: 0 0 0.5rem;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.help-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0;
  padding: 0;
}

.help-item {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  font-size: 0.9rem;
  line-height: 1.4;
}

.help-item dt {
  flex-shrink: 0;
  min-width: 150px;
}

.help-item code {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: var(--surface-sunken);
  color: var(--text-strong);
  font-size: 0.85rem;
}

.help-item dd {
  margin: 0;
  color: var(--text-muted);
}

.help-note {
  margin: 0.6rem 0 0;
  color: var(--text-subtle);
  font-size: 0.82rem;
  line-height: 1.45;
}

.help-example {
  margin: 0.65rem 0 0;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  background: var(--surface-sunken);
  color: var(--text);
  font-size: 0.85rem;
  line-height: 1.5;
}

/* ── Toasts ────────────────────────────────────────────────────────── */
.toast-viewport {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 2000;
  display: grid;
  gap: 0.55rem;
  max-width: min(420px, calc(100vw - 2rem));
  pointer-events: none;
}

.toast {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  color: #ffffff;
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 600;
  box-shadow: 0 10px 22px rgba(30, 27, 75, 0.24);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.16s ease, transform 0.16s ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.toast-success {
  background: linear-gradient(135deg, #059669, #10b981);
}

.toast-error {
  background: linear-gradient(135deg, #b91c1c, #ef4444);
}

/* ── Footer ────────────────────────────────────────────────────────── */
.credit-footer {
  width: 100%;
  padding: 1.25rem 1rem 1.5rem;
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
}

.credit-footer a {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.credit-footer a:hover {
  text-decoration: underline;
}

/* ── Admin ─────────────────────────────────────────────────────────── */
.admin-page {
  display: grid;
  gap: 1.25rem;
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.25rem;
}

.admin-page .brand {
  font-size: 1.8rem;
}

.admin-login-form {
  display: grid;
  gap: 0.5rem;
  max-width: 28rem;
}

.admin-entry-list {
  display: grid;
  gap: 0.75rem;
}

.admin-entry {
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.admin-entry h3 {
  margin-top: 0;
}

.admin-entry p {
  margin: 0.2rem 0;
}

.admin-preview-modal {
  width: min(920px, calc(100vw - 2rem));
}

.admin-preview-shell {
  display: grid;
  gap: 0.65rem;
}

.admin-preview-shell .modal-header {
  margin-bottom: 0;
}

#admin-preview-canvas {
  display: block;
  width: 100%;
  height: auto;
}

/* ── Responsive ────────────────────────────────────────────────────── */
@media (max-width: 980px) {
  .intro-page {
    min-height: auto;
  }

  .intro-grid {
    grid-template-columns: 1fr;
  }

  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      'canvas'
      'code'
      'explain';
  }

  .community-gallery-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 720px) {
  .topbar {
    flex-wrap: wrap;
  }

  .topbar-actions {
    width: 100%;
  }

  .topbar-actions #open-prompt-button {
    margin-left: auto;
  }

  .examples-grid.intro-showcase-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  .community-gallery-grid {
    grid-template-columns: 1fr;
  }

  .toast-viewport {
    left: 1rem;
    right: 1rem;
    bottom: 0.9rem;
    max-width: none;
  }
}

@media (max-width: 480px) {
  .intro-page {
    padding: 1.25rem 1rem 1.5rem;
  }

  .intro-lead {
    font-size: 1.08rem;
  }

  .intro-setup {
    padding: 1.1rem;
  }

  .topbar {
    padding: 0.75rem 0.75rem 0.6rem;
  }

  .brand {
    font-size: 1.35rem;
  }

  .brand .brand-mark {
    width: 1.9rem;
    height: 1.9rem;
  }

  .layout {
    gap: 0.75rem;
    padding: 0.75rem;
  }

  .panel {
    padding: 0.9rem;
  }

  .button-row {
    gap: 0.45rem;
  }

  .examples-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .help-grid {
    grid-template-columns: 1fr;
  }

  .help-item dt {
    min-width: 130px;
  }

  .help-diagrams {
    grid-template-columns: 1fr;
  }

  /* The map scales down on phones, so give its labels more room to stay readable. */
  .help-map text {
    font-size: 21px;
  }

  .help-compass {
    max-width: 200px;
    margin: 0 auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
  }

  button:active,
  .button-link:active,
  .example-card:hover,
  .example-card:focus-visible,
  .example-card:active {
    transform: none;
  }
}
