:root {
  --cream: #f3f2ed;
  --charcoal: #171717;
  --orange: #c76315;
  --olive: #6a802e;
  --border: rgba(23, 23, 23, 0.16);
  --control-surface: rgba(255, 255, 255, 0.42);
  --control-radius: 9px;
}

* { box-sizing: border-box; }
html { background: var(--cream); }
body {
  min-width: 320px;
  margin: 0;
  background: var(--cream);
  color: var(--charcoal);
  font-family: Arial, Helvetica, sans-serif;
}
button, input, select, textarea { font: inherit; }

.topbar {
  min-height: 72px;
  padding: 18px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.builder-title,
.back-link {
  margin: 0;
  color: inherit;
  font-size: 14px;
  font-weight: 400;
}
.back-link { text-decoration: none; }
.back-link:focus-visible {
  outline: 2px solid var(--orange);
  outline-offset: 4px;
}

.builder {
  min-height: calc(100vh - 72px);
  display: grid;
  grid-template-columns: minmax(300px, 380px) minmax(0, 1fr);
  gap: clamp(32px, 6vw, 80px);
  padding: 40px 28px 56px;
}
.controls {
  align-self: start;
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: var(--control-radius);
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  flex-direction: column;
  gap: 24px;
}
.control-group,
.dynamic-fields { display: grid; gap: 9px; }
.dynamic-fields { gap: 16px; }
.control-group label,
.control-label {
  color: rgba(23, 23, 23, 0.72);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}
.range-group label {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}
.range-group output {
  color: rgba(23, 23, 23, 0.58);
  font-size: 11px;
  font-weight: 400;
  letter-spacing: 0;
  text-transform: none;
}

input,
textarea,
select,
button {
  appearance: none;
  border: 1px solid rgba(23, 23, 23, 0.24);
  color: inherit;
}
input:not([type="checkbox"]):not([type="range"]),
textarea,
select {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--control-surface);
  transition: border-color 160ms ease, box-shadow 160ms ease, background-color 160ms ease;
}
select {
  padding-right: 42px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='8' viewBox='0 0 14 8'%3E%3Cpath d='m1 1 6 6 6-6' fill='none' stroke='%23171717' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-position: right 15px center;
  background-repeat: no-repeat;
}
input:not([type="checkbox"]):not([type="range"]):focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--charcoal);
  box-shadow: 0 0 0 3px rgba(199, 99, 21, 0.2);
  background-color: #fff;
}

input[type="range"] {
  width: 100%;
  height: 20px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 4px;
  background: rgba(23, 23, 23, 0.18);
}
input[type="range"]::-webkit-slider-thumb {
  width: 14px;
  height: 14px;
  margin-top: -5px;
  appearance: none;
  border: 2px solid var(--cream);
  border-radius: 50%;
  background: var(--charcoal);
  box-shadow: 0 0 0 1px var(--charcoal);
}
input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 4px;
  background: rgba(23, 23, 23, 0.18);
}
input[type="range"]::-moz-range-thumb {
  width: 11px;
  height: 11px;
  border: 2px solid var(--cream);
  border-radius: 50%;
  background: var(--charcoal);
  box-shadow: 0 0 0 1px var(--charcoal);
}
input[type="range"]:focus-visible {
  outline: 2px solid var(--orange);
  outline-offset: 3px;
}

.segmented-control,
.swatch-grid {
  display: grid;
  gap: 6px;
}
.segmented-control {
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.24);
}
.segmented-two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.segmented-four { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.segmented-control button {
  padding: 9px 7px;
  border-color: transparent;
  font-size: 12px;
}
.segmented-control button[aria-pressed="true"] {
  border-color: rgba(23, 23, 23, 0.2);
  background: var(--charcoal);
  color: var(--cream);
}

.swatch-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.color-swatch {
  min-height: 58px;
  padding: 8px;
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  border-color: transparent;
  font-size: 11px;
  text-align: left;
}
.swatch-cream { background: var(--cream); border-color: rgba(23, 23, 23, 0.2); }
.swatch-charcoal { background: var(--charcoal); color: var(--cream); }
.swatch-orange { background: var(--orange); color: var(--cream); }
.swatch-olive { background: var(--olive); color: var(--cream); }
.color-swatch[aria-pressed="true"] {
  outline: 2px solid var(--charcoal);
  outline-offset: 2px;
}

.toggle-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  font-size: 14px;
  cursor: pointer;
}
.toggle-switch {
  position: relative;
  width: 42px;
  height: 24px;
  flex: 0 0 auto;
}
.toggle-switch input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}
.toggle-track {
  width: 100%;
  height: 100%;
  display: block;
  border: 1px solid rgba(23, 23, 23, 0.26);
  border-radius: 999px;
  background: rgba(23, 23, 23, 0.12);
}
.toggle-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--cream);
  box-shadow: 0 1px 2px rgba(23, 23, 23, 0.18);
  transition: transform 160ms ease;
}
.toggle-switch input:checked + .toggle-track {
  border-color: var(--charcoal);
  background: var(--charcoal);
}
.toggle-switch input:checked + .toggle-track .toggle-thumb { transform: translateX(18px); }
.toggle-switch input:focus-visible + .toggle-track {
  outline: 2px solid var(--orange);
  outline-offset: 3px;
}

button {
  padding: 12px 15px;
  border-radius: 9px;
  background: transparent;
  font-size: 13px;
  cursor: pointer;
  transition: background-color 160ms ease, border-color 160ms ease, color 160ms ease, transform 160ms ease;
}
button:hover:not(:disabled) {
  border-color: var(--charcoal);
  transform: translateY(-1px);
}
button:active:not(:disabled) { transform: translateY(0); }
button:focus-visible {
  outline: none;
  border-color: var(--charcoal);
  box-shadow: 0 0 0 3px rgba(199, 99, 21, 0.2);
}
button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}
.primary-button {
  background: var(--charcoal);
  color: var(--cream);
  border-color: var(--charcoal);
}
.primary-button:hover:not(:disabled) { background: rgba(23, 23, 23, 0.88); }

.motion-actions,
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.motion-actions button { flex: 1 1 140px; }
.actions button { flex: 1 1 130px; }
.export-status,
.export-note,
.canvas-tip,
.canvas-size {
  color: rgba(23, 23, 23, 0.62);
  font-size: 12px;
}
.export-status { margin: -8px 0 0; }
.export-note { margin: -12px 0 0; line-height: 1.45; }

.preview-panel {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.canvas-wrap {
  width: min(100%, 900px);
  padding: 18px;
  background: rgba(23, 23, 23, 0.07);
}
canvas {
  display: block;
  width: 100%;
  height: auto;
  background: var(--charcoal);
}
.canvas-tip,
.canvas-size {
  width: min(100%, 900px);
  margin: 12px 0 0;
}
.canvas-size { margin-top: 8px; }

@media (max-width: 900px) {
  .topbar { padding: 16px 20px; }
  .builder {
    min-height: 0;
    grid-template-columns: 1fr;
    gap: 32px;
    padding: 28px 20px 40px;
  }
  .controls { padding: 20px; }
  .preview-panel { order: -1; }
  .canvas-wrap,
  .canvas-tip,
  .canvas-size { width: 100%; }
}

@media (max-width: 520px) {
  .segmented-four { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .swatch-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
