/* Equation Converter: calm, neutral surfaces, one accent colour. */

:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e2e5ea;
  --text: #1c2330;
  --muted: #667084;
  --accent: #2a6fdb;
  --accent-ink: #ffffff;
  --accent-soft: #e9f0fc;
  --danger: #c4372f;
  --grid: #e8ebef;
  --axis: #8a93a3;
  --shadow: 0 1px 2px rgb(20 30 50 / 0.05), 0 6px 20px -8px rgb(20 30 50 / 0.1);

  --tk-k: #8a3ea6;
  --tk-t: #0a7a73;
  --tk-n: #b25400;
  --tk-s: #2f7d32;
  --tk-c: #8a93a3;
  --tk-f: #2a6fdb;

  --ui-font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono-font:
    ui-monospace, 'SF Mono', 'Cascadia Code', Menlo, Consolas, 'Liberation Mono', monospace;
  --math-font:
    'STIX Two Math', 'Cambria Math', 'Latin Modern Math', 'Times New Roman', 'Liberation Serif',
    Georgia, serif;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #1a1e25;
    --surface-2: #161a20;
    --border: #2a303a;
    --text: #e7eaf0;
    --muted: #97a0af;
    --accent: #6fa3ff;
    --accent-ink: #0b1220;
    --accent-soft: #1f2b44;
    --danger: #ff8d85;
    --grid: #252b34;
    --axis: #778090;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 24px -10px rgb(0 0 0 / 0.5);

    --tk-k: #c99bff;
    --tk-t: #6fd5c9;
    --tk-n: #f0b072;
    --tk-s: #94d696;
    --tk-c: #7b8494;
    --tk-f: #82b4ff;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: #05070f;
  color: var(--text);
  font: 15px/1.5 var(--ui-font);
  -webkit-font-smoothing: antialiased;
}

/* ------------------------------------------------------------- space scene */

/* Fixed layers behind the app: nebula glow, then two star fields that twinkle
   out of phase. Pure CSS, no images. */
body::before,
body::after {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
}

body::before {
  background:
    radial-gradient(ellipse 60% 45% at 12% 8%, rgb(88 70 190 / 0.35), transparent 70%),
    radial-gradient(ellipse 50% 40% at 92% 30%, rgb(40 110 200 / 0.28), transparent 70%),
    radial-gradient(ellipse 70% 50% at 70% 100%, rgb(170 60 140 / 0.22), transparent 70%),
    radial-gradient(ellipse at 50% 50%, #0b1024 0%, #05070f 100%);
}

body::after {
  background-image:
    radial-gradient(1px 1px at 20px 30px, #fff, transparent),
    radial-gradient(1px 1px at 120px 80px, rgb(255 255 255 / 0.8), transparent),
    radial-gradient(1.5px 1.5px at 60px 160px, #fff, transparent),
    radial-gradient(1px 1px at 170px 140px, rgb(200 220 255 / 0.9), transparent),
    radial-gradient(1px 1px at 90px 40px, rgb(255 255 255 / 0.6), transparent),
    radial-gradient(2px 2px at 230px 210px, rgb(255 240 220 / 0.9), transparent),
    radial-gradient(1px 1px at 300px 60px, #fff, transparent),
    radial-gradient(1.5px 1.5px at 340px 290px, rgb(190 210 255 / 0.85), transparent),
    radial-gradient(1px 1px at 260px 330px, rgb(255 255 255 / 0.7), transparent);
  background-size: 380px 380px;
  animation: twinkle 6s ease-in-out infinite alternate;
}

.app::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-image:
    radial-gradient(1px 1px at 40px 200px, #fff, transparent),
    radial-gradient(1px 1px at 410px 90px, rgb(255 255 255 / 0.75), transparent),
    radial-gradient(1.5px 1.5px at 520px 410px, rgb(210 225 255 / 0.9), transparent),
    radial-gradient(1px 1px at 150px 470px, #fff, transparent),
    radial-gradient(2px 2px at 600px 150px, rgb(255 235 215 / 0.85), transparent),
    radial-gradient(1px 1px at 330px 560px, rgb(255 255 255 / 0.6), transparent);
  background-size: 640px 640px;
  animation: twinkle 9s ease-in-out -4s infinite alternate;
}

@keyframes twinkle {
  from {
    opacity: 1;
  }
  to {
    opacity: 0.45;
  }
}

@media (prefers-reduced-motion: reduce) {
  body::after,
  .app::before {
    animation: none;
  }
}

/* The header sits directly on the dark scene in both themes. */
.top .brand p {
  color: #aab3c5;
}

button,
input {
  font: inherit;
  color: inherit;
}

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

/* ------------------------------------------------------------------ layout */

.app {
  max-width: 1320px;
  margin: 0 auto;
  padding: 28px 28px 56px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  grid-template-areas:
    'top  top'
    'main side';
  gap: 24px;
  align-items: start;
}

.top {
  grid-area: top;
  color: #eef1f8;
}
.main {
  grid-area: main;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  min-width: 0;
  container-type: inline-size;
}
.side {
  grid-area: side;
  position: sticky;
  top: 20px;
  padding: 18px 12px 12px 18px;
  max-height: calc(100vh - 40px);
  display: flex;
  flex-direction: column;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}
.brand h1 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}
.logo {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 11px;
  background: var(--accent);
  color: var(--accent-ink);
  font: italic 600 24px/1 var(--math-font);
  padding-bottom: 3px;
}

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

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 40px;
}
.card-title {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
}
.card-note {
  color: var(--muted);
  font-size: 0.8rem;
}
.eyebrow {
  display: block;
  font-size: 0.78rem;
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
}

.workspace {
  display: grid;
  gap: 20px;
  grid-template-columns: minmax(0, 1fr);
}
@container (min-width: 820px) {
  .workspace {
    grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
    align-items: start;
  }
}

/* ------------------------------------------------------------------- input */

.entry {
  padding: 18px 22px 12px;
}

.field {
  position: relative;
  margin-top: 8px;
  border-bottom: 2px solid var(--border);
  transition: border-color 0.15s;
}
.field:focus-within {
  border-color: var(--accent);
}
.field:has(input[aria-invalid='true']) {
  border-color: var(--danger);
}
.field-text {
  display: block;
  width: 100%;
  margin: 0;
  padding: 8px 0 10px;
  border: 0;
  font: 400 1.55rem/1.35 var(--mono-font);
  letter-spacing: 0;
  white-space: pre;
}
.field input {
  position: relative;
  background: transparent;
  outline: none;
}
.field input::placeholder {
  color: var(--muted);
  opacity: 0.55;
}
.mirror {
  position: absolute;
  inset: 0;
  overflow: hidden;
  color: transparent;
  pointer-events: none;
}
.mirror mark {
  color: transparent;
  background: color-mix(in srgb, var(--danger) 16%, transparent);
  text-decoration: underline wavy var(--danger);
  text-decoration-thickness: 1.5px;
  text-underline-offset: 5px;
  text-decoration-skip-ink: none;
  border-radius: 3px;
}

.preview {
  display: flex;
  align-items: center;
  min-height: 132px;
  padding: 14px 4px;
  overflow-x: auto;
  overflow-y: hidden;
  font-size: 2.05rem;
  transition: opacity 0.15s;
}
.preview .hint {
  font: 0.95rem var(--ui-font);
  color: var(--muted);
}
.stale {
  opacity: 0.42;
}

.status {
  min-height: 2.9em;
  padding: 8px 0 6px;
  border-top: 1px solid var(--border);
  font-size: 0.86rem;
  color: var(--muted);
}
.status .err {
  color: var(--danger);
}
.status .err-pos {
  color: var(--muted);
  margin-left: 6px;
}
.status .warn {
  display: block;
}

/* ----------------------------------------------------------------- buttons */

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 9px;
  padding: 5px 14px;
  font-size: 0.85rem;
  font-weight: 550;
  cursor: pointer;
  transition:
    background 0.12s,
    border-color 0.12s,
    color 0.12s;
}
.btn:hover:not(:disabled) {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
}
.btn:disabled {
  opacity: 0.45;
  cursor: default;
}
.btn.done {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn-quiet {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  padding: 4px 10px;
}

/* -------------------------------------------------------------------- code */

.code-card {
  padding: 8px 8px 8px;
  min-width: 0;
}
.code-card .card-head {
  padding: 0 4px 0 6px;
}
.tabs {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
}
.tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 7px 12px;
  border-radius: 9px;
  font-size: 0.88rem;
  font-weight: 550;
  cursor: pointer;
}
.tab:hover {
  color: var(--text);
  background: var(--surface-2);
}
.tab[aria-selected='true'] {
  color: var(--accent);
  background: var(--accent-soft);
}
.code {
  margin: 6px 0 0;
  padding: 16px 18px;
  min-height: 210px;
  max-height: 520px;
  overflow: auto;
  background: var(--surface-2);
  border-radius: 10px;
  font: 0.86rem/1.6 var(--mono-font);
  tab-size: 4;
  white-space: pre;
}
.code .empty {
  color: var(--muted);
  font-family: var(--ui-font);
  font-size: 0.9rem;
}
.code-err {
  color: var(--danger);
  white-space: pre-wrap;
  font-family: var(--ui-font);
}
.tk-k {
  color: var(--tk-k);
}
.tk-t {
  color: var(--tk-t);
}
.tk-n {
  color: var(--tk-n);
}
.tk-s {
  color: var(--tk-s);
}
.tk-c {
  color: var(--tk-c);
  font-style: italic;
}
.tk-f {
  color: var(--tk-f);
}

/* --------------------------------------------------------------------- viz */

.viz-card {
  padding: 8px 16px 16px;
  min-width: 0;
}
.plot {
  display: block;
  width: 100%;
  height: 280px;
  border-radius: 8px;
  touch-action: pan-y;
}
.readout {
  min-height: 1.5em;
  margin-top: 4px;
  font: 0.8rem var(--mono-font);
  color: var(--muted);
  white-space: pre;
  overflow: hidden;
}
.zoom {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
  font-size: 0.82rem;
  color: var(--muted);
}
.zoom input {
  flex: 1;
  accent-color: var(--accent);
}
.eval {
  display: grid;
  gap: 10px;
  padding-top: 4px;
}
.ev-row {
  display: grid;
  grid-template-columns: 5.5em 1fr;
  align-items: center;
  gap: 10px;
}
.ev-name {
  font-size: 1.15rem;
  text-align: right;
  color: var(--muted);
}
.ev-row input {
  min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2);
  font: 0.95rem var(--mono-font);
}
.ev-row input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.ev-row input[aria-invalid='true'] {
  border-color: var(--danger);
}
.ev-result {
  margin-top: 6px;
  padding: 14px 16px;
  border-radius: 10px;
  background: var(--accent-soft);
  font-size: 1.25rem;
  min-height: 3.6rem;
  overflow-x: auto;
}
.ev-result .ev-msg {
  font: 0.88rem var(--ui-font);
  color: var(--muted);
}
.ev-result b {
  font-weight: 600;
  color: var(--accent);
}

/* ----------------------------------------------------------------- history */

.history {
  list-style: none;
  margin: 6px 0 0;
  padding: 0 6px 0 0;
  overflow-y: auto;
  display: grid;
  gap: 6px;
}
.history .empty {
  padding: 8px 4px;
  color: var(--muted);
  font-size: 0.85rem;
}
.hi {
  position: relative;
}
.hi-pick {
  appearance: none;
  display: block;
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 9px 34px 8px 12px;
  cursor: pointer;
  overflow: hidden;
}
.hi-pick:hover {
  border-color: var(--border);
}
.hi.current .hi-pick {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
}
.hi-math {
  display: block;
  font-size: 1.02rem;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 0;
  scrollbar-width: none;
}
.hi-raw {
  display: block;
  margin-top: 2px;
  font: 0.72rem/1.4 var(--mono-font);
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hi-del {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0.55;
}
.hi:hover .hi-del,
.hi-del:focus-visible {
  opacity: 1;
}
.hi-del:hover {
  background: var(--border);
  color: var(--text);
}

/* -------------------------------------------------------------------- help */

.help summary {
  cursor: pointer;
  padding: 14px 22px;
  font-weight: 600;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.help summary::-webkit-details-marker {
  display: none;
}
.help summary::before {
  content: '';
  width: 7px;
  height: 7px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform 0.15s;
}
.help[open] summary::before {
  transform: rotate(45deg);
}
.help-body {
  padding: 0 22px 20px;
}
.help-lead {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.88rem;
}
.examples {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 10px;
}
.ex {
  appearance: none;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  min-width: 0;
}
.ex:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.ex-math {
  display: block;
  font-size: 1.1rem;
  overflow-x: auto;
  overflow-y: hidden;
  min-height: 2.3em;
  padding: 2px 0;
  scrollbar-width: none;
}
.ex-raw {
  display: block;
  margin-top: 4px;
  font: 0.74rem var(--mono-font);
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tips {
  margin: 18px 0 0;
  padding-left: 18px;
  color: var(--muted);
  font-size: 0.88rem;
  display: grid;
  gap: 4px;
}
.tips code {
  font: 0.82rem var(--mono-font);
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
}

/* ---------------------------------------------------------------- notebook */

.notebook {
  padding: 8px 22px 22px;
  min-width: 0;
}
.nb-lead {
  margin: 2px 0 14px;
  color: var(--muted);
  font-size: 0.88rem;
}
.nb-tools {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.nb-search,
.nb-save-name {
  min-width: 0;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2);
  font-size: 0.92rem;
}
.nb-search {
  flex: 1 1 240px;
}
.nb-search:focus,
.nb-save-name:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.nb-save {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 10px;
}
.nb-save[hidden] {
  display: none;
}
.nb-save-label {
  display: flex;
  gap: 8px;
  align-items: center;
  flex: 1 1 260px;
  font-size: 0.85rem;
  color: var(--muted);
}
.nb-save-name {
  flex: 1;
  color: var(--text);
}
.nb-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 14px 0 4px;
}
.nb-chip {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  border-radius: 999px;
  padding: 3px 12px;
  font-size: 0.8rem;
  font-weight: 550;
  cursor: pointer;
}
.nb-chip:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  color: var(--text);
}
.nb-chip[aria-pressed='true'] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.nb-list {
  max-height: 720px;
  overflow-y: auto;
  margin-top: 6px;
  padding-right: 4px;
}
.nb-group-title {
  position: sticky;
  top: 0;
  z-index: 1;
  margin: 0;
  padding: 12px 0 8px;
  background: var(--surface);
  font-size: 0.78rem;
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
}
.nb-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 10px;
}
.nb-item {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
}
.nb-item:hover {
  border-color: var(--accent);
}
.nb-item.current {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
}
.nb-pick {
  appearance: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  width: 100%;
  flex: 1;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 10px;
  padding: 10px 12px 6px;
  cursor: pointer;
  min-width: 0;
}
.nb-mine .nb-pick {
  padding-right: 34px;
  padding-bottom: 10px;
}
.nb-name {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
}
.nb-math {
  display: block;
  margin: 4px 0 2px;
  font-size: 1.02rem;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 0;
  scrollbar-width: none;
}
.nb-about,
.nb-example {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
}
.nb-example {
  margin-top: 2px;
  font-style: italic;
}
.nb-raw {
  display: block;
  font: 0.72rem/1.4 var(--mono-font);
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nb-vars {
  padding: 0 12px 8px;
  font-size: 0.8rem;
}
.nb-vars summary {
  cursor: pointer;
  color: var(--accent);
  font-weight: 550;
  width: fit-content;
}
.nb-vars dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 10px;
  margin: 6px 0 0;
}
.nb-vars dt {
  font-size: 0.95rem;
  text-align: right;
}
.nb-vars dd {
  margin: 0;
  color: var(--muted);
  align-self: center;
}
.nb-del {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0.55;
}
.nb-item:hover .nb-del,
.nb-del:focus-visible {
  opacity: 1;
}
.nb-del:hover {
  background: var(--border);
  color: var(--text);
}
.nb-empty {
  margin: 14px 0 4px;
  color: var(--muted);
  font-size: 0.88rem;
}

/* -------------------------------------------------------------- responsive */

@media (max-width: 980px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      'top'
      'main'
      'side';
    padding: 20px 16px 40px;
  }
  .side {
    position: static;
    max-height: none;
  }
  .history {
    max-height: 420px;
  }
}
@media (max-width: 520px) {
  .field-text {
    font-size: 1.2rem;
  }
  .preview {
    font-size: 1.6rem;
    min-height: 110px;
  }
  .entry {
    padding: 16px 16px 10px;
  }
  .brand p {
    display: none;
  }
}

/* ------------------------------------------------------------- typesetting */

.m-math {
  font-family: var(--math-font);
  line-height: 1.25;
  white-space: nowrap;
}
.m-v {
  font-style: italic;
}
.m-c,
.m-n,
.m-id,
.m-fn {
  font-style: normal;
}
.m-id {
  letter-spacing: 0.01em;
}
.m-fn {
  margin-right: 0.1em;
}
.m-op {
  margin: 0 0.24em;
}
.m-dot {
  margin: 0 0.18em;
}
.m-eq-sign {
  margin: 0 0.34em;
}
.m-eq {
  margin: 0 0.15em;
}
.m-word {
  font-style: normal;
  margin: 0 0.5em;
}
.m-minus {
  margin-right: 0.08em;
}
.m-comma {
  margin-right: 0;
}
.m-thin {
  display: inline-block;
  width: 0.14em;
}
.m-sup,
.m-sub {
  font-size: 0.7em;
  line-height: 0;
}
.m-sup {
  margin-left: 0.04em;
}
.m-sub {
  vertical-align: -0.38em;
  margin-left: 0.03em;
}

/* stacked fraction: two equal rows, so the bar sits on the math axis */
.m-frac {
  display: inline-grid;
  grid-template-rows: 1fr 1fr;
  vertical-align: middle;
  text-align: center;
  margin: 0 0.1em;
}
.m-num {
  display: block;
  align-self: end;
  padding: 0 0.18em 0.07em;
  border-bottom: 0.065em solid currentColor;
}
.m-den {
  display: block;
  align-self: start;
  padding: 0.07em 0.18em 0;
}

/* stretchy brackets */
.m-big {
  display: inline-flex;
  align-items: stretch;
  vertical-align: middle;
}
.m-in {
  display: block;
}
.m-lp,
.m-rp,
.m-lb,
.m-rb {
  flex: none;
  border: 0 solid currentColor;
}
.m-lp,
.m-rp {
  width: 0.3em;
}
.m-lp {
  border-left-width: 0.075em;
  border-radius: 90% 0 0 90% / 50% 0 0 50%;
  margin-right: 0.1em;
}
.m-rp {
  border-right-width: 0.075em;
  border-radius: 0 90% 90% 0 / 0 50% 50% 0;
  margin-left: 0.1em;
}
.m-lb {
  border-left-width: 0.07em;
  margin-right: 0.14em;
}
.m-rb {
  border-right-width: 0.07em;
  margin-left: 0.14em;
}

/* radical */
.m-sqrt {
  display: inline-flex;
  align-items: stretch;
  vertical-align: middle;
}
.m-rad {
  position: relative;
  flex: none;
  width: 0.82em;
}
.m-rad svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.m-rad path,
.m-brace path {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.3;
  vector-effect: non-scaling-stroke;
  stroke-linejoin: miter;
  stroke-linecap: butt;
}
.m-rc {
  display: block;
  border-top: 0.065em solid currentColor;
  padding: 0.07em 0.12em 0 0.1em;
  margin-top: 0.03em;
}
.m-idx {
  align-self: flex-start;
  font-size: 0.6em;
  margin: 0.45em -0.85em 0 0.1em;
  position: relative;
  z-index: 1;
}

/* big operators */
.m-bigop {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: middle;
  margin: 0 0.08em;
}
.m-sym {
  font-size: 1.7em;
  line-height: 1.05;
}
.m-lim {
  font-size: 0.7em;
  line-height: 1.15;
  white-space: nowrap;
}
.m-sp {
  display: inline-block;
  width: 0.18em;
}

/* piecewise */
.m-pw {
  display: inline-flex;
  align-items: stretch;
  vertical-align: middle;
}
.m-brace {
  position: relative;
  flex: none;
  width: 0.6em;
  margin-right: 0.18em;
}
.m-brace svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.m-rows {
  display: inline-grid;
  grid-template-columns: auto auto;
  column-gap: 1.3em;
  row-gap: 0.3em;
  align-items: center;
  justify-items: start;
  padding: 0.12em 0;
}
.m-if {
  font-style: normal;
  margin-right: 0.45em;
  color: var(--muted);
}
.m-err {
  color: var(--danger);
}

/* sub/superscripts use line-height 0; stacked structures inside them need their own */
.m-frac,
.m-big,
.m-sqrt,
.m-bigop,
.m-pw {
  line-height: 1.25;
}
.m-fn + .m-sup {
  margin-left: -0.08em;
  margin-right: 0.08em;
}

/* ------------------------------------------------------- entry: mode, palette */

.entry-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 30px;
}
.seg {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
}
.seg button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 3px 12px;
  border-radius: 8px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}
.seg button:hover {
  color: var(--text);
}
.seg button[aria-checked='true'] {
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 1px 2px rgb(20 30 50 / 0.12);
}
.notice {
  margin: 8px 0 0;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--accent-soft);
  font-size: 0.84rem;
  color: var(--text);
}
.notice[hidden] {
  display: none;
}

.palette {
  margin: 6px -4px 0;
}
.pal {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.pal-group {
  display: flex;
  flex: none;
  align-items: center;
  gap: 0;
  padding-right: 6px;
  border-right: 1px solid var(--border);
}
.pal-group:last-child {
  border-right: 0;
  padding-right: 0;
}
.pal-btn {
  appearance: none;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-width: 28px;
  height: 34px;
  padding: 0 4px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  color: var(--text);
  font: 1.02rem/1 var(--math-font);
  white-space: nowrap;
  cursor: pointer;
  transition:
    background 0.1s,
    border-color 0.1s;
}
.pal-btn:hover {
  background: var(--surface-2);
  border-color: var(--border);
}
.pal-btn:active,
.pal-btn[aria-expanded='true'] {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
}
.pal-more {
  font-size: 0.86rem;
  font-family: var(--ui-font);
  color: var(--muted);
  gap: 2px;
}
.pal-more span:first-child {
  font-style: italic;
  font-family: var(--math-font);
  font-size: 0.95rem;
}
.pal-caret {
  font-size: 0.66rem;
}
.pal-btn .m-math,
.pal-btn .m-frac {
  line-height: 1;
}
.pal-btn .m-frac {
  font-size: 0.72em;
  margin: 0;
}
.pal-btn .m-num {
  padding: 0 0.12em 0.08em;
}
.pal-btn .m-den {
  padding: 0.08em 0.12em 0;
}
.pal-btn .m-fn {
  font-size: 0.95rem;
  margin: 0;
}
.pg-box {
  display: inline-block;
  width: 0.5em;
  height: 0.5em;
  border: 1px dashed var(--muted);
  border-radius: 2px;
  vertical-align: baseline;
}
.pg-sup,
.pg-sub,
.pg-idx {
  font-size: 0.6em;
  line-height: 1;
  font-style: italic;
}
.pg-sup {
  align-self: flex-start;
  margin-top: 0.1em;
}
.pg-sub {
  align-self: flex-end;
  margin-bottom: 0.1em;
}
.pg-idx {
  align-self: flex-start;
  margin: 0.1em -0.25em 0 0;
}
.pg-rt {
  display: inline-flex;
  align-items: center;
}
.pg-rt::before {
  content: '√';
  font-size: 1.15em;
}
.pg-rt > span {
  border-top: 1.5px solid currentColor;
  padding: 0.12em 0.1em 0;
  margin-left: -0.08em;
  line-height: 1;
}
.pg-bar,
.pg-p {
  font-size: 1.15em;
  font-weight: 300;
}
.pg-brace {
  font-size: 1.9em;
  font-weight: 200;
  line-height: 0.7;
  margin-right: 1px;
}
.pg-rows {
  display: inline-flex;
  flex-direction: column;
  gap: 2px;
}
.pg-rows .pg-box {
  width: 0.4em;
  height: 0.4em;
}
.pal-btn .pg-op {
  margin: 0;
}

.pal-pop {
  position: fixed;
  z-index: 60;
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), auto);
  gap: 2px;
  padding: 8px;
  max-width: calc(100vw - 16px);
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow:
    0 2px 4px rgb(20 30 50 / 0.08),
    0 14px 34px -10px rgb(20 30 50 / 0.3);
}
.pal-pop[hidden] {
  display: none;
}
.pal-pbtn {
  min-width: 38px;
  justify-content: center;
}

/* ------------------------------------------------------------ visual editor */

.field-visual {
  margin-top: 6px;
  background: var(--surface-2);
  border-radius: 10px 10px 0 0;
  padding: 0 10px;
}
.field-visual:has(.ed[aria-invalid='true']) {
  border-color: var(--danger);
}
.field-visual:has(.ed.ed-soft) {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
}
.field-textbox {
  margin-top: 6px;
}
.entry[data-mode='visual'] .preview,
.entry[data-mode='text'] .signature {
  display: none;
}

.signature {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 10px;
  min-height: 44px;
  padding: 8px 4px;
  font-size: 1.15rem;
  transition: opacity 0.15s;
}
.sig-lead {
  font: 0.84rem var(--ui-font);
  color: var(--muted);
}
.sig-badge {
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font: 600 0.76rem var(--ui-font);
  letter-spacing: 0.02em;
}

.ed {
  position: relative;
  font-family: var(--math-font);
  font-size: 1.9rem;
  line-height: 1.25;
  color: var(--text);
  cursor: text;
  -webkit-user-select: none;
  user-select: none;
}
.ed-scroll {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.3em 0.05em 0.34em;
  min-height: 2.4em;
  display: flex;
  align-items: center;
  scrollbar-width: thin;
}
.ed-surface {
  position: relative;
  flex: 1 0 auto;
  min-width: 100%;
  white-space: nowrap;
  padding: 0 0.1em;
}
.ed-content {
  display: inline-block;
  vertical-align: top;
}
.ed-input {
  position: absolute;
  left: 0;
  top: 0;
  width: 2px;
  height: 1.2em;
  margin: 0;
  padding: 0;
  border: 0;
  outline: none;
  resize: none;
  overflow: hidden;
  opacity: 0;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  font-size: 16px;
  pointer-events: none;
}
.ed-caret {
  position: absolute;
  display: none;
  width: 1.5px;
  border-radius: 1px;
  background: var(--text);
  pointer-events: none;
}
.ed-focused .ed-caret {
  display: block;
  animation: ed-blink 1.1s steps(1) infinite;
}
.ed-caret[hidden] {
  display: none !important;
}
@keyframes ed-blink {
  0%,
  55% {
    opacity: 1;
  }
  56%,
  100% {
    opacity: 0;
  }
}
.ed-sel {
  position: absolute;
  border-radius: 3px;
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent) 35%, transparent);
  pointer-events: none;
}
.ed-sel[hidden] {
  display: none;
}
.ed:not(.ed-focused) .ed-sel {
  opacity: 0.55;
}
.ed-hint {
  position: absolute;
  left: 0.5rem;
  top: 50%;
  display: none;
  transform: translateY(-50%);
  font: 0.95rem var(--ui-font);
  color: var(--muted);
  opacity: 0.7;
  pointer-events: none;
}
.ed-blank .ed-hint {
  display: block;
}
.ed-live {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.ed-block {
  display: inline-block;
}
.ed-root.ed-empty {
  min-width: 1px;
  height: 1.25em;
  vertical-align: top;
}
.ed-ph {
  display: inline-block;
  width: 0.55em;
  height: 0.8em;
  margin: 0 0.04em;
  vertical-align: -0.02em;
  border: 1.5px dashed color-mix(in srgb, var(--muted) 60%, transparent);
  border-radius: 3px;
  background: color-mix(in srgb, var(--muted) 7%, transparent);
}
.ed-focused .ed-ph {
  border-color: color-mix(in srgb, var(--accent) 55%, transparent);
}

.ed-a {
  font-style: normal;
}
.ed-k-var,
.ed-k-greek {
  font-style: italic;
}
.ed-k-greek.ed-up {
  font-style: normal;
}
.ed-k-op {
  margin: 0 0.24em;
}
.ed-k-op.ed-unary {
  margin: 0 0.06em 0 0;
}
.ed-k-fn {
  margin-right: 0.1em;
}
.ed-k-punct {
  margin-right: 0.28em;
}

.ed-sup,
.ed-sub {
  display: inline-block;
  font-size: 0.7em;
  line-height: 1.15;
}
.ed-sup {
  vertical-align: 0.75em;
  margin-left: 0.04em;
}
.ed-sub {
  vertical-align: -0.5em;
  margin-left: 0.03em;
}

.ed-frac {
  display: inline-grid;
  grid-template-rows: 1fr 1fr;
  vertical-align: middle;
  text-align: center;
  margin: 0 0.1em;
}
.ed-num {
  display: block;
  align-self: end;
  padding: 0 0.18em 0.07em;
  border-bottom: 0.065em solid currentColor;
}
.ed-den {
  display: block;
  align-self: start;
  padding: 0.07em 0.18em 0;
}
.ed-num > .ed-block,
.ed-den > .ed-block {
  display: block;
}

.ed-paren {
  position: relative;
  display: inline-block;
  margin-left: 0.04em;
  padding: 0 0.4em;
}
.ed-in {
  display: inline-block;
}
.ed-br {
  position: absolute;
  top: 0.1em;
  bottom: 0.1em;
  border: 0 solid currentColor;
}
.ed-lp {
  left: 0.06em;
  width: 0.3em;
  border-left-width: 0.075em;
  border-radius: 90% 0 0 90% / 50% 0 0 50%;
}
.ed-rp {
  right: 0.06em;
  width: 0.3em;
  border-right-width: 0.075em;
  border-radius: 0 90% 90% 0 / 0 50% 50% 0;
}
.ed-ps .ed-lp,
.ed-ps .ed-rp {
  width: 0.2em;
  border-radius: 0;
  border-top-width: 0.07em;
  border-bottom-width: 0.07em;
}
.ed-pa .ed-lp,
.ed-pa .ed-rp {
  width: 0;
  border-radius: 0;
  top: 0.05em;
  bottom: 0.05em;
}
.ed-pa {
  padding: 0 0.3em;
}
.ed-pc {
  padding: 0 0.6em 0 0.62em;
}
.ed-lbrace,
.ed-rbrace {
  top: 0.02em;
  bottom: 0.02em;
  width: 0.5em;
  border: 0;
}
.ed-lbrace {
  left: 0.04em;
}
.ed-rbrace {
  right: 0.04em;
  transform: scaleX(-1);
}
.ed-br svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.ed-br path,
.ed-rad path {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.3;
  vector-effect: non-scaling-stroke;
  stroke-linejoin: miter;
  stroke-linecap: butt;
}

.ed-sqrt {
  white-space: nowrap;
}
.ed-rt {
  position: relative;
  display: inline-block;
  padding-left: 0.82em;
}
.ed-rad {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0.82em;
}
.ed-rad svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.ed-rc {
  display: inline-block;
  border-top: 0.065em solid currentColor;
  padding: 0.07em 0.12em 0 0.1em;
  margin-top: 0.03em;
}
.ed-idx {
  position: relative;
  z-index: 1;
  display: inline-block;
  font-size: 0.6em;
  line-height: 1.1;
  vertical-align: 1.1em;
  margin: 0 -0.5em 0 0.1em;
}

.ed-bigop {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: middle;
  margin: 0 0.2em 0 0.08em;
}
.ed-sym {
  font-size: 1.7em;
  line-height: 1.05;
  font-style: normal;
}
.ed-lim {
  display: block;
  font-size: 0.7em;
  line-height: 1.15;
  white-space: nowrap;
}

@media (max-width: 520px) {
  .ed {
    font-size: 1.5rem;
  }
  .field-visual {
    padding: 0 6px;
  }
}
