*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
textarea { resize: none; }

html, body {
  height: 100%;
  overflow-x: hidden;
}

body {
  font-family: var(--font-body);
  color: var(--color-text-primary);
}

/* ---------- Page layout — mobile first ---------- */

main {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "canvas"
    "toolbar"
    "form";
  gap: var(--space-lg);
  padding: var(--space-md);
  margin: 0 auto;
  align-items: start;
}

/* ---------- Canvas ---------- */

.canvas-area {
  grid-area: canvas;
  display: flex;
  justify-content: center;
  position: relative;
  overflow: hidden;
  padding: 1px 0;
}

/* Fabric.js positions its hidden textarea in canvas coordinate space (600px),
   which is larger than the CSS-scaled display size. Pin it off-screen so it
   can't contribute to page width. Keyboard input still works via event listeners. */
.canvas-container textarea {
  position: fixed !important;
  top: -9999px !important;
  left: -9999px !important;
  width: 1px !important;
  height: 1px !important;
}

.canvas-frame {
  position: relative;
  display: inline-block;
  width: min(85vw, 360px);
}

.canvas-ctrls-left {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.canvas-ctrls-row {
  display: flex;
  flex-direction: row;
  gap: 4px;
}

.canvas-ctrls-right {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 20;
}

.canvas-ctrls-bottom-left {
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 20;
}

.canvas-ctrls-bottom-right {
  position: absolute;
  bottom: 0;
  right: 0;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 4px;
  z-index: 20;
}

.canvas-ctrls-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.btn-canvas-ctrl {
  width: 46px;
  height: 46px;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  background: var(--color-surface);
  color: var(--color-text-primary);
  cursor: pointer;
  opacity: 0.8;
  position: relative;
}

.btn-label {
  font-family: var(--font-body);
  font-size: 10px;
  font-weight: 500;
  line-height: 1;
  letter-spacing: 0.01em;
  color: currentColor;
  pointer-events: none;
  user-select: none;
}

@keyframes btn-spin {
  to { transform: rotate(360deg); }
}

.btn-canvas-ctrl.is-uploading svg {
  opacity: 0;
}

.btn-canvas-ctrl.is-uploading::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-text-primary);
  border-radius: 50%;
  animation: btn-spin 0.7s linear infinite;
}

.btn-canvas-ctrl:not(:disabled):hover {
  background: var(--color-bg);
}

.btn-canvas-ctrl:disabled {
  opacity: 0.3;
  cursor: default;
  pointer-events: none;
}

/* ---------- Focus ring ---------- */

:focus-visible {
  outline: 2px solid var(--color-text-primary);
  outline-offset: 2px;
}


#font-size {
  flex: 1;
  width: 100%;
}

#mobile-kb {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0.01;
  pointer-events: none;
}

.canvas-wrapper {
  position: relative;
  display: block;
  width: min(85vw, 360px);
  aspect-ratio: 1 / 1;
  border-radius: 50%;
  overflow: hidden;
  box-shadow: var(--shadow-md), 0 0 0 1px var(--color-border);
}

/* Designer guides — for the person making the design (not the printer). The circular
   canvas is the BLEED / ink edge (1.625" — the whole design prints, so fill
   backgrounds/art to the edge). The dashed FACE guide (1.5", ≈92.3% → 3.85% inset)
   marks what's actually visible on the pin; keep text/logos inside it. The CUT line
   (1.75") is a manufacturing concern and is drawn only on the print output, not here.
   See button-geometry.js. */
.canvas-wrapper::after {
  content: '';
  position: absolute;
  inset: 3.85%;
  border-radius: 50%;
  border: 2px dashed white;
  mix-blend-mode: difference;
  pointer-events: none;
  z-index: 10;
}


/* Fabric.js injects a .canvas-container div with inline pixel dimensions —
   override it so the canvas scales to the wrapper size, not 600×600 */
.canvas-wrapper .canvas-container {
  width: 100% !important;
  height: 100% !important;
}

.canvas-wrapper canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* Page-level fade-in when restoring a saved design */
main {
  transition: opacity 3s cubic-bezier(0.45, 0, 0.55, 1);
}

main.is-loading {
  opacity: 0;
  pointer-events: none;
}

.page-spinner {
  position: fixed;
  z-index: 100;
  pointer-events: none;
  transition: opacity 2s cubic-bezier(0.45, 0, 0.55, 1);
}

.page-spinner.is-hiding {
  opacity: 0;
}

.page-spinner svg {
  width: 100%;
  height: 100%;
}


.spinner-orbit {
  transform-origin: 50px 50px;
  animation: arc-spin 2s linear infinite;
}

.spinner-comet {
  fill: none;
  stroke: var(--spinner-color, var(--color-text-secondary));
}


@keyframes arc-spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

/* ---------- Background panels (stacked grid so height stays constant) ---------- */

.bg-panels {
  display: grid;
}

.bg-panels > * {
  grid-area: 1 / 1;
}

.bg-panels .is-hidden {
  visibility: hidden;
  pointer-events: none;
}

#bg-gradient-controls {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

/* ---------- Background switch ---------- */

.bg-switch {
  display: flex;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.bg-switch__option {
  flex: 1;
  display: flex;
  cursor: pointer;
}

.bg-switch__option input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.bg-switch__option span {
  flex: 1;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 500;
  padding: var(--space-sm) var(--space-xs);
  background: var(--color-bg);
  color: var(--spinner-color, var(--color-text-secondary));
  transition: background 0.1s, color 0.1s;
  white-space: nowrap;
}

.bg-switch__option input:checked + span {
  background: var(--color-text-primary);
  color: var(--color-surface);
}

/* ---------- Toolbar ---------- */

.toolbar {
  grid-area: toolbar;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.toolbar-group {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.toolbar-group--tabbed .toolbar-panel {
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

/* ---------- Toolbar tabs ---------- */

.toolbar-tabs {
  display: flex;
  border-bottom: 1px solid var(--color-border);
}

.toolbar-tab {
  flex: 1;
  min-height: 40px;
  padding: var(--space-xs) var(--space-sm);
  font-size: 0.8rem;
  font-weight: 500;
  border: none;
  border-radius: 0;
  background: var(--color-bg);
  color: var(--spinner-color, var(--color-text-secondary));
  cursor: pointer;
  transition: background 0.1s, color 0.1s;
}

.toolbar-tab:hover {
  background: var(--color-surface);
  border-color: transparent;
  color: var(--color-text-primary);
}

.toolbar-tab.active {
  background: var(--color-surface);
  color: var(--color-text-primary);
  border-color: transparent;
  box-shadow: inset 0 -2px 0 var(--color-text-primary);
}


.btn-format {
  width: 44px;
  height: 44px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}

.btn-format b { font-weight: 700; font-style: normal; }
.btn-format i { font-style: italic; font-weight: 400; }

.btn-danger-icon:hover {
  color: var(--color-danger);
  border-color: var(--color-danger);
}

.control-row {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  flex-wrap: wrap;
}

.control-row--fill > * { flex: 1; }

.control-row label,
.control-row span {
  font-size: 0.8rem;
  color: var(--spinner-color, var(--color-text-secondary));
  white-space: nowrap;
  min-width: 44px;
}

/* ---------- Inputs ---------- */

input[type="color"] {
  width: 44px;
  height: 44px;
  padding: 2px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  background: none;
  flex-shrink: 0;
}

#bg-solid-color,
#bg-grad-start,
#bg-grad-end {
  flex: 1;
  width: auto;
}

#bg-grad-angle { flex: 1; }

input[type="number"],
select {
  font-family: var(--font-body);
  font-size: 1rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: var(--space-sm) var(--space-sm);
  background: var(--color-bg);
  color: var(--color-text-primary);
  min-width: 60px;
  min-height: 44px;
  flex: 1;
}

input[type="range"] { flex: 1; cursor: pointer; }

select { cursor: pointer; flex: 0 1 auto; min-width: 0; }
#font-picker { display: none !important; }

/* ---------- Custom font picker ---------- */

.font-picker {
  position: relative;
  flex: 1;
  min-width: 0;
}

.font-picker__trigger {
  width: 100%;
  text-align: left;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm);
  font-size: 1rem;
  min-height: 44px;
  white-space: nowrap;
  overflow: hidden;
}

.font-picker__label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.font-picker__arrow {
  flex-shrink: 0;
  font-family: var(--font-body) !important;
  font-style: normal;
  font-size: 0.75rem;
  opacity: 0.5;
}

.font-picker__list {
  position: fixed;
  z-index: 200;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  max-height: 260px;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: var(--space-xs) 0;
}

.font-picker__option {
  padding: var(--space-sm) var(--space-md);
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1.4;
  color: var(--color-text-primary);
}

.font-picker__option:hover,
.font-picker__option[aria-selected="true"] {
  background: var(--color-bg);
}

@media (min-width: 760px) {
  .font-picker__trigger {
    min-height: unset;
    font-size: 0.85rem;
  }

  .font-picker__option {
    font-size: 1rem;
    padding: 6px var(--space-md);
  }
}

/* ---------- Buttons ---------- */

button {
  font-family: var(--font-body);
  font-size: 0.95rem;
  font-weight: 500;
  padding: var(--space-sm) var(--space-md);
  min-height: 44px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-primary);
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s;
  white-space: nowrap;
  touch-action: manipulation;
}

button:hover {
  background: var(--color-bg);
  border-color: var(--spinner-color, var(--color-text-secondary));
}

button:active {
  background: var(--color-border);
}

button.active {
  background: var(--color-text-primary);
  color: var(--color-surface);
  border-color: var(--color-text-primary);
}


#btn-submit {
  background: var(--color-btn-primary);
  color: var(--color-surface);
  border-color: var(--color-btn-primary);
  font-size: 1rem;
  padding: var(--space-sm) var(--space-lg);
  width: 100%;
}

#btn-submit:hover:not(:disabled) {
  background: var(--color-btn-primary-hover);
  border-color: var(--color-btn-primary-hover);
}

#btn-submit:disabled,
#btn-submit.is-disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#btn-submit.is-disabled:hover {
  background: var(--color-btn-primary);
  border-color: var(--color-btn-primary);
}

.terms-agree {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  font-size: 0.9rem;
  color: var(--color-text-primary);
  cursor: pointer;
}

.terms-agree input[type="checkbox"] {
  flex-shrink: 0;
}

.terms-agree a {
  color: inherit;
  text-decoration: underline;
}


/* ---------- Submission form ---------- */

.submission-form {
  grid-area: form;
  align-self: start;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-md);
}

#submit-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.submission-form h2 {
  font-family: var(--font-display);
  font-size: 1.25rem;
  font-weight: 700;
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.field label {
  font-size: 0.85rem;
  font-weight: 500;
}

.field .label-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.field .instructions {
  color: color-mix(in srgb, var(--spinner-color, var(--color-text-secondary)) 70%, transparent);
  font-weight: 400;
  font-size: 12px;
  margin-right: 4px;
}

.field input[type="text"],
.field textarea {
  font-family: var(--font-body);
  font-size: 1rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: var(--space-sm) var(--space-md);
  background: var(--color-bg);
  color: var(--color-text-primary);
  width: 100%;
  min-height: 44px;
  transition: border-color 0.1s;
}

.field input[type="text"]:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--color-text-primary);
}


/* ---------- Desktop enhancement (≥ 760px) ---------- */

@media (min-width: 760px) {
  main {
    grid-template-columns: 1fr 336px;
    grid-template-areas:
      "canvas  toolbar"
      "canvas  form";
    column-gap: var(--space-xl);
    row-gap: calc(var(--space-xl) / 2);
    padding: var(--space-xl);
    max-width: 1100px;
  }

  .canvas-frame {
    width: min(100%, 520px);
  }

  .canvas-wrapper {
    width: 100%;
  }

  input[type="color"] {
    width: 32px;
    height: 32px;
  }

  input[type="number"],
  select,
  button,
  .field input[type="text"],
  .field textarea {
    min-height: unset;
    font-size: 0.85rem;
  }

  .btn-format {
    width: 32px;
    height: 32px;
    font-size: 0.85rem;
  }

.field input[type="text"],
  .field textarea {
    font-size: 0.95rem;
  }

  #btn-submit {
    font-size: 1rem;
  }

  .submission-form {
    padding: var(--space-lg);
  }
}

/* ---------- Theme toggle ---------- */

.toolbar-group--tabbed {
  position: relative;
}

.btn-theme-toggle {
  position: absolute;
  top: 0;
  right: 8px;
  z-index: 10;
  background: none;
  border: none;
  padding: 0 12px;
  cursor: pointer;
  color: var(--color-text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.6;
  min-width: 44px;
  /* Height matches the tab row (.toolbar-tab min-height) and top:0 sits it on that
     row — previously top:8 + min-height:44 pushed it below the tabs into the panel. */
  height: 40px;
}

.btn-theme-toggle:hover {
  opacity: 1;
}

/* Reserve room at the end of the tab row for the theme toggle so tabs never slide
   under it. Sibling selector scopes this to the public designer only — the admin
   modal injects the same tabs but has no .btn-theme-toggle, so it's unaffected.
   56px = eye width (44) + its right offset (8) + 4px gap. */
.btn-theme-toggle ~ .toolbar-tabs {
  padding-right: 56px;
}


/* ---------- Terms modal ---------- */

.terms-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

.terms-modal[hidden] { display: none; }

.terms-modal__backdrop {
  position: absolute;
  inset: 0;
  background: var(--color-backdrop);
}

.terms-modal__panel {
  position: relative;
  width: min(860px, calc(100vw - 2 * var(--space-lg)));
  height: min(82vh, 900px);
  background: var(--color-surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-paper);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.terms-modal__close {
  position: absolute;
  top: var(--space-sm);
  right: var(--space-sm);
  z-index: 1;
  width: 32px;
  height: 32px;
  border: none;
  background: var(--color-bg);
  color: var(--color-text-primary);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.8;
  transition: opacity 0.15s;
}

.terms-modal__close:hover { opacity: 1; }

.terms-modal__body {
  flex: 1;
  min-height: 0 !important;
  overflow-y: auto;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  bottom: var(--space-lg);
  left: 50%;
  transform: translateX(-50%) translateY(0);
  background: var(--color-text-primary);
  color: var(--color-surface);
  padding: var(--space-sm) var(--space-lg);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.4s, transform 0.4s;
  z-index: 100;
}

.toast.toast--hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(8px);
}

/* ---------- Site footer ---------- */

.site-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-lg) var(--space-md);
  font-size: 1rem;
  color: #ffffff;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.65);
}

@media (min-width: 760px) {
  .site-footer {
    flex-direction: row;
    justify-content: center;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    pointer-events: none;
  }

  .site-footer a,
  .site-footer button {
    pointer-events: auto;
  }

  /* rites stay centered; feedback sits in the right corner */
  .footer-feedback {
    position: fixed;
    right: var(--space-md);
    bottom: var(--space-lg);
  }
}

.site-footer a,
.footer-feedback {
  color: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.footer-feedback {
  opacity: 0.85;
  transition: opacity 0.15s;
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  cursor: pointer;
}

.footer-feedback:hover,
.footer-feedback:active {
  opacity: 1;
  background: none;
}

.footer-eye {
  width: 1.2em;
  height: 1.2em;
  flex-shrink: 0;
}

.footer-megaphone {
  width: 1.3em;
  height: 1.3em;
  flex-shrink: 0;
}

/* ---------- Feedback modal ---------- */

.feedback-panel {
  height: auto;
  max-height: min(82vh, 560px);
  width: min(520px, calc(100vw - 2 * var(--space-lg)));
  padding: var(--space-xl) var(--space-lg) var(--space-lg);
}

.feedback-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  overflow-y: auto;
}

.feedback-title {
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--color-text-primary);
}

.feedback-blurb {
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--color-text-secondary);
}

#feedback-text {
  font-family: var(--font-body);
  font-size: 1rem;
  resize: vertical;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: var(--space-sm) var(--space-md);
  background: var(--color-bg);
  color: var(--color-text-primary);
  width: 100%;
  min-height: 120px;
  transition: border-color 0.1s;
}

#feedback-text:focus {
  outline: none;
  border-color: var(--color-text-primary);
}

#btn-feedback-send {
  align-self: flex-start;
  background: var(--color-btn-primary);
  color: var(--color-surface);
  border: 1px solid var(--color-btn-primary);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  padding: var(--space-sm) var(--space-lg);
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s;
}

#btn-feedback-send:hover:not(:disabled) {
  background: var(--color-btn-primary-hover);
  border-color: var(--color-btn-primary-hover);
}

#btn-feedback-send:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  main {
    transition: none;
  }
  .page-spinner {
    transition: none;
  }
  .spinner-orbit {
    animation: none;
  }
  .btn-canvas-ctrl.is-uploading::after {
    animation: none;
  }
  .toast {
    transition: none;
  }
}
