/* The vineyard blocks: their toggle, their legend and their pins. See js/blocks.js
   (the overlay on the relief) and js/ui.js (the toggle and the legend).

   Internal data, so all of it shows only with all names on: the toggle as soon
   as blocks.json has loaded, the legend and the pins while the toggle is on
   (html[data-blocks="on"]). Where each goes:
   - a wide window: the toggle at the left of the names switch, the legend at
     the right under the names, where ui.js takes its room off the model's;
   - an upright phone: the toggle at the right of the tour's button, the legend
     between them and the list of places, which gives up the room it takes;
   - a small phone on its side: the toggle beside the switch, the legend at the
     foot of the column at the left, the list giving up the room again. */

/* ---- The toggle: the tour's pill, at the switch's height ----------------- */

.blocks {
  display: flex;
  padding: 3px;
  border-radius: 999px;
  background: rgb(253 251 246 / 0.86);
  box-shadow: inset 0 0 0 1px var(--ink-45), 0 14px 30px -22px rgb(20 18 14 / 0.4);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  touch-action: none; /* as on the switch: a pinch that lands here must not zoom the page */
  animation: rise var(--dur-4) var(--ease-rise) both;
  /* ui.js and labels.js measure it the moment --index-room moves it (on a phone), so
     it may not ease there: the reduced-motion reset in base.css would give every
     property 0.01ms, and it would be measured where it was, as the tour's button was. */
  transition: none;
}

.blocks[hidden],
:root:not([data-names="all"]) .blocks,
[data-state="error"] .blocks,
[data-mode="edit"] .blocks {
  display: none;
}

.blocks__button {
  position: relative;
  isolation: isolate; /* keeps the pressed fill behind the label and in front of the pill */
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 6px 14px 7px 8px; /* the switch's options' height; the icon centred in the rounded end */
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--ink-70);
  /* Chrome's own sheet gives a button its own type: the names switch's, set in full. */
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
  letter-spacing: 0.01em;
  font-feature-settings: inherit;
  white-space: nowrap;
  cursor: pointer;
  transition: color var(--dur-2) var(--ease-estate), transform var(--dur-2) var(--ease-estate);
  -webkit-tap-highlight-color: transparent;
}

/* On: filled in ink, as the switch's chosen option is. Laid in by opacity and
   scale, so the state is a shape as well as a colour. */
.blocks__button::before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 0;
  border-radius: inherit;
  background: var(--ink);
  opacity: 0;
  transform: scale(0.94, 0.82);
  transition: opacity var(--dur-2) var(--ease-estate), transform var(--dur-3) var(--ease-estate);
}

.blocks__button[aria-pressed="true"] {
  color: var(--bone);
}

.blocks__button[aria-pressed="true"]::before {
  opacity: 1;
  transform: none;
}

.blocks__icon {
  flex: none;
  width: 18px;
  height: 18px;
  margin: -1px 0; /* no taller than the line beside it: the pill keeps the switch's height */
  fill: none;
  stroke: currentColor;
  stroke-width: 1.25;
}

.blocks__button[aria-pressed="true"] .blocks__icon {
  fill: currentColor;
  fill-opacity: 0.4;
}

/* What the toggle shows: said as the index says it of the internal names. */
.blocks__mark {
  color: var(--terracotta-700);
}

.blocks__button[aria-pressed="true"] .blocks__mark {
  color: var(--amber-soft);
}

@media (hover: hover) {
  .blocks__button[aria-pressed="false"]:hover {
    color: var(--ink);
  }
}

.blocks__button:active {
  transform: scale(0.97);
}

.blocks__button:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

@media (pointer: coarse) {
  .blocks__button {
    min-height: 44px;
    padding: 0 16px 0 12px;
  }
}

/* ---- The legend ---------------------------------------------------------- */

.legend {
  position: absolute;
  z-index: 4;
  top: calc(var(--head-room, 120px) + 12px); /* under the names, however tall they are (ui.js) */
  right: calc(var(--edge) + env(safe-area-inset-right, 0px));
  box-sizing: border-box;
  width: max-content;
  max-width: 240px;
  margin: 0;
  padding: 12px 16px 13px 14px;
  border-radius: var(--radius-core-sm);
  background: rgb(253 251 246 / 0.86);
  box-shadow: var(--bezel-ring), 0 14px 30px -22px rgb(20 18 14 / 0.4);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  animation: rise var(--dur-4) var(--ease-rise) both;
  transition: none; /* measured as it is laid out, never mid-way: see .blocks */
}

.legend[hidden],
:root:not([data-blocks="on"]) .legend,
[data-state="error"] .legend,
[data-mode="edit"] .legend {
  display: none;
}

.legend__title {
  margin: 0 0 6px;
  color: var(--ink-60);
  font-size: 10px; /* an h2 by role, an eyebrow by look */
}

.legend__list {
  display: grid;
  gap: 2px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.legend__item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink-70);
  font-size: 12.5px;
  line-height: 1.5;
  white-space: nowrap;
}

/* A block as the map draws it: its colour at the fill's 40%, edged darker. */
.legend__swatch {
  flex: none;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: color-mix(in srgb, var(--swatch, var(--ink-45)) 40%, transparent);
  box-shadow: inset 0 0 0 1.5px color-mix(in srgb, var(--swatch, var(--ink-45)) 62%, #000);
}

/* Not yet planted: hatched, as on the map. */
.legend__item--future .legend__swatch {
  background: repeating-linear-gradient(135deg, var(--ink-60) 0 1.25px, transparent 1.25px 3.5px);
  box-shadow: inset 0 0 0 1.25px var(--ink-60);
}

/* ---- The pins: a block's own colour in a small square ------------------- */

.pin--blocks .pin__dot {
  border-radius: 2px;
  background: var(--pin-swatch, var(--map-ink));
  box-shadow: 0 0 0 1.5px var(--halo), 0 0 0 2.5px rgb(46 32 24 / 0.4), 0 1px 4px 2px rgb(46 32 24 / 0.2);
}

/* Short and many: set plainer and tighter than an area's name. */
.pin--blocks .pin__name {
  padding-left: calc(4px + 0.04em);
  color: var(--map-ink);
  font-size: 13px;
  font-style: normal;
  letter-spacing: 0.04em;
}

/* ---- Narrow windows: a smaller toggle and legend ------------------------ */

@media (max-width: 640px) {
  /* "Internal" under the name, not after it: about 70px narrower, so the toggle
     fits beside the tour's button, or between the wordmark and the switch on
     its side. Still the switch's height. */
  .blocks__button {
    display: grid;
    grid-template-columns: auto auto;
    column-gap: 8px;
    justify-items: start;
    padding-top: 4px;
    padding-bottom: 4px;
    text-align: left;
  }

  .blocks__icon {
    grid-row: 1 / span 2;
  }

  .blocks__label {
    line-height: 1.15;
  }

  .blocks__mark {
    font-size: 9px;
    line-height: 1.3;
  }

  /* The toggle just above says what this is; the heading stays for a screen reader. */
  .legend__title {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

/* ---- An upright phone --------------------------------------------------- */

@media (max-width: 640px) and (min-height: 501px), (max-width: 640px) and (orientation: portrait) {
  /* Beside the tour's button, which keeps the left of that row (controls.css).
     Fixed, so it leaves the names' block, which does not move on a phone. */
  .blocks {
    position: fixed;
    z-index: 4;
    right: calc(var(--edge) + env(safe-area-inset-right, 0px));
    bottom: calc(var(--index-room, var(--foot-row)) + 10px + var(--legend-room, 0px));
  }

  .tour {
    bottom: calc(var(--index-room, var(--foot-row)) + 10px + var(--legend-room, 0px));
  }

  /* Between that row and the list, one line or two. */
  .legend {
    top: auto;
    right: calc(var(--edge) + env(safe-area-inset-right, 0px));
    bottom: calc(var(--index-room, var(--foot-row)) + 10px);
    left: calc(var(--edge) + env(safe-area-inset-left, 0px));
    width: auto;
    max-width: none;
    padding: 9px 14px 10px;
  }

  .legend__list {
    display: flex;
    flex-wrap: wrap;
    gap: 2px 14px;
  }

  /* The list gives up the legend's room, as it does the tour's button's (places.css). */
  :root[data-blocks="on"] body:has(.tour:not([hidden])) .index .index__list {
    max-height: max(calc(2 * var(--index-entry)),
      min(calc(46dvh - 60px - var(--legend-room, 0px)), calc(100dvh - 480px - var(--legend-room, 0px))));
  }
}

/* The narrowest phones: "Blocks" alone, so the toggle still shares the tour's
   row (the list is at its two rows already, so a row of its own would come off
   the model). "Vineyard" stays in the button's name for a screen reader. */
@media (max-width: 359px) {
  .blocks__long {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .blocks__label {
    text-transform: capitalize;
  }
}

/* ---- Any short window on its side: a compact legend --------------------- */

/* Wider than 640px the legend keeps the right-hand spot, but there is little
   height between the names and the view controls: no heading (the toggle says
   it), tighter lines, and the entries in columns of five. */
@media (min-width: 641px) and (max-height: 500px) and (orientation: landscape) {
  .legend {
    max-width: none;
    padding: 8px 14px 9px 12px;
  }

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

  .legend__list {
    grid-template-rows: repeat(5, auto);
    grid-auto-flow: column;
    gap: 0 14px;
  }

  .legend__item {
    font-size: 12px;
    line-height: 1.4;
  }
}

/* ---- A small phone on its side: the foot of the column at the left ------- */

@media (max-width: 640px) and (max-height: 500px) and (orientation: landscape) {
  .legend {
    top: auto;
    right: auto;
    bottom: var(--foot-row);
    left: calc(var(--edge) + env(safe-area-inset-left, 0px));
    max-width: min(240px, 42vw);
    padding: 8px 12px 9px;
  }

  .legend__list {
    display: flex;
    flex-wrap: wrap;
    gap: 0 12px;
  }

  .legend__item {
    font-size: 12px;
    line-height: 1.4;
  }

  :root[data-blocks="on"] .index {
    max-height: calc(100% - var(--column-top) - var(--tour-pill) - 10px - var(--foot-row) - var(--legend-room, 0px));
  }
}

@media (prefers-reduced-transparency: reduce) {
  .blocks,
  .legend {
    background: var(--bone);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
}
