*, *::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);
  background: var(--color-bg);
  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;
}

.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);
}

/* Dashed bleed guide — shows the area that will be hidden when the button is made */
.canvas-wrapper::after {
  content: '';
  position: absolute;
  inset: 20px;
  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;
}

/* ---------- 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(--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(--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(--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 { flex: 1; }

/* ---------- 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(--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-add-text {
  background: var(--color-accent);
  color: #fff;
  border-color: var(--color-accent);
}

#btn-add-text:hover {
  background: var(--color-accent-hover);
  border-color: var(--color-accent-hover);
}

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

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

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

#btn-clear {
  color: var(--color-danger);
  border-color: var(--color-danger);
}

#btn-clear:hover {
  background: var(--color-danger);
  color: #fff;
}

/* ---------- 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 .optional {
  color: var(--color-text-secondary);
  font-weight: 400;
}

.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-wrapper {
    width: min(100%, 520px);
  }

  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);
  }
}
