@layer reset, tokens, base, layout, components;

/* ------------------------------------------------------------------ */
/* Reset                                                               */
/* ------------------------------------------------------------------ */

@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  * {
    margin: 0;
  }

  img,
  svg {
    display: block;
    max-width: 100%;
  }

  input,
  button,
  select,
  textarea {
    font: inherit;
    color: inherit;
  }

  ul[role="list"] {
    list-style: none;
    padding: 0;
  }

  [x-cloak] {
    display: none !important;
  }
}

/* ------------------------------------------------------------------ */
/* Design tokens                                                       */
/* Warm editorial palette after Monarch: linen canvas, paper cards,   */
/* ink text and one chromatic accent (ember orange). Every text       */
/* colour is checked for WCAG 2.1 AA (>= 4.5:1) against bg, surfaces  */
/* and every tint; form control borders for non-text contrast        */
/* (>= 3:1). White on ember is only 2.9:1, so filled ember buttons    */
/* carry ink text; ember text uses the darker --color-accent-strong.  */
/* ------------------------------------------------------------------ */

@layer tokens {
  :root {
    color-scheme: light;

    /* light-dark(light value, dark value), switched by color-scheme */
    --color-bg: light-dark(#efecea, #1c1a17);
    --color-surface: light-dark(#ffffff, #24211e);
    --color-surface-2: light-dark(#f7f5f3, #2e2a26);
    --color-border: light-dark(#dcd9d6, #3a3631);
    --color-field-border: light-dark(#8f8c89, #7f7a74);
    --color-text: light-dark(#22201d, #efecea);
    --color-text-muted: light-dark(#666461, #a9a49f);

    /* Ember is the only accent: filled with ink text on top */
    --color-accent: light-dark(#ff692d, #ff7a45);
    --color-on-accent: #22201d;
    --color-accent-soft: light-dark(#fbe5da, #3a2519);
    --color-accent-strong: light-dark(#a83c0e, #ff9a6e);

    --color-success: light-dark(#2c6b3f, #8ad3a0);
    --color-success-soft: light-dark(#e3efe2, #1f2e23);
    --color-danger: light-dark(#b3262f, #ffa3a3);
    --color-danger-soft: light-dark(#fbe3e1, #3a2222);

    /* Warm tints for stat icons and avatars: ember and linen shades
       only, no extra hues (text on them stays --color-text) */
    --tint-lav: light-dark(#fbe5da, #3a2519);
    --tint-mint: light-dark(#efecea, #34302b);
    --tint-peach: light-dark(#f6dccd, #43291c);
    --tint-sky: light-dark(#e6e1dc, #2f2b27);
    --tint-butter: light-dark(#f8ece1, #3b2f25);
    --tint-pink: light-dark(#ece4dd, #36302a);

    --font-sans: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI",
      Roboto, sans-serif;
    --font-serif: "Fraunces", "Source Serif 4", Georgia, "Times New Roman",
      serif;

    --radius-sm: 0.5rem;
    --radius-md: 0.75rem;
    --radius-full: 9999px;

    --shadow-sm: 0 1px 2px rgb(34 32 29 / 0.05);
    --shadow-md: 0 10px 15px -3px rgb(34 32 29 / 0.1),
      0 4px 6px -4px rgb(34 32 29 / 0.1);

    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.5rem;
    --space-6: 2rem;
    --space-7: 3rem;
    --space-8: 4rem;

    --gutter: clamp(1rem, 4vw, 2rem);
    --content-width: 68rem;

    --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
    --duration-fast: 120ms;
    --duration: 200ms;
    /* The form dialog's slide where the WebGL peel can't run: a quick
       slide in that overshoots a little, a quicker slide out. main.ts
       waits --duration-slide-out before closing the dialog */
    --duration-slide-in: 340ms;
    --duration-slide-out: 160ms;
    --ease-bounce: cubic-bezier(0.34, 1.45, 0.64, 1);
    --ease-in: cubic-bezier(0.55, 0, 1, 0.45);
  }

  :root[data-theme="dark"] {
    color-scheme: dark;
    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.3);
    --shadow-md: 0 10px 15px -3px rgb(0 0 0 / 0.4),
      0 4px 6px -4px rgb(0 0 0 / 0.4);
  }

  @media (prefers-reduced-motion: reduce) {
    :root {
      --duration-fast: 0ms;
      --duration: 0ms;
      --duration-slide-in: 0ms;
      --duration-slide-out: 0ms;
    }
  }
}

/* ------------------------------------------------------------------ */
/* Base                                                                */
/* ------------------------------------------------------------------ */

@layer base {
  html {
    -webkit-text-size-adjust: none;
    text-size-adjust: none;
    scrollbar-gutter: stable;
  }

  body {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--color-bg);
    color: var(--color-text);
    font-family: var(--font-sans);
    font-size: 1rem;
    line-height: 1.5;
    letter-spacing: -0.01em;
    -webkit-font-smoothing: antialiased;
  }

  /* Serif display face for headings only, light weight; hierarchy comes
     from size and tight tracking, not weight */
  h1,
  h2 {
    font-family: var(--font-serif);
    font-weight: 400;
    font-variation-settings: "opsz" 72;
    line-height: 1.2;
    letter-spacing: -0.04em;
    text-wrap: balance;
  }

  h1 {
    font-size: clamp(2rem, 1.5rem + 2vw, 2.5rem);
  }

  h3 {
    line-height: 1.33;
    font-weight: 500;
    text-wrap: balance;
  }

  p {
    text-wrap: pretty;
  }

  a {
    color: var(--color-accent-strong);
    font-weight: 500;
    text-decoration-color: color-mix(in oklab, currentColor, transparent 50%);
    text-underline-offset: 0.2em;
    text-decoration-thickness: 1px;
  }

  :focus-visible {
    outline: 2px solid var(--color-accent-strong);
    outline-offset: 2px;
  }

  ::selection {
    background: var(--color-accent);
    color: var(--color-on-accent);
  }

  .icon {
    width: 1.125em;
    height: 1.125em;
    flex: none;
  }

  /* Set before first paint when the saved language isn't the English
     markup: main.js removes it once Alpine has translated the page. If the
     script never runs, the page shows up (in English) after a second. */
  :root[data-i18n-pending] body {
    visibility: hidden;
    animation: i18n-reveal 0s 1s forwards;
  }

  @keyframes i18n-reveal {
    to {
      visibility: visible;
    }
  }

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

  .skip-link {
    position: absolute;
    top: var(--space-2);
    left: var(--space-2);
    z-index: 100;
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius-full);
    background: var(--color-accent);
    color: var(--color-on-accent);
    translate: 0 -200%;

    &:focus-visible {
      translate: 0 0;
    }
  }
}

/* ------------------------------------------------------------------ */
/* Layout                                                              */
/* ------------------------------------------------------------------ */

@layer layout {
  .container {
    width: min(100% - 2 * var(--gutter), var(--content-width));
    margin-inline: auto;
  }

  main {
    flex: 1;
    padding-block: var(--space-7) var(--space-8);
    view-transition-name: content;

    @media (width < 40rem) {
      padding-block: var(--space-5) var(--space-7);
    }
  }

  .stack {
    display: grid;
    gap: var(--space-8);
  }

  .section-title {
    font-size: 2rem;
  }

  h2.section-title {
    font-size: 1.75rem;
  }

  .panel {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
  }
}

/* ------------------------------------------------------------------ */
/* Components                                                          */
/* ------------------------------------------------------------------ */

@layer components {
  /* Buttons ---------------------------------------------------------- */

  .btn {
    --btn-bg: var(--color-surface);
    --btn-fg: var(--color-text);
    --btn-border: var(--color-border);

    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: 2.75rem;
    padding: var(--space-2) var(--space-5);
    border: 1px solid var(--btn-border);
    border-radius: var(--radius-full);
    background: var(--btn-bg);
    color: var(--btn-fg);
    font-weight: 500;
    font-size: 1rem;
    line-height: 1.2;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
    transition:
      background-color var(--duration-fast) ease,
      border-color var(--duration-fast) ease,
      color var(--duration-fast) ease;

    &:hover {
      --btn-bg: var(--color-surface-2);
      --btn-border: var(--color-field-border);
    }

    /* After the variants, so it wins over their hover */
    &:disabled,
    &:disabled:hover {
      --btn-bg: var(--color-surface-2);
      --btn-fg: var(--color-text-muted);
      --btn-border: var(--color-border);

      box-shadow: none;
      cursor: not-allowed;
    }
  }

  .btn--primary {
    --btn-bg: var(--color-accent);
    --btn-fg: var(--color-on-accent);
    --btn-border: transparent;

    box-shadow: var(--shadow-sm);

    &:hover {
      --btn-bg: color-mix(in oklab, var(--color-accent), #22201d 10%);
      --btn-border: transparent;
    }
  }

  .btn--danger {
    --btn-fg: var(--color-danger);

    &:hover {
      --btn-bg: var(--color-danger-soft);
      --btn-border: var(--color-danger);
    }
  }

  .btn--ghost {
    --btn-bg: transparent;
    --btn-border: transparent;

    &:hover {
      --btn-border: transparent;
    }
  }

  .btn--sm {
    min-height: 2.375rem;
    padding: var(--space-1) var(--space-4);
    font-size: 0.875rem;
  }

  /* Round icon-only button */
  .icon-btn {
    display: inline-grid;
    place-items: center;
    width: 2.25rem;
    height: 2.25rem;
    padding: 0;
    border: 1px solid transparent;
    border-radius: var(--radius-full);
    background: transparent;
    color: var(--color-text-muted);
    cursor: pointer;
    transition:
      background-color var(--duration-fast) ease,
      border-color var(--duration-fast) ease,
      color var(--duration-fast) ease;

    &:hover {
      background: var(--color-surface-2);
      color: var(--color-text);
    }

    .icon {
      width: 1.125rem;
      height: 1.125rem;
    }
  }

  .icon-btn--danger:hover {
    background: var(--color-danger-soft);
    color: var(--color-danger);
  }

  /* Header ----------------------------------------------------------- */

  @scope (.site-header) {
    :scope {
      position: sticky;
      top: 0;
      z-index: 10;
      border-bottom: 1px solid var(--color-border);
      background: var(--color-surface);
      view-transition-name: site-header;
    }

    .app-bar {
      min-height: 4rem;
    }
  }

  /* While there are no subscriptions the empty state shows the brand and
     the controls itself (main.ts and the script in base.vto set the
     attribute) */
  :root[data-empty] .site-header {
    display: none;
  }

  /* Brand and controls (partials/app-bar.vto): the header's content, and
     the top of the empty state */
  @scope (.app-bar) {
    :scope {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: var(--space-4);

      @media (width < 40rem) {
        gap: var(--space-3);
      }
    }

    .brand {
      display: inline-flex;
      align-items: center;
      gap: var(--space-3);
      min-width: 0;
      color: var(--color-text);
      font-family: var(--font-serif);
      font-size: 1.375rem;
      font-weight: 400;
      letter-spacing: -0.03em;
      text-decoration: none;

      img {
        width: 1.75rem;
        height: 1.75rem;
      }

      span {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      /* Phones: a smaller name that wraps onto two lines rather than
         getting cut off next to the buttons */
      @media (width < 40rem) {
        gap: var(--space-2);
        font-size: 1.125rem;
        line-height: 1.15;

        img {
          width: 1.5rem;
          height: 1.5rem;
        }

        span {
          white-space: normal;
          text-wrap: balance;
        }
      }
    }

    .app-bar__actions {
      display: flex;
      align-items: center;
      gap: var(--space-2);
    }

    /* Segmented control with the language codes; the pressed one is
       raised onto the surface */
    .lang-switch {
      display: flex;
      gap: 2px;
      padding: 3px;
      border: 1px solid var(--color-border);
      border-radius: var(--radius-full);
      background: var(--color-bg);
    }

    .lang-switch__option {
      min-width: 2.25rem;

      @media (width < 40rem) {
        min-width: 2rem;
      }

      height: 1.875rem;
      padding: 0 var(--space-2);
      border: 0;
      border-radius: var(--radius-full);
      background: transparent;
      color: var(--color-text-muted);
      font-size: 0.75rem;
      font-weight: 500;
      letter-spacing: 0.04em;
      text-transform: uppercase;
      cursor: pointer;
      transition:
        background-color var(--duration-fast) ease,
        color var(--duration-fast) ease;

      &:hover {
        color: var(--color-text);
      }

      &[aria-pressed="true"] {
        background: var(--color-surface);
        color: var(--color-text);
        box-shadow: var(--shadow-sm);
        cursor: default;
      }
    }

    .theme-toggle {
      border-color: var(--color-border);

      /* Both icons share the same cell, only one is visible */
      > span {
        display: grid;
        grid-area: 1 / 1;
      }
    }
  }

  /* Kept outside @scope: scoped rules win on proximity, which would
     stop the dark-theme overrides below from applying */
  .theme-toggle__sun {
    visibility: hidden;
  }

  :root[data-theme="dark"] {
    .theme-toggle__sun {
      visibility: visible;
    }

    .theme-toggle__moon {
      visibility: hidden;
    }
  }

  /* Footer ----------------------------------------------------------- */

  @scope (.site-footer) {
    :scope {
      padding-block: var(--space-5);
      border-top: 1px solid var(--color-border);
      color: var(--color-text-muted);
      font-size: 0.8125rem;
      text-align: center;
    }
  }

  /* Empty state ------------------------------------------------------ */

  @scope (.empty) {
    :scope {
      display: grid;
      max-width: 40rem;
      margin: var(--space-6) auto 0;
      border: 0;
      overflow: clip;

      @media (width < 40rem) {
        margin-top: 0;
      }
    }

    /* The header's controls, as the card's top row */
    .empty__bar {
      padding: var(--space-3) var(--space-4) var(--space-3) var(--space-5);
      border-bottom: 1px solid var(--color-border);

      @media (width < 40rem) {
        padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
      }
    }

    .empty__body {
      display: grid;
      justify-items: center;
      gap: var(--space-3);
      padding: var(--space-7) var(--space-5) var(--space-8);
      text-align: center;

      @media (width < 40rem) {
        padding: var(--space-6) var(--space-4);
      }
    }

    p {
      max-width: 32rem;
      color: var(--color-text-muted);
      font-size: 1.125rem;
    }

    .empty__actions {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      align-items: center;
      gap: var(--space-3);
      margin-top: var(--space-4);

      @media (width < 40rem) {
        flex-direction: column;
        align-self: stretch;

        .btn {
          width: 100%;
        }
      }
    }

    .empty__or {
      color: var(--color-text-muted);
      font-size: 0.875rem;
    }

    /* Import, as the card's bottom row (the bar mirrors it on top) */
    .empty__import {
      display: flex;
      justify-content: center;
      padding: var(--space-4);
      border-top: 1px solid var(--color-border);
      background: var(--color-surface-2);
    }
  }

  /* Stats ------------------------------------------------------------ */
  /* One bordered strip split in cells: the 1px gap shows the border    */
  /* colour behind them.                                                */

  @scope (.stats) {
    :scope {
      display: grid;
      gap: var(--space-5);
    }

    .stats__grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
      gap: 1px;
      overflow: hidden;
      background: var(--color-border);
    }

    .stat {
      display: grid;
      align-content: start;
      gap: var(--space-1);
      padding: var(--space-5);
      background: var(--color-surface);
    }

    /* Eyebrow: the one place for tracked caps */
    .stat__label {
      display: flex;
      align-items: center;
      gap: var(--space-2);
      color: var(--color-text-muted);
      font-size: 0.75rem;
      font-weight: 500;
      letter-spacing: 0.06em;
      text-transform: uppercase;
    }

    .stat__icon {
      display: grid;
      place-items: center;
      width: 1.75rem;
      height: 1.75rem;
      border-radius: var(--radius-full);
      background: var(--tint);
      color: var(--color-text);

      .icon {
        width: 1rem;
        height: 1rem;
      }
    }

    .stat--lav {
      --tint: var(--tint-lav);
    }

    .stat--mint {
      --tint: var(--tint-mint);
    }

    .stat--peach {
      --tint: var(--tint-peach);
    }

    .stat__value {
      margin-top: var(--space-2);
      font-family: var(--font-serif);
      font-size: 2.5rem;
      font-weight: 400;
      font-variation-settings: "opsz" 72;
      line-height: 1.1;
      letter-spacing: -0.04em;
      font-variant-numeric: lining-nums tabular-nums;
    }

    .stat__desc {
      color: var(--color-text-muted);
      font-size: 0.8125rem;
    }
  }

  /* Subscriptions list ----------------------------------------------- */

  @scope (.subs) {
    :scope {
      display: grid;
      gap: var(--space-5);
    }

    .subs__header {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      justify-content: space-between;
      gap: var(--space-3);

      @media (width < 40rem) {
        .btn {
          width: 100%;
        }
      }
    }

    .subs__title {
      display: flex;
      align-items: center;
      align-items: baseline;
      gap: var(--space-3);
    }

    .subs__count {
      min-width: 1.75rem;
      padding: 0.125rem var(--space-2);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-full);
      background: var(--color-surface);
      color: var(--color-text-muted);
      font-family: var(--font-sans);
      font-size: 0.8125rem;
      font-weight: 500;
      letter-spacing: 0;
      text-align: center;
      font-variant-numeric: tabular-nums;
    }

    /* The list is full: why the add button is disabled */
    .subs__limit {
      display: flex;
      align-items: center;
      gap: var(--space-2);
      margin-top: calc(-1 * var(--space-2));
      padding: var(--space-3) var(--space-4);
      border-radius: var(--radius-md);
      background: var(--color-accent-soft);
      color: var(--color-accent-strong);
      font-size: 0.9375rem;
      font-weight: 500;

      .icon {
        flex: none;
        width: 1.125rem;
        height: 1.125rem;
      }
    }

    .subs__grid {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(min(100%, 17rem), 1fr));
      gap: var(--space-4);
    }

    .subs__actions {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      gap: var(--space-2);
      margin-top: var(--space-4);
      padding-top: var(--space-5);
      border-top: 1px solid var(--color-border);

      @media (width < 40rem) {
        flex-direction: column;

        .btn {
          width: 100%;
        }
      }
    }

    .subs__backup {
      display: flex;
      flex-wrap: wrap;
      gap: var(--space-2);

      @media (width < 40rem) {
        flex-direction: column;
      }
    }
  }

  /* Pulse for the cards due this month (keyframes can't live in @scope) */
  @keyframes due-glow {
    from {
      box-shadow: 0 0 4px 0 var(--due-glow), var(--shadow-sm);
    }

    to {
      box-shadow: 0 0 14px 2px var(--due-glow), var(--shadow-sm);
    }
  }

  @scope (.sub-card) {
    :scope {
      --tint: var(--tint-lav);

      display: grid;
      align-content: start;
      gap: var(--space-4);
      padding: var(--space-5);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-md);
      background: var(--color-surface);
      box-shadow: var(--shadow-sm);
      transition:
        border-color var(--duration-fast) ease,
        box-shadow var(--duration) ease;

      &:hover {
        box-shadow: var(--shadow-md);
      }
    }

    /* Cycle the avatar tint (nth-of-type skips Alpine's <template>) */
    :scope:nth-of-type(6n + 2) {
      --tint: var(--tint-mint);
    }

    :scope:nth-of-type(6n + 3) {
      --tint: var(--tint-peach);
    }

    :scope:nth-of-type(6n + 4) {
      --tint: var(--tint-sky);
    }

    :scope:nth-of-type(6n + 5) {
      --tint: var(--tint-butter);
    }

    :scope:nth-of-type(6n + 6) {
      --tint: var(--tint-pink);
    }

    /* Expiring in the current month: an accent border, a slow glow and a
       badge */
    :scope.is-due {
      --due-glow: color-mix(in oklab, var(--color-accent), transparent 45%);

      border-color: var(--color-accent);
      box-shadow: 0 0 10px 1px var(--due-glow), var(--shadow-sm);
      animation: due-glow 2.4s ease-in-out infinite alternate;

      @media (prefers-reduced-motion: reduce) {
        animation: none;
      }
    }

    :scope.is-inactive {
      border-style: dashed;
      background: var(--color-surface-2);
      box-shadow: none;
      color: var(--color-text-muted);
      animation: none;
    }

    .sub-card__top {
      display: grid;
      grid-template-columns: 2.5rem minmax(0, 1fr) auto;
      align-items: center;
      gap: var(--space-3);
    }

    .sub-card__avatar {
      display: grid;
      place-items: center;
      width: 2.5rem;
      height: 2.5rem;
      border-radius: var(--radius-full);
      background: var(--tint);
      color: var(--color-text);
      font-family: var(--font-serif);
      font-size: 1.25rem;
      line-height: 1;
    }

    :scope.is-inactive .sub-card__avatar {
      filter: grayscale(1);
    }

    .sub-card__name {
      min-width: 0;

      h3 {
        font-size: 1.125rem;
        font-weight: 500;
        overflow-wrap: anywhere;
      }
    }

    .sub-card__recurrence {
      color: var(--color-text-muted);
      font-size: 0.8125rem;
    }

    /* Edit and delete, pulled into the card's corner padding */
    .sub-card__tools {
      display: flex;
      align-self: start;
      gap: var(--space-1);
      margin: calc(-1 * var(--space-2)) calc(-1 * var(--space-2)) 0 0;
    }

    .sub-card__price {
      font-family: var(--font-serif);
      font-size: 2rem;
      font-weight: 400;
      font-variation-settings: "opsz" 72;
      line-height: 1.1;
      letter-spacing: -0.04em;
      font-variant-numeric: lining-nums tabular-nums;
    }

    .sub-card__bottom {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      justify-content: space-between;
      gap: var(--space-2);
      padding-top: var(--space-4);
      border-top: 1px solid var(--color-border);
    }

    .sub-card__expires {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: var(--space-1) var(--space-2);
      color: var(--color-text-muted);
      font-size: 0.875rem;
      font-variant-numeric: tabular-nums;

      .icon {
        width: 1rem;
        height: 1rem;
      }
    }

    :scope.is-due .sub-card__expires {
      color: var(--color-accent-strong);
      font-weight: 500;
    }

    .sub-card__due {
      padding: 0.0625rem var(--space-2);
      border-radius: var(--radius-full);
      background: var(--color-accent-soft);
      color: var(--color-accent-strong);
      font-size: 0.6875rem;
      font-weight: 500;
      letter-spacing: 0.06em;
      text-transform: uppercase;
      white-space: nowrap;
    }

    /* Status badge, doubles as the active/inactive toggle */
    .status {
      --status-fg: var(--color-success);
      --status-bg: var(--color-success-soft);

      display: inline-flex;
      align-items: center;
      gap: var(--space-2);
      min-height: 1.875rem;
      padding: 0 var(--space-3);
      border: 1px solid transparent;
      border-radius: var(--radius-full);
      background: var(--status-bg);
      color: var(--status-fg);
      font-size: 0.8125rem;
      font-weight: 500;
      white-space: nowrap;
      cursor: pointer;
      transition:
        color var(--duration-fast) ease,
        background-color var(--duration-fast) ease,
        border-color var(--duration-fast) ease;

      &:hover {
        border-color: var(--status-fg);
      }
    }

    :scope.is-inactive .status {
      --status-fg: var(--color-text-muted);
      --status-bg: var(--color-surface);

      border-color: var(--color-border);
    }

    .status__dot {
      width: 0.4375rem;
      height: 0.4375rem;
      border-radius: var(--radius-full);
      background: currentColor;
    }

    :scope.is-inactive .status__dot {
      background: transparent;
      box-shadow: inset 0 0 0 1.5px currentColor;
    }
  }

  /* Add and edit dialog --------------------------------------------- */
  /* A modal over the home page with a blurred backdrop; on phones it   */
  /* fills the screen and reads as a page of its own. The dialog only   */
  /* places the panel (the visible card), so main.ts can animate the    */
  /* panel on its own: a WebGL peel (js/peel.ts) where html-in-canvas   */
  /* exists, else the CSS fade below.                                   */

  @scope (.sheet) {
    :scope {
      width: min(100% - 2 * var(--gutter), 44rem);
      max-width: none;
      max-height: none;
      margin: auto;
      padding: 0;
      border: 0;
      background: transparent;
      color: var(--color-text);
      overflow: visible;

      @media (width < 40rem) {
        width: 100%;
        height: 100%;
      }
    }

    .sheet__panel {
      max-height: calc(100dvh - 2 * var(--gutter));
      border: 1px solid var(--color-border);
      border-radius: var(--radius-md);
      background: var(--color-surface);
      box-shadow: var(--shadow-md);
      overflow: auto;
      overscroll-behavior: contain;

      @media (width < 40rem) {
        height: 100%;
        max-height: none;
        border: 0;
        border-radius: 0;
        box-shadow: none;
      }
    }

    /* The fallback without the peel: the panel slides up and settles
       with a small bounce (the easing overshoots), then slides back down.
       On phones it comes in from the bottom edge, like a page */
    :scope:not(.sheet--peel) .sheet__panel {
      --slide-from: 3rem;

      transition:
        opacity calc(var(--duration-slide-in) * 0.5) ease-out,
        translate var(--duration-slide-in) var(--ease-bounce);

      @starting-style {
        opacity: 0;
        translate: 0 var(--slide-from);
      }

      @media (width < 40rem) {
        --slide-from: 100%;
      }
    }

    :scope.is-closing:not(.sheet--peel) .sheet__panel {
      opacity: 0;
      translate: 0 var(--slide-from);
      transition:
        opacity var(--duration-slide-out) ease-in,
        translate var(--duration-slide-out) var(--ease-in);
    }

    /* The snapshot (laid out but not painted) and the WebGL output, which
       covers the viewport so the peeled sheet can leave the dialog */
    .peel-canvas {
      position: fixed;
      pointer-events: none;
    }

    .peel-canvas--output {
      inset: 0;
      width: 100%;
      height: 100%;
    }
  }

  /* Outside @scope: the backdrop is a pseudo-element of the dialog. It
     fades in and out whichever way the panel moves */
  .sheet::backdrop,
  .tour::backdrop,
  .transfer::backdrop {
    background: color-mix(in oklab, #22201d, transparent 70%);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    transition: opacity var(--duration) ease;

    @starting-style {
      opacity: 0;
    }
  }

  .sheet.is-closing::backdrop {
    opacity: 0;
  }

  /* The page behind doesn't scroll while a dialog is open */
  :root:has(.sheet[open], .tour[open], .transfer[open]) {
    overflow: hidden;
  }

  /* "How it works" ------------------------------------------------- */
  /* The link in the empty state, and the dialog playing the animation  */
  /* (js/tour.ts draws the stage).                                      */

  .tour-link {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    margin-top: var(--space-4);
    padding: var(--space-1) var(--space-2);
    border: 0;
    background: none;
    color: var(--color-accent-strong);
    font-weight: 500;
    text-decoration: underline;
    text-decoration-color: color-mix(in oklab, currentColor, transparent 50%);
    text-underline-offset: 0.2em;
    cursor: pointer;

    &:hover {
      text-decoration-color: currentColor;
    }

    .icon {
      width: 1rem;
      height: 1rem;
    }
  }

  @scope (.tour) {
    :scope {
      width: min(100% - 2 * var(--gutter), 46rem);
      max-width: none;
      max-height: none;
      margin: auto;
      padding: 0;
      border: 0;
      background: transparent;
      color: var(--color-text);
      overflow: visible;
    }

    .tour__panel {
      display: grid;
      gap: var(--space-4);
      max-height: calc(100dvh - 2 * var(--gutter));
      padding: var(--space-5);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-md);
      background: var(--color-surface);
      box-shadow: var(--shadow-md);
      overflow: auto;
      overscroll-behavior: contain;
      transition:
        opacity calc(var(--duration-slide-in) * 0.5) ease-out,
        translate var(--duration-slide-in) var(--ease-bounce);

      @starting-style {
        opacity: 0;
        translate: 0 3rem;
      }

      @media (width < 40rem) {
        padding: var(--space-4);
      }
    }

    .tour__header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: var(--space-3);

      h2 {
        font-size: 1.75rem;
      }
    }

    /* 16:9, also while the animation is still loading */
    .tour__stage {
      aspect-ratio: 16 / 9;
      border-radius: var(--radius-md);
      background: var(--color-bg);
      overflow: hidden;
    }

    .tour__status,
    .tour__caption {
      color: var(--color-text-muted);
      text-align: center;
      text-wrap: balance;
    }

    .tour__caption {
      min-height: 3em;
      color: var(--color-text);
      font-size: 1.0625rem;
    }

    .tour__controls {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: var(--space-3);
    }

    .tour__play {
      border-color: var(--color-border);
    }

    .tour__dots {
      display: flex;
      flex: 1;
      align-items: center;
      gap: var(--space-1);
    }

    /* A dot per scene, with a bigger hit area than it looks */
    .tour__dot {
      display: grid;
      place-items: center;
      width: 1.5rem;
      height: 1.5rem;
      padding: 0;
      border: 0;
      background: none;
      cursor: pointer;

      &::before {
        content: "";
        width: 0.5rem;
        height: 0.5rem;
        border-radius: var(--radius-full);
        background: var(--color-border);
        transition:
          width var(--duration) var(--ease-out),
          background-color var(--duration) ease;
      }

      &:hover::before {
        background: var(--color-field-border);
      }

      &[aria-current="true"]::before {
        width: 1.25rem;
        background: var(--color-accent);
      }
    }

    .tour__cta {
      @media (width < 40rem) {
        width: 100%;
      }
    }
  }

  /* Export and import ---------------------------------------------- */
  /* A dialog to pick a file or a QR code (partials/transfer.vto).      */

  @scope (.transfer) {
    :scope {
      width: min(100% - 2 * var(--gutter), 34rem);
      max-width: none;
      max-height: none;
      margin: auto;
      padding: 0;
      border: 0;
      background: transparent;
      color: var(--color-text);
      overflow: visible;
    }

    .transfer__panel {
      display: grid;
      gap: var(--space-4);
      max-height: calc(100dvh - 2 * var(--gutter));
      padding: var(--space-5);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-md);
      background: var(--color-surface);
      box-shadow: var(--shadow-md);
      overflow: auto;
      overscroll-behavior: contain;
      transition:
        opacity calc(var(--duration-slide-in) * 0.5) ease-out,
        translate var(--duration-slide-in) var(--ease-bounce);

      @starting-style {
        opacity: 0;
        translate: 0 3rem;
      }

      @media (width < 40rem) {
        padding: var(--space-4);
      }
    }

    .transfer__header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: var(--space-3);

      h2 {
        font-size: 1.75rem;
      }
    }

    .transfer__body {
      display: grid;
      gap: var(--space-4);
    }

    .transfer__intro,
    .transfer__note {
      color: var(--color-text-muted);
      text-wrap: pretty;
    }

    .transfer__note {
      font-size: 0.875rem;
    }

    .transfer__error {
      color: var(--color-danger);
      font-weight: 500;
    }

    .transfer__choices {
      display: grid;
      gap: var(--space-3);
    }

    /* An option card: icon on the left, title and description beside it */
    .choice {
      display: grid;
      grid-template-columns: auto 1fr;
      column-gap: var(--space-4);
      row-gap: var(--space-1);
      align-items: start;
      padding: var(--space-4);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-md);
      background: var(--color-surface);
      color: var(--color-text);
      text-align: start;
      font: inherit;
      transition:
        background-color var(--duration-fast) ease,
        border-color var(--duration-fast) ease;
    }

    button.choice {
      cursor: pointer;

      &:hover:not(:disabled) {
        border-color: var(--color-field-border);
        background: var(--color-surface-2);
      }

      &:disabled {
        cursor: progress;
      }
    }

    .choice__icon {
      display: grid;
      grid-row: span 2;
      place-items: center;
      width: 2.5rem;
      height: 2.5rem;
      border-radius: var(--radius-full);
      background: var(--tint-lav);

      .icon {
        width: 1.25rem;
        height: 1.25rem;
      }
    }

    .choice__title {
      font-weight: 600;
    }

    .choice__text {
      color: var(--color-text-muted);
      font-size: 0.9375rem;
    }

    .transfer__paste {
      display: grid;
      grid-column: 1 / -1;
      gap: var(--space-2);
      margin-top: var(--space-3);

      label {
        font-size: 0.875rem;
        font-weight: 500;
      }
    }

    .transfer__paste-row {
      display: flex;
      gap: var(--space-2);

      input {
        flex: 1;
        min-width: 0;
        min-height: 2.375rem;
        padding: var(--space-1) var(--space-3);
        border: 1px solid var(--color-field-border);
        border-radius: var(--radius-sm);
        background: var(--color-surface);

        &::placeholder {
          color: var(--color-text-muted);
          opacity: 1;
        }

        &:focus-visible {
          outline: none;
          border-color: var(--color-accent-strong);
          box-shadow: 0 0 0 3px var(--color-accent-soft);
        }
      }
    }

    /* The SVG brings its own white quiet zone */
    .transfer__qr {
      justify-self: center;
      width: min(100%, 20rem);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-md);
      overflow: hidden;

      svg {
        display: block;
        width: 100%;
        height: auto;
      }
    }

    .transfer__actions {
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-end;
      gap: var(--space-2);

      @media (width < 40rem) {
        flex-direction: column-reverse;

        .btn {
          width: 100%;
        }
      }
    }
  }

  @scope (.add) {
    :scope {
      display: grid;
    }

    /* Title with the close button in the corner; on phones the button is
       a back arrow before the title, like a page header */
    .add__header {
      display: grid;
      grid-template-columns: minmax(0, 1fr) auto;
      align-items: start;
      gap: var(--space-3);
      padding: var(--space-6) var(--space-5) var(--space-5);
      border-bottom: 1px solid var(--color-border);

      @media (width < 40rem) {
        grid-template-columns: auto minmax(0, 1fr);
        padding-top: var(--space-4);
      }
    }

    h1 {
      font-size: clamp(1.75rem, 1.4rem + 1.5vw, 2.25rem);
    }

    .add__close {
      margin: calc(-1 * var(--space-2)) calc(-1 * var(--space-2)) 0 0;

      /* Both icons share the same cell, only one is visible */
      > span {
        display: grid;
        grid-area: 1 / 1;
      }

      @media (width < 40rem) {
        order: -1;
        margin: 0 0 0 calc(-1 * var(--space-2));
      }
    }

    .add__close-back {
      visibility: hidden;

      @media (width < 40rem) {
        visibility: visible;
      }
    }

    .add__close-x {
      @media (width < 40rem) {
        visibility: hidden;
      }
    }

    .add__intro {
      grid-column: 1 / -1;
      color: var(--color-text-muted);
      font-size: 1.0625rem;

      strong {
        color: var(--color-text);
        font-weight: 600;
      }
    }
  }

  @scope (.form) {
    :scope {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
      gap: var(--space-4);
      padding: var(--space-5);
    }

    .field {
      display: grid;
      gap: var(--space-2);
      align-content: start;
    }

    .field--full,
    .form__actions {
      grid-column: 1 / -1;
    }

    label {
      font-size: 0.875rem;
      font-weight: 500;
    }

    input:not([type="checkbox"]),
    select {
      width: 100%;
      min-height: 2.75rem;
      padding: var(--space-2) var(--space-3);
      border: 1px solid var(--color-field-border);
      border-radius: var(--radius-sm);
      background: var(--color-surface);
      font-variant-numeric: tabular-nums;
      transition:
        border-color var(--duration-fast) ease,
        box-shadow var(--duration-fast) ease;

      &::placeholder {
        color: var(--color-text-muted);
        opacity: 1;
      }

      &:hover:not(:disabled) {
        border-color: var(--color-text-muted);
      }

      &:focus-visible {
        outline: none;
        border-color: var(--color-accent-strong);
        box-shadow: 0 0 0 3px var(--color-accent-soft);
      }

      &:disabled {
        background: var(--color-surface-2);
        cursor: not-allowed;
      }

      /* Only flag invalid fields after the user has touched them */
      &:user-invalid {
        border-color: var(--color-danger);
      }
    }

    select {
      appearance: none;
      padding-right: var(--space-6);
      background-image:
        linear-gradient(45deg, transparent 50%, currentColor 50%),
        linear-gradient(135deg, currentColor 50%, transparent 50%);
      background-position:
        right 1.1rem center,
        right 0.8rem center;
      background-size: 0.3rem 0.3rem;
      background-repeat: no-repeat;
    }

    .field--switch {
      display: flex;
      align-items: center;
      gap: var(--space-3);

      label {
        font-size: 0.9375rem;
        font-weight: 500;
        cursor: pointer;
      }
    }

    .switch {
      appearance: none;
      position: relative;
      flex: none;
      width: 2.5rem;
      height: 1.5rem;
      border: 1px solid var(--color-field-border);
      border-radius: var(--radius-full);
      background: var(--color-surface-2);
      cursor: pointer;
      transition:
        background-color var(--duration) ease,
        border-color var(--duration) ease;

      &::before {
        content: "";
        position: absolute;
        inset: 3px auto 3px 3px;
        aspect-ratio: 1;
        border-radius: var(--radius-full);
        background: var(--color-text-muted);
        transition:
          translate var(--duration) var(--ease-out),
          background-color var(--duration) ease;
      }

      &:checked {
        border-color: var(--color-accent-strong);
        background: var(--color-accent);

        &::before {
          translate: 1rem 0;
          background: var(--color-on-accent);
        }
      }
    }

    /* Submit last on the right, cancel next to it */
    .form__actions {
      display: flex;
      flex-direction: row-reverse;
      flex-wrap: wrap;
      gap: var(--space-2);
      margin: var(--space-2) calc(-1 * var(--space-5)) calc(-1 *
        var(--space-5));
      padding: var(--space-4) var(--space-5);
      border-top: 1px solid var(--color-border);
      background: var(--color-surface-2);
      /* Always in reach while the dialog scrolls */
      position: sticky;
      bottom: 0;

      @media (width < 40rem) {
        flex-direction: column;

        .btn {
          width: 100%;
        }
      }
    }
  }

  /* Banners: install and new version ------------------------------- */
  /* Floating cards stacked bottom-right on wide screens, full width on   */
  /* phones.                                                              */

  .banners {
    position: fixed;
    inset-inline: var(--gutter);
    bottom: calc(var(--gutter) + env(safe-area-inset-bottom, 0px));
    z-index: 20;
    display: grid;
    justify-items: end;
    gap: var(--space-3);
    view-transition-name: banners;
    /* Only the cards take clicks, not the gaps around them */
    pointer-events: none;
  }

  @scope (.banner) {
    :scope {
      display: grid;
      grid-template-columns: auto 1fr;
      gap: var(--space-3) var(--space-3);
      width: min(100%, 26rem);
      padding: var(--space-5);
      pointer-events: auto;
      border: 1px solid var(--color-border);
      border-radius: var(--radius-md);
      background: var(--color-surface);
      box-shadow: var(--shadow-md);
      transition:
        opacity var(--duration) ease,
        translate var(--duration) var(--ease-out);

      @starting-style {
        opacity: 0;
        translate: 0 0.5rem;
      }
    }

    .banner__logo {
      width: 2.5rem;
      height: 2.5rem;
    }

    .banner__body {
      display: grid;
      gap: var(--space-1);
    }

    .banner__title {
      font-family: var(--font-serif);
      font-size: 1.375rem;
      font-weight: 400;
      letter-spacing: -0.03em;
    }

    .banner__text {
      color: var(--color-text-muted);
      font-size: 0.875rem;
    }

    /* Mirrors the iOS Share button so it is easy to spot */
    .banner__share {
      display: inline-grid;
      place-items: center;
      width: 1.5rem;
      height: 1.5rem;
      margin-inline: 0.1em;
      border-radius: var(--radius-full);
      background: var(--tint-sky);
      color: var(--color-text);
      vertical-align: middle;

      .icon {
        width: 1rem;
        height: 1rem;
      }
    }

    .banner__actions {
      grid-column: 1 / -1;
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-end;
      gap: var(--space-2);

      @media (width < 40rem) {
        .btn {
          flex: 1;
        }
      }
    }
  }

  /* Entry fade --------------------------------------------------------- */
  /* Alpine's x-if inserts these sections: @starting-style gives them a  */
  /* from-state, so they fade in without any JS.                        */

  .empty,
  .stats,
  .subs {
    transition: opacity var(--duration) ease;

    @starting-style {
      opacity: 0;
    }
  }
}

/* ------------------------------------------------------------------ */
/* View transitions: page-to-page and theme switch                     */
/* ------------------------------------------------------------------ */

@view-transition {
  navigation: auto;
  types: page;
}

/* Theme switches use the default root cross-fade; page navigations
   cross-fade the content a bit faster */
:root:active-view-transition-type(page)::view-transition-old(content),
:root:active-view-transition-type(page)::view-transition-new(content) {
  animation-duration: 150ms;
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none !important;
  }
}
