:root {
  --bg: #14161c;
  --bar: #1c1f28;
  --line: #2c3140;
  --ink: #e8ecf5;
  --muted: #98a1b5;
  --accent: #5aa9ff;
  --accent-ink: #06101f;
  --warn: #ff7b72;
  --radius: 10px;
  --tap: 44px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ---------- bars ---------- */

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bar);
  border-bottom: 1px solid var(--line);
  flex: none;
}

.bar--top { padding-top: max(8px, env(safe-area-inset-top)); }

.bar--bottom {
  border-bottom: none;
  border-top: 1px solid var(--line);
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  align-items: center;
}

.group {
  display: flex;
  gap: 4px;
  padding: 3px;
  background: #171a22;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.group--right { margin-left: auto; border: none; background: none; padding: 0; }

/* ---------- buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: var(--tap);
  height: var(--tap);
  padding: 0 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.btn:hover { background: #262b38; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn[disabled] { opacity: .35; pointer-events: none; }

.btn.is-active {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn--primary { background: var(--accent); color: var(--accent-ink); }
.btn--primary:hover { background: #7bbcff; }
.btn--warn:hover { background: #3a2326; color: var(--warn); }

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

.btn .px { font-size: 10px; font-weight: 500; opacity: .6; }

/* ---------- canvas stage ---------- */

#stage {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  min-height: 0;
  background:
    radial-gradient(120% 120% at 50% 0%, #1b1f2a 0%, var(--bg) 70%);
}

#art {
  display: block;
  border-radius: 4px;
  box-shadow: 0 0 0 1px var(--line), 0 18px 40px rgba(0,0,0,.45);
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  -ms-interpolation-mode: nearest-neighbor;
  touch-action: none;
  cursor: crosshair;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
}

/* ---------- color controls ---------- */

.swatch--current {
  position: relative;
  width: var(--tap);
  height: var(--tap);
  flex: none;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 0 2px var(--line);
  cursor: pointer;
}

.swatch--current input {
  position: absolute;
  inset: -8px;
  width: calc(100% + 16px);
  height: calc(100% + 16px);
  opacity: 0;
  border: 0;
  padding: 0;
  cursor: pointer;
}

#currentSwatch {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.recents, .palette {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.recents { padding-right: 8px; border-right: 1px solid var(--line); min-height: 28px; }
.palette { flex: 1 1 auto; }

.chip {
  width: 28px;
  height: 28px;
  border: 0;
  padding: 0;
  border-radius: 6px;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.14);
}

.chip.is-active { box-shadow: 0 0 0 2px var(--accent); }

/* ---------- modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal[hidden] { display: none; }

.modal__backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.6); }

.modal__panel {
  position: relative;
  width: min(420px, 100%);
  max-height: 100%;
  overflow: auto;
  padding: 18px;
  border-radius: 14px;
  background: var(--bar);
  border: 1px solid var(--line);
  box-shadow: 0 24px 60px rgba(0,0,0,.6);
}

.modal__panel h2 { margin: 0 0 12px; font-size: 17px; }

.preview {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  border-radius: 10px;
  background-color: #cfd4de;
  background-image:
    linear-gradient(45deg, #a8afbd 25%, transparent 25%, transparent 75%, #a8afbd 75%),
    linear-gradient(45deg, #a8afbd 25%, transparent 25%, transparent 75%, #a8afbd 75%);
  background-size: 16px 16px;
  background-position: 0 0, 8px 8px;
}

#preview {
  max-width: 100%;
  max-height: 42vh;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
}

.field {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 14px 0 6px;
  font-size: 13px;
  color: var(--muted);
}

.field select {
  flex: 1 1 auto;
  height: 38px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #171a22;
  color: var(--ink);
  font: inherit;
}

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

.modal__actions {
  display: flex;
  flex-direction: column-reverse;   /* primary action on top, Close at the bottom */
  gap: 8px;
  margin-top: 16px;
}

.modal__actions .btn { width: 100%; }

/* ---------- narrow screens ---------- */

@media (max-width: 560px) {
  .bar { gap: 6px; padding: 6px; }
  .btn { padding: 0 8px; min-width: 38px; height: 38px; }
  .btn svg { width: 20px; height: 20px; }
  .group { gap: 2px; }
  .group--right { margin-left: 0; }   /* keep Save on the same row as history */
  .lbl { display: none; }
  .chip { width: 26px; height: 26px; }
  .recents { display: none; }
  #stage { padding: 8px; }
}
