*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-size: 16px;
  font-family: "DM Sans", Helvetica, Arial, sans-serif;
  background: #fff;
}

.app {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 360px;
  padding: 20px;
  background: #fff;
  display: flex;
  flex-direction: column;
  gap: 48px;
  position: relative;
  z-index: 100;
  max-height: 100vh;
  overflow:scroll;
}

.sidebar-top {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sidebar-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.app-title {
  margin: 0;
  font-size: 42px;
  font-weight: 800;
  line-height: 1;
}

.panel {
  padding-top: 8px;
  border-top: 2px solid #000;
}

/* Non-accordion Save panel keeps flex layout directly */
section.panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Accordion panels use details/summary */
details.panel {
  display: block;
}

.panel-title,
summary.panel-title {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 700;
}

summary.panel-title {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 0;
  user-select: none;
  margin-bottom: 0;
}

summary.panel-title::-webkit-details-marker {
  display: none;
}

summary.panel-title::after {
  content: '+';
  font-size: 22px;
  font-weight: 300;
  line-height: 1;
  opacity: 0.5;
}

details.panel[open] > summary.panel-title::after {
  content: '−';
}

.panel-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 14px;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.field-row.color {
    gap: 24px;
}

.field-row.small {
  justify-content: flex-start;
  gap: 12px;
  font-size: 16px;
}

.field-column {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field-label {
  font-size: 16px;
  padding-bottom: 4px;
  display: block;
}

.inline-field {
  display: flex;
  align-items: center;
  gap: 8px;
}

p.field-note {
    color: #999;
    font-size: 14px;
    margin-top: 2px;
}

.inline-field label {
  white-space: nowrap;
}

.inline-field input[type="range"] {
  flex: 1; /* slider expands */
}

#palette-size-label,
#pickup-width-label,
#pickup-height-label,
#pickup-pen-width-label,
#pickup-pen-height-label {
  min-width: 3ch;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.sidebar label {
  font-size: 16px;
}

.sidebar input[type="text"],
.sidebar select {
  width: 100%;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid #999;
  font-size: 16px;
  background: #fff;
}

.sidebar .base-color {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.color-chip {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid rgba(0,0,0,0.3);
  background: 000;
  cursor: pointer;
}

.color-chip:focus{
  outline: 2px solid rgba(0,0,0,0.3);
  outline-offset: 2px;
}

#iro-baseA, #iro-baseB {
  width: 140px;
}

.sidebar input[type="range"] {
  width: 100%;
  accent-color: #000;
  transform: translateY(-2px);
}

.random-controls {
  padding: 15px 15px;
  border-radius: 8px;
  background: #eaeaea;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.btn {
  display: inline-block;
  padding: 12px 14px;
  border-radius: 999px;
  border: none;
  background: #000;
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}

.btn-primary {
  width: 100%;
  margin-top: 8px;
}

.btn-full {
  width: 100%;
  margin-top: 4px;
}

.btn-secondary {
  background: #fff;
  border: 1px solid #000;
  color: #000;
}

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

.random-actions .btn {
  flex: 1 1 auto;
}
.btn:hover {
  opacity: 0.8;
}

.sidebar input[type="checkbox"] {
  width: 20px;
  height: 20px;
}

.sidebar input[type="radio"] {
  width: 16px;
  height: 16px;
  accent-color: #000;
  flex-shrink: 0;
}

.sidebar input[type="number"] {
  width: 80px;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid #999;
  font-size: 16px;
  background: #fff;
}

.canvas-area.pickup-mode {
  cursor: crosshair;
}

.canvas-area {
  flex: 1;
  min-width: 0;
  height: 100vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #d9d9d9;
}

#canvas-container {
  position: relative;
  padding: 40px;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #ddd;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}

canvas {
  width: auto;
  height: auto;
  display: block;
  /*object-fit: contain;*/
}

#canvas-scale-wrap {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: top left;
  z-index: 1;
}

.swatch-btn {
  position: absolute;
  z-index: 50;
  pointer-events: auto;
  padding: 0;
  margin: 0;
  border: none;
  background: rgba(255,255,255,0.0);
  color:rgba(0,0,0,0.0);
  cursor: pointer;
}

#thread-picker {
  z-index: 100;
}

.iro-popover{
  position: fixed;
  z-index: 999;
  background: #fff;
  border: 1px solid #ccc;
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  overflow: visible;
}

.iro-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 10px;
  max-width: 180px;
}

.iro-swatch{
  width: 18px;
  height: 15px;
  border-radius: 3px;
  border: 1px solid rgba(0,0,0,0.3);
  cursor: pointer;
}

#add-swatch-btn {
  margin-top: 8px;
  display: block;
  border-radius: 20px;
  background: #fff;
  border: 1px solid #000;
  cursor: pointer;
}

/* Info Panel */
.app-info, .info-close {
  border-radius: 50%;
  width: 20px;
  height: 40px;
  inline-size: 40px;
  background: #000;
  color: #fff;
  text-align: center;
  vertical-align: middle;
  position: absolute;
  top: 20px;
  right: 20px;
  border: none;
  font-size: 18px;
}

.app-info:hover, .info-close:hover {
  opacity: 0.8;
  cursor: pointer;
}

.info-panel {
  position: fixed;
  inset: 0;
  z-index: 999;
  pointer-events: none;
  transition: opacity 0.5s ease;
}

.info-panel.open::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.3);
}

.info-panel-inner {
  position: relative;
  height: 100%;
  display: flex;
  transform: translateX(-100%);
  transition: transform 0.5s ease-out;
  width: 500px;
  background: #fff;
  padding: 20px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 40px;
  opacity: 1;
}

.info-panel.open {
  opacity: 1;
  pointer-events: auto;
}
.info-panel.open .info-panel-inner {
  transform: translateX(0);
}
.info-panel p {
  line-height: 1.2;
  font-size: 20px;
  max-width: 430px;
}

.info-credit, .info-credit a {
  margin-top: auto;
  font-family: "DM Mono", monospace;
  color: #999;
}

p.info-credit, .info-credit a {
    font-size: 14px;
}

.info-credit a:hover {
    color: #000;
}

/* ── Mobile drawer ─────────────────────────────────────────────────────────── */

/* Toggle button — hidden on desktop, floats top-left on mobile */
.sidebar-toggle {
  display: none;
}

/* Backdrop — hidden until drawer opens */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 190;
}

.sidebar-overlay.active {
  display: block;
}

/* Close button — only shown on mobile */
.sidebar-close {
  display: none;
}

@media (max-width: 768px) {
  /* Sidebar becomes a fixed off-screen drawer */
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: 300px;
    height: 100dvh;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.28s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.18);
    gap: 24px;
    padding: 16px;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  /* Shrink type for controls */
  .app-title {
    font-size: 32px;
  }

  .sidebar label,
  .sidebar select,
  .sidebar input[type="text"],
  .sidebar input[type="number"],
  .field-label,
  .panel-title,
  .btn,
  p.field-note {
    font-size: 14px;
  }

  .sidebar input[type="checkbox"] {
    width: 16px;
    height: 16px;
  }

  /* Pull header back to flex-start so ? and ✕ can cluster on the right */
  .sidebar-header {
    justify-content: flex-start;
  }

  /* Move ? button inline so close sits just to its right */
  .app-info {
    position: static;
    width: 32px;
    height: 32px;
    inline-size: 32px;
    font-size: 16px;
    flex-shrink: 0;
    top: unset;
    right: unset;
    margin-left: auto;
  }

  /* Close button — visible inside sidebar on mobile */
  .sidebar-close {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid #000;
    background: transparent;
    color: #000;
    font-size: 16px;
    cursor: pointer;
  }

  .sidebar-close:hover {
    background: #000;
    color: #fff;
  }

  /* Canvas fills the whole screen */
  .canvas-area {
    width: 100vw;
  }

  /* "Controls" toggle — visible, top-left of canvas area */
  .sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 14px;
    left: 14px;
    z-index: 150;
    height: 36px;
    padding: 0 14px;
    border-radius: 999px;
    border: none;
    background: #000;
    color: #fff;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  }

  .sidebar-toggle:hover {
    opacity: 0.8;
  }

  /* Info panel — match sidebar width so it doesn't go off screen */
  .info-panel-inner {
    width: 300px;
  }

  .info-panel p {
    font-size: 16px;
  }
}

/* Flip cloth button — sits at the bottom-centre of the canvas area */
.flip-btn {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  background: #000;
  color: #fff;
  border: none;
  white-space: nowrap;
  font-size: 14px;
  padding: 8px 18px;
}

.flip-btn.active {
  background: #444;
}