/*
 * The engine's charts. Every rule sits under .duty-vis and every class is named duty-vis-…, so
 * this file can't clash with a host's Bootstrap or Tailwind. It is not in a cascade layer:
 * Bootstrap's CSS isn't, so a layered file would lose every conflict.
 */

/*
 * The colours are custom properties a host can change. Their defaults sit on the page root at
 * zero specificity (:where), so a host rule on .duty-vis, or on any element around a chart,
 * wins without a fight over specificity or source order.
 */
:where(:root) {
  /* What a duty spends its time on. A train is blue whether the driver works it (solid) or
     passes on it (hollow); a taxi leaves the railway. Checked as a set for colour blindness. */
  --duty-vis-work: #1c5cab;
  --duty-vis-taxi: #eda100;
  --duty-vis-pnb: #008300;
  --duty-vis-prep: #e87ba4;
  --duty-vis-remarshal: #7b4fb0;
  --duty-vis-booking-on: #5b6573;

  /* The PNB window's edges: a rule across every chart, in a green of the PNB's family but brighter
     and limier, so it belongs with the PNB blocks yet stands out against them and the blue. */
  --duty-vis-window: #6ad41a;

  /* The page the charts are drawn on, and the greys of their text and lines. */
  --duty-vis-paper: #fff;
  --duty-vis-ink: oklch(21% 0.034 264.665);
  --duty-vis-muted: oklch(44.6% 0.03 256.802);
  --duty-vis-tick: oklch(55.1% 0.027 264.364);
  --duty-vis-rule-strong: oklch(87.2% 0.01 258.338);
  --duty-vis-rule: oklch(92.8% 0.006 264.531);
  --duty-vis-soft: oklch(37.3% 0.034 259.733);
  --duty-vis-stone: oklch(98.5% 0.001 106.423);

  /* Monaco first, then the system's monospace fonts: no web fonts. */
  --duty-vis-mono: Monaco, ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    "Courier New", monospace;
  --duty-vis-radius: 3px;

  /* The column of the duties' names beside the charts of a set: one width in every chart, so
     charts drawn as separate fragments line up under the set's ruler. */
  --duty-vis-name-width: 7rem;
}

/*
 * The root gives the charts the reset they need, rather than relying on Tailwind's Preflight or
 * Bootstrap's Reboot. A class selector beats Reboot's element selectors without any tricks. It
 * also sets the text colour, line height and tabular figures. It starts a stacking context, so
 * the charts' z-indexes can't compete with a host's, and asks printers to keep the colours.
 */
.duty-vis {
  color: var(--duty-vis-ink);
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
  isolation: isolate;
  print-color-adjust: exact;

  &,
  & *,
  & *::before,
  & *::after {
    box-sizing: border-box;
    border: 0 solid;
  }
}

/* What a block of each kind looks like, as a swatch in the key and as a mark in the charts. */
.duty-vis {
  .duty-vis-fill-work {
    background: var(--duty-vis-work);
    color: #fff;
  }

  .duty-vis-fill-pass {
    border: 2px solid var(--duty-vis-work);
    background: var(--duty-vis-paper);
    color: var(--duty-vis-ink);
  }

  .duty-vis-fill-taxi {
    background: var(--duty-vis-taxi);
    color: var(--duty-vis-ink);
  }

  .duty-vis-fill-pnb {
    border: 1px solid var(--duty-vis-pnb);
    background: color-mix(in srgb, var(--duty-vis-pnb) 15%, transparent);
    color: var(--duty-vis-ink);
  }

  .duty-vis-fill-prep {
    background: var(--duty-vis-prep);
    color: var(--duty-vis-ink);
  }

  .duty-vis-fill-remarshal {
    background: var(--duty-vis-remarshal);
    color: var(--duty-vis-paper);
  }

  .duty-vis-fill-booking_on {
    background: var(--duty-vis-booking-on);
    color: var(--duty-vis-paper);
  }

  /* Diagonal stripes for a transition: the time between activities. */
  .duty-vis-hatched,
  .duty-vis-fill-transition {
    background-image: repeating-linear-gradient(
      135deg, var(--duty-vis-rule-strong) 0 2px, transparent 2px 7px
    );
  }

  .duty-vis-fill-transition {
    background-color: var(--duty-vis-paper);
    color: var(--duty-vis-muted);
  }

  /* The mandated break within a PNB: a darker shade of the PNB's own. */
  .duty-vis-mandated {
    background: color-mix(in srgb, var(--duty-vis-pnb) 40%, transparent);
  }
}

.duty-vis {
  .duty-vis-swatch {
    display: inline-block;
    flex-shrink: 0;
    width: 1.25rem;
    height: 0.75rem;
    border-radius: var(--duty-vis-radius);
  }

  .duty-vis-swatch-mandated {
    border: 1px solid var(--duty-vis-pnb);
    background: color-mix(in srgb, var(--duty-vis-pnb) 40%, transparent);
  }

  /* One edge of the PNB window: the key shows two, with the words between. */
  .duty-vis-window-mark {
    display: inline-block;
    flex-shrink: 0;
    height: 0.75rem;
    border-left: 3px solid var(--duty-vis-window);
  }

  .duty-vis-key {
    display: flex;
    flex-wrap: wrap;
    column-gap: 1rem;
    margin: 0;
    padding: 0;
    list-style: none;
    color: var(--duty-vis-muted);
  }

  .duty-vis-key-item {
    display: flex;
    align-items: center;
    gap: 0.375rem;
  }
}

/* The ruler: hours labelled along the top, with a tick for each hour and quarter. */
.duty-vis {
  .duty-vis-ruler {
    position: relative;
    height: 1.75rem;
    font-size: 0.75rem;
    line-height: 1rem;
    color: var(--duty-vis-muted);
  }

  /* A solid halo of paper, so a PNB window's edge doesn't show through the time. */
  .duty-vis-ruler-time {
    position: absolute;
    top: 0.25rem;
    transform: translateX(-50%);
    text-shadow:
      -2px -2px 0 var(--duty-vis-paper), 0 -2px 0 var(--duty-vis-paper),
      2px -2px 0 var(--duty-vis-paper), -2px 0 0 var(--duty-vis-paper),
      2px 0 0 var(--duty-vis-paper), -2px 2px 0 var(--duty-vis-paper),
      0 2px 0 var(--duty-vis-paper), 2px 2px 0 var(--duty-vis-paper);
  }

  .duty-vis-tick {
    position: absolute;
    bottom: 0;
    height: 0.5rem;
    border-left: 1px solid var(--duty-vis-tick);
  }

  .duty-vis-tick-quarter {
    height: 0.25rem;
    border-left-color: var(--duty-vis-rule-strong);
  }
}

/*
 * The duty bar: the whole duty on one line. It scrolls sideways rather than squash below 44rem,
 * where its labels would touch. With a name it has two columns, the name and the bar.
 */
.duty-vis {
  .duty-vis-duty-bar {
    overflow-x: auto;
  }

  .duty-vis-duty-bar-grid {
    display: grid;
    grid-template-columns: 1fr;
    min-width: 44rem;
    column-gap: 1rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
  }

  .duty-vis-named {
    grid-template-columns: max-content 1fr;
  }

  /* Drawn to the scale of other bars: as narrow as its duration asks, and no scrolling to read it. */
  .duty-vis-duty-bar-grid.duty-vis-to-scale {
    min-width: 0;
  }

  .duty-vis-head {
    border-bottom: 1px solid var(--duty-vis-rule-strong);
  }

  /* The ruler's cell, so the window's edges can be placed across it as they are on the track. */
  .duty-vis-head-ruler {
    position: relative;

    /* The ruler is a stacking context of its own: lifted above the window's edges, its times
       (and their halos) sit over them. */
    > .duty-vis {
      position: relative;
      z-index: 11;
    }
  }

  .duty-vis-name {
    padding: 0.5rem 0;
    font-family: var(--duty-vis-mono);
    font-weight: 600;
    white-space: nowrap;
  }

  .duty-vis-name-line {
    display: flex;
    justify-content: space-between;
    gap: 0.75rem;
  }

  /* A chart in a set: its name column is the set's width, with the days beside the planning
     stage rather than at the column's far end, and its track starts and spans as its slot says
     (--duty-vis-from and --duty-vis-share, percentages of the set's width). */
  &.duty-vis-framed {
    .duty-vis-named {
      grid-template-columns: var(--duty-vis-name-width) 1fr;
      column-gap: 0.75rem;
    }

    .duty-vis-name-line {
      justify-content: flex-start;
    }

    .duty-vis-track {
      width: var(--duty-vis-share, 100%);
      margin-left: var(--duty-vis-from, 0%);
    }
  }

  /* An hour line or a window edge at the very end of the duty is drawn a pixel past the track,
     which would make the bar scroll; nothing is meant to show beyond it. */
  .duty-vis-track {
    position: relative;
    padding: 0.5rem 0 0.75rem;
    overflow-x: clip;
  }

  .duty-vis-hour {
    position: absolute;
    top: 0;
    bottom: 0;
    border-left: 1px solid var(--duty-vis-rule);
  }

  .duty-vis-window-edge {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 10;
    margin-left: -1px;
    border-left: 3px solid var(--duty-vis-window);
  }

  /* At the very start or end of the duty the line would be half outside the bar, so it sits inside. */
  .duty-vis-window-edge-start {
    margin-left: 0;
  }

  .duty-vis-window-edge-end {
    margin-left: -3px;
  }

  .duty-vis-lane {
    position: relative;
    height: 1.5rem;
  }

  /* Anything drawn along the bar, positioned by left and width in the element's style. */
  .duty-vis-mark {
    position: absolute;
    top: 0;
    bottom: 0;
  }

  .duty-vis-leg,
  .duty-vis-booking-on-block,
  .duty-vis-pnb-block,
  .duty-vis-prep-block,
  .duty-vis-remarshal-block {
    border-radius: var(--duty-vis-radius);
  }

  .duty-vis-prep-block,
  .duty-vis-remarshal-block {
    z-index: 10;
  }

  /*
   * A trip that starts with a prep is one bar: the prep leads into the trip's first service. The
   * two meet square and keep their rounded outer ends.
   */
  .duty-vis-prep-lead {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }

  .duty-vis-leg-led {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
  }

  /* A remarshal with one time is an instant: keep it visible. */
  .duty-vis-remarshal-block {
    min-width: 4px;
  }

  .duty-vis-pnb-block {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 0.25rem;
    font-size: 11px;
    font-weight: 600;
  }

  .duty-vis-pnb-block > .duty-vis-mandated {
    position: absolute;
    top: 0;
    bottom: 0;
  }

  /* Above the PNB window's edges (z-index 10), so one crossing a PNB doesn't cut its place name. */
  .duty-vis-pnb-place {
    position: relative;
    z-index: 11;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* A trip's whole duration, under a block for each service it runs. */
  .duty-vis-trip-line {
    position: absolute;
    top: 50%;
    height: 0.25rem;
    transform: translateY(-50%);
  }

  .duty-vis-trip-line-work {
    background: var(--duty-vis-work);
  }

  .duty-vis-trip-line-taxi {
    background: var(--duty-vis-taxi);
  }

  .duty-vis-labels {
    position: relative;
    z-index: 20;
    display: block;
    width: 100%;
    margin-top: 0.25rem;
    overflow: visible;
  }

  .duty-vis-label {
    font-family: var(--duty-vis-mono);
    fill: var(--duty-vis-ink);
    stroke: var(--duty-vis-paper);
    stroke-width: 3px;
    paint-order: stroke;
    font-size: 11px;
    font-weight: 600;
  }
}

/* A wide chart scrolls sideways instead of squeezing. */
.duty-vis {
  .duty-vis-scroll {
    overflow-x: auto;
  }
}

/*
 * The day summary: a bar split by kind, and a legend with each kind's time. With a title it has two
 * columns, as a duty bar has: the name, then the summary.
 */
.duty-vis {
  .duty-vis-titled {
    display: grid;
    grid-template-columns: max-content 1fr;
    column-gap: 1rem;
    align-items: start;

    /* The name's first line stands level with the bar, not below the padding a duty bar's track needs. */
    > .duty-vis-name {
      padding: 0;
      font-size: 0.875rem;
      line-height: 1.25rem;
    }
  }

  .duty-vis-summary-bar {
    display: flex;
    gap: 0.125rem;
    height: 1rem;
  }

  .duty-vis-summary-segment {
    border-radius: var(--duty-vis-radius);
  }

  .duty-vis-summary-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 1.25rem;
    margin: 0.5rem 0 0;
    padding: 0;
    list-style: none;
    font-size: 0.875rem;
    line-height: 1.25rem;
  }

  .duty-vis-summary-item {
    display: flex;
    align-items: center;
    gap: 0.375rem;
  }

  .duty-vis-summary-name {
    color: var(--duty-vis-muted);
  }

  .duty-vis-summary-total {
    font-weight: 600;
  }

  /* Miles for each mode: name, a bar against the longest, and the figure. */
  .duty-vis-miles {
    margin-top: 1rem;
  }

  .duty-vis-miles-list {
    display: grid;
    grid-template-columns: max-content 1fr max-content;
    gap: 0.375rem 0.75rem;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 0.875rem;
    line-height: 1.25rem;
  }

  .duty-vis-miles-row {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: subgrid;
    align-items: center;
  }

  .duty-vis-miles-name {
    color: var(--duty-vis-muted);
  }

  .duty-vis-miles-track {
    height: 0.625rem;
  }

  .duty-vis-miles-fill {
    display: block;
    height: 100%;
    border-radius: var(--duty-vis-radius);
  }

  .duty-vis-miles-value {
    font-weight: 600;
    text-align: right;
  }

  .duty-vis-notice.duty-vis-miles-note {
    margin: 0.5rem 0 0;
    padding: 0.5rem 0.75rem;
    font-size: 0.8125rem;
  }
}

/*
 * The string-line graph: time across and places down, drawn as an SVG that scales to its width
 * (between 50rem and 60rem). SVG lines and shapes take their colours from stroke and fill.
 * Two classes in it have no rule on purpose: .duty-vis-sl-leg (its colour comes from
 * .duty-vis-stroke-*) and .duty-vis-empty (the "No moves." text takes the host's styling). They
 * are hooks for specs and hosts.
 */
.duty-vis {
  /* Says something is missing from a chart, so it is noticed: for the string-line graph, where
     the graph would be. The dashes are the graph's own place lines, the bar the taxi amber. */
  .duty-vis-notice {
    max-width: 60rem;
    padding: 0.75rem 1rem;
    border: 1px dashed var(--duty-vis-taxi);
    border-left: 4px solid var(--duty-vis-taxi);
    border-radius: var(--duty-vis-radius);
    background: color-mix(in oklab, var(--duty-vis-taxi) 12%, var(--duty-vis-paper));
    color: var(--duty-vis-ink);

    strong {
      display: block;
      font-weight: 600;
    }
  }

  .duty-vis-notice-place {
    font-family: var(--duty-vis-mono);
    font-size: 0.9em;
    font-weight: 600;
  }

  .duty-vis-string-line {
    display: block;
    width: 100%;
    min-width: 50rem;
    max-width: 60rem;
    height: auto;
  }

  /* Drawn to the scale of other graphs: the width it is given (a share of the longest's), with
     no minimum or maximum to unmake it. */
  .duty-vis-string-line.duty-vis-to-scale {
    min-width: 0;
    max-width: none;
  }

  .duty-vis-sl-hour-line {
    stroke: var(--duty-vis-rule);
  }

  .duty-vis-sl-hour-tick {
    stroke: var(--duty-vis-tick);
  }

  .duty-vis-sl-quarter-tick {
    stroke: var(--duty-vis-rule-strong);
  }

  /* Drawn after the PNB window's edges, with a halo of paper so they don't run through it. */
  .duty-vis-sl-hour-label {
    fill: var(--duty-vis-muted);
    stroke: var(--duty-vis-paper);
    stroke-width: 3px;
    paint-order: stroke;
    font-size: 0.75rem;
  }

  /* A fixed width on screen, however far the graph is scaled to fit the page. */
  .duty-vis-sl-window-edge {
    stroke: var(--duty-vis-window);
    stroke-width: 3px;
    vector-effect: non-scaling-stroke;
  }

  .duty-vis-sl-place-line {
    stroke: var(--duty-vis-rule-strong);
  }

  .duty-vis-sl-place-name {
    fill: var(--duty-vis-ink);
    font-family: var(--duty-vis-mono);
    font-size: 0.75rem;
    font-weight: 600;
  }

  /*
   * A graph drawn to scale of others is squashed, and shown very wide, with many above each
   * other. Its text is sized in screen pixels, not by how far the graph is scaled to fit the
   * page: --duty-vis-unit (set by the labels controller) is how many of its own units make a
   * pixel, so the text is the same size on any screen. Without the controller it has the
   * usual size.
   */
  .duty-vis-to-scale {
    .duty-vis-sl-hour-label {
      font-size: calc(11px * var(--duty-vis-unit, 1));
    }

    .duty-vis-sl-place-name {
      font-size: calc(11px * var(--duty-vis-unit, 1));
      font-weight: 500;
    }

    .duty-vis-sl-label {
      font-size: calc(11px * var(--duty-vis-unit, 1));
    }
  }

  .duty-vis-sl-mandated {
    fill: color-mix(in srgb, var(--duty-vis-pnb) 40%, transparent);
  }

  .duty-vis-sl-pnb {
    fill: color-mix(in srgb, var(--duty-vis-pnb) 15%, transparent);
    stroke: var(--duty-vis-pnb);
  }

  .duty-vis-sl-stay {
    stroke: var(--duty-vis-rule-strong);
  }

  .duty-vis-stroke-work {
    stroke: var(--duty-vis-work);
  }

  .duty-vis-stroke-taxi {
    stroke: var(--duty-vis-taxi);
  }

  .duty-vis-sl-prep {
    fill: var(--duty-vis-prep);
    stroke: var(--duty-vis-paper);
  }

  .duty-vis-sl-sign {
    fill: var(--duty-vis-ink);
    stroke: var(--duty-vis-paper);
  }

  /* A headcode, or (with .duty-vis-sl-note) a PNB's duration, kept clear of what it crosses. */
  .duty-vis-sl-label {
    font-family: var(--duty-vis-mono);
    fill: var(--duty-vis-ink);
    stroke: var(--duty-vis-paper);
    stroke-width: 3px;
    paint-order: stroke;
    font-size: 11px;
    font-weight: 600;
  }

  .duty-vis-sl-note {
    fill: var(--duty-vis-soft);
  }
}

/*
 * The text diagram: a preformatted block, in a fixed-width font, laid out in the columns the
 * source diagram prints. Every property a host's `pre` rule sets (Bootstrap sets margin, font
 * size, display and overflow) is set here.
 */
.duty-vis {
  .duty-vis-diagram {
    display: block;
    max-width: fit-content;
    margin: 0;
    padding: 1rem;
    overflow-x: auto;
    border: 1px solid var(--duty-vis-rule-strong);
    border-radius: var(--duty-vis-radius);
    background: var(--duty-vis-stone);
    font-family: var(--duty-vis-mono);
    font-size: 13px;
    line-height: 1.25rem;
    color: var(--duty-vis-ink);
  }
}
