/* Admin pages (layouts/admin). Scoped to body.admin because the public layout loads every stylesheet.
   A sidebar (admin/_nav) beside the page; blocks are white cards on a light ground. The bus colours are map.css's. */
.admin {
  --ink: var(--color-ink);
  --muted: #5c6470;
  --line: #dfe3ea;
  --ground: #f4f6f9;
  --blue: var(--color-button);

  font: 16px/1.5 system-ui, sans-serif;
  color: var(--ink);
  background: var(--ground);
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 14rem minmax(0, 1fr);
  align-items: start;

  .sidebar {
    position: sticky;
    top: 0;
    box-sizing: border-box;
    height: 100vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    padding: 1rem 0.75rem;
    background: #fff;
    border-right: 1px solid var(--line);

    .brand { display: flex; align-items: center; gap: 0.5rem; padding: 0 0.5rem; color: var(--ink); font-weight: 700; text-decoration: none; }
    .menu-toggle, .menu-label { display: none; }
    nav { display: flex; flex-direction: column; gap: 1.25rem; flex: 1; }
    p { margin: 0 0 0.125rem; padding: 0 0.5rem; font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); }
    nav a { display: block; padding: 0.3rem 0.5rem; border-radius: 0.375rem; color: var(--ink); text-decoration: none; }
    nav a:hover { background: var(--ground); }
    nav a[aria-current] { background: #e7f0fd; color: var(--blue); font-weight: 700; }
    .button_to { margin-top: auto; }
  }

  main {
    box-sizing: border-box;
    max-width: 72rem;
    padding: 1.5rem;
  }
  /* Signed out (sign in, password reset) there is no sidebar: the form sits alone in the middle. */
  &:not(:has(.sidebar)) { display: block; main { max-width: 26rem; margin: 10vh auto 0; } }

  h1 { font-size: 1.5rem; margin: 0 0 1rem; }
  h2 { font-size: 1.1rem; margin: 1.5rem 0 0.5rem; }
  h3 { font-size: 1rem; margin: 1.25rem 0 0.5rem; }

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

  /* Tables are cards. Separate borders, so the corners can be round. */
  table { border-collapse: separate; border-spacing: 0; width: 100%; background: #fff; border: 1px solid var(--line); border-radius: 0.5rem; }
  th, td { text-align: left; padding: 0.55rem 0.75rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
  thead th { font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
  tbody tr:last-child > * { border-bottom: 0; }

  form:not(.button_to, .inline) {
    display: grid;
    grid-template-columns: minmax(0, 1fr); /* a field's size attribute (Rails sets it from maxlength) must not widen the form */
    gap: 0.25rem;
    max-width: 26rem;
    padding: 1rem;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 0.5rem;

    label:not(:first-child) { margin-top: 0.5rem; }
    input:not([type="submit"]), select, textarea { width: 100%; }
    input[type="submit"] { justify-self: start; margin-top: 0.75rem; }
  }

  input, select, textarea, button { font: inherit; }
  input:not([type="submit"], [type="checkbox"]), select, textarea {
    box-sizing: border-box;
    max-width: 100%;
    padding: 0.4rem 0.6rem;
    border: 1px solid #b9c1cd;
    border-radius: 0.375rem;
    background: #fff;
    color: var(--ink);
  }
  /* Buttons: a submit or a .button link is the page's action (blue); a button_to is quieter, one that deletes is red. */
  button, input[type="submit"], .button {
    display: inline-block;
    padding: 0.4rem 0.8rem;
    border: 1px solid var(--line);
    border-radius: 0.375rem;
    background: #fff;
    color: var(--ink);
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
  }
  button:hover, .inline input[type="submit"]:hover { background: var(--ground); }
  input[type="submit"], .button { border-color: var(--blue); background: var(--blue); color: #fff; }
  .inline input[type="submit"] { border-color: var(--line); background: #fff; color: var(--ink); }
  main input[name="_method"][value="delete"] ~ button { color: var(--color-bad); }
  :is(a, button, input, select, textarea):focus-visible { outline: 3px solid rgb(10 88 202 / 0.45); outline-offset: 1px; }

  .field_with_errors { display: contents; } /* Rails' wrapper would take the field out of the form grid */
  .button_to, .inline { display: inline-flex; gap: 0.5rem; margin: 0; }
  .actions { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; align-items: center; }
  td.actions { justify-content: flex-end; }

  .badge { font-size: 0.8em; font-weight: 600; padding: 0.1em 0.5em; border-radius: 1em; background: #fff4c7; color: #5a4700; white-space: nowrap; }

  .flash { margin: 0 0 1rem; padding: 0.6rem 0.8rem; border-radius: 0.375rem; list-style: none; }
  .flash-notice { background: #e3f3ea; color: #0f4a31; }
  .flash-alert { background: #f8d7da; color: #58151c; }
  .flash-warning { background: #fff4c7; color: #5a4700; }
  td .flash { margin: 0.25rem 0 0; }

  .expired { color: #6c6c6c; }

  progress { display: block; width: 100%; max-width: 24rem; }

  /* admin/_map_preview and the dashboard's live map */
  .map-preview { margin: 0 0 1rem; border: 1px solid var(--line); border-radius: 0.5rem; overflow: hidden; .map { height: 24rem; } }

  /* The dashboard (admin/dashboards/show): the map and the buses, with a column of panels beside them. */
  .dashboard {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 17rem;
    gap: 1rem;
    align-items: start;

    h2 { margin-top: 0; }
    aside { display: grid; gap: 1rem; }
    .panel {
      padding: 1rem;
      background: #fff;
      border: 1px solid var(--line);
      border-radius: 0.5rem;

      ul { margin: 0; padding: 0; list-style: none; display: grid; gap: 0.75rem; }
      p { margin: 0 0 0.5rem; }
      > :last-child { margin-bottom: 0; }
      small { display: block; color: var(--muted); }
      table { border: 0; }
      :is(th, td):first-child { padding-left: 0; }
    }
    /* A heading row per status; a dot per bus in the colour of its pill on the map. */
    .fleet tbody th { padding-top: 1rem; font-size: 0.85rem; color: var(--muted); }
    .dot { display: inline-block; width: 0.6rem; height: 0.6rem; margin-right: 0.4rem; border-radius: 50%; background: #5c6470; }
    .dot-on_time { background: var(--color-ok); }
    .dot-late { background: var(--color-warn); }
    .dot-very_late { background: var(--color-bad); }
    .dot-silent { background: none; box-shadow: inset 0 0 0 2px #9aa3af; }
    .badge-very_late { background: var(--color-bad); color: #fff; }
  }

  /* A line's timetable (admin/lines/show): a wide grid scrolls sideways under a stop column that stays put. */
  .timetable-scroll { overflow-x: auto; margin-bottom: 1.5rem; }
  .timetable {
    width: auto;
    font-variant-numeric: tabular-nums;
    th, td { padding: 0.15rem 0.4rem; white-space: nowrap; }
    th { position: sticky; left: 0; background: #fff; font-weight: normal; border-right: 1px solid var(--line); }
    tr:hover > * { background: var(--ground); }
  }

  /* A stop's departures (admin/stops/show): a day type per row, its times wrapping like a posted timetable. */
  .stop-departures {
    margin-bottom: 1.5rem;
    font-variant-numeric: tabular-nums;
    th { font-weight: normal; width: 14rem; vertical-align: top; }
    td { word-spacing: 0.5em; }
    @media (max-width: 40rem) { th { display: block; width: auto; padding: 0.25rem 0; border: 0; font-weight: 600; } }
  }

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

  /* A phone: the sidebar is a bar across the top, its links behind the Menu label. */
  @media (max-width: 48rem) {
    display: block;

    .sidebar {
      position: static;
      height: auto;
      flex-direction: row;
      flex-wrap: wrap;
      align-items: center;
      gap: 0.75rem;
      border-right: 0;
      border-bottom: 1px solid var(--line);

      /* Out of sight, still reachable by keyboard; the label shows its focus. */
      .menu-toggle { display: block; position: absolute; opacity: 0; pointer-events: none; }
      .menu-label { display: block; margin-left: auto; padding: 0.4rem 0.8rem; border: 1px solid var(--line); border-radius: 0.375rem; font-weight: 600; cursor: pointer; }
      .menu-toggle:focus-visible + .menu-label { outline: 3px solid rgb(10 88 202 / 0.45); }
      nav { display: none; flex-basis: 100%; }
      .menu-toggle:checked ~ nav { display: flex; }
      nav a { padding: 0.6rem 0.5rem; }
    }
    main { padding: 1rem; }
  }

  /* Tables become stacked cards on a phone; a timetable grid stays a grid and scrolls instead. */
  @media (max-width: 40rem) {
    table:not(.timetable) {
      thead { display: none; }
      tr { display: block; padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--line); }
      tr:last-child { border-bottom: 0; }
      td, tbody th { display: block; border: 0; padding: 0.25rem 0; }
      td.actions { display: flex; justify-content: flex-start; }
    }
  }
}
