:root {
  --bg: #e7eae5;
  --surface: rgba(251, 252, 250, 0.92);
  --surface-solid: #fbfcfa;
  --surface-muted: #f0f2ee;
  --border: #d2d8d3;
  --border-strong: #b6bfb8;
  --ink: #17201c;
  --ink-muted: #5a6660;
  --accent: #1f5c4a;
  --accent-soft: #e4f0eb;
  --cut: #bf3f27;
  --fill: #1d785a;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(18, 28, 24, 0.06), 0 6px 20px rgba(18, 28, 24, 0.08);
  --font-sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, monospace;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.35;
  user-select: none;
}

.viewport {
  position: fixed;
  inset: 0;
  touch-action: none;
  cursor: grab;
}

.viewport.is-sculpt {
  cursor: crosshair;
}

.viewport.is-grabbing {
  cursor: grabbing;
}

.hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.panel {
  pointer-events: auto;
  background: var(--surface);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* Top bar */
.topbar {
  position: fixed;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}

.top-group {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px;
}

.version-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  height: 30px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  max-width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.version-tag {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--ink-muted);
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}

.segmented {
  display: inline-flex;
  background: var(--surface-muted);
  border-radius: 6px;
  padding: 2px;
  gap: 2px;
}

.segmented button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-muted);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  height: 26px;
  padding: 0 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}

.segmented button:hover {
  color: var(--ink);
}

.segmented button.active {
  background: var(--surface-solid);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* Metrics strip */
.metrics {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 6px;
}

.metric-item {
  display: flex;
  align-items: baseline;
  gap: 5px;
  padding: 3px 8px;
  white-space: nowrap;
}

.metric-item + .metric-item {
  border-left: 1px solid var(--border);
}

.metric-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  display: inline-block;
  align-self: center;
}

.metric-dot.cut {
  background: var(--cut);
}

.metric-dot.fill {
  background: var(--fill);
}

.metric-label {
  font-size: 11px;
  color: var(--ink-muted);
}

.metric-val {
  font-family: var(--font-mono);
  font-size: 12.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.metric-val.cut {
  color: var(--cut);
}

.metric-val.fill {
  color: var(--fill);
}

.scope-toggle {
  margin-left: 4px;
  padding-left: 6px;
  border-left: 1px solid var(--border);
}

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  height: 30px;
  padding: 0 9px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  cursor: pointer;
  white-space: nowrap;
  position: relative;
  transition: background 110ms ease, border-color 110ms ease, color 110ms ease;
}

.btn:hover:not(:disabled) {
  background: var(--surface-muted);
}

.btn:disabled {
  opacity: 0.38;
  cursor: default;
}

.btn-primary {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.btn-primary:hover:not(:disabled) {
  background: #18493b;
}

.btn-outline {
  border-color: var(--border);
  background: var(--surface-solid);
}

.btn-outline:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: var(--surface-muted);
}

.btn-danger {
  color: var(--cut);
}

.badge-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--warn);
}

.icon {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Left tool rail */
.tool-rail {
  position: fixed;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 4px;
}

.tool-btn {
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 6px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: background 110ms ease, color 110ms ease;
}

.tool-btn:hover:not(:disabled) {
  background: var(--surface-muted);
  color: var(--ink);
}

.tool-btn.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: rgba(31, 92, 74, 0.25);
}

.tool-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.rail-sep {
  height: 1px;
  background: var(--border);
  margin: 3px 4px;
}

/* Bottom contextual bar */
.bottom-left {
  position: fixed;
  left: 10px;
  bottom: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
}

.param-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--ink-muted);
}

.param-field input[type="range"] {
  width: 88px;
  accent-color: var(--accent);
  cursor: pointer;
}

.param-field input[type="number"] {
  width: 78px;
  height: 26px;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-solid);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 12px;
}

.param-val {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  min-width: 40px;
}

/* Bottom-right telemetry */
.telemetry {
  position: fixed;
  right: 10px;
  bottom: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-muted);
}

.telemetry strong {
  color: var(--ink);
  font-weight: 600;
}

.telemetry .dh-cut {
  color: var(--cut);
}

.telemetry .dh-fill {
  color: var(--fill);
}

/* Diff legend (compact vertical scale on right when diff view active) */
.diff-legend {
  position: fixed;
  right: 10px;
  top: 62px;
  padding: 8px 9px;
  display: flex;
  align-items: center;
  gap: 7px;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--ink-muted);
}

.diff-bar {
  width: 10px;
  height: 92px;
  border-radius: 3px;
  background: linear-gradient(to bottom, #1d785a 0%, #7ec2a8 32%, #e6e9e4 50%, #df9484 68%, #bf3f27 100%);
  border: 1px solid var(--border);
}

.diff-labels {
  height: 92px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

/* Slide-over Review / Import Drawer */
.drawer {
  position: fixed;
  top: 10px;
  right: 10px;
  bottom: 10px;
  width: min(460px, calc(100vw - 20px));
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 36px rgba(18, 28, 24, 0.16);
  z-index: 30;
  display: flex;
  flex-direction: column;
  transform: translateX(calc(100% + 24px));
  transition: transform 180ms cubic-bezier(0.2, 0.8, 0.2, 1);
  user-select: text;
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}

.drawer-tabs {
  display: flex;
  gap: 2px;
  background: var(--surface-muted);
  padding: 2px;
  border-radius: 6px;
}

.drawer-tabs button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-muted);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 4px 9px;
  border-radius: 4px;
  cursor: pointer;
}

.drawer-tabs button.active {
  background: var(--surface-solid);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.drawer-footer {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--surface-muted);
  border-radius: 0 0 var(--radius) var(--radius);
}

/* Clean compact forms & tables inside drawer */
.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.field label {
  font-size: 11px;
  color: var(--ink-muted);
  font-weight: 500;
}

.field input,
.field select {
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #ffffff;
  color: var(--ink);
  font: inherit;
  font-size: 12px;
}

.dropzone {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--surface-muted);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.dropzone.dragover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.file-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
}

.file-status {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.kpi-cell {
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
}

.kpi-cell .k {
  font-size: 10.5px;
  color: var(--ink-muted);
}

.kpi-cell .v {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.chip {
  appearance: none;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--ink-muted);
  font: inherit;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 99px;
  cursor: pointer;
}

.chip.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11.5px;
}

.data-table th {
  text-align: left;
  font-weight: 600;
  color: var(--ink-muted);
  border-bottom: 1px solid var(--border);
  padding: 5px 4px;
  position: sticky;
  top: 0;
  background: var(--surface-solid);
}

.data-table td {
  padding: 4px;
  border-bottom: 1px solid #ecefec;
  vertical-align: middle;
}

.data-table tr.is-open {
  background: #fffbeb;
}

.data-table select {
  height: 24px;
  font-size: 11px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #ffffff;
  color: var(--ink);
}

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.row-box {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #ffffff;
}

.row-box.is-warn {
  border-color: #eab308;
  background: #fffbeb;
}

.row-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
}

.row-meta {
  font-size: 11px;
  color: var(--ink-muted);
}

.vertex-grid {
  display: grid;
  grid-template-columns: auto 1fr 1fr;
  gap: 4px 6px;
  align-items: center;
  font-size: 11px;
}

.vertex-grid input {
  height: 24px;
  width: 100%;
  padding: 0 5px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 11px;
}

.audit-log {
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  max-height: 130px;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--ink-muted);
  display: flex;
  flex-direction: column;
  gap: 3px;
}

/* Modal */
.modal-root {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 20, 0.28);
  backdrop-filter: blur(3px);
  z-index: 40;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-root.open {
  display: flex;
}

.modal-box {
  width: min(420px, 100%);
  max-height: 80vh;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: text;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

.modal-body {
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.proj-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #ffffff;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translate(-50%, 24px);
  opacity: 0;
  pointer-events: none;
  background: #17201c;
  color: #f6f8f7;
  font-size: 12px;
  padding: 7px 12px;
  border-radius: 6px;
  z-index: 50;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* Responsive mobile / touch layout */
@media (max-width: 900px) {
  .topbar {
    flex-wrap: wrap;
    gap: 6px;
  }

  .version-chip {
    display: none;
  }

  .btn-label {
    display: none;
  }

  .metrics {
    order: 3;
    width: 100%;
    justify-content: space-between;
    overflow-x: auto;
  }

  .tool-rail {
    top: auto;
    bottom: 58px;
    left: 10px;
    transform: none;
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    max-width: calc(100vw - 20px);
  }

  .rail-sep {
    width: 1px;
    height: 22px;
    margin: 8px 2px;
  }

  .bottom-left {
    bottom: 108px;
    max-width: calc(100vw - 20px);
    flex-wrap: wrap;
  }

  .telemetry {
    left: 10px;
    right: 10px;
    bottom: 10px;
    justify-content: space-between;
    font-size: 11px;
    gap: 6px;
  }

  .telemetry .coord-xy {
    display: none;
  }

  .diff-legend {
    top: 104px;
  }
}
