#036 style light mobile: native-first Tactile Analog & Pixel-Craft

Pixel-Craft Sprite Bench

Warm parchment and mahogany stepped pixel-bevel UI with a working 12×12 sprite editor and retro palette swatches.

Inspired by Aseprite ↗

Download .html
Fullscreen ↗

WHY THIS AESTHETIC IS UNIQUE

Replaces cold dark developer chrome with cozy, hand-pixelated parchment frames and stepped 2px borders inspired by classic pixel-art creation tools.

BEST FOR

Indie game dev toolsPixel art & chiptune communitiesCreative sandbox web appsRetro workshop portfolios

TAGS

COLOR PALETTE (CLICK TO COPY HEX)

TYPOGRAPHY

Display
Silkscreen
Body
IBM Plex Mono
Mono
IBM Plex Mono
Scale
Crisp Silkscreen pixel headings paired with sturdy monospace tool labels and grid coordinate counters.

Google Fonts Stylesheet ↗

ARCHITECTURE & MOBILE NOTES

LAYOUT

Workbench layout combining a left palette & tool strip, central interactive 12×12 pixel canvas, and right-hand 1x/4x live sprite preview & stamp library.

INTERACTION

Fully interactive pixel painting canvas with clickable retro color swatches, Pencil/Eraser/Horizontal-Mirror tools, preset sprite stamps (Heart, Sword, Invader, Mushroom), and real-time 1x/4x preview.

MOBILE

Reflows the workbench cleanly into a single column at 390px with large touchable palette chips, >=44px tool buttons, and crisp pixel-grid tap painting.

AI AGENT GUARDRAILS

DO

  • Use stepped pixel corners (box-shadow or clip-path) and warm mahogany #3E2F3B borders instead of pure black.
  • Set image-rendering: pixelated on all canvases and previews.
  • Keep the UI tactile and cozy using warm parchment and retro game palette tones.

DON'T

  • Never mix blurry anti-aliased icons or smooth glossy gradients into pixel-craft chrome.
  • Avoid cramped micro-swatches on mobile; keep clickable color chips generous.
STANDALONE SOURCE · demos/pixel-craft-sprite-bench.html (30.8 KB)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SpriteBench v1.3 — Hand-Crafted Pixel Art Studio</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Silkscreen:wght@400;700&family=VT323&display=swap" rel="stylesheet">
<style>
  :root {
    --parchment: #F3E5C8;
    --parchment-hi: #FBF3E0;
    --parchment-lo: #DFC9A1;
    --desk: #C8B08A;
    --mahogany: #655561;
    --mahogany-dark: #3E2F3B;
    --mahogany-deep: #291E27;
    --ink: #2C2029;
    --accent: #C95B4A;
    --accent-hi: #E37966;
    --teal: #3B828E;
    --gold: #D99B26;
    --px: 2px;
  }

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

  html, body {
    margin: 0;
    padding: 0;
    max-width: 100%;
    overflow-x: hidden;
    background-color: var(--desk);
    background-image:
      radial-gradient(var(--parchment-lo) 1px, transparent 1px),
      radial-gradient(#B79E77 1px, transparent 1px);
    background-size: 16px 16px;
    background-position: 0 0, 8px 8px;
    color: var(--ink);
    font-family: 'VT323', ui-monospace, 'Courier New', monospace;
    font-size: 20px;
    line-height: 1.35;
    -webkit-font-smoothing: none;
    image-rendering: pixelated;
  }

  /* Stepped 2px pixel bevel frame */
  .px-frame {
    background: var(--parchment);
    border: 2px solid var(--mahogany-dark);
    box-shadow:
      inset 2px 2px 0 var(--parchment-hi),
      inset -2px -2px 0 var(--parchment-lo),
      0 4px 0 var(--mahogany-dark),
      4px 0 0 rgba(41, 30, 39, 0.18);
    position: relative;
  }

  .px-inset {
    background: var(--parchment-lo);
    border: 2px solid var(--mahogany-dark);
    box-shadow:
      inset 2px 2px 0 rgba(62, 47, 59, 0.28),
      inset -2px -2px 0 var(--parchment-hi);
  }

  .px-titlebar {
    background: var(--mahogany);
    color: var(--parchment-hi);
    padding: 6px 10px;
    border-bottom: 2px solid var(--mahogany-dark);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    font-family: 'Silkscreen', monospace;
    font-size: 12px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }

  .px-dots {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
  }
  .px-dots span {
    width: 8px;
    height: 8px;
    border: 2px solid var(--mahogany-deep);
    background: var(--parchment);
    display: block;
  }
  .px-dots span:first-child { background: var(--accent-hi); }
  .px-dots span:nth-child(2) { background: var(--gold); }

  /* Buttons */
  .px-btn {
    appearance: none;
    font-family: 'Silkscreen', monospace;
    font-size: 11px;
    line-height: 1.2;
    color: var(--ink);
    background: var(--parchment-hi);
    border: 2px solid var(--mahogany-dark);
    box-shadow:
      inset 2px 2px 0 #FFFFFF,
      inset -2px -2px 0 var(--parchment-lo),
      0 2px 0 var(--mahogany-dark);
    padding: 8px 10px;
    min-height: 44px;
    min-width: 44px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    text-transform: uppercase;
    user-select: none;
    transition: transform 0.05s steps(2);
  }
  .px-btn:hover {
    background: #FFF9EC;
  }
  .px-btn:active, .px-btn.is-active {
    transform: translateY(2px);
    box-shadow:
      inset 2px 2px 0 rgba(62, 47, 59, 0.35),
      inset -2px -2px 0 var(--parchment-hi);
    background: var(--mahogany);
    color: var(--parchment-hi);
  }
  .px-btn.is-accent {
    background: var(--accent);
    color: #FFF9EC;
    box-shadow:
      inset 2px 2px 0 var(--accent-hi),
      inset -2px -2px 0 #8E372B,
      0 2px 0 var(--mahogany-dark);
  }
  .px-btn.is-accent:hover { background: var(--accent-hi); }
  .px-btn:focus-visible {
    outline: 3px solid var(--teal);
    outline-offset: 2px;
  }

  /* Top App Bar */
  .app-shell {
    max-width: 1120px;
    margin: 0 auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 14px;
  }

  .topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 12px;
    background: var(--mahogany-dark);
    color: var(--parchment);
    border: 2px solid var(--mahogany-deep);
    box-shadow: 0 4px 0 rgba(41, 30, 39, 0.35);
  }

  .brand {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  .brand-title {
    font-family: 'Silkscreen', monospace;
    font-size: 16px;
    color: var(--parchment-hi);
    margin: 0;
    line-height: 1.1;
  }
  .brand-sub {
    font-size: 17px;
    color: var(--parchment-lo);
    display: block;
  }

  .menubar {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
  }

  .menu-pill {
    font-family: 'Silkscreen', monospace;
    font-size: 10px;
    padding: 6px 8px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    background: var(--mahogany);
    color: var(--parchment-hi);
    border: 2px solid var(--mahogany-deep);
    text-decoration: none;
    cursor: pointer;
  }
  .menu-pill:hover { background: #796674; }

  /* Workbench Grid */
  .workbench {
    display: grid;
    grid-template-columns: 1fr;
    gap: 14px;
    align-items: start;
  }

  @media (min-width: 900px) {
    .workbench {
      grid-template-columns: 230px minmax(0, 1fr) 240px;
    }
  }

  .panel-body {
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .section-label {
    font-family: 'Silkscreen', monospace;
    font-size: 11px;
    color: var(--mahogany-dark);
    text-transform: uppercase;
    margin: 0 0 6px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  /* Palette Grid */
  .palette-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }

  .swatch {
    width: 100%;
    min-height: 44px;
    border: 2px solid var(--mahogany-dark);
    cursor: pointer;
    position: relative;
    padding: 0;
    box-shadow: inset 2px 2px 0 rgba(255,255,255,0.35), inset -2px -2px 0 rgba(0,0,0,0.25);
  }
  .swatch.is-selected {
    outline: 3px solid var(--mahogany-deep);
    outline-offset: 2px;
    transform: scale(1.04);
    z-index: 2;
  }
  .swatch.is-selected::after {
    content: "";
    position: absolute;
    inset: 4px;
    border: 2px solid #FFFFFF;
    box-shadow: 0 0 0 1px #000;
  }

  .active-color-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px;
  }
  .active-chip {
    width: 32px;
    height: 32px;
    border: 2px solid var(--mahogany-dark);
    flex-shrink: 0;
  }
  .active-meta {
    font-size: 18px;
    line-height: 1.1;
  }
  .active-meta strong {
    font-family: 'Silkscreen', monospace;
    font-size: 11px;
    display: block;
  }

  /* Tools & Stamps */
  .tool-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
  }
  @media (min-width: 520px) and (max-width: 899px) {
    .tool-row { grid-template-columns: repeat(3, 1fr); }
  }

  .stamp-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
  }
  @media (min-width: 520px) and (max-width: 899px) {
    .stamp-row { grid-template-columns: repeat(4, 1fr); }
  }

  /* Center Canvas Stage */
  .canvas-stage {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 14px 12px;
  }

  .editor-wrap {
    width: 100%;
    max-width: 360px;
    aspect-ratio: 1 / 1;
    padding: 6px;
    background: var(--mahogany-dark);
    border: 2px solid var(--mahogany-deep);
    box-shadow: inset 2px 2px 0 #7C6A78, 0 4px 0 rgba(41, 30, 39, 0.25);
  }

  #spriteCanvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: crosshair;
    touch-action: none;
    image-rendering: pixelated;
    background: #FFFFFF;
  }

  .status-strip {
    width: 100%;
    max-width: 360px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 6px;
    padding: 6px 10px;
    font-size: 18px;
  }
  .status-strip span {
    white-space: nowrap;
  }

  .canvas-actions {
    width: 100%;
    max-width: 360px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
  }

  /* Right Preview Panel */
  .preview-well {
    display: flex;
    align-items: center;
    justify-content: space-around;
    gap: 12px;
    padding: 14px 10px;
    min-height: 130px;
  }
  .preview-well.bg-parchment { background: var(--parchment-hi); }
  .preview-well.bg-dark { background: #1D2B53; }
  .preview-well.bg-checker {
    background-color: #E5D7BC;
    background-image:
      linear-gradient(45deg, #CFC0A4 25%, transparent 25%),
      linear-gradient(-45deg, #CFC0A4 25%, transparent 25%),
      linear-gradient(45deg, transparent 75%, #CFC0A4 75%),
      linear-gradient(-45deg, transparent 75%, #CFC0A4 75%);
    background-size: 12px 12px;
    background-position: 0 0, 0 6px, 6px -6px, -6px 0;
  }

  .preview-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
  }
  .preview-box canvas {
    image-rendering: pixelated;
    border: 2px solid var(--mahogany-dark);
    background: transparent;
  }
  .preview-tag {
    font-family: 'Silkscreen', monospace;
    font-size: 10px;
    padding: 2px 5px;
    background: var(--mahogany-dark);
    color: var(--parchment-hi);
  }

  .bounce-active {
    animation: spriteBounce 0.6s steps(2) infinite;
  }
  @keyframes spriteBounce {
    0% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
    100% { transform: translateY(0); }
  }

  .bg-switcher {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
  }

  /* Bottom Craft Notes */
  .notes-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 14px;
  }
  @media (min-width: 720px) {
    .notes-grid { grid-template-columns: 1.2fr 1fr; }
  }

  .spec-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    gap: 6px;
  }
  .spec-list li {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    border-bottom: 2px dotted var(--mahogany);
    padding-bottom: 4px;
  }
  .kbd {
    font-family: 'Silkscreen', monospace;
    font-size: 10px;
    background: var(--parchment-hi);
    border: 2px solid var(--mahogany-dark);
    padding: 2px 6px;
    box-shadow: 0 2px 0 var(--mahogany-dark);
    white-space: nowrap;
  }

  .footer-bar {
    text-align: center;
    font-size: 17px;
    color: var(--mahogany-dark);
    padding: 6px 0 16px;
  }

  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0,0,0,0);
    white-space: nowrap;
  }
</style>
</head>
<body>
<div class="app-shell">

  <!-- Top Studio Chrome -->
  <header class="topbar">
    <div class="brand">
      <!-- Hand-plotted 12x12 SVG Sprite Logo -->
      <svg width="40" height="40" viewBox="0 0 12 12" shape-rendering="crispEdges" aria-hidden="true">
        <rect width="12" height="12" fill="#F3E5C8"/>
        <rect x="0" y="0" width="12" height="1" fill="#3E2F3B"/>
        <rect x="0" y="11" width="12" height="1" fill="#3E2F3B"/>
        <rect x="0" y="0" width="1" height="12" fill="#3E2F3B"/>
        <rect x="11" y="0" width="1" height="12" fill="#3E2F3B"/>
        <rect x="3" y="3" width="2" height="2" fill="#C95B4A"/>
        <rect x="7" y="3" width="2" height="2" fill="#C95B4A"/>
        <rect x="2" y="4" width="8" height="3" fill="#C95B4A"/>
        <rect x="3" y="7" width="6" height="1" fill="#C95B4A"/>
        <rect x="4" y="8" width="4" height="1" fill="#C95B4A"/>
        <rect x="5" y="9" width="2" height="1" fill="#C95B4A"/>
        <rect x="3" y="4" width="1" height="1" fill="#FFF1E8"/>
      </svg>
      <div>
        <h1 class="brand-title">SPRITEBENCH v1.3</h1>
        <span class="brand-sub">12&times;12 Pixel-Art Bench &middot; Indexed PICO-16</span>
      </div>
    </div>

    <nav class="menubar" aria-label="Studio actions">
      <button type="button" class="px-btn" id="btnUndo" title="Undo last stroke (Ctrl+Z)">Undo</button>
      <button type="button" class="px-btn" id="btnGrid" aria-pressed="true">Grid: On</button>
      <button type="button" class="px-btn is-accent" id="btnExport">Export PNG</button>
    </nav>
  </header>

  <!-- Main 3-Column Pixel Workbench -->
  <main class="workbench">

    <!-- Left Panel: Palette & Tools -->
    <section class="px-frame" aria-label="Palette and drawing tools">
      <div class="px-titlebar">
        <span>Palette &amp; Tools</span>
        <span class="px-dots" aria-hidden="true"><span></span><span></span><span></span></span>
      </div>
      <div class="panel-body">
        <div>
          <p class="section-label"><span>PICO-16 Swatches</span><span id="colorIdx">#08</span></p>
          <div class="palette-grid" id="paletteGrid" role="radiogroup" aria-label="Color palette"></div>
        </div>

        <div class="px-inset active-color-bar">
          <div class="active-chip" id="activeChip"></div>
          <div class="active-meta">
            <strong id="activeName">Crimson</strong>
            <span id="activeHex">#FF004D</span>
          </div>
        </div>

        <div>
          <p class="section-label"><span>Tools</span><span id="toolReadout">Pencil</span></p>
          <div class="tool-row">
            <button type="button" class="px-btn is-active" data-tool="pencil" aria-pressed="true">Pencil</button>
            <button type="button" class="px-btn" data-tool="eraser" aria-pressed="false">Eraser</button>
            <button type="button" class="px-btn" data-tool="fill" aria-pressed="false">Fill</button>
            <button type="button" class="px-btn" id="btnMirror" aria-pressed="false">Mirror X</button>
          </div>
        </div>
      </div>
    </section>

    <!-- Center Panel: 12x12 Interactive Sprite Canvas -->
    <section class="px-frame" aria-label="Sprite editor canvas">
      <div class="px-titlebar">
        <span>sprite_01.ase — 12&times;12 px</span>
        <span class="px-dots" aria-hidden="true"><span></span><span></span><span></span></span>
      </div>
      <div class="canvas-stage">
        <div class="editor-wrap">
          <canvas id="spriteCanvas" width="288" height="288" aria-label="12 by 12 interactive pixel art grid"></canvas>
        </div>

        <div class="px-inset status-strip" aria-live="polite">
          <span id="coordStatus">POS: 00, 00</span>
          <span id="mirrorStatus">MIRROR: OFF</span>
          <span id="pixelCount">PX: 0</span>
        </div>

        <div style="width:100%;max-width:360px;">
          <p class="section-label"><span>Preset Sprite Stamps</span><span>Click to load</span></p>
          <div class="stamp-row">
            <button type="button" class="px-btn" data-stamp="heart">Heart</button>
            <button type="button" class="px-btn" data-stamp="sword">Sword</button>
            <button type="button" class="px-btn" data-stamp="invader">Invader</button>
            <button type="button" class="px-btn" data-stamp="mushroom">Mushroom</button>
          </div>
        </div>

        <div class="canvas-actions">
          <button type="button" class="px-btn" id="btnFlipH">Flip H</button>
          <button type="button" class="px-btn" id="btnShiftR">Nudge &rarr;</button>
          <button type="button" class="px-btn" id="btnClear">Clear</button>
        </div>
      </div>
    </section>

    <!-- Right Panel: Live 1x & 4x Preview -->
    <section class="px-frame" aria-label="Live sprite preview">
      <div class="px-titlebar">
        <span>Live Preview</span>
        <span class="px-dots" aria-hidden="true"><span></span><span></span><span></span></span>
      </div>
      <div class="panel-body">
        <div class="px-inset preview-well bg-checker" id="previewWell">
          <div class="preview-box">
            <canvas id="prev1x" width="12" height="12" style="width:24px;height:24px;"></canvas>
            <span class="preview-tag">1X (12px)</span>
          </div>
          <div class="preview-box">
            <canvas id="prev4x" width="48" height="48" style="width:96px;height:96px;"></canvas>
            <span class="preview-tag">4X (48px)</span>
          </div>
        </div>

        <div>
          <p class="section-label"><span>Preview Stage</span></p>
          <div class="bg-switcher">
            <button type="button" class="px-btn is-active" data-bg="checker">Check</button>
            <button type="button" class="px-btn" data-bg="parchment">Paper</button>
            <button type="button" class="px-btn" data-bg="dark">Night</button>
          </div>
        </div>

        <button type="button" class="px-btn" id="btnBounce" aria-pressed="false">Idle Bounce: Off</button>

        <div class="px-inset" style="padding:10px;font-size:18px;">
          <strong style="font-family:'Silkscreen',monospace;font-size:10px;display:block;margin-bottom:4px;">INDEXED EXPORT</strong>
          Exports a crisp 192&times;192 nearest-neighbor PNG with alpha transparency preserved.
        </div>
      </div>
    </section>

  </main>

  <!-- Studio Notes & Shortcuts -->
  <section class="notes-grid" aria-label="Studio documentation">
    <article class="px-frame">
      <div class="px-titlebar">
        <span>Craft Notes — Why 12&times;12?</span>
        <span class="px-dots" aria-hidden="true"><span></span><span></span><span></span></span>
      </div>
      <div class="panel-body" style="gap:8px;">
        <p style="margin:0;">
          At <strong>12&times;12 pixels</strong>, every single dot carries silhouette weight. Toggle <strong>Mirror X</strong> to block out symmetrical creatures, emblems, and RPG items in half the strokes, then inspect the <strong>1X</strong> tile to verify readability at native game scale.
        </p>
        <p style="margin:0;color:var(--mahogany-dark);">
          Palette indices follow classic 16-color fantasy console discipline: warm parchment highlights, deep plum outlines, and high-saturation jewel accents.
        </p>
      </div>
    </article>

    <aside class="px-frame">
      <div class="px-titlebar">
        <span>Bench Shortcuts</span>
        <span class="px-dots" aria-hidden="true"><span></span><span></span><span></span></span>
      </div>
      <div class="panel-body">
        <ul class="spec-list">
          <li><span>Pencil / Eraser</span><span class="kbd">P / E</span></li>
          <li><span>Flood Fill Bucket</span><span class="kbd">G</span></li>
          <li><span>Toggle Mirror X</span><span class="kbd">M</span></li>
          <li><span>Undo Stroke</span><span class="kbd">CTRL+Z</span></li>
        </ul>
      </div>
    </aside>
  </section>

  <footer class="footer-bar">
    SpriteBench Pixel Studio &middot; Hand-beveled 2px UI &middot; Zero blur, 100% crisp edges
  </footer>
</div>

<script>
(() => {
  const GRID = 12;
  const CELL = 24; // 12 * 24 = 288px internal resolution
  const PALETTE = [
    { hex: '#1D2B53', name: 'Deep Navy' },
    { hex: '#7E2553', name: 'Plum Ink' },
    { hex: '#008751', name: 'Forest' },
    { hex: '#AB5236', name: 'Rust Clay' },
    { hex: '#5F574F', name: 'Slate Bark' },
    { hex: '#C2C3C7', name: 'Silver' },
    { hex: '#FFF1E8', name: 'Cream' },
    { hex: '#FF004D', name: 'Crimson' },
    { hex: '#FFA300', name: 'Amber' },
    { hex: '#FFEC27', name: 'Sun Gold' },
    { hex: '#00E436', name: 'Lime Pop' },
    { hex: '#29ADFF', name: 'Sky Cyan' },
    { hex: '#83769C', name: 'Lavender' },
    { hex: '#FF77A8', name: 'Blossom' },
    { hex: '#FFCCAA', name: 'Peach' },
    { hex: '#291E27', name: 'Espresso' }
  ];

  // Hand-plotted 12x12 stamps ('.' = transparent, hex index 0-9, a-f)
  const STAMPS = {
    heart: [
      '............',
      '..ff....ff..',
      '.f77f..f77f.',
      'f7677ff7777f',
      'f7677777777f',
      'f7777777771f',
      '.f77777771f.',
      '..f777771f..',
      '...f7771f...',
      '....f71f....',
      '.....ff.....',
      '............'
    ],
    sword: [
      '.........ff.',
      '........f65f',
      '.......f65f.',
      '......f65f..',
      '.....f65f...',
      '..ff.f5f....',
      '...f8f5f....',
      '....f8f.....',
      '...f3f8f....',
      '..f3f..ff...',
      '.f8f........',
      '.ff.........'
    ],
    invader: [
      '............',
      '...a....a...',
      '....a..a....',
      '...aaaaaa...',
      '..aa0aa0aa..',
      '.aaaaaaaaaa.',
      '.a.aaaaaa.a.',
      '.a.a....a.a.',
      '....aa.aa...',
      '............',
      '..9......9..',
      '............'
    ],
    mushroom: [
      '....ffff....',
      '..ff7777ff..',
      '.f76677777f.',
      'f7667777667f',
      'f7777777667f',
      'f7777777777f',
      '.ffffffffff.',
      '...f6666f...',
      '...f6060f...',
      '...f6666f...',
      '....ffff....',
      '............'
    ]
  };

  // State: 12x12 array of hex strings or null (transparent)
  let pixels = Array.from({ length: GRID }, () => Array(GRID).fill(null));
  let history = [];
  let selectedIdx = 7; // Crimson
  let tool = 'pencil';
  let mirrorX = false;
  let showGrid = true;
  let isDrawing = false;

  const canvas = document.getElementById('spriteCanvas');
  const ctx = canvas.getContext('2d');
  const prev1x = document.getElementById('prev1x').getContext('2d');
  const prev4x = document.getElementById('prev4x').getContext('2d');
  const paletteGrid = document.getElementById('paletteGrid');
  const activeChip = document.getElementById('activeChip');
  const activeName = document.getElementById('activeName');
  const activeHex = document.getElementById('activeHex');
  const colorIdx = document.getElementById('colorIdx');
  const coordStatus = document.getElementById('coordStatus');
  const mirrorStatus = document.getElementById('mirrorStatus');
  const pixelCount = document.getElementById('pixelCount');
  const toolReadout = document.getElementById('toolReadout');

  function cloneGrid(g) {
    return g.map(row => row.slice());
  }

  function pushHistory() {
    history.push(cloneGrid(pixels));
    if (history.length > 30) history.shift();
  }

  function buildPalette() {
    paletteGrid.innerHTML = '';
    PALETTE.forEach((c, idx) => {
      const btn = document.createElement('button');
      btn.type = 'button';
      btn.className = 'swatch' + (idx === selectedIdx ? ' is-selected' : '');
      btn.style.backgroundColor = c.hex;
      btn.setAttribute('role', 'radio');
      btn.setAttribute('aria-checked', String(idx === selectedIdx));
      btn.setAttribute('aria-label', `${c.name} ${c.hex}`);
      btn.title = `${c.name} (${c.hex})`;
      btn.addEventListener('click', () => selectColor(idx));
      paletteGrid.appendChild(btn);
    });
    updateActiveSwatch();
  }

  function selectColor(idx) {
    selectedIdx = idx;
    if (tool === 'eraser') setTool('pencil');
    [...paletteGrid.children].forEach((el, i) => {
      el.classList.toggle('is-selected', i === idx);
      el.setAttribute('aria-checked', String(i === idx));
    });
    updateActiveSwatch();
  }

  function updateActiveSwatch() {
    const c = PALETTE[selectedIdx];
    activeChip.style.backgroundColor = c.hex;
    activeName.textContent = c.name;
    activeHex.textContent = c.hex;
    colorIdx.textContent = '#' + String(selectedIdx + 1).padStart(2, '0');
  }

  function setTool(nextTool) {
    tool = nextTool;
    toolReadout.textContent = nextTool.charAt(0).toUpperCase() + nextTool.slice(1);
    document.querySelectorAll('[data-tool]').forEach(btn => {
      const active = btn.dataset.tool === nextTool;
      btn.classList.toggle('is-active', active);
      btn.setAttribute('aria-pressed', String(active));
    });
  }

  function render() {
    // Draw checkerboard background
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (let y = 0; y < GRID; y++) {
      for (let x = 0; x < GRID; x++) {
        ctx.fillStyle = (x + y) % 2 === 0 ? '#F7EEDB' : '#E7D8BA';
        ctx.fillRect(x * CELL, y * CELL, CELL, CELL);
        if (pixels[y][x]) {
          ctx.fillStyle = pixels[y][x];
          ctx.fillRect(x * CELL, y * CELL, CELL, CELL);
        }
      }
    }

    // Optional grid lines
    if (showGrid) {
      ctx.strokeStyle = 'rgba(62, 47, 59, 0.18)';
      ctx.lineWidth = 1;
      for (let i = 1; i < GRID; i++) {
        ctx.beginPath();
        ctx.moveTo(i * CELL + 0.5, 0);
        ctx.lineTo(i * CELL + 0.5, canvas.height);
        ctx.stroke();
        ctx.beginPath();
        ctx.moveTo(0, i * CELL + 0.5);
        ctx.lineTo(canvas.width, i * CELL + 0.5);
        ctx.stroke();
      }
    }

    // Mirror center guide
    if (mirrorX) {
      ctx.strokeStyle = 'rgba(201, 91, 74, 0.85)';
      ctx.lineWidth = 2;
      ctx.setLineDash([4, 4]);
      ctx.beginPath();
      ctx.moveTo(canvas.width / 2, 0);
      ctx.lineTo(canvas.width / 2, canvas.height);
      ctx.stroke();
      ctx.setLineDash([]);
    }

    // Update 1x & 4x previews
    prev1x.clearRect(0, 0, 12, 12);
    prev4x.clearRect(0, 0, 48, 48);
    let filled = 0;
    for (let y = 0; y < GRID; y++) {
      for (let x = 0; x < GRID; x++) {
        const col = pixels[y][x];
        if (col) {
          filled++;
          prev1x.fillStyle = col;
          prev1x.fillRect(x, y, 1, 1);
          prev4x.fillStyle = col;
          prev4x.fillRect(x * 4, y * 4, 4, 4);
        }
      }
    }
    pixelCount.textContent = `PX: ${filled}`;
  }

  function floodFill(startX, startY, replacement) {
    const target = pixels[startY][startX];
    if (target === replacement) return;
    const stack = [[startX, startY]];
    while (stack.length) {
      const [x, y] = stack.pop();
      if (x < 0 || x >= GRID || y < 0 || y >= GRID) continue;
      if (pixels[y][x] !== target) continue;
      pixels[y][x] = replacement;
      stack.push([x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]);
    }
  }

  function applyCell(x, y) {
    if (x < 0 || x >= GRID || y < 0 || y >= GRID) return;
    const color = tool === 'eraser' ? null : PALETTE[selectedIdx].hex;
    if (tool === 'fill') {
      floodFill(x, y, color);
      if (mirrorX) floodFill(GRID - 1 - x, y, color);
    } else {
      pixels[y][x] = color;
      if (mirrorX) pixels[y][GRID - 1 - x] = color;
    }
    render();
  }

  function getCellFromPointer(e) {
    const rect = canvas.getBoundingClientRect();
    const x = Math.floor(((e.clientX - rect.left) / rect.width) * GRID);
    const y = Math.floor(((e.clientY - rect.top) / rect.height) * GRID);
    return {
      x: Math.max(0, Math.min(GRID - 1, x)),
      y: Math.max(0, Math.min(GRID - 1, y))
    };
  }

  canvas.addEventListener('pointerdown', (e) => {
    e.preventDefault();
    canvas.setPointerCapture(e.pointerId);
    pushHistory();
    isDrawing = true;
    const { x, y } = getCellFromPointer(e);
    coordStatus.textContent = `POS: ${String(x).padStart(2,'0')}, ${String(y).padStart(2,'0')}`;
    applyCell(x, y);
  });

  canvas.addEventListener('pointermove', (e) => {
    const { x, y } = getCellFromPointer(e);
    coordStatus.textContent = `POS: ${String(x).padStart(2,'0')}, ${String(y).padStart(2,'0')}`;
    if (isDrawing && tool !== 'fill') {
      applyCell(x, y);
    }
  });

  window.addEventListener('pointerup', () => { isDrawing = false; });

  function loadStamp(name) {
    const rows = STAMPS[name];
    if (!rows) return;
    pushHistory();
    for (let y = 0; y < GRID; y++) {
      for (let x = 0; x < GRID; x++) {
        const ch = rows[y][x];
        if (ch === '.') {
          pixels[y][x] = null;
        } else {
          const idx = parseInt(ch, 16);
          pixels[y][x] = PALETTE[idx] ? PALETTE[idx].hex : '#291E27';
        }
      }
    }
    render();
  }

  // Bind UI controls
  document.querySelectorAll('[data-tool]').forEach(btn => {
    btn.addEventListener('click', () => setTool(btn.dataset.tool));
  });

  document.querySelectorAll('[data-stamp]').forEach(btn => {
    btn.addEventListener('click', () => loadStamp(btn.dataset.stamp));
  });

  const btnMirror = document.getElementById('btnMirror');
  btnMirror.addEventListener('click', () => {
    mirrorX = !mirrorX;
    btnMirror.classList.toggle('is-active', mirrorX);
    btnMirror.setAttribute('aria-pressed', String(mirrorX));
    mirrorStatus.textContent = `MIRROR: ${mirrorX ? 'ON' : 'OFF'}`;
    render();
  });

  const btnGrid = document.getElementById('btnGrid');
  btnGrid.addEventListener('click', () => {
    showGrid = !showGrid;
    btnGrid.textContent = `Grid: ${showGrid ? 'On' : 'Off'}`;
    btnGrid.setAttribute('aria-pressed', String(showGrid));
    render();
  });

  document.getElementById('btnUndo').addEventListener('click', () => {
    if (!history.length) return;
    pixels = history.pop();
    render();
  });

  document.getElementById('btnClear').addEventListener('click', () => {
    pushHistory();
    pixels = Array.from({ length: GRID }, () => Array(GRID).fill(null));
    render();
  });

  document.getElementById('btnFlipH').addEventListener('click', () => {
    pushHistory();
    pixels = pixels.map(row => row.slice().reverse());
    render();
  });

  document.getElementById('btnShiftR').addEventListener('click', () => {
    pushHistory();
    pixels = pixels.map(row => [row[GRID - 1], ...row.slice(0, GRID - 1)]);
    render();
  });

  // Preview backdrop switcher
  const previewWell = document.getElementById('previewWell');
  document.querySelectorAll('[data-bg]').forEach(btn => {
    btn.addEventListener('click', () => {
      document.querySelectorAll('[data-bg]').forEach(b => b.classList.remove('is-active'));
      btn.classList.add('is-active');
      previewWell.className = 'px-inset preview-well bg-' + btn.dataset.bg;
    });
  });

  // Idle bounce toggle
  const btnBounce = document.getElementById('btnBounce');
  const prev4xEl = document.getElementById('prev4x');
  btnBounce.addEventListener('click', () => {
    const on = prev4xEl.classList.toggle('bounce-active');
    btnBounce.textContent = `Idle Bounce: ${on ? 'On' : 'Off'}`;
    btnBounce.classList.toggle('is-active', on);
    btnBounce.setAttribute('aria-pressed', String(on));
  });

  // Export crisp 192x192 PNG
  document.getElementById('btnExport').addEventListener('click', () => {
    const out = document.createElement('canvas');
    out.width = 192;
    out.height = 192;
    const octx = out.getContext('2d');
    const scale = 16;
    for (let y = 0; y < GRID; y++) {
      for (let x = 0; x < GRID; x++) {
        if (pixels[y][x]) {
          octx.fillStyle = pixels[y][x];
          octx.fillRect(x * scale, y * scale, scale, scale);
        }
      }
    }
    const a = document.createElement('a');
    a.download = 'spritebench-12x12.png';
    a.href = out.toDataURL('image/png');
    a.click();
  });

  // Keyboard shortcuts
  window.addEventListener('keydown', (e) => {
    if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'z') {
      e.preventDefault();
      if (history.length) { pixels = history.pop(); render(); }
      return;
    }
    if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
    const k = e.key.toLowerCase();
    if (k === 'p') setTool('pencil');
    if (k === 'e') setTool('eraser');
    if (k === 'g') setTool('fill');
    if (k === 'm') btnMirror.click();
  });

  // Initialize with Heart stamp
  buildPalette();
  loadStamp('heart');
  history = [];
})();
</script>
</body>
</html>