/* Pixelator — app layer on top of Keystone. Only what Keystone doesn't cover. */

/* Panels below are laid out with `display: grid/flex`, which outranks the
   user-agent rule for [hidden]. This has to win over those declarations. */
[hidden] { display: none !important; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* --------------------------------------------------------------- app shell */

/* The preview must never scroll out of reach while a control is being dragged.
   Rather than rely on sticky positioning, the page is a fixed-viewport shell:
   the document itself never scrolls, and only the control rail does. That makes
   it structurally impossible for the image to leave the screen. */
/* Kept in sync with the real header height by main.js, so the sticky preview
   on narrow screens parks exactly beneath it rather than at a guessed offset. */
:root { --app-header-h: 64px; }

html, body { height: 100%; }

body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
  overflow: hidden;
}

.app-header {
  background: var(--ks-surface-default);
  border-bottom: var(--ks-border-width) solid var(--ks-border-default);
}

.app-header-inner {
  max-width: 1600px;
  margin: 0 auto;
  padding: var(--ks-space-3) var(--ks-space-6);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--ks-space-4);
}

.app-brand { display: flex; align-items: center; gap: var(--ks-space-3); }

/* A literal pixel grid as the logo — cheaper and more on-theme than an asset. */
.app-mark {
  width: 28px; height: 28px;
  border-radius: var(--ks-radius-sm);
  box-shadow: var(--ks-edge-soft);
  background:
    conic-gradient(from 90deg, var(--ks-brand-primary) 25%, transparent 0 50%,
                   var(--ks-brand-primary) 0 75%, transparent 0)
    0 0 / 14px 14px,
    var(--ks-brand-accent);
}

.app-title { margin: 0; font-size: var(--ks-text-base); line-height: var(--ks-leading-tight); }
.app-subtitle { margin: 0; font-size: var(--ks-text-xs); color: var(--ks-text-muted); }

.app-main {
  max-width: 1600px;
  width: 100%;
  margin: 0 auto;
  padding: var(--ks-space-4) var(--ks-space-6);
  display: grid;
  grid-template-columns: minmax(0, 1fr) 316px;
  gap: var(--ks-space-5);
  /* Without this, grid children refuse to shrink and the rail pushes the
     page taller than the viewport — the whole bug, in one line. */
  min-height: 0;
}

.stage {
  display: flex;
  flex-direction: column;
  gap: var(--ks-space-3);
  min-height: 0;
  min-width: 0;
}

/* Only this column scrolls, and only when the controls genuinely overflow. */
.controls {
  display: flex;
  flex-direction: column;
  gap: var(--ks-space-3);
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: var(--ks-space-1);
}

/* ------------------------------------------------------------------ dropzone */

.dropzone {
  flex: 1;
  min-height: 0;
  align-content: center;
  border: var(--ks-border-width) dashed var(--ks-border-emphasis);
  border-radius: var(--ks-radius-card);
  background: var(--ks-surface-subtle);
  cursor: pointer;
  transition: border-color var(--ks-duration-fast), background var(--ks-duration-fast);
}

.dropzone:hover, .dropzone:focus-visible { border-color: var(--ks-border-brand); }

.dropzone.is-over {
  border-color: var(--ks-border-brand);
  background: var(--ks-brand-primary-subtle);
}

.dropzone.is-over .ks-empty-icon { color: var(--ks-brand-primary); }
.dropzone-note { margin-top: var(--ks-space-3); max-width: 44ch; }

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

.workspace {
  display: flex;
  flex-direction: column;
  gap: var(--ks-space-3);
  min-height: 0;
  flex: 1;
}

.stage-bar { display: flex; align-items: center; gap: var(--ks-space-2); }
.stage-bar-spacer { flex: 1; }

/* The result is the deliverable, so it gets the whole stage. The source is a
   reframing affordance and lives as an inset over it. */
.stage-frame {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: var(--ks-space-5);
  border-radius: var(--ks-radius-card);
  background: var(--ks-surface-subtle);
  box-shadow: var(--ks-edge-soft);
}

/* Sized in JS to an integer multiple of the pixel grid — see drawResult(). A
   fractional multiple would render pixel rows of uneven width, which is the
   one defect that reads instantly as "not real pixel art". */
.canvas-out {
  display: block;
  image-rendering: pixelated;
  border-radius: var(--ks-radius-sm);
  box-shadow: var(--ks-edge), var(--ks-shadow-lg);
}

.src-inset {
  position: absolute;
  left: var(--ks-space-4);
  bottom: var(--ks-space-4);
  width: clamp(92px, 13%, 140px);
  border-radius: var(--ks-radius-md);
  overflow: hidden;
  box-shadow: var(--ks-edge), var(--ks-shadow-lg);
  background: var(--ks-surface-default);
  opacity: .9;
  transition: opacity var(--ks-duration-fast);
}

.src-inset:hover, .src-inset:focus-within { opacity: 1; }

.canvas-src {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  cursor: grab;
  touch-action: none;
}

.canvas-src.is-dragging { cursor: grabbing; }
.canvas-src.is-locked { cursor: default; }

.src-inset-hint {
  position: absolute;
  inset-inline: 0;
  bottom: 0;
  padding: 2px var(--ks-space-2);
  text-align: center;
  font-size: 10px;
  background: var(--ks-surface-overlay);
  opacity: 0;
  transition: opacity var(--ks-duration-fast);
}

.src-inset:hover .src-inset-hint { opacity: 1; }

.zoom-pod {
  position: absolute;
  right: var(--ks-space-4);
  bottom: var(--ks-space-4);
  display: flex;
  align-items: center;
  gap: var(--ks-space-2);
  width: 216px;
  padding: var(--ks-space-1) var(--ks-space-2) var(--ks-space-1) var(--ks-space-3);
  border-radius: var(--ks-radius-full);
  background: var(--ks-surface-overlay);
  box-shadow: var(--ks-edge), var(--ks-shadow-md);
  color: var(--ks-text-muted);
}

.zoom-pod .range { flex: 1; }

/* A render slow enough to notice dims the result rather than covering it, so
   the image you are comparing against never disappears. */
.stage-frame[aria-busy="true"] .canvas-out {
  opacity: .72;
  transition: opacity var(--ks-duration-fast);
}

/* ---------------------------------------------------------------- stage foot */

.stage-foot {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: end;
  gap: var(--ks-space-5);
}

.eyebrow {
  display: flex;
  align-items: center;
  gap: var(--ks-space-1);
  margin-bottom: var(--ks-space-2);
  font-size: var(--ks-text-xs);
  font-weight: var(--ks-weight-semibold);
  letter-spacing: var(--ks-tracking-widest);
  text-transform: uppercase;
  color: var(--ks-text-subtle);
}

/* One continuous ribbon rather than loose tiles — it reads as a palette
   instead of a row of chips. Sorted by luminance in drawPalette(). */
.palette-swatches {
  display: flex;
  height: 30px;
  border-radius: var(--ks-radius-md);
  overflow: hidden;
  box-shadow: var(--ks-edge), var(--ks-shadow-sm);
}

.palette-swatches span {
  flex: 1 1 0;
  min-width: 6px;
  transition: flex-grow var(--ks-duration-fast) var(--ks-ease-default);
}

.palette-swatches span:hover { flex-grow: 2.4; }

.meta-block .ks-cluster { gap: var(--ks-space-2); }

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

.ks-card-header strong {
  font-size: var(--ks-text-xs);
  font-weight: var(--ks-weight-semibold);
  letter-spacing: var(--ks-tracking-widest);
  text-transform: uppercase;
  color: var(--ks-text-muted);
}

.ks-card-header .ks-i, .ks-accordion-trigger .ks-i { color: var(--ks-text-subtle); }

.ks-accordion-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--ks-space-2);
}

.ks-accordion-trigger strong {
  font-size: var(--ks-text-xs);
  font-weight: var(--ks-weight-semibold);
  letter-spacing: var(--ks-tracking-widest);
  text-transform: uppercase;
  color: var(--ks-text-muted);
}

.ks-accordion-trigger .ks-accordion-caret { margin-inline-start: auto; }

/* Keystone's .ks-field carries its own margin-bottom, which double-spaces
   every row inside a .ks-stack that already has a gap. */
.ks-card-body .ks-field, .ks-accordion-content-inner .ks-field { margin-bottom: 0; }
.ks-card-body.ks-stack, .ks-accordion-content-inner.ks-stack { gap: var(--ks-space-2); }

/* A single dense row — label, control, value — instead of a stacked label
   above a full-width slider. Halves the rail height, which is what lets the
   whole control set fit on screen at once. */
.ks-field.row {
  display: grid;
  grid-template-columns: 82px minmax(0, 1fr) 46px;
  align-items: center;
  gap: var(--ks-space-3);
  min-height: 32px;
}

.ks-field.row > .ks-label {
  margin: 0;
  font-size: var(--ks-text-sm);
  font-weight: var(--ks-weight-medium);
  letter-spacing: var(--ks-tracking-normal);
  color: var(--ks-text-default);
}

.ks-field.row--wide > .ks-select { grid-column: 2 / -1; }
.ks-field.row > .ks-select { height: var(--ks-control-height-sm); }

.ks-field.is-disabled { opacity: .45; }

/* Tabular figures and a fixed width, so the number does not change size as it
   changes value and jitter the row. */
.val {
  font-family: var(--ks-font-mono);
  font-size: var(--ks-text-xs);
  font-variant-numeric: tabular-nums;
  font-weight: var(--ks-weight-medium);
  color: var(--ks-text-default);
  text-align: right;
}

.zoom-pod .val { min-width: 5ch; }

/* ------------------------------------------------------------------- presets */

.ks-segmented > label > span { pointer-events: none; }

/* Keystone styles .is-active for button-based segmented controls but has no
   rule for the radio form, which is the one with usable keyboard semantics. */
.ks-segmented > label:has(input:checked) {
  background: var(--ks-surface-default);
  color: var(--ks-text-default);
  box-shadow: var(--ks-edge), var(--ks-shadow-xs);
}

#presets[data-dirty="true"] > label:has(input:checked) > span {
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}

/* ------------------------------------------------------------------- ranges */

/* Keystone ships no range component, so this is a local primitive built from
   its tokens rather than a competing set of colours. */
.range {
  width: 100%;
  appearance: none;
  background: transparent;
  cursor: pointer;
}

.range::-webkit-slider-runnable-track {
  height: 5px;
  border-radius: var(--ks-radius-full);
  background: var(--ks-surface-emphasis);
  box-shadow: var(--ks-edge-soft);
}

.range::-moz-range-track {
  height: 5px;
  border-radius: var(--ks-radius-full);
  background: var(--ks-surface-emphasis);
}

.range::-webkit-slider-thumb {
  appearance: none;
  width: 15px; height: 15px;
  margin-top: -5px;
  border: none;
  border-radius: var(--ks-radius-full);
  background: var(--ks-brand-primary);
  box-shadow: var(--ks-shadow-sm);
}

.range::-moz-range-thumb {
  width: 15px; height: 15px;
  border: none;
  border-radius: var(--ks-radius-full);
  background: var(--ks-brand-primary);
  box-shadow: var(--ks-shadow-sm);
}

.range:focus-visible { outline: none; }
.range:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px var(--ks-border-focus); }
.range:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 3px var(--ks-border-focus); }

/* --------------------------------------------------------------- responsive */

/* Below this width the two columns stack, and a fixed-viewport shell stops
   working: the stage and eleven controls cannot share one short screen, so the
   frame gets squeezed to nothing. The page scrolls normally instead, and the
   preview is pinned with sticky positioning — same guarantee that it stays
   visible while you adjust, reached a different way. */
@media (max-width: 1080px) {
  html, body { height: auto; }
  body { display: block; overflow: visible; }

  .app-header { position: sticky; top: 0; z-index: 10; }

  /* A sticky element can only travel within its own containing block, and
     .stage ends where .controls begins — so sticking the frame inside it would
     release the preview the moment you scrolled into the sliders. Collapsing
     the wrappers makes .app-main the containing block, which spans both. */
  .app-main { display: block; min-height: 0; }
  .stage, .workspace { display: contents; }

  .controls { min-height: 0; overflow: visible; max-height: none; }

  .stage-frame {
    position: sticky;
    top: var(--app-header-h);
    z-index: 5;
    flex: none;
    height: 46dvh;
    padding: var(--ks-space-3);
    /* display:contents drops the parent's gap, so spacing becomes explicit. */
    margin-block: var(--ks-space-3);
  }

  .stage-bar, .stage-foot { margin-block: var(--ks-space-3); }
  .dropzone { min-height: 50dvh; }
  .src-inset { width: 88px; }
}

@media (max-width: 680px) {
  .app-header-inner, .app-main { padding-inline: var(--ks-space-3); }
  .app-subtitle { display: none; }
  .stage-frame { height: 40dvh; }
  .stage-foot { grid-template-columns: minmax(0, 1fr); gap: var(--ks-space-3); }
  .stage-bar { flex-wrap: wrap; }
  .zoom-pod { width: 148px; }
  .ks-field.row { grid-template-columns: 76px minmax(0, 1fr) 44px; min-height: 40px; }
  /* Bigger targets for touch. */
  .range::-webkit-slider-thumb { width: 20px; height: 20px; margin-top: -8px; }
  .range::-moz-range-thumb { width: 20px; height: 20px; }
}
