:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --ink: #1d2330;
  --muted: #5d6677;
  --line: #dde1e8;
  --accent: #2357c6;
  --accent-soft: #e7eefc;
  --pivot: #ffe7a3;
  --changed: #eef6ee;
  --ok: #1d7a3c;
  --err: #b42318;
  --err-soft: #fdecea;
  --math-font: "STIX Two Math", "Latin Modern Math", "Cambria Math", math;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151b;
    --surface: #1b1f27;
    --ink: #e6e9ef;
    --muted: #9aa3b2;
    --line: #2e3440;
    --accent: #7aa2ff;
    --accent-soft: #1f2a44;
    --pivot: #5a4810;
    --changed: #1c2e22;
    --ok: #5cc98a;
    --err: #ff8a80;
    --err-soft: #3a1d1b;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #12151b;
  --surface: #1b1f27;
  --ink: #e6e9ef;
  --muted: #9aa3b2;
  --line: #2e3440;
  --accent: #7aa2ff;
  --accent-soft: #1f2a44;
  --pivot: #5a4810;
  --changed: #1c2e22;
  --ok: #5cc98a;
  --err: #ff8a80;
  --err-soft: #3a1d1b;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 32px 16px 64px;
}

h1 { margin: 0 0 4px; font-size: 1.75rem; letter-spacing: -0.01em; }
.lead { margin: 0 0 24px; color: var(--muted); max-width: 65ch; }

math { font-family: var(--math-font); }

/* ---------- Input form ---------- */

form {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
}

label { display: block; font-weight: 600; margin-bottom: 6px; }

.input-row { display: flex; gap: 8px; }

.input-row input {
  flex: 1;
  min-width: 0;
  font: 1.05rem/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  padding: 10px 12px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.input-row input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

button[type="submit"] {
  font: inherit;
  font-weight: 600;
  padding: 10px 18px;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}

button[type="submit"]:hover { filter: brightness(1.08); }

.preview {
  min-height: 2.2em;
  margin-top: 10px;
  font-size: 1.15rem;
  color: var(--muted);
  overflow-x: auto;
}

.examples {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  font-size: 0.9rem;
}

.examples span { color: var(--muted); margin-right: 2px; }

.examples button {
  font: inherit;
  padding: 3px 10px;
  color: var(--accent);
  background: var(--accent-soft);
  border: 0;
  border-radius: 999px;
  cursor: pointer;
}

.examples button:hover { text-decoration: underline; }

.help { margin-top: 12px; font-size: 0.9rem; color: var(--muted); }
.help summary { cursor: pointer; }
.help ul { margin: 8px 0 0; padding-left: 20px; }
.help code { color: var(--ink); }

/* ---------- Result card ---------- */

#result { margin: 24px 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--ok);
  border-radius: 10px;
  padding: 16px 20px;
}

.card h2 { margin: 0 0 6px; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.card .eq { font-size: 1.6rem; overflow-x: auto; }
.card.error { border-left-color: var(--err); background: var(--err-soft); }
.card.error p { margin: 0; }

/* ---------- Steps table ---------- */

#steps h2 { font-size: 1.2rem; margin: 0 0 10px; }

table.steps {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}

table.steps th {
  text-align: left;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}

table.steps td {
  vertical-align: top;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

table.steps tr:last-child td { border-bottom: 0; }

td.num {
  width: 3em;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

td.desc { width: 38%; }
td.desc strong { display: block; margin-bottom: 4px; }
td.desc p { margin: 0 0 6px; color: var(--muted); font-size: 0.93rem; }
td.desc p:last-child { margin-bottom: 0; }

td.work { font-size: 1.1rem; }
.scroll { overflow-x: auto; }
.scroll + .scroll { margin-top: 10px; }

tr.final td.work { font-size: 1.35rem; }
tr.fail td { background: var(--err-soft); }
tr.fail td.num { color: var(--err); }

/* Inner HTML tables (atom counts / check) */
table.inner { border-collapse: collapse; font-size: 0.95rem; }

table.inner th,
table.inner td {
  padding: 4px 10px;
  border-bottom: 1px solid var(--line);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

table.inner th { font-weight: 600; color: var(--muted); }
table.inner td:first-child, table.inner th:first-child { text-align: left; }
table.inner .zero { color: var(--line); }
table.inner .ok { color: var(--ok); font-weight: 700; }
table.inner .bad { color: var(--err); font-weight: 700; }
table.inner .side { color: var(--muted); font-size: 0.85rem; }

/* ---------- MathML matrices and equation systems ---------- */

mtable.matrix mtd { padding: 3px 9px; text-align: right; }
mtable.matrix mtr.hdr mtd { color: var(--muted); text-align: right; padding-bottom: 4px; }
mtable.matrix mtd.rl { color: var(--muted); padding-right: 12px; }
mtable.matrix mtr.changed mtd.v { background-color: var(--changed); }
mtable.matrix mtd.pivot { background-color: var(--pivot) !important; }

/* Square brackets drawn with borders + corner ticks */
mtable.matrix mtd.bl { border-left: 1.5px solid currentColor; }
mtable.matrix mtd.br { border-right: 1.5px solid currentColor; }

mtable.matrix mtd.bl.top,
mtable.matrix mtd.bl.bot,
mtable.matrix mtd.br.top,
mtable.matrix mtd.br.bot {
  background-image: linear-gradient(currentColor, currentColor);
  background-size: 6px 1.5px;
  background-repeat: no-repeat;
}

mtable.matrix mtd.bl.top { background-position: left top; }
mtable.matrix mtd.bl.bot { background-position: left bottom; }
mtable.matrix mtd.br.top { background-position: right top; }
mtable.matrix mtd.br.bot { background-position: right bottom; }

mtable.matrix mtd.bl.top.bot,
mtable.matrix mtd.br.top.bot {
  background-image: linear-gradient(currentColor, currentColor), linear-gradient(currentColor, currentColor);
}

mtable.matrix mtd.bl.top.bot { background-position: left top, left bottom; }
mtable.matrix mtd.br.top.bot { background-position: right top, right bottom; }

mtable.sys mtd { padding: 3px 4px; }
mtable.sys mtd.l { text-align: left; }
mtable.sys mtd.r { text-align: right; }
mtable.sys mtd.c { text-align: center; }
mtable.sys mtd.lbl { color: var(--muted); padding-right: 14px; text-align: left; }

/* ---------- Small screens ---------- */

@media (max-width: 720px) {
  table.steps thead { display: none; }
  table.steps tr { display: grid; grid-template-columns: 2.5em 1fr; border-bottom: 1px solid var(--line); }
  table.steps tr:last-child { border-bottom: 0; }
  table.steps td { border-bottom: 0; padding: 10px 12px; }
  td.desc { width: auto; }
  td.work { grid-column: 1 / -1; padding-top: 0; min-width: 0; }
  td.num { width: auto; }
  .input-row { flex-direction: column; }
  .card .eq { font-size: 1.25rem; }
}
