/* Form controls. No outlines: a control is a fill (see --fill), and only focus draws a violet ring. */
@layer components {
  .form { display: grid; gap: var(--space-5); max-width: var(--form-width); }
  .form__actions { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; }
  /* With a way out, it sits at the start and the action at the end. */
  .form__actions:has(> .button--quiet) { justify-content: space-between; }

  .field { display: grid; gap: var(--space-2); }
  .field__label { font-size: var(--text-sm); font-weight: var(--weight-bold); }
  .field__hint { font-size: var(--text-sm); }

  .input, .select {
    width: 100%;
    min-height: var(--control-height);
    padding: 0 var(--space-4);
    border: 0;
    border-radius: var(--radius-md);
    background: var(--fill);
    color: var(--color-ink);
    font: var(--weight-bold) var(--text-md) / var(--leading-snug) var(--font-sans);

    &::placeholder { color: var(--color-ink); font-weight: var(--weight-medium); opacity: 1; }
    &:focus { outline: 2px solid var(--color-focus); outline-offset: 0; }
    &:disabled { opacity: 0.5; }
  }

  textarea.input { min-height: 7rem; padding: var(--space-3) var(--space-4); resize: vertical; }

  .select {
    appearance: none;
    padding-right: var(--space-10);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' fill='none'%3E%3Cpath d='m1 1.5 5 5 5-5' stroke='%230a2a1b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--space-4) center;
    cursor: pointer;
  }

  .select--small { min-height: var(--control-height-sm); font-size: var(--text-sm); }
  .select--small { width: auto; padding-left: var(--space-3); padding-right: var(--space-8); background-position: right var(--space-3) center; }

  .input--code {
    font: var(--weight-bold) var(--text-xl) var(--font-mono);
    letter-spacing: 0.3em;
    text-align: center;
    text-transform: uppercase;
  }

  /* The open list of a dropdown: a white squircle that floats under its trigger, with options as soft fills. */
  .select::picker(select), .picker__list {
    min-width: anchor-size(width);
    max-height: min(22rem, 70dvh);
    margin: var(--space-2) 0;
    padding: var(--space-2);
    border: 0;
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-ink);
    box-shadow: var(--shadow-float);
    overflow-y: auto;
  }

  .select option, .picker__option {
    display: flex;
    gap: var(--space-3);
    align-items: center;
    min-height: var(--control-height-sm);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-sm);
    font-weight: var(--weight-bold);
    cursor: pointer;

    &:hover, &:focus-visible, &:has(:focus-visible) { background: var(--color-ink-8); outline: 0; }
    &[hidden] { display: none; }
  }

  .select option:checked, .picker__option--single:has(:checked) { background: var(--color-mint); }

  /* Chromium renders native selects with our list; other browsers keep their own. */
  @supports (appearance: base-select) {
    select.select, .select::picker(select) { appearance: base-select; }
    select.select { display: flex; align-items: center; }
    .select::picker-icon { display: none; }
    .select option::checkmark { display: none; }
  }

  /* Browsers draw their own coloured clear button in search fields; text is ink only. */
  .input[type=search]::-webkit-search-cancel-button { display: none; }

  /* A file picker as a big squircle: click to choose, or drop a file anywhere on it. */
  .dropzone {
    position: relative;
    display: grid;
    place-content: center;
    gap: var(--space-1);
    min-height: 14rem;
    padding: var(--space-6);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    text-align: center;
    cursor: pointer;
    transition: background var(--duration-fast) var(--ease);

    &:hover { background: var(--color-surface-hover); }
    &:has(:focus-visible) { outline: 2px solid var(--color-focus); }
    input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
  }

  .dropzone__title { font-size: var(--text-xl); letter-spacing: var(--tracking-title); }

  /* A sample of the file in one tinted block, so its rows don't read as fields to type in. */
  .preview {
    display: grid;
    gap: var(--space-2);
    padding: var(--space-4);
    border-radius: var(--radius-md);
    background: var(--fill);
  }

  .preview__row { display: flex; gap: var(--space-3); align-items: baseline; min-width: 0; }
  @media (max-width: 767px) { .preview__row { flex-direction: column; gap: 0; } }
  .preview__more { font-size: var(--text-sm); font-weight: var(--weight-bold); }
  .preview__name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .preview__detail { overflow: hidden; font-size: var(--text-sm); text-overflow: ellipsis; white-space: nowrap; }

  /* A search with its filter beside it, on one line at every width. */
  .search-bar { display: grid; grid-template-columns: minmax(0, 1fr) minmax(8rem, 14rem); gap: var(--space-2); }
  @media (max-width: 767px) { .search-bar { grid-template-columns: 1fr 1fr; } }
  /* A search with its actions beside it, such as + Walk-in and a filter button, each at its own width. */
  .search-bar--action { grid-template-columns: minmax(0, 1fr); grid-auto-columns: auto; grid-auto-flow: column; }

  /* Multi-select and search-select: a .select-looking button that opens a popover of real radios or checkboxes. */
  .picker { min-width: 0; }
  .picker__trigger { overflow: hidden; text-align: left; text-overflow: ellipsis; white-space: nowrap; }

  .picker__list {
    position: absolute;
    position-area: block-end span-inline-end;
    position-try-fallbacks: flip-block;
    inset: auto;

    &:popover-open { display: grid; gap: var(--space-1); align-content: start; }
  }

  .picker__search {
    position: sticky;
    top: 0;
    z-index: 1;
    margin-bottom: var(--space-1);
    box-shadow: 0 0 0 var(--space-2) var(--color-surface);

    &::-webkit-search-cancel-button { display: none; }
  }
  .picker__empty { padding: var(--space-2) var(--space-3); }

  /* On phones the list rises as a sheet, like the panel. */
  @media (max-width: 767px) {
    .picker__list {
      position: fixed;
      position-area: none;
      inset: auto 0 0 0;
      width: 100%;
      max-width: none;
      max-height: 70dvh;
      margin: 0;
      padding: var(--space-4);
      border-radius: var(--radius-xl) var(--radius-xl) 0 0;

      &::backdrop { background: var(--color-scrim); }
    }

    .picker__option { min-height: var(--control-height); }

    /* Already inside a sheet, it opens from its field as on desktop, rather than a sheet over the sheet: upward, where a
       bottom sheet leaves the room, unless that doesn't fit. */
    .panel .picker__list {
      position: absolute;
      position-area: block-start span-inline-end;
      inset: auto;
      width: anchor-size(width);
      max-height: min(22rem, 45dvh);
      margin: var(--space-2) 0;
      padding: var(--space-2);
      border-radius: var(--radius-md);

      &::backdrop { background: none; }
    }
  }

  /* One choice from two or three, as a track: the chosen option rises as a white squircle. Real radios inside labels. */
  .segmented {
    display: grid;
    grid-auto-columns: 1fr;
    grid-auto-flow: column;
    gap: var(--space-1);
    padding: var(--space-1);
    border-radius: var(--radius-md);
    background: var(--color-ink-8);
  }

  .segmented__option {
    display: flex;
    gap: var(--space-2);
    align-items: center;
    justify-content: center;
    min-height: calc(var(--control-height) - 2 * var(--space-1));
    padding-inline: var(--space-4);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background var(--duration-fast) var(--ease);

    input { position: absolute; opacity: 0; pointer-events: none; }
    &:hover { background: var(--color-ink-4); }
    &:has(:checked) { background: var(--color-surface); font-weight: var(--weight-bold); box-shadow: var(--shadow-raised); }
    &:has(:focus-visible) { outline: 2px solid var(--color-focus); }
  }

  /* An address: venue and street across, then city, region, postal code and country in pairs. */
  .address {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-4) var(--space-3);

    > .field:nth-child(-n+2) { grid-column: 1 / -1; }
  }

  .input--color { width: 5rem; padding: var(--space-1); cursor: pointer; }

  .checkbox {
    appearance: none;
    flex: none;
    width: 1.375rem;
    height: 1.375rem;
    margin: 0;
    border-radius: var(--radius-xs);
    background: var(--fill);
    cursor: pointer;

    &:checked {
      background: var(--color-button) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='10' fill='none'%3E%3Cpath d='m1.5 5 3 3 6-6.5' stroke='white' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center no-repeat;
    }
  }
}

/* Settings and the page editor: a form beside previews of the page it styles, with an unsaved-changes bar. */
@layer components {
  /* An image as one row: thumbnail, what it's for, then Upload or Replace and Remove. */
  .image-field { display: flex; gap: var(--space-3); align-items: center; }
  .image-field__thumb { flex: none; width: 3.5rem; height: 3.5rem; border-radius: var(--radius-sm); object-fit: cover; }
  .image-field__thumb--wide { width: 5.5rem; object-fit: contain; background: var(--fill); }
  .image-field__thumb--empty { background: var(--fill); }
  .image-field__text { display: grid; flex: 1; min-width: 0; }
  .image-field__actions { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; justify-content: end; }
  /* Too narrow for one row, the buttons go under the text. */
  .image-field { flex-wrap: wrap; }
  .image-field__text { flex: 1 1 10rem; }

  /* Named colour chips, each a real radio, then Custom. */
  .swatches { display: flex; flex-wrap: wrap; gap: var(--space-3) var(--space-2); }
  .swatch-option {
    display: grid; gap: var(--space-1); justify-items: center; font-size: var(--text-sm); cursor: pointer;

    input[type=radio] { position: absolute; opacity: 0; pointer-events: none; }
  }
  .swatch-option__chip {
    display: grid; place-items: center; width: 2.5rem; height: 2.5rem; border-radius: var(--radius-sm);
    box-shadow: inset 0 0 0 1.5px var(--color-ink-12); font-size: var(--text-xl); font-weight: var(--weight-medium);
  }
  .swatch-option:has(:checked) { font-weight: var(--weight-bold); }
  .swatch-option:has(:checked) .swatch-option__chip { box-shadow: inset 0 0 0 2px var(--color-button), inset 0 0 0 4px var(--color-surface); }
  .swatch-option:has(:focus-visible) .swatch-option__chip { outline: 2px solid var(--color-focus); outline-offset: 2px; }
  .swatch-option--custom .swatch-option__chip { background: var(--color-surface); }

  /* A choice as a picture with its name: a layout, a font. */
  .tiles { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-3); }
  .tile {
    display: grid; gap: var(--space-2); padding: var(--space-2); border-radius: var(--radius-md); background: var(--fill);
    font-weight: var(--weight-bold); text-align: center; cursor: pointer;

    input { position: absolute; opacity: 0; }
    /* Inset rings: they follow the squircle and aren't clipped by a scrolling column. */
    &:has(:checked) { background: var(--color-surface); box-shadow: inset 0 0 0 2px var(--color-button); }
    &:has(:focus-visible) { box-shadow: inset 0 0 0 2px var(--color-focus); }
  }
  .tile__art { display: block; height: 4.5rem; border-radius: var(--radius-sm); background: var(--color-canvas); overflow: hidden; }
  .tile__art span { display: block; background: var(--accent); }
  .tile__art--bands span { height: 55%; border-radius: 0 0 50% 30% / 0 0 20% 20%; }
  .tile__art--split span { width: 40%; height: 100%; border-radius: 0 30% 20% 0 / 0 50% 50% 0; }
  /* The events page's layouts: white rows under the band, alone or beside a map. */
  .tile__art--timeline span, .tile__art--map span { height: 30%; border-radius: 0 0 50% 30% / 0 0 30% 30%; }
  .tile__art--timeline { background: linear-gradient(var(--color-surface) 0 0) 70% 48% / 62% 16% no-repeat, linear-gradient(var(--color-surface) 0 0) 70% 82% / 62% 16% no-repeat, var(--color-canvas); }
  .tile__art--map { background: linear-gradient(var(--color-surface) 0 0) 12% 48% / 40% 16% no-repeat, linear-gradient(var(--color-surface) 0 0) 12% 82% / 40% 16% no-repeat, linear-gradient(var(--color-mint) 0 0) 88% 72% / 40% 50% no-repeat, var(--color-canvas); }
  .tile__art--font { display: grid; place-items: center; font-size: var(--text-3xl); letter-spacing: var(--tracking-title); }
  .font--serif { font-family: "Fraunces Variable", ui-serif, Georgia, serif; }

  /* A page on a computer and on a phone at once, captioned. */
  .devices { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 1fr); gap: var(--space-6); align-items: end; }
  .device { display: grid; gap: var(--space-3); margin: 0; }
  .device__screen { position: relative; overflow: hidden; background: var(--color-canvas); box-shadow: var(--shadow-raised); }
  .device__screen iframe { position: absolute; inset: 0 auto auto 0; border: 0; transform-origin: top left; }
  .device--computer .device__screen { aspect-ratio: 16 / 10; border-radius: var(--radius-md); }
  .device--phone .device__screen { aspect-ratio: 9 / 17; border: 0.375rem solid var(--color-surface); border-radius: var(--radius-xl); }
  .device__caption { font-size: var(--text-sm); font-weight: var(--weight-bold); text-align: center; }

  /* The bar that says there's something to save, floating at the bottom of the window. */
  .unsaved {
    position: fixed; inset: auto 0 var(--space-4); z-index: 3; display: flex; gap: var(--space-5); align-items: center;
    width: min(calc(100% - 2 * var(--gutter)), 44rem); margin-inline: auto; padding: var(--space-3) var(--space-3) var(--space-3) var(--space-6);
    border-radius: var(--radius-lg); background: var(--color-surface); box-shadow: var(--shadow-float);

    &[hidden] { display: none; }
  }
  .unsaved__text { flex: 1; font-weight: var(--weight-bold); }

  .address-chip { font-size: var(--text-lg); font-weight: var(--weight-bold); letter-spacing: var(--tracking-snug); }

  /* Something to copy exactly, such as a token or a command, on a fill with its copy button. */
  .secret { display: flex; gap: var(--space-3); align-items: center; padding: var(--space-3) var(--space-4); border-radius: var(--radius-sm); background: var(--fill); }
  .secret__text { flex: 1; min-width: 0; font: var(--text-sm) var(--font-mono); overflow-wrap: anywhere; }
}

@layer layouts {
  /* Settings: the cards, and beside them what guests see. */
  .settings { display: grid; grid-template-columns: minmax(0, 34rem) minmax(0, 1fr); gap: var(--space-8); align-items: start; padding-bottom: var(--space-16); }
  .settings__cards { display: grid; gap: var(--space-4); }
  .settings__preview { position: sticky; top: var(--space-6); display: grid; gap: var(--space-4); }

  /* The page editor: the previews, and the choices in one card beside them. */
  .focus .page:has(> .editor) { max-width: none; }
  .editor { display: grid; grid-template-columns: minmax(0, 1fr) 22rem; gap: var(--space-8); align-items: start; padding-bottom: var(--space-16); }
  .editor .devices { position: sticky; top: var(--space-6); }
  .editor__controls { gap: var(--space-6); }
  .editor__section { display: grid; gap: var(--space-3); margin: 0; padding: 0; border: 0; }
  /* Floated, a legend joins the fieldset's grid and padding like any heading. */
  .editor__title { float: left; width: 100%; padding: 0; font-size: var(--text-lg); font-weight: var(--weight-bold); letter-spacing: var(--tracking-snug); }
  .editor__done, .dock { display: none; }

  @media (max-width: 1023px) {
    .settings { grid-template-columns: minmax(0, 1fr); }
    .settings__preview { position: static; }
    .settings .devices { grid-template-columns: minmax(0, 1fr); justify-items: center; }
    .settings .device--computer { display: none; }
    .settings .device--phone { width: min(100%, 20rem); }
  }

  /* On a phone the page is the editor: the preview fills the screen, the dock opens one choice at a time in a sheet,
     and the unsaved-changes bar takes the dock's place. */
  @media (max-width: 767px) {
    .focus:has(.editor) .page-header__subline { display: none; }
    .editor { grid-template-columns: minmax(0, 1fr); gap: var(--space-4); padding-bottom: 7rem; }
    .editor .devices { position: static; grid-template-columns: minmax(0, 1fr); }
    .editor .device--computer, .editor .device__caption { display: none; }
    .editor .device--phone .device__screen { aspect-ratio: auto; height: calc(100dvh - 15rem); min-height: 24rem; border: 0; border-radius: var(--radius-lg); }

    .editor__controls { padding: 0; background: none; }
    .editor__section {
      position: fixed; inset: auto 0 0; z-index: 4; display: none; padding: var(--space-5) var(--gutter) var(--space-6);
      border-radius: var(--radius-xl) var(--radius-xl) 0 0; background: var(--color-surface); box-shadow: var(--shadow-float);
    }
    .editor__done { display: inline-flex; }
    .editor[data-open="editor-cover"] #editor-cover, .editor[data-open="editor-colour"] #editor-colour,
    .editor[data-open="editor-layout"] #editor-layout, .editor[data-open="editor-font"] #editor-font { display: grid; }
    .editor[data-open] .unsaved { display: none; }

    .dock {
      position: fixed; inset: auto var(--gutter) var(--space-4); z-index: 3; display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-2);
      padding: var(--space-2); border-radius: var(--radius-lg); background: var(--color-surface); box-shadow: var(--shadow-float);
    }
    .editor:has(.unsaved:not([hidden])) .dock, .editor[data-open] .dock { display: none; }
    .dock__item {
      display: grid; gap: var(--space-1); justify-items: center; padding: var(--space-2); border: 0; border-radius: var(--radius-md);
      background: none; font-size: var(--text-sm); font-weight: var(--weight-bold); cursor: pointer;

      &:hover { background: var(--fill); }
    }
    .dock__icon { width: 2.5rem; height: 2rem; border-radius: var(--radius-xs); object-fit: cover; }
    .dock__icon--empty { background: var(--fill); }
    .dock__icon--colour { width: 2rem; border-radius: 50%; corner-shape: round; background: var(--accent); box-shadow: inset 0 0 0 1.5px var(--color-ink-12); }
    .dock__icon--font { display: grid; place-items: center; font-size: var(--text-xl); line-height: 1; }

    .unsaved { padding-left: var(--space-4); gap: var(--space-4); }
    .unsaved__text { font-size: var(--text-sm); white-space: nowrap; }
  }
}
