:root {
  --bg: #f3f5f2;
  --panel: #ffffff;
  --panel-soft: #f8faf7;
  --ink: #17221d;
  --muted: #647067;
  --line: #d7dfd8;
  --line-strong: #c6d0c7;
  --accent: #21695d;
  --accent-hover: #19564c;
  --accent-soft: #e6f0ec;
  --accent-2: #2f7d48;
  --danger: #b42318;
  --shadow: 0 14px 36px rgba(32, 48, 40, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", Arial, sans-serif;
  scroll-behavior: smooth;
}

.app {
  width: min(1640px, calc(100% - clamp(64px, 10vw, 256px)));
  margin: clamp(18px, 2.4vw, 36px) auto 56px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: clamp(12px, 1.4vw, 20px);
  box-shadow: var(--shadow);
}

.intro {
  margin-bottom: 16px;
  background: var(--panel-soft);
}

h1, h2 {
  margin: 0 0 10px;
  letter-spacing: 0;
  text-wrap: balance;
}

h1 {
  font-size: clamp(26px, 2vw, 36px);
  line-height: 1.12;
}

h2 { font-size: 20px; }

p {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
  max-width: 68ch;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 16px;
}

.tab {
  color: var(--ink);
  background: #e8eee9;
}

.tab.active {
  background: var(--accent);
  color: #fff;
}

.tab:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.tab-page { display: none; }
.tab-page.active { display: block; }

.input-panel { margin-bottom: 16px; }
label { display: block; font-weight: 700; margin-bottom: 8px; }

textarea {
  width: 100%;
  min-height: 92px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  font: 18px/1.5 Consolas, "Courier New", monospace;
  color: var(--ink);
}

.text-input,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
  font: 16px/1.4 Consolas, "Courier New", monospace;
  color: var(--ink);
  background: #fff;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 12px 0;
}

button {
  border: 0;
  border-radius: 6px;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
  min-height: 40px;
  transition: background 160ms ease, transform 160ms ease, box-shadow 160ms ease, opacity 160ms ease;
}

button:hover:not(:disabled) {
  background: var(--accent-hover);
  box-shadow: 0 8px 18px rgba(33, 105, 93, 0.16);
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:focus-visible {
  outline: 3px solid rgba(33, 105, 93, 0.24);
  outline-offset: 2px;
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border-radius: 6px;
  padding: 10px 14px;
  color: var(--ink);
  background: #e9eee9;
  font-weight: 700;
  text-decoration: none;
  transition: background 160ms ease, transform 160ms ease, box-shadow 160ms ease;
}

.button-link:hover {
  background: #dfe8e2;
}

.button-link:focus-visible {
  outline: 3px solid rgba(33, 105, 93, 0.24);
  outline-offset: 2px;
}

button.ghost {
  color: var(--ink);
  background: #e9eee9;
}

button.ghost:hover:not(:disabled) {
  background: #dfe8e2;
}

.compact { margin: 10px 0; }

.formula-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 10px 0 12px;
}

.tool-hint {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
}

button.mini {
  min-width: 42px;
  padding: 8px 10px;
}

.pretty-box {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
  padding: 12px;
  margin: 10px 0 12px;
}

.pretty-label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 8px;
}

.pretty-formula {
  min-height: 32px;
  font: 22px/1.5 Consolas, "Courier New", monospace;
  color: var(--ink);
  overflow-x: auto;
  white-space: nowrap;
}

.hint {
  font-size: 14px;
}

.image-panel {
  display: grid;
  grid-template-columns: minmax(240px, 320px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
  margin-bottom: 16px;
}

input[type="file"] {
  width: 100%;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
}

.image-preview {
  min-height: 120px;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  display: grid;
  place-items: center;
  color: var(--muted);
  overflow: hidden;
  background: #fff;
}

.preview-stage {
  position: relative;
  max-width: 100%;
}

.image-preview img {
  display: block;
  max-width: 100%;
  max-height: 260px;
  object-fit: contain;
}

.bar-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.bar-overlay line {
  stroke: var(--accent);
  stroke-width: 3;
}

.bar-overlay text {
  fill: var(--accent);
  font: 700 14px "Segoe UI", Arial, sans-serif;
}

.ocr-status {
  margin-top: 10px;
  font-size: 14px;
  color: var(--accent-2);
}

.ocr-result {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  background: var(--panel-soft);
  color: var(--ink);
  display: grid;
  gap: 8px;
}

.ocr-result code {
  font: 15px/1.4 Consolas, "Courier New", monospace;
}

.ocr-pretty {
  font: 18px/1.5 Consolas, "Courier New", monospace;
}

.ocr-warning,
.ocr-warnings {
  color: var(--danger);
}

.ocr-warnings {
  margin: 4px 0 0;
  padding-left: 20px;
}

.ocr-actions {
  margin-top: 4px;
}

.export-preview {
  margin-top: 16px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #e9eee9;
  padding: 18px;
}

.export-sheet {
  width: min(1180px, 100%);
  min-height: 820px;
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.export-sheet section + section {
  margin-top: 22px;
}

.export-sheet h3 {
  margin: 0 0 10px;
  font-size: 18px;
}

.export-formula {
  font: 18px/1.5 Consolas, "Courier New", monospace;
  margin: 6px 0;
}

.export-sheet svg {
  min-width: 0;
  max-width: 100%;
  height: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 460px), 1fr));
  gap: 16px;
  margin-bottom: 16px;
}

.scheme-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.scheme-card {
  min-width: 0;
}

.scheme-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.scheme-head h2 {
  margin: 0;
}

.inline-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.inline-control select {
  width: auto;
  min-width: 132px;
  font: 14px/1.4 "Segoe UI", Arial, sans-serif;
  font-weight: 700;
}

.bs-variants {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.bs-variants h3 {
  margin: 0 0 8px;
  font-size: 16px;
}

.formulas {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 10px;
  margin: 0;
}

dt {
  color: var(--muted);
  font-weight: 700;
}

dd {
  margin: 0;
  font: 18px/1.45 Consolas, "Courier New", monospace;
}

.derivation {
  display: grid;
  gap: 10px;
  font: 18px/1.45 Consolas, "Courier New", monospace;
}

.derivation-line {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr);
  align-items: start;
}

.derivation-left {
  color: var(--muted);
  font-weight: 700;
}

.scheme {
  width: 100%;
  height: clamp(300px, 44vh, 540px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
  display: grid;
  place-items: center;
}

.scheme svg {
  width: 100%;
  height: 100%;
  min-width: 0;
}

svg {
  display: block;
  max-width: 100%;
}

.error {
  margin-bottom: 16px;
  border: 1px solid #fecdca;
  background: #fff1f0;
  color: var(--danger);
  border-radius: 8px;
  padding: 12px 14px;
}

.hidden { display: none; }

.bar-layer {
  display: inline-block;
  border-top: 2px solid currentColor;
  padding-top: 3px;
  margin-top: 2px;
  line-height: 1.05;
  vertical-align: middle;
}

.bar-layer .bar-layer {
  margin-top: 4px;
}

.op {
  display: inline-block;
  margin: 0 0.38em;
}

@media (max-width: 860px) {
  .intro, .image-panel {
    grid-template-columns: 1fr;
    display: grid;
  }
}

@media (max-width: 560px) {
  .app {
    width: calc(100% - 20px);
    margin-top: 10px;
  }

  h1 { font-size: 24px; }

  h2 { font-size: 18px; }

  .tabs {
    gap: 6px;
  }

  .tab {
    flex: 1 1 calc(50% - 6px);
    padding: 9px 10px;
  }

  textarea {
    min-height: 78px;
    font-size: 16px;
  }

  .formula-tools button {
    flex: 1 1 auto;
  }

  .tool-hint {
    flex-basis: 100%;
  }

  .pretty-formula,
  .ocr-pretty,
  dd,
  .export-formula,
  .derivation {
    font-size: 16px;
  }

  .formulas {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  .scheme {
    height: 300px;
  }

  .bs-variants {
    grid-template-columns: 1fr;
  }

  .export-preview {
    padding: 10px;
  }

  .export-sheet {
    min-height: 0;
    padding: 14px;
  }
}
