/*
 * Responsible for: the shape of the mail app that both of its looks share: the window, the folder
 * column, the toolbar over the list, the windowed list, the stacked email row (which is also the row of
 * a phone), the folder label, the Keep button and its menu, the same on every screen that shows them.
 * The colours and the layout of one mail service's look live in kit-mail-gmail.css and kit-mail-outlook.css.
 *
 * Must never: hold a colour, size, radius or speed that is not a custom property from tokens.css (the
 * few raw pixel values are pure geometry: a column width, an icon, a star). Rows must never look
 * pressable. Only transform and opacity are animated, so reduced motion (speeds of 0) removes all movement.
 */
@layer kit {
  /* ---------- The window: it fills the box it is put in, in height and in width ---------- */
  .ia-mail {
    container: ia-mail / inline-size;
    width: 100%;
    height: 100%;
    min-width: 0;
    font-family: var(--sans);
    color: var(--ink);
  }
  .ia-mail[data-mode='picture'] { min-height: calc(var(--s-8) * 4); }
  .ia-mail [hidden] { display: none; }

  .ia-mail__frame {
    --ia-col-w: 220px;
    --ia-mail-row-h: calc(var(--s-7) + var(--s-5));
    --ia-dot-col: var(--s-5);
    --ia-keep-col: calc(var(--s-8) + var(--s-6) + var(--s-5));
    --ia-name-size: var(--t-md);
    --ia-preview-size: var(--t-sm);
    --ia-folder-h: var(--s-6);
    --ia-icon: 20px;
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    background: var(--panel);
    border: 1px solid var(--rule);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-1);
    overflow: hidden;
  }
  .ia-mail__body {
    flex: 1 1 auto;
    display: grid;
    grid-template-columns: var(--ia-col-w) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    min-height: 0;
  }
  /* The bar across the top belongs to a wide window; each look draws it. */
  .ia-mail__bar { display: none; }

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

  /* ---------- Folder column ---------- */
  .ia-mail__col {
    display: flex;
    flex-direction: column;
    gap: var(--s-2);
    min-width: 0;
    min-height: 0;
    padding: var(--s-4) var(--s-2) var(--s-3);
    background: var(--bg);
    border-right: 1px solid var(--rule);
  }
  .ia-mail-compose { display: none; }
  .ia-mail__folders {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-height: 0;
    margin: 0;
    padding: 0;
    list-style: none;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: none;
  }
  .ia-mail__folders::-webkit-scrollbar { display: none; }
  .ia-mail__folders--end { flex: 0 0 auto; overflow: visible; }
  /* In a still picture nothing scrolls, so the junk label needs no pin: it follows the other labels as one list. */
  .ia-mail[data-mode='picture'] .ia-mail__folders:not(.ia-mail__folders--end) { flex: 0 1 auto; }
  .ia-mail__folders--end .ia-mail-folder { margin-top: var(--s-2); }
  .ia-mail-folder[data-group='2'] + .ia-mail-folder[data-group='1'] { margin-top: var(--s-3); }

  /* A real folder and a row that is only drawn to complete the picture stand alike and are told apart by their words. */
  .ia-mail-folder,
  .ia-mail-deco {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: var(--s-2);
    min-width: 0;
    min-height: var(--ia-folder-h);
    padding: 0 var(--s-3);
    border-radius: var(--r-sm);
    font-size: var(--t-sm);
    color: var(--ink);
    cursor: default;
    user-select: none;
    -webkit-user-select: none;
  }
  .ia-mail-deco { color: var(--muted); }
  .ia-mail-deco.is-heading { font-weight: 600; }
  .ia-mail-folder.is-current { background: var(--tint); font-weight: 600; }
  .ia-mail-folder.is-junk { background: color-mix(in srgb, var(--c-junk) 14%, transparent); font-weight: 600; }
  .ia-mail-folder__icon,
  .ia-mail-deco__icon {
    --c: var(--muted);
    flex: none;
    display: grid;
    place-items: center;
    width: var(--ia-icon);
    height: var(--ia-icon);
    color: var(--c);
  }
  .ia-mail-folder.is-current .ia-mail-folder__icon { color: var(--accent); }
  .ia-mail__icon { width: 100%; height: 100%; }
  .ia-mail__icon--solid { stroke: none; }
  .ia-mail-folder__name,
  .ia-mail-deco__name { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .ia-mail-folder__count {
    flex: none;
    font-size: var(--t-xs);
    color: var(--faint);
    font-variant-numeric: tabular-nums;
  }
  .ia-mail-folder.is-strong .ia-mail-folder__count { color: var(--ink); font-weight: 700; }
  .ia-mail-folder.is-junk .ia-mail-folder__count { color: var(--c-junk); font-weight: 600; }
  /* A folder the person can open: the button holds the words, and its hit area covers the whole row, icon included. */
  .ia-mail-folder.is-pressable { position: relative; }
  .ia-mail-folder__press {
    all: unset;
    display: flex;
    align-items: center;
    gap: inherit;
    flex: 1 1 auto;
    min-width: 0;
    align-self: stretch;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .ia-mail-folder__press::after { content: ''; position: absolute; inset: 0; }
  /* The ring is drawn on the row, so it follows the row's own rounded shape in each skin. */
  .ia-mail-folder.is-pressable:has(.ia-mail-folder__press:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }
  @media (hover: hover) {
    .ia-mail-folder.is-pressable:not(.is-current):hover { background: color-mix(in srgb, var(--ink) 6%, transparent); }
  }

  /* ---------- Toolbar over the list ---------- */
  .ia-mail__main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--panel); }
  .ia-mail__head {
    flex: none;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--s-2);
    padding: var(--s-3) var(--s-4) var(--s-2);
    border-bottom: 1px solid var(--rule);
  }
  .ia-mail__head-tools { display: none; }
  .ia-mail__head-title { font-size: var(--t-lg); font-weight: 700; }
  .ia-mail__head-count { margin-left: auto; font-size: var(--t-xs); color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

  /* ---------- List ---------- */
  .ia-mail-list {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    overflow-anchor: none;
    overscroll-behavior: contain;
    touch-action: pan-y;
    scrollbar-width: thin;
    scrollbar-color: var(--rule) transparent;
  }
  .ia-mail-list.is-picture { overflow: hidden; scrollbar-width: none; }
  .ia-mail-list:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
  .ia-mail-list__spacer { position: relative; width: 100%; overflow: hidden; }

  /* ---------- Row: shows an email, opens nothing. Stacked: sender and time, subject, preview. ---------- */
  .ia-mail-row {
    position: absolute;
    top: 0;
    left: 0;
    box-sizing: border-box;
    width: 100%;
    height: var(--ia-mail-row-h);
    display: grid;
    grid-template-columns: var(--ia-dot-col) minmax(0, 1fr);
    align-items: center;
    padding-right: var(--s-4);
    background: var(--panel);
    cursor: default;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    contain: layout paint style;
    transition: translate var(--move) var(--ease);
  }
  .ia-mail-row.is-keepable { grid-template-columns: var(--ia-dot-col) minmax(0, 1fr) var(--ia-keep-col); padding-right: 0; }
  .ia-mail-row--probe { visibility: hidden; pointer-events: none; transition: none; }
  .ia-mail-row::after {
    content: '';
    position: absolute;
    left: var(--ia-dot-col);
    right: 0;
    bottom: 0;
    border-bottom: 1px solid var(--rule);
  }
  .ia-mail-row.is-entering { animation: ia-mail-in var(--move) var(--ease) both; }
  .ia-mail-row.is-leaving { animation: ia-mail-out var(--fast) var(--ease) forwards; pointer-events: none; }
  @keyframes ia-mail-in { from { opacity: 0; scale: .94; } }
  @keyframes ia-mail-out { to { opacity: 0; scale: .94; } }

  .ia-mail-row__box { display: none; }
  .ia-mail-row__dot { width: 10px; height: 10px; justify-self: center; border-radius: var(--r-pill); }
  .ia-mail-row.is-unread .ia-mail-row__dot { background: var(--unread); }
  .ia-mail-row__body { display: flex; flex-direction: column; justify-content: center; gap: 1px; min-width: 0; }
  .ia-mail-row__top { display: flex; align-items: center; gap: var(--s-2); min-width: 0; }
  .ia-mail-row__name {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: var(--ia-name-size);
    font-weight: 500;
    line-height: 1.35;
  }
  .ia-mail-row.is-unread .ia-mail-row__name { font-weight: 700; }
  .ia-mail-row__star { flex: none; display: grid; width: 13px; height: 13px; color: var(--star); }
  .ia-mail-row__star:not(.is-on) { display: none; }
  .ia-mail__star-icon { width: 100%; height: 100%; }
  .ia-mail-row__time { flex: none; font-size: var(--t-xs); color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

  /* The label, then the subject, on one line; the preview under them. The copy's own box vanishes so all three can be placed. */
  .ia-mail-row__text { display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; min-width: 0; }
  .ia-mail-row__copy { display: contents; }
  .ia-mail-row__subject,
  .ia-mail-row__preview {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: var(--t-sm);
    line-height: 1.35;
  }
  .ia-mail-row__subject { grid-column: 2; grid-row: 1; }
  .ia-mail-row.is-unread .ia-mail-row__subject { font-weight: 600; }
  .ia-mail-row__dash { display: none; }
  .ia-mail-row__preview { grid-column: 1 / -1; grid-row: 2; font-size: var(--ia-preview-size); color: var(--muted); }

  /* An email without its words yet: a quiet bar stands where the subject and another where the preview will be, as high as the
     words and inside a row of fixed height, so nothing moves when the words come. They are still: motion has nothing to respect. */
  .ia-mail-row.is-waiting .ia-mail-row__subject::before,
  .ia-mail-row.is-waiting .ia-mail-row__preview::before {
    content: '';
    display: inline-block;
    height: .7em;
    vertical-align: middle;
    border-radius: var(--r-pill);
    background: var(--rule);
  }
  .ia-mail-row.is-waiting .ia-mail-row__subject::before { width: min(16em, 60%); }
  .ia-mail-row.is-waiting .ia-mail-row__preview::before { width: min(26em, 90%); }

  /* The folder's name on the email, in the folder's own colour. */
  .ia-mail-chip {
    --c: var(--muted);
    grid-column: 1;
    grid-row: 1;
    max-width: 11em;
    margin-right: var(--s-2);
    padding: 0 var(--s-2);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    border-radius: var(--r-sm);
    background: var(--c);
    color: var(--on-accent);
    font-size: var(--t-xs);
    font-weight: 600;
    line-height: 1.6;
  }

  /* ---------- Keep button and its menu ---------- */
  .ia-mail-row__keep {
    appearance: none;
    display: grid;
    place-items: center;
    width: calc(var(--ia-keep-col) - var(--s-2));
    height: var(--tap);
    justify-self: center;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: var(--r-md);
    background: none;
    font: inherit;
    color: var(--muted);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .ia-mail-row__keep-label {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--s-1);
    padding: var(--s-1) var(--s-2);
    border: 1px solid var(--rule);
    border-radius: var(--r-pill);
    font-size: var(--t-xs);
    font-weight: 600;
    line-height: 1.4;
  }
  .ia-mail__check-icon { width: 12px; height: 12px; }
  .ia-mail-row__keep:not(.is-kept) .ia-mail__check-icon { display: none; }
  .ia-mail-row__keep.is-kept { color: var(--accent); }
  .ia-mail-row__keep.is-kept .ia-mail-row__keep-label { border-color: transparent; background: var(--tint); }
  .ia-mail-row__keep:focus-visible,
  .ia-mail-menu__item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

  .ia-mail-menu {
    position: absolute;
    z-index: var(--z-popover);
    display: flex;
    flex-direction: column;
    width: max-content;
    max-width: min(22rem, calc(100% - var(--s-4)));
    padding: var(--s-1);
    background: var(--pop);
    border: 1px solid var(--rule);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-2);
  }
  .ia-mail-menu__item {
    appearance: none;
    min-height: var(--tap);
    margin: 0;
    padding: var(--s-2) var(--s-3);
    border: 0;
    border-radius: var(--r-sm);
    background: none;
    font: inherit;
    font-size: var(--t-sm);
    line-height: 1.35;
    color: var(--ink);
    text-align: left;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .ia-mail-menu__item:focus-visible { background: var(--hover); }
  @media (hover: hover) {
    .ia-mail-row__keep:hover .ia-mail-row__keep-label { background: var(--hover); }
    .ia-mail-menu__item:hover { background: var(--hover); }
  }

  /* ---------- Phone: the container is narrower than a laptop window (placed last so it wins) ---------- */
  @container ia-mail (max-width: 560px) {
    .ia-mail__frame {
      --ia-col-w: 136px;
      --ia-mail-row-h: calc(var(--s-7) + var(--s-4) + var(--s-1));
      --ia-dot-col: calc(var(--s-4) + var(--s-1));
      --ia-keep-col: calc(var(--s-7) + var(--s-3));
      --ia-name-size: var(--t-sm);
      --ia-preview-size: var(--t-xs);
      --ia-folder-h: calc(var(--s-5) + var(--s-1));
      --ia-icon: 14px;
    }
    .ia-mail__col { padding: var(--s-3) var(--s-1); }
    .ia-mail__head-unread { display: none; }
    .ia-mail-folder,
    .ia-mail-deco { gap: var(--s-1); padding: 0 var(--s-1) 0 var(--s-2); font-size: var(--t-xs); }
    .ia-mail__check-icon { display: none; }
    .ia-mail__head { padding: var(--s-3) var(--s-3) var(--s-2); }
    .ia-mail-row__keep-label { padding: 0 var(--s-2); white-space: nowrap; line-height: calc(var(--s-5) - var(--s-1)); }
    /* A row with a Keep button: the button takes the preview's line at the right, in one line, so the sender and the subject keep the width. */
    .ia-mail-row.is-keepable { grid-template-columns: var(--ia-dot-col) minmax(0, 1fr); padding-right: var(--s-2); }
    .ia-mail-row.is-keepable .ia-mail-row__preview { display: none; }
    .ia-mail-row.is-keepable .ia-mail-row__body { grid-column: 2; grid-row: 1; }
    .ia-mail-row.is-keepable .ia-mail-row__keep { grid-column: 2; grid-row: 1; justify-self: end; align-self: end; align-items: end; width: auto; padding-bottom: var(--s-1); }
    .ia-mail-row.is-keepable .ia-mail-row__keep-label { border-radius: var(--r-pill); }
    /* On a phone the folder's name takes the preview's line, so the subject keeps the whole line above it. */
    .ia-mail-row__subject { grid-column: 1 / -1; }
    .ia-mail-row__preview { grid-column: 2; }
    .ia-mail-chip { grid-row: 2; max-width: 7em; }
  }
}
