/*
 * Responsible for: how the product's small popups look and move: the popover and its sheet form on a phone,
 * the menu inside it, the name and folder-choice dialogs, and the notice at the bottom of the screen.
 *
 * Must never: hold a colour, size or speed of its own (everything comes from tokens.css), animate anything
 * but transform and opacity, let a popup run past the window's edge or cover the phone's home bar, or hide
 * the keyboard focus ring.
 */
@layer kit {
  .ia-popover,
  .ia-popover *,
  .ia-dialog,
  .ia-dialog *,
  .ia-notice,
  .ia-notice * {
    box-sizing: border-box;
  }

  .ia-popover,
  .ia-dialog,
  .ia-notice {
    font-family: var(--sans);
    color: var(--ink);
  }

  /* Every pressable thing in a popup starts from the same bare button. */
  .ia-popover-close,
  .ia-menu-item,
  .ia-dialog-button,
  .ia-choose-item,
  .ia-notice-action,
  .ia-notice-close {
    appearance: none;
    margin: 0;
    border: 0;
    background: none;
    font: inherit;
    color: inherit;
    cursor: pointer;
  }

  .ia-menu-item:hover,
  .ia-dialog-button:hover,
  .ia-choose-item:hover {
    background: var(--hover);
  }

  .ia-popover-close:focus-visible,
  .ia-menu-item:focus-visible,
  .ia-choose-item:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
  }

  @keyframes ia-popover-in {
    from { opacity: 0; transform: translateY(var(--s-1)); }
    to { opacity: 1; transform: none; }
  }

  @keyframes ia-sheet-in {
    from { transform: translateY(100%); }
    to { transform: none; }
  }

  @keyframes ia-fade-in {
    from { opacity: 0; }
  }

  /* Popover, and the sheet it becomes on a narrow phone */
  .ia-popover {
    position: fixed;
    z-index: var(--z-popover);
    min-width: 200px;
    max-width: calc(100vw - var(--s-4));
    max-height: calc(100dvh - var(--s-4));
    overflow: auto;
    overscroll-behavior: contain;
    background: var(--pop);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-2);
    outline: none;
    animation: ia-popover-in var(--fast) var(--ease);
  }

  .ia-popover-body {
    padding: var(--s-1);
  }

  .ia-popover-scrim {
    position: fixed;
    inset: 0;
    z-index: calc(var(--z-popover) - 1);
    background: var(--ink);
    opacity: .28;
    animation: ia-fade-in var(--fast) var(--ease);
  }

  .ia-popover[data-sheet] {
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100%;
    max-width: none;
    max-height: 80dvh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    padding-bottom: env(safe-area-inset-bottom);
    animation: ia-sheet-in var(--move) var(--ease);
  }

  .ia-popover[data-sheet] .ia-popover-body {
    padding: var(--s-6) var(--s-2) var(--s-2);
  }

  .ia-popover-close {
    position: absolute;
    top: var(--s-1);
    right: var(--s-1);
    display: grid;
    place-items: center;
    width: var(--tap);
    height: var(--tap);
    padding: 0;
    border-radius: var(--r-pill);
    color: var(--muted);
  }

  .ia-popover-close svg,
  .ia-notice-close svg,
  .ia-menu-check {
    width: var(--s-4);
    height: var(--s-4);
  }

  /* Menu */
  .ia-menu {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .ia-menu-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s-4);
    width: 100%;
    min-height: var(--s-7);
    padding: 0 var(--s-3);
    border-radius: var(--r-sm);
    font-size: var(--t-md);
    text-align: start;
  }

  .ia-menu-check {
    color: var(--accent);
  }

  /* Dialogs. The dialog is the whole window; its panel is the card; the dimming is a layer that fades in. */
  .ia-dialog {
    margin: 0;
    padding: clamp(var(--s-5), 14vh, 140px) var(--s-4) var(--s-4);
    border: 0;
    width: 100%;
    max-width: none;
    height: 100%;
    max-height: none;
    background: none;
    overflow: hidden;
  }

  .ia-dialog[open] {
    display: grid;
    justify-items: center;
    align-items: start;
  }

  .ia-dialog::backdrop {
    background: none;
  }

  .ia-dialog::before {
    content: "";
    position: fixed;
    inset: 0;
    background: var(--ink);
    opacity: .32;
    animation: ia-fade-in var(--fast) var(--ease);
  }

  .ia-dialog-panel {
    position: relative;
    width: 100%;
    max-width: 420px;
    max-height: 100%;
    display: flex;
    flex-direction: column;
    margin: 0;
    padding: var(--s-5);
    background: var(--panel);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-2);
    animation: ia-popover-in var(--move) var(--ease);
  }

  .ia-dialog-title {
    margin: 0 0 var(--s-4);
    font-size: var(--t-lg);
    font-weight: 600;
    line-height: 1.3;
  }

  .ia-dialog-field {
    display: grid;
    gap: var(--s-2);
  }

  .ia-dialog-label { font-size: var(--t-sm); color: var(--muted); }

  /* 17px: a smaller field makes an iPhone zoom the page when the field is touched. */
  .ia-dialog-input {
    appearance: none;
    width: 100%;
    height: var(--tap);
    margin: 0;
    padding: 0 var(--s-3);
    border: 1px solid var(--rule);
    border-radius: var(--r-md);
    background: var(--panel);
    font: inherit;
    font-size: var(--t-lg);
    color: var(--ink);
  }

  .ia-dialog-input:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    border-color: transparent;
  }

  .ia-dialog-input[aria-invalid="true"] { border-color: var(--warn); }
  .ia-dialog-input[aria-invalid="true"]:focus-visible { outline-color: var(--warn); }

  .ia-dialog-refusal {
    min-height: calc(var(--t-sm) * 1.4);
    margin: var(--s-2) 0 var(--s-3);
    font-size: var(--t-sm);
    line-height: 1.4;
    color: var(--warn);
  }

  .ia-dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--s-2);
    margin-top: var(--s-2);
  }

  .ia-dialog-button {
    /* A link that looks like a button (the one that goes to Google) centres its words as a button does. */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    min-height: var(--tap);
    padding: 0 var(--s-4);
    border-radius: var(--r-md);
    font-size: var(--t-md);
    font-weight: 600;
  }

  .ia-dialog-button:focus-visible,
  .ia-notice-action:focus-visible,
  .ia-notice-close:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  .ia-dialog-primary {
    background: var(--accent);
    color: var(--on-accent);
  }

  .ia-dialog-primary:hover {
    background: var(--accent-strong);
  }

  .ia-dialog-primary:disabled {
    opacity: .4;
    cursor: not-allowed;
  }

  /* Folder choice */
  .ia-choose-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-height: 0;
    max-height: min(50dvh, 420px);
    margin: 0 calc(var(--s-2) * -1) var(--s-3);
    overflow: auto;
    overscroll-behavior: contain;
  }

  .ia-choose-item {
    flex: none;
    display: flex;
    align-items: center;
    gap: var(--s-3);
    min-height: var(--tap);
    padding: 0 var(--s-3) 0 calc(var(--s-3) + var(--depth, 0) * var(--s-4));
    border-radius: var(--r-md);
    font-size: var(--t-md);
    text-align: start;
  }

  .ia-choose-item[data-root] {
    font-weight: 600;
  }

  .ia-choose-icon {
    flex: none;
    width: var(--s-5);
    height: var(--s-5);
    color: var(--c, var(--muted));
  }

  .ia-choose-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Notice: on the dark ink, so it cannot be mistaken for the page's own content. */
  .ia-notice {
    position: fixed;
    z-index: var(--z-notice);
    left: 0;
    right: 0;
    bottom: calc(var(--s-4) + env(safe-area-inset-bottom));
    display: flex;
    justify-content: center;
    padding-inline: var(--s-4);
    pointer-events: none;
  }

  .ia-notice-body {
    pointer-events: auto;
    display: flex;
    align-items: center;
    gap: var(--s-2);
    max-width: min(100%, 360px);
    padding: var(--s-1) var(--s-1) var(--s-1) var(--s-4);
    border-radius: var(--r-lg);
    background: var(--ink);
    color: var(--on-accent);
    box-shadow: var(--shadow-2);
    font-size: var(--t-md);
    line-height: 1.35;
    animation: ia-popover-in var(--move) var(--ease);
  }

  .ia-notice-text {
    min-width: 0;
    padding-block: var(--s-2);
  }

  .ia-notice-action {
    flex: none;
    min-height: var(--tap);
    padding: 0 var(--s-3);
    border-radius: var(--r-md);
    font-weight: 600;
    color: var(--tint);
  }

  .ia-notice-close {
    flex: none;
    display: grid;
    place-items: center;
    width: var(--tap);
    height: var(--tap);
    padding: 0;
    border-radius: var(--r-pill);
    opacity: .7;
  }

  .ia-notice-action:hover,
  .ia-notice-close:hover,
  .ia-notice-close:focus-visible {
    opacity: 1;
  }

  .ia-notice-action:focus-visible,
  .ia-notice-close:focus-visible {
    outline-color: var(--on-accent);
    outline-offset: -2px;
  }
}
