/* SovereignFlags · flag painter — dark, dependency-free UI */

:root {
  color-scheme: dark;
  --bg: #0a0d12;
  --panel: #12171f;
  --panel-2: #171e28;
  --panel-3: #1d2632;
  --line: #212b38;
  --line-2: #2c3846;
  --text: #dde4ee;
  --muted: #8794a8;
  --accent: #4cc9f0;
  --accent-soft: #123440;
  --accent-ink: #04222c;
  --danger: #ff7a7a;
  --radius: 13px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100vh;
  background:
    radial-gradient(1200px 620px at 18% -10%, #16222e 0%, transparent 62%),
    radial-gradient(900px 520px at 96% 0%, #1a2033 0%, transparent 58%),
    var(--bg);
  color: var(--text);
  font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

code, kbd, .hex, .stat, .tag, .stage-label, .status { font-family: var(--mono); }

/* ── top bar ─────────────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 14px clamp(14px, 2.4vw, 30px);
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(19, 25, 34, .92), rgba(10, 13, 18, .55));
  backdrop-filter: blur(6px);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.brand-mark {
  width: 34px; height: 34px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}
.brand-mark .flag { fill: rgba(76, 201, 240, .16); }

.brand-text h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.brand-text p {
  margin: 1px 0 0;
  color: var(--muted);
  font-size: 12.5px;
}
.brand-text code {
  color: #a9b8cc;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0 4px;
  font-size: 11.5px;
}

.top-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.divider { width: 1px; height: 22px; background: var(--line-2); margin: 0 2px; }

/* ── controls ────────────────────────────────────────────── */

button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-radius: 9px;
  padding: 7px 12px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease, color .12s ease;
  white-space: nowrap;
}
button:hover { background: var(--panel-3); border-color: #3a4a5e; }
button:active { transform: translateY(1px); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button svg {
  width: 15px; height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

button.primary {
  background: linear-gradient(180deg, #5ad3f7, #35a9cf);
  border-color: #6fd9f9;
  color: var(--accent-ink);
  font-weight: 650;
}
button.primary:hover { background: linear-gradient(180deg, #70ddfb, #3fb6dc); }

button.ghost { background: transparent; }
button.ghost:hover { background: var(--panel-2); }

.icon-btn { padding: 6px; border-radius: 8px; }
.icon-btn[disabled] { opacity: .35; cursor: default; }
.icon-btn[disabled]:hover { background: var(--panel-2); border-color: var(--line-2); }

.chip {
  padding: 4px 9px;
  font-size: 12px;
  border-radius: 7px;
  font-family: var(--mono);
  color: var(--muted);
}
.chip.is-active {
  color: var(--accent);
  border-color: #2d7d95;
  background: var(--accent-soft);
}

button.mini { padding: 5px 10px; font-size: 12.5px; }

kbd {
  font-size: 10.5px;
  color: var(--muted);
  border: 1px solid var(--line-2);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 3px;
  line-height: 1.35;
}

select, input[type="text"] {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-radius: 8px;
  padding: 6px 8px;
}
select { cursor: pointer; padding-right: 6px; }

input[type="text"].hex {
  width: 8.2ch;
  text-transform: lowercase;
  padding: 5px 7px;
  letter-spacing: .02em;
}
input[type="text"].hex:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* colour input styled as a swatch */
.swatch {
  position: relative;
  width: 30px; height: 28px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--line-2);
  flex: none;
  background-image:
    linear-gradient(45deg, #2a3441 25%, transparent 25%, transparent 75%, #2a3441 75%),
    linear-gradient(45deg, #2a3441 25%, transparent 25%, transparent 75%, #2a3441 75%);
  background-size: 10px 10px;
  background-position: 0 0, 5px 5px;
  background-color: #10151d;
}
.swatch input[type="color"] {
  position: absolute;
  inset: -25%;
  width: 150%; height: 150%;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}
.swatch.small { width: 28px; height: 26px; }

.switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.switch input { accent-color: var(--accent); margin: 0; }

/* ── layout ──────────────────────────────────────────────── */

.layout {
  display: grid;
  grid-template-columns: minmax(430px, 1.05fr) minmax(380px, .95fr);
  gap: clamp(14px, 1.6vw, 22px);
  align-items: start;
  padding: clamp(14px, 2vw, 26px);
  max-width: 1580px;
  margin: 0 auto;
}

.boot-warning {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: baseline;
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid rgba(255, 196, 87, .38);
  background: rgba(52, 38, 10, .7);
  color: #ffe0a3;
  font-size: 13px;
}
.boot-warning strong { color: #ffc857; }
.boot-warning code {
  font-family: var(--mono);
  font-size: 12px;
  background: rgba(0, 0, 0, .35);
  border-radius: 4px;
  padding: 1px 5px;
}
body.is-ready .boot-warning { display: none; }

.card {
  background: linear-gradient(180deg, rgba(24, 31, 42, .92), rgba(17, 22, 30, .92));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: clamp(12px, 1.3vw, 18px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 22px 50px -30px rgba(0, 0, 0, .9), inset 0 1px 0 rgba(255, 255, 255, .03);
}

.card-head {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 24px;
}
.card-head h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 620;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: #b9c6d8;
}
.spacer { flex: 1 1 auto; }

.tag, .stat {
  font-size: 11px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 6px;
  background: rgba(11, 15, 21, .6);
}
.stat { border-color: transparent; background: none; padding: 0; }

/* ── toolbar ─────────────────────────────────────────────── */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  align-items: center;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(10, 14, 19, .5);
}

.tool-group .label {
  font-size: 11.5px;
  color: var(--muted);
  padding: 0 5px 0 4px;
}

.tool {
  border: 1px solid transparent;
  background: transparent;
  padding: 6px 9px;
  border-radius: 8px;
  font-size: 13px;
  gap: 6px;
}
.tool:hover { background: var(--panel-3); border-color: transparent; }
.tool.is-active {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: #2a7186;
}
.tool.is-active kbd { color: #8fd8ea; border-color: #2a7186; }

/* ── paint bar ───────────────────────────────────────────── */

.paint-bar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.color-field {
  display: flex;
  align-items: center;
  gap: 8px;
}

.label {
  font-size: 11.5px;
  letter-spacing: .03em;
  text-transform: uppercase;
  color: var(--muted);
}

.palette { display: flex; flex-wrap: wrap; gap: 4px; }
.palette button {
  width: 18px; height: 18px;
  padding: 0;
  border-radius: 5px;
  border: 1px solid rgba(255, 255, 255, .16);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .35);
}
.palette button:hover { transform: scale(1.14); border-color: #fff; }

/* ── canvas stage ────────────────────────────────────────── */

.checker { background-color: #131a24; }

.canvas-stage {
  position: relative;
  border: 1px solid var(--line-2);
  border-radius: 10px;
  overflow: hidden;
  line-height: 0;
  background-color: #131a24;
  touch-action: none;
}

#editor-canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 64 / 40;
  cursor: crosshair;
  image-rendering: pixelated;
}

.canvas-stage.is-drop {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 2px rgba(76, 201, 240, .45);
}

.stage-label {
  position: absolute;
  left: 8px; bottom: 6px;
  font-size: 10.5px;
  color: rgba(190, 205, 225, .55);
  background: rgba(6, 9, 13, .62);
  border-radius: 4px;
  padding: 1px 6px;
  pointer-events: none;
  line-height: 1.5;
}

/* ── bottom split fields ─────────────────────────────────── */

.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.field-block {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.row { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }

.file-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 11px;
  border-radius: 9px;
  border: 1px dashed var(--line-2);
  background: rgba(10, 14, 19, .5);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: border-color .12s ease, background .12s ease;
}
.file-btn:hover { border-color: var(--accent); background: var(--accent-soft); }
.file-btn svg {
  width: 14px; height: 14px;
  fill: none; stroke: currentColor; stroke-width: 1.7;
  stroke-linecap: round; stroke-linejoin: round;
}

.status {
  margin: 0;
  font-size: 11.5px;
  color: var(--muted);
  min-height: 17px;
  border-top: 1px solid var(--line);
  padding-top: 9px;
}
.status.flash { color: var(--accent); }
.status.warn { color: var(--danger); }

/* ── preview column ──────────────────────────────────────── */

.preview-wrap {
  width: min(100%, 400px);
  border: 1px solid var(--line-2);
  border-radius: 10px;
  overflow: hidden;
  line-height: 0;
}
#preview-canvas { display: block; width: 100%; height: auto; image-rendering: pixelated; }

.model-head { margin-top: 4px; }

.model-stage {
  position: relative;
  border: 1px solid var(--line-2);
  border-radius: 10px;
  overflow: hidden;
  aspect-ratio: 1 / 1;
  background: #0c1017;
  touch-action: none;
}
#model-canvas { display: block; width: 100%; height: 100%; cursor: grab; }
#model-canvas:active { cursor: grabbing; }

.stage-hint, .cloth-hint, .model-error {
  position: absolute;
  pointer-events: none;
  font-size: 11px;
  border-radius: 6px;
  padding: 3px 8px;
  left: 8px;
}
.stage-hint {
  bottom: 7px;
  font-family: var(--mono);
  color: rgba(190, 205, 225, .5);
  background: rgba(6, 9, 13, .55);
}
.cloth-hint {
  top: 8px;
  color: #ffe0a3;
  background: rgba(52, 38, 10, .82);
  border: 1px solid rgba(255, 196, 87, .35);
  max-width: calc(100% - 16px);
}
.model-error {
  inset: auto 8px 8px 8px;
  top: 50%;
  transform: translateY(-50%);
  left: 8px;
  right: 8px;
  padding: 12px 14px;
  color: #ffc4c4;
  background: rgba(60, 16, 20, .92);
  border: 1px solid rgba(255, 122, 122, .4);
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.5;
  pointer-events: auto;
}

.help {
  border-top: 1px solid var(--line);
  padding-top: 8px;
  font-size: 12.5px;
  color: var(--muted);
}
.help summary {
  cursor: pointer;
  color: #9fb0c6;
  font-size: 12.5px;
  list-style: none;
}
.help summary::before { content: "▸ "; color: var(--accent); }
.help[open] summary::before { content: "▾ "; }
.help ul { margin: 8px 0 0; padding-left: 18px; display: grid; gap: 5px; }
.help li::marker { color: #39c9f0; }
.help code {
  color: #a9b8cc;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  font-size: 11px;
}

/* ── export dialog ───────────────────────────────────────── */

.dialog {
  border: 1px solid var(--line-2);
  border-radius: 16px;
  padding: 0;
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  background: linear-gradient(180deg, #18202b, #10151d);
  color: var(--text);
  box-shadow: 0 40px 90px -30px #000, inset 0 1px 0 rgba(255, 255, 255, .05);
}
.dialog::backdrop { background: rgba(4, 6, 10, .68); backdrop-filter: blur(3px); }
.dialog[open] { animation: dialog-in .16s ease-out; }
@keyframes dialog-in {
  from { opacity: 0; transform: translateY(8px) scale(.99); }
}

.dialog-inner {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 20px 22px 18px;
  max-height: calc(100vh - 66px);
  overflow: auto;
}

.dialog-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}
.dialog-head h2 {
  margin: 0 0 3px;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.dialog-head p {
  margin: 0;
  color: var(--muted);
  font-size: 12.5px;
  max-width: 60ch;
}
.dialog-head .icon-btn { margin-left: auto; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field input[type="text"] {
  font-family: var(--mono);
  font-size: 14px;
  padding: 9px 11px;
  border-radius: 9px;
}
.field input[type="text"]:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.derived {
  margin: 0;
  display: grid;
  gap: 4px;
  font-family: var(--mono);
  font-size: 12px;
}
.derived > div { display: flex; gap: 10px; }
.derived dt {
  color: var(--muted);
  min-width: 6.5ch;
  text-transform: uppercase;
  font-size: 10.5px;
  letter-spacing: .06em;
  padding-top: 1px;
}
.derived dd { margin: 0; color: #a9c4d8; overflow-wrap: anywhere; }

.json-preview, .install {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(8, 11, 16, .55);
  overflow: hidden;
}
.json-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 11px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, .02);
}
.json-preview pre, .install pre {
  margin: 0;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.65;
  color: #b9c9dc;
  overflow: auto;
  tab-size: 2;
}
.json-preview pre { max-height: 230px; }
.install { padding-bottom: 2px; }
.install .label { display: block; padding: 8px 12px 0; }
.install pre { color: #93a6bd; white-space: pre-wrap; }

.dialog-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 4px;
  border-top: 1px solid var(--line);
  margin-top: 2px;
  padding: 14px 0 0;
}
.dialog-foot button[disabled] { opacity: .4; cursor: default; }

/* ── responsive ──────────────────────────────────────────── */

@media (max-width: 1080px) {
  .layout { grid-template-columns: 1fr; }
}
@media (max-width: 700px) {
  .split { grid-template-columns: 1fr; }
  .top-actions { width: 100%; }
  .brand-text p { display: none; }
  .tool span { display: none; }
  .tool kbd { display: none; }
  .tool { padding: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
