:root {
  --bg-page: #f3f3f1;
  --bg-panel: #ffffff;
  --bg-header: #f7f7f5;
  --bg-hover: #ececea;
  --border: #e3e3e0;
  --border-strong: #d3d3cf;
  --text-primary: #22262b;
  --text-secondary: #6b7076;
  --text-muted: #9a9da2;
  --radius: 8px;
  --radius-lg: 12px;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: var(--font-sans);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  background: var(--bg-page);
}

.app {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
  gap: 12px;
  padding: 12px;
}

.col {
  flex: 1 1 50%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.col-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: var(--bg-header);
  border-bottom: 1px solid var(--border);
}

.col-title {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  position: relative;
  background: var(--bg-header);
}

.tool-divider {
  width: 1px;
  height: 20px;
  background: var(--border-strong);
  margin: 0 6px;
  flex: 0 0 auto;
}

.tool-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s ease, color 0.12s ease;
  flex: 0 0 auto;
  appearance: none;
  -webkit-appearance: none;
}

.tool-btn svg {
  width: 27px;
  height: 27px;
}

.tool-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.tool-btn:focus-visible {
  outline: 2px solid var(--border-strong);
  outline-offset: 1px;
}

.tool-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.tool-btn:disabled:hover {
  background: transparent;
  color: var(--text-secondary);
}

.dropdown[data-open] > .tool-btn {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* --- Dropdown-Panels --- */

.dropdown {
  position: relative;
}

.dropdown-panel {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 20;
  width: 260px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 24px rgba(20, 20, 20, 0.12), 0 2px 6px rgba(20, 20, 20, 0.06);
  padding: 12px;
}

.dropdown[data-open] > .dropdown-panel {
  display: block;
}

/* --- Toolbar overflow menu (static/editor.js, initToolbarOverflow) ---
   When the input toolbar gets too narrow (e.g. a narrow Moodle column),
   the data-collapsible buttons move, from the right, into the panel of
   the last button ("More", menu icon), one below the other. */
.col-header .toolbar {
  min-width: 0;        /* may be narrower than its buttons: JS measures */
}

.toolbar-more[hidden] {
  display: none;
}

.toolbar-more > .toolbar-more-panel {
  left: auto;
  right: 0;
  width: auto;
  padding: 6px;
}

.toolbar-more[data-open] > .toolbar-more-panel {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
}

.toolbar-more-panel .tool-divider {
  display: none;
}

@media (min-width: 761px) {
  /* A menu of a button inside the overflow menu opens to its left. */
  .toolbar-more-panel .dropdown-panel {
    top: 0;
    left: auto;
    right: calc(100% + 8px);
  }
}

.panel-title {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}

.panel-hint {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

.chip-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.chip-grid button {
  min-width: 30px;
  height: 28px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-panel);
  color: var(--text-primary);
  font-size: 13px;
  font-family: var(--font-sans);
  cursor: pointer;
}

.chip-grid button:hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
}

.setting-control {
  display: flex;
  align-items: center;
  gap: 4px;
}

.setting-control select {
  height: 28px;
  padding: 0 4px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font-family: var(--font-sans);
  font-size: 13px;
  background: #ffffff;
}

.setting-control input[type="text"] {
  width: 64px;
  height: 28px;
  padding: 0 6px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font-family: var(--font-sans);
  font-size: 13px;
}

.setting-suffix {
  color: var(--text-secondary);
}

.info-list {
  margin: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 12px;
  row-gap: 4px;
  font-size: 13px;
}

.info-list dt {
  color: var(--text-secondary);
}

.info-list dd {
  margin: 0;
  text-align: right;
}

/* --- Editor / Output --- */

.editor-wrap, .output-wrap {
  flex: 1;
  display: flex;
  min-height: 0;
  padding: 12px;
}

textarea {
  flex: 1;
  width: 100%;
  resize: none;
  font-family: var(--font-mono);
  font-size: inherit;
  line-height: 1.8;
  box-sizing: border-box;
  overflow: auto;
  white-space: pre;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-panel);
  padding: 10px 12px;
  color: var(--text-primary);
}

textarea:focus {
  outline: none;
  border-color: var(--border-strong);
}

.output-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: auto;
  min-height: 0;
  white-space: nowrap;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-panel);
  padding: 12px;
}

.formula-block {
  margin-bottom: 0.4em;
  white-space: normal;
}

/* Units in the LaTeX output are wrapped in \engiunit{...} (see
   mathlib/units.py::wrap_unit_latex()); the MathJax macro in
   templates/index.html turns that into this class. The size is the
   "Unit font size" setting: app.py passes it to the template, which
   sets --unit-scale (a percentage, 100% = same size as the numbers)
   on .output-container. The fallback below only applies if that
   variable is missing. The size for the .tex export is set in
   core/latex_export.py from the same setting. */
.engi-unit {
  font-size: var(--unit-scale, 100%);
}

/* Sheet prefix of references to other sheets ("S1::" in S1::L), wrapped
   in \engisheet{...} (mathlib/units.py::wrap_sheet_prefix_latex()); the
   MathJax macro in templates/index.html turns that into this class.
   Size = the "Sheet prefix size" setting (--sheet-scale, set like
   --unit-scale on .output-container). */
.engi-sheet {
  font-size: var(--sheet-scale, 50%);
}

/* The result table is only a wrapper for the print header (the sheet
   name repeats on every printed page, see @media print): on screen it
   must not exist as a box at all, so .output-container stays a direct
   flex child of .output-wrap. */
.print-frame,
.print-frame > tbody,
.print-frame > tbody > tr,
.print-frame > tbody > tr > td {
  display: contents;
}

.print-frame > thead,
.print-sheets {
  display: none;
}

/* --- Sheet bar (below the editor) --- */

.sheet-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px 10px;
  min-height: 44px;
}

.sheet-tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  min-width: 0;
}

.sheet-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-header);
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}

.sheet-tab:hover {
  background: var(--bg-hover);
}

.sheet-tab.is-active {
  background: var(--bg-panel);
  color: var(--text-primary);
  border-color: var(--border-strong);
  font-weight: 600;
}

.sheet-tab input {
  width: 9ch;
  min-width: 4ch;
  height: 22px;
  padding: 0 4px;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  font-family: var(--font-sans);
  font-size: 13px;
}

.sheet-tab input.is-invalid {
  border-color: #c0392b;
  background: #fdf0ee;
}

.sheet-tab-close {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  padding: 0 0 0 2px;
  cursor: pointer;
}

.sheet-tab-close:hover {
  color: #c0392b;
}

.formula-block img {
  max-width: 100%;
  height: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-top: 0.5em;
}

/* --- Pictures: picture("name") --- */

.picture-wrap {
  margin: 0.4em 0;
}

.picture-left { text-align: left; }
.picture-center { text-align: center; }
.picture-right { text-align: right; }

/* Without width=: the picture's own size, but at most 500 px (and the
   column). With width=: exactly that width (inline style), at most the column. */
.formula-block img.engipad-picture {
  display: inline-block;
  max-width: min(500px, 100%);
  height: auto;
  border: none;
  border-radius: 0;
  margin: 0;
}

.formula-block img.engipad-picture.picture-sized {
  max-width: 100%;
}

/* --- Responsive: Dropdown -> Boden-Sheet --- */

@media (max-width: 760px) {
  .app {
    flex-direction: column;
    padding: 8px;
    gap: 8px;
  }

  .col {
    flex: 1 1 50%;
  }

  .dropdown-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: auto;
    max-height: 50vh;
    overflow-y: auto;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -8px 24px rgba(20, 20, 20, 0.16);
  }
}

@media print {
  .picture-wrap {
    break-inside: avoid;
  }

  html, body {
    height: auto !important;
    overflow: visible !important;
  }

  body {
    background: #ffffff !important;
    display: block !important;
  }

  .app {
    display: block !important;
    padding: 0 !important;
    gap: 0 !important;
  }

  .col-input {
    display: none !important;
  }

  .col-result {
    border: none !important;
    border-radius: 0 !important;
    display: block !important;
    height: auto !important;
  }

  .col-header .toolbar {
    display: none !important;
  }

  /* The toolbar rule above only hid the buttons -- the header BAR
     itself (.col-header: background + border-bottom, see the base
     rule around line 54) stayed visible with just the "Result" title
     in it, printing as an empty-looking frame/header line at the top
     of the page. Hide the whole header for the result column instead
     -- .col-input is already display:none above, so its header goes
     with it; only .col-result's header was ever left showing. */
  .col-result .col-header {
    display: none !important;
  }

  .output-wrap {
    padding: 0 !important;
  }

  .output-container {
    border: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    background: #ffffff !important;
    overflow: visible !important;
    height: auto !important;
    max-height: none !important;
    white-space: normal !important;
  }

  .formula-block {
    page-break-inside: avoid;
    margin-bottom: 1em;
  }

  /* Sheet name as a header on every printed page: a table header row
     repeats on each page the table spans (also in Firefox, unlike
     @page margin boxes). Only shown when there is more than one sheet
     (.is-empty otherwise). */
  .sheet-bar {
    display: none !important;
  }

  .print-frame {
    display: table !important;
    width: 100%;
  }

  .print-frame > thead,
  .print-sheet > thead {
    display: table-header-group !important;
  }

  .print-frame > tbody,
  .print-sheet > tbody {
    display: table-row-group !important;
  }

  .print-frame > tbody > tr,
  .print-frame > thead > tr,
  .print-sheet > tbody > tr,
  .print-sheet > thead > tr {
    display: table-row !important;
  }

  .print-frame > tbody > tr > td,
  .print-frame > thead > tr > th,
  .print-sheet > tbody > tr > td,
  .print-sheet > thead > tr > th {
    display: table-cell !important;
  }

  .print-frame > thead.is-empty {
    display: none !important;
  }

  .print-frame th,
  .print-sheet th {
    text-align: left;
    font-family: var(--font-sans);
    font-size: 11pt;
    font-weight: 600;
    padding: 0 0 6px 0;
    border-bottom: 1px solid #999999;
  }

  .print-frame td,
  .print-sheet td {
    padding: 0;
  }

  /* "Print/export all sheets": script adds body.print-all while printing. */
  body.print-all .print-frame {
    display: none !important;
  }

  body.print-all .print-sheets {
    display: block !important;
  }

  .print-sheets {
    white-space: normal;
    width: 100%;
    flex: 1 1 auto;
  }

  .print-sheet {
    display: table !important;
    width: 100%;
  }

  .print-sheet + .print-sheet {
    break-before: page;
    page-break-before: always;
  }
}

/* Web version only (templates/web.html, static/web/web_shell.js): status
   line above the result while Python loads, hidden once everything is there. */
.web-status {
  padding: 6px 16px;
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--bg-header);
  border-bottom: 1px solid var(--border);
}
.web-status.is-hidden { display: none; }
.web-status.is-error { color: #8a1c13; background: #fdecea; }
@media print {
  .web-status { display: none !important; }
}

/* Note in the output area, e.g. "Example 'x' not found." (editor.js) */
.output-note {
  color: var(--text-secondary);
  font-style: italic;
}
