/* === PLB BRAND TOKENS === */
:root {
  --plb-purple:       #24135f;
  --plb-purple-dark:  #1a0d47;
  --plb-lavender:     #b4b5df;
  --plb-lavender-bg:  #f4f3fb;
  --plb-text:         #121212;
  --plb-text-mid:     #555;
  --plb-text-light:   #888;
  --plb-border:       #e0dff0;
  --plb-bg:           #f7f6fc;
  --plb-white:        #ffffff;
}

/* === RESET & BASE === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Cabin', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--plb-bg);
  color: var(--plb-text);
  line-height: 1.5;
}

.container {
  max-width: 1040px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

/* === HEADER === */
header { text-align: center; margin-bottom: 2rem; }

.header-top {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  margin-bottom: 0.75rem;
}

.plb-brand {
  display: flex;
  justify-content: center;
}
.plb-logo-img {
  height: 180px;
  width: auto;
}

.faq-btn {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  background: var(--plb-purple);
  color: #fff;
  font-family: 'Cabin', sans-serif;
  font-weight: 700;
  font-size: 0.9rem;
  padding: 0.5rem 1.2rem;
  border-radius: 999px;
  text-decoration: none;
  letter-spacing: 0.04em;
  box-shadow: 0 2px 8px rgba(36,19,95,0.2);
  transition: background 0.2s, box-shadow 0.2s;
}
.faq-btn:hover {
  background: var(--plb-purple-dark);
  box-shadow: 0 4px 14px rgba(36,19,95,0.3);
}

h1 {
  font-family: 'Assistant', sans-serif;
  font-size: 1.8rem;
  font-weight: 600;
  color: var(--plb-purple);
}
.subtitle { color: #5a4a9e; font-weight: 600; margin-top: 0.25rem; font-size: 0.95rem; }

/* === HERO SPLIT === */
#hero-split {
  margin-bottom: 2rem;
  text-align: center;
}

#hero-wrap {
  position: relative;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  border-radius: 12px;
  overflow: hidden;
  cursor: col-resize;
  user-select: none;
  box-shadow: 0 4px 24px rgba(36,19,95,0.18);
  /* padding-top creates square aspect ratio without collapsing */
  padding-top: min(560px, 100%);
  height: 0;
}

#hero-photo-canvas,
#hero-mosaic-canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

#hero-mosaic-canvas {
  /* clipped to right half by JS */
}

#hero-divider {
  position: absolute;
  top: 0;
  width: 3px;
  height: 100%;
  background: #fff;
  box-shadow: 0 0 8px rgba(0,0,0,0.35);
  transform: translateX(-50%);
  pointer-events: none;
}

#hero-handle {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #fff;
  color: var(--plb-purple);
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  font-weight: 700;
  pointer-events: none;
}

#hero-caption {
  margin-top: 0.85rem;
  font-size: 0.95rem;
  color: var(--plb-text-mid);
  font-style: italic;
}

/* === UPLOAD SECTION === */
#drop-zone {
  border: 2px dashed #bbb;
  border-radius: 12px;
  padding: 3rem 2rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  background: #fff;
}
#drop-zone:hover,
#drop-zone.drag-over {
  border-color: var(--plb-purple);
  background: #faf8ff;
}
.drop-icon { font-size: 2.5rem; margin-bottom: 0.5rem; }
.drop-text { font-size: 1.05rem; color: #555; }
.drop-or { color: #999; font-size: 0.85rem; margin: 0.5rem 0; }
#browse-btn {
  background: var(--plb-purple);
  color: #fff;
  border: none;
  padding: 0.6rem 1.5rem;
  border-radius: 6px;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.2s;
}
#browse-btn:hover { background: var(--plb-purple-dark); }

/* === HIDDEN STATE === */
.hidden { display: none !important; }

/* === CROP EDITOR === */
#crop-section { margin-top: 1.5rem; }

.crop-header { text-align: center; margin-bottom: 1rem; }
.crop-header h3 { font-size: 1.1rem; font-weight: 600; color: #333; }
.crop-hint { color: #999; font-size: 0.82rem; margin-top: 0.2rem; }

#crop-wrapper {
  position: relative;
  width: 480px;
  height: 480px;
  margin: 0 auto;
  border-radius: 8px;
  overflow: hidden;
  cursor: grab;
  background: #1a1a1a;
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
}
#crop-wrapper:active { cursor: grabbing; }
#crop-canvas {
  display: block;
  width: 480px;
  height: 480px;
  touch-action: none;
}

#crop-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  margin: 1rem auto 0;
  max-width: 400px;
}
#zoom-out-btn, #zoom-in-btn {
  width: 34px;
  height: 34px;
  border: 2px solid #ccc;
  border-radius: 6px;
  background: #fff;
  font-size: 1.2rem;
  font-weight: 700;
  color: #555;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.2s, color 0.2s;
  line-height: 1;
}
#zoom-out-btn:hover, #zoom-in-btn:hover {
  border-color: var(--plb-purple);
  color: var(--plb-purple);
}
#zoom-slider {
  flex: 1;
  accent-color: var(--plb-purple);
}
.grid-toggle-label {
  font-size: 0.82rem;
  color: #666;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  margin-left: 0.5rem;
  cursor: pointer;
  white-space: nowrap;
}
.grid-toggle-label input { accent-color: var(--plb-purple); }

#crop-actions {
  text-align: center;
  margin-top: 1.25rem;
  display: flex;
  gap: 1rem;
  justify-content: center;
}
#confirm-crop-btn {
  background: var(--plb-purple);
  color: #fff;
  border: none;
  padding: 0.7rem 2rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
#confirm-crop-btn:hover { background: var(--plb-purple-dark); }
#reupload-btn {
  background: #fff;
  color: #666;
  border: 2px solid #ddd;
  padding: 0.7rem 1.5rem;
  border-radius: 8px;
  font-size: 0.95rem;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s;
}
#reupload-btn:hover { border-color: #bbb; color: #333; }

/* === PREVIEW === */
#preview-section { margin-top: 1.5rem; }

.preview-row {
  display: flex;
  gap: 2rem;
  justify-content: center;
  flex-wrap: wrap;
}
.preview-panel {
  background: #fff;
  border-radius: 10px;
  padding: 1rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  text-align: center;
}
.preview-panel h3 {
  font-size: 0.9rem;
  color: #666;
  margin-bottom: 0.5rem;
  font-weight: 600;
}
.preview-panel canvas {
  display: block;
  width: 100%;
  max-width: 480px;
  height: auto;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  image-rendering: auto;
}
.preview-panel {
  flex: 1 1 300px;
  max-width: 500px;
}
.mosaic-wrap {
  position: relative;
  display: block;
}

.mosaic-spinner {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.82);
  border-radius: 4px;
  gap: 14px;
}

.mosaic-spinner.hidden {
  display: none;
}

.spinner-ring {
  width: 52px;
  height: 52px;
  border: 5px solid #e0e0e0;
  border-top-color: var(--plb-purple);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.spinner-label {
  font-size: 0.88rem;
  color: #555;
  margin: 0;
  font-style: italic;
}

.placeholder-text {
  color: #aaa;
  font-size: 0.85rem;
  margin-top: 0.5rem;
  font-style: italic;
}

/* === ADJUSTMENTS === */
#adjustments {
  background: #fff;
  border-radius: 10px;
  padding: 1rem 1.5rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  max-width: 520px;
  margin: 0 auto 1.25rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.5rem;
  align-items: center;
}
.slider-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1 1 200px;
}
.slider-group label {
  font-size: 0.82rem;
  font-weight: 600;
  color: #555;
  min-width: 130px;
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

/* Info icon + tooltip */
.info-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--plb-lavender);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  cursor: default;
  position: relative;
  flex-shrink: 0;
  line-height: 1;
}
.info-icon:hover {
  background: var(--plb-purple);
}
.info-icon .tooltip {
  display: none;
  position: absolute;
  left: 22px;
  top: 50%;
  transform: translateY(-50%);
  background: #2d2d2d;
  color: #f0f0f0;
  font-size: 0.78rem;
  font-weight: 400;
  line-height: 1.5;
  padding: 10px 13px;
  border-radius: 8px;
  width: 280px;
  white-space: normal;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  z-index: 100;
  pointer-events: none;
}
.info-icon .tooltip::before {
  content: '';
  position: absolute;
  left: -6px;
  top: 50%;
  transform: translateY(-50%);
  border: 6px solid transparent;
  border-right-color: #2d2d2d;
  border-left: none;
}
.info-icon:hover .tooltip {
  display: block;
}
.slider-group input[type="range"] {
  flex: 1;
  accent-color: var(--plb-purple);
}
.slider-group > span {
  font-size: 0.8rem;
  color: #888;
  min-width: 28px;
  text-align: right;
}
.select-group {
  flex: 1 1 200px;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.select-group label {
  font-size: 0.82rem;
  font-weight: 600;
  color: #555;
}
.select-group select {
  flex: 1;
  padding: 0.3rem 0.5rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 0.82rem;
  background: #fff;
}

/* === CONTROLS === */
#controls {
  text-align: center;
  margin: 1.5rem 0;
}
#generate-btn {
  background: var(--plb-purple);
  color: #fff;
  border: none;
  padding: 0.75rem 2rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
#generate-btn:hover { background: var(--plb-purple-dark); }
#generate-btn:disabled {
  background: #b0b0b0;
  cursor: not-allowed;
}

/* === RESULTS === */
#results-section { margin-top: 1.5rem; }

#brick-counts {
  background: #fff;
  border-radius: 10px;
  padding: 1.25rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  max-width: 520px;
  margin: 0 auto;
}
#brick-counts h3 {
  font-size: 1rem;
  margin-bottom: 0.75rem;
  font-weight: 600;
}

#counts-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
#counts-table th {
  text-align: left;
  padding: 0.4rem 0.5rem;
  border-bottom: 2px solid #eee;
  color: #888;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
}
#counts-table td {
  padding: 0.5rem;
  border-bottom: 1px solid #f0f0f0;
}
.col-swatch { width: 32px; }
.swatch {
  display: inline-block;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  border: 1px solid rgba(0,0,0,0.12);
}
.usage-bar {
  background: #eee;
  border-radius: 3px;
  height: 8px;
  width: 100%;
  max-width: 100px;
  overflow: hidden;
}
.usage-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.3s ease;
}

#total-count {
  margin-top: 0.75rem;
  font-weight: 600;
  text-align: right;
  font-size: 0.9rem;
  color: #555;
}

/* === ACTION BUTTONS === */
#action-buttons {
  text-align: center;
  margin-top: 1.5rem;
  display: flex;
  gap: 1rem;
  justify-content: center;
  flex-wrap: wrap;
}
#download-btn {
  background: var(--plb-purple);
  color: #fff;
  border: none;
  padding: 0.75rem 2rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
#download-btn:hover { background: var(--plb-purple-dark); }

#reset-btn {
  background: #fff;
  color: #666;
  border: 2px solid #ddd;
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s;
}
#reset-btn:hover { border-color: #bbb; color: #333; }

/* === TOAST / STATUS === */
#status-msg {
  text-align: center;
  margin-top: 1rem;
  font-size: 0.9rem;
  color: #c0392b;
}

/* === REGION COHERENCE CONTROL === */
.coherence-group {
  border-top: 1px solid #e8e8e8;
  padding-top: 8px;
  margin-top: 4px;
}
.coherence-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  cursor: pointer;
}
.coherence-label input[type="checkbox"] {
  width: 15px;
  height: 15px;
  cursor: pointer;
}

/* === SLIDER ACTION BUTTONS (Undo / Reset to Auto) === */
#slider-actions {
  display: flex;
  gap: 8px;
  margin: 10px 0 4px;
}
#slider-actions button {
  flex: 1;
  padding: 7px 10px;
  font-size: 0.82rem;
  font-weight: 600;
  border-radius: 7px;
  border: 1.5px solid #ccc;
  background: #f7f7f7;
  color: #444;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
#slider-actions button:hover:not(:disabled) {
  background: #ececec;
  border-color: #aaa;
  color: #111;
}
#slider-actions button:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}
#undo-btn { border-color: #b0b8c8; }
#undo-btn:hover:not(:disabled) { background: #eef1f7; border-color: #7a90b5; color: #2a4070; }
#reset-auto-btn { border-color: #b8c8b0; }
#reset-auto-btn:hover:not(:disabled) { background: #eef7ee; border-color: #6a9b6a; color: #2a5a2a; }

/* === VARIATION THUMBNAILS STRIP === */
#variations-section {
  margin: 1.2rem 0 0.6rem;
  background: #fff;
  border: 1px solid #e8e8e8;
  border-radius: 10px;
  padding: 0.9rem 1rem;
}

.variations-header {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  margin-bottom: 0.75rem;
}

.variations-title {
  font-weight: 700;
  font-size: 0.9rem;
  color: #333;
  white-space: nowrap;
}

.variations-hint {
  font-size: 0.78rem;
  color: #888;
}

#variations-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}

#variations-strip::-webkit-scrollbar {
  height: 5px;
}
#variations-strip::-webkit-scrollbar-track { background: #f0f0f0; border-radius: 3px; }
#variations-strip::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
#variations-strip::-webkit-scrollbar-thumb:hover { background: #aaa; }

.variation-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  scroll-snap-align: start;
  cursor: pointer;
  border-radius: 7px;
  padding: 4px;
  border: 2px solid transparent;
  background: #fafafa;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.variation-card:hover {
  border-color: #bbb;
  background: #f0f0f0;
  transform: translateY(-1px);
}

.variation-card.selected {
  border-color: var(--plb-purple);
  background: var(--plb-lavender-bg);
}

.variation-card.loading {
  opacity: 0.45;
  cursor: wait;
}

.variation-card img {
  width: 120px;
  height: 120px;
  display: block;
  border-radius: 4px;
  image-rendering: pixelated;
}

.variation-label {
  font-size: 0.68rem;
  color: #666;
  text-align: center;
  white-space: nowrap;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.variation-card.selected .variation-label {
  color: var(--plb-purple);
  font-weight: 600;
}

/* === MOBILE RESPONSIVE === */
@media (max-width: 540px) {
  /* Header: stack logo above FAQ button */
  .header-top {
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
  }
  .faq-btn {
    position: static;
    transform: none;
  }
  .plb-logo-img {
    height: 110px;
  }

  /* Tighten main padding */
  main {
    padding: 0 0.75rem;
  }

  /* Heading */
  h1 {
    font-size: 1.4rem;
  }
  .subtitle {
    font-size: 0.88rem;
  }

  /* Sliders: stack vertically */
  #adjustments {
    padding: 0.75rem 1rem;
    gap: 0.5rem;
  }
  .slider-group,
  .select-group {
    flex: 1 1 100%;
  }
  .slider-group label {
    min-width: 110px;
    font-size: 0.78rem;
  }

  /* Tooltip: flip to above on mobile to avoid off-screen */
  .info-icon .tooltip {
    left: 50%;
    top: auto;
    bottom: calc(100% + 8px);
    transform: translateX(-50%);
    width: 220px;
  }
  .info-icon .tooltip::before {
    left: 50%;
    top: 100%;
    bottom: auto;
    transform: translateX(-50%);
    border: 6px solid transparent;
    border-top-color: #2d2d2d;
    border-right: none;
    border-bottom: none;
  }

  /* Buttons */
  #action-buttons {
    flex-direction: column;
    align-items: center;
  }
  #download-btn,
  #reset-btn {
    width: 100%;
    max-width: 320px;
  }

  /* Variation thumbnails: smaller on mobile */
  .variation-card img {
    width: 90px;
    height: 90px;
  }
  .variation-label {
    max-width: 90px;
  }
}
