.ka256-pixel-icon {
  --icon-grid-size: 8;
  --icon-cell-size: 2px;
  display: grid;
  grid-template-columns: repeat(var(--icon-grid-size), var(--icon-cell-size));
  grid-template-rows: repeat(var(--icon-grid-size), var(--icon-cell-size));
  width: calc(var(--icon-grid-size) * var(--icon-cell-size));
  height: calc(var(--icon-grid-size) * var(--icon-cell-size));
  flex: none;
  background: transparent;
  image-rendering: pixelated;
}

.ka256-pixel-icon > i {
  display: block;
  width: var(--icon-cell-size);
  height: var(--icon-cell-size);
}

.ka256-pixel-icon > i[data-tone="0"] { background: transparent; }
.ka256-pixel-icon > i[data-tone="1"] { background: var(--line-a); }
.ka256-pixel-icon > i[data-tone="2"] { background: var(--line-b); }
.ka256-pixel-icon > i[data-tone="3"] { background: var(--line-c); }

.canvas-brush-cursor {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2147483647;
  display: none;
  /* Must stay transparent to hit-testing so document.elementFromPoint resolves
     the real element under the pointer, not the follow-cursor itself. */
  pointer-events: none;
}

.canvas-brush-cursor.is-visible {
  display: block;
}

.canvas-brush-footprint {
  --brush-screen-size: 2px;
  display: block;
  width: var(--brush-screen-size);
  height: var(--brush-screen-size);
  image-rendering: pixelated;
}

.canvas-brush-footprint[data-tool="pen-a"] {
  background: var(--line-a);
}

.canvas-brush-footprint[data-tool="pen-b"] {
  background: var(--line-b);
}

.canvas-brush-footprint[data-tool="pen-c"] {
  background: var(--line-c);
}

.canvas-brush-footprint[data-tool="eraser"] {
  background: var(--bg);
}

/* Match the production toolbar to the editor's Toolbar preview. */
.canvas-toolbar {
  padding: 8px;
  border-width: 2px;
  gap: 6px 0;
}

.ht-btn,
.canvas-toolbar-toggle {
  border-width: 2px;
}

.ht-group + .ht-group {
  margin-left: 6px;
  padding-left: 6px;
  border-left-width: 2px;
}

.ht-btn.is-active {
  border-color: var(--line-a);
  outline: 0;
  box-shadow: 0 0 0 2px var(--line-a);
}

.canvas-toolbar-toggle {
  bottom: 18px;
}

.theme-toggle {
  bottom: 48px;
  border-width: 2px;
}

/* Compact Works into a content-sized panel instead of stretching it rightward. */
.works-layout {
  grid-template-columns: minmax(0, 1fr) 318px;
  gap: clamp(24px, 4vw, 48px);
  width: min(100%, 1360px);
  margin: 0 auto;
}

.works-side {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas:
    "title counter"
    "controls controls"
    "thumbs thumbs";
  column-gap: 12px;
  row-gap: 8px;
  width: 318px;
  max-width: 100%;
  justify-self: start;
  padding: 8px;
  border: 2px solid var(--hair);
  background: var(--panel);
}

.works-side .h {
  grid-area: title;
  margin: 0;
}

.works-counter {
  grid-area: counter;
  justify-self: end;
  align-self: center;
  margin: 0;
}

.works-ctrl {
  grid-area: controls;
  display: flex;
  gap: 6px;
}

.works-thumbs {
  grid-area: thumbs;
  display: grid;
  grid-template-columns: repeat(auto-fit, 32px);
  gap: 6px;
  width: 100%;
  max-width: none;
  align-items: start;
}

.step-btn,
.works-thumb {
  box-sizing: border-box;
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
  padding: 0;
  border: 2px solid var(--line-b);
  background: transparent;
}

.step-btn {
  display: grid;
  place-items: center;
}

.step-btn:hover,
.works-thumb:hover {
  border-color: var(--line-a);
}

.works-thumb.is-current {
  border-color: var(--line-a);
  outline: 0;
  box-shadow: 0 0 0 2px var(--line-a);
}

@media (min-width: 761px) {
  .works-layout {
    --works-art-size: min(72dvh, 44vw);
    grid-template-columns: var(--works-art-size) 318px;
    width: fit-content;
    max-width: 100%;
  }

  .works-view,
  #works-image {
    width: var(--works-art-size);
    height: var(--works-art-size);
    max-width: 100%;
    max-height: none;
  }
}

@media (max-width: 760px) {
  .canvas-toolbar-toggle {
    bottom: 14px;
  }

  .theme-toggle {
    bottom: 42px;
  }

  .ht-group + .ht-group {
    margin-left: 6px;
    padding-left: 6px;
  }

  .works-layout {
    grid-template-columns: 1fr;
    gap: 20px;
    width: 100%;
  }

  .works-side {
    width: min(318px, 100%);
    justify-self: center;
  }
}

/* Portrait / phone layout: float the toolbar as a centered bar above the
   bottom controls and let the groups flow together so buttons wrap tidily.
   Kept last so it wins over the width-based control offsets above. */
@media (orientation: portrait) {
  .canvas-toolbar {
    position: fixed;
    left: 50%;
    right: auto;
    bottom: 20px;
    width: max-content;
    max-width: calc(100vw - 152px);
    translate: -50% 0;
    justify-content: center;
    gap: 6px;
  }

  .canvas-toolbar .ht-group {
    display: contents;
  }

  .canvas-toolbar .ht-group + .ht-group {
    margin-left: 0;
    padding-left: 0;
    border-left: 0;
  }

  .canvas-toolbar-toggle {
    position: fixed;
    left: 20px;
    bottom: 20px;
  }

  .theme-toggle {
    right: 20px;
    bottom: 20px;
    width: 32px;
    height: 32px;
  }
}
