:root {
  color-scheme: light dark;
  --bg: #f4f6fb;
  --panel: #ffffff;
  --panel-2: #f0f3fa;
  --border: #dce2ee;
  --text: #17202f;
  --muted: #5c6a83;
  --accent: #2d5bd7;
  --accent-soft: #e7edfd;
  --hot-row: rgba(45, 91, 215, 0.07);
  --hot-col: rgba(45, 91, 215, 0.12);
  --hot-cross: rgba(45, 91, 215, 0.2);
  --danger: #b42318;
  --danger-soft: #fdecea;
  --radius: 14px;
  --mono: "Fira Code", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1017;
    --panel: #151a23;
    --panel-2: #1b2230;
    --border: #273142;
    --text: #e6ebf5;
    --muted: #93a0b5;
    --accent: #7aa2ff;
    --accent-soft: #1d2740;
    --hot-row: rgba(122, 162, 255, 0.09);
    --hot-col: rgba(122, 162, 255, 0.14);
    --hot-cross: rgba(122, 162, 255, 0.24);
    --danger: #ff9b90;
    --danger-soft: #331a1a;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: clamp(1rem, 3vw, 2.5rem);
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-ligatures: common-ligatures contextual;
  line-height: 1.5;
}

.masthead,
main,
footer {
  max-width: 1180px;
  margin-inline: auto;
}

.masthead {
  margin-bottom: 1.25rem;
}

h1 {
  margin: 0 0 0.35rem;
  font-size: clamp(1.6rem, 3vw, 2.1rem);
  letter-spacing: -0.02em;
}

.masthead p {
  margin: 0;
  max-width: 72ch;
  color: var(--muted);
}

.masthead p + p {
  margin-top: 0.45rem;
}

main {
  display: grid;
  grid-template-columns: minmax(300px, 400px) minmax(0, 1fr);
  gap: 1.25rem;
  align-items: start;
}

@media (max-width: 900px) {
  main {
    grid-template-columns: minmax(0, 1fr);
  }
}

.stack {
  display: grid;
  gap: 1.25rem;
  align-content: start;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.15rem 1.25rem;
}

h2 {
  margin: 0 0 0.9rem;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--muted);
}

.hint {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

code {
  padding: 0.06rem 0.3rem;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--panel-2);
  font-family: var(--mono);
  font-size: 0.9em;
}

.display {
  display: block;
  width: 100%;
  padding: 0.95rem 1rem;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel-2);
  color: var(--text);
  font-family: var(--mono);
  font-size: clamp(1.4rem, 4.5vw, 1.85rem);
  font-weight: 600;
  font-variant-ligatures: common-ligatures contextual;
  line-height: 1.15;
  text-align: right;
}

.display::placeholder {
  color: var(--muted);
  font-size: 0.72em;
  font-weight: 500;
}

button {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

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

button.primary {
  padding-inline: 1.05rem;
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

#eval-form.htmx-request button.primary {
  opacity: 0.55;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.5rem;
  margin-top: 0.8rem;
}

.keypad button {
  min-height: 2.8rem;
  padding: 0.45rem 0.35rem;
  border-radius: 999px;
  font-family: var(--mono);
  font-size: 1.05rem;
  line-height: 1;
}

/* Placeholder slots for keys the wheel has no operation for: digits 2-9 and
   the decimal point. They keep the familiar calculator grid without
   pretending to be usable. */
.keypad .key-blank {
  background-color: var(--panel);
  cursor: default;
  pointer-events: none;
}

.keypad .primary {
  font-size: 1.15rem;
  font-weight: 700;
}

.examples {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.95rem;
}

.examples-label {
  margin-right: 0.15rem;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.examples button {
  padding: 0.28rem 0.55rem;
  font-family: var(--mono);
  font-size: 0.82rem;
}

#console {
  min-height: 7rem;
  margin-top: 1rem;
}

.result-card {
  display: grid;
  gap: 0.15rem;
  place-items: center;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
  text-align: center;
}

.result-expression {
  margin: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.9rem;
}

.result-name {
  margin: 0.15rem 0 0;
  font-family: var(--mono);
  font-size: clamp(2.5rem, 6vw, 3.6rem);
  font-weight: 700;
  line-height: 1.05;
}

.result-legend {
  margin: 0;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.82rem;
}

/* Plain-language gloss for the bottom value, shown with any ⊥ result. */
.result-explainer {
  max-width: 46ch;
  margin: 0.5rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
  text-align: left;
}

.result-card.is-error {
  border-color: var(--danger);
  background: var(--danger-soft);
}

.error-title {
  margin: 0.35rem 0 0;
  color: var(--danger);
  font-weight: 600;
}

.error-detail {
  margin: 0;
  font-size: 0.9rem;
}

.error-caret {
  margin: 0.55rem 0 0;
  max-width: 100%;
  overflow-x: auto;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  background: var(--bg);
}

.error-caret code {
  padding: 0;
  border: 0;
  background: transparent;
  white-space: pre;
  font-size: 0.85rem;
}

.history {
  margin-top: 1rem;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.history-header h2 {
  margin: 0;
}

.ghost {
  padding: 0.2rem 0.45rem;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
}

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

.history ol {
  display: grid;
  gap: 0.35rem;
  max-height: 15rem;
  margin: 0.55rem 0 0;
  padding: 0;
  overflow: auto;
  list-style: none;
}

.history-entry {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  width: 100%;
  padding: 0.45rem 0.6rem;
  font-family: var(--mono);
  font-size: 0.88rem;
  text-align: left;
}

.history-expression {
  overflow: hidden;
  color: var(--text);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-result {
  color: var(--accent);
  font-weight: 700;
}

.table-grid {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 1.1rem 1.5rem;
}

.table-note {
  margin: 1rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.cayley {
  width: auto;
  min-width: 8.5rem;
  border-collapse: collapse;
  font-family: var(--mono);
  font-size: 0.95rem;
}

.cayley caption {
  padding-bottom: 0.3rem;
  color: var(--muted);
  font-size: 0.8rem;
  letter-spacing: 0.04em;
  text-align: left;
}

.cayley th,
.cayley td {
  padding: 0.28rem 0.6rem;
  border-bottom: 1px solid var(--border);
  text-align: center;
}

.cayley thead th {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}

.cayley tbody th {
  color: var(--muted);
  font-weight: 600;
}

.cayley tbody tr:last-child th,
.cayley tbody tr:last-child td {
  border-bottom: 0;
}

.cayley thead th,
.cayley tbody th,
.cayley td {
  transition:
    background-color 120ms ease-out,
    color 120ms ease-out,
    box-shadow 120ms ease-out;
}

.cayley th.is-row-hot,
.cayley td.is-row-hot {
  background: var(--hot-row);
}

.cayley thead th.is-col-hot,
.cayley td.is-col-hot {
  background: var(--hot-col);
}

.cayley .is-row-hot.is-col-hot {
  background: var(--hot-cross);
}

.cayley tbody th.is-row-hot,
.cayley thead th.is-col-hot {
  color: var(--accent);
}

.cayley td.is-cell-hot {
  box-shadow: inset 0 0 0 2px var(--accent);
}

@media (prefers-reduced-motion: reduce) {
  .cayley thead th,
  .cayley tbody th,
  .cayley td {
    transition: none;
  }
}

.explain-intro {
  margin: 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.steps {
  display: grid;
  gap: 0.55rem;
  margin: 0.85rem 0 0;
  padding: 0;
  list-style: none;
}

.step {
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
}

.step-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.6rem;
}

.step-expr {
  padding: 0;
  border-color: transparent;
  background: transparent;
  font-size: 0.95rem;
}

.step-op {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.step-rule {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.step-work {
  display: grid;
  gap: 0.15rem;
  margin-top: 0.3rem;
}

.step-work code {
  display: block;
  padding: 0.15rem 0.35rem;
  border-color: transparent;
  background: var(--bg);
  font-size: 0.8rem;
  overflow-wrap: anywhere;
}

.step-result {
  margin: 0.35rem 0 0;
  font-family: var(--mono);
  font-size: 0.88rem;
}

.step-pair {
  color: var(--muted);
}

.explain-final {
  margin: 0.7rem 0 0;
  font-family: var(--mono);
  font-size: 0.92rem;
}

.final-gloss {
  color: var(--muted);
  font-weight: 400;
}

.conventional {
  margin-top: 1rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--border);
}

.conventional h3 {
  display: flex;
  gap: 0.4rem;
  align-items: baseline;
  margin: 0 0 0.4rem;
  font-size: 0.85rem;
}

.conv-tag {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 400;
}

.conv-verdict {
  margin: 0;
  font-family: var(--mono);
  font-weight: 700;
}

.conv-verdict.is-value {
  color: var(--accent);
}

.conv-verdict.is-undefined {
  color: var(--danger);
}

.conv-steps {
  display: grid;
  gap: 0.15rem;
  margin: 0.4rem 0 0;
  padding: 0;
  list-style: none;
}

.conv-steps code {
  display: block;
  padding: 0.15rem 0.35rem;
  border-color: transparent;
  background: var(--bg);
  font-size: 0.8rem;
}

.conv-steps li.is-failed code {
  color: var(--danger);
}

.conv-detail {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}

footer {
  margin-top: 1.5rem;
  color: var(--muted);
  font-size: 0.85rem;
}

footer p {
  margin: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  border: 0;
  white-space: nowrap;
}
