:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #182230;
  --muted: #667085;
  --line: #d7dde7;
  --soft: #eef3f7;
  --accent: #0f766e;
  --accent-dark: #0b5f59;
  --danger: #b42318;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.shell {
  width: min(1180px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0 36px;
}

.hero {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 16px;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  letter-spacing: 0;
}

h2 {
  font-size: 18px;
  letter-spacing: 0;
}

h3 {
  font-size: 16px;
  margin-bottom: 8px;
}

p {
  margin-top: 5px;
  color: var(--muted);
}

.flow {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.flow span {
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: #344054;
  font-weight: 650;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
  margin-top: 16px;
}

.grid {
  display: grid;
  grid-template-columns: minmax(0, 0.95fr) minmax(0, 1.05fr);
  gap: 18px;
  padding: 18px;
}

.block {
  min-width: 0;
}

.dropzone {
  display: grid;
  place-items: center;
  gap: 6px;
  min-height: 230px;
  margin-top: 14px;
  border: 2px dashed #aeb8c7;
  border-radius: 8px;
  background: #fafbfc;
  color: var(--muted);
  cursor: pointer;
  text-align: center;
  padding: 18px;
}

.dropzone:hover {
  border-color: var(--accent);
  background: #f2fbf9;
}

.dropzone input {
  display: none;
}

.dropzone strong {
  color: var(--ink);
  font-size: 18px;
}

.file-list {
  list-style: none;
  padding: 0;
  margin: 12px 0 0;
  display: grid;
  gap: 6px;
}

.file-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
}

.file-name {
  min-width: 0;
  flex: 1;
  overflow-wrap: anywhere;
}

.file-size {
  color: var(--muted);
  white-space: nowrap;
}

.remove-file {
  min-height: 30px;
  padding: 4px 9px;
  color: var(--danger);
  white-space: nowrap;
}

.remove-file:hover {
  border-color: #f3b8b0;
  background: #fff5f3;
}

textarea {
  width: 100%;
  min-height: 278px;
  margin-top: 14px;
  resize: vertical;
  border: 1px solid #cfd6e2;
  border-radius: 8px;
  padding: 12px;
  font: 14px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--ink);
}

textarea:focus {
  outline: 2px solid rgba(15, 118, 110, 0.18);
  border-color: var(--accent);
}

.actions {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 18px 18px;
}

.rules-port {
  margin: 0 18px 18px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.mode-port {
  margin: 0 18px 18px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.price-port {
  margin: 0 18px 18px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.mode-options {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.mode-option {
  min-height: 86px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
}

.mode-option:has(input:checked) {
  border-color: var(--accent);
  background: #f2fbf9;
}

.mode-option input {
  margin-top: 4px;
  accent-color: var(--accent);
}

.mode-option span {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.mode-option strong {
  color: var(--ink);
}

.mode-option small {
  color: var(--muted);
}

.price-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.field-control {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.field-control span {
  color: #344054;
  font-weight: 800;
}

.field-control input,
.field-control select {
  min-height: 42px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 9px 11px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

.field-control input:focus,
.field-control select:focus {
  outline: 2px solid rgba(15, 118, 110, 0.18);
  border-color: var(--accent);
}

.formula-preview {
  margin-top: 12px;
  border: 1px dashed #aeb8c7;
  border-radius: 8px;
  padding: 11px 12px;
  background: #fafbfc;
  color: #344054;
  font-weight: 700;
}

.rules-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 12px;
}

.rule-library {
  display: grid;
  grid-template-columns: minmax(260px, 0.9fr) minmax(0, 1.4fr);
  gap: 12px;
  margin-bottom: 12px;
}

.rule-section-title {
  margin-bottom: 8px;
  color: #344054;
  font-weight: 800;
}

.rule-list {
  display: grid;
  gap: 8px;
}

.rule-option {
  width: 100%;
  min-height: 84px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px;
  text-align: left;
  background: #fff;
}

.rule-option.active {
  border-color: var(--accent);
  background: #f2fbf9;
}

.rule-dot {
  width: 14px;
  height: 14px;
  flex: 0 0 auto;
  margin-top: 4px;
  border: 2px solid #98a2b3;
  border-radius: 999px;
}

.rule-option.active .rule-dot {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 3px #fff;
  background: var(--accent);
}

.rule-option-text {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.rule-option-text strong,
.rule-option-text small,
.rule-option-text em {
  overflow-wrap: anywhere;
}

.rule-option-text small {
  color: var(--muted);
  font-weight: 500;
}

.rule-option-text em {
  color: #475467;
  font-size: 12px;
  font-style: normal;
  font-weight: 600;
}

.rule-empty {
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 14px;
  color: var(--muted);
  background: #fafbfc;
}

.rule-empty.error {
  color: var(--danger);
}

.rule-preview {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.rule-preview-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.rule-preview-tools {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.rule-preview pre {
  max-height: 360px;
  margin: 0;
  overflow: auto;
  padding: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: #344054;
  background: #fafbfc;
}

.rule-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafbfc;
}

.secondary-link,
.rule-upload {
  min-height: 40px;
  border-radius: 7px;
  border: 1px solid var(--line);
  padding: 9px 14px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}

.secondary-link:hover,
.rule-upload:hover {
  background: var(--soft);
}

.rule-upload input {
  display: none;
}

.rule-name-input {
  min-height: 40px;
  width: min(220px, 100%);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 9px 12px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

.rule-name-input:focus {
  outline: 2px solid rgba(15, 118, 110, 0.18);
  border-color: var(--accent);
}

#ruleStatus {
  color: var(--muted);
}

button,
.download {
  min-height: 40px;
  border-radius: 7px;
  border: 1px solid var(--line);
  padding: 9px 14px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}

button.primary,
.download {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

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

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

#statusText {
  color: var(--muted);
}

#statusText.error {
  color: var(--danger);
  font-weight: 700;
}

.hidden {
  display: none;
}

.result-panel {
  padding: 18px;
}

.result-head {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  padding-bottom: 14px;
}

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

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

.card .label {
  color: var(--muted);
  font-size: 13px;
}

.card .value {
  margin-top: 5px;
  font-size: 18px;
  font-weight: 800;
  word-break: break-word;
}

.output-list {
  margin-top: 18px;
}

.output-list ul {
  margin: 0;
  padding-left: 20px;
}

.output-list li {
  margin: 5px 0;
}

.output-list a {
  color: var(--accent-dark);
  font-weight: 700;
  text-decoration: none;
}

.output-list a:hover {
  text-decoration: underline;
}

.output-preview {
  margin-top: 18px;
  border-top: 1px solid var(--line);
  padding-top: 16px;
}

.preview-heading {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
}

.preview-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.preview-tab {
  min-height: 38px;
  padding: 8px 12px;
  border-color: var(--line);
  background: #fff;
}

.preview-tab.active {
  border-color: var(--accent);
  background: #f2fbf9;
  color: var(--accent-dark);
}

.preview-body {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}

.preview-doc-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--line);
  background: #fafbfc;
}

.preview-doc-head h4,
.preview-section h5 {
  margin: 0;
}

.preview-doc-head h4 {
  font-size: 17px;
}

.preview-sections {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  padding: 14px;
}

.preview-section h5 {
  margin-bottom: 8px;
  color: #344054;
  font-size: 14px;
}

.preview-items {
  padding: 0 14px 14px;
}

.table-scroll {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.kv-table,
.items-table {
  width: 100%;
  border-collapse: collapse;
}

.kv-table th,
.kv-table td,
.items-table th,
.items-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px 10px;
  vertical-align: top;
}

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

.kv-table th {
  width: 36%;
  color: var(--muted);
  font-weight: 700;
  text-align: left;
}

.kv-table td {
  overflow-wrap: anywhere;
}

.items-table {
  min-width: 760px;
}

.items-table th {
  background: #f2f5f8;
  color: #344054;
  font-size: 13px;
  text-align: left;
  white-space: nowrap;
}

.items-table td:nth-child(1),
.items-table td:nth-child(3),
.items-table td:nth-child(4),
.items-table td:nth-child(5),
.items-table td:nth-child(6) {
  white-space: nowrap;
}

.preview-empty {
  padding: 14px;
  color: var(--muted);
}

@media (max-width: 860px) {
  .hero,
  .result-head,
  .rules-head,
  .rule-preview-head,
  .preview-heading,
  .preview-doc-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .grid,
  .rule-library,
  .mode-options,
  .price-grid,
  .summary-grid,
  .preview-sections {
    grid-template-columns: 1fr;
  }

  .flow {
    justify-content: flex-start;
  }

}
