/**
 * ColorImagePicker styles.
 * Self-contained under .cip-container. Supports multiple persistent markers:
 * the active marker is a large loupe with a hex label that follows under it;
 * the rest are small color dots.
 */

.cip-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  border-radius: 10px;
  background:
    repeating-conic-gradient(#e9e9eb 0% 25%, #f3f3f5 0% 50%) 0 0 / 16px 16px;
  min-height: 360px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.06);
}

.cip-canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  cursor: crosshair;
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 4px 16px rgba(0, 0, 0, 0.06);
  touch-action: none;
}

/* ----- Marker (shared) ----- */

.cip-marker {
  --cip-loupe-size: 72px;
  --cip-color: #000000;
  /* Native <button> reset. !important defeats theme-global button styles
     (e.g. `.tool button { background: <accent> }`) without depending on
     load order or specificity. */
  appearance: none !important;
  -webkit-appearance: none !important;
  border: 0 !important;
  padding: 0 !important;
  margin: 0 !important;
  min-width: 0 !important;
  min-height: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
  font: inherit;
  color: inherit;
  line-height: normal;
  position: absolute;
  left: 0;
  top: 0;
  /* JS sets transform: translate(calc(Xpx - 50%), calc(Ypx - 50%)) -- compositor-friendly */
  transform: translate(-50%, -50%);
  display: grid;
  place-items: center;
  /* inactive markers carry a 44px touch target around the small dot */
  width: 44px;
  height: 44px;
  cursor: grab;
  touch-action: none;
  z-index: 2;
  opacity: 1;
  transition: opacity 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

/* Hide markers until an image is loaded -- avoids a flash at the top-left corner. */
.cip-container:not(.cip-has-image) .cip-marker {
  opacity: 0;
  pointer-events: none;
}

.cip-marker.is-active {
  width: var(--cip-loupe-size);
  height: var(--cip-loupe-size);
  z-index: 3;
}

.cip-marker:active {
  cursor: grabbing;
}

/* Promote to its own layer only while actually dragging. */
.cip-container.cip-dragging .cip-marker {
  will-change: transform;
}

/* While dragging, hide the cursor so the loupe + exact pixel are never obscured. */
.cip-container.cip-dragging,
.cip-container.cip-dragging * {
  cursor: none !important;
}

.cip-marker:focus-visible {
  outline: none;
}

.cip-marker:focus-visible .cip-marker-body {
  box-shadow:
    inset 0 0 0 1px rgba(0, 0, 0, 0.22),
    inset 0 0 0 5px #ffffff,
    0 0 0 3px var(--accent, #4f46e5),
    0 6px 18px rgba(0, 0, 0, 0.28);
}

/* ----- Marker body (the visible circle) ----- */

.cip-marker-body {
  position: relative;
  display: grid;
  place-items: center;
  border-radius: 50%;
  overflow: hidden;
  background: var(--cip-color);
  /* dot state */
  width: 22px;
  height: 22px;
  box-shadow:
    inset 0 0 0 1px rgba(0, 0, 0, 0.22),
    inset 0 0 0 3px #ffffff,
    0 2px 6px rgba(0, 0, 0, 0.28);
  transition:
    width 0.2s ease-out,
    height 0.2s ease-out,
    box-shadow 0.2s ease-out;
}

.cip-marker.is-active .cip-marker-body {
  width: var(--cip-loupe-size);
  height: var(--cip-loupe-size);
  box-shadow:
    inset 0 0 0 1px rgba(0, 0, 0, 0.22),
    inset 0 0 0 6px rgba(0, 0, 0, 0.06),
    inset 0 0 0 5px #ffffff,
    0 6px 18px rgba(0, 0, 0, 0.28),
    0 2px 6px rgba(0, 0, 0, 0.16);
  /* springy grow when becoming active */
  transition:
    width 0.26s cubic-bezier(0.34, 1.56, 0.64, 1),
    height 0.26s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.2s ease-out;
}

/* Hover is an enhancement only -- never required, and off on touch. */
@media (hover: hover) and (pointer: fine) {
  .cip-marker:not(.is-active):hover .cip-marker-body {
    width: 26px;
    height: 26px;
  }
}

/* ----- Pixel-zoom (active only) ----- */

.cip-marker-zoom {
  position: absolute;
  border-radius: 50%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease-out;
}

.cip-marker.is-active .cip-marker-zoom {
  opacity: 1;
}

/* Center indicator is drawn onto the zoom canvas; element kept for fallbacks. */
.cip-marker-crosshair {
  display: none;
}

/* ----- Hex readout that follows under the active loupe ----- */

.cip-readout {
  position: absolute;
  left: 0;
  top: 0;
  /* JS sets transform to follow the loupe; only opacity animates. */
  background: rgba(20, 20, 22, 0.82);
  color: #ffffff;
  padding: 6px 14px 6px 6px;
  border-radius: 999px;
  font-family: ui-monospace, 'SFMono-Regular', 'Menlo', 'Monaco', 'Consolas', monospace;
  font-size: 13px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  display: flex;
  align-items: center;
  gap: 9px;
  white-space: nowrap;
  backdrop-filter: blur(10px) saturate(140%);
  -webkit-backdrop-filter: blur(10px) saturate(140%);
  z-index: 4;
  pointer-events: none;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.15s ease-out;
}

.cip-container.cip-has-image .cip-readout.is-visible {
  opacity: 1;
  visibility: visible;
}

.cip-readout-swatch {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #000000;
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.4),
    0 0 0 1px rgba(0, 0, 0, 0.2);
}

/* ----- Mobile sizing ----- */

@media (max-width: 600px) {
  .cip-container { min-height: 300px; }
  .cip-marker { --cip-loupe-size: 84px; }
  .cip-readout { font-size: 12px; }
}

/* ----- Reduced motion ----- */

@media (prefers-reduced-motion: reduce) {
  .cip-marker,
  .cip-marker-body,
  .cip-marker.is-active .cip-marker-body,
  .cip-marker-zoom,
  .cip-readout,
  .cip-mode-group button,
  .cip-done-btn {
    transition: none;
  }
}

/* ----- Optional mode-toggle row consumers can drop above the picker ----- */

.cip-mode-bar {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  z-index: 5;
  pointer-events: none;
}

.cip-mode-bar > * { pointer-events: auto; }

.cip-mode-group {
  display: inline-flex;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 999px;
  padding: 4px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.cip-mode-group button {
  border: 0;
  background: transparent;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  color: rgba(20, 20, 22, 0.6);
  transition: background 0.15s ease, color 0.15s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.cip-mode-group button:focus-visible,
.cip-done-btn:focus-visible {
  outline: 2px solid var(--accent, #4f46e5);
  outline-offset: 2px;
}

/* small color dot inside the toggle button that mirrors the marker color */
.cip-mode-group button .cip-mode-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.18);
  background: currentColor;
}

.cip-mode-group button.active {
  background: var(--accent, #4f46e5);
  color: #ffffff;
}

@media (hover: hover) and (pointer: fine) {
  .cip-mode-group button:hover:not(.active) {
    background: rgba(0, 0, 0, 0.05);
    color: rgba(20, 20, 22, 0.85);
  }
  .cip-done-btn:hover { background: #ffffff; }
}

.cip-done-btn {
  border: 0;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  color: rgba(20, 20, 22, 0.85);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: background 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Bigger tap targets on touch devices (44px) without bulking up the desktop UI. */
@media (pointer: coarse) {
  .cip-mode-group button,
  .cip-done-btn {
    min-height: 44px;
  }
}

/* ----- Shared image-picker modal (Upload / URL / Unsplash) ----- */

.unsplash-display {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  overflow: auto;
  overscroll-behavior: contain;
  width: 100%;
  max-height: 234px;
  gap: 0.4em;
}

.unsplash-display .unsplash-photo {
  max-width: 200px;
  height: 100px;
  overflow: hidden;
  position: relative;
}

.unsplash-display .unsplash-photo img {
  object-fit: cover;
  object-position: center center;
  cursor: pointer;
  height: 100%;
  width: 100%;
}

.unsplash-pick-color button.active {
  border: 3px solid var(--accent) !important;
}

.unsplash-photo .photo-credit {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  font-size: 0.55em;
  padding: 0.3em 0.4em;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.6));
  color: #fff;
  opacity: 0;
  transition: opacity 0.15s ease;
  pointer-events: none;
}

@media (hover: hover) and (pointer: fine) {
  .unsplash-photo:hover .photo-credit {
    opacity: 1;
  }
}

.unsplash-photo .photo-credit a {
  color: #fff;
  text-decoration: underline;
  pointer-events: auto;
}
