/* ==========================================================================
   Appearance picker
   --------------------------------------------------------------------------
   A palette button in the topbar whose menu appears on hover, holding two
   rows: five accent swatches, and five background choices (four patterns plus
   "none"). Shared by the home page and the device test tool. The markup is
   built by js/palette.js.

   Opening on hover is the intended behaviour, but hover alone would leave the
   control unusable by keyboard and unreachable on touch — so the same open
   state is produced by :focus-within and by an .is-open class the script
   toggles on click. All three share one rule; none of them is special.
   ========================================================================== */

.palette {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.palette__toggle {
  display: grid;
  place-items: center;
}

/* The menu. Kept in the DOM and hidden with opacity + visibility rather than
   display:none, so it can transition and so its buttons stay focusable in
   order once open.

   The radius is a card radius, not the 999px pill it used to be: a pill reads
   correctly around one row of round swatches and badly around two rows, where
   the curve cuts into the corner buttons. */
.palette__menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  visibility: hidden;
  transform: translateY(-4px);
  transition: opacity .15s ease, transform .15s ease, visibility .15s;
}

/* One row per setting. The two rows hold the same number of controls at the
   same size, so they align without either row needing a width of its own. */
.palette__row {
  display: flex;
  gap: 7px;
}

/* A hairline between the rows, drawn on the second one so no extra element is
   needed for it. */
.palette__row + .palette__row {
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

.palette:hover .palette__menu,
.palette:focus-within .palette__menu,
.palette.is-open .palette__menu {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

/* A bridge across the gap between button and menu. Without it the menu closes
   as the pointer crosses the 8px gap, which makes hover-to-open feel broken. */
.palette::after {
  content: "";
  position: absolute;
  top: 100%;
  right: 0;
  width: 100%;
  height: 10px;
}

.palette__swatch {
  width: 24px;
  height: 24px;
  padding: 0;
  cursor: pointer;
  background: var(--swatch);
  border: 2px solid transparent;
  border-radius: 8px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .12);
  transition: transform .14s ease, border-color .14s ease;
}

.palette__swatch:hover {
  transform: scale(1.12);
}

/* The current colour. An outline ring rather than a tick, because a tick in a
   readable colour on five different backgrounds is a contrast problem. */
.palette__swatch[aria-checked="true"] {
  border-color: var(--ink);
}

.palette__swatch:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* ==========================================================================
   Background pattern tiles
   --------------------------------------------------------------------------
   Each button previews its own pattern. The previews are drawn here rather
   than reusing the recipes from ocean-patterns.css, because those are tuned
   to sit on --paper at the edge of visibility — reproduced at 24px on
   --surface they would be five identical blank squares.

   So these are the same motifs redrawn for legibility at button size: tighter
   spacing, and --ink at an alpha that reads on --surface in both themes. A
   preview's job is to say which pattern this is, not to be a colour-accurate
   sample of it.
   ========================================================================== */

.palette__pattern {
  width: 24px;
  height: 24px;
  padding: 0;
  cursor: pointer;
  background-color: var(--paper);
  border: 2px solid transparent;
  border-radius: 8px;
  box-shadow: inset 0 0 0 1px var(--line);
  transition: transform .14s ease, border-color .14s ease;
}

.palette__pattern:hover {
  transform: scale(1.12);
}

/* Matches the swatches above: a ring, not a tick. */
.palette__pattern[aria-checked="true"] {
  border-color: var(--ink);
}

.palette__pattern:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* --- The five previews ---------------------------------------------------- */

/* None: a single stroke across an empty tile, the usual "off" sign. Drawn at
   exactly the tile's diagonal so it meets both corners. */
.palette__pattern[data-pattern="none"] {
  background-image: linear-gradient(to top right,
    transparent calc(50% - .7px),
    color-mix(in srgb, var(--ink) 40%, transparent) calc(50% - .7px) calc(50% + .7px),
    transparent calc(50% + .7px));
}

.palette__pattern[data-pattern="diagonal"] {
  background-image: repeating-linear-gradient(58deg,
    color-mix(in srgb, var(--ink) 38%, transparent) 0 1px,
    transparent 1px 5px);
}

.palette__pattern[data-pattern="crosshatch"] {
  background-image:
    repeating-linear-gradient(45deg,
      color-mix(in srgb, var(--ink) 34%, transparent) 0 1px,
      transparent 1px 6px),
    repeating-linear-gradient(-45deg,
      color-mix(in srgb, var(--ink) 34%, transparent) 0 1px,
      transparent 1px 6px);
}

/* Swell: the same arc construction as the real pattern, scaled to the tile —
   only the outer rim of each ellipse is painted. */
.palette__pattern[data-pattern="swell"] {
  background-image:
    radial-gradient(ellipse 5px 2.5px at 50% 100%,
      transparent calc(100% - 1px),
      color-mix(in srgb, var(--ink) 38%, transparent) calc(100% - 1px) 100%,
      transparent 100%);
  background-size: 10px 6px;
}

.palette__pattern[data-pattern="bubbles"] {
  background-image:
    radial-gradient(circle at 4px 5px,   color-mix(in srgb, var(--ink) 40%, transparent) 0 1.5px, transparent 2px),
    radial-gradient(circle at 13px 11px, color-mix(in srgb, var(--ink) 40%, transparent) 0 1.1px, transparent 1.6px),
    radial-gradient(circle at 7px 16px,  color-mix(in srgb, var(--ink) 40%, transparent) 0 1.3px, transparent 1.8px),
    radial-gradient(circle at 16px 4px,  color-mix(in srgb, var(--ink) 40%, transparent) 0 0.9px, transparent 1.4px);
}

@media (prefers-reduced-motion: reduce) {
  .palette__menu,
  .palette__swatch,
  .palette__pattern {
    transition: none;
  }
  .palette__swatch:hover,
  .palette__pattern:hover {
    transform: none;
  }
}
