:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #d9dde3;
  --text: #1c2129;
  --muted: #667085;
  --primary: #2f6fed;
  --primary-text: #ffffff;
  --accent: #12a150;
  --accent-text: #ffffff;
  --danger: #d92d20;
  --checker-a: #ffffff;
  --checker-b: #e3e6ea;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .06);
  --radius: 8px;
  --panel-w: 310px;
  --tools-w: 210px;
  --brand: #b03030;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1c2027;
    --surface-2: #252a33;
    --border: #343a46;
    --text: #e6e9ee;
    --muted: #98a2b3;
    --primary: #5b8cff;
    --accent: #2fbf71;
    --checker-a: #2a2f38;
    --checker-b: #22262e;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 12px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

/* Explicit overrides for the manual light/dark toggle — win over the OS
   preference above once the visitor has picked a side (see js/ui-theme.js). */
:root[data-theme="light"] {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #d9dde3;
  --text: #1c2129;
  --muted: #667085;
  --primary: #2f6fed;
  --accent: #12a150;
  --checker-a: #ffffff;
  --checker-b: #e3e6ea;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .06);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #14171c;
  --surface: #1c2027;
  --surface-2: #252a33;
  --border: #343a46;
  --text: #e6e9ee;
  --muted: #98a2b3;
  --primary: #5b8cff;
  --accent: #2fbf71;
  --checker-a: #2a2f38;
  --checker-b: #22262e;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 12px rgba(0, 0, 0, .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

body { display: flex; flex-direction: column; overflow: hidden; }

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; margin-right: auto; min-width: 0; }
#brand-logo { height: 38px; width: auto; display: block; }
.brand-fallback { font-weight: 800; letter-spacing: .08em; font-size: 15px; color: var(--brand); white-space: nowrap; }
.brand-divider { width: 1px; height: 30px; background: var(--border); }
.product { display: flex; flex-direction: column; line-height: 1.1; }
.product strong { font-size: 19px; letter-spacing: -.01em; }
.product strong::first-letter { color: var(--brand); }
.product small { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .08em; }
.actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.sep { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 6px 12px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font: inherit; cursor: pointer; user-select: none; white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.icon { width: 34px; padding: 0; font-size: 18px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--primary); }
.btn.accent { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.accent:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.block { display: flex; width: 100%; margin-top: 10px; white-space: normal; text-align: center; }

input[type="number"], input[type="text"], select {
  width: 100%; min-height: 34px; padding: 5px 8px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text); font: inherit;
}
input.inline { width: 70px; min-height: 26px; padding: 2px 6px; }
input[type="range"] { width: 100%; accent-color: var(--primary); }
input[type="color"] { width: 44px; height: 34px; padding: 2px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); }
input[type="checkbox"] { accent-color: var(--primary); width: 16px; height: 16px; }

.link { color: var(--primary); text-decoration: underline; cursor: pointer; }
.muted { color: var(--muted); }

/* ---------- Layout ---------- */
.layout { flex: 1; display: grid; grid-template-columns: var(--tools-w) 1fr var(--panel-w); min-height: 0; }

.tools {
  display: flex; flex-direction: column; gap: 1px; padding: 6px 8px 16px;
  background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto;
}
.tools .group {
  margin: 12px 8px 4px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--muted);
}
.tools .group:first-child { margin-top: 6px; }
.tools button {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border: 0; border-radius: var(--radius);
  background: transparent; color: var(--text); font: inherit; font-size: 13.5px; cursor: pointer; text-align: left;
}
.tools button svg { width: 18px; height: 18px; flex: none; color: var(--muted); }
.tools button.active svg { color: var(--primary); }
.tools button:hover { background: var(--surface-2); color: var(--text); }
.tools button.active { background: color-mix(in srgb, var(--primary) 14%, transparent); color: var(--primary); font-weight: 600; }

.stage { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.dropzone {
  flex: 1; margin: 24px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  border: 2px dashed var(--border); border-radius: 16px; color: var(--text); padding: 24px;
}
.dropzone svg { width: 56px; height: 56px; color: var(--primary); }
.dropzone h2 { margin: 12px 0 4px; }
.dropzone p { margin: 4px 0; }
body.dragging .dropzone, body.dragging .viewport { outline: 3px dashed var(--primary); outline-offset: -8px; }

.viewport { flex: 1; overflow: auto; display: grid; place-items: center; padding: 24px; min-height: 0; }
.canvas-wrap {
  position: relative; box-shadow: var(--shadow); line-height: 0; overflow: hidden;
  background-color: var(--checker-a);
  background-image:
    linear-gradient(45deg, var(--checker-b) 25%, transparent 25%, transparent 75%, var(--checker-b) 75%),
    linear-gradient(45deg, var(--checker-b) 25%, transparent 25%, transparent 75%, var(--checker-b) 75%);
  background-size: 16px 16px; background-position: 0 0, 8px 8px;
}
.canvas-wrap canvas, .canvas-wrap img { display: block; width: 100%; height: 100%; }
.canvas-wrap img { position: absolute; inset: 0; background: #fff; }
.canvas-wrap.pixelated canvas { image-rendering: pixelated; }
.canvas-wrap.tool-crop { cursor: crosshair; }
.canvas-wrap.tool-wand { cursor: crosshair; }
.canvas-wrap.tool-brush { cursor: none; }

.crop-box {
  position: absolute; border: 1px solid #fff; outline: 1px dashed #000;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .5); cursor: move;
}
.crop-box::before, .crop-box::after { content: ""; position: absolute; border: 0 solid rgba(255, 255, 255, .5); pointer-events: none; }
.crop-box::before { left: 33.33%; right: 33.33%; top: 0; bottom: 0; border-left-width: 1px; border-right-width: 1px; }
.crop-box::after { top: 33.33%; bottom: 33.33%; left: 0; right: 0; border-top-width: 1px; border-bottom-width: 1px; }
.crop-box .h { position: absolute; width: 14px; height: 14px; background: #fff; border: 1px solid #333; border-radius: 3px; z-index: 1; }
.h.nw { left: 0; top: 0; cursor: nwse-resize; }
.h.n { left: calc(50% - 7px); top: 0; cursor: ns-resize; }
.h.ne { right: 0; top: 0; cursor: nesw-resize; }
.h.w { left: 0; top: calc(50% - 7px); cursor: ew-resize; }
.h.e { right: 0; top: calc(50% - 7px); cursor: ew-resize; }
.h.sw { left: 0; bottom: 0; cursor: nesw-resize; }
.h.s { left: calc(50% - 7px); bottom: 0; cursor: ns-resize; }
.h.se { right: 0; bottom: 0; cursor: nwse-resize; }

.brush-cursor {
  position: absolute; pointer-events: none; border-radius: 50%;
  border: 1px solid #fff; outline: 1px solid #000; transform: translate(-50%, -50%);
}

.docview { flex: 1; overflow: auto; padding: 24px; display: flex; flex-direction: column; align-items: center; gap: 14px; min-height: 0; }
.doc-sheet { max-width: 100%; box-shadow: var(--shadow); background: #fff; line-height: 0; }
.doc-sheet img { display: block; max-width: 100%; height: auto; }
.doc-sheet img[hidden] { display: none; }
.doc-caption { max-width: 760px; text-align: center; }
.doc-caption table { margin: 8px auto; }
.error-text { color: var(--danger); }
table.legend { border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
table.legend th, table.legend td { border: 1px solid var(--border); padding: 3px 10px; text-align: left; }
table.legend th { background: var(--surface-2); }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 2px; vertical-align: -1px; border: 1px solid rgba(0,0,0,.2); }
.tip { background: color-mix(in srgb, var(--primary) 10%, transparent); border-left: 3px solid var(--primary); padding: 8px 10px; border-radius: 4px; font-size: 12.5px; margin: 8px 0; }
.result-box { background: var(--surface-2); border-radius: 6px; padding: 8px 10px; margin-top: 8px; font-size: 12.5px; }
.panel .row > label { min-width: 0; }

.statusbar {
  padding: 4px 12px; font-size: 12px; color: var(--muted);
  border-top: 1px solid var(--border); background: var(--surface); min-height: 26px;
}

/* ---------- Side panel ---------- */
.panel { background: var(--surface); border-left: 1px solid var(--border); overflow-y: auto; padding: 14px 16px 24px; }
.panel h3 { margin: 0 0 8px; font-size: 16px; }
.panel h4 { margin: 18px 0 4px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.panel label { position: relative; display: block; margin: 10px 0 0; font-weight: 500; }
.panel label > input, .panel label > select { margin-top: 4px; }
.panel label > output { position: absolute; right: 0; top: 0; font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }
.panel label.check { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.panel label.check input { margin: 0; }
.hint { color: var(--muted); font-size: 12.5px; margin: 6px 0; }
.row { display: flex; gap: 8px; align-items: flex-end; }
.row > label { flex: 1; }
.row > .btn { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.grid2 .btn { white-space: normal; text-align: center; }
.chips { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-top: 10px; }
.chips > button {
  padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 12px; cursor: pointer;
}
.chips > label.check { margin: 0; }
.segmented { display: flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-top: 8px; }
.segmented button { flex: 1; padding: 7px 4px; border: 0; background: var(--surface); color: var(--text); font: inherit; font-size: 12.5px; cursor: pointer; }
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button.active { background: var(--primary); color: var(--primary-text); }

.progress { position: relative; height: 22px; margin-top: 8px; border-radius: 6px; background: var(--surface-2); overflow: hidden; }
.progress > div { height: 100%; width: 0; background: color-mix(in srgb, var(--primary) 40%, transparent); transition: width .2s; }
.progress > span { position: absolute; inset: 0; display: grid; place-items: center; font-size: 12px; }

/* ---------- Overlays ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: #1c2129; color: #fff; padding: 10px 16px; border-radius: var(--radius);
  box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: 90vw; z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

.busy {
  position: fixed; inset: 0; display: flex; flex-direction: column; gap: 12px; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .35); color: #fff; font-size: 15px; z-index: 30;
}
.spinner { width: 42px; height: 42px; border: 4px solid rgba(255, 255, 255, .3); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Small screens ---------- */
@media (max-width: 900px) {
  body { overflow: auto; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto minmax(55vh, 1fr) auto; }
  .tools { flex-direction: row; border-right: 0; border-bottom: 1px solid var(--border); overflow-x: auto; padding: 6px; }
  .tools .group { display: none; }
  .tools button { flex-direction: column; min-width: 84px; gap: 4px; font-size: 11.5px; text-align: center; }
  .brand-divider, .product small { display: none; }
  .panel { border-left: 0; border-top: 1px solid var(--border); }
  .actions .sep { display: none; }
}

/* ---------- Studio tools ---------- */
textarea { width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); font: inherit; resize: vertical; margin-top: 4px; }
.linkbtn { border: 0; background: none; padding: 0; color: var(--primary); text-decoration: underline; font: inherit; cursor: pointer; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.swatch-btn { position: relative; width: 36px; height: 36px; border-radius: 8px; border: 2px solid var(--border); cursor: pointer; margin: 0 !important; }
.swatch-btn input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.theme-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 6px; }
.theme-grid .btn { min-height: 30px; font-size: 12.5px; padding: 4px 6px; white-space: normal; }
.decor-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 8px; }
.decor-grid button { aspect-ratio: 1; padding: 4px; border: 1px solid var(--border); border-radius: 8px; background: #fff; cursor: pointer; }
.decor-grid button:hover { border-color: var(--primary); }
.decor-grid svg { width: 100%; height: 100%; stroke: none; }
.chips > button.active { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.parts-list { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.part-row { display: flex; align-items: center; gap: 6px; background: var(--surface-2); border-radius: 6px; padding: 4px 6px; }
.part-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.part-name small { display: block; color: var(--muted); font-size: 11.5px; }
.panel label.qty { display: flex; align-items: center; gap: 2px; margin: 0; font-weight: 400; }
.panel label.qty input { width: 58px; margin: 0; min-height: 28px; }
.part-row .btn.icon { width: 28px; min-height: 28px; font-size: 13px; }
.ai-chip { border-color: color-mix(in srgb, var(--primary) 40%, var(--border)); }

/* ---------- Light / dark toggle ---------- */
#btn-theme { position: relative; }
#btn-theme svg { width: 18px; height: 18px; }
#btn-theme .icon-moon { display: none; }
:root[data-theme="dark"] #btn-theme .icon-sun { display: none; }
:root[data-theme="dark"] #btn-theme .icon-moon { display: inline-block; }

/* ---------- Splash screen ---------- */
#splash {
  position: fixed; inset: 0; z-index: 9999;
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(ellipse 900px 600px at 50% 42%, rgba(176, 48, 48, .16), transparent 65%),
    linear-gradient(160deg, #0a0c10 0%, #14171d 55%, #0a0c10 100%);
  overflow: hidden;
  animation: splash-out .7s ease-in 4.3s forwards;
}
#splash.splash-skip { animation-duration: .4s; animation-delay: 0s; }
@keyframes splash-out {
  to { opacity: 0; visibility: hidden; pointer-events: none; }
}

.splash-beam {
  position: absolute; top: -10%; left: 50%; width: 2px; height: 0;
  background: linear-gradient(180deg, transparent, #d94b4b 45%, #ff8a8a 50%, #d94b4b 55%, transparent);
  box-shadow: 0 0 24px 4px rgba(217, 75, 75, .55);
  transform: translateX(-50%) rotate(0deg);
  animation: splash-beam-drop 1.1s cubic-bezier(.2, .8, .2, 1) .15s forwards;
}
@keyframes splash-beam-drop {
  0% { height: 0; opacity: 0; }
  10% { opacity: 1; }
  70% { height: 130%; }
  100% { height: 130%; opacity: .5; }
}

.splash-inner {
  position: relative; z-index: 1;
  display: flex; flex-direction: column; align-items: center; gap: 18px;
  opacity: 0;
  animation: splash-fade-in .7s ease-out .55s forwards;
}
@keyframes splash-fade-in {
  from { opacity: 0; transform: translateY(10px) scale(.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.splash-logo { width: min(46vw, 340px); height: auto; filter: drop-shadow(0 6px 30px rgba(0, 0, 0, .5)); }

.splash-title {
  font-size: clamp(28px, 5vw, 44px); font-weight: 800; letter-spacing: .01em;
  color: #f2f4f7; display: flex; gap: 2px;
}
.splash-title .splash-beam-text {
  color: #ff5c5c;
  background: linear-gradient(90deg, #ff8a8a, #d92d20 60%, #ff5c5c);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.splash-tag {
  font-size: 13.5px; letter-spacing: .12em; text-transform: uppercase;
  color: #8b93a3; opacity: 0; animation: splash-fade-in .6s ease-out 1s forwards;
}

@media (prefers-reduced-motion: reduce) {
  #splash, .splash-beam, .splash-inner, .splash-tag { animation: none !important; }
  #splash { opacity: 0; visibility: hidden; pointer-events: none; transition: none; }
}
.ai-chip.on { background: color-mix(in srgb, var(--accent) 15%, var(--surface)); border-color: var(--accent); }
.ai-box { border: 1px dashed var(--border); border-radius: var(--radius); padding: 2px 10px 8px; margin-top: 12px; }
.ai-box h4 { margin-top: 10px; }
.panel a { color: var(--primary); }
