*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg:      #04040a;
  --surface: rgba(6, 6, 18, 0.88);
  --rim:     rgba(0, 245, 255, 0.18);
  --cyan:    #00f5ff;
  --pink:    #ff006e;
  --green:   #39ff14;
  --purple:  #bf00ff;
  --orange:  #ff6600;
  --yellow:  #ffe600;
  --red:     #ff3131;
  --white:   #ffffff;
  --dim:     rgba(0, 245, 255, 0.35);
  --text:    rgba(0, 245, 255, 0.75);
}

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: var(--bg);
  user-select: none;
}

/* ── Canvas wrap (dot-grid background) ───────────────────────────── */
#wrap {
  position: fixed; inset: 0;
  background-color: var(--bg);
  background-image: radial-gradient(circle, rgba(0,245,255,0.13) 1px, transparent 1px);
  background-size: 40px 40px;
}

#canvas {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%;
  touch-action: none;
}

/* ── Toolbar ──────────────────────────────────────────────────────── */
#toolbar {
  position: fixed;
  left: 14px; top: 50%;
  transform: translateY(-50%);
  z-index: 200;
  display: flex; flex-direction: column; gap: 6px;
  width: 188px;
  background: var(--surface);
  border: 1px solid var(--rim);
  border-radius: 18px;
  padding: 16px 14px;
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  box-shadow:
    0 0 0 1px rgba(0,245,255,0.06),
    0 0 40px rgba(0,245,255,0.08),
    inset 0 1px 0 rgba(255,255,255,0.04);
}

.tb-label {
  font-family: 'Orbitron', sans-serif;
  font-size: 7.5px; letter-spacing: 2.5px;
  color: var(--dim);
  margin-top: 4px;
}

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

/* Color swatches */
.swatch-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  margin: 2px 0;
}

.sw {
  height: 30px;
  border-radius: 7px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s;
  position: relative;
}
.sw:hover  { transform: scale(1.14); }
.sw.active { border-color: #fff; box-shadow: 0 0 0 1px rgba(255,255,255,0.25) !important; }

.sw-cyan   { background: var(--cyan);   box-shadow: 0 0 8px var(--cyan); }
.sw-pink   { background: var(--pink);   box-shadow: 0 0 8px var(--pink); }
.sw-green  { background: var(--green);  box-shadow: 0 0 8px var(--green); }
.sw-purple { background: var(--purple); box-shadow: 0 0 8px var(--purple); }
.sw-orange { background: var(--orange); box-shadow: 0 0 8px var(--orange); }
.sw-yellow { background: var(--yellow); box-shadow: 0 0 8px var(--yellow); }
.sw-red    { background: var(--red);    box-shadow: 0 0 8px var(--red); }
.sw-white  { background: var(--white); }

/* Brush preview + slider (fixed slot so the dot resizing does not reflow the slider) */
.brush-row {
  display: flex; align-items: center; gap: 8px;
  min-width: 0; /* allow row to respect toolbar width */
}
.brush-dot-wrap {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#brush-dot {
  border-radius: 50%;
  transition: width .1s, height .1s, background .1s, box-shadow .1s;
}
/* Wrapper takes flex space; input is width:100% so thumb travels full track (flex on the input breaks WebKit). */
.brush-slider-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
}
#brush-size {
  -webkit-appearance: none; appearance: none;
  display: block;
  width: 100%;
  height: 18px;
  margin: 0;
  padding: 0;
  border-radius: 2px;
  background: transparent;
  outline: none;
  cursor: pointer;
}
#brush-size::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 2px;
  background: rgba(0,245,255,0.2);
}
#brush-size::-moz-range-track {
  height: 3px;
  border-radius: 2px;
  background: rgba(0,245,255,0.2);
}
#brush-size::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px; height: 13px;
  margin-top: -5px;
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 8px var(--cyan);
  cursor: pointer;
}
#brush-size::-moz-range-thumb {
  width: 13px; height: 13px; border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 8px var(--cyan);
  cursor: pointer; border: none;
}

/* Zoom row */
.zoom-row { display: flex; gap: 5px; }
.zoom-btn {
  flex: 1; padding: 6px 4px;
  border-radius: 7px;
  border: 1px solid var(--rim);
  background: transparent;
  color: var(--text);
  font-family: 'Orbitron', sans-serif;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.14s;
  line-height: 1;
}
.zoom-btn:hover {
  background: rgba(0,245,255,0.07);
  box-shadow: 0 0 10px rgba(0,245,255,0.12);
}
#zoom-pct {
  text-align: center;
  font-family: 'Orbitron', sans-serif;
  font-size: 8px; letter-spacing: 1px;
  color: var(--dim);
  margin: 1px 0;
}

/* ── HUD overlays ─────────────────────────────────────────────────── */
#save-flash {
  position: fixed; top: 16px; right: 16px;
  font-family: 'Orbitron', sans-serif;
  font-size: 8px; letter-spacing: 2px;
  color: var(--green);
  text-shadow: 0 0 10px var(--green);
  opacity: 0;
  transition: opacity 0.25s;
  z-index: 300; pointer-events: none;
}
#save-flash.on { opacity: 1; }

#status {
  position: fixed; bottom: 12px; right: 16px;
  font-family: 'Orbitron', sans-serif;
  font-size: 8px; letter-spacing: 1px;
  color: var(--dim);
  display: flex; gap: 18px;
  z-index: 300; pointer-events: none;
}

/* ── Loading screen ───────────────────────────────────────────────── */
#loading {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9000;
  transition: opacity 0.55s;
}
#loading.fade { opacity: 0; pointer-events: none; }

.ld-sub {
  font-family: 'Orbitron', sans-serif;
  font-size: 9px; letter-spacing: 4px;
  color: var(--dim);
}
.ld-bar-wrap {
  width: 220px; height: 2px;
  background: rgba(0,245,255,0.12);
  border-radius: 2px; margin-top: 24px; overflow: hidden;
}
.ld-bar {
  height: 100%; width: 0;
  background: linear-gradient(90deg, transparent, var(--cyan));
  box-shadow: 0 0 8px var(--cyan);
  animation: ldbar 1.2s ease-in-out forwards;
}

@keyframes ldbar {
  0%   { width: 0 }
  70%  { width: 80% }
  100% { width: 100% }
}

@keyframes neonpulse {
  0%,100% { text-shadow: 0 0 20px var(--cyan), 0 0 60px rgba(0,245,255,0.25); }
  50%     { text-shadow: 0 0 35px var(--cyan), 0 0 120px rgba(0,245,255,0.5); }
}

/* Cursor states */
body.mode-pen { cursor: crosshair; }
body.mode-pan { cursor: grab; }
body.panning     { cursor: grabbing; }

/* Tip */
#tip {
  position: fixed; bottom: 12px; left: 50%;
  transform: translateX(-50%);
  font-family: 'Orbitron', sans-serif;
  font-size: 7.5px; letter-spacing: 1.5px;
  color: var(--dim);
  z-index: 200; pointer-events: none;
  white-space: nowrap;
}
