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

@layer pages {
  /*
   * The run list's summary: one card for each state a run can be in, with how
   * many runs are in it now and, as bars, how many of each of the last seven
   * days' runs are. Each card is a link to the list filtered to that state.
   */
  .stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(12.5rem, 1fr));
    gap: var(--space-4);
  }

  .stat {
    --tone: var(--lamp-cancelled);
    --tone-wash: var(--wash-cancelled);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    background: var(--surface);
    text-decoration: none;
    box-shadow: var(--shadow);
    transition: border-color 100ms ease, box-shadow 100ms ease;
  }

  .stat:hover,
  .stat[aria-current="true"] {
    border-color: color-mix(in srgb, var(--tone) 45%, var(--surface));
  }

  .stat[aria-current="true"] {
    box-shadow: 0 0 0 3px var(--tone-wash);
  }

  .stat--completed { --tone: var(--lamp-completed); --tone-wash: var(--wash-completed); }
  .stat--active { --tone: var(--lamp-running); --tone-wash: var(--wash-running); }
  .stat--waiting { --tone: var(--lamp-waiting); --tone-wash: var(--wash-waiting); }
  .stat--failed { --tone: var(--lamp-failed); --tone-wash: var(--wash-failed); }

  .stat__icon {
    flex: none;
    display: grid;
    place-items: center;
    width: 2.75rem;
    height: 2.75rem;
    border-radius: 50%;
    background: var(--tone-wash);
    color: var(--tone);
  }

  .stat__icon .icon {
    width: 1.3rem;
    height: 1.3rem;
    stroke-width: 2.4;
  }

  .stat__figure {
    display: grid;
    margin-right: auto;
    line-height: 1.2;
  }

  .stat__count {
    font-size: var(--step-2);
    font-weight: 750;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
  }

  .stat__label {
    color: var(--ink-soft);
    font-size: var(--step--1);
  }

  .stat__bars {
    flex: none;
    width: 4rem;
    height: 2rem;
    fill: var(--tone);
  }

  .stat__bars rect {
    opacity: 0.35;
  }

  .stat__bars rect:last-of-type {
    opacity: 1;
  }

  /*
   * A run's page: its head (what it is and its state), then two columns: the
   * objective and the activity on the left, and cards about the run on the
   * right. On a narrow window the right column follows the left.
   */
  .run-head__name {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2) var(--space-3);
  }

  .run-head__icon {
    flex: none;
    display: grid;
    place-items: center;
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 10px;
    background: var(--wash-completed);
    color: var(--lamp-completed);
  }

  .run-head__icon .icon {
    width: 1.5rem;
    height: 1.5rem;
  }

  .run-head__state {
    display: inline-flex;
    flex-wrap: wrap;
    gap: var(--space-2);
  }

  .run-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr);
    gap: var(--space-4);
    align-items: start;
  }

  .run-grid--even {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .run-grid__main,
  .run-grid__side {
    display: grid;
    gap: var(--space-4);
    min-width: 0;
  }

  .objective {
    line-height: 1.55;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }

  /* --- Run status card ----------------------------------------------------- */

  .status {
    --tone: var(--lamp-cancelled);
  }

  .status--queued { --tone: var(--lamp-queued); }
  .status--running { --tone: var(--lamp-running); }
  .status--waiting { --tone: var(--lamp-waiting); }
  .status--completed { --tone: var(--lamp-completed); }
  .status--failed { --tone: var(--lamp-failed); }

  .status__icon {
    flex: none;
    display: grid;
    place-items: center;
    width: 1.6rem;
    height: 1.6rem;
    border-radius: 50%;
    background: var(--tone);
    color: #fff;
  }

  .status__icon .icon {
    width: 1rem;
    height: 1rem;
    stroke-width: 2.6;
  }

  .status--running .status__icon .icon {
    animation: lamp-turn 1s steps(8) infinite;
  }

  .status__steps {
    color: var(--ink-soft);
    font-size: var(--step--1);
    font-variant-numeric: tabular-nums;
  }

  .status__steps strong {
    color: var(--ink);
  }

  .status .gauge {
    height: 6px;
    margin-top: 0;
  }

  /* Progress in the state's colour, until it nears the limit and warns. */
  .status .gauge:not(.gauge--high, .gauge--full) .gauge__fill {
    background: var(--tone);
  }

  .figures {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .figure {
    display: grid;
    gap: var(--space-1);
    align-content: start;
    padding: 0 var(--space-3);
    border-left: 1px solid var(--rule-soft);
  }

  .figure:first-child {
    padding-left: 0;
    border-left: 0;
  }

  .figure dt {
    display: grid;
    gap: var(--space-1);
    color: var(--ink-soft);
    font-size: var(--step--2);
  }

  .figure dt .icon {
    width: 1.15rem;
    height: 1.15rem;
    color: var(--ink);
  }

  .figure dd {
    font-size: var(--step-0);
    font-weight: 750;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
  }

  /* --- Workspace card ------------------------------------------------------ */

  .changes {
    display: grid;
    gap: var(--space-2);
    font-size: var(--step--1);
  }

  .changes li {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: var(--space-2);
    align-items: baseline;
  }

  .changes .icon {
    align-self: center;
    color: var(--signal);
  }

  .preview__name {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    font-size: var(--step--1);
  }

  /* The file shown in the card is a glimpse: the whole of it is a click away. */
  .preview .sheet,
  .preview .well {
    max-height: 22rem;
    box-shadow: none;
  }

  .preview .grid-scroll {
    max-height: 22rem;
    box-shadow: none;
  }

  .preview .pdf-frame {
    height: 26rem;
    box-shadow: none;
  }

  .preview .sheet {
    padding: var(--space-4);
  }

  .preview .prose {
    font-size: var(--step--1);
  }

  .preview .prose h1 { font-size: var(--step-1); }
  .preview .prose h2 { font-size: var(--step-0); }
  .preview .prose h3 { font-size: var(--step--1); }

  .preview .prose > * + :is(h1, h2, h3, h4, h5, h6) {
    margin-top: var(--space-4);
  }

  /* --- The activity card ---------------------------------------------------- */

  /*
   * The feed: the trace in a window of its own. The feed controller gives the
   * window the height left on the page (--feed-room) and keeps the newest
   * entry in it; without the controller it is a tall scrolling box.
   */
  .feed {
    gap: var(--space-3);
  }

  .feed__window {
    position: relative;
    min-width: 0;
  }

  .feed__scroll {
    position: relative;
    display: grid;
    gap: var(--space-4);
    max-height: var(--feed-room, 70vh);
    margin: 0 calc(-1 * var(--space-2));
    padding: var(--space-1) var(--space-2);
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--rule-strong) transparent;
  }

  .feed__scroll:focus-visible {
    outline-offset: 0;
    border-radius: var(--radius-control);
  }

  /* Back to the newest entry, when the reader has scrolled away from it. */
  .feed__jump {
    position: absolute;
    bottom: var(--space-3);
    left: 50%;
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
    padding: 0.35rem 0.8rem 0.35rem 0.6rem;
    border: 0;
    border-radius: 999px;
    background: var(--ink);
    color: #fff;
    font: inherit;
    font-size: var(--step--1);
    font-weight: 650;
    translate: -50% 0;
    cursor: pointer;
    box-shadow: var(--shadow-raised);
  }

  .feed__jump:hover {
    background: var(--signal);
  }

  /* The reply box under the activity of a run that is over. */
  .feed__reply {
    padding-top: var(--space-3);
    border-top: 1px solid var(--rule-soft);
  }

  .feed__reply-row {
    display: flex;
    flex-wrap: wrap;
    align-items: start;
    gap: var(--space-2);
  }

  .feed__reply-row textarea {
    flex: 1 1 14rem;
    min-height: 2.5rem;
    field-sizing: content;
    max-height: 9rem;
  }

  /* "Live": the run is still going and this page is following it. */
  .live {
    display: inline-flex;
    align-items: center;
    gap: 0.45em;
    padding: 0.15rem 0.6rem;
    border-radius: 999px;
    background: var(--wash-completed);
    color: var(--lamp-completed);
    font-size: var(--step--2);
    font-weight: 700;
  }

  .live::before {
    content: "";
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: currentColor;
    animation: live-beat 1.6s ease-in-out infinite;
  }

  @keyframes live-beat {
    50% { opacity: 0.3; }
  }

  /* How the run ended, as the last thing in the feed. */
  .result {
    display: grid;
    gap: var(--space-3);
    padding: var(--space-4);
    border: 1px solid color-mix(in srgb, var(--lamp-completed) 25%, var(--surface));
    border-radius: 10px;
    background: color-mix(in srgb, var(--wash-completed) 45%, var(--surface));
  }

  .result .card-title > .icon {
    color: var(--lamp-completed);
    stroke-width: 2.6;
  }

  .result .well {
    border-color: var(--rule-soft);
    background: var(--surface);
  }

  .feed__scroll > .plate {
    box-shadow: none;
  }

  .plate--failed .card-title > .icon {
    color: var(--lamp-failed);
  }

  /*
   * The trace: a run's activity as a strip-chart. Time runs down the left, a
   * rail carries a dot for each event in the colour of how it went, and beside
   * the dot an icon says what kind of thing happened: the model was asked, a
   * tool ran, a file was written.
   */
  .trace {
    --rail: var(--rule);
    display: grid;
  }

  .trace__event {
    --mark: var(--ink);
    --chip: var(--panel);
    --chip-ink: var(--ink-soft);
    display: grid;
    grid-template-columns: 4.4rem 0.75rem 1.75rem minmax(0, 1fr) auto;
    column-gap: var(--space-3);
    padding: 0 var(--space-2);
    border-radius: var(--radius-control);
  }

  .trace__time {
    padding-top: 0.3rem;
    color: var(--ink-faint);
    text-align: right;
  }

  .trace__mark {
    position: relative;
    display: block;
  }

  .trace__mark::before {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: calc(50% - 1px);
    width: 2px;
    background: var(--rail);
  }

  .trace__event:first-child .trace__mark::before {
    top: 0.8rem;
  }

  .trace__event:last-child .trace__mark::before {
    bottom: calc(100% - 0.8rem);
  }

  .trace__mark::after {
    content: "";
    position: absolute;
    top: 0.55rem;
    left: calc(50% - 0.25rem);
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: var(--mark);
    box-shadow: 0 0 0 3px var(--surface);
  }

  .trace__chip {
    display: grid;
    place-items: center;
    width: 1.75rem;
    height: 1.75rem;
    border-radius: 7px;
    background: var(--chip);
    color: var(--chip-ink);
  }

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

  .trace__event--ok { --mark: var(--lamp-completed); --chip: var(--wash-completed); --chip-ink: var(--lamp-completed); }
  .trace__event--live { --mark: var(--lamp-running); --chip: var(--wash-running); --chip-ink: var(--lamp-running); }
  .trace__event--queued { --mark: var(--lamp-queued); --chip: var(--wash-queued); --chip-ink: var(--lamp-queued); }
  .trace__event--warn { --mark: var(--lamp-waiting); --chip: var(--wash-waiting); --chip-ink: var(--lamp-waiting); }
  .trace__event--fail { --mark: var(--lamp-failed); --chip: var(--wash-failed); --chip-ink: var(--lamp-failed); }
  .trace__event--off { --mark: var(--lamp-cancelled); }

  /* A file the run wrote stands out from the talk around it. */
  .trace__event--file {
    --mark: var(--signal);
    --chip: var(--surface);
    --chip-ink: var(--signal);
    background: var(--signal-wash);
  }

  .trace__event--file .trace__mark::after {
    box-shadow: 0 0 0 3px var(--signal-wash);
  }

  .trace__body {
    display: grid;
    gap: var(--space-1);
    min-width: 0;
    padding: 0.2rem 0 var(--space-3);
  }

  .trace__title {
    font-size: var(--step--1);
    font-weight: 650;
    overflow-wrap: anywhere;
  }

  .trace__title code {
    padding: 0.05rem 0.35rem;
    border-radius: 5px;
    background: var(--panel-deep);
    font-weight: 500;
  }

  .trace__detail {
    color: var(--ink-soft);
    font-size: var(--step--2);
    overflow-wrap: anywhere;
  }

  .trace__detail--fail {
    color: var(--lamp-failed);
  }

  .trace__quote {
    padding-left: var(--space-3);
    border-left: 2px solid var(--rule);
    color: var(--ink-soft);
    font-size: var(--step--1);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }

  /* Where a file change leads: the run's workspace tab. */
  .trace__where {
    align-self: start;
    display: inline-flex;
    align-items: center;
    gap: 0.35em;
    margin-top: 0.3rem;
    color: var(--ink-soft);
    font-size: var(--step--2);
    text-decoration: none;
    white-space: nowrap;
  }

  .trace__where:hover {
    color: var(--signal-deep);
  }

  /* New events arriving over the wire settle in rather than pop. */
  .trace__event.is-new {
    animation: trace-arrive 360ms ease-out;
  }

  @keyframes trace-arrive {
    from { opacity: 0; translate: 0 6px; }
    to { opacity: 1; translate: 0 0; }
  }

  /* --- The transcript (Messages tab) ---------------------------------------- */

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

  .transcript__message {
    --chip: var(--panel);
    --chip-ink: var(--ink-soft);
    display: grid;
    grid-template-columns: 2rem minmax(0, 1fr);
    gap: var(--space-3);
    padding: var(--space-4);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
  }

  .transcript__message--user { --chip: var(--signal-wash); --chip-ink: var(--signal-deep); }
  .transcript__message--model { --chip: var(--wash-completed); --chip-ink: var(--lamp-completed); }

  .transcript__chip {
    display: grid;
    place-items: center;
    width: 2rem;
    height: 2rem;
    border-radius: 8px;
    background: var(--chip);
    color: var(--chip-ink);
  }

  .transcript__body {
    display: grid;
    gap: var(--space-2);
    min-width: 0;
  }

  .transcript__who {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: var(--space-2);
    font-size: var(--step--1);
  }

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

  .transcript__call p {
    display: flex;
    align-items: center;
    gap: 0.4em;
    font-size: var(--step--1);
    font-weight: 600;
  }

  /* Parent and child runs */
  .tree {
    display: grid;
    gap: var(--space-2);
  }

  .tree .tree {
    margin-left: 0.4rem;
    padding-left: var(--space-4);
    border-left: 2px solid var(--rule);
  }

  .tree__node {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: var(--space-1) var(--space-3);
  }

  .tree__node[aria-current="true"] {
    font-weight: 650;
  }

  .tree__node[aria-current="true"] .tree__note {
    font-weight: 400;
  }

  .tree__note {
    flex-basis: 100%;
    font-size: var(--step--1);
  }

  .tree__state {
    color: var(--ink-soft);
    font-size: var(--step--1);
    font-weight: 400;
  }

  .tree__state--attention {
    padding: 0 0.4rem;
    border-radius: 5px;
    background: var(--wash-waiting);
    color: var(--ink);
    font-weight: 600;
  }

  /* A delegated run in the run list, set in under the run that started it. */
  .table__child td {
    background: var(--well);
  }

  .table__nest[data-depth="1"],
  .table__nest[data-depth="2"],
  .table__nest[data-depth="3"] {
    padding-left: var(--space-3);
    border-left: 2px solid var(--rule);
  }

  .table__nest[data-depth="1"] { margin-left: var(--space-2); }
  .table__nest[data-depth="2"] { margin-left: calc(var(--space-2) + var(--space-5)); }
  .table__nest[data-depth="3"] { margin-left: calc(var(--space-2) + 2 * var(--space-5)); }


  @media (max-width: 64rem) {
    .run-grid,
    .run-grid--even {
      grid-template-columns: minmax(0, 1fr);
    }

    /* In one column the cards of both sides are laid out as one list, so
       that the status and the objective can lead it, ahead of the activity. */
    .run-grid__main,
    .run-grid__side {
      display: contents;
    }

    .run-grid__side > .status {
      order: -2;
    }

    .run-objective {
      order: -1;
    }
  }

  @media (max-width: 40rem) {
    .figures {
      grid-template-columns: repeat(2, minmax(0, 1fr));
      row-gap: var(--space-3);
    }

    .figure:nth-child(odd) {
      padding-left: 0;
      border-left: 0;
    }

    /* The time goes above the event's words, and the rail is left out. */
    .trace__event {
      grid-template-columns: 1.75rem minmax(0, 1fr) auto;
    }

    .trace__mark {
      display: none;
    }

    .trace__chip {
      grid-column: 1;
      grid-row: 1 / span 2;
      margin-top: 0.3rem;
    }

    .trace__time {
      grid-column: 2;
      grid-row: 1;
      text-align: left;
    }

    .trace__body {
      grid-column: 2;
      grid-row: 2;
      padding-top: 0;
    }

    .trace__where {
      grid-column: 3;
      grid-row: 1;
    }
  }
}
