:root {
  --bg: #0f0f14;
  --panel: #17171f;
  --card: #1d1d27;
  --line: #2c2c3a;
  --text: #ecebf3;
  --muted: #9a98ad;
  --accent: #f2b544;
  --accent-2: #e2557a;
  --ok: #3ccf8e;
  --warn: #f2b544;
  --bad: #ef5b5b;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; }
body {
  background: radial-gradient(1200px 700px at 80% -10%, #2a1d3a 0%, transparent 60%), var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

/* ---------- top bar ---------- */
.topbar {
  padding: 18px 28px;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; gap: 14px; align-items: center; }
.brand h1 { margin: 0; font-size: 20px; letter-spacing: .3px; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.logo-mark {
  width: 38px; height: 38px; border-radius: 10px; flex: none;
  background:
    radial-gradient(circle at 30% 30%, #fff 0 18%, transparent 19%),
    conic-gradient(from 210deg, var(--accent), var(--accent-2), #6b5cff, var(--accent));
  box-shadow: 0 6px 20px rgba(226, 85, 122, .35);
}

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  gap: 0;
  min-height: calc(100vh - 77px);
}
.panel {
  border-right: 1px solid var(--line);
  padding: 18px;
  overflow-y: auto;
  max-height: calc(100vh - 77px);
  display: flex; flex-direction: column; gap: 14px;
}
.stage {
  padding: 28px;
  display: flex; justify-content: center; align-items: flex-start;
}
.stage-inner {
  position: sticky; top: 28px;
  width: min(100%, 600px);
  display: flex; flex-direction: column; gap: 14px;
}

button, .btn, .tile, label.check { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }

/* ---------- cards & controls ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 14px 16px;
  display: flex; flex-direction: column; gap: 10px;
}
.card h2 {
  margin: 0 0 2px; font-size: 12px; text-transform: uppercase;
  letter-spacing: 1.2px; color: var(--muted); font-weight: 600;
}
label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--muted); }
.lbl { margin-bottom: -4px; }
input[type=text], input[type=email], input[type=tel], input[type=url], textarea, select {
  background: #121219; color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; font: inherit; font-size: 13.5px; width: 100%;
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 0; border-color: transparent; }

.fields { display: flex; flex-direction: column; gap: 10px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
label.check input { accent-color: var(--accent); width: 16px; height: 16px; }

label.color {
  flex-direction: row; align-items: center; justify-content: space-between;
  background: #121219; border: 1px solid var(--line); border-radius: 8px; padding: 5px 6px 5px 10px; color: var(--text);
}
input[type=color] {
  -webkit-appearance: none; appearance: none; border: 0; padding: 0;
  width: 34px; height: 26px; border-radius: 6px; background: none; cursor: pointer;
}
input[type=color]::-webkit-color-swatch-wrapper { padding: 0; }
input[type=color]::-webkit-color-swatch { border: 1px solid rgba(255,255,255,.2); border-radius: 6px; }
input[type=color]::-moz-color-swatch { border: 1px solid rgba(255,255,255,.2); border-radius: 6px; }

label.range { gap: 2px; }
label.range output { float: right; color: var(--text); font-variant-numeric: tabular-nums; }
label.range { display: block; }
input[type=range] { width: 100%; accent-color: var(--accent); margin: 6px 0 0; }

.hint { margin: -2px 0 0; font-size: 12px; color: var(--muted); opacity: .85; }

/* segmented buttons */
.layout > *, .card, .fields, .row2 > * { min-width: 0; }
.seg button { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 4px; background: #121219; padding: 4px; border-radius: 10px; border: 1px solid var(--line); }
.seg-wrap { grid-auto-flow: row; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); }
.seg button {
  background: transparent; color: var(--muted); border: 0; border-radius: 7px;
  padding: 7px 6px; font: inherit; font-size: 12.5px; cursor: pointer; white-space: nowrap;
}
.seg button:hover { color: var(--text); background: #1f1f2b; }
.seg button.on { background: var(--text); color: #15151c; font-weight: 600; }

/* buttons */
.btn {
  border: 1px solid var(--line); background: #22222e; color: var(--text);
  border-radius: 9px; padding: 9px 14px; font: inherit; font-size: 13px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  flex-direction: row;
}
.btn:hover { border-color: #44445a; background: #282836; }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #1a0f14;
  border: 0; font-weight: 700;
}
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.file-btn { color: var(--text); font-size: 13px; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* art & preset grids */
.art-grid, .presets { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 8px; }
.tile {
  position: relative; border: 2px solid transparent; border-radius: 10px; padding: 0;
  background: #121219; cursor: pointer; overflow: hidden; aspect-ratio: 1;
  display: block; color: var(--text);
}
.tile { min-width: 0; }
.tile canvas, .tile img { width: 100%; height: 100%; display: block; object-fit: cover; }
.tile span {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 4px 4px;
  font-size: 10.5px; text-align: center; font-weight: 600;
  background: linear-gradient(transparent, rgba(0,0,0,.8));
}
.tile.on { border-color: var(--accent); }
.tile:hover { border-color: #55556c; }
.tile.on:hover { border-color: var(--accent); }
.presets .tile { background: #fff; }

/* preview */
.canvas-wrap {
  position: relative; border-radius: 16px; overflow: hidden;
  background: repeating-conic-gradient(#1a1a22 0 25%, #20202a 0 50%) 0 0/20px 20px;
  box-shadow: 0 30px 80px rgba(0,0,0,.5), 0 0 0 1px var(--line);
}
.canvas-wrap canvas { width: 100%; height: auto; display: block; }
.drop-hint {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(15, 15, 20, .8); color: var(--text); font-size: 16px; font-weight: 600;
  border: 2px dashed var(--accent); border-radius: 16px; opacity: 0; pointer-events: none; transition: opacity .15s;
}
.canvas-wrap.drag .drop-hint { opacity: 1; }

/* scan status */
.scan {
  display: flex; gap: 12px; align-items: center; padding: 12px 14px;
  border-radius: 12px; background: var(--card); border: 1px solid var(--line);
}
.scan .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex: none; }
.scan strong { display: block; font-size: 14px; }
.scan small { color: var(--muted); font-size: 12.5px; }
.scan[data-state=ok] .dot { background: var(--ok); box-shadow: 0 0 12px var(--ok); }
.scan[data-state=ok] { border-color: color-mix(in srgb, var(--ok) 40%, var(--line)); }
.scan[data-state=warn] .dot { background: var(--warn); box-shadow: 0 0 12px var(--warn); }
.scan[data-state=warn] { border-color: color-mix(in srgb, var(--warn) 40%, var(--line)); }
.scan[data-state=bad] .dot { background: var(--bad); box-shadow: 0 0 12px var(--bad); }
.scan[data-state=bad] { border-color: color-mix(in srgb, var(--bad) 50%, var(--line)); }
.scan[data-state=checking] .dot { animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .3; } }

.export { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.export label { flex: 0 0 auto; }
.export select { width: auto; }
.export .primary { flex: 1; min-width: 160px; padding: 11px 14px; }
.meta { margin: 0; color: var(--muted); font-size: 12px; text-align: center; }

[hidden] { display: none !important; }

/* ================= mobile (portrait phones & small tablets) ================= */
@media (max-width: 860px) and (orientation: portrait), (max-width: 560px) {
  body { font-size: 15px; }
  .topbar { padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top)); }
  .brand h1 { font-size: 18px; }
  .brand p { font-size: 12.5px; }
  .logo-mark { width: 32px; height: 32px; }

  html, body { overflow-x: hidden; }
  .layout { grid-template-columns: minmax(0, 1fr); min-height: 0; }

  /* Preview stays pinned at the top while you scroll the controls. */
  .stage {
    order: 1; position: sticky; top: 0; z-index: 5;
    padding: 10px 16px 10px;
    background: rgba(15, 15, 20, .96);
    border-bottom: 1px solid var(--line);
  }
  .stage-inner { position: static; width: 100%; gap: 8px; align-items: center; }
  .canvas-wrap { width: min(100%, 36svh, 420px); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.45), 0 0 0 1px var(--line); }
  .scan { width: 100%; padding: 8px 12px; gap: 10px; }
  .scan strong { font-size: 13.5px; }
  .scan small { font-size: 12px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .meta { display: none; }

  /* Export actions become a bottom bar. */
  .export {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    margin: 0; padding: 10px 16px; padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: rgba(23, 23, 31, .97); border-top: 1px solid var(--line);
    flex-wrap: nowrap; align-items: center; gap: 8px;
  }
  .export label { font-size: 0; gap: 0; }           /* hide the "Size" word, keep the select */
  .export select { font-size: 14px; padding: 10px 6px; max-width: 96px; }
  .export .primary { min-width: 0; padding: 12px 10px; }
  .export .btn { padding: 12px 10px; flex: none; }
  .export .primary { flex: 1 1 auto; white-space: nowrap; }
  .export.has-share #copyBtn { display: none; }   /* Share covers it on phones */

  .panel {
    order: 2; max-height: none; overflow: visible; border-right: 0;
    padding: 14px 16px calc(88px + env(safe-area-inset-bottom));
  }
  .card { padding: 14px; }

  /* 16px inputs stop iOS from zooming in on focus; bigger tap targets. */
  input[type=text], input[type=email], input[type=tel], input[type=url], textarea, select { font-size: 16px; padding: 10px 12px; }
  .seg button { min-height: 40px; font-size: 13.5px; }
  .seg-wrap { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
  .btn { min-height: 42px; }
  input[type=range] { height: 28px; }
  input[type=color] { width: 40px; height: 30px; }
  .art-grid, .presets { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .drop-hint { display: none; }
}

@media (max-width: 400px) {
  .export:has(#shareBtn:not([hidden])) #copyBtn { display: none; }
  .export .primary { min-width: 120px; }
}

@media (max-width: 380px) {
  .row2 { grid-template-columns: 1fr; }
  .art-grid, .presets { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .seg { grid-auto-flow: row; grid-template-columns: repeat(2, 1fr); }
}

/* Landscape phones: keep side-by-side, but fit the preview to the short screen. */
@media (max-height: 520px) and (orientation: landscape) {
  .topbar { display: none; }
  .layout { min-height: 100vh; grid-template-columns: minmax(280px, 1fr) minmax(0, 1fr); }
  .panel { max-height: 100vh; padding-left: max(16px, env(safe-area-inset-left)); }
  .stage { padding: 12px 16px; padding-right: max(16px, env(safe-area-inset-right)); }
  .stage-inner { top: 12px; gap: 8px; }
  .canvas-wrap { width: min(100%, 58vh); margin: 0 auto; }
  .meta { display: none; }
}
