:root {
  --bg: #f4f4f5;
  --card: #ffffff;
  --border: #e7e7ea;
  --field: #fcfcfd;
  --ink: #111113;
  --muted: #8a8a93;
  --track: #e4e4e7;
  --check: #e2e2e6;
  --radius: 16px;
  --stage: 320px;
}
* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 44px 32px 36px;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------------- page header / footer ---------------- */
.page-head,
.page-foot {
  max-width: 940px;
  margin: 0 auto;
  text-align: center;
}

.page-head { margin-bottom: 28px; }

.page-head h1 {
  margin: 0;
  font-size: 30px;
  font-weight: 600;
  letter-spacing: -0.022em;
}

.page-head p {
  margin: 9px 0 0;
  font-size: 13.5px;
  color: var(--muted);
}

.page-foot {
  margin-top: 28px;
  font-size: 12.5px;
  line-height: 1.7;
  color: var(--muted);
}

.page-foot p { margin: 0; }

.page-foot a {
  color: var(--ink);
  text-decoration: underline;
  text-decoration-color: rgb(17 17 19 / 30%);
  text-underline-offset: 2px;
}

.page-foot a:hover { text-decoration-color: var(--ink); }

@media (max-width: 560px) {
  .page-head h1 { font-size: 24px; }
  .page-head p { font-size: 12.5px; }
}

/* ---------------- layout ---------------- */
.layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 28px;
  align-items: stretch;        /* both cards share the taller height */
  max-width: 940px;
  margin: 0 auto;
}

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

.card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 22px;
  padding: 24px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgb(0 0 0 / 4%);
}

.card-head h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.card-head p {
  margin: 4px 0 0;
  font-size: 13px;
  color: var(--muted);
}

.control-section {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.control-title {
  margin: 0;
  font-size: 12.5px;
  font-weight: 400;
}

/* ---------------- fields ---------------- */
.field { display: flex; flex-direction: column; gap: 8px; }

.field label {
  font-size: 12.5px;
  color: var(--ink);
}

input[type="text"] {
  height: 40px;
  padding: 0 12px;
  font: inherit;
  color: var(--ink);
  background: var(--field);
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
}

input[type="text"]:focus { border-color: var(--ink); }

/* colour picker + hex field */
.color-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

input[type="color"] {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  padding: 3px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 6px; }
input[type="color"]::-moz-color-swatch { border: none; border-radius: 6px; }

.color-row input[type="text"] { flex: 1 1 auto; min-width: 0; }

/* ---------------- preset swatches ---------------- */
.swatches {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 26px));
  gap: min(8px, 1vw);
}

.swatch {
  width: 100%;
  max-width: 26px;
  aspect-ratio: 1;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: currentColor;   /* set per button from JS */
  cursor: pointer;
  transition: box-shadow 0.12s ease, transform 0.14s ease;
}

.swatch:hover { transform: scale(1.12); }

.swatch[data-light="true"] { box-shadow: inset 0 0 0 1px var(--border); }

.swatch[aria-pressed="true"] {
  box-shadow: 0 0 0 2px var(--card), 0 0 0 3.6px var(--ink);
}

.swatch[data-light="true"][aria-pressed="true"] {
  box-shadow: inset 0 0 0 1px var(--border), 0 0 0 2px var(--card), 0 0 0 3.6px var(--ink);
}

.swatch:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; }

/* ---------------- toggle ---------------- */
.switch {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  user-select: none;
}

.switch input { position: absolute; opacity: 0; width: 0; height: 0; }

.switch .pill {
  position: relative;
  flex: 0 0 46px;
  width: 46px;
  height: 26px;
  border-radius: 13px;
  background: #d4d4d8;
  transition: background 0.18s ease;
}

.switch .pill::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.18s ease;
}

.switch input:checked + .pill { background: var(--ink); }
.switch input:checked + .pill::after { transform: translateX(20px); }
.switch input:focus-visible + .pill { box-shadow: 0 0 0 3px rgb(17 17 19 / 15%); }
.switch.is-disabled { color: var(--muted); cursor: not-allowed; }
.switch input:disabled + .pill { background: var(--track); }
.switch input:disabled ~ .switch-label { color: var(--muted); }

.switch-label { font-size: 13px; }

/* ---------------- export options ---------------- */
.option-field {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.option-field legend {
  margin-bottom: 8px;
  padding: 0;
  font-size: 12.5px;
}

.segmented {
  display: grid;
  padding: 3px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.size-options {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

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

.format-options { grid-template-columns: repeat(4, 1fr); }

.segmented button,
.size-presets button {
  min-width: 0;
  height: 34px;
  padding: 0 10px;
  color: var(--muted);
  font: 600 12px/1 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 7px;
  cursor: pointer;
}

.segmented button { background: transparent; border-color: transparent; }

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

.segmented button[aria-pressed="true"],
.size-presets button[aria-pressed="true"] {
  color: var(--ink);
  background: var(--card);
}

.segmented button[aria-pressed="true"] {
  box-shadow: 0 1px 3px rgb(0 0 0 / 9%);
}

.segmented button:focus-visible,
.size-presets button:focus-visible,
.preview-copy:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 1px;
}

.size-presets button[aria-pressed="true"] { border-color: var(--ink); }

.custom-size {
  position: relative;
  min-width: 92px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 7px;
}

.custom-size::after {
  content: "";
  position: absolute;
  top: 50%;
  right: 14px;
  width: 7px;
  height: 7px;
  border-right: 1.5px solid var(--ink);
  border-bottom: 1.5px solid var(--ink);
  transform: translateY(-70%) rotate(45deg);
  pointer-events: none;
}

.custom-size select {
  width: 100%;
  height: 34px;
  padding: 0 28px 0 10px;
  color: var(--ink);
  font: 500 12px/1 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  background: transparent;
  border: 0;
  outline: 0;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}

.custom-size.is-active { border-color: var(--ink); }

.custom-size:focus-within {
  outline: 2px solid var(--ink);
  outline-offset: 1px;
}

/* ---------------- preview ---------------- */
.stage-wrap {
  flex: 1 1 auto;              /* soaks up the slack so the buttons sit at the bottom */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.stage {
  position: relative;
  width: min(var(--stage), 100%);
  aspect-ratio: 1;
  overflow: visible;
}

.plate {
  width: 100%;
  height: 100%;
  line-height: 0;
}

.plate canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.plate.is-transparent {
  background-color: #fff;
  background-image:
    linear-gradient(45deg, var(--check) 25%, transparent 25% 75%, var(--check) 75%),
    linear-gradient(45deg, var(--check) 25%, transparent 25% 75%, var(--check) 75%);
  background-size: 20px 20px;
  background-position: 0 0, 10px 10px;
}

.plate:not(.is-transparent) {
  background: #fff;
  box-shadow: 0 0 0 1px var(--border);
}

.preview-copy {
  position: absolute;
  z-index: 2;
  top: -12px;
  right: -12px;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  color: var(--ink);
  background: rgb(255 255 255 / 92%);
  border: 1px solid rgb(17 17 19 / 12%);
  border-radius: 9px;
  box-shadow: 0 2px 8px rgb(0 0 0 / 10%);
  backdrop-filter: blur(8px);
  cursor: pointer;
}

.preview-copy:hover { background: #fff; }

.preview-copy svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
}

.preview-copy svg rect:last-child { fill: var(--card); }

/* ---------------- buttons ---------------- */
.actions { display: flex; gap: 10px; }

.btn {
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font: 500 14px/1 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.btn-primary {
  flex: 1 1 auto;
  color: #fff;
  background: var(--ink);
  border: 1px solid var(--ink);
}

.btn-primary:hover { background: #2a2a2e; }

.btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ---------------- iOS export fallback ---------------- */
.export-dialog {
  position: fixed;
  z-index: 20;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgb(17 17 19 / 55%);
}

.export-dialog.is-hidden { display: none; }

.export-dialog-card {
  width: min(360px, 100%);
  padding: 24px;
  text-align: center;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 16px 48px rgb(0 0 0 / 20%);
}

.export-dialog-card h2 {
  margin: 0;
  font-size: 18px;
}

.export-dialog-card p {
  margin: 8px 0 18px;
  color: var(--muted);
}

.export-dialog-card img {
  display: block;
  width: min(256px, 100%);
  height: auto;
  margin: 0 auto 20px;
  border: 1px solid var(--border);
  -webkit-touch-callout: default;
  user-select: auto;
}

.export-dialog-card .btn { width: 100%; }

/* ---------------- toast ---------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  transform: translate(-50%, 8px);
  padding: 9px 16px;
  font-size: 13px;
  color: #fff;
  background: var(--ink);
  border-radius: 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

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

/* ---------------- validation / responsive refinements ---------------- */
input[type="text"][aria-invalid="true"] { border-color: #dc2626; }

.btn:disabled,
.preview-copy:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

@media (max-width: 434px) {
  body { padding: 72px 18px 28px; }

  .card { padding: 20px; }

  .swatches { gap: 6px; }

  .size-options { grid-template-columns: minmax(0, 1fr); }
  .custom-size { width: 100%; }
}
