:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-strong: #eef3f8;
  --text: #17212b;
  --muted: #667586;
  --border: #d8e0ea;
  --accent: #1769aa;
  --accent-dark: #0f4d82;
  --good: #147a54;
  --warn: #a15c00;
  --bad: #ad3030;
  --shadow: 0 14px 40px rgba(26, 39, 54, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Arial, sans-serif;
  font-size: 14px;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  width: min(1760px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 28px 0 40px;
}

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

h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.15;
  font-weight: 700;
  letter-spacing: 0;
}

p {
  margin: 8px 0 0;
  color: var(--muted);
}

.status-pill {
  min-width: 190px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--muted);
  text-align: center;
  font-size: 13px;
  box-shadow: var(--shadow);
}

.status-pill.ok {
  border-color: rgba(20, 122, 84, 0.25);
  color: var(--good);
}

.status-pill.error {
  border-color: rgba(173, 48, 48, 0.25);
  color: var(--bad);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 14px;
}

.field {
  display: grid;
  gap: 7px;
}

label {
  color: #344252;
  font-size: 13px;
  font-weight: 600;
}

input {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 9px 11px;
  background: #fff;
  color: var(--text);
  outline: none;
}

select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 9px 11px;
  background: #fff;
  color: var(--text);
  outline: none;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(23, 105, 170, 0.12);
}

.actions {
  display: flex;
  gap: 10px;
}

.align-end {
  align-items: end;
}

button {
  min-height: 40px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  padding: 9px 14px;
  cursor: pointer;
  font-weight: 650;
}

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

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

button.secondary {
  background: #fff;
  color: var(--accent);
}

button.secondary:hover {
  background: #edf5fb;
}

.upload-grid {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) minmax(260px, 1fr) 220px;
  gap: 14px;
}

.dropzone {
  display: grid;
  align-content: center;
  min-height: 88px;
  border: 1px dashed #aab8c7;
  border-radius: 8px;
  padding: 16px;
  cursor: pointer;
  background: #fbfdff;
}

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

.dropzone.drag-over {
  border-color: var(--accent);
  background: #eaf4fc;
  box-shadow: inset 0 0 0 1px rgba(23, 105, 170, 0.25);
}

.dropzone input {
  display: none;
}

.drop-title {
  font-weight: 700;
  margin-bottom: 6px;
}

.drop-hint {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.run-box {
  display: grid;
  align-content: center;
  gap: 10px;
}

.action-panel {
  display: grid;
  gap: 14px;
}

.action-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
}

h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.25;
  font-weight: 700;
  letter-spacing: 0;
}

.write-status {
  min-width: 260px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #f8fbfe;
  color: #334155;
  padding: 9px 11px;
  font-size: 13px;
  text-align: right;
}

.action-grid {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(220px, 1fr) 180px;
  gap: 14px;
  align-items: end;
}

.check-line {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  color: #344252;
}

.check-line input {
  width: 16px;
  min-height: 16px;
}

.action-warning {
  border: 1px solid rgba(161, 92, 0, 0.25);
  border-radius: 6px;
  background: #fff8ea;
  color: #744300;
  padding: 10px 12px;
  font-size: 13px;
}

.action-plan {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 10px;
}

.plan-item {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #f8fbfe;
  padding: 10px 12px;
}

.plan-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 5px;
}

.plan-item strong {
  display: block;
  color: var(--text);
  font-size: 18px;
  line-height: 1.2;
}

.plan-item.warn {
  border-color: rgba(161, 92, 0, 0.25);
  background: #fff8ea;
}

.inventory-preview {
  display: grid;
  gap: 10px;
}

.inventory-preview:empty {
  display: none;
}

.inventory-preview-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.inventory-preview-head strong {
  display: block;
  color: var(--text);
}

.inventory-preview-head span {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.inventory-preview-table {
  max-height: 320px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.inventory-preview-table table {
  min-width: 980px;
  table-layout: auto;
}

.inventory-preview-table th,
.inventory-preview-table td {
  padding: 8px 10px;
}

.inventory-preview-table th:nth-child(n),
.inventory-preview-table td:nth-child(n) {
  width: auto;
}

.mini-badge {
  display: inline-block;
  border-radius: 999px;
  padding: 3px 7px;
  font-size: 11px;
  font-weight: 700;
  background: #eef3f8;
  color: #405065;
}

.mini-badge.ok {
  background: #e5f5ee;
  color: var(--good);
}

.mini-badge.warn {
  background: #fff4df;
  color: var(--warn);
}

.mini-badge.bad {
  background: #fff0f0;
  color: var(--bad);
}

.operation-log {
  display: grid;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f8fbfe;
  padding: 12px;
}

.operation-log[hidden] {
  display: none;
}

.operation-log.done {
  border-color: rgba(20, 122, 84, 0.24);
  background: #f3fbf7;
}

.operation-log.error {
  border-color: rgba(173, 48, 48, 0.24);
  background: #fff7f7;
}

.operation-log-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.operation-log-head strong,
.operation-log-head span {
  display: block;
}

.operation-log-head strong {
  color: var(--text);
  font-size: 14px;
}

.operation-log-head span {
  margin-top: 3px;
  color: var(--muted);
  font-size: 13px;
}

.operation-spinner {
  width: 22px;
  height: 22px;
  flex: 0 0 22px;
  border: 3px solid #d6e5f1;
  border-top-color: var(--accent);
  border-radius: 999px;
  animation: spin 0.85s linear infinite;
}

.operation-log:not(.running) .operation-spinner {
  display: none;
}

.operation-steps {
  display: grid;
  gap: 8px;
}

.operation-step {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  gap: 8px;
  align-items: start;
}

.operation-step-icon {
  position: relative;
  width: 18px;
  height: 18px;
  margin-top: 1px;
  border: 2px solid var(--border);
  border-radius: 999px;
  background: #fff;
}

.operation-step.running .operation-step-icon {
  border-color: #d6e5f1;
  border-top-color: var(--accent);
  animation: spin 0.85s linear infinite;
}

.operation-step.done .operation-step-icon {
  border-color: var(--good);
  background: var(--good);
}

.operation-step.done .operation-step-icon::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 1px;
  width: 5px;
  height: 9px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.operation-step.error .operation-step-icon {
  border-color: var(--bad);
  background: var(--bad);
}

.operation-step.error .operation-step-icon::before,
.operation-step.error .operation-step-icon::after {
  content: "";
  position: absolute;
  left: 7px;
  top: 3px;
  width: 2px;
  height: 9px;
  background: #fff;
}

.operation-step.error .operation-step-icon::before {
  transform: rotate(45deg);
}

.operation-step.error .operation-step-icon::after {
  transform: rotate(-45deg);
}

.operation-step strong,
.operation-step span {
  display: block;
}

.operation-step strong {
  color: #263648;
  font-size: 13px;
  line-height: 1.35;
}

.operation-step span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.primary-action {
  min-height: 48px;
  width: 100%;
  font-size: 16px;
}

.advanced-actions {
  border-top: 1px solid var(--border);
  padding-top: 2px;
}

.advanced-actions summary {
  width: max-content;
  cursor: pointer;
  color: var(--accent-dark);
  font-weight: 700;
  padding: 8px 0;
}

.advanced-actions[open] {
  display: grid;
  gap: 12px;
}

.enter-mode-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.enter-mode-row .check-line {
  flex: 1 1 360px;
}

.preview-summary {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #f8fbfe;
  color: #334155;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.45;
}

.action-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.table-toolbar {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tab {
  min-height: 34px;
  background: #fff;
  border-color: var(--border);
  color: #334155;
  padding: 6px 10px;
  font-size: 13px;
  font-weight: 600;
}

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

.search {
  max-width: 340px;
  min-height: 34px;
}

.table-wrap {
  max-height: calc(100vh - 360px);
  min-height: 360px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  min-width: 1560px;
  table-layout: fixed;
}

th:nth-child(1),
td:nth-child(1) {
  width: 125px;
}

th:nth-child(2),
td:nth-child(2) {
  width: 240px;
}

th:nth-child(3),
td:nth-child(3) {
  width: 315px;
}

th:nth-child(4),
td:nth-child(4) {
  width: 245px;
}

th:nth-child(5),
td:nth-child(5) {
  width: 355px;
}

th:nth-child(6),
td:nth-child(6) {
  width: 280px;
}

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

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel-strong);
  color: #273545;
  font-size: 13px;
  font-weight: 700;
}

td {
  background: #fff;
  line-height: 1.35;
}

tr:hover td {
  background: #f8fbfe;
}

.empty {
  height: 160px;
  text-align: center;
  color: var(--muted);
  vertical-align: middle;
}

.badge {
  display: inline-block;
  white-space: nowrap;
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 700;
  background: #eef3f8;
  color: #405065;
}

.badge.full_match {
  background: #e5f5ee;
  color: var(--good);
}

.badge.found_by_code_set_external,
.badge.found_by_external_set_code,
.badge.found_by_code_no_tilda,
.badge.found_by_identity_set_code,
.badge.found_by_identity_set_external,
.badge.found_by_identity_set_both {
  background: #eaf4fc;
  color: var(--accent-dark);
}

.badge.found_by_code_overwrite_external,
.badge.found_by_external_overwrite_code {
  background: #fff4df;
  color: var(--warn);
}

.badge.ambiguous_tilda,
.badge.duplicate_moysklad_code,
.badge.duplicate_moysklad_external,
.badge.conflict_code_external,
.badge.identity_conflict,
.badge.ambiguous_moysklad_identity,
.badge.batch_identity_collision,
.badge.no_stock_code {
  background: #fff4df;
  color: var(--warn);
}

.badge.create_new,
.badge.no_tilda_match {
  background: #f1f5f9;
  color: #334155;
}

.sub {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}

.mapping-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
}

.mapping-note {
  display: inline-flex;
  padding: 3px 7px;
  border-radius: 999px;
  background: #eef7f0;
  color: #28623b;
  font-size: 11px;
  line-height: 1.3;
}

.mapping-controls button {
  width: auto;
  min-height: 0;
  padding: 4px 7px;
  font-size: 11px;
}

.mt-6 {
  margin-top: 6px;
}

.tilda-choice {
  width: min(360px, 100%);
  min-height: 34px;
  font-size: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.manual-match {
  min-width: 0;
  min-height: 34px;
  font-size: 13px;
  padding: 7px 9px;
}

.manual-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 34px;
  width: 100%;
}

.manual-control .manual-match {
  border-radius: 6px 0 0 6px;
}

.manual-control.invalid .manual-match,
.manual-control.invalid .manual-open {
  border-color: #c77220;
  background: #fff9f1;
}

.manual-warning {
  color: #9a4f10;
  margin-top: 4px;
}

.manual-open {
  min-height: 34px;
  border-color: var(--border);
  border-left: 0;
  border-radius: 0 6px 6px 0;
  background: #fff;
  color: #344252;
  padding: 0;
  font-size: 13px;
}

.manual-open:hover {
  border-color: var(--accent);
  background: #edf5fb;
  color: var(--accent-dark);
}

.autocomplete-panel {
  position: fixed;
  z-index: 50;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 48px rgba(26, 39, 54, 0.18);
}

.autocomplete-panel[hidden] {
  display: none;
}

.autocomplete-option {
  display: block;
  width: 100%;
  min-height: auto;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: #fff;
  color: var(--text);
  padding: 9px 11px;
  text-align: left;
  font-weight: 500;
}

.autocomplete-option:hover,
.autocomplete-option.active {
  background: #eaf4fc;
  color: var(--text);
}

.autocomplete-option:last-child {
  border-bottom: 0;
}

.autocomplete-name,
.autocomplete-meta {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.autocomplete-name {
  font-size: 13px;
  line-height: 1.25;
}

.autocomplete-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 400;
}

.autocomplete-empty {
  padding: 12px;
  color: var(--muted);
  font-size: 13px;
}

.app-dialog {
  width: min(620px, calc(100vw - 32px));
  border: 0;
  border-radius: 8px;
  padding: 0;
  background: transparent;
  color: var(--text);
}

.app-dialog::backdrop {
  background: rgba(15, 23, 42, 0.42);
}

.dialog-card {
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(15, 23, 42, 0.22);
}

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

.dialog-head h3 {
  margin: 0;
  font-size: 18px;
  line-height: 1.25;
  letter-spacing: 0;
}

.dialog-close {
  width: 34px;
  min-height: 34px;
  border-color: var(--border);
  background: #fff;
  color: #334155;
  padding: 0;
  font-size: 20px;
  line-height: 1;
}

.dialog-close:hover {
  background: #edf5fb;
  color: var(--accent-dark);
}

.dialog-body {
  max-height: min(52vh, 440px);
  overflow: auto;
  padding: 16px 18px;
  color: #263648;
  line-height: 1.5;
  white-space: normal;
}

.dialog-body p {
  margin: 0 0 10px;
  color: #263648;
}

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

.dialog-summary {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.dialog-summary div {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  border-bottom: 1px solid #edf2f7;
  padding-bottom: 7px;
}

.dialog-summary div:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.dialog-summary span {
  color: var(--muted);
}

.dialog-summary strong {
  text-align: right;
}

.dialog-warning {
  border: 1px solid #f5d590;
  border-radius: 6px;
  background: #fff8ea;
  padding: 8px 10px;
  color: #744300;
}

.error-groups {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.error-groups div {
  display: grid;
  gap: 3px;
  border: 1px solid #f5d590;
  border-radius: 6px;
  background: #fff8ea;
  padding: 8px 10px;
}

.error-groups strong {
  color: var(--warn);
}

.error-groups span {
  color: #344252;
}

.error-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.error-list div {
  display: grid;
  gap: 3px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  padding: 8px 10px;
}

.error-list strong {
  color: var(--text);
}

.error-list span {
  color: var(--muted);
  font-size: 12px;
}

.error-list em {
  color: var(--bad);
  font-size: 12px;
  font-style: normal;
}

.dialog-meta {
  border-top: 1px solid var(--border);
  background: #fff8ea;
  color: #744300;
  padding: 10px 18px;
  font-size: 13px;
  line-height: 1.45;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  border-top: 1px solid var(--border);
  padding: 14px 18px 16px;
  background: #fff;
}

@media (max-width: 900px) {
  .app-shell {
    width: min(100vw - 24px, 900px);
    padding-top: 18px;
  }

  .topbar,
  .table-toolbar {
    display: grid;
  }

  .grid-3,
  .upload-grid,
  .action-grid {
    grid-template-columns: 1fr;
  }

  .action-header {
    display: grid;
  }

  .write-status {
    min-width: 0;
    text-align: left;
  }

  .search {
    max-width: none;
  }
}
