:root {
  --surface: #ffffff;
  --muted: #f7f7f8;
  --ink: #101114;
  --subtle: #6b7280;
  --line: #d6d9df;
  --accent: #002fa7;
  --danger: #e4002b;
  --good: #127a3a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--surface);
  color: var(--ink);
  font-family: "Helvetica Neue", Arial, "Microsoft YaHei", sans-serif;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

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

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

select,
input {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
  background: #fff;
}

label {
  display: grid;
  gap: 6px;
  color: var(--subtle);
  font-size: 13px;
}

label input,
label select {
  color: var(--ink);
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto auto 1fr;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: end;
  padding: 24px 28px 18px;
  border-bottom: 1px solid var(--ink);
}

.topbar h1 {
  margin: 0;
  font-size: 34px;
  line-height: 1;
  font-weight: 700;
}

.folio {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
}

.container-switch {
  display: grid;
  grid-template-columns: 44px 220px;
  align-items: center;
  gap: 10px;
}

.mode-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr));
  border-bottom: 1px solid var(--ink);
}

.tab {
  height: 44px;
  border: 0;
  border-right: 1px solid var(--ink);
  border-radius: 0;
  font-weight: 700;
}

.tab:last-child {
  border-right: 0;
}

.tab.is-active {
  background: var(--ink);
  color: #fff;
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(540px, 38vw) 1fr;
}

.input-panel {
  min-width: 0;
  border-right: 1px solid var(--ink);
  background: var(--muted);
  overflow: auto;
}

.mode-panel {
  display: none;
  padding: 20px;
}

.mode-panel.is-active {
  display: block;
}

.panel-heading {
  border-bottom: 1px solid var(--line);
  padding-bottom: 14px;
  margin-bottom: 16px;
}

.panel-heading h2,
.result-panel h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.35;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.preset-section {
  border: 1px solid var(--line);
  background: #fff;
  margin-bottom: 16px;
}

.shared-preset {
  margin: 20px 20px 0;
}

.preset-heading {
  display: grid;
  grid-template-columns: 1fr 180px;
  gap: 12px;
  align-items: end;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.preset-heading h3 {
  margin: 0;
  font-size: 16px;
  line-height: 1.3;
}

.preset-table-wrap {
  max-height: 220px;
  overflow: auto;
}

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

.preset-table th,
.preset-table td {
  padding: 7px 8px;
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  text-align: left;
  font-size: 12px;
}

.preset-table th:last-child,
.preset-table td:last-child {
  border-right: 0;
}

.preset-table th {
  position: sticky;
  top: 0;
  background: var(--muted);
  color: var(--subtle);
  font-weight: 700;
}

.preset-table tr {
  cursor: pointer;
}

.preset-table tr:hover,
.preset-table tr.is-selected {
  background: rgba(0, 47, 167, 0.08);
}

.preset-table tr.is-selected td:first-child {
  color: var(--accent);
  font-weight: 700;
}

.preset-actions {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(120px, 1fr);
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--line);
}

.preset-actions button {
  min-height: 36px;
}

.orientation-row {
  margin: 16px 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.orientation-row label {
  min-height: 36px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--ink);
  background: #fff;
}

.orientation-row input {
  width: 16px;
  min-height: 16px;
}

.primary-action {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
}

.primary-action:hover {
  background: #001f73;
  color: #fff;
}

.table-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.table-toolbar select {
  width: 150px;
}

.product-table-wrap {
  width: 100%;
  overflow: auto;
  border: 1px solid var(--line);
  background: #fff;
}

.product-table {
  width: 100%;
  min-width: 700px;
  border-collapse: collapse;
}

.product-table th,
.product-table td {
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  padding: 6px;
  text-align: left;
  font-size: 12px;
}

.product-table th {
  background: var(--muted);
  color: var(--subtle);
  font-weight: 700;
}

.product-table td:last-child,
.product-table th:last-child {
  border-right: 0;
}

.product-table input,
.product-table select {
  min-width: 76px;
  min-height: 30px;
  padding: 4px 6px;
}

.remove-row {
  width: 32px;
  min-height: 30px;
  padding: 0;
  color: var(--danger);
}

.visual-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(360px, 1fr) auto auto;
  background: #fff;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  border-bottom: 1px solid var(--ink);
}

.metric {
  padding: 12px 14px;
  border-right: 1px solid var(--line);
}

.metric:last-child {
  border-right: 0;
}

.metric span {
  display: block;
  color: var(--subtle);
  font-size: 12px;
  margin-bottom: 4px;
}

.metric strong {
  display: block;
  font-size: 22px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.viewer-frame {
  position: relative;
  min-height: 360px;
  background:
    linear-gradient(to right, rgba(0, 47, 167, 0.08) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(0, 47, 167, 0.08) 1px, transparent 1px);
  background-size: 48px 48px;
}

#containerCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

.distance-panel {
  position: absolute;
  top: 14px;
  right: 14px;
  width: min(360px, calc(100% - 28px));
  border: 1px solid var(--ink);
  background: rgba(255, 255, 255, 0.94);
  color: var(--ink);
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.5;
  pointer-events: none;
}

.distance-panel strong {
  display: block;
  margin-bottom: 4px;
  font-size: 14px;
}

.distance-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 2px 10px;
  font-variant-numeric: tabular-nums;
}

.load-progress-panel {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 10px 16px;
  align-items: center;
  padding: 10px 18px;
  border-top: 1px solid var(--ink);
  background: var(--surface);
}

.progress-meta {
  display: grid;
  gap: 2px;
}

.progress-meta strong {
  font-size: 14px;
}

.progress-meta span {
  color: var(--subtle);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

#loadProgress {
  width: 100%;
  accent-color: var(--accent);
}

.view-toolbar {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.view-toolbar button {
  min-height: 32px;
  font-size: 13px;
}

.view-toolbar button.is-active {
  background: var(--ink);
  color: #fff;
}

.result-panel {
  border-top: 1px solid var(--ink);
  padding: 16px 18px 20px;
  max-height: 260px;
  overflow: auto;
}

.result-text {
  margin-top: 10px;
  color: var(--ink);
  line-height: 1.6;
  font-size: 14px;
}

.result-text table {
  border-collapse: collapse;
  margin-top: 8px;
  width: 100%;
}

.result-text th,
.result-text td {
  border: 1px solid var(--line);
  padding: 6px 8px;
  text-align: left;
}

.is-hidden {
  display: none;
}

@media (max-width: 980px) {
  .topbar {
    align-items: start;
    gap: 16px;
    flex-direction: column;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .input-panel {
    border-right: 0;
    border-bottom: 1px solid var(--ink);
  }

  .metrics {
    grid-template-columns: repeat(2, 1fr);
  }
}
