:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --accent: #06b6d4;
  --danger: #ef4444;
  --success: #10b981;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.04);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-alt: #334155;
    --border: #334155;
    --text: #f1f5f9;
    --muted: #94a3b8;
  }
}

/* Manual theme overrides via Settings — beat the system query above.
   Each block tweaks the surface palette plus optional --primary/--accent so the
   accent colour fits the mood of the theme. */

/* Light family */
:root[data-theme="light"] {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
}
:root[data-theme="paper"] {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-alt: #f5f5f5;
  --border: #d4d4d4;
  --text: #111111;
  --muted: #555555;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --accent: #0891b2;
}
:root[data-theme="sepia"] {
  --bg: #f4ecd8;
  --surface: #fbf6e6;
  --surface-alt: #ece2c8;
  --border: #d4c5a0;
  --text: #5b4636;
  --muted: #6f5c44; /* darkened from #8b7355 to pass WCAG AA on Sepia surface */
  --primary: #b67c3a;
  --primary-hover: #95622c;
  --accent: #a35a2c;
}
:root[data-theme="solarized-light"] {
  --bg: #fdf6e3;
  --surface: #fffbed;
  --surface-alt: #eee8d5;
  --border: #ddd6c1;
  --text: #586e75;
  --muted: #5d747a; /* darkened from #93a1a1 (which only had 2.58 contrast) */
  --primary: #268bd2;
  --primary-hover: #1e6fa9;
  --accent: #2aa198;
}
:root[data-theme="sky"] {
  --bg: #e0f2fe;
  --surface: #ffffff;
  --surface-alt: #f0f9ff;
  --border: #bae6fd;
  --text: #0c4a6e;
  --muted: #475569;
  --primary: #0284c7;
  --primary-hover: #0369a1;
  --accent: #06b6d4;
}

/* Dark family */
:root[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-alt: #334155;
  --border: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
}
:root[data-theme="solarized-dark"] {
  --bg: #002b36;
  --surface: #073642;
  --surface-alt: #0c4a52;
  --border: #14545d;
  --text: #eee8d5;
  --muted: #93a1a1;
  --primary: #268bd2;
  --primary-hover: #1e6fa9;
  --accent: #2aa198;
}
:root[data-theme="dracula"] {
  --bg: #282a36;
  --surface: #383a4a;
  --surface-alt: #44475a;
  --border: #565969;
  --text: #f8f8f2;
  --muted: #b8c0e0; /* lightened from #9aa1c4 to pass WCAG AA on Dracula surface */
  --primary: #bd93f9;
  --primary-hover: #a07cf0;
  --accent: #ff79c6;
}
:root[data-theme="nord"] {
  --bg: #2e3440;
  --surface: #3b4252;
  --surface-alt: #434c5e;
  --border: #4c566a;
  --text: #d8dee9;
  --muted: #b8c5d6; /* lightened from #9aaabf to pass WCAG AA on Nord surface */
  --primary: #88c0d0;
  --primary-hover: #76aebd;
  --accent: #81a1c1;
}
:root[data-theme="midnight"] {
  --bg: #000814;
  --surface: #001d3d;
  --surface-alt: #003566;
  --border: #023e8a;
  --text: #d4eaff;
  --muted: #93a8c4;
  --primary: #00b4d8;
  --primary-hover: #0096b9;
  --accent: #48cae4;
}

* { box-sizing: border-box; }

/* ── Accessibility primitives ────────────────────────────────────────────── */

/* Skip-to-content link — invisible until focused */
.skip-link {
  position: absolute;
  left: 0;
  top: -100px;
  background: var(--primary);
  color: white;
  padding: 0.6rem 1rem;
  border-radius: 0 0 8px 0;
  text-decoration: none;
  z-index: 2000;
  font-weight: 600;
  transition: top 0.15s;
}
.skip-link:focus { top: 0; outline: 3px solid var(--accent); outline-offset: 2px; }

/* Crisp focus ring on every interactive element via :focus-visible (mouse
   clicks don't trigger a ring; keyboard navigation always does). */
button:focus-visible,
a:focus-visible,
[role="tab"]:focus-visible,
[role="menuitem"]:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Visually-hidden helper for screen-reader-only labels */
.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}

/* Honour the user's reduced-motion preference: kill all transitions,
   animations, and the drawer slide-in. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .nav-drawer { transition: none !important; }
}

/* Touch-target floor: 44 × 44 px on coarse pointers (phones, tablets). */
@media (pointer: coarse) {
  button, .btn, .calc-btn, .palette-cat, .palette-sym,
  .formula-cat, .train-cat, .nav-item, .alt-chip, .chem-chip,
  .train-tile, .copy-menu-item, .output-tab {
    min-height: 44px;
  }
  .calc-btn { padding: 0.7rem 0.5rem; }
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  margin-left: 0.75rem; /* breathing room between the ☰ button and the logo */
}

.btn-menu {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 1.25rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s;
}
.btn-menu:hover { background: var(--primary); color: white; border-color: var(--primary); }

.btn-help {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface-alt);
  color: var(--muted);
  font-size: 1rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  flex-shrink: 0;
  transition: all 0.15s;
}

.btn-help:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.logo { width: 36px; height: 36px; }

.brand h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.tagline {
  color: var(--muted);
  font-size: 0.875rem;
}

/* Tabs are kept in DOM for handlers but visually hidden — menu drives them */
.tabs {
  display: none;
}

.tab {
  background: none;
  border: none;
  padding: 0.75rem 1.25rem;
  font-size: 0.95rem;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
  font-weight: 500;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.tab-icon { font-size: 0.95em; line-height: 1; }

.tab:hover { color: var(--text); }

.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

main {
  flex: 1;
  padding: 1.5rem;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.panel { display: none; }
.panel.active { display: block; animation: fade 0.2s ease; }

@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.editor-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.5rem;
}

.editor-card h2 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
}

.label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.5rem;
  color: var(--muted);
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.label.inline {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  text-transform: none;
  letter-spacing: normal;
  font-weight: 500;
  font-size: 0.875rem;
  margin-bottom: 0;
}

math-field {
  display: block;
  width: 100%;
  min-height: 80px;
  font-size: 1.5rem;
  padding: 1rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  color: var(--text);
}

math-field:focus-within {
  border-color: var(--primary);
  outline: none;
}

.toolbar {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 1rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.btn:hover {
  background: var(--surface-alt);
  border-color: var(--muted);
}

.btn.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.btn.primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}

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

.btn.large {
  padding: 1rem 2rem;
  font-size: 1.1rem;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Copy ▾ dropdown ─────────────────────────────────────────────────────── */

.copy-menu-wrap {
  position: relative;
  display: inline-block;
}
.copy-menu {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 0.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.12);
  padding: 0.25rem;
  display: flex;
  flex-direction: column;
  min-width: 160px;
  z-index: 50;
}
.copy-menu-item {
  background: none;
  border: none;
  padding: 0.5rem 0.75rem;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  border-radius: 6px;
  white-space: nowrap;
}
.copy-menu-item:hover { background: var(--surface-alt); }

.output-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.output-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
}

.output-tab {
  background: none;
  border: none;
  padding: 0.6rem 1rem;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.output-tab:hover { color: var(--text); }
.output-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: var(--surface);
}

.output-panel {
  padding: 1rem 1.25rem;
}

.rendered {
  min-height: 60px;
  font-size: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
}

.code {
  font-family: "SF Mono", Menlo, Monaco, Consolas, monospace;
  background: var(--surface-alt);
  padding: 0.75rem;
  border-radius: 6px;
  margin: 0;
  font-size: 0.875rem;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
}

.muted { color: var(--muted); font-size: 0.9rem; }

.speak-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin: 1rem 0;
}

.mic-icon {
  font-size: 1.3rem;
  display: inline-block;
}

#btn-mic.listening {
  background: var(--danger);
  border-color: var(--danger);
  animation: pulse 1.2s infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
  50% { box-shadow: 0 0 0 12px rgba(239, 68, 68, 0); }
}

.status {
  color: var(--muted);
  font-size: 0.9rem;
}

.status.error { color: var(--danger); }
.status.success { color: var(--success); }

.transcript-box {
  margin-top: 1.25rem;
  padding: 1rem;
  background: var(--surface-alt);
  border-radius: 8px;
}

.transcript-box h3 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.transcript {
  font-size: 1.05rem;
  min-height: 1.5em;
  margin: 0;
}

.canvas-wrap {
  border: 2px dashed var(--border);
  border-radius: 8px;
  background: white;
  overflow: hidden;
  margin-bottom: 1rem;
}

#draw-canvas {
  display: block;
  width: 100%;
  max-width: 100%;
  cursor: crosshair;
  touch-action: none;
}

.history-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.history-header h2 {
  margin: 0;
  flex: 1;
}

.history-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
  background: var(--surface-alt);
  border-radius: 8px;
  gap: 1rem;
  flex-wrap: wrap;
}

.history-item .preview {
  font-size: 1.3rem;
  flex: 1;
  min-width: 200px;
}

.history-item .meta {
  color: var(--muted);
  font-size: 0.8rem;
}

.history-item .actions {
  display: flex;
  gap: 0.5rem;
}

.empty {
  text-align: center;
  padding: 2rem;
}

.app-footer {
  text-align: center;
  padding: 1rem;
  color: var(--muted);
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.hidden { display: none !important; }

/* MFR AI model banner + progress */
.mfr-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: #ede9fe;
  border-radius: 8px;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
@media (prefers-color-scheme: dark) {
  .mfr-banner { background: #2e1065; }
}
.mfr-banner-text { font-size: 0.9rem; flex: 1; }

.mfr-progress-wrap {
  margin-bottom: 1rem;
}
.mfr-progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.mfr-progress-bar {
  height: 6px;
  background: var(--surface-alt);
  border-radius: 99px;
  overflow: hidden;
}
.mfr-progress-bar #mfr-progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 99px;
  transition: width 0.3s ease;
}

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

.draw-header h2 { margin: 0 0 0.25rem; }

.tf-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.3rem 0.7rem;
  border-radius: 99px;
  white-space: nowrap;
}

.tf-badge.loading { background: var(--surface-alt); color: var(--muted); }
.tf-badge.ready { background: #d1fae5; color: #065f46; }
.tf-badge.error { background: #fee2e2; color: #991b1b; }

@media (prefers-color-scheme: dark) {
  .tf-badge.ready { background: #064e3b; color: #6ee7b7; }
  .tf-badge.error { background: #7f1d1d; color: #fca5a5; }
}

/* Recognition card — equation is the primary content; LaTeX is secondary. */
.recog-rendered-large {
  min-height: 80px;
  padding: 1rem 0.5rem;
  font-size: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.recog-actions-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.recog-source {
  margin-left: auto;
  font-size: 0.85rem;
}
.recog-source summary {
  cursor: pointer;
  color: var(--muted);
  user-select: none;
  padding: 0.3rem 0.5rem;
  border-radius: 4px;
}
.recog-source summary:hover { background: var(--surface-alt); color: var(--text); }
.recog-source[open] summary { color: var(--text); }
.recog-source pre {
  margin-top: 0.5rem;
  max-width: 100%;
}

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

.alt-chip {
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 99px;
  background: var(--surface-alt);
  font-family: "SF Mono", Menlo, monospace;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s;
}

.alt-chip:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.train-summary {
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0;
}

.train-summary::-webkit-details-marker { display: none; }

.train-summary::before {
  content: "▶";
  font-size: 0.7rem;
  color: var(--muted);
  transition: transform 0.2s;
}

details[open] .train-summary::before { transform: rotate(90deg); }

.badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.2rem 0.5rem;
  border-radius: 99px;
  background: #ede9fe;
  color: var(--primary);
}

@media (prefers-color-scheme: dark) {
  .badge { background: #2e1065; color: #c4b5fd; }
}

.train-body {
  padding-top: 1rem;
}

.train-controls {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: flex-end;
  margin: 0.75rem 0;
}

.text-input {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  color: var(--text);
  font-size: 0.95rem;
  margin-top: 0.25rem;
}

.text-input.short { width: 70px; }

.symbol-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.sym-tile {
  width: 48px;
  height: 48px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  font-weight: 600;
  cursor: default;
  position: relative;
}

.sym-tile.user { border-color: var(--primary); }

/* ── Train-picker ────────────────────────────────────────────────────────── */

.train-section-title {
  margin: 0.85rem 0 0.4rem;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
}

.train-categories {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-bottom: 0.5rem;
}
.train-cat {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.65rem;
  font-size: 0.82rem;
  cursor: pointer;
  color: var(--muted);
  font-family: inherit;
}
.train-cat:hover, .train-cat.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.train-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.25rem 0;
  min-height: 2.6rem;
}
.train-tile {
  min-width: 2.4rem;
  height: 2.4rem;
  padding: 0 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  transition: background 0.12s, border-color 0.12s;
}
.train-tile:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.train-tile.guess {
  /* AI-suggested tiles get a subtle accent so users notice them first */
  border-color: var(--primary);
  background: var(--surface-alt);
}
.train-tile .katex { font-size: 0.92em; }

.train-advanced {
  margin-top: 0.75rem;
  padding-top: 0.5rem;
  border-top: 1px dashed var(--border);
}
.train-advanced summary {
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
  margin-bottom: 0.5rem;
}

.sym-tile .sym-label {
  position: absolute;
  bottom: 2px;
  right: 3px;
  font-size: 0.5rem;
  color: var(--muted);
}

/* ── Scientific Calculator ─────────────────────────────────────────────────── */

.calc-layout {
  display: flex;
  justify-content: center;
}

.calc-card {
  width: 100%;
  max-width: 480px;
}

.calc-display {
  background: var(--surface-alt);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
  text-align: right;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 0.25rem;
}

.calc-expr {
  font-size: 0.9rem;
  color: var(--muted);
  min-height: 1.2em;
  word-break: break-all;
}

.calc-result {
  font-size: 2rem;
  font-weight: 600;
  word-break: break-all;
}

.calc-angle-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.75rem;
}

.calc-mode {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.6rem;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
}

.calc-mode.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.calc-mem-ind {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary);
  margin-left: auto;
}

.calc-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.4rem;
}

.calc-btn {
  padding: 0.7rem 0.25rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s;
  text-align: center;
}

.calc-btn:hover { background: var(--surface-alt); }

.calc-btn.fn {
  background: var(--surface-alt);
  font-size: 0.82rem;
}

.calc-btn.num { background: var(--surface); font-size: 1.1rem; font-weight: 600; }

.calc-btn.op {
  background: #ede9fe;
  color: var(--primary);
  border-color: #c4b5fd;
  font-size: 1.1rem;
}

@media (prefers-color-scheme: dark) {
  .calc-btn.op { background: #2e1065; border-color: #4c1d95; }
}

.calc-btn.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  font-size: 1.2rem;
}

.calc-btn.primary:hover { background: var(--primary-hover); }

.calc-btn.danger {
  background: #fee2e2;
  color: #b91c1c;
  border-color: #fca5a5;
}

@media (prefers-color-scheme: dark) {
  .calc-btn.danger { background: #7f1d1d; color: #fca5a5; border-color: #991b1b; }
}

.calc-btn.zero { grid-column: span 2; }
.calc-btn.equals { grid-column: span 4; }

.calc-footer-row {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
}

/* ── Graphing Calculator ────────────────────────────────────────────────────── */

.graph-header {
  margin-bottom: 1rem;
}

.graph-header h2 { margin: 0 0 0.75rem; }

.graph-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.graph-range-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.graph-mode-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.active-toggle {
  background: var(--primary) !important;
  color: white !important;
  border-color: var(--primary) !important;
}

.graph-add-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.5rem;
}

.graph-coords {
  font-family: "SF Mono", Menlo, Monaco, Consolas, monospace;
  font-size: 0.8rem;
  color: var(--muted);
  text-align: right;
  padding: 0.25rem 0;
}

.graph-exprs {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.graph-expr-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.graph-swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}

.graph-fn-input { flex: 1; margin-top: 0; }

.graph-del { padding: 0.4rem 0.6rem; }

.graph-plot {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  margin: 1rem 0;
  background: white;
}

.graph-plot svg { display: block; }

/* ── Chemistry panel ──────────────────────────────────────────────────────── */

.chem-preview {
  min-height: 3.5rem;
  padding: 1rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 0.75rem 0 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  overflow-x: auto;
}

.chem-chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin: 0.75rem 0;
}

.chem-group-lbl {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0 0.25rem;
}

.chem-chip {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.6rem;
  font-size: 0.88rem;
  cursor: pointer;
  color: var(--text);
  transition: background 0.15s, border-color 0.15s;
}

.chem-chip:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

/* ── Physics palette ──────────────────────────────────────────────────────── */

.physics-palette {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  margin: 0.5rem 0;
}

.palette-cats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-bottom: 0.75rem;
}

.palette-cat {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.65rem;
  font-size: 0.8rem;
  cursor: pointer;
  color: var(--muted);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.palette-cat:hover,
.palette-cat.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.palette-symbols {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.palette-sym {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  min-width: 2.4rem;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  line-height: 1;
}

.palette-sym:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.palette-sym .katex { font-size: 1em; }

@media (max-width: 640px) {
  .app-header { padding: 0.75rem 1rem; }
  main { padding: 1rem; }
  .editor-card { padding: 1rem; }
  math-field { font-size: 1.25rem; }
  .btn.large { padding: 0.75rem 1.5rem; font-size: 1rem; }
  .calc-btn { padding: 0.6rem 0.2rem; font-size: 0.85rem; }
  .calc-btn.fn { font-size: 0.72rem; }
}

/* ── Formula library modal ────────────────────────────────────────────────── */

.formula-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.formula-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
}

.formula-dialog {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.3);
  width: min(960px, 100%);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.formula-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.formula-header h2 {
  margin: 0;
  font-size: 1.15rem;
  white-space: nowrap;
}

.formula-header .text-input {
  flex: 1;
  margin-top: 0;
}

.formula-body {
  display: flex;
  flex: 1;
  min-height: 0;
}

.formula-cats {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 1rem 0.75rem;
  border-right: 1px solid var(--border);
  background: var(--surface-alt);
  min-width: 160px;
  overflow-y: auto;
}

.formula-cat {
  text-align: left;
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0.4rem 0.75rem;
  font-size: 0.88rem;
  cursor: pointer;
  color: var(--text);
}

.formula-cat:hover { background: var(--surface); }
.formula-cat.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.formula-list {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
}

.formula-cat-heading {
  margin: 0 0 0.5rem;
  padding-top: 0.5rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.formula-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.formula-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.85rem;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-family: inherit;
  color: var(--text);
  transition: border-color 0.12s, box-shadow 0.12s;
  min-height: 90px;
}

.formula-card:hover {
  border-color: var(--primary);
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.12);
}

.formula-card-name {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.formula-card-preview {
  flex: 1;
  display: flex;
  align-items: center;
  font-size: 0.95rem;
  overflow-x: auto;
}

.formula-card-preview .katex { font-size: 0.92em; }

.formula-card-notes {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.formula-empty {
  padding: 1.5rem;
  text-align: center;
}

@media (max-width: 700px) {
  .formula-body { flex-direction: column; }
  .formula-cats {
    flex-direction: row;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    min-width: 0;
  }
  .formula-cat { white-space: nowrap; }
}

/* ── Autocomplete popup ──────────────────────────────────────────────────── */

.autocomplete-popup {
  position: absolute;
  z-index: 200;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.12);
  padding: 0.25rem;
  display: flex;
  flex-direction: column;
  max-height: 320px;
  overflow-y: auto;
  min-width: 240px;
  font-size: 0.92rem;
}

.autocomplete-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.4rem 0.6rem;
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
  border-radius: 6px;
  font-family: inherit;
  font-size: inherit;
  color: var(--text);
}

.autocomplete-row:hover, .autocomplete-row.active {
  background: var(--surface-alt);
}
.autocomplete-row.active {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

.autocomplete-key {
  font-family: "SF Mono", Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  color: var(--muted);
  min-width: 70px;
  flex-shrink: 0;
}

.autocomplete-display {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.autocomplete-display .katex {
  font-size: 1em;
}

/* ── Drawing — correction popup ────────────────────────────────────────── */

.correction-popup {
  position: fixed;
  z-index: 300;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.15);
  padding: 0.25rem;
  display: flex;
  flex-direction: column;
  min-width: 160px;
}

.correction-row {
  background: none;
  border: none;
  padding: 0.4rem 0.65rem;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.88rem;
  color: var(--text);
  border-radius: 4px;
}

.correction-row:hover { background: var(--surface-alt); }

/* ── Hamburger drawer ────────────────────────────────────────────────────── */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  z-index: 800;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }

.nav-drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: 4px 0 16px rgba(15, 23, 42, 0.08);
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.22s ease;
  z-index: 850;
}
.nav-drawer.open { transform: translateX(0); }

.nav-drawer-header {
  padding: 1.25rem 1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
.nav-drawer-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
}

.nav-list {
  flex: 1;
  padding: 0.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.nav-drawer-footer {
  border-top: 1px solid var(--border);
  padding: 0.5rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.85rem;
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.95rem;
  color: var(--text);
  text-align: left;
  width: 100%;
}
.nav-item:hover { background: var(--surface-alt); }
.nav-item.active {
  background: var(--primary);
  color: white;
}
.nav-item-icon {
  font-size: 1.05rem;
  width: 1.5em;
  text-align: center;
}

/* ── Settings modal ──────────────────────────────────────────────────────── */

.settings-modal {
  position: fixed;
  inset: 0;
  z-index: 1100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.settings-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.5); }
.settings-dialog {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.3);
  width: min(560px, 100%);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.settings-header h2 { margin: 0; font-size: 1.15rem; }
.settings-body {
  padding: 1rem 1.25rem;
  overflow-y: auto;
}
.settings-group { margin-bottom: 1.25rem; }
.settings-group h3 {
  margin: 0 0 0.4rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.settings-help {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--border);
}
.settings-row:last-child { border-bottom: none; }
.settings-row select, .settings-row input[type="number"], .settings-row input[type="range"] {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem 0.5rem;
  color: var(--text);
  font-family: inherit;
}
.settings-footer {
  display: flex;
  justify-content: flex-end;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
}
