:root {
  --bg: #0e0f13; --panel: #16181e; --line: #272a33; --ink: #eceef3; --dim: #9096a3;
  --accent: #6d5efc; --field: #0f1116;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font: 13px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "Noto Sans Thai", sans-serif; display: flex; flex-direction: column; height: 100dvh; overflow: hidden; -webkit-tap-highlight-color: transparent; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; background: #20232b; border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; touch-action: manipulation; }
button:hover { background: #292d37; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { filter: brightness(1.1); }
button:disabled { opacity: .6; cursor: default; }
a { color: #a79dff; }

.bar { display: flex; align-items: center; gap: 8px; padding: 8px 14px; padding-top: max(8px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); background: var(--panel); flex: none; }
.brand { font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 8px; margin: 0 auto 0 0; white-space: nowrap; }
.logo { width: 18px; height: 22px; border-radius: 5px; background: linear-gradient(160deg, #8b7dff, #ff7ab6); display: inline-block; flex: none; }
.credit { text-decoration: none; color: #ff8fb8; border: 1px solid #4a2a3a; background: #2a1a24; border-radius: 8px; padding: 6px 10px; white-space: nowrap; }
.credit:hover { background: #3a2030; }
.seg { display: flex; background: var(--field); border: 1px solid var(--line); border-radius: 9px; padding: 2px; overflow-x: auto; scrollbar-width: none; }
.seg::-webkit-scrollbar { display: none; }
.seg button { border: 0; background: none; padding: 5px 11px; border-radius: 7px; color: var(--dim); white-space: nowrap; flex: none; }
.seg button.on { background: #2b2f3a; color: var(--ink); }

main { flex: 1; display: flex; flex-direction: row-reverse; min-height: 0; }
.panel { width: 384px; flex: none; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); min-height: 0; }
.tabs { display: flex; flex: none; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button { flex: 1 1 auto; min-width: 0; border: 0; border-radius: 0; background: none; padding: 10px 5px; white-space: nowrap; color: var(--dim); border-bottom: 2px solid transparent; font-weight: 600; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
.panel-body { flex: 1; overflow-y: auto; padding: 0 16px 24px; padding-bottom: max(24px, env(safe-area-inset-bottom)); -webkit-overflow-scrolling: touch; }
.panel section { padding: 14px 0; border-bottom: 1px solid var(--line); }
h2 { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--dim); margin: 0 0 10px; font-weight: 600; }
dialog h2 { font-size: 16px; letter-spacing: 0; text-transform: none; color: var(--ink); }
.fld { display: block; margin-bottom: 10px; color: var(--dim); }
.fld:last-child { margin-bottom: 0; }
textarea, input[type=text], select { display: block; width: 100%; margin-top: 4px; background: var(--field); border: 1px solid var(--line); border-radius: 8px; padding: 8px; color: var(--ink); }
textarea { resize: vertical; }
textarea:focus, select:focus, input[type=text]:focus, button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.hint { color: var(--dim); font-size: 12px; margin: 6px 0 10px; }
.hint:last-child { margin-bottom: 0; }
.foot { color: var(--dim); font-size: 12px; margin-top: 14px; }
.warn { color: #f5b74e; font-size: 12px; margin: 10px 0 0; min-height: 1em; }
.row { display: flex; align-items: center; gap: 12px; margin: 10px 0; flex-wrap: wrap; }
.row:last-child { margin-bottom: 0; }
.row.end { justify-content: flex-end; margin-bottom: 0; }
.row.end .hint { margin: 0 auto 0 0; }
.grow { flex: 1; min-width: 100px; color: var(--dim); }
.check { display: flex; align-items: center; gap: 6px; color: var(--ink); }
.check input { accent-color: var(--accent); flex: none; }
.color { display: flex; align-items: center; gap: 6px; color: var(--dim); }
input[type=color] { width: 30px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 7px; background: none; }

.slots { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.slot { position: relative; }
.slot .pick { width: 100%; height: 78px; padding: 0; border-style: dashed; background: var(--field) center/contain no-repeat; display: flex; align-items: flex-end; justify-content: center; overflow: hidden; }
.slot .pick span { font-size: 10.5px; background: rgba(14,15,19,.8); padding: 1px 5px; border-radius: 6px 6px 0 0; }
.slot.has .pick { border-style: solid; }
.slot .clear { display: none; position: absolute; top: 3px; right: 3px; padding: 0; width: 20px; height: 20px; border-radius: 10px; line-height: 1; }
.slot.has .clear { display: block; }

.tpls { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 10px; }
.tpl { padding: 3px; border-radius: 9px; background: none; border: 2px solid transparent; display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 10.5px; color: var(--dim); }
.tpl canvas { width: 100%; border-radius: 5px; display: block; }
.tpl.on { border-color: var(--accent); color: var(--ink); }

.themes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.theme { padding: 3px; background: none; border: 0; display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 10.5px; color: var(--dim); }
.theme i { width: 100%; aspect-ratio: 1.25; border-radius: 9px; display: grid; place-items: center; font: 800 20px/1 system-ui, sans-serif; font-style: normal; border: 1px solid var(--line); }
.theme i b { font-weight: 800; }
.theme:hover { background: none; color: var(--ink); }
.starters { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 12px 0; }
.starter { padding: 6px; border-radius: 12px; display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 12px; color: var(--ink); background: var(--field); }
.starter canvas { width: 100%; border-radius: 7px; display: block; }
.starter:hover { border-color: var(--accent); background: var(--field); }
#random2 { flex: none; padding: 6px 9px; }
.swatches { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.swatches button { height: 32px; padding: 0; border-radius: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
h2 + .chips { margin-top: 0; }
.chips button { padding: 5px 11px; border-radius: 999px; color: var(--dim); }
.chips button.on { background: var(--accent); border-color: var(--accent); color: #fff; }

.stage3d { position: relative; height: 270px; margin-top: 14px; border-radius: 14px; overflow: hidden; cursor: pointer; background: radial-gradient(ellipse at 50% 88%, #4a3a8c 0, #241f45 38%, #15161d 75%); user-select: none; -webkit-user-select: none; }
.stage3d canvas { width: 100%; height: 100%; display: block; }
.stage3d .bubble { position: absolute; left: 50%; top: 10px; transform: translateX(-50%); background: #fff; color: #15161d; font-weight: 600; font-size: 12px; padding: 5px 10px; border-radius: 12px; width: max-content; max-width: 90%; text-align: center; opacity: 0; transition: opacity .3s; pointer-events: none; }
.stage3d.ready .bubble { opacity: 1; }
.about p, .about li { color: #c5c9d3; font-size: 13px; }
.about p { margin: 0 0 10px; }
.about ul { margin: 0; padding-left: 18px; }
.about li { margin: 4px 0; }
.about details { border-top: 1px solid var(--line); padding: 8px 0; }
.about details:first-of-type { border-top: 0; padding-top: 0; }
.about summary { cursor: pointer; font-weight: 600; }
.about details p { margin: 6px 0 0; }
.cta { display: block; text-align: center; text-decoration: none; background: linear-gradient(135deg, #ff5f8f, #8b5cf6); color: #fff; font-weight: 700; padding: 11px; border-radius: 10px; }

.stage { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.devbar { flex: none; display: flex; gap: 8px; align-items: center; justify-content: center; padding: 10px 12px 0; }
.devbar .seg { min-width: 0; }
#orient { flex: none; }
#orient.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.canvasWrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 18px 24px; }
#preview { max-width: 100%; max-height: 100%; border-radius: 10px; box-shadow: 0 20px 60px rgba(0,0,0,.5); }
.film { flex: none; display: flex; gap: 10px; align-items: center; padding: 10px 16px; border-top: 1px solid var(--line); background: var(--panel); overflow-x: auto; }
.thumb { position: relative; flex: none; padding: 0; border: 2px solid transparent; border-radius: 8px; background: none; }
.thumb canvas { height: 116px; display: block; border-radius: 6px; }
.thumb.on { border-color: var(--accent); }
.thumb .tools { position: absolute; inset: 3px 3px auto 3px; display: none; justify-content: space-between; gap: 2px; }
.thumb.on .tools { display: flex; }
.thumb .tools span { background: rgba(14,15,19,.85); border-radius: 5px; width: 15px; height: 20px; flex: 1; max-width: 22px; display: grid; place-items: center; font-size: 12px; }
.thumb .tools span:hover { background: var(--accent); }
.add { flex: none; height: 116px; width: 56px; border-style: dashed; font-size: 22px; color: var(--dim); }

.dropveil { position: fixed; inset: 0; background: rgba(109,94,252,.25); backdrop-filter: blur(2px); display: none; place-items: center; z-index: 10; pointer-events: none; }
.dropveil.on { display: grid; }
.dropveil div { background: var(--panel); border: 2px dashed var(--accent); padding: 24px 36px; border-radius: 16px; font-size: 18px; font-weight: 600; }
.toast { position: fixed; left: 50%; top: 64px; transform: translateX(-50%); background: #2b2f3a; border: 1px solid var(--line); padding: 8px 14px; border-radius: 10px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 20; width: max-content; max-width: 88vw; }
.toast.on { opacity: 1; }

dialog { background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: min(660px, 94vw); max-height: 88dvh; }
dialog::backdrop { background: rgba(0,0,0,.6); }
.quick { gap: 6px; }
.sizes { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 20px; margin-top: 10px; }
.sizes h3 { grid-column: 1 / -1; font-size: 12px; color: var(--dim); margin: 10px 0 2px; font-weight: 600; }
.sizes .check small { color: var(--dim); }

/* phones and narrow windows: preview on top, tabbed controls underneath */
@media (max-width: 820px) {
  main { flex-direction: column; }
  .stage { flex: none; height: 46dvh; border-bottom: 1px solid var(--line); }
  .panel { width: auto; flex: 1; border-right: 0; }
  .devbar { justify-content: flex-start; padding: 8px 10px 0; }
  .canvasWrap { padding: 10px 14px; }
  .film { padding: 6px 10px; gap: 6px; }
  .thumb canvas, .add { height: 56px; }
  .add { width: 36px; font-size: 18px; }
  .thumb .tools { display: none !important; }
  .brand { font-size: 14px; }
  .credit span { display: none; }
  .bar { gap: 6px; padding-left: 10px; padding-right: 10px; }
  .bar button, .credit { padding: 6px 9px; }
  textarea, input[type=text], select { font-size: 16px; } /* stops iOS zooming on focus */
  .chips button { padding: 7px 12px; }
  .tabs button { padding: 12px 3px; font-size: 12.5px; }
  .sizes { grid-template-columns: 1fr; }
  .panel-body { padding-left: 14px; padding-right: 14px; }
}
@media (max-width: 380px) {
  .brand span:last-child { display: none; }
  .tpls, .themes { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 820px) and (max-height: 620px) { .stage { height: 40dvh; } }
