:root {
  --bg: #17171c;
  --panel: #1f1f26;
  --panel-2: #232329;
  --border: #2e2e37;
  --text: #e8e8ee;
  --text-dim: #9a9aa6;
  --accent: #6d4aff;
  --accent-soft: rgba(109, 74, 255, .35);
  --radius: 12px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: hidden;          /* the page itself never scrolls (desktop) */
}

.site-layout {
  position: relative;
  width: 100%;
  min-width: 0;
}

.ad-rail {
  position: fixed;
  top: 50%;
  left: max(24px, calc(50% - 784px));
  width: 160px;
  min-height: 600px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  transform: translateY(-50%);
}
.ad-rail-right {
  left: auto;
  right: max(24px, calc(50% - 784px));
}
.ad-rail .adsbygoogle {
  display: block;
  width: 160px;
  min-height: 600px;
}

.app {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 24px;
  width: min(1040px, 100%);
  margin: 0 auto;
  min-width: 0;
  height: 85vh;
  min-height: 0;                /* let children shrink below content size */
}

.site-copyright {
  position: fixed;
  right: 24px;
  bottom: 20px;
  left: 24px;
  color: var(--text-dim);
  font-size: 1rem;
  text-align: center;
}
.about-link {
  margin-left: 8px;
  border: 0;
  background: none;
  color: var(--text);
  cursor: pointer;
  font: inherit;
  text-decoration: underline;
}
.about-link:hover { color: #fff; }

.about-dialog {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  width: min(520px, calc(100% - 32px));
  padding: 32px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 16px 48px rgb(0 0 0 / 45%);
}
.about-dialog::backdrop { background: rgb(0 0 0 / 65%); }
.about-dialog h2 { margin-bottom: 16px; padding-right: 24px; }
.about-dialog p {
  color: var(--text-dim);
  line-height: 1.6;
  text-align: justify;
}
.about-dialog p + p { margin-top: 12px; }
.about-dialog a {
  color: white;
}
.dialog-images {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 16px 0;
}
.dialog-close {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1.6rem;
  line-height: 1;
}
.dialog-close:hover { background: var(--panel-2); color: var(--text); }

.dialog-img {
  display: block;
  width: 100%;
  /* max-width: 10em; */
  object-fit: cover;
}

/* ---------- Left: preview canvas ---------- */
.preview-wrap {
  background: var(--panel);
  border-radius: 16px;
  padding: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  overflow: hidden;
}
.mobile-ad {
  display: none;
}
#mainCanvas {
  width: min(100%, 80vh);
  aspect-ratio: 1;
  background: var(--panel-2);
  border: 2px solid #d3c8df;
  border-radius: 8px;
  cursor: pointer;
}

/* ---------- Right: sidebar ---------- */
.sidebar {
  background: var(--panel);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;               /* allow sidebar to be constrained by .app */
}
.sidebar h1 {
  font-size: 1.15rem;
  margin: 14px 0 4px;
  text-align: center;
  flex-shrink: 0;
}

/* ---------- The scrolling region ---------- */
.sidebar-scroll {
  flex: 1 1 0;                 /* basis 0: size by container, not content */
  overflow-y: auto;
  padding: 18px;
  min-height: 0;               /* critical: inner scroll instead of growing */
}

.section-label {
  font-size: 1rem;
  font-weight: bold;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin: 14px 0 8px;
}
.divider { border: none; border-top: 1px solid var(--border); margin: 14px 0; }

/* ---------- Pattern + palette grids: 3 per row ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.thumb {
  aspect-ratio: 1;
  border: 2px solid transparent;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  padding: 0;
  overflow: hidden;
  transition: transform .12s ease;
}
.thumb:hover { transform: scale(1.04); }
.thumb.selected { outline: 2px solid var(--accent); outline-offset: 2px; }
.thumb canvas { width: 100%; height: 100%; display: block; }

/* palette presets: 5 vertical stripes */
.palette-chip {
  aspect-ratio: 3 / 2;
  display: flex;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  cursor: pointer;
  padding: 0;
  background: none;
}
.palette-chip span { flex: 1; }
.palette-chip.selected { outline: 2px solid var(--accent); outline-offset: 2px; }
.palette-group { margin: 10px 0 6px; }
.group-title {
  list-style: none;
  font-size: 1rem;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}
.group-title::-webkit-details-marker { display: none; }
.group-title::before {
  content: ">";
  display: inline-block;
  margin-right: 6px;
  transition: transform .12s ease;
}
.palette-group[open] .group-title::before { transform: rotate(90deg); }
.palette-group .grid { margin-top: 6px; }

/* ---------- Pattern-specific controls ---------- */
.customize-control {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin: 10px 0;
  color: var(--text-dim);
  font-size: .85rem;
}
.customize-control input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 9px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
}
.customize-control:has(input[type="checkbox"]) {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.customize-control input[type="checkbox"] {
  width: auto;
  flex: 0 0 auto;
  margin: 0;
}
.customize-control input[type="color"] {
  width: 84px;
  height: 32px;
  padding: 3px;
  align-self: flex-start;
}
.range-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}
.range-value {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* ---------- Color editor row ---------- */
.editor-row {
  display: flex;
  gap: 12px;
  justify-content: center;
  padding: 14px 18px;
  position: relative;
  flex-shrink: 0;              /* dots row never gets squashed */
}
.color-dot {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 2px solid white;
  cursor: pointer;
  padding: 0;
  transition: transform .12s ease;
  display: inline-block;
}
.color-dot:hover, .color-dot.active { border-color: var(--accent); transform: scale(1.08); }
.color-dot input { position: absolute; width: 0; height: 0; opacity: 0; }

/* ---------- Sticky footer ---------- */
.sidebar-footer {
  display: flex;
  gap: 10px;
  padding: 14px 18px;
  border-top: 1px solid var(--border);
  background: var(--panel-2);
  flex-shrink: 0;
}
button.btn { cursor: pointer; border: none; border-radius: 10px; font-weight: 600; }
.btn-secondary {
  flex: 1;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 10px;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  padding: 10px;
}
.btn-primary:hover { filter: brightness(1.1); }
.download-menu {
  flex: 2;
  display: flex;
  position: relative;
}
.download-menu .download-main {
  flex: 1;
  border-radius: 10px 0 0 10px;
}
.download-menu .download-toggle {
  width: 34px;
  padding: 10px 0;
  border-left: 1px solid rgb(255 255 255 / 35%);
  border-radius: 0 10px 10px 0;
}
.download-options {
  position: absolute;
  z-index: 5;
  right: 0;
  bottom: calc(100% + 8px);
  min-width: 150px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  box-shadow: 0 8px 24px rgb(0 0 0 / 25%);
}
.download-options button {
  display: block;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font: inherit;
}
.download-options button:hover { background: var(--panel); }

/* toast */
.bmc-button {
  position: fixed;
  right: 22px;
  bottom: 18px;
  z-index: 20;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px 8px 10px;
  border-radius: 999px;
  border: 1px solid rgba(0, 0, 0, 0.08);
  background: #ffdd00;
  color: #111111;
  text-decoration: none;
  font-weight: 800;
  font-size: 0.88rem;
  letter-spacing: 0.01em;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  transition: transform .12s ease, box-shadow .12s ease, filter .12s ease;
}
.bmc-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 14px 32px rgba(0, 0, 0, 0.32);
  filter: brightness(1.02);
}
.bmc-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(17, 17, 17, 0.08);
  color: #111111;
  flex-shrink: 0;
}
.bmc-icon svg {
  width: 18px;
  height: 18px;
  display: block;
}
.bmc-label {
  line-height: 1;
  white-space: nowrap;
}

#toast {
  position: fixed; bottom: 24px; left: 50%;
  transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 8px 16px; border-radius: 8px; font-size: .85rem;
  opacity: 0; transition: opacity .3s; pointer-events: none;
}
#toast.show { opacity: 1; }

/* ---------- Mobile: stacked layout, page scrolls naturally ---------- */
@media (max-width: 800px) {
  html, body {
    height: auto;                 /* undo the desktop height constraint */
    overflow: visible;            /* page flows and scrolls naturally */
  }
  body {
    min-height: 100vh;            /* keep background painted, but growable */
    padding: 12px;
    align-items: flex-start;
    flex-direction: column;
  }
  .site-layout {
    width: 100%;
  }
  .app {
    grid-template-columns: 1fr;
    height: auto;                 /* was calc(100vh - 48px) — release it */
    max-height: none;
    min-height: 0;
    gap: 16px;
  }

  /* sidebar: no inner scroll, full natural height */
  .sidebar {
    overflow: visible;
  }
  .sidebar-scroll {
    overflow: visible;            /* was overflow-y: auto */
    flex: 0 0 auto;               /* was flex: 1 1 0 — size to content */
    padding: 12px;
    min-height: 0;
  }

  /* preview sized sensibly, stacked on top */
  .preview-wrap { padding: 12px; }
  #mainCanvas {
    width: 100%;
    max-width: 420px;
  }
  .mobile-ad {
    display: block;
    min-height: 50px;
    overflow: hidden;
  }
  .mobile-ad .adsbygoogle {
    display: block;
    width: 100%;
  }

  /* editor dots and footer flow normally — no sticky tricks */
  .sidebar-footer { position: static; }
  .editor-row { padding: 10px 12px; }
  .color-dot { width: 32px; height: 32px; }
  .sidebar-footer { padding: 10px 12px; }

  /* keep grids tight; 4-per-row is still nice on phones */
  .grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
  }

  .dialog-images {
    grid-template-columns: 1fr;
  }
  .dialog-img {
    width: 100%;
  }

  .bmc-button {
    right: 12px;
    bottom: 12px;
    padding: 7px 12px 7px 8px;
    font-size: 0.76rem;
  }
  .bmc-icon {
    width: 22px;
    height: 22px;
  }
  #toast { bottom: 24px; }
  .site-copyright {
    position: static;
    margin: auto 0 4px;
  }
}

@media (max-width: 1439px) {
  .ad-rail {
    display: none;
  }
}