@layer base, components;

@layer base {
  :root {
    --bg: #f6f7f9;
    --surface: #fff;
    --text: #14181d;
    --muted: #5d6673;
    --line: #dfe3e8;
    --accent: #0b57d0;
    --late: #b3261e;
    --early: #7a5900;
    --ontime: #146c2e;
    --ac: #0b6a75;
    --radius: 10px;
  }

  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #12151a;
      --surface: #1b2028;
      --text: #e7eaee;
      --muted: #9aa4b2;
      --line: #2c333d;
      --accent: #a8c7fa;
      --late: #f2b8b5;
      --early: #e4c66a;
      --ontime: #7ee2a0;
      --ac: #6fd3de;
    }
  }

  * { box-sizing: border-box; }

  body {
    margin: 0;
    padding: 1.5rem 1rem 3rem;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  }

  main { max-width: 42rem; margin-inline: auto; }

  h1 { font-size: 1.5rem; margin: 0 0 1rem; }
  h2 { font-size: 1rem; margin: 0 0 .5rem; display: flex; gap: .5rem; align-items: center; }

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

  ul { list-style: none; margin: 0; padding: 0; }
}

@layer components {
  .muted { color: var(--muted); }

  .warn {
    padding: .5rem .75rem;
    border-radius: var(--radius);
    background: color-mix(in srgb, var(--late) 15%, var(--surface));
    color: var(--late);
    font-size: .9rem;
  }

  .search {
    display: flex;
    flex-direction: column;
    gap: .25rem;
    margin-bottom: 1rem;

    label { font-size: .85rem; color: var(--muted); }

    input {
      padding: .6rem .75rem;
      font-size: 1rem;
      color: var(--text);
      background: var(--surface);
      border: 1px solid var(--line);
      border-radius: var(--radius);

      &:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
    }
  }

  .badge {
    display: inline-grid;
    place-items: center;
    min-width: 2.25rem;
    padding: .1rem .4rem;
    border-radius: 6px;
    background: var(--accent);
    color: var(--surface);
    font-weight: 600;
    font-size: .85rem;
  }

  .badge-tram  { background: #c8102e; color: #fff; }
  .badge-metro { background: #e64415; color: #fff; }
  .badge-bus   { background: #0057b8; color: #fff; }
  .badge-train { background: #00728f; color: #fff; }

  .group {
    margin-bottom: 1rem;

    li {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 1rem;
      padding: .4rem .6rem;
      border-bottom: 1px solid var(--line);

      &:last-child { border-bottom: 0; }
    }
  }

  .add {
    text-decoration: none;
    padding: .2rem .5rem;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: .85rem;
    white-space: nowrap;

    &:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
  }

  .added { color: var(--muted); font-size: .85rem; }

  .card {
    margin-bottom: .75rem;
    padding: .75rem;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);

    header {
      display: flex;
      align-items: center;
      gap: .5rem;
    }

    .stop { font-weight: 600; }
    .remove { margin-left: auto; text-decoration: none; font-size: 1.25rem; line-height: 1; }
  }

  /* Line-level, so it sits in the header rather than on every departure. */
  .rt {
    padding: .05rem .35rem;
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--muted);
    font-size: .7rem;
  }

  .dir {
    margin: 0;
    color: var(--muted);
    font-size: .8rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The next departure is the card. */
  .next {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: baseline;
    margin-top: .4rem;
  }

  .at {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
    align-items: baseline;
    margin: 0;
    font-size: .85rem;
  }

  .later {
    margin: .6rem 0 0;
    color: var(--muted);
    font-size: .8rem;

    time + time::before { content: " · "; }
  }

  .countdown {
    font-weight: 600;
    font-variant-numeric: tabular-nums;

    &.lead {
      font-size: 2.5rem;
      font-weight: 650;
      line-height: 1.15;
      letter-spacing: -.01em;
    }

    /* Gone, but still on the board until the next poll. */
    &.departed { color: var(--late); }
  }

  .clock { font-weight: 600; font-variant-numeric: tabular-nums; }
  .towards { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .delay { font-size: .85rem; font-variant-numeric: tabular-nums; }
  .delay.late   { color: var(--late); }
  .delay.early  { color: var(--early); }
  .delay.ontime { color: var(--ontime); }

  /* One line for the board: fills over the scheduled poll delay, full when the
     next request goes out. Runs from zero on every swap, so no offset is
     needed — only the duration, which arrives as classes. */
  .poll {
    --poll: calc(var(--poll-tens, 0) + var(--poll-units, 0));

    position: relative;
    margin: 0 0 .75rem;
    padding-bottom: .45rem;
    color: var(--muted);
    font-size: .75rem;

    &::before,
    &::after {
      content: "";
      position: absolute;
      inset-inline: 0;
      bottom: 0;
      block-size: 3px;
      border-radius: 2px;
    }

    &::before { background: color-mix(in srgb, var(--muted) 25%, transparent); }

    &::after {
      background: var(--accent);
      transform-origin: left;
      animation: poll-fill calc(var(--poll) * 1s) linear forwards;
    }
  }

  @keyframes poll-fill {
    from { transform: scaleX(0); }
    to   { transform: scaleX(1); }
  }

  /* Poll delay in seconds, from the server; tens plus units is 17 rules instead
     of 70. Covers any TTL under a minute. */
  .poll-t0 { --poll-tens: 0; }
  .poll-t1 { --poll-tens: 10; }
  .poll-t2 { --poll-tens: 20; }
  .poll-t3 { --poll-tens: 30; }
  .poll-t4 { --poll-tens: 40; }
  .poll-t5 { --poll-tens: 50; }
  .poll-t6 { --poll-tens: 60; }

  .poll-u0 { --poll-units: 0; }
  .poll-u1 { --poll-units: 1; }
  .poll-u2 { --poll-units: 2; }
  .poll-u3 { --poll-units: 3; }
  .poll-u4 { --poll-units: 4; }
  .poll-u5 { --poll-units: 5; }
  .poll-u6 { --poll-units: 6; }
  .poll-u7 { --poll-units: 7; }
  .poll-u8 { --poll-units: 8; }
  .poll-u9 { --poll-units: 9; }

  .ac { white-space: nowrap; }
  .ac.yes { color: var(--ac); font-size: 1.1rem; }
  .ac.no { color: var(--muted); font-size: .75rem; }
}