:root {
  color-scheme: dark;
  --bg: #1b1b1a;
  --bg-elevated: #232321;
  --bg-panel: #272724;
  --bg-soft: #30302c;
  --line: #45443d;
  --line-strong: #676556;
  --text: #eeeeee;
  --text-muted: #b9b7aa;
  --text-soft: #8f8c80;
  --mint: #c3f7e4;
  --mint-strong: #65d6b2;
  --amber: #f9b42c;
  --amber-soft: #5a4420;
  --red: #f07167;
  --blue: #7db2ff;
  --shadow: rgba(0, 0, 0, 0.36);
  --focus: #f9b42c;
  --radius: 8px;
  --font-body: "Inter", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", "Cascadia Mono", Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
}

body {
  background:
    linear-gradient(135deg, rgba(195, 247, 228, 0.05), transparent 34%),
    radial-gradient(circle at 82% 8%, rgba(249, 180, 44, 0.07), transparent 28%),
    var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  letter-spacing: 0;
}

button,
input {
  font: inherit;
}

button,
.file-button {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
.file-button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  min-height: 72px;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(27, 27, 26, 0.9);
  backdrop-filter: blur(18px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 3px;
  padding: 4px;
  border: 1px solid var(--line-strong);
  background: #20201e;
  box-shadow: 0 10px 24px var(--shadow);
}

.brand-mark span {
  display: block;
  background: var(--mint);
}

.brand-mark span:nth-child(2) {
  background: var(--amber);
}

.brand-mark span:nth-child(3) {
  background: #7db2ff;
}

.brand-mark span:nth-child(4) {
  background: #f07167;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--mint);
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

h1,
h2 {
  margin: 0;
  color: var(--text);
  font-weight: 720;
  line-height: 1.1;
}

h1 {
  font-size: clamp(20px, 3vw, 28px);
}

h2 {
  font-size: clamp(24px, 3.2vw, 40px);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

#fileInput {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.file-button,
.icon-button,
.tool-button,
.mini-button,
.segment {
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text);
  min-height: 38px;
  border-radius: var(--radius);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition:
    border-color 160ms ease,
    background 160ms ease,
    transform 160ms ease,
    color 160ms ease;
}

.file-button:hover,
.icon-button:hover,
.tool-button:hover,
.mini-button:hover,
.segment:hover {
  border-color: var(--mint-strong);
  background: #383830;
}

.file-button:active,
.icon-button:active,
.tool-button:active,
.mini-button:active,
.segment:active {
  transform: translateY(1px);
}

button:disabled,
button:disabled:hover {
  cursor: not-allowed;
  opacity: 0.42;
  border-color: #3e3d37;
  background: #20201e;
  color: var(--text-soft);
  transform: none;
}

.file-button {
  padding: 0 13px;
}

.icon-button,
.tool-button {
  width: 40px;
  padding: 0;
}

.mini-button {
  min-height: 30px;
  padding: 0 9px;
  color: var(--mint);
  font-size: 12px;
}

svg {
  width: 17px;
  height: 17px;
  stroke-width: 2;
}

.workspace {
  width: 100%;
  min-height: calc(100vh - 72px);
  display: grid;
  grid-template-columns: minmax(240px, 300px) minmax(420px, 1fr) minmax(280px, 360px);
  gap: 16px;
  padding: 16px;
  align-items: start;
}

.panel-stack {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}

.panel,
.stage {
  border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.025), transparent 140px), var(--bg-panel);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.18);
}

.panel {
  border-radius: var(--radius);
  padding: 14px;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
  color: var(--text);
  font-size: 14px;
  font-weight: 680;
}

.panel-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  flex-wrap: wrap;
}

.panel-heading small {
  color: var(--text-soft);
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
}

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

.metric {
  min-height: 68px;
  padding: 11px;
  border: 1px solid #3e3d37;
  background: #20201e;
  border-radius: 6px;
}

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

.metric strong {
  display: block;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 18px;
  line-height: 1.1;
  overflow-wrap: anywhere;
  white-space: nowrap;
}

.layer-control {
  display: grid;
  grid-template-columns: 42px 1fr 42px;
  gap: 8px;
  align-items: center;
}

.layer-readout {
  min-height: 58px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 1px solid #3e3d37;
  background: #20201e;
  border-radius: 6px;
}

.layer-readout span {
  color: var(--text-soft);
  font-size: 11px;
}

.layer-readout strong {
  margin-top: 4px;
  color: var(--amber);
  font-family: var(--font-mono);
  font-size: 24px;
}

.range {
  width: 100%;
  margin: 14px 0;
  accent-color: var(--amber);
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}

.segment {
  min-height: 34px;
  padding: 0 8px;
  color: var(--text-muted);
  font-size: 12px;
}

.segment.is-active {
  background: #193b32;
  border-color: var(--mint-strong);
  color: var(--mint);
}

.toggle-list {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.switch-row {
  min-height: 38px;
  padding: 0 10px;
  border: 1px solid #3e3d37;
  background: #20201e;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--text-muted);
  font-size: 13px;
}

.switch-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--mint-strong);
}

.tool-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.tool-row .tool-button {
  width: 100%;
}

.stage {
  min-height: 0;
  border-radius: var(--radius);
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
}

.stage-header {
  padding: 18px 18px 12px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  border-bottom: 1px solid var(--line);
}

.stage-nav {
  display: none;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}

.stage-nav span {
  min-width: 72px;
  min-height: 38px;
  display: inline-grid;
  place-items: center;
  border: 1px solid #3e3d37;
  border-radius: var(--radius);
  background: #20201e;
  color: var(--amber);
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 760;
}

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

.chip {
  min-height: 30px;
  display: inline-flex;
  align-items: center;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #20201e;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 12px;
  white-space: nowrap;
}

.chip-mint {
  color: var(--mint);
  border-color: rgba(101, 214, 178, 0.55);
}

.chip-amber {
  color: var(--amber);
  border-color: rgba(249, 180, 44, 0.55);
}

.canvas-shell {
  position: relative;
  min-height: 420px;
  height: clamp(420px, calc(100vh - 260px), 640px);
  overflow: hidden;
  background:
    linear-gradient(rgba(255, 255, 255, 0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.025) 1px, transparent 1px),
    #1f1f1d;
  background-size: 28px 28px;
}

#layerCanvas {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
}

.axis {
  position: absolute;
  min-width: 34px;
  min-height: 22px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(27, 27, 26, 0.82);
  color: var(--mint);
  font-family: var(--font-mono);
  font-size: 11px;
  pointer-events: none;
}

.axis-top {
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
}

.axis-right {
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
}

.axis-bottom {
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
}

.axis-left {
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
}

.hover-card {
  position: absolute;
  left: 16px;
  bottom: 16px;
  max-width: min(440px, calc(100% - 32px));
  padding: 10px 12px;
  border: 1px solid rgba(195, 247, 228, 0.35);
  border-radius: var(--radius);
  background: rgba(27, 27, 26, 0.88);
  box-shadow: 0 14px 30px var(--shadow);
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 12px;
  pointer-events: none;
  opacity: 0;
  transform: translateY(6px);
  transition:
    opacity 160ms ease,
    transform 160ms ease;
}

.hover-card.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.difference-strip {
  min-height: 60px;
  padding: 12px 18px;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  align-items: center;
}

.difference-strip div {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 13px;
}

.difference-strip strong {
  color: var(--text);
  font-family: var(--font-mono);
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

.dot-current {
  background: var(--amber);
}

.dot-shared {
  background: var(--mint-strong);
}

.dot-ghost {
  border: 1px dashed var(--blue);
}

.palette-list {
  display: grid;
  gap: 8px;
  max-height: 360px;
  overflow: auto;
  padding-right: 3px;
}

.palette-item {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 9px;
  align-items: center;
  padding: 8px;
  border: 1px solid #3e3d37;
  background: #20201e;
  border-radius: 6px;
}

.swatch {
  width: 28px;
  height: 28px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.34);
}

.palette-name {
  min-width: 0;
}

.palette-name strong,
.palette-name span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.palette-name strong {
  color: var(--text);
  font-size: 12px;
}

.palette-name span {
  color: var(--text-soft);
  font-family: var(--font-mono);
  font-size: 11px;
}

.palette-count {
  color: var(--amber);
  font-family: var(--font-mono);
  font-size: 12px;
}

.preview-panel canvas {
  display: block;
  width: 100%;
  height: 250px;
  border: 1px solid #3e3d37;
  border-radius: 6px;
  background: #20201e;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.preview-panel canvas.is-dragging {
  cursor: grabbing;
}

.selection-box {
  min-height: 120px;
  display: grid;
  gap: 8px;
  color: var(--text-muted);
  font-size: 13px;
}

.selection-stack {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  gap: 6px;
}

.stack-cell {
  min-height: 36px;
  border: 1px solid #3e3d37;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  background: #20201e;
}

.stack-cell.is-filled {
  color: #161613;
  font-weight: 800;
}

.build-panel {
  min-height: 360px;
}

.material-summary {
  display: grid;
  gap: 6px;
  margin-bottom: 10px;
}

.material-pill {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 6px 8px;
  border: 1px solid #3e3d37;
  border-radius: 6px;
  background: rgba(32, 32, 30, 0.72);
  color: var(--text-muted);
  font-size: 12px;
}

.material-pill strong,
.material-pill span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.material-pill strong {
  color: var(--text);
}

.material-pill span:last-child {
  color: var(--amber);
  font-family: var(--font-mono);
}

.mini-swatch {
  width: 18px;
  height: 18px;
  border: 1px solid rgba(255, 255, 255, 0.35);
}

.build-list {
  max-height: 430px;
  overflow: auto;
  display: grid;
  gap: 6px;
  padding-right: 3px;
}

.run-row {
  display: grid;
  grid-template-columns: 24px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 8px;
  border: 1px solid #3e3d37;
  border-radius: 6px;
  background: #20201e;
}

.run-row code {
  display: block;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 12px;
}

.run-row span {
  display: block;
  margin-top: 3px;
  color: var(--text-soft);
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.run-count {
  color: var(--amber);
  font-family: var(--font-mono);
  font-size: 12px;
}

.empty-state {
  min-height: 80px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  color: var(--text-soft);
  text-align: center;
  padding: 14px;
}

.toast {
  position: fixed;
  right: 18px;
  top: 88px;
  max-width: min(420px, calc(100vw - 36px));
  padding: 12px 14px;
  border: 1px solid rgba(195, 247, 228, 0.4);
  border-radius: var(--radius);
  background: rgba(27, 27, 26, 0.94);
  color: var(--text);
  box-shadow: 0 18px 40px var(--shadow);
  transform: translateY(-10px);
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 180ms ease,
    transform 180ms ease;
  z-index: 40;
}

.toast.is-visible {
  transform: translateY(0);
  opacity: 1;
}

::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-track {
  background: #1d1d1b;
}

::-webkit-scrollbar-thumb {
  background: #4c4a42;
  border: 2px solid #1d1d1b;
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 280px minmax(0, 1fr);
  }

  .right-panel {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

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

  .topbar-actions {
    justify-content: flex-start;
  }

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

  .stage {
    order: 1;
  }

  .left-panel {
    order: 2;
  }

  .right-panel {
    order: 3;
    display: flex;
  }

  .stage {
    min-height: 0;
  }

  .stage-header,
  .difference-strip {
    grid-template-columns: 1fr;
    flex-direction: column;
    align-items: flex-start;
  }

  .stage-nav {
    display: flex;
  }

  .difference-strip {
    display: grid;
  }
}

@media (max-width: 560px) {
  .metric-grid,
  .segmented,
  .difference-strip {
    grid-template-columns: 1fr;
  }

  .stage-header {
    padding: 14px;
  }

  .chip-row {
    justify-content: flex-start;
  }

  .panel-actions {
    width: 100%;
    justify-content: stretch;
  }

  .panel-actions .mini-button {
    flex: 1;
  }
}
