/* SPDX-License-Identifier: Apache-2.0 */
/* Last shell layer: retain existing tokens, DOM and independently scrollable math. */
:root {
  --om-usability-focus: var(--fc-primary-9);
}

body {
  grid-template-rows: auto auto minmax(0, 1fr) auto;
}
#topbar { grid-row: 1; }
#version-banner { grid-row: 2; }
#workspace { grid-row: 3; }
#bottom { grid-row: 4; }

#workspace {
  grid-template-columns: minmax(0, 48fr) minmax(0, 52fr);
  grid-template-rows: minmax(0, 1.15fr) minmax(0, 1fr);
  grid-template-areas: "input render" "input steps";
  overflow: hidden;
}
#input-pane { grid-area: input; }
#render-pane { grid-area: render; }
#steps-pane { grid-area: steps; }

#workspace .pane__body {
  min-width: 0;
  padding: var(--fc-space-16);
  scrollbar-gutter: stable;
}
#workspace .pane__title {
  padding: var(--fc-space-12) var(--fc-space-16);
  color: var(--fc-text-primary);
  background: var(--fc-bg-surface);
}
#input-pane > .pane__body {
  display: flex;
  flex-direction: column;
}
#input-pane > .pane__body > * { flex-shrink: 0; }
#input-pane #formula-toolbar { order: 0; }
#input-pane #formula-help { order: 1; }
#input-pane #editor-actions { order: 2; }
#input-pane .formula-paper { order: 3; }
#input-pane #calculation-feedback { order: 4; }
#input-pane #formula-preview { order: 5; }
#input-pane #formula-error { order: 6; }
#input-pane #formula-library { order: 7; }
#input-pane .formula-source { order: 8; }
#input-pane #examples {
  order: 9;
  max-height: 144px;
  overflow-y: auto;
}

.version-picker {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--fc-space-8);
  min-width: 0;
  max-width: 100%;
  color: var(--fc-text-secondary);
}
.version-picker #version-select {
  width: 18ch;
  min-width: 0;
  max-width: 100%;
  min-height: var(--fc-hit-min);
}
#version-banner {
  margin: 0;
  padding: var(--fc-space-8) var(--fc-space-16);
  border-bottom: 1px solid var(--fc-line-strong);
  background: var(--fc-bg-surface);
  color: var(--fc-text-secondary);
  font-size: 14px;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

#input-pane .formula-paper {
  min-height: 240px;
  padding: var(--fc-space-16);
  border-color: var(--fc-line-strong);
}
#input-pane .formula-paper:focus-within {
  border-color: var(--om-usability-focus);
  box-shadow: inset 3px 0 0 var(--om-usability-focus);
}
#input-pane .formula-line {
  min-height: 170px;
  max-width: 100%;
  padding-block: var(--fc-space-16);
  scroll-padding-inline: var(--fc-space-8);
}
#formula-editor {
  min-width: 0;
  max-width: 100%;
  padding: var(--fc-space-4);
}
#formula-editor:has(> .formula-slot:only-child) { flex: 1 1 auto; }
#formula-editor > .formula-slot:only-child {
  width: 100%;
  max-width: 100%;
  min-height: 56px;
  padding: var(--fc-space-8) var(--fc-space-12);
}
#formula-editor .formula-slot,
#input-pane .bound-input,
#input-pane .formula-variable input {
  min-height: var(--fc-hit-min);
}
#formula-editor .formula-slot::placeholder {
  color: var(--fc-text-secondary);
  opacity: 1;
}
/* Preserve the editor's deliberately tiny, unfocused insertion gaps. */
#formula-editor .formula-gap:placeholder-shown:not(:focus)::placeholder {
  color: transparent;
}
#input-pane .formula-variable {
  flex-wrap: wrap;
  gap: var(--fc-space-8) var(--fc-space-12);
  padding-top: var(--fc-space-12);
  border-top: 1px solid var(--fc-line-subtle);
  font-size: 14px;
}
#input-pane .formula-help {
  margin-block: var(--fc-space-8) var(--fc-space-12);
}

/* ID scope also wins over the editor's dynamically appended library stylesheet. */
#formula-library {
  min-width: 0;
  margin-block: var(--fc-space-12);
}
#formula-library > summary {
  min-height: var(--fc-hit-min);
  padding-block: var(--fc-space-12);
}
#formula-library .formula-categories {
  flex-wrap: nowrap;
  max-width: 100%;
  overflow-x: auto;
  padding: var(--fc-space-4);
  scroll-padding-inline: var(--fc-space-4);
}
#formula-library .formula-categories button {
  flex: 0 0 auto;
  min-height: var(--fc-hit-min);
  white-space: nowrap;
  font-size: 13px;
}
#formula-library .formula-library-grid {
  grid-template-columns: repeat(auto-fit, minmax(76px, 1fr));
  max-height: 144px;
  padding: var(--fc-space-4);
}
#formula-library .formula-library-grid button {
  min-width: 0;
  min-height: var(--fc-hit-min);
}
#formula-library .formula-library-help {
  max-height: 4.8em;
  overflow-y: auto;
}
#formula-editor .formula-matrix-controls button {
  min-width: var(--fc-hit-min);
  min-height: var(--fc-hit-min);
  padding: var(--fc-space-4) var(--fc-space-8);
  font-size: 12px;
}

#op-bar {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--fc-space-8);
  padding: var(--fc-space-12) var(--fc-space-16);
  position: static;
}
#op-bar button {
  min-width: 0;
  min-height: var(--fc-hit-min);
  width: 100%;
  padding: var(--fc-space-8) var(--fc-space-12);
  white-space: normal;
  overflow-wrap: anywhere;
}
#op-bar [data-op="evaluate"] {
  grid-column: 1 / -1;
  min-height: 48px;
  background: var(--fc-primary-7);
  border-color: var(--fc-primary-7);
  color: var(--fc-text-on-brand);
  font-weight: 600;
}
@media (hover: hover) and (pointer: fine) {
  #op-bar [data-op="evaluate"]:hover {
    background: var(--fc-primary-9);
    border-color: var(--fc-primary-9);
  }
}
#op-bar [data-op="evaluate"]:disabled {
  background: var(--fc-bg-sunken);
  border-color: var(--fc-line-subtle);
  color: var(--fc-text-secondary);
}

#editor-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--fc-space-8);
  margin-block: var(--fc-space-8);
}
#editor-actions button {
  min-width: var(--fc-hit-min);
  min-height: var(--fc-hit-min);
  padding: var(--fc-space-4) var(--fc-space-12);
}
#calculation-feedback {
  margin-block: var(--fc-space-8);
  color: var(--fc-text-secondary);
  line-height: 1.6;
  overflow-wrap: anywhere;
}
#calculation-feedback:empty { display: none; }
#calculation-feedback[data-state="error"],
#calculation-feedback[data-kind="error"],
#calculation-feedback[data-message-kind="error"],
#calculation-feedback[data-calculation-state="error"],
#calculation-feedback[data-calculation-state="maximaTimeout"] {
  color: var(--fc-danger);
}
#input-pane .math-error {
  font-family: var(--fc-font-ui);
  line-height: 1.6;
}
#keyboard-help {
  min-width: 0;
  max-width: 100%;
  color: var(--fc-text-secondary);
  font-size: 14px;
  line-height: 1.6;
  overflow-wrap: anywhere;
}
#keyboard-help > summary {
  min-height: var(--fc-hit-min);
  padding-block: var(--fc-space-12);
  cursor: pointer;
}
#input-pane :is(#usability-examples, #usability-history) > summary {
  min-height: var(--fc-hit-min);
  padding-block: var(--fc-space-12);
  cursor: pointer;
}
#keyboard-help kbd {
  display: inline-block;
  padding: var(--fc-space-2) var(--fc-space-4);
  border: 1px solid var(--fc-line-subtle);
  border-radius: var(--fc-radius-sm);
  background: var(--fc-bg-sunken);
}
#result { min-height: 112px; }
#render-pane .desktop-latex > summary { min-height: var(--fc-hit-min); }
#latex-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--fc-space-8);
}
#latex-actions #result-usability-actions {
  display: flex;
  flex: 2 1 16rem;
  flex-wrap: wrap;
  align-items: center;
  min-width: 0;
  padding: 0;
  border: 0;
  background: transparent;
}
#latex-actions .action {
  flex: 1 1 8rem;
  min-width: 0;
  white-space: normal;
  overflow-wrap: anywhere;
  padding: var(--fc-space-8) var(--fc-space-12);
}
#workspace :is(.formula-preview, .math-display, .openmath-math-scroll) {
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
}
#plot-host .plot-workbench {
  --fc-border-default: var(--fc-line-subtle);
  --fc-bg-panel: var(--fc-bg-surface);
  --fc-bg-input: var(--fc-bg-surface);
  --fc-accent: var(--om-usability-focus);
}
#plot-host .plot-workbench :is(input, select, textarea) {
  min-height: var(--fc-hit-min);
}
#workspace :is(button, select, input, textarea, summary, [tabindex]):focus-visible,
#topbar :is(button, select, a):focus-visible,
#version-banner a:focus-visible,
#bottom summary:focus-visible {
  outline: 3px solid var(--om-usability-focus);
  outline-offset: 2px;
}

@media (prefers-color-scheme: dark) {
  :root { --om-usability-focus: #8cb4ff; }
  #plot-host .plot-chart {
    background: linear-gradient(135deg, var(--fc-bg-sunken), var(--fc-bg-surface));
  }
  #notebook-host .notebook {
    --notebook-line: var(--fc-line-strong);
    --notebook-paper: var(--fc-bg-surface);
    --notebook-accent: #8ccbc5;
    color: var(--fc-text-primary);
    background: linear-gradient(135deg, var(--fc-bg-sunken), var(--fc-bg-surface));
  }
  #notebook-host .notebook__button { background: var(--fc-bg-surface); }
  #notebook-host .notebook__button:hover:not(:disabled) { background: var(--fc-bg-hover); }
  #notebook-host :is(.notebook__status, .notebook__help, .notebook__scope) {
    color: var(--fc-text-secondary);
  }
  #notebook-host :is(.notebook__message, .notebook__error) { color: var(--fc-danger); }
}
@media (prefers-contrast: more) {
  #input-pane .formula-paper,
  #formula-library,
  #workspace .pane__title { border-color: var(--fc-line-strong); }
  #calculation-feedback, #version-banner { color: var(--fc-text-primary); }
}
@media (forced-colors: active) {
  #input-pane .formula-paper:focus-within { border-color: Highlight; }
  #op-bar [data-op="evaluate"] {
    background: Highlight;
    border-color: Highlight;
    color: HighlightText;
  }
  :root { --om-usability-focus: Highlight; }
}

@media (min-width: 1180px) {
  #op-bar { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
@media (max-width: 900px) {
  html, body { height: auto; min-height: 100%; }
  body {
    grid-template-rows: auto auto auto auto;
    overflow: visible;
  }
  #workspace {
    display: flex;
    flex-direction: column;
    gap: var(--fc-space-8);
    overflow: visible;
    background: var(--fc-bg-canvas);
  }
  #input-pane { order: 0; }
  #render-pane { order: 1; }
  #steps-pane { order: 2; }
  #workspace > .pane { flex: 0 0 auto; }
  #workspace .pane__body { overflow: visible; }
  /* Flatten only the mobile input shell so Calculate follows the actual paper. */
  #input-pane > .pane__body { display: contents; }
  #input-pane > .pane__title { order: -1; }
  #input-pane #op-bar { order: 6; }
  #input-pane :is(#formula-toolbar, #formula-help, #editor-actions,
    .formula-paper, #calculation-feedback, #formula-preview,
    #formula-error, #formula-library, .formula-source, #examples) {
    margin-inline: var(--fc-space-12);
  }
  #input-pane .formula-paper { min-height: 210px; }
  #input-pane .formula-line { min-height: 140px; }
}
@media (max-width: 560px) {
  #workspace .pane__body { padding: var(--fc-space-12); }
  #workspace .pane__title { padding: var(--fc-space-8) var(--fc-space-12); }
  #input-pane .formula-paper { padding: var(--fc-space-12); }
  #op-bar { padding: var(--fc-space-12); }
  .version-picker { width: 100%; }
  .version-picker #version-select { flex: 1 1 12ch; width: auto; }
  #version-banner { padding-inline: var(--fc-space-12); }
  #editor-actions button { flex: 1 1 5rem; }
}
@media (prefers-reduced-motion: reduce) {
  #workspace *, #topbar *, #version-banner, #bottom * {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
@media print {
  html, body { height: auto; }
  body { display: block; overflow: visible; }
  #workspace { display: block; overflow: visible; }
  #version-banner, .version-picker, #editor-actions,
  #calculation-feedback, #keyboard-help, #formula-library,
  #formula-toolbar { display: none !important; }
  #workspace .pane__body { overflow: visible; }
  #input-pane .formula-paper {
    min-height: 0;
    background: #fff;
    border-color: #000;
    box-shadow: none;
  }
  #input-pane .formula-line { min-height: 0; }
  #workspace input, #input-pane .formula-variable {
    color: #000;
    background: #fff;
  }
}
