* { box-sizing: border-box; }

:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #6366f1;
  --accent-2: #0ea5e9;
  --code-bg: #0f172a;
  --code-text: #e2e8f0;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15,23,42,.06), 0 8px 24px rgba(15,23,42,.06);
}

[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #111a2e;
  --surface-2: #0f172a;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --border: #1e293b;
  --code-bg: #060b16;
  --code-text: #e2e8f0;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
}

html, body { margin: 0; padding: 0; }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
  padding-top: calc(12px + env(safe-area-inset-top));
}
.brand {
  display: inline-flex; align-items: center; gap: 10px;
  color: var(--text); text-decoration: none; font-weight: 700; font-size: 1.05rem;
  letter-spacing: -0.01em;
}
.logo { width: 32px; height: 32px; border-radius: 8px; display: block; }
.header-actions { display: flex; gap: 8px; align-items: center; }

/* ---------- Buttons ---------- */
.btn {
  font: inherit; font-weight: 600; font-size: 0.92rem;
  padding: 9px 14px; border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text);
  cursor: pointer; transition: transform .06s, background .15s, border-color .15s, opacity .15s;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; border-color: transparent;
  box-shadow: 0 4px 14px rgba(99,102,241,.35);
}
.btn.ghost { background: transparent; }

/* ---------- Tabs ---------- */
.tabs {
  display: flex; gap: 6px; overflow-x: auto;
  padding: 10px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 57px; z-index: 10;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  font: inherit; font-size: 0.9rem; font-weight: 600;
  padding: 8px 14px; border-radius: 999px;
  border: 1px solid transparent;
  background: transparent; color: var(--muted);
  cursor: pointer; white-space: nowrap;
  transition: background .15s, color .15s, border-color .15s;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
}

/* ---------- Main ---------- */
.app-main {
  flex: 1;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}
.panel { display: none; animation: fade .2s ease; }
.panel.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

h2 {
  font-size: 1.25rem; margin: 0 0 6px;
  letter-spacing: -0.01em;
}
.hint {
  color: var(--muted); font-size: 0.92rem; margin: 0 0 16px;
}
.hint code {
  background: var(--surface-2);
  padding: 1px 6px; border-radius: 6px;
  font-size: 0.85em; border: 1px solid var(--border);
}

/* ---------- Inputs ---------- */
input[type="text"], input[type="number"], select, textarea {
  font: inherit;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  width: 100%;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}
textarea { resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9rem; line-height: 1.55; }
label {
  display: block; font-size: 0.82rem; font-weight: 600;
  color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: .04em;
}
label.inline { display: inline-flex; align-items: center; gap: 6px; text-transform: none; letter-spacing: 0; font-size: 0.85rem; }
label.inline input { width: 80px; }

.row {
  display: flex; gap: 10px; align-items: center;
  flex-wrap: wrap; margin: 12px 0;
}
.row > input[type="text"] { flex: 1 1 240px; }
.arrow { color: var(--muted); font-weight: 700; }

/* ---------- Output ---------- */
.output {
  margin-top: 14px;
  padding: 16px 18px;
  min-height: 56px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: 0.95rem;
}
.output:empty { display: none; }
.output.mono, pre.output {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88rem;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--code-text);
  background: var(--code-bg);
  border-color: transparent;
}
.output.mono:empty, pre.output:empty { display: none; }
.result-line {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
  font-size: 1.05rem;
  color: var(--muted);
}
.result-line strong { color: var(--text); font-size: 1.15em; }
.error { color: #ef4444; font-weight: 600; }

/* ---------- Examples chips ---------- */
.examples {
  display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px;
}
.examples button {
  font: inherit; font-size: 0.82rem;
  padding: 6px 12px; border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface); color: var(--muted);
  cursor: pointer; transition: all .15s;
}
.examples button:hover { color: var(--accent); border-color: var(--accent); }

/* ---------- Layout helpers ---------- */
.grid-2 {
  display: grid; gap: 16px;
  grid-template-columns: 1fr;
}
@media (min-width: 760px) {
  .grid-2 { grid-template-columns: 1fr 1fr; }
}

/* ---------- Grapher ---------- */
.canvas-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 360px;
}
#graf-canvas { display: block; width: 100%; height: 360px; }

/* ---------- Editor ---------- */
textarea.editor {
  background: var(--code-bg);
  color: var(--code-text);
  border-color: transparent;
  font-size: 0.9rem;
  line-height: 1.6;
  tab-size: 2;
}

/* ---------- Stats grid ---------- */
.stats-grid {
  display: grid; gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  margin-top: 16px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex; flex-direction: column; gap: 4px;
}
.stat span { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.stat strong { font-size: 1.1rem; font-variant-numeric: tabular-nums; }

/* ---------- Footer ---------- */
.app-footer {
  display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px;
  padding: 16px 20px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.82rem;
}

/* ---------- KaTeX tweak ---------- */
.katex-display { margin: 0 !important; }