@layer tokens, base, ui, shell, surface;
@layer ui {

  /* ---- the Geist mark (ui/mark.js), from the original mark.css ---- */
  /* ============================================================================
     The Geist mark's states (docs/archive/design-language.md §6.3), for js/ui/mark/geistMark.js.
     The origin is the core: every group transforms around (0,0) of the -800..800 viewBox.
     The 8 large petals carry motion; the 8 small blades (.gm-slot) carry meaning.
     ========================================================================== */

  .gm {
    --gm-ink: currentColor;
    /* A theme's logo colours (themes.css), falling back to the ink and act around the mark. */
    --gm-act: var(--logo-act, var(--color-accent-fill));
    --gm-measure: var(--color-measure);
    --gm-danger: var(--color-danger);
    --gm-empty: color-mix(in srgb, var(--color-ink) 18%, var(--color-surface));
    display: inline-block;
    flex: none;
    overflow: visible;
    vertical-align: middle;
    color: var(--logo-ink, var(--color-ink));
  }

  .gm use,
  .gm g,
  .gm circle {
    transform-box: view-box;
    transform-origin: 0 0;
  }

  .gm use {
    fill: var(--gm-ink);
    transition: fill var(--d) var(--ease), opacity var(--d) var(--ease);
  }

  /* The centre: the platform may colour it (the extension shows the browser mode there). */
  .gm .gm-core {
    fill: var(--mark-core, var(--gm-ink));
  }

  .gm-ripple {
    fill: none;
    stroke: var(--gm-act);
    stroke-width: 22;
    opacity: 0;
  }

  /* ---- slot signals (data-slots) -------------------------------------------- */

  .gm .gm-slot[data-signal="running"] {
    fill: var(--gm-act);
    animation: gm-led 1.2s ease-in-out infinite;
  }

  .gm .gm-slot[data-signal="needs"] {
    fill: var(--gm-measure);
    animation: gm-led 2s ease-in-out infinite;
  }

  .gm .gm-slot[data-signal="failed"] {
    fill: var(--gm-danger);
  }

  .gm .gm-slot[data-signal="done"] {
    fill: var(--gm-ink);
  }

  .gm .gm-slot[data-signal="empty"] {
    fill: var(--gm-empty);
  }

  /* The signal mark: small blades in act at rest (empty states beside a plate). */
  .gm--signal .gm-slot:not([data-signal]) {
    fill: var(--gm-act);
  }

  /* ---- idle: Breathe ---------------------------------------------------------- */

  .gm[data-mark-state="idle"] .gm-all {
    animation: gm-breathe 3.4s ease-in-out infinite;
  }

  /* ---- sending: Draw (the outline drafted blade by blade, filled, cleared) ------- */

  .gm[data-mark-state="sending"] use {
    stroke: currentColor;
    /* A 0.5 px hairline on screen (0.8 read heavy on the small blades): non-scaling-stroke is lost
       on <use> clones, so the width is given in the blades' units (viewBox 1600, blade transform
       ×0.1) for the mark's size. */
    stroke-width: calc(8000px / var(--gm-size, 20));
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    fill-opacity: 0;
    animation: gm-draw 3.6s cubic-bezier(0.6, 0, 0.3, 1) infinite;
    animation-delay: calc(var(--i, 0) / 16 * 0.5s);
  }

  /* ---- planning: Bloom (all blades out in unison, core draws in) ---------------- */

  .gm[data-mark-state="planning"] .gm-petal,
  .gm[data-mark-state="planning"] .gm-slot {
    animation: gm-bloom 2.2s cubic-bezier(0.45, 0, 0.55, 1) infinite;
  }

  .gm[data-mark-state="planning"] .gm-core {
    animation: gm-bloom-core 2.2s cubic-bezier(0.45, 0, 0.55, 1) infinite;
  }

  /* ---- running: Chase (brightness runs clockwise round all 16 blades) -----------
     Every blade takes part so the loop still reads at 16 px, where the small blades
     alone are a couple of pixels. */

  .gm[data-mark-state="running"] .gm-petal,
  .gm[data-mark-state="running"] .gm-slot:not([data-signal]) {
    animation: gm-chase 1.3s linear infinite;
    animation-delay: calc(var(--i) / 16 * 1.3s - 1.3s);
  }

  .gm[data-mark-state="running"] .gm-slot:not([data-signal]) {
    fill: var(--gm-act);
  }

  /* ---- delegating: three small blades fly out, grow ×1.9, return --------------- */

  .gm[data-mark-state="delegating"] .gm-slot-1,
  .gm[data-mark-state="delegating"] .gm-slot-4,
  .gm[data-mark-state="delegating"] .gm-slot-7 {
    animation: gm-dispatch 3.2s cubic-bezier(0.5, 0, 0.2, 1) infinite;
  }

  .gm[data-mark-state="delegating"] .gm-slot-1:not([data-signal]),
  .gm[data-mark-state="delegating"] .gm-slot-4:not([data-signal]),
  .gm[data-mark-state="delegating"] .gm-slot-7:not([data-signal]) {
    fill: var(--gm-act);
  }

  .gm[data-mark-state="delegating"] .gm-slot-4 {
    animation-delay: -0.12s;
  }

  .gm[data-mark-state="delegating"] .gm-slot-7 {
    animation-delay: -0.24s;
  }

  .gm[data-mark-state="delegating"] .gm-petal {
    animation: gm-hold 3.2s ease-in-out infinite;
  }

  .gm[data-mark-state="delegating"] .gm-core {
    animation: gm-merge 3.2s ease-out infinite;
  }

  /* ---- writing: Trace (act travels round the small blades only) ----------------- */

  .gm[data-mark-state="writing"] .gm-slot:not([data-signal]) {
    fill: var(--gm-empty);
    animation: gm-trace 1.2s linear infinite;
    animation-delay: calc(var(--k) / 8 * 1.2s - 1.2s);
  }

  /* ---- needs: still; the waiting slot pulses amber ------------------------------ */

  .gm[data-mark-state="needs"]:not([data-slots]) .gm-slot {
    fill: var(--gm-measure);
    animation: gm-led 2s ease-in-out infinite;
  }

  /* ---- listening: small blades act, core swells with --voice-level, ripples ----- */

  .gm[data-mark-state="listening"] .gm-petal {
    opacity: 0.45;
  }

  .gm[data-mark-state="listening"] .gm-slot {
    fill: var(--gm-act);
  }

  .gm[data-mark-state="listening"] .gm-core {
    transform: scale(calc(1 + var(--voice-level, 0) * 0.3));
    transition: transform 90ms linear;
  }

  .gm[data-mark-state="listening"] .gm-ripple {
    animation: gm-ripple 1.8s ease-out infinite;
  }

  .gm[data-mark-state="listening"] .gm-ripple--2 {
    animation-delay: -0.9s;
  }

  /* ---- done: Settle once ----------------------------------------------------------- */

  .gm[data-mark-state="done"] .gm-core {
    animation: gm-settle 1.2s cubic-bezier(0.3, 1.5, 0.5, 1) 1;
  }

  .gm[data-mark-state="done"] .gm-petal,
  .gm[data-mark-state="done"] .gm-slot {
    animation: gm-snap 0.6s ease-out 1;
  }

  /* ---- failed / stopped / offline: still ------------------------------------------ */

  .gm[data-mark-state="failed"]:not([data-slots]) .gm-slot {
    fill: var(--gm-danger);
  }

  .gm[data-mark-state="stopped"] {
    opacity: 0.55;
  }

  .gm[data-mark-state="stopped"] .gm-slot:not([data-signal]) {
    fill: var(--gm-empty);
  }

  .gm[data-mark-state="offline"] {
    opacity: 0.4;
  }

  .gm[data-mark-state="offline"] .gm-slot {
    fill: none;
    stroke: currentColor;
    stroke-width: 1px;
    vector-effect: non-scaling-stroke;
  }

  /* ---- keyframes -------------------------------------------------------------------- */

  @keyframes gm-breathe {
    0%,
    100% {
      transform: scale(0.94);
      opacity: 0.72;
    }
    50% {
      transform: scale(1);
      opacity: 1;
    }
  }

  /* The outline hands over to the fill: a stroke left on the filled blades fattened them and
     closed the gaps between them, so the drawn mark wasn't quite the logo. */
  @keyframes gm-draw {
    0% {
      stroke-dashoffset: 1;
      stroke-opacity: 1;
      fill-opacity: 0;
      opacity: 1;
    }
    40% {
      stroke-dashoffset: 0;
      stroke-opacity: 1;
      fill-opacity: 0;
    }
    55%,
    82% {
      stroke-dashoffset: 0;
      stroke-opacity: 0;
      fill-opacity: 1;
      opacity: 1;
    }
    100% {
      stroke-dashoffset: 0;
      stroke-opacity: 0;
      fill-opacity: 1;
      opacity: 0;
    }
  }

  @keyframes gm-bloom {
    0%,
    100% {
      transform: translate(0, 0);
    }
    50% {
      transform: translate(calc(var(--ux) * 208px), calc(var(--uy) * 208px));
    }
  }

  @keyframes gm-bloom-core {
    0%,
    100% {
      transform: scale(1);
    }
    50% {
      transform: scale(0.78);
    }
  }

  @keyframes gm-chase {
    0% {
      opacity: 1;
    }
    55%,
    100% {
      opacity: 0.16;
    }
  }

  /* Out 264 units along the blade's radius and ×1.9 around its own centroid (r ≈ 668). */
  @keyframes gm-dispatch {
    0%,
    8%,
    84%,
    100% {
      transform: translate(0px, 0px) scale(1) translate(0px, 0px);
    }
    30%,
    62% {
      transform: translate(calc(var(--ux) * 932px), calc(var(--uy) * 932px)) scale(1.9)
        translate(calc(var(--ux) * -668px), calc(var(--uy) * -668px));
    }
  }

  @keyframes gm-hold {
    0%,
    8%,
    84%,
    100% {
      opacity: 1;
    }
    30%,
    62% {
      opacity: 0.35;
    }
  }

  @keyframes gm-merge {
    0%,
    84% {
      transform: scale(1);
    }
    90% {
      transform: scale(1.18);
    }
    100% {
      transform: scale(1);
    }
  }

  @keyframes gm-trace {
    0% {
      fill: var(--gm-act);
    }
    40%,
    100% {
      fill: var(--gm-empty);
    }
  }

  @keyframes gm-led {
    0%,
    100% {
      opacity: 1;
    }
    50% {
      opacity: 0.35;
    }
  }

  @keyframes gm-ripple {
    0% {
      transform: scale(1);
      opacity: 0.7;
    }
    100% {
      transform: scale(4.2);
      opacity: 0;
    }
  }

  @keyframes gm-settle {
    0% {
      transform: scale(0.7);
    }
    60% {
      transform: scale(1.15);
    }
    100% {
      transform: scale(1);
    }
  }

  @keyframes gm-snap {
    0% {
      transform: translate(calc(var(--ux) * 48px), calc(var(--uy) * 48px));
      opacity: 0.4;
    }
    100% {
      transform: none;
      opacity: 1;
    }
  }

  /* Reduced motion: no loops; colour still carries the state. */
  @media (prefers-reduced-motion: reduce) {
    .gm *,
    .gm .gm-slot[data-signal] {
      animation: none !important;
      transition: none !important;
    }

    .gm[data-mark-state="running"] .gm-slot:not([data-signal]),
    .gm[data-mark-state="delegating"] .gm-slot:not([data-signal]),
    .gm[data-mark-state="writing"] .gm-slot:not([data-signal]),
    .gm[data-mark-state="listening"] .gm-slot:not([data-signal]) {
      fill: var(--gm-act);
      opacity: 1;
    }

    .gm[data-mark-state="sending"] use {
      stroke-dasharray: none;
      fill-opacity: 1;
    }
  }


  /* The sprite the marks draw from, out of the flow. */
  .gm-sprite {
    position: absolute;
  }
}
