:root {
  color-scheme: light;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1b1f24;
  --muted: #667085;
  --border: #d8dde5;
  --accent: #1769e0;
  --accent-soft: #e8f1ff;
  --danger: #b42318;
  --danger-soft: #fff1f0;
  --shadow: 0 12px 28px rgba(16, 24, 40, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  min-height: 44px;
  border-radius: 6px;
  padding: 0 14px;
  cursor: pointer;
}

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

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

.app-shell {
  width: min(100% - 20px, 1280px);
  margin: 10px auto;
}

.loader-band,
.preview-panel,
.controls-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.loader-band {
  padding: 14px;
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  align-items: end;
}

.loader-band h1,
.preview-header h2 {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
}

.loader-band p,
.preview-header p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.4;
}

.load-form {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.load-form input,
.control-field input,
.control-field textarea,
.control-field select,
#export-output {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  min-height: 44px;
  padding: 9px 10px;
  background: #fff;
  color: var(--text);
}

.status {
  grid-column: 1 / -1;
  min-height: 20px;
  color: var(--muted);
  font-size: 14px;
  white-space: pre-line;
}

.status.is-error {
  color: var(--danger);
}

.workspace {
  margin-top: 18px;
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
  align-items: start;
}

.workspace.is-empty {
  opacity: 0.9;
}

.preview-panel,
.controls-panel {
  padding: 16px;
}

.preview-panel {
  position: sticky;
  top: 0;
  z-index: 4;
}

.preview-header {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: start;
  margin-bottom: 14px;
}

.price-delta {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 10px;
  color: var(--muted);
  font-size: 13px;
}

.preview-stage {
  position: relative;
  width: min(100%, 320px, 40dvh);
  margin: 0 auto;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  background: #eef1f5;
  border: 1px solid var(--border);
  border-radius: 8px;
  touch-action: none;
}

.preview-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-align: center;
  padding: 20px;
}

.preview-layer {
  position: absolute;
  display: block;
  pointer-events: none;
}

.preview-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.preview-fill {
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.preview-base {
  z-index: 0;
}

.preview-background {
  z-index: 1;
}

.image-layer {
  z-index: 2;
}

.image-layer.is-active-edit {
  z-index: 3;
}

.preview-overlay {
  z-index: 4;
}

.text-layer {
  z-index: 5;
}

.preview-mask {
  z-index: 6;
}

.placement-layer {
  position: absolute;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  text-align: center;
}

.text-layer {
  pointer-events: auto;
  cursor: move;
}

.text-layer.is-single-line {
  white-space: nowrap;
  overflow-wrap: normal;
  word-break: normal;
}

.placement-layer.is-draggable {
  pointer-events: auto;
  cursor: move;
  outline: 1.5px dashed rgba(23, 105, 224, 0.9);
  outline-offset: 2px;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.72);
}

.placement-layer.is-active-edit {
  outline: 2px solid #ff9900;
  outline-offset: 3px;
  box-shadow: 0 0 0 1px rgba(17, 17, 17, 0.22), 0 0 0 2px rgba(255, 255, 255, 0.72);
}

.placement-layer img {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
  max-width: none;
  max-height: none;
  user-select: none;
  pointer-events: none;
}

.preview-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.preview-actions button {
  flex: 1;
}

.warnings {
  display: grid;
  gap: 8px;
}

.warning,
.field-error,
.validation-summary {
  background: var(--danger-soft);
  border: 1px solid #f5c2bd;
  color: var(--danger);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.4;
}

.control-group {
  position: relative;
  border-bottom: 1px solid var(--border);
  padding: 16px 0;
}

.control-group.is-active-edit {
  background: var(--accent-soft);
  margin-inline: -10px;
  padding-inline: 10px;
  border-radius: 6px;
}

.control-group:first-child {
  padding-top: 0;
}

.control-title {
  display: flex;
  justify-content: flex-start;
  gap: 10px;
  margin-bottom: 8px;
}

.control-title h3 {
  margin: 0;
  font-size: 16px;
}

.required {
  color: var(--danger);
  font-size: 13px;
}

.optional {
  color: var(--muted);
  font-size: 16px;
  font-weight: 400;
}

.instructions {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.option-grid {
  display: flex;
  gap: 10px;
  max-height: none;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 4px 10px;
  scroll-snap-type: x proximity;
  -webkit-overflow-scrolling: touch;
}

.option-grid.is-collapsed {
  max-height: none;
  overflow-x: auto;
  overflow-y: hidden;
}

.option-card {
  flex: 0 0 132px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 8px;
  min-height: 96px;
  text-align: left;
  display: grid;
  gap: 8px;
  scroll-snap-align: start;
}

.option-card.has-no-image {
  min-height: 54px;
  align-content: center;
}

.option-card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.option-grid.is-yes-no {
  display: flex;
  gap: 10px;
  max-height: none;
  overflow: visible;
}

.option-grid.is-yes-no .option-card {
  min-width: 90px;
  min-height: 48px;
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  border-color: var(--border);
  border-radius: 8px;
  background: #fff;
  font-weight: 800;
}

.option-grid.is-yes-no .option-card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
  background: #f7fafa;
}

.option-grid.is-yes-no .option-thumb,
.option-grid.is-yes-no .option-cost {
  display: none;
}

.option-grid.is-yes-no .option-name {
  font-size: 16px;
  line-height: 1;
}

.option-grid.is-text-choice {
  max-height: none;
  overflow-x: auto;
  overflow-y: hidden;
}

.option-grid.is-text-choice .option-card {
  min-height: 98px;
  padding: 12px 10px;
  align-content: start;
  border-color: var(--border);
  border-radius: 8px;
  gap: 8px;
}

.option-grid.is-text-choice .option-card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
  background: #f7fafa;
}

.option-grid.is-text-choice .option-thumb {
  display: none;
}

.option-grid.is-text-choice .option-name {
  font-size: 16px;
  line-height: 1.25;
  font-weight: 700;
}

.option-grid.is-text-choice .option-cost {
  color: var(--text);
  font-size: 13px;
  line-height: 1.1;
}

.option-card:not(.is-selected):not(:disabled):hover {
  border-color: #8d9797;
  box-shadow: 0 0 0 1px #e3e6e6;
}

.option-thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #f7f8fa;
  object-fit: contain;
}

.option-name {
  font-size: 13px;
  line-height: 1.25;
  color: var(--text);
}

.option-cost {
  color: var(--muted);
  font-size: 12px;
}

.option-toggle {
  justify-self: start;
  margin-top: 10px;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 400;
  padding: 0;
}

.option-toggle::after {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-left: 8px;
  border: solid currentColor;
  border-width: 0 1.5px 1.5px 0;
  transform: translateY(-2px) rotate(45deg);
}

.option-grid.is-expanded + .option-toggle::after {
  transform: translateY(2px) rotate(225deg);
}

.option-toggle:hover {
  font-weight: 700;
  text-decoration: underline;
}

.option-list {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 20;
  display: grid;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 8px 24px rgba(16, 24, 40, 0.16);
}

.option-list.is-above {
  box-shadow: 0 -8px 24px rgba(16, 24, 40, 0.14);
}

.option-row {
  min-height: 56px;
  display: grid;
  grid-template-columns: 54px 1fr;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  border: 1px solid transparent;
  border-radius: 0;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.option-row:hover {
  background: #f7fafa;
  border-color: var(--border);
}

.option-row.is-selected {
  background: #e6f4fb;
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.option-row img,
.option-row-icon {
  width: 38px;
  height: 38px;
  object-fit: contain;
  justify-self: center;
}

.option-row-icon {
  background: #f7fafa;
  border: 1px solid var(--border);
}

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

.swatch {
  width: 38px;
  height: 38px;
  border-radius: 999px;
  border: 1px solid var(--border);
  padding: 0;
  background: var(--swatch-color);
  color: transparent;
}

.swatch.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.control-field {
  display: grid;
  gap: 8px;
}

.field-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
}

.zoom-control {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.zoom-control input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

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

.edit-actions button {
  min-height: 34px;
  padding: 0 10px;
  font-size: 13px;
}

.font-dropdown {
  position: relative;
}

.font-dropdown summary {
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  list-style: none;
  padding: 9px 38px 9px 12px;
  display: flex;
  align-items: center;
  font-size: 18px;
  line-height: 1.15;
}

.font-dropdown summary::-webkit-details-marker {
  display: none;
}

.font-dropdown summary::after {
  content: "";
  position: absolute;
  right: 14px;
  top: 18px;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 6px solid var(--text);
}

.font-dropdown[open] summary::after {
  border-top: 0;
  border-bottom: 6px solid var(--text);
}

.font-choice-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 8px;
  max-height: 220px;
  overflow: auto;
  padding: 8px;
}

.font-dropdown .font-choice-list {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 4px);
  z-index: 10;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: var(--shadow);
}

.font-choice {
  min-height: 38px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 18px;
  line-height: 1.15;
  text-align: left;
}

.font-choice.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.field-error {
  display: none;
}

.field-error:not(:empty) {
  display: block;
}

.controls-empty {
  color: var(--muted);
  padding: 24px 0;
}

.export-label {
  display: block;
  margin-top: 16px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
}

#export-output {
  min-height: 180px;
  resize: vertical;
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
}

.validation-summary:empty,
.warnings:empty {
  display: none;
}

@media (min-width: 561px) {
  .app-shell {
    width: min(1280px, calc(100% - 32px));
    margin: 18px auto;
  }

  .load-form {
    grid-template-columns: 1fr auto;
  }

  .preview-stage {
    width: min(100%, 420px, 48dvh);
  }

  .option-grid,
  .option-grid.is-collapsed {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
    overflow: visible;
    padding: 4px;
    scroll-snap-type: none;
  }

  .option-card {
    flex: initial;
    min-height: 96px;
  }

  .option-grid.is-text-choice {
    grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
    overflow: visible;
  }
}

@media (min-width: 901px) {
  .loader-band {
    grid-template-columns: minmax(220px, 0.8fr) minmax(360px, 1.2fr);
    padding: 18px;
  }

  .workspace {
    grid-template-columns: minmax(360px, 480px) minmax(0, 1fr);
  }

  .preview-panel {
    top: 16px;
  }

  .preview-stage {
    width: 100%;
  }
}
