:root {
  --bg: #f3f4f7;
  --ink: #1c2029;
  --panel: #ffffff;
  --panel-2: #f7f8fb;
  --line: #e2e4ea;
  --muted: #6b7280;
  --accent: #3a4de0;
  --accent-ink: #ffffff;
  --danger: #d64545;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(20,24,38,.06);
  --shadow: 0 6px 20px rgba(20,24,38,.08);
  --maxw: 860px;
  --sans: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans);
  color: var(--ink);
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  /* Neutral surface with a faint, small dot texture. */
  background-color: var(--bg);
  background-image: radial-gradient(rgba(28,32,41,.05) 1px, transparent 1.4px);
  background-size: 14px 14px;
  background-position: 0 0;
  background-attachment: fixed;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---- header ---- */
.topbar { max-width: var(--maxw); margin: 0 auto; padding: 28px 18px 8px; text-align: center; }
.brand { display: inline-flex; align-items: center; gap: 9px; font-weight: 700; font-size: 21px; letter-spacing: -.01em; color: var(--ink); }
.brand-mark {
  width: 26px; height: 26px; border-radius: 7px; background: var(--accent);
  position: relative;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 5px; border-radius: 3px;
  background:
    linear-gradient(#fff,#fff) 0 0/45% 55% no-repeat,
    linear-gradient(rgba(255,255,255,.55),rgba(255,255,255,.55)) 100% 100%/45% 40% no-repeat,
    linear-gradient(rgba(255,255,255,.55),rgba(255,255,255,.55)) 0 100%/45% 40% no-repeat,
    linear-gradient(#fff,#fff) 100% 0/45% 55% no-repeat;
}
.tagline {
  color: var(--muted); font-size: 13.5px; margin: 8px auto 0; font-weight: 500;
  display: inline-block;
}

/* ---- app / panels ---- */
.app { max-width: var(--maxw); margin: 0 auto; padding: 14px 16px 40px; display: grid; gap: 14px; }
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}

/* ---- dropzone ---- */
.dropzone {
  border: 1.5px dashed var(--line);
  border-radius: var(--radius);
  background: var(--panel-2);
  padding: 32px 18px; text-align: center; cursor: pointer;
  transition: background .15s, border-color .15s; outline: none;
}
.dropzone:hover, .dropzone:focus-visible { background: #eef0ff; border-color: var(--accent); }
.dropzone.is-drag { background: #e7e9ff; border-color: var(--accent); }
.dropzone:active { transform: scale(.997); }
.dz-icon { width: 30px; height: 30px; color: var(--accent); }
.dz-title { margin: 10px 0 2px; font-weight: 700; font-size: 16px; color: var(--ink); }
.dz-sub { margin: 0; color: var(--muted); font-size: 13px; }

/* ---- tray ---- */
.tray-head { display: flex; align-items: center; justify-content: space-between; margin: 16px 2px 8px; }
.count { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.link-btn { background: none; border: 0; color: var(--muted); font-weight: 600; cursor: pointer; font-size: 12.5px; padding: 4px; }
.link-btn:hover { color: var(--danger); }

.tray { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); }
.thumb {
  position: relative; aspect-ratio: 1; border-radius: var(--radius-sm);
  overflow: hidden; background: var(--panel-2); border: 1px solid var(--line);
  cursor: grab; user-select: none; touch-action: none;
}
.thumb.dragging { opacity: .4; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.thumb .idx {
  position: absolute; left: 5px; top: 5px; min-width: 17px; height: 17px; padding: 0 4px;
  border-radius: 6px; background: rgba(28,32,41,.72); color: #fff; font-size: 10.5px; font-weight: 700;
  display: grid; place-items: center;
}
.thumb .rm {
  position: absolute; right: 4px; top: 4px; width: 20px; height: 20px; border-radius: 50%;
  border: 1px solid var(--line); background: #fff; color: var(--ink); font-size: 13px; line-height: 1; cursor: pointer;
  display: grid; place-items: center; font-weight: 700;
}
.thumb .rm:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

/* ---- options ---- */
.opt-row { display: flex; align-items: center; gap: 14px; padding: 9px 2px; flex-wrap: wrap; }
.opt-row-col { align-items: flex-start; }
.opt-label { width: 66px; flex: none; color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding-top: 6px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 7px 15px; border-radius: 999px; font-size: 13.5px; font-weight: 600; cursor: pointer;
  font-family: inherit; transition: background .12s, color .12s, border-color .12s;
}
.chip:hover { border-color: var(--accent); }
.chip.is-active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.chips-sm .chip { padding: 5px 13px; font-size: 12.5px; }

.title-controls { display: grid; gap: 10px; flex: 1; min-width: 220px; }
.title-input {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px;
  font-size: 13.5px; font-family: inherit; color: var(--ink); background: #fff;
}
.title-input::placeholder { color: #9aa0ad; }
.title-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(58,77,224,.12); }

.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track { width: 40px; height: 22px; border-radius: 999px; background: #fff; border: 1px solid var(--line); position: relative; transition: background .15s, border-color .15s; flex: none; }
.switch-track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); transition: transform .15s, background .15s; }
.switch input:checked + .switch-track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); background: #fff; }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch-text { font-size: 13.5px; font-weight: 500; color: var(--ink); }

/* ---- buttons / actions ---- */
.actions { margin-top: 14px; }
.btn {
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 20px; font-size: 14.5px; font-weight: 700;
  cursor: pointer; transition: transform .05s, box-shadow .1s, background .15s, border-color .15s; font-family: inherit;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); box-shadow: var(--shadow-sm); }
.btn-primary:hover:not(:disabled) { background: #2f3fc4; }
.btn-primary:disabled { opacity: .45; cursor: not-allowed; box-shadow: none; }
.btn-ghost { background: #fff; color: var(--ink); }
.btn-ghost:hover { background: var(--panel-2); border-color: var(--accent); }
#createBtn { width: 100%; }

.privacy-note { color: var(--muted); font-size: 12px; margin: 14px 2px 0; line-height: 1.5; }

/* ---- result ---- */
.result-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.result-title { font-size: 18px; margin: 0; font-weight: 700; letter-spacing: -.01em; color: var(--ink); }
.result-title:empty { display: none; }
.result-actions { display: flex; gap: 8px; margin-left: auto; }
.stage {
  background:
    radial-gradient(rgba(28,32,41,.05) 1px, transparent 1.4px) 0 0/14px 14px,
    var(--panel-2);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 12px;
  display: grid; place-items: center; overflow: auto;
}
.collage { max-width: 100%; box-shadow: var(--shadow); line-height: 0; border-radius: 4px; overflow: hidden; }
.collage canvas, .collage img { display: block; max-width: 100%; height: auto; }
.result-meta { color: var(--muted); font-size: 12px; margin: 10px 2px 0; font-weight: 500; }

/* ---- overlay ---- */
.overlay {
  position: fixed; inset: 0; z-index: 50; display: grid; place-content: center; justify-items: center; gap: 16px;
  background: rgba(243,244,247,.92); backdrop-filter: blur(3px);
}
.spinner { width: 40px; height: 40px; border-radius: 50%; border: 4px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.overlay-text { color: var(--ink); font-weight: 600; font-size: 14.5px; }

/* ---- toast ---- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: #fff; border: 1px solid var(--ink); box-shadow: var(--shadow);
  padding: 11px 18px; border-radius: 10px; font-size: 13.5px; font-weight: 500; z-index: 60; max-width: 90vw; text-align: center;
}
.toast.warn { background: var(--danger); border-color: var(--danger); }

.foot { text-align: center; color: var(--muted); font-size: 12px; padding: 4px 16px 26px; font-weight: 500; }

@media (max-width: 520px) {
  .opt-label { width: 100%; padding-top: 0; }
  .result-actions { width: 100%; }
  .result-actions .btn { flex: 1; }
}
