:root {
  color-scheme: light;
  --bg: #f5f7f8;
  --surface: #ffffff;
  --surface-2: #eef4f1;
  --ink: #17211f;
  --muted: #62706c;
  --line: #d9e2df;
  --primary: #098960;
  --primary-dark: #056645;
  --accent: #245a82;
  --danger: #b23a3a;
  --warning: #fff5df;
  --shadow: 0 12px 36px rgba(20, 34, 31, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.5;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  padding: 0 12px;
}

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

button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

button.primary:hover {
  background: var(--primary-dark);
}

button.danger {
  color: var(--danger);
}

input,
select {
  width: 100%;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  color: var(--ink);
  padding: 0 10px;
}

textarea {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  color: var(--ink);
  padding: 6px 10px;
}

.clearable-field {
  position: relative;
  display: block;
  width: 100%;
}

.clearable-field input,
.clearable-field textarea {
  padding-right: 34px;
}

.clear-input {
  position: absolute;
  top: 50%;
  right: 7px;
  transform: translateY(-50%);
  display: none;
  width: 20px;
  height: 20px;
  min-height: 0;
  border: 0;
  border-radius: 50%;
  background: #e7efec;
  color: var(--muted);
  line-height: 1;
  padding: 0;
}

.clear-input.visible {
  display: inline-grid;
  place-items: center;
}

.clear-input:hover {
  background: #d7e5df;
  color: var(--ink);
}

.clearable-field textarea ~ .clear-input {
  top: 18px;
  transform: none;
}

.link-button {
  min-height: auto;
  width: max-content;
  border: 0;
  background: transparent;
  color: var(--primary);
  padding: 0;
  font-size: 12px;
  text-decoration: underline;
}

textarea {
  resize: vertical;
}

label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
  min-width: 0;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 9px;
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

pre {
  overflow: auto;
  border-radius: 8px;
  background: #111c19;
  color: #d8f7e8;
  padding: 16px;
  min-height: 240px;
}

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: end;
  padding: 28px 32px 20px;
  background: linear-gradient(180deg, #ffffff 0%, #edf5f2 100%);
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--primary);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
h4,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(28px, 5vw, 44px);
  letter-spacing: 0;
}

h2 {
  margin-bottom: 4px;
  font-size: 20px;
}

h3 {
  font-size: 16px;
}

.header-actions {
  display: grid;
  grid-template-columns: minmax(220px, 1.6fr) 110px 90px;
  gap: 12px;
  width: min(620px, 100%);
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  gap: 20px;
  padding: 24px 32px;
}

.workspace,
.results-panel,
.tabs {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.workspace {
  padding: 18px;
}

.results-panel {
  position: sticky;
  top: 16px;
  align-self: start;
  padding: 18px;
}

.toolbar,
.panel-heading,
.section-heading,
.subsection-head,
.product-card-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
}

.toolbar p {
  margin-bottom: 0;
  color: var(--muted);
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.product-list {
  display: grid;
  gap: 14px;
  margin-top: 16px;
}

.product-card,
.line-card,
.history-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdfc;
}

.product-card {
  padding: 16px;
}

.product-title {
  font-size: 16px;
}

.product-grid,
.form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 18px 12px;
  margin-top: 14px;
  align-items: start;
}

.cn-picker {
  position: relative;
  display: grid;
  gap: 6px;
  min-width: 0;
}

.cn-picker .dropdown {
  position: absolute;
  z-index: 20;
  top: calc(100% + 2px);
  left: 0;
  right: 0;
  display: none;
  max-height: 260px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  box-shadow: var(--shadow);
}

.cn-picker .dropdown.show {
  display: grid;
}

.cn-picker .option {
  display: grid;
  min-height: 0;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 8px 10px;
  text-align: left;
}

.cn-picker .option:hover {
  background: var(--surface-2);
}

.cn-picker .option strong {
  color: var(--primary-dark);
}

.cn-picker .option span,
.match-hint,
.no-result {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
}

.match-hint {
  display: block;
  min-height: 15px;
  overflow-wrap: anywhere;
}

.no-result {
  padding: 10px;
}

.product-grid.compact {
  grid-template-columns: repeat(3, minmax(0, 160px));
}

.checkline {
  align-content: end;
  grid-template-columns: 18px auto;
  color: var(--ink);
}

.checkline input {
  width: auto;
}

.cement-mix,
.precursors {
  margin-top: 16px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.precursor-list {
  display: grid;
  gap: 12px;
  margin-top: 12px;
  overflow-x: visible;
}

.precursor-node {
  display: grid;
  min-width: 0;
}

.precursor-row {
  display: grid;
  grid-template-columns:
    minmax(0, 1.1fr)
    minmax(0, 0.82fr)
    minmax(0, 0.96fr)
    minmax(0, 0.58fr)
    minmax(0, 0.82fr)
    minmax(0, 0.82fr)
    minmax(0, 1fr)
    minmax(0, 0.68fr);
  gap: 8px;
  align-items: start;
  width: 100%;
  min-width: 0;
  padding: 10px;
  border-radius: 8px;
  background: var(--surface-2);
}

.precursor-row > * {
  min-width: 0;
}

.precursor-row label {
  gap: 3px;
  font-size: 11px;
}

.precursor-row input,
.precursor-row select {
  min-width: 0;
  height: 34px;
  padding-left: 8px;
  padding-right: 8px;
  font-size: 13px;
}

.precursor-relation {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.precursor-level {
  display: inline-grid;
  width: max-content;
  min-width: 32px;
  height: 22px;
  place-items: center;
  border-radius: 999px;
  background: var(--primary);
  color: white;
  font-size: 12px;
  line-height: 1;
}

.precursor-path {
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.25;
}

.precursor-parent,
.precursor-child-count {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.precursor-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 0.72fr);
  gap: 4px;
  align-items: start;
  padding-top: 18px;
  white-space: nowrap;
}

.precursor-actions button {
  min-width: 0;
  min-height: 34px;
  overflow: hidden;
  padding: 0 5px;
  font-size: 12px;
  text-overflow: ellipsis;
}

.precursor-children {
  display: none;
}

.precursor-node[data-depth="1"] > .precursor-row,
.precursor-node[data-depth="3"] > .precursor-row,
.precursor-node[data-depth="5"] > .precursor-row {
  background: #f6faf8;
}

.precursor-node[data-depth="2"] > .precursor-row,
.precursor-node[data-depth="4"] > .precursor-row {
  background: #eef6f3;
}

.precursor-node[data-depth="1"] .precursor-level,
.precursor-node[data-depth="4"] .precursor-level {
  background: var(--accent);
}

.precursor-node[data-depth="2"] .precursor-level,
.precursor-node[data-depth="5"] .precursor-level {
  background: #6f5b13;
}

.precursor-node[data-depth="3"] .precursor-level {
  background: #7f3f56;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 14px 0;
}

.summary-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #f8fbfa;
}

.summary-card span,
.summary-card small {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.summary-card strong {
  display: block;
  margin: 3px 0;
  color: var(--primary-dark);
  font-size: 24px;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.validation {
  border: 1px solid #c8e2d7;
  border-radius: 8px;
  background: #effaf5;
  color: #18533e;
  padding: 10px 12px;
  margin: 12px 0 18px;
}

.validation.has-errors {
  border-color: #efc3aa;
  background: var(--warning);
  color: #7d3c16;
}

.validation p:last-child {
  margin-bottom: 0;
}

.line-results {
  display: grid;
  gap: 10px;
}

.line-card {
  padding: 12px;
}

.line-card span,
.history-card span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.line-card dl {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 12px;
  margin: 10px 0 0;
}

.line-card dt {
  color: var(--muted);
  font-size: 12px;
}

.line-card dd {
  margin: 0;
  font-weight: 700;
}

.tabs {
  margin: 0 32px 32px;
  overflow: hidden;
}

.tab-buttons {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--line);
  background: #f4f8f6;
}

.tab-button {
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  padding: 12px 16px;
}

.tab-button.active {
  background: white;
  color: var(--primary-dark);
  font-weight: 800;
}

.tab-panel {
  display: none;
  padding: 18px;
}

.tab-panel.active {
  display: block;
}

.data-layout {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 18px;
}

.editor-form {
  display: grid;
  gap: 10px;
  align-self: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: #fbfdfc;
}

.editor-form .form-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.data-table-wrap {
  overflow: auto;
}

.filters {
  margin-bottom: 10px;
}

.annual-editor {
  margin-top: 22px;
}

.annual-editor input {
  min-width: 110px;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 16px;
  align-items: center;
  padding: 12px;
}

.empty {
  color: var(--muted);
}

.hidden {
  display: none !important;
}

@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .results-panel {
    position: static;
  }

  .precursor-actions {
    padding-top: 24px;
  }

  .data-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 760px) {
  .app-header,
  .toolbar,
  .panel-heading,
  .section-heading,
  .product-card-head {
    align-items: stretch;
    flex-direction: column;
  }

  .app-header,
  .layout {
    padding-left: 16px;
    padding-right: 16px;
  }

  .tabs {
    margin-left: 16px;
    margin-right: 16px;
  }

  .header-actions,
  .product-grid,
  .product-grid.compact,
  .form-grid,
  .editor-form .form-grid,
  .summary-grid,
  .history-card {
    grid-template-columns: 1fr;
  }

  .tab-buttons {
    overflow-x: auto;
  }
}
