/* ── LKPixel editor chrome ─────────────────────────────────────────
   Styles the canvas-based pixel-art editor whose DOM contract is produced
   by public/tools/design/js/pixel.js (see task-2-report.md "DOM contract").
   Depends on css/shared.css being loaded first for design tokens
   (--bg, --surface, --border, --text, --muted, --accent, --radius*, --shadow*).

   Layout contract for the page author (task 3):

     <div id="lkpixel-root">
       <div class="lkpixel-toolbar"> ...[data-tool] / [data-action] buttons... </div>
       <div class="lkpixel-stage">
         <div id="lkpixel-canvas-wrap"><canvas id="lkpixel-canvas"></canvas></div>
       </div>
       <div class="lkpixel-side">
         <div class="lkpixel-panel"> ...#lkpixel-color, #lkpixel-swatches, #lkpixel-recent, [data-harmony]... </div>
         <div class="lkpixel-panel"> ...#lkpixel-grid-size, #lkpixel-grid-custom, #lkpixel-grid-toggle... </div>
         <div class="lkpixel-panel"> ...#lkpixel-shape-fill, #lkpixel-symmetry-x/-y, #lkpixel-scale... </div>
       </div>
     </div>

   `.lkpixel-panel` also works as a `<details>` (collapsible drawer) — both
   forms are styled, matching the `lkdraw-panel` convention in design.css.
   ------------------------------------------------------------------ */

:root {
  --lkpixel-toolbar-bg:     #1c1f2e;
  --lkpixel-toolbar-fg:     #c7cbe3;
  --lkpixel-toolbar-fg-hi:  #ffffff;
  --lkpixel-toolbar-border: #2c2f45;
  --lkpixel-toolbar-hover:  rgba(255,255,255,.09);
  --lkpixel-desk-bg:        var(--surface2);
  --lkpixel-check-a:        #eef0fb;
  --lkpixel-check-b:        #dfe2f5;
}

/* ── App shell ─────────────────────────────────────────────────── */
#lkpixel-root {
  display: grid;
  grid-template-columns: 116px minmax(0, 1fr) 300px;
  grid-template-areas: "toolbar stage side";
  height: clamp(540px, 78vh, 820px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  font-family: var(--font-body, 'Outfit', system-ui, sans-serif);
  color: var(--text);
}

/* ── Left toolbar: [data-tool] + [data-action] buttons ────────────── */
.lkpixel-toolbar {
  grid-area: toolbar;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  padding: 12px 10px;
  background: linear-gradient(180deg, #232739 0%, var(--lkpixel-toolbar-bg) 46%);
  color: var(--lkpixel-toolbar-fg);
  border-right: 1px solid var(--lkpixel-toolbar-border);
  box-shadow: inset -1px 0 0 rgba(0,0,0,.25), 2px 0 8px rgba(15,15,46,.10);
  overflow-y: auto;
  overflow-x: hidden;
}
.lkpixel-toolbar::-webkit-scrollbar { width: 6px; }
.lkpixel-toolbar::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); border-radius: 999px; }

.lkpixel-toolbar-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 100%;
}
.lkpixel-toolbar-group + .lkpixel-toolbar-group {
  margin-top: 6px;
  padding-top: 10px;
  border-top: 1px solid var(--lkpixel-toolbar-border);
}

/* Two-per-row grouped layout (Tools / Edit / View / Export) with a small
   section label — keeps the rail compact, matching the other design tools. */
.lkpixel-toolbar-group.lkpixel-toolbar-group--grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  justify-items: center;
}
.lkpixel-toolbar-label {
  grid-column: 1 / -1;
  width: 100%;
  font-size: .66rem;
  font-weight: 800;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: #ffffff;
  opacity: 1;
  text-align: left;
  padding: 0 2px;
  margin-bottom: -1px;
}
/* Short text labels (PNG/CPY export codes) — tidy small caps, not chunky text */
.lkpixel-glyph { font-size: .58rem; font-weight: 800; letter-spacing: .03em; }

.lkpixel-toolbar [data-tool],
.lkpixel-toolbar [data-action] {
  width: 46px;
  height: 46px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: 11px;
  background: transparent;
  color: inherit;
  font-family: inherit;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s, box-shadow .15s, transform .1s;
}
.lkpixel-toolbar [data-tool]:active,
.lkpixel-toolbar [data-action]:active { transform: translateY(1px); }
.lkpixel-toolbar [data-tool] svg,
.lkpixel-toolbar [data-action] svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
/* Signature colored pencil (matches the sudoku/crossword brand pencil) — opt out of the monochrome line-icon rule */
.lkpixel-toolbar [data-tool="pencil"] svg.lkdraw-pencil-icon { width: 25px; height: 25px; fill: initial; stroke: none; overflow: visible; }

/* Brush-size presets (injected by pixel.js) — squares of increasing size */
.lkpixel-brush-group { gap: 5px; }
.lkpixel-brush-group [data-brush] {
  width: 46px; height: 34px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid transparent; border-radius: 9px;
  background: transparent; cursor: pointer; padding: 0;
  transition: background .15s, border-color .15s, box-shadow .15s, transform .1s;
}
.lkpixel-brush-group [data-brush]:hover { background: var(--lkpixel-toolbar-hover); }
.lkpixel-brush-group [data-brush]:active { transform: translateY(1px); }
.lkpixel-brush-dot { background: var(--lkpixel-toolbar-fg); border-radius: 2px; display: block; }
.lkpixel-brush-group [data-brush].is-active { background: var(--accent); border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79, 70, 229, .28); }
.lkpixel-brush-group [data-brush].is-active .lkpixel-brush-dot { background: #fff; }

.lkpixel-toolbar [data-tool]:hover,
.lkpixel-toolbar [data-action]:hover:not(:disabled) {
  background: var(--lkpixel-toolbar-hover);
  color: var(--lkpixel-toolbar-fg-hi);
}
.lkpixel-toolbar [data-tool]:focus-visible,
.lkpixel-toolbar [data-action]:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--lkpixel-toolbar-bg), 0 0 0 4px var(--accent-light, var(--accent));
}

/* Active tool state */
.lkpixel-toolbar [data-tool].is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .28);
}

/* Disabled action state (undo/redo when history is empty) */
.lkpixel-toolbar [data-action]:disabled {
  opacity: .32;
  cursor: not-allowed;
  pointer-events: none;
}

/* ── Center: canvas stage ──────────────────────────────────────── */
.lkpixel-stage {
  grid-area: stage;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  padding: 24px;
  background: var(--lkpixel-desk-bg);
  overflow: auto;
  position: relative;
}

#lkpixel-canvas-wrap {
  position: relative;
  display: flex;
  max-width: 100%;
  max-height: 100%;
  border-radius: 4px;
  box-shadow: var(--shadow-lg), 0 0 0 1px rgba(15, 15, 46, .06);
  /* Checkerboard so transparent cells read clearly against the desk bg. */
  background-image:
    linear-gradient(45deg, var(--lkpixel-check-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--lkpixel-check-a) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--lkpixel-check-a) 75%),
    linear-gradient(-45deg, transparent 75%, var(--lkpixel-check-a) 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
  background-color: var(--lkpixel-check-b);
}

/* CRITICAL: crisp pixels + capped display size so a large grid/zoom never
   blows out the layout — the canvas's intrinsic resolution grows with zoom,
   but its CSS display size stays bounded here. */
#lkpixel-canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  max-width: 100%;
  max-height: 70vh;
  width: auto;
  height: auto;
}

/* ── Right column: color / grid / options panels ──────────────────── */
.lkpixel-side {
  grid-area: side;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
}

.lkpixel-panel { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.lkpixel-panel:last-child { border-bottom: none; }

.lkpixel-panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 12px;
  list-style: none;
  cursor: default;
}
.lkpixel-panel-title::-webkit-details-marker { display: none; }

details.lkpixel-panel { padding: 0; }
details.lkpixel-panel > .lkpixel-panel-title {
  padding: 14px 16px;
  margin-bottom: 0;
  cursor: pointer;
}
details.lkpixel-panel[open] > .lkpixel-panel-title { border-bottom: 1px solid var(--border); }
details.lkpixel-panel > .lkpixel-panel-title::after {
  content: '\25BE';
  font-size: .65rem;
  opacity: .6;
  transition: transform .15s;
}
details.lkpixel-panel:not([open]) > .lkpixel-panel-title::after { transform: rotate(-90deg); }
details.lkpixel-panel > *:not(.lkpixel-panel-title) { padding: 14px 16px; }

/* Generic form controls anywhere in the side column */
.lkpixel-side label {
  display: block;
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 4px;
}
.lkpixel-side input[type="number"],
.lkpixel-side input[type="text"],
.lkpixel-side select {
  width: 100%;
  font-family: inherit;
  font-size: .85rem;
  padding: 6px 8px;
  border: 1px solid var(--border-control, #7a7fb5);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}
.lkpixel-side input[type="color"] {
  width: 44px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border-control, #7a7fb5);
  border-radius: 6px;
  background: none;
  cursor: pointer;
}
.lkpixel-side input:focus-visible,
.lkpixel-side select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .15);
}
.lkpixel-side input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; cursor: pointer; }

.lkpixel-field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.lkpixel-field:last-child { margin-bottom: 0; }
.lkpixel-field-row { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.lkpixel-field-row label { margin-bottom: 0; flex-shrink: 0; }
.lkpixel-field-row.lkpixel-toggle-row { justify-content: space-between; }

/* Two-up grid so short, related controls sit side by side (e.g. the View
   toggles), matching the 2-column inspector on the other design tools. */
.lkpixel-field-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; margin-bottom: 10px; }
.lkpixel-field-pair:last-child { margin-bottom: 0; }
.lkpixel-field-pair .lkpixel-field,
.lkpixel-field-pair .lkpixel-field-row { margin-bottom: 0; }

/* File-picker button (Import Image) — full panel width like the selects above. */
label[for="lkpixel-import-input"] { display: block; width: 100%; text-align: center; box-sizing: border-box; }

/* Current-color swatch + hex input row */
.lkpixel-color-row { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.lkpixel-color-row label { margin-bottom: 0; }

/* Hidden import file input — visually hidden, not display:none, so it
   stays reachable via the button that opens it. */
#lkpixel-import-input {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Mobile-only Export shortcut in the side panel — the toolbar's export buttons
   sit at the far end of the horizontal mobile toolbar and are easy to miss. */
.lkpixel-export-mobile { display: none; }
.lkpixel-export-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.lkpixel-export-grid .btn { width: 100%; text-align: center; }
@media (max-width: 820px) { .lkpixel-export-mobile { display: block; } }

/* Color harmony buttons */
.lkpixel-harmony-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 10px; }
[data-harmony] {
  font-family: inherit;
  font-size: .72rem;
  font-weight: 600;
  padding: 12px 4px;
  min-height: 44px;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
[data-harmony]:hover { border-color: var(--accent); color: var(--accent); }
[data-harmony].is-active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Swatch grids — .palette-sw is the class LKPalette.renderSwatches() assigns */
#lkpixel-swatches,
#lkpixel-recent,
#lkpixel-default-swatches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(24px, 1fr));
  gap: 6px;
  margin-top: 6px;
}
#lkpixel-swatches .palette-sw,
#lkpixel-recent .palette-sw,
#lkpixel-default-swatches .palette-sw,
#lkpixel-swatches button,
#lkpixel-recent button,
#lkpixel-default-swatches button {
  width: 100%;
  aspect-ratio: 1 / 1;
  height: auto;
  padding: 0;
  border: 1px solid rgba(15, 15, 46, .14);
  border-radius: 6px;
  cursor: pointer;
  transition: transform .12s, box-shadow .12s;
}
#lkpixel-swatches .palette-sw:hover,
#lkpixel-recent .palette-sw:hover,
#lkpixel-default-swatches .palette-sw:hover,
#lkpixel-swatches button:hover,
#lkpixel-recent button:hover,
#lkpixel-default-swatches button:hover {
  transform: scale(1.1);
  box-shadow: 0 2px 6px rgba(15, 15, 46, .22);
}

/* Zoom readout */
#lkpixel-zoom-level {
  font-family: var(--mono);
  font-size: .72rem;
  font-weight: 600;
  opacity: .75;
  white-space: nowrap;
}

/* Grid-size row: select + custom number side by side */
.lkpixel-grid-row { display: flex; gap: 8px; margin-bottom: 10px; }
.lkpixel-grid-row select { flex: 1 1 auto; }
.lkpixel-grid-row input[type="number"] { flex: 0 0 76px; }

/* ── Responsive: side panels + toolbar collapse/stack below ~820px ── */
@media (max-width: 820px) {
  #lkpixel-root {
    grid-template-columns: 1fr;
    grid-template-areas: "toolbar" "stage" "side";
    height: auto;
    min-height: 0;
  }
  .lkpixel-toolbar {
    flex-direction: row;
    align-items: center;
    overflow-x: auto;
    overflow-y: visible;
    min-width: 0;      /* let the grid item shrink so overflow-x scrolls internally instead of widening the page */
    max-width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--lkpixel-toolbar-border);
    padding: 8px;
  }
  .lkpixel-toolbar-group { flex-direction: row; width: auto; }
  .lkpixel-toolbar-group.lkpixel-toolbar-group--grid { display: flex; }
  .lkpixel-toolbar-label { display: none; }
  .lkpixel-toolbar-group + .lkpixel-toolbar-group {
    margin-top: 0;
    margin-left: 6px;
    padding-top: 0;
    padding-left: 10px;
    border-top: none;
    border-left: 1px solid var(--lkpixel-toolbar-border);
  }
  .lkpixel-stage { min-height: 360px; padding: 16px; }
  #lkpixel-canvas { max-height: 55vh; }
  .lkpixel-side {
    border-left: none;
    border-top: 1px solid var(--border);
    max-height: none;      /* expand fully and scroll with the page instead of a nested 50vh scroll box */
    overflow-y: visible;
  }
}

@media (max-width: 480px) {
  .lkpixel-toolbar [data-tool],
  .lkpixel-toolbar [data-action] { width: 44px; height: 44px; }
  .lkpixel-stage { min-height: 300px; }
  #lkpixel-canvas { max-height: 42vh; }
  #lkpixel-root { border-radius: var(--radius); }
}

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

/* ── FAQ accordion — matches image/pdf/generators suites ──────── */
.faq details { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 10px; overflow: hidden; }
.faq summary { cursor: pointer; padding: 16px 18px; font-weight: 700; font-size: .98rem; list-style: none; }
.faq summary::-webkit-details-marker { display: none; }
.faq summary::after { content: '+'; float: right; color: var(--accent); font-weight: 700; }
.faq details[open] summary::after { content: '\2013'; }
.faq .faq-body { padding: 0 18px 16px; font-size: .92rem; color: var(--muted); line-height: 1.7; }
.faq .faq-body a { color: var(--accent); }
.faq summary:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ── Numbered how-to steps ────────────────────────────────────── */
.steps { display: grid; gap: 14px; margin-top: 18px; }
.step { display: flex; gap: 12px; align-items: flex-start; }
.step-num { flex: 0 0 auto; width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: #fff; font-weight: 800; font-size: .85rem; display: flex; align-items: center; justify-content: center; }
.step p { margin: 0; color: var(--muted); }
.step strong { color: var(--text); }

/* ── Trust row ────────────────────────────────────────────────── */
.trust-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px 22px; max-width: 760px; margin: 28px auto 0; padding: 0 20px; }
.trust-item { display: inline-flex; align-items: center; gap: 7px; font-size: .88rem; font-weight: 600; color: var(--muted); }
.trust-item svg { width: 16px; height: 16px; stroke: var(--accent); fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: 0 0 auto; }
