/*
 * The price chart (frames/companies/chart, the stock-chart controller): its
 * range and interval switches, its layers and the canvas Highcharts draws on.
 * The alert drawer reaches it by this class too.
 */
@layer components {
  /* Highcharts sizes a chart from its container when it is given no height,
     and the `stock-chart` controller draws into a div of its own inside the
     canvas, which has to fill it. The interval menu sits above the canvas
     inside the same height, so the placeholder's height still holds. */
  .company-chart {
    container: company-chart / inline-size;
    position: relative;
    display: flex;
    flex-direction: column;
    block-size: var(--company-chart-height);
  }

  /* Two rows over the chart: the candle widths on the left and the spans on
     the right, every one a button, wrapping under each other where the
     chart is too narrow for both; then, under a hairline, what the chart
     flags. The buttons sit tighter than a segmented control's own gap, so
     eleven of them fit a phone in two lines. */
  .company-chart__head {
    display: flex;
    flex: none;
    flex-wrap: wrap;
    align-items: center;
    gap: 0 var(--space-md);

    & .segmented {
      gap: 0 var(--space-sm);
    }
  }

  /* At the row's end, and at the end of a line of its own where the row
     wraps. On a phone, first after the candle widths (and the style), before
     the dates and the event toggles: in the page's column they share the
     candle widths' line, at its end; over the whole window they take a line
     of their own, from the left as every other line does. */
  .company-chart__ranges {
    justify-content: flex-end;
    margin-inline-start: auto;

    @media (width < 768px) {
      order: 1;

      .company-chart--full &,
      html[data-chart-opening] & {
        justify-content: flex-start;
        margin-inline-start: 0;
      }
    }
  }

  .company-chart__intervals {
    flex: none;
  }

  /* How the price is drawn — Candles, Bars, Line, Area — after the candle
     widths, set off from them by a hairline: over the whole window only.
     Through the head, so it outweighs `.segmented`'s own display. */
  .company-chart__head > .company-chart__styles {
    display: none;
    flex: none;
    padding-inline-start: var(--space-md);
    border-inline-start: var(--rule-hairline);
  }

  /* Show and the kinds of event, after the candle widths (and the style),
     set off by a hairline as the style is. */
  .company-chart__layers {
    display: flex;
    flex: none;
    flex-wrap: wrap;
    align-items: center;
    gap: 0 var(--space-sm);
    padding-inline-start: var(--space-md);
    border-inline-start: var(--rule-hairline);

    & .segmented {
      flex-wrap: wrap;
      gap: 0 var(--space-sm);
    }

    /* A phone has no room beside the candle widths: a line of its own, after
       the spans, with no hairline to set it off, and the "Show" label for a
       screen reader only (beside it the toggles are 2px too wide for a 375px
       phone). */
    @media (width < 768px) {
      flex: 1 1 100%;
      order: 3;
      padding-inline-start: 0;
      border-inline-start: 0;

      & > .caps-label {
        position: absolute;
        inline-size: 1px;
        block-size: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
      }
    }
  }

  /* The key, and the button that opens the full window at its end, under a
     hairline; over the whole window the tools row above it has the hairline. */
  .company-chart__keyline {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--space-xs) var(--space-md);
    margin-block-end: var(--space-xs);
    padding-block-start: var(--space-xs);
    border-block-start: var(--rule-hairline);
  }

  /* On a phone, in the page's column, where the candle widths and the spans
     share the first line and the event toggles take the next: a hairline sets
     the rows off from the candles. The key stays above the candles, and the
     button that opens the full window goes under them, across the column, so
     the buttons over the chart are two lines and not four. */
  @media (width < 768px) {
    html:not([data-chart-opening]) .company-chart:not(.company-chart--full) {
      & .company-chart__head {
        border-block-end: var(--rule-hairline);
      }

      & .company-chart__keyline {
        display: contents;
      }

      & .company-chart__open {
        order: 1;
        margin-block-start: var(--space-xs);
        margin-inline-start: 0;

        & > .button {
          justify-content: center;
          inline-size: 100%;
        }
      }
    }
  }

  .company-chart__canvas {
    flex: 1;
    min-block-size: 0;

    & > div {
      block-size: 100%;
    }
  }

  /* The button that opens the chart over the whole window, at the end of the
     key's line (`chart_tools`, tasks/growth/milestones/37). */
  .company-chart__open {
    margin-inline-start: auto;
  }

  /* The chart over the whole window (the `chart-tools` controller): the same
     box, fixed over the page on the page's ground, above the masthead and
     every drawer, its height the window's: a modal within the site, not the
     browser's own full screen. The page under it does not scroll. The
     title row, with the close button, shows only here.

     A page opened at `#chart` (companies/keystats/show) is that from its first
     paint: the frame covers the window on the ground while it loads, and the
     chart in it takes the full form before `chart-tools` has opened it, so it
     is drawn at its full size once and does not jump. */
  html[data-chart-opening] {
    overflow: hidden;

    & #company_chart {
      position: fixed;
      inset: 0;
      z-index: 80;
      padding-block: max(var(--space-sm), env(safe-area-inset-top, 0px)) max(var(--space-sm), env(safe-area-inset-bottom, 0px));
      padding-inline: max(var(--gutter), env(safe-area-inset-left, 0px)) max(var(--gutter), env(safe-area-inset-right, 0px));
      background-color: var(--color-ground);

      /* Its placeholder (shared/_loading_box) fills the window, breathing,
         until the chart arrives, and says what is coming: a block that big
         and that pale would otherwise read as a blank page. The words are
         the "Draw on this chart" button's working label. */
      & > .skeleton {
        display: grid;
        place-items: center;
        block-size: 100%;

        &::after {
          content: "Opening the chart…";
          color: var(--color-ink-muted);
          font-family: var(--font-ui);
          font-size: var(--text-ui-sm);
          font-weight: var(--weight-semibold);
          letter-spacing: var(--tracking-caps);
          text-transform: uppercase;
        }
      }
    }
  }

  .company-chart--full,
  html[data-chart-opening] .company-chart {
    position: fixed;
    inset: 0;
    z-index: 80;
    block-size: auto;
    padding-block: max(var(--space-sm), env(safe-area-inset-top, 0px)) max(var(--space-sm), env(safe-area-inset-bottom, 0px));
    padding-inline: max(var(--gutter), env(safe-area-inset-left, 0px)) max(var(--gutter), env(safe-area-inset-right, 0px));
    background-color: var(--color-ground);

    & .company-chart__open {
      display: none;
    }

    & .company-chart__keyline {
      padding-block-start: 0;
      border-block-start: 0;
    }

    & .company-chart__title,
    & .company-chart__tools,
    & .company-chart__span,
    & .company-chart__styles {
      display: flex;
    }

    /* A phone on its side has the width and not the height: the event
       toggles give their row to the candles. */
    @media (orientation: landscape) and (width < 1024px) and (pointer: coarse) {
      & .company-chart__layers {
        display: none;
      }
    }
  }

  /* The dates the chart shows (frames/companies/_chart_span), before the
     range buttons: over the whole window only, two slim boxes joined by a
     dash, at the height of the row's buttons. On a phone
     the row wraps them onto a line of their own, under the ranges and from
     the left. */
  .company-chart__span {
    display: none;
    align-items: center;
    gap: var(--space-2xs);
    margin-inline-start: auto;

    @media (width < 768px) {
      order: 2;
      margin-inline-start: 0;
    }
  }

  /* The shared compact field (components/field.css), its caption for a
     screen reader only and its box slimmed to the row's buttons; 16px on
     touch, so iOS does not zoom into it. */
  .company-chart__date > .field__input {
    min-block-size: 1.75rem;
    padding-inline: var(--space-2xs);
    font-size: var(--text-ui-sm);

    @media (pointer: coarse) {
      min-block-size: var(--target-min);
      font-size: var(--text-input);
    }
  }

  .company-chart__dash {
    color: var(--color-ink-muted);
  }

  /* The tools row (frames/companies/_chart_tools): over the whole window
     only, under a hairline as the event toggles are. */
  .company-chart__tools {
    display: none;
    flex: none;
    flex-wrap: wrap;
    align-items: center;
    gap: 0 var(--space-md);
    border-block-start: var(--rule-hairline);
  }

  /* The drawing tools: a row of icons, as charting tools draw them, that
     scrolls sideways where the window is too narrow for it, as a phone's is.
     The armed one sits pressed on the sunken ground with an ink rule. */
  .company-chart__drawing {
    display: flex;
    align-items: center;
    gap: var(--space-3xs);
    max-inline-size: 100%;
    overflow-x: auto;
  }

  .company-chart__tool {
    display: inline-grid;
    flex: none;
    place-items: center;
    inline-size: 2rem;
    block-size: 2rem;
    color: var(--color-ink-soft);
    cursor: pointer;

    &:hover:not(:disabled) {
      color: var(--color-ink);
      background-color: var(--color-row-hover);
    }

    &[aria-pressed="true"] {
      color: var(--color-ink);
      background-color: var(--color-surface-sunken);
      box-shadow: inset 0 -2px 0 var(--color-ink);
    }

    &:disabled {
      color: var(--color-ink-subtle);
      cursor: default;
    }

    @media (pointer: coarse) {
      inline-size: var(--target-min);
      block-size: var(--target-min);
    }
  }

  .company-chart__divider {
    flex: none;
    inline-size: 1px;
    block-size: 1.25rem;
    margin-inline: var(--space-2xs);
    background-color: var(--color-rule);
  }

  /* The selected drawing's own delete, off its top right corner: the
     controller places it (`--x`, `--y`, from the chart's corner). */
  .company-chart__erase {
    position: absolute;
    inset-block-start: var(--y, 0);
    inset-inline-start: var(--x, 0);
    z-index: 5;
    display: grid;
    place-items: center;
    inline-size: 2rem;
    block-size: 2rem;
    border: var(--rule-width) solid var(--color-ink);
    background-color: var(--color-field);
    color: var(--color-ink);
    box-shadow: var(--shadow-overlay);
    cursor: pointer;

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

    @media (pointer: coarse) {
      inline-size: var(--target-min);
      block-size: var(--target-min);
    }
  }

  /* A note being written, where it goes: the box it is drawn in, at the
     point clicked (`--x`, `--y`, from the chart's corner). */
  .company-chart__note-editor {
    position: absolute;
    inset-block-start: var(--y, 0);
    inset-inline-start: var(--x, 0);
    z-index: 5;
    field-sizing: content;
    min-inline-size: 10rem;
    max-inline-size: 22rem;
    padding: var(--space-3xs) var(--space-2xs);
    border: var(--rule-width) solid var(--color-ink);
    border-radius: 0;
    background-color: var(--color-field);
    color: var(--color-ink);
    font-family: var(--font-data);
    font-size: 0.6875rem;
    box-shadow: var(--shadow-overlay);

    &:focus-visible {
      outline: var(--focus-ring);
      outline-offset: var(--focus-offset);
    }

    @media (pointer: coarse) {
      font-size: 1rem;
    }
  }

  .company-chart__level {
    gap: var(--space-3xs);
  }

  /* The tools row's menus — Indicators, Share — keep to its end, and open
     back across it rather than off the window's edge. On a phone, where the
     drawing tools fill a line, they take the next from the left, as the
     lines above do. */
  .company-chart__tools .indicator-menu {
    margin-inline-start: auto;

    @media (width < 768px) {
      margin-inline-start: 0;
    }
  }

  .company-chart__tools .disclosure--popover > .disclosure__body {
    inset-inline: auto 0;
  }

  /* On a phone a menu's button may sit mid-row: its menu opens from the
     row's end instead, at the row's width. */
  @media (width < 768px) {
    .company-chart__tools {
      position: relative;
    }

    .company-chart__tools .disclosure--popover {
      position: static;
    }
  }

  /* A menu's summary reads as the chart's other caps options: their size,
     weight, tracking and link colour, ink while its menu is open. Through
     the tools row and the disclosure, so it outweighs the popover
     summary's own type (components/disclosure.css). */
  .company-chart__tools .disclosure > .company-chart__menu {
    gap: var(--space-2xs);
    min-block-size: var(--target-min);
    color: var(--color-link);
    font-size: var(--text-caps);
    font-weight: var(--weight-medium);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;

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

  .company-chart__tools .disclosure[open] > .company-chart__menu {
    color: var(--color-ink);
  }

  /* The Share menu's link, notes and image buttons (frames/chart_shares). */
  .chart-share-menu > .disclosure__body {
    inline-size: min(24rem, calc(100vw - 2 * var(--gutter)));
  }

  .chart-share {
    display: grid;
    gap: var(--space-xs);
  }

  .chart-share__heading {
    margin-block-start: var(--space-xs);
  }

  .chart-share__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2xs) var(--space-md);
  }

  .chart-share__note {
    margin: 0;
    color: var(--color-ink-muted);
    font-size: var(--text-meta-xs);
    line-height: var(--leading-meta-xs);

    /* The image buttons' status line, until there is something to say. */
    &:empty {
      display: none;
    }
  }

  .chart-share__form {
    display: none;
  }

  /* The Save chart menu (frames/chart_saves): Save to notebook, across
     the menu, then the charts saved on the company. */
  .chart-saves {
    display: grid;
    gap: var(--space-sm);
  }

  .chart-saves__save {
    inline-size: 100%;
  }

  .chart-saves__list {
    display: grid;
    gap: var(--space-2xs);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  /* A saved chart: its link and date, and the bin at the row's end. */
  .chart-saves__item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    column-gap: var(--space-xs);
    font-size: var(--text-ui-sm);
    line-height: var(--leading-ui-sm);
  }

  .chart-saves__delete {
    grid-column: 2;
    grid-row: 1 / span 2;
    align-self: center;
  }

  /* What the chart draws besides the candles, each with its colour, its
     settings and its latest value: in the Overview's column as over the
     whole window. */
  .company-chart__key {
    display: flex;
    flex: none;
    flex-wrap: wrap;
    gap: var(--space-3xs) var(--space-md);
    padding-block: var(--space-3xs);
    color: var(--color-ink-muted);
    font-family: var(--font-data);
    font-size: var(--text-meta-xs);
    line-height: var(--leading-meta-xs);

    /* Nothing drawn, no key; but over the whole window, while the reader has
       indicators that are still to be drawn (`data-keyed`, set by
       `chart-tools`), it keeps its line, so the chart is not shortened when
       they arrive. With none, no empty line stands over the chart. */
    &:empty {
      display: none;

      .company-chart--full[data-keyed] &,
      html[data-chart-opening] .company-chart[data-keyed] & {
        display: block;
        min-block-size: calc(1lh + 2 * var(--space-3xs));
      }
    }

    & .figure {
      color: var(--color-ink);
      font-weight: var(--weight-semibold);
    }
  }

  .company-chart__key-item {
    display: inline-flex;
    align-items: baseline;
    gap: var(--space-2xs);
    white-space: nowrap;

    &::before {
      content: "";
      align-self: center;
      inline-size: 0.75rem;
      block-size: 2px;
      background-color: var(--swatch);
    }
  }

  :root:has(.company-chart--full) {
    overflow: hidden;
  }

  .company-chart__title {
    display: none;
    flex: none;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-sm);
    padding-block-end: var(--space-2xs);
    border-block-end: var(--rule-double);

    & .heading {
      margin: 0;
    }
  }
}
