/*
 * Palette Extractor — layout that media-tools.css does not already cover:
 * the pickable image stage with its loupe, the swatch grid, the ramp strip,
 * the contrast matrix, and the modal/toast pair that replace native dialogs.
 * Every selector is prefixed `pal-` so nothing leaks into other tools.
 */

.pal-seg-2 { grid-template-columns: repeat(2, 1fr); }
.pal-seg-4 { grid-template-columns: repeat(4, 1fr); }

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

/* No overflow clipping here: the loupe floats above the pointer and would be
   cut off along the top edge of the image. The canvas rounds itself instead. */
.pal-stage {
  position: relative;
  display: grid;
  min-height: 320px;
  place-items: center;
  padding: 14px;
  border-radius: 17px;
  background: #e7e2d9;
}

#pickCanvas {
  display: block;
  max-width: 100%;
  max-height: 62vh;
  border-radius: 9px;
  box-shadow: 0 10px 30px rgba(40, 35, 26, .16);
  cursor: default;
}

/* Touch is only captured while the eyedropper is on. The image is most of a
   phone's screen, and with touch-action: none always set a swipe that started
   on it could not scroll the page to the palette. */
.pal-stage.is-picking #pickCanvas { cursor: crosshair; touch-action: none; }

.pal-loupe {
  position: absolute;
  z-index: 6;
  display: grid;
  gap: 5px;
  justify-items: center;
  padding: 6px;
  border-radius: 13px;
  background: rgba(25, 25, 24, .78);
  box-shadow: 0 12px 30px rgba(0, 0, 0, .28);
  backdrop-filter: blur(6px);
  pointer-events: none;
  transform: translate(-50%, -100%);
}

.pal-loupe.is-below { transform: translate(-50%, 0); }
.pal-loupe.is-side { transform: translate(-50%, -50%); }

.pal-loupe canvas {
  display: block;
  width: 110px;
  height: 110px;
  border: 2px solid rgba(255, 255, 255, .85);
  border-radius: 50%;
  image-rendering: pixelated;
}

.pal-loupe-hex {
  color: #fff;
  font-family: "DM Mono", ui-monospace, monospace;
  font-size: .64rem;
  letter-spacing: .04em;
}

/* ----- shared panel chrome for the stacked sections ----- */

.pal-panel { margin-top: 16px; padding: 22px; }

.pal-panel-head { margin-bottom: 16px; }
.pal-panel-head h2 { margin: 0; font-size: 1.02rem; letter-spacing: -.03em; }
.pal-panel-note { max-width: 640px; margin: 7px 0 0; color: var(--media-muted); font-size: .72rem; line-height: 1.55; }

.pal-card {
  min-width: 0;
  padding: 17px;
  border: 1px solid var(--media-line);
  border-radius: 16px;
  background: rgba(255, 253, 248, .72);
}

.pal-card h3 { margin: 0 0 12px; font-size: .82rem; letter-spacing: -.015em; }

/* ----- derived colors ----- */

.pal-derived {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 9px;
  margin-bottom: 16px;
}

.pal-derived-item {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr);
  gap: 11px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--media-line);
  border-radius: 13px;
  color: inherit;
  background: rgba(255, 253, 248, .8);
  cursor: pointer;
  text-align: left;
}

.pal-derived-item:hover { border-color: var(--media-accent); }

/* The Add button exists for touch screens only (see the hover: none block);
   everywhere else the cell dissolves and the item is the grid cell again. */
.pal-derived-cell { display: contents; }
.pal-derived-add { display: none; }
.pal-derived-swatch { width: 40px; height: 40px; border: 1px solid rgba(25, 25, 24, .12); border-radius: 10px; }
.pal-derived-label { margin: 0; color: #7d7970; font-family: "DM Mono", ui-monospace, monospace; font-size: .57rem; letter-spacing: .1em; text-transform: uppercase; }
.pal-derived-hex { margin: 3px 0 0; font-family: "DM Mono", ui-monospace, monospace; font-size: .74rem; font-weight: 500; }

/* ----- swatch grid ----- */

.pal-swatches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  gap: 11px;
}

.pal-swatch {
  display: grid;
  overflow: hidden;
  border: 1px solid var(--media-line);
  border-radius: 15px;
  background: #fffdf8;
}

.pal-swatch.is-selected { border-color: var(--media-accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--media-accent) 15%, transparent); }
.pal-swatch.is-dragging { opacity: .45; }
.pal-swatch.is-drop-target { border-color: var(--media-accent); border-style: dashed; }

.pal-chip {
  position: relative;
  display: block;
  width: 100%;
  height: 92px;
  padding: 0;
  border: 0;
  border-bottom: 1px solid rgba(25, 25, 24, .08);
  cursor: pointer;
}

.pal-chip-share {
  position: absolute;
  right: 8px;
  bottom: 7px;
  padding: 3px 6px;
  border-radius: 7px;
  background: rgba(255, 255, 255, .82);
  color: #191918;
  font-family: "DM Mono", ui-monospace, monospace;
  font-size: .58rem;
  font-weight: 500;
}

.pal-chip-lockmark {
  position: absolute;
  top: 7px;
  left: 8px;
  padding: 3px 6px;
  border-radius: 7px;
  background: rgba(25, 25, 24, .62);
  color: #fff;
  font-size: .55rem;
  letter-spacing: .05em;
  text-transform: uppercase;
}

.pal-swatch-foot { display: grid; grid-template-columns: 32px minmax(0, 1fr) auto auto; gap: 6px; align-items: center; padding: 8px; }
.pal-swatch-foot input[type="color"] { width: 32px; height: 32px; padding: 2px; border: 1px solid var(--media-line); border-radius: 8px; background: #fffdf8; cursor: pointer; }
.pal-hex { min-height: 32px; padding: 0 7px; font-family: "DM Mono", ui-monospace, monospace; font-size: .68rem; text-transform: uppercase; }
.pal-mini-btn { display: grid; width: 28px; height: 28px; place-items: center; border: 1px solid transparent; border-radius: 8px; color: #858078; background: transparent; cursor: pointer; font-size: .78rem; }
.pal-mini-btn:hover { border-color: var(--media-line); background: #fff; }
.pal-mini-btn[aria-pressed="true"] { border-color: var(--media-accent); color: var(--media-accent-dark); background: var(--media-soft); }
.pal-mini-btn.pal-remove:hover { color: #b73f31; }

.pal-swatch-badges { display: flex; flex-wrap: wrap; gap: 4px; padding: 0 8px 9px; }

.pal-swatch-move { display: none; }

.pal-badge {
  padding: 2px 6px;
  border-radius: 6px;
  background: #eae5db;
  color: #6b6760;
  font-family: "DM Mono", ui-monospace, monospace;
  font-size: .54rem;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.pal-badge.is-pass { background: #dff0e2; color: #256b40; }
.pal-badge.is-large { background: #f7ecd4; color: #8a6412; }
.pal-badge.is-fail { background: #f2e2df; color: #9a4436; }

/* ----- inspector ----- */

.pal-inspect { margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--media-line); }
.pal-inspect-head { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.pal-inspect-chip { width: 58px; height: 58px; flex: 0 0 auto; border: 1px solid rgba(25, 25, 24, .12); border-radius: 14px; }
.pal-inspect-head h3 { margin: 0; font-size: .92rem; letter-spacing: -.02em; }
.pal-inspect-sub { margin: 4px 0 0; color: var(--media-muted); font-size: .69rem; }

.pal-values { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 8px; margin-bottom: 18px; }

.pal-value-row {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--media-line);
  border-radius: 11px;
  color: inherit;
  background: rgba(255, 253, 248, .8);
  cursor: pointer;
  text-align: left;
}

.pal-value-row:hover { border-color: var(--media-accent); }
.pal-value-key { color: #7d7970; font-family: "DM Mono", ui-monospace, monospace; font-size: .55rem; letter-spacing: .1em; text-transform: uppercase; }
.pal-value-text { overflow: hidden; font-family: "DM Mono", ui-monospace, monospace; font-size: .71rem; text-overflow: ellipsis; white-space: nowrap; }

.pal-ramp { display: grid; grid-template-columns: repeat(10, minmax(0, 1fr)); gap: 5px; }

.pal-ramp-step {
  display: grid;
  height: 74px;
  align-content: end;
  justify-items: center;
  padding: 6px 2px;
  border: 1px solid rgba(25, 25, 24, .08);
  border-radius: 9px;
  cursor: pointer;
  font-family: "DM Mono", ui-monospace, monospace;
  font-size: .53rem;
}

.pal-ramp-step.is-base { outline: 2px solid var(--media-accent); outline-offset: 1px; }

/* ----- accessibility ----- */

.pal-a11y-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }

.pal-contrast-list { display: grid; gap: 7px; }

.pal-contrast-row {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--media-line);
  border-radius: 11px;
  background: #fffdf8;
}

.pal-contrast-chip { width: 30px; height: 30px; border: 1px solid rgba(25, 25, 24, .12); border-radius: 8px; }
.pal-contrast-hex { margin: 0; font-family: "DM Mono", ui-monospace, monospace; font-size: .68rem; font-weight: 500; }
.pal-contrast-lines { display: grid; gap: 4px; margin-top: 5px; }
.pal-contrast-line { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; color: #7c7870; font-size: .62rem; }
.pal-contrast-line b { font-family: "DM Mono", ui-monospace, monospace; font-weight: 500; color: #46433d; }

.pal-cvd-list { display: grid; gap: 7px; }

.pal-cvd-row {
  display: grid;
  gap: 7px;
  padding: 9px 10px;
  border: 1px solid var(--media-line);
  border-radius: 11px;
  background: #fffdf8;
  font-size: .66rem;
}

.pal-cvd-pair { display: flex; align-items: center; gap: 6px; }
.pal-cvd-dot { width: 17px; height: 17px; border: 1px solid rgba(25, 25, 24, .12); border-radius: 5px; }
.pal-cvd-arrow { color: #a19c92; }
.pal-cvd-note { margin: 0; color: #7c7870; font-size: .6rem; line-height: 1.45; }
.pal-cvd-ok { margin: 0; padding: 14px 10px; border: 1px dashed var(--media-line); border-radius: 11px; color: #6f7d4b; font-size: .68rem; text-align: center; }

.pal-matrix-card { margin-top: 12px; }
.pal-matrix-scroll { overflow: auto; margin-top: 11px; border: 1px solid var(--media-line); border-radius: 12px; background: #fffdf8; }
.pal-matrix { border-collapse: collapse; font-family: "DM Mono", ui-monospace, monospace; font-size: .58rem; }
.pal-matrix th, .pal-matrix td { padding: 0; border: 1px solid #ece7de; text-align: center; }
.pal-matrix th { width: 40px; height: 34px; }
.pal-matrix-head { display: block; width: 34px; height: 22px; margin: 0 auto; border: 1px solid rgba(25, 25, 24, .12); border-radius: 5px; }
.pal-matrix td { width: 40px; height: 34px; }
.pal-matrix-cell { display: grid; width: 100%; height: 34px; place-items: center; }
.pal-matrix-cell.is-pass { background: #dff0e2; color: #23663c; }
.pal-matrix-cell.is-large { background: #f7ecd4; color: #85600f; }
.pal-matrix-cell.is-fail { background: #f1eee8; color: #a19c92; }
.pal-matrix-cell.is-self { background: #f7f4ed; color: #c5c0b6; }

/* ----- export ----- */

.pal-export-controls { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin-bottom: 13px; align-items: end; }
.pal-export-check { padding-bottom: 11px; }

.pal-code {
  width: 100%;
  padding: 14px;
  border: 1px solid var(--media-line);
  border-radius: 13px;
  outline: 0;
  background: rgba(248, 245, 238, .9);
  color: #38352f;
  font-family: "DM Mono", ui-monospace, monospace;
  font-size: .7rem;
  line-height: 1.65;
  resize: vertical;
}

.pal-export-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.pal-handoff { margin: 11px 0 0; color: #5e6b2c; font-size: .69rem; line-height: 1.5; }
.pal-handoff a { color: var(--media-accent-dark); font-weight: 700; }

/* ----- toast and modal ----- */

.pal-toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  z-index: 99998;
  padding: 11px 16px;
  border-radius: 12px;
  background: rgba(25, 25, 24, .92);
  color: #fffdf8;
  box-shadow: 0 14px 34px rgba(25, 25, 24, .24);
  font-size: .74rem;
  font-weight: 600;
  transform: translateX(-50%);
}

.pal-modal { position: fixed; inset: 0; z-index: 99999; display: grid; place-items: center; padding: 20px; background: rgba(35, 32, 26, .38); backdrop-filter: blur(3px); }

.pal-modal-card {
  width: min(400px, 100%);
  padding: 22px;
  border: 1px solid var(--media-line);
  border-radius: 18px;
  background: #fffdf8;
  box-shadow: 0 26px 70px rgba(38, 32, 24, .26);
}

.pal-modal-card h2 { margin: 0 0 8px; font-size: 1rem; letter-spacing: -.025em; }
.pal-modal-card p { margin: 0 0 18px; color: var(--media-muted); font-size: .78rem; line-height: 1.6; }
.pal-modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

@media (max-width: 840px) {
  .pal-a11y-grid { grid-template-columns: 1fr; }
  .pal-export-controls { grid-template-columns: 1fr; }
  .pal-derived { grid-template-columns: 1fr; }
  .pal-ramp { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}

@media (max-width: 540px) {
  .pal-panel { padding: 15px; }
  .pal-swatches { grid-template-columns: repeat(auto-fill, minmax(146px, 1fr)); }
  .pal-export-actions .btn { flex: 1 1 140px; }
  /* Two swatches abreast on a phone leave the hex field about 48px, which cut
     "#3F2344" to "#3F23"; tighter spacing gives it the room for all seven. */
  .pal-swatch-foot { gap: 4px; padding: 8px 6px; }
  .pal-hex { padding: 0 3px; font-size: .64rem; text-align: center; }
}

/* Narrower still (375px phones, most Androids) even that is too little, so the
   lock and remove buttons drop to a row of their own. */
@media (max-width: 389px) {
  .pal-swatch-foot { grid-template-columns: 32px minmax(0, 1fr); row-gap: 2px; }
  .pal-swatch-foot .pal-remove { justify-self: end; }
}

/* ----- touch screens ----- */

.pal-touch-only { display: none; }

@media (hover: none) {
  .pal-hover-only { display: none; }
  .pal-touch-only { display: inline; }

  /* The eyedropper is a mode on a phone, so the mode has to be visible: the
     button reads as pressed and the image is ringed while a touch will pick. */
  #pickToggle[aria-pressed="true"] { border-color: var(--media-accent); color: var(--media-accent-dark); background: var(--media-soft); }
  .pal-stage.is-picking #pickCanvas {
    outline: 3px solid var(--media-accent);
    outline-offset: 3px;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
  }

  .pal-derived-cell { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px; min-width: 0; }
  .pal-derived-add { display: inline-flex; min-width: 64px; height: auto; border-radius: 13px; }

  /* The move buttons make the selected card taller than its row-mate, which
     would otherwise spread its own rows apart to match. */
  .pal-swatch { align-content: start; }
  .pal-swatch-move { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 0 8px 8px; }
  .pal-move-btn {
    min-height: 40px;
    border: 1px solid var(--media-line);
    border-radius: 10px;
    background: #fff;
    color: var(--media-ink);
    font-size: 1rem;
    font-weight: 600;
  }
  .pal-move-btn:disabled { opacity: .35; }

  /* A tap leaves :hover behind on the last thing touched. */
  .pal-derived-item:hover,
  .pal-value-row:hover { border-color: var(--media-line); }
  .pal-mini-btn:not([aria-pressed="true"]):hover { border-color: transparent; color: #858078; background: transparent; }
}

/* The label wraps its checkbox, so the whole row already toggles it; on a
   touch screen it is made finger-tall. `.field label` makes it a block, so the
   flex centring is restated. */
@media (pointer: coarse) {
  .pal-export-check .inline-check { display: flex; align-items: center; min-height: 44px; }
  .pal-export-check .inline-check input { width: 20px; height: 20px; margin: 0; }
}
