/* Shared look for the tools: colours, cards, inputs, tabs, working steps, fractions and touch behaviour. */
:root {
  --bg: #f6f7f9; --card: #ffffff; --text: #1d2330; --muted: #6b7385; --border: #dde1e8;
  --accent: #3b6ef5; --accent-soft: #e8eeff; --error: #c92a2a; --ok: #2b8a3e; --grid: #eceef2; --axis: #9aa1b0;
  --line: #3b6ef5; --line2: #e8590c; --point: #e8590c; --shade: rgba(59, 110, 245, .22);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b; --card: #1d2027; --text: #e6e8ee; --muted: #9098aa; --border: #2e323c;
    --accent: #7c9cff; --accent-soft: #252c42; --error: #ff6b6b; --ok: #69db7c; --grid: #262a33; --axis: #5c6373;
    --line: #7c9cff; --line2: #ff922b; --point: #ff922b; --shade: rgba(124, 156, 255, .25);
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  padding: 24px 16px 48px;
}
main { max-width: 760px; margin: 0 auto; }
h1 { font-size: 1.5rem; margin: 0 0 4px; }
h2 { font-size: 1rem; margin: 0 0 10px; }
.sub { color: var(--muted); margin: 0 0 20px; font-size: .95rem; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 20px; margin-bottom: 16px; min-width: 0; }
[hidden] { display: none !important; }

/* inputs */
.inputs { display: grid; grid-template-columns: repeat(var(--cols, 3), 1fr); gap: 14px; }
@media (max-width: 560px) { .inputs { grid-template-columns: repeat(var(--cols-sm, 1), minmax(0, 1fr)); } }
.inputs > * { min-width: 0; }
label, .label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
math-field {
  display: block; width: 100%; min-height: 64px; font-size: 1.35rem; padding: 8px 12px; align-content: center;
  border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text);
  --caret-color: var(--accent); --selection-background-color: var(--accent-soft);
  --contains-highlight-background-color: transparent;
}
math-field:focus-within { outline: 2px solid var(--accent); outline-offset: -1px; }
math-field.bad, input.bad, select.bad { border-color: var(--error); }
math-field::part(menu-toggle) { display: none; }
/* the maths keyboard button only shows on touchscreens */
@media (pointer: fine) { math-field::part(virtual-keyboard-toggle) { display: none; } }
/* MathLive makes the keyboard button 60px on touchscreens; too wide for small boxes on phones */
@media (max-width: 560px) {
  math-field { padding: 6px 8px; }
  math-field::part(virtual-keyboard-toggle) { min-width: 32px; min-height: 32px; width: 32px; height: 32px; margin-right: 0; }
}
input[type="text"], input[type="number"], select {
  width: 100%; font: inherit; font-size: 16px; padding: 10px 12px; border-radius: 8px; /* 16px stops iPhone zooming in */
  border: 1px solid var(--border); background: var(--bg); color: var(--text); min-height: 46px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.hint { font-size: .8rem; color: var(--muted); margin-top: 10px; }
.hint kbd { font: inherit; background: var(--accent-soft); color: var(--accent); padding: 0 5px; border-radius: 4px; }
.err { color: var(--error); font-size: .9rem; margin-top: 10px; min-height: 1.2em; }
.note { color: var(--muted); font-size: .9rem; }
.inline { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.inline > label { margin: 0; }

/* buttons and tabs */
button {
  font: inherit; font-size: .9rem; padding: 6px 12px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--accent-soft); color: var(--accent);
  touch-action: manipulation; transition: transform .1s, background-color .15s, color .15s;
}
button:active:not(:disabled) { transform: scale(.96); }
button:disabled { opacity: .35; cursor: default; }
.tabs { display: inline-flex; gap: 4px; padding: 4px; margin-bottom: 16px; border-radius: 10px; background: var(--bg); border: 1px solid var(--border); max-width: 100%; }
.tabs button { border: none; background: transparent; color: var(--muted); padding: 6px 14px; }
.tabs button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); font-weight: 600; }
@media (max-width: 560px) {
  .tabs { display: flex; width: 100%; }
  .tabs button { flex: 1; padding: 6px; font-size: .85rem; }
}
@media (hover: hover) {
  button:hover:not(:disabled) { filter: brightness(.96); }
  .tabs button:not(.on):hover { color: var(--text); filter: none; }
}
@media (pointer: coarse) { button { min-height: 44px; } .tabs button { min-height: 40px; } }
@media (prefers-reduced-motion: reduce) { button { transition: none; } button:active:not(:disabled) { transform: none; } }

/* results */
.answer {
  font-family: "Cambria Math", "STIX Two Math", Georgia, serif; font-size: 1.8rem; line-height: 1.6;
  padding: 4px 0; min-width: 0; max-width: 100%; overflow-x: auto;
}
.answer .sep { color: var(--muted); font-size: .7em; margin: 0 .4em; }
.answer .chunk { display: inline-block; white-space: nowrap; vertical-align: middle; margin: 2px 0; } /* wraps as a whole */
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.math { font-family: "Cambria Math", "STIX Two Math", Georgia, serif; font-size: 1.15rem; line-height: 2.2; }
.frac { display: inline-flex; flex-direction: column; align-items: center; vertical-align: middle; font-size: .85em; line-height: 1.1; margin: 0 3px; }
.frac > span:first-child { border-bottom: 1.5px solid currentColor; padding: 0 3px; }
.frac > span:last-child { padding: 0 3px; }
.sqrt { white-space: nowrap; }
.sqrt > span { border-top: 1.5px solid currentColor; padding: 0 2px; margin-left: 1px; }
sup, sub { font-size: .65em; }
.steps { margin: 0; padding-left: 0; list-style: none; }
.steps li { padding: 8px 0; border-top: 1px solid var(--border); overflow-x: auto; }
.steps li:first-child { border-top: none; }
.steps .label { display: block; font-size: .8rem; color: var(--muted); font-weight: 400; margin: 0; }
.good { color: var(--ok); }
.bad-text { color: var(--error); }
.extra { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 560px) { .extra { grid-template-columns: 1fr; } }
canvas { width: 100%; aspect-ratio: 1; display: block; border-radius: 8px; touch-action: pan-y; }
canvas.wide { aspect-ratio: 16 / 10; }

/* tables */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; font-size: .92rem; }
th, td { padding: 6px 10px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: .8rem; }
th:first-child, td:first-child { text-align: left; }
tr.hl td { background: var(--accent-soft); }
