/* Spritesheet → CSS — page shell styles.
   Layout: a header strip, then a two-column grid — the sheet stage on the
   left (zoom controls + Choose file along its bottom edge) and the result
   panel on the right. Below 760 px the panel stacks under the stage and the
   stage is limited to 60vh. */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #d6d9de;
  --text: #1d2026;
  --muted: #5d6470;
  --accent: #2357d8;
  --accent-hover: #1b47b5;
  --accent-text: #ffffff;
  --error: #c62828;
  --error-bg: #fdecea;
  --warning: #8a5a00;
  --warning-bg: #fff4d6;
  --warning-edge: #ffb300;
  --note-bg: #eef2f8;
  --radius: 6px;
  --header-h: 56px;
  --panel-w: 320px;
  --checker: repeating-conic-gradient(#d9dce1 0% 25%, #f7f8fa 0% 50%) 0 0 / 16px 16px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

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

html,
body {
  margin: 0;
  height: 100%;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

[hidden] {
  display: none !important;
}

/* ---------- Header strip ---------- */

.site-header {
  display: flex;
  align-items: baseline;
  gap: 16px;
  flex-wrap: wrap;
  min-height: var(--header-h);
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.site-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
}

.site-tagline {
  margin: 0;
  color: var(--muted);
}

/* ---------- Two-column layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--panel-w);
  /* The row is pinned to the layout's height so an enlarged sheet scrolls
     inside the stage instead of growing the row and pushing the panel away. */
  grid-template-rows: minmax(0, 1fr);
  gap: 16px;
  padding: 16px 20px;
  height: calc(100vh - var(--header-h));
  min-height: 420px;
}

.stage-area {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
  min-height: 0;
}

/* The stage is the drop zone and the scroll container for the canvas. */
.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  /* Reserve the vertical scrollbar's space permanently, so Fit's width
     measurement does not change when a tall sheet makes the stage scroll. */
  scrollbar-gutter: stable;
  background: var(--surface);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
}

.stage[data-state="loaded"] {
  border-style: solid;
}

/* Always-visible scrollbars on the stage. macOS overlay scrollbars stay
   hidden until the user is already scrolling, so a zoomed-in sheet gave no
   sign that the rest of it lies off to the right. Classic scrollbars are the
   affordance; Firefox, which ignores these, keeps its own. */
.stage::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}

.stage::-webkit-scrollbar-track {
  background: var(--bg);
}

.stage::-webkit-scrollbar-thumb {
  background: #b4b9c1;
  border: 3px solid transparent;
  border-radius: 6px;
  background-clip: padding-box;
}

.stage::-webkit-scrollbar-thumb:hover {
  background-color: #8d939c;
}

.stage::-webkit-scrollbar-corner {
  background: transparent;
}

.stage.is-dragover {
  border-color: var(--accent);
  background: #e9effc;
}

.drop-zone {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 200px;
  padding: 24px;
  text-align: center;
}

.drop-zone-text {
  margin: 0;
  font-size: 1.1rem;
  color: var(--muted);
}

.canvas-wrap {
  display: inline-block;
  /* Top-aligned so the inline-block adds no descender gap below the canvas:
     a sheet whose zoomed height exactly fits would otherwise overflow by a
     few px and soak up the wheel as a pointless vertical scroll. */
  vertical-align: top;
  line-height: 0;
}

/* The sheet canvas: drawn with smoothing off at an integer backing scale and
   stretched by CSS to the display zoom with nearest-neighbour rendering. Its
   width/height are set inline by app.js; max-width must not shrink it. */
.sheet-canvas {
  display: block;
  max-width: none;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  cursor: crosshair;
}

/* In-stage status overlay (Reading image… / Detecting…). It covers only the
   stage, never the whole page, and needs no dismissal. */
.stage-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.78);
  color: var(--text);
  cursor: progress;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* A slow detection in progress: busy cursor everywhere on the stage. */
.stage.is-detecting,
.stage.is-detecting * {
  cursor: progress !important;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.stage-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.stage-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 16px;
  flex: 0 0 auto;
}

.zoom-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.toolbar-label {
  color: var(--muted);
  margin-right: 4px;
}

/* ---------- Inline messages ---------- */

.msg {
  margin: 0;
  padding: 8px 10px;
  border-radius: var(--radius);
  font-size: 0.95rem;
}

.msg-error {
  color: var(--error);
  background: var(--error-bg);
}

.msg-warning {
  color: var(--warning);
  background: var(--warning-bg);
  border-left: 4px solid var(--warning-edge);
}

/* A message holding several lines (e.g. two detection warnings). */
.msg-line {
  display: block;
}

.msg-line + .msg-line {
  margin-top: 4px;
}

.msg-note {
  color: var(--text);
  background: var(--note-bg);
}

/* ---------- Buttons ---------- */

.btn {
  font: inherit;
  font-size: 0.95rem;
  padding: 6px 12px;
  border-radius: var(--radius);
  cursor: pointer;
}

.btn:focus-visible,
.css-block code:focus-visible,
input[type="range"]:focus-visible,
.sheet-canvas:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Secondary controls are visually quiet: no fill, thin border. */
.btn-quiet {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-quiet:hover {
  background: #eceef2;
}

.btn-quiet[aria-pressed="true"] {
  background: #dfe5f2;
  border-color: #9fb0d6;
}

/* Copy CSS is the one primary action: the only filled, accent button. */
.btn-primary {
  width: 100%;
  padding: 10px 14px;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-text);
  border: 1px solid var(--accent);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ---------- Result panel ---------- */

.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  overflow: auto;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.panel-placeholder {
  margin: 0;
  color: var(--muted);
}

.result {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.swatch-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.swatch {
  width: 16px;
  height: 16px;
  flex: 0 0 16px;
  border: 1px solid #00000040;
}

.swatch.is-transparent {
  background: repeating-conic-gradient(#c8ccd2 0% 25%, #ffffff 0% 50%) 0 0 / 8px 8px;
}

.bounds {
  margin: 0;
}

/* Shown under the numbers after a click: the way out when a sprite's parts
   are detached. */
.point-hint {
  margin: -6px 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

/* The preview sits on a neutral checkerboard in a box whose content area is
   at least 160 × 160 px (the preview's fit target), with a 16 px margin of
   checkerboard showing all round even at full size. A sprite wider than that
   at 1× scrolls inside the box rather than spilling over the panel. */
.preview-box {
  display: flex;
  align-self: flex-start;
  min-width: 194px;
  min-height: 194px;
  max-width: 100%;
  padding: 16px;
  overflow: auto;
  background: var(--checker);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* Reserves the preview's scaled size in layout (a CSS transform does not),
   so the scaled sprite never overlaps the CSS block beneath. Its width and
   height are set by app.js; margin: auto centres it without clipping when
   it is larger than the box. */
.preview-sizer {
  flex: 0 0 auto;
  margin: auto;
  overflow: hidden;
}

.preview {
  image-rendering: crisp-edges;
  image-rendering: pixelated;
  transform-origin: top left;
}

.css-block {
  margin: 0;
  padding: 10px 12px;
  overflow-x: auto;
  background: #f6f7f9;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
}

.css-block code {
  display: block;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  white-space: pre;
  user-select: text;
}

.copy-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.copy-fallback {
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
}

.tolerance-row {
  display: grid;
  grid-template-columns: auto 1fr 3ch;
  align-items: center;
  gap: 8px;
}

.tolerance-row input[type="range"] {
  width: 100%;
  accent-color: var(--muted);
}

.tolerance-row output {
  text-align: right;
}

.bg-row {
  display: flex;
  gap: 8px;
}

/* ---------- Narrow viewports (< 760 px) ---------- */

@media (max-width: 759px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    height: auto;
    min-height: 0;
    padding: 12px;
  }

  .stage {
    flex: 0 0 auto;
    max-height: 60vh;
  }

  .drop-zone {
    min-height: 40vh;
  }

  .panel {
    overflow: visible;
  }
}

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

/* Pick background while armed: inverted, so the waiting state is obvious. */
#pick-bg.is-armed,
#pick-bg.is-armed:hover {
  background: var(--text);
  color: var(--surface);
  border-color: var(--text);
}

/* While Pick background is armed the canvas shows an eyedropper (hotspot at
   its tip, bottom-left) with 'cell' as the fallback; disarming removes the
   class and the crosshair above applies again. */
.sheet-canvas.is-picking {
  cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath d='M2 22l1.5-4.5L14 7l3 3L6.5 20.5z' fill='%23fff' stroke='%23000' stroke-width='1.5' stroke-linejoin='round'/%3E%3Cpath d='M13 5.5l2.5-2.5a2.1 2.1 0 0 1 3 0l2.5 2.5a2.1 2.1 0 0 1 0 3L18.5 11z' fill='%23000'/%3E%3C/svg%3E") 2 22, cell;
}

.swatch-pin {
  font-size: 0.9em;
  line-height: 1;
}
