/* The calendar page's two views, switched by a floating Agenda | Grid control
   at every width (calendar-views.js; calendar-page.html loads both files):
     Agenda - createcal's month tables restyled as a list of course weeks
     Grid   - the month grid, its month and weekday rows sticky; on phones
              (<768px) it pans sideways as one surface and pinch-zooms
   The view is a class on <html>: cal-agenda or cal-grid. Loaded after the
   site's CSS (Bootstrap, main.css), unlike calendar.css, which loads before. */

/* ===== View switch + jump button (both views) ===== */
/* The switch floats at the bottom centre so it's reachable however far the
   reader has scrolled. Fixed, not sticky: in Grid view <main> is a horizontal
   scroller, which would trap a sticky element. The selector out-ranks Grid's
   "main > :not(table) { position: sticky }" rule. */
:root main.calendar-page > div.cal-view {
  position: fixed;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 1020;  /* under Bootstrap's navbar (1030) and its open menu */
  max-width: none;
  display: flex;
  align-items: center;
  margin: 0;
  padding: 4px;
  background: #fff;
  border-radius: 999px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
}
/* "View:" stays for screen readers (it labels the button group). */
div.cal-view .cal-view-label {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}
div.cal-view .cal-view-buttons {
  display: inline-flex;
  border: 1px solid #4b2e83;
  border-radius: 999px;
  overflow: hidden;
}
/* Room under the last event and the footer, so the switch never covers them. */
html.cal-agenda body,
html.cal-grid body {
  padding-bottom: 72px;
}
div.cal-view button {
  border: 0;
  background: #fff;
  color: #4b2e83;
  font: inherit;
  font-weight: 600;
  padding: 6px 14px;
  min-height: 36px;
}
div.cal-view button + button { border-left: 1px solid #4b2e83; }
div.cal-view button[aria-pressed="true"] {
  background: #4b2e83;
  color: #fff;
}
div.cal-view button:focus-visible { outline: 3px solid SkyBlue; outline-offset: -3px; }


/* ===== Agenda view ===== */
html.cal-agenda table.monthtable,
html.cal-agenda table.monthtable thead,
html.cal-agenda table.monthtable tbody,
html.cal-agenda table.monthtable tr,
html.cal-agenda table.monthtable td,
html.cal-agenda table.monthtable th {
  display: block;
  width: auto;
  border: 0;
}
html.cal-agenda table.monthtable {
  font-size: 15px;
  margin: 0 0 8px;
  max-width: 100%;
}
html.cal-agenda table.monthtable td {
  overflow-wrap: anywhere;
}

/* Month name becomes a section heading; weekday row goes away. */
html.cal-agenda table.monthtable th.month-header {
  background: none;
  filter: none;
  text-align: left;
  font-size: 20px;
  font-weight: 600;
  padding: 14px 0 4px;
  border-bottom: 2px solid #4b2e83;
  margin-bottom: 8px;
}
html.cal-agenda table.monthtable thead tr:nth-child(2),
html.cal-agenda table.monthtable th.day-header,
html.cal-agenda table.monthtable td.noevent,
html.cal-agenda table.monthtable td.notinquarter {
  display: none;
}

/* Each remaining day is a card headed by its weekday and date. */
html.cal-agenda table.monthtable td.eventtd {
  font-size: 15px;
  letter-spacing: normal;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin: 0 0 10px;
  padding: 0 4px 4px;
  background: #fff;
}
html.cal-agenda table.monthtable td.eventtd::before {
  content: attr(aria-label);
  display: block;
  font-weight: 600;
  color: #333;
  padding: 6px 4px 4px;
}
html.cal-agenda table.monthtable td span.datespan {
  display: none;
}
html.cal-agenda table.monthtable td.eventtd > div {
  margin: 4px 0 0;
  padding: 6px 8px;
  border-radius: 3px;
}

/* Today: the thick SkyBlue border, even on a day with no events. */
html.cal-agenda table.monthtable td.today {
  display: block;
  border: 5px solid SkyBlue;
}
html.cal-agenda table.monthtable td.today::before {
  content: attr(aria-label) " \2014  Today";
  color: #1a5f80;
}
html.cal-agenda table.monthtable td.noevent.today {
  border-radius: 4px;
  margin: 0 0 10px;
  padding: 6px 8px;
  font-weight: 600;
}

/* ---- Week grouping (classes and headings added by calendar-views.js) ---- */
html.cal-agenda.cal-weeks table.monthtable {
  margin: 0;
}
html.cal-agenda.cal-weeks table.monthtable thead {
  display: none;  /* week headings carry the month now */
}
html.cal-agenda.cal-weeks table.monthtable tr.cal-week {
  margin: 0 0 18px;
}
html.cal-agenda.cal-weeks table.monthtable tr.cal-week-cont {
  margin-top: -18px;  /* second half of a week split across months */
}
html.cal-agenda.cal-weeks th.cal-weekhead {
  background: none;
  text-align: left;
  padding: 0;
  margin: 0 0 4px;
  border-bottom: 2px solid #4b2e83;
}
html.cal-agenda.cal-weeks th.cal-weekhead h2 {
  font-size: 17px;
  font-weight: 600;
  color: #4b2e83;
  margin: 0;
  padding: 4px 2px;
}
html.cal-agenda.cal-weeks tr.cal-week-current th.cal-weekhead {
  background: #4b2e83;
  border-radius: 4px 4px 0 0;
}
html.cal-agenda.cal-weeks tr.cal-week-current th.cal-weekhead h2 {
  color: #fff;
  padding: 6px 8px;
}
html.cal-agenda.cal-weeks span.cal-now {
  float: right;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: SkyBlue;
  color: #12384b;
  border-radius: 10px;
  padding: 2px 8px;
  margin-top: 1px;
}
html.cal-agenda.cal-weeks tr.cal-week-current {
  background: #f6f3fb;
  border-radius: 4px;
  padding-bottom: 4px;
}
html.cal-agenda.cal-weeks tr.cal-week-empty::after {
  content: "No events this week";
  display: block;
  color: #777;
  font-style: italic;
  padding: 4px 2px;
}

/* Within a week, days are compact rows: weekday + date on the left, events right. */
html.cal-agenda.cal-weeks table.monthtable td.eventtd {
  display: grid;
  grid-template-columns: 2.9em 1fr;
  column-gap: 6px;
  align-items: start;
  border: 0;
  border-bottom: 1px solid #e3e3e3;
  border-radius: 0;
  margin: 0;
  padding: 6px 0;
  background: none;
}
html.cal-agenda.cal-weeks table.monthtable td.eventtd::before,
html.cal-agenda.cal-weeks table.monthtable td.eventtd::after {
  grid-column: 1;
  grid-row: 1 / span 30;  /* both share the date column; ::after sits below */
  display: block;
  text-align: center;
  padding: 0;
  color: #555;
}
html.cal-agenda.cal-weeks table.monthtable td.eventtd::before {
  content: attr(data-cal-dow);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  padding-top: 4px;
}
html.cal-agenda.cal-weeks table.monthtable td.eventtd::after {
  content: attr(data-cal-dom);
  padding-top: 21px;
  font-size: 20px;
  font-weight: 600;
  line-height: 1.1;
}
html.cal-agenda.cal-weeks table.monthtable td.eventtd > div {
  grid-column: 2;
  margin: 0 0 4px;
}
html.cal-agenda.cal-weeks table.monthtable td.eventtd.today {
  border: 4px solid SkyBlue;
  border-radius: 4px;
  background: #fff;
  padding: 4px 4px 2px 2px;
  margin: 4px 0;
}
html.cal-agenda.cal-weeks table.monthtable td.eventtd.today::before,
html.cal-agenda.cal-weeks table.monthtable td.eventtd.today::after {
  color: #1a5f80;
}
html.cal-agenda.cal-weeks table.monthtable td.noevent.today::before {
  content: attr(data-cal-dow) " " attr(data-cal-dom) " \2014  Today, no events";
}

/* "Jump to this week": a full-width tappable button under the subscribe link. */
p.cal-jump { margin: 0 0 16px; }
p.cal-jump a {
  display: block;
  padding: 10px 12px;
  border-radius: 6px;
  background: #4b2e83;
  color: #fff;
  font-weight: 600;
  text-align: center;
  text-decoration: none;
}
p.cal-jump a:focus-visible { outline: 3px solid SkyBlue; outline-offset: 2px; }


/* Agenda on a wide screen: a readable column, not 1140px-wide cards. */
@media (min-width: 768px) {
  p.cal-jump a {
    display: inline-block;  /* a button, not a 1140px bar */
  }
  html.cal-agenda main.calendar-page {
    max-width: 760px;
    margin: 0 auto;
  }
}

/* ===== Grid view, all widths: sticky month name + day-of-week row ===== */
/* Each month's header rows stick under the navbar until the next month's
   push them off (sticky stays inside its own table). Sticky table headers
   need three things to stay seamless:
   1. Cells must own their borders. In the collapsed border model the *table*
      paints the shared lines, so they stay put while the sticky header moves:
      the line between the month and weekday rows vanishes and the events
      scrolling underneath show through it. Separate borders with zero
      spacing, each cell drawing only its right and bottom edge (plus the
      outer left/top edges), look the same and travel with their cells.
   2. Overlap what's above instead of meeting it: tuck 1px under the navbar,
      which paints on top (z-index 1030). --navbar-height is a rounded
      offsetHeight and scroll offsets land on fractions of a device pixel, so
      an exact abutment leaves hairline gaps; the overlap absorbs both.
   3. An opaque backing that moves with the header: Chrome paints a thead's
      background as one rectangle under all its cells, so any remaining
      sub-pixel seam shows header grey, not events. */
html.cal-grid table.monthtable {
  border-collapse: separate;
  border-spacing: 0;
}
html.cal-grid table.monthtable th,
html.cal-grid table.monthtable td {
  border-width: 0 1px 1px 0;  /* right + bottom; neighbours supply the rest */
}
html.cal-grid table.monthtable tr > :first-child {
  border-left-width: 1px;
}
html.cal-grid table.monthtable th.month-header {
  border-top-width: 1px;
}
html.cal-grid table.monthtable td.today {
  border-width: 5px;  /* calendar.css's today highlight, which the rules above out-rank */
}
html.cal-grid table.monthtable thead {
  position: sticky;
  top: calc(var(--navbar-height) - 1px);
  z-index: 3;
  background: #e8e8e8;  /* the bottom stop of the header cells' gradient */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}
html.cal-grid table.monthtable td {
  scroll-margin-top: calc(var(--navbar-height) + 70px);  /* "Jump to today" lands below the headers */
}

/* ===== Grid view, phones ===== */
@media (max-width: 767px) {
  /* The calendar's <main> becomes the one horizontal scroller, stretched to
     the viewport edges (cancelling the .container gutter) so the grid pans
     edge to edge.  Every month shares its scroll position, so columns line
     up.  We deliberately don't let the document itself overflow: mobile
     browsers then widen the layout viewport, which stretches the fixed
     navbar (hamburger pushed off-screen) and the footer. */
  html.cal-grid main.calendar-page {
    margin-left: -15px;
    margin-right: -15px;
    padding-left: 15px;
    padding-right: 15px;
    /* Scrolls both ways, so the sticky month/day headers below have a
       vertical scroller to stick in; the page around it barely scrolls. */
    overflow: auto;
    height: calc(100vh - var(--navbar-height));
    height: calc(100dvh - var(--navbar-height));
    padding-bottom: 72px;                /* clear of the floating switch */
    -webkit-overflow-scrolling: touch;   /* momentum on older iOS */
    overscroll-behavior-x: contain;      /* don't trigger back-swipe */
    /* One-finger panning stays native; pinches are calendar-views.js's,
       which zoom the grid itself rather than the whole page. */
    touch-action: pan-x pan-y;
  }

  /* Readable fixed-width columns: 7 x 112px, scaled by the reader's pinch.
     calendar-views.js sets --cal-zoom; every size in the grid that should
     scale is written as calc(N * var(--z)). Not CSS zoom: WebKit (so every iPhone
     browser) zooms the boxes but keeps text at its unzoomed size, and
     reports geometry inside a zoomed element in unzoomed units. Scaling the
     sizes themselves re-lays out the grid the same way in every engine. */
  html.cal-grid {
    --z: var(--cal-zoom, 1);
  }
  html.cal-grid table.monthtable {
    font-size: calc(15.7px * var(--z));
    width: calc(785px * var(--z));
    max-width: none;
    /* Contain the abs-positioned span.visually-hidden labels in each table. */
    position: relative;
  }
  html.cal-grid table.monthtable td {
    overflow-wrap: anywhere;
  }
  /* Half a screen of room below the last month, so a pinch near the end of
     the calendar can keep the spot under the fingers in place instead of
     running out of scroll and sliding. */
  html.cal-grid table.monthtable:last-of-type {
    margin-bottom: 50vh;
  }

  /* Page title, subscribe link and hint stay put at viewport width while
     the grid pans underneath. */
  html.cal-grid main.calendar-page > :not(table) {
    position: sticky;
    left: 0;   /* measured from the scroller's padding edge, i.e. 15px in */
    max-width: calc(100vw - 30px);
  }

  /* One swipe hint, under the subscribe link. */
  html.cal-grid p.calendar-subscribe::after {
    content: "Swipe sideways, or pinch to zoom out and see the whole week";
    display: block;
    margin-top: 4px;
    font-size: 13px;
    color: #777;
  }

  /* Month name: left-aligned, and pinned to the left edge while panning
     (calendar-views.js wraps the bare text in span.cal-month-name). */
  /* Headers scale with the grid but within limits: big enough to read when
     zoomed out (the weekdays fall back to "Wed" when the full name won't
     fit), never shouting when zoomed in. Body text scales freely. */
  html.cal-grid th.month-header {
    text-align: left;
    padding: clamp(2px, calc(4px * var(--z)), 5px) 0;
    /* Just a step above the weekday names at every zoom (they're 11-17px),
       so the two header rows read as one block. */
    font-size: clamp(12px, calc(16.5px * var(--z)), 18px);
  }
  html.cal-grid th.day-header {
    font-size: clamp(11px, calc(15.7px * var(--z)), 17px);
  }
  html.cal-grid th.month-header span.cal-month-name {
    position: sticky;
    left: 0;
    display: inline-block;
    padding: 0 calc(8px * var(--z));
  }
  html.cal-grid span.datespan {
    font-size: calc(16px * var(--z));  /* calendar.css's "medium" */
    margin-right: calc(5px * var(--z));
  }

  /* Event text, scaled with the grid. */
  html.cal-grid td.eventtd {
    font-size: calc(12.5px * var(--z));
    letter-spacing: 0;
    line-height: 1.3;
  }
  html.cal-grid td.eventtd div {
    padding: calc(3px * var(--z));
    margin: calc(2px * var(--z));
  }
  /* Inside the scrolling <main> the headers stick to its top, not the page's
     (still overlapping by 1px, for the same seam reason as above). */
  html.cal-grid table.monthtable thead {
    top: -1px;
  }
  html.cal-grid table.monthtable td {
    /* Room for the stuck headers, whose height is clamped like their text. */
    scroll-margin-top: clamp(50px, calc(70px * var(--z)), 75px);
  }
}

/* ===== Grid view: short weekday names when the full ones don't fit =====
   calendar-views.js wraps each header as <span.cal-dh-full>Wednesday</span>
   <span.cal-dh-short aria-hidden>Wed</span> and sets html.cal-short-days from
   what actually rendered. Screen readers always get the full name. */
th.day-header span.cal-dh-short {
  display: none;
}
html.cal-short-days th.day-header span.cal-dh-short {
  display: inline;
}
html.cal-short-days th.day-header span.cal-dh-full {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}
