@layer base, components, utilities;

:root {
  /* Brand */
  --navy: #13294B;
  --orange: #E8722B;

  /* Light scheme */
  --bg-light: #F4F5F8;
  --surface-light: #FFFFFF;
  --surface-2-light: #EEF1F6;
  --text-light: #13294B;
  --muted-light: #4E5A6E;
  --line-light: #D6DBE4;
  --link-light: #13294B;
  --tag-bg-light: #E9EDF4;
  --phase-bg-light: #FDE8DB;
  --phase-text-light: #8A3D0F;
  --stale-light: #B3261E;
  --stale-bg-light: #FBE9E7;
  --good-light: #1F6B3A;
  --build-bg-light: #13294B;
  --build-text-light: #FFFFFF;

  /* Dark scheme */
  --bg-dark: #0C1628;
  --surface-dark: #16253F;
  --surface-2-dark: #1D2F4F;
  --text-dark: #EEF1F6;
  --muted-dark: #C9D2E0;
  --line-dark: #2C3D5C;
  --link-dark: #F2A571;
  --tag-bg-dark: #243653;
  --phase-bg-dark: #4A2A14;
  --phase-text-dark: #FFC9A3;
  --stale-dark: #FF8A80;
  --stale-bg-dark: #3B1A17;
  --good-dark: #8FD3A5;
  --build-bg-dark: #3A5485;
  --build-text-dark: #FFFFFF;

  /* Semantic tokens, default light */
  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --surface-2: var(--surface-2-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --line: var(--line-light);
  --link: var(--link-light);
  --tag-bg: var(--tag-bg-light);
  --phase-bg: var(--phase-bg-light);
  --phase-text: var(--phase-text-light);
  --stale: var(--stale-light);
  --stale-bg: var(--stale-bg-light);
  --good: var(--good-light);
  --build-bg: var(--build-bg-light);
  --build-text: var(--build-text-light);

  @media (prefers-color-scheme: dark) {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --surface-2: var(--surface-2-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --line: var(--line-dark);
    --link: var(--link-dark);
    --tag-bg: var(--tag-bg-dark);
    --phase-bg: var(--phase-bg-dark);
    --phase-text: var(--phase-text-dark);
    --stale: var(--stale-dark);
    --stale-bg: var(--stale-bg-dark);
    --good: var(--good-dark);
    --build-bg: var(--build-bg-dark);
    --build-text: var(--build-text-dark);
  }

  @supports (color: light-dark(white, black)) {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --surface-2: light-dark(var(--surface-2-light), var(--surface-2-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --line: light-dark(var(--line-light), var(--line-dark));
    --link: light-dark(var(--link-light), var(--link-dark));
    --tag-bg: light-dark(var(--tag-bg-light), var(--tag-bg-dark));
    --phase-bg: light-dark(var(--phase-bg-light), var(--phase-bg-dark));
    --phase-text: light-dark(var(--phase-text-light), var(--phase-text-dark));
    --stale: light-dark(var(--stale-light), var(--stale-dark));
    --stale-bg: light-dark(var(--stale-bg-light), var(--stale-bg-dark));
    --good: light-dark(var(--good-light), var(--good-dark));
    --build-bg: light-dark(var(--build-bg-light), var(--build-bg-dark));
    --build-text: light-dark(var(--build-text-light), var(--build-text-dark));
  }

  color-scheme: light dark;
  accent-color: var(--orange);

  --font: "Montserrat", system-ui, sans-serif;
  --radius: 10px;
  --gutter: 16px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --focus: 3px solid var(--orange);
}

@layer base {
  html {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.45;
    -webkit-text-size-adjust: 100%;
  }

  body {
    margin: 0;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
  }

  h1, h2, h3 {
    line-height: 1.2;
    margin: 0;
  }

  p { margin: 0; }

  a { color: var(--link); }

  ::selection {
    background: var(--orange);
    color: var(--navy);
  }

  :where(a:any-link, button, input, select, summary, [tabindex]):focus-visible {
    outline: var(--focus);
    outline-offset: 2px;
  }

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

  /* Screen reader only text. */
  .visually-hidden:where(:not(:focus-within, :active)) {
    position: absolute !important;
    clip-path: inset(50%) !important;
    overflow: hidden !important;
    width: 1px !important;
    height: 1px !important;
    margin: -1px !important;
    padding: 0 !important;
    border: 0 !important;
    white-space: nowrap !important;
  }

  /* Keep the hidden attribute winning over display rules in later layers. */
  [hidden]:not([hidden="until-found"]) {
    display: none !important;
  }
}

@layer components {
  .skip-link {
    position: absolute;
    inset-block-start: var(--space-2);
    inset-inline-start: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius);
    z-index: 10;
    transform: translateY(-200%);
  }
  .skip-link:focus-visible { transform: none; }

  .topbar {
    background: var(--navy);
    color: #FFFFFF;
    padding: var(--space-4) var(--gutter);
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-2) var(--space-4);
  }
  .topbar h1 {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: var(--space-2) var(--space-3);
    font-size: clamp(1.3rem, 1rem + 1.4vw, 1.75rem);
    font-weight: 700;
  }
  .topbar .brand {
    font-size: max(0.85rem, 0.6em);
    font-weight: 600;
    color: #F2A571;
  }
  .topbar .brand::after {
    content: "";
    display: inline-block;
    inline-size: 1px;
    block-size: 0.9em;
    margin-inline-start: var(--space-3);
    background: currentColor;
    opacity: 0.5;
    vertical-align: -0.1em;
  }
  .updated {
    font-size: 0.8rem;
    color: #DCE3EE;
  }

  main {
    flex: 1;
    padding: var(--space-4) var(--gutter) var(--space-6);
    width: 100%;
    max-width: 100rem;
    margin-inline: auto;
    box-sizing: border-box;
  }

  /* ----- Filters ----- */
  .filters {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3) var(--space-4);
    align-items: end;
    margin-block-end: var(--space-3);
  }
  .filter {
    display: grid;
    gap: var(--space-1);
    flex: 1 1 9rem;
    max-inline-size: 14rem;
    min-inline-size: 0;
  }
  .filter > span {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
  }
  .filter select {
    min-block-size: 42px;
    inline-size: 100%;
    padding-inline: var(--space-3);
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    font-weight: 600;
    cursor: pointer;
  }
  .filter select:hover { border-color: var(--orange); }

  .refresh {
    min-block-size: 42px;
    padding-inline: var(--space-4);
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    font-weight: 600;
    cursor: pointer;
    margin-inline-start: auto;
  }
  .refresh:hover:not(:disabled) { border-color: var(--orange); }
  .refresh:disabled { opacity: 0.6; cursor: progress; }

  .count {
    font-size: 0.9rem;
    color: var(--muted);
    margin-block-end: var(--space-4);
    min-block-size: 1.3em;
  }

  .status {
    display: grid;
    gap: var(--space-3);
    justify-items: start;
    padding: var(--space-4);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    margin-block-end: var(--space-4);
  }
  .status progress { inline-size: 100%; }
  .status.error { border-color: var(--stale); }

  /* ----- Collapsible sections ----- */
  .group {
    margin-block-end: var(--space-4);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    overflow: clip;
  }
  .group > summary {
    list-style: none;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-block-size: 48px;
    padding-inline: var(--space-4);
    font-size: 1.05rem;
    font-weight: 700;
    cursor: pointer;
    user-select: none;
  }
  .group > summary::-webkit-details-marker { display: none; }
  .group > summary::before {
    content: "";
    inline-size: 0.6em;
    block-size: 0.6em;
    flex-shrink: 0;
    border-inline-end: 2.5px solid currentColor;
    border-block-end: 2.5px solid currentColor;
    rotate: -45deg;
    translate: 0 -0.1em;
  }
  .group[open] > summary::before {
    rotate: 45deg;
    translate: 0 -0.2em;
  }
  @media (prefers-reduced-motion: no-preference) {
    .group > summary::before { transition: rotate 150ms ease, translate 150ms ease; }
  }
  .group > summary:hover { background: var(--surface-2); }
  .group > summary:focus-visible { outline-offset: -3px; }
  .group-count {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--muted);
  }
  .group[data-type="Retail"] > summary { border-inline-start: 0; }

  .empty {
    padding: var(--space-5) var(--space-4);
    color: var(--muted);
    border-block-start: 1px solid var(--line);
  }

  /* ----- Table ----- */
  .table-wrap {
    overflow-x: auto;
    max-inline-size: 100%;
    border-block-start: 1px solid var(--line);
    overscroll-behavior-x: contain;
  }
  table.jobs {
    border-collapse: separate;
    border-spacing: 0;
    table-layout: fixed;
    inline-size: 100%;
    min-inline-size: 76rem;
    font-size: 0.88rem;
  }
  /* Shared column widths so the Retail and Build tables line up. */
  .c-job { inline-size: 14%; }
  .c-type { inline-size: 8%; }
  .c-phase { inline-size: 10%; }
  .c-priority { inline-size: 6%; }
  .c-owner { inline-size: 6%; }
  .c-next { inline-size: 15%; }
  .c-review { inline-size: 8%; }
  .c-updated { inline-size: 9%; }
  .c-notes { inline-size: 15.5%; }
  table.jobs :is(th, td) {
    padding: var(--space-2) var(--space-3);
    text-align: start;
    vertical-align: top;
    border-block-end: 1px solid var(--line);
    background: var(--surface);
  }
  table.jobs thead th {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    background: var(--surface-2);
    line-height: 1.25;
    padding-block: var(--space-2);
  }
  /* The Job column stays pinned while the rest scrolls sideways. */
  table.jobs .col-job {
    position: sticky;
    inset-inline-start: 0;
    z-index: 1;
    box-shadow: 1px 0 0 var(--line);
  }
  table.jobs tbody th.col-job {
    font-weight: 700;
    overflow-wrap: anywhere;
  }
  .job-link {
    color: inherit;
    text-decoration-color: color-mix(in srgb, currentColor 35%, transparent);
    text-underline-offset: 3px;
  }
  .job-link:hover { text-decoration-color: var(--orange); }

  table.jobs :is(.cell-next, .cell-notes) { overflow-wrap: anywhere; }
  table.jobs .cell-next { font-weight: 600; }
  .subtype {
    display: block;
    margin-block-start: var(--space-1);
    font-size: 0.75rem;
    color: var(--muted);
    line-height: 1.25;
  }
  table.jobs .cell-notes {
    color: var(--muted);
    font-size: 0.85rem;
    white-space: pre-line;
  }
  table.jobs tbody tr:last-child :is(th, td) { border-block-end: 0; }

  /* Stale rows: red text in Last Updated plus a tinted row, including the pinned cell. */
  table.jobs tr.stale :is(th, td) { background: var(--stale-bg); }
  table.jobs tr.stale .col-job { box-shadow: inset 4px 0 0 var(--stale), 1px 0 0 var(--line); }
  .stale-flag { color: var(--stale); }
  .stale-flag b {
    display: block;
    font-weight: 700;
  }
  .good { color: var(--good); font-weight: 600; }

  /* Bucket header rows inside each table */
  table.jobs .bucket-row th {
    background: var(--surface-2);
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    padding-block: var(--space-1);
  }
  .bucket-label {
    position: sticky;
    inset-inline-start: var(--space-3);
    display: inline-block;
  }

  .tag {
    display: inline-block;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--tag-bg);
    color: var(--text);
    line-height: 1.3;
  }
  .tag.type.retail {
    background: var(--orange);
    color: var(--navy);
  }
  .tag.type.build {
    background: var(--build-bg);
    color: var(--build-text);
  }
  .tag.phase {
    background: var(--phase-bg);
    color: var(--phase-text);
  }
  .tag.warn {
    background: var(--stale-bg);
    color: var(--stale);
    border: 1px solid currentColor;
    padding-block: 1px;
  }
  .owner {
    display: inline-grid;
    place-items: center;
    min-inline-size: 2.1em;
    min-block-size: 2.1em;
    padding-inline: 0.4em;
    border-radius: 999px;
    background: var(--tag-bg);
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    cursor: default;
  }

  .priority { font-weight: 500; }
  .priority.high { color: var(--stale); font-weight: 700; }
  .priority.low { color: var(--muted); }
  .flag.needs {
    color: var(--stale);
    font-weight: 700;
  }

  .foot {
    padding: var(--space-4) var(--gutter);
    font-size: 0.8rem;
    color: var(--muted);
    text-align: center;
  }
}

@media (pointer: coarse) {
  .filter select, .refresh { min-block-size: 46px; }
  .group > summary { min-block-size: 52px; }
}

@media (forced-colors: active) {
  table.jobs tr.stale :is(th, td) { outline: 1px solid; }
}
