/*
 * Calendar — page file (body.page-calendar). The page fills the screen and only
 * the grid scrolls. Meetings are placed with custom properties in minutes
 * (--start, --length, --minute) set by the server (research R6).
 */

/* ---- Page: pin the shell so the grid, not the page, scrolls ---- */

.page-calendar .shell {
  height: 100dvh;
}

.page-calendar .shell__main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  padding: var(--space-3) var(--space-3) calc(56px + env(safe-area-inset-bottom) + var(--space-2));
}

/* The friends button would sit on the grid; Create takes its place. */
.page-calendar .shell__friends-fab {
  display: none;
}

@media (min-width: 640px) {
  .page-calendar .shell__main {
    padding: var(--space-4);
  }
}

.calendar {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  gap: var(--space-3);
}

/* ---- Round Create button below desktop (the panel is hidden there) ---- */

.calendar__fab {
  position: fixed;
  right: var(--space-4);
  bottom: calc(56px + var(--space-4) + env(safe-area-inset-bottom));
  z-index: 24;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  color: var(--color-night);
  background-color: var(--color-purple);
  border-radius: var(--radius-circle);
  box-shadow: var(--shadow-dialog);
}

.calendar__fab:focus-visible {
  outline: none;
  box-shadow: var(--glow-purple);
}

@media (min-width: 640px) {
  .calendar__fab {
    bottom: var(--space-6);
  }
}

@media (min-width: 1024px) {
  .calendar__fab {
    display: none;
  }
}

/* ---- Toolbar: Calendar · Today · ‹ › · label ---- */

.calendar__toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
}

.calendar__title {
  display: none;
  margin: 0 var(--space-2) 0 0;
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
}

.calendar__today {
  display: inline-flex;
  align-items: center;
  height: 36px;
  padding: 0 var(--space-4);
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  color: var(--color-foreground);
  text-decoration: none;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
}

.calendar__today:hover {
  text-decoration: none;
  background-color: var(--color-selection);
}

.calendar__steps {
  display: inline-flex;
  gap: var(--space-1);
}

.calendar__step {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  color: var(--color-foreground);
  border-radius: var(--radius-circle);
}

.calendar__step:hover {
  background-color: var(--color-selection);
}

.calendar__today:focus-visible,
.calendar__step:focus-visible,
.calendar__create:focus-visible {
  outline: none;
  box-shadow: var(--glow-purple);
}

.calendar__label {
  margin: 0;
  font-size: var(--text-xl);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
}

/* The label; below desktop it opens the mini month. */
.calendar__jump {
  position: relative;
}

.calendar__jump summary {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  list-style: none;
  cursor: pointer;
}

.calendar__jump summary::-webkit-details-marker {
  display: none;
}

/* Anchored to the label's right edge (the label sits on the right on phones),
   and never wider than the screen. */
.calendar__jump-menu {
  position: absolute;
  top: calc(100% + var(--space-2));
  right: 0;
  z-index: 20;
  width: min(280px, calc(100vw - var(--space-8)));
  padding: var(--space-3);
  background-color: var(--color-card-raised);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-comfortable);
  box-shadow: var(--shadow-dialog);
}

/* Desktop: the panel shows the mini month, so the label is just a label. */
@media (min-width: 1024px) {
  .calendar__jump summary {
    pointer-events: none;
    cursor: default;
  }

  /* Scoped so it beats the shared .icon rule, which loads later. */
  .calendar__jump .calendar__jump-icon,
  .calendar__jump-menu {
    display: none;
  }
}

/* Day | Week | Month | Year */
.calendar__views {
  display: inline-flex;
  margin-left: auto;
  padding: 2px;
  background-color: var(--color-card);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
}

.calendar__view-link {
  padding: 0 var(--space-3);
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  line-height: 30px;
  color: var(--color-comment);
  text-decoration: none;
  border-radius: var(--radius-pill);
}

.calendar__view-link:hover {
  color: var(--color-foreground);
  text-decoration: none;
}

.calendar__view-link[aria-current="page"] {
  color: var(--color-night);
  background-color: var(--color-purple);
}

.calendar__view-link:focus-visible {
  outline: none;
  box-shadow: var(--glow-purple);
}

/* Phones: the switcher takes its own row. */
@media (max-width: 639px) {
  .calendar__views {
    order: 5;
    width: 100%;
    margin-left: 0;
    justify-content: space-between;
  }

  .calendar__view-link {
    flex: 1;
    text-align: center;
  }
}

@media (min-width: 640px) {
  .calendar__title {
    display: block;
  }
}

/* ---- Body: calendar panel + view ---- */

.calendar__body {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  flex: 1;
  min-height: 0;
  gap: var(--space-4);
}

.calendar__panel {
  display: none;
}

.calendar__view {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

@media (min-width: 1024px) {
  .calendar__body {
    grid-template-columns: 240px minmax(0, 1fr);
  }

  .calendar__panel {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    min-height: 0;
  }
}

.calendar__create {
  display: inline-flex;
  align-items: center;
  align-self: flex-start;
  gap: var(--space-2);
  height: 48px;
  padding: 0 var(--space-5) 0 var(--space-4);
  font-weight: var(--weight-semibold);
  color: var(--color-foreground);
  text-decoration: none;
  background-color: var(--color-card-raised);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-card);
}

.calendar__create:hover {
  text-decoration: none;
  background-color: var(--color-selection);
}

/* ---- Day and Week grid ---- */

.calendar-grid {
  --hour: 48px;
  --gutter: 52px;
  --column-min: 0px;
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  background-color: var(--color-card);
  border-radius: var(--radius-comfortable);
}

/* Seven columns don't fit a phone: the week scrolls sideways inside the grid. */
.calendar__view--week .calendar-grid {
  --column-min: 64px;
}

.calendar-grid__head,
.calendar-grid__body {
  display: grid;
  grid-template-columns: var(--gutter) repeat(var(--days), minmax(var(--column-min), 1fr));
}

.calendar-grid__head {
  position: sticky;
  top: 0;
  z-index: 4;
  background-color: var(--color-card);
  border-bottom: 1px solid var(--color-border);
}

.calendar-grid__corner {
  position: sticky;
  left: 0;
  background-color: var(--color-card);
}

.calendar-grid__day-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: var(--space-2) 0;
}

.calendar-grid__weekday {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-badge);
  text-transform: uppercase;
  color: var(--color-comment);
}

.calendar-grid__date {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--color-foreground);
  text-decoration: none;
  border-radius: var(--radius-circle);
}

.calendar-grid__date:hover {
  text-decoration: none;
  background-color: var(--color-selection);
}

.calendar-grid__day-head--today .calendar-grid__weekday {
  color: var(--color-purple);
}

.calendar-grid__day-head--today .calendar-grid__date {
  color: var(--color-night);
  background-color: var(--color-purple);
}

.calendar-grid__hours {
  position: sticky;
  left: 0;
  z-index: 3;
  background-color: var(--color-card);
}

.calendar-grid__hour {
  display: block;
  height: var(--hour);
  padding-right: var(--space-2);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--color-comment);
  text-align: right;
  transform: translateY(-0.6em);
}

.calendar-grid__column {
  position: relative;
  border-left: 1px solid var(--color-border-subtle);
}

.calendar-grid__column--today {
  background-color: color-mix(in srgb, var(--color-purple) 5%, transparent);
}

/* Hour lines use the stronger border (#454158) so rows are easy to follow. */
.calendar-slot {
  display: block;
  height: var(--hour);
  border-top: 1px solid var(--color-border);
}

a.calendar-slot:hover {
  background-color: color-mix(in srgb, var(--color-purple) 12%, transparent);
}

a.calendar-slot:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 2px var(--color-purple);
}

/* ---- Now-line ---- */

.calendar-now {
  position: absolute;
  top: calc(var(--minute) / 60 * var(--hour));
  left: 0;
  right: 0;
  z-index: 3;
  height: 2px;
  background-color: var(--color-red);
  pointer-events: none;
}

.calendar-now::before {
  content: "";
  position: absolute;
  left: -5px;
  top: -4px;
  width: 10px;
  height: 10px;
  background-color: var(--color-red);
  border-radius: var(--radius-circle);
}

/* ---- Meetings and stacks ---- */

.calendar-stack {
  position: absolute;
  top: calc(var(--start) / 60 * var(--hour));
  height: calc(var(--length) / 60 * var(--hour) - 2px);
  left: 2px;
  right: 6px;
  z-index: 1;
}

.calendar-stack--many {
  right: 12px;
}

.calendar-event {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 2px var(--space-2);
  overflow: hidden;
  font-size: var(--text-sm);
  line-height: 1.25;
  color: var(--event-text);
  text-decoration: none;
  background-color: var(--event-color);
  border-radius: var(--radius-subtle);
  box-shadow: 0 0 0 1px var(--color-card);
}

.calendar-event:hover {
  text-decoration: none;
  filter: brightness(1.08);
}

.calendar-event:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--color-foreground);
}

.calendar-event--past {
  opacity: 0.55;
}

/* An app incident nobody has resolved yet: drawn up to now, open at the bottom. */
.calendar-event--ongoing {
  border-bottom: 2px dashed var(--event-text);
}

.calendar-event__chip {
  align-self: flex-start;
  flex-shrink: 0;
  padding: 0 var(--space-1);
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  line-height: var(--leading-snug);
  border: 1px solid currentColor;
  border-radius: var(--radius-subtle);
}

/* The title never gives up its line; the time is cut off first. */
.calendar-event__title {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  gap: 3px;
  font-weight: var(--weight-semibold);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.calendar-event__title .icon {
  flex-shrink: 0;
}

.calendar-event__time {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  opacity: 0.85;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Short meetings: one line, title then time. */
.calendar-stack--short .calendar-event {
  flex-direction: row;
  align-items: center;
  gap: var(--space-1);
  padding-top: 0;
  padding-bottom: 0;
}

/* The title keeps its room; the time gives way first in narrow columns. */
.calendar-stack--short .calendar-event__title {
  flex: 0 1 auto;
  min-width: 0;
}

.calendar-stack--short .calendar-event__time {
  flex: 0 100 auto;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
}

/* The meetings behind the top one peek out down and to the right. */
.calendar-stack__shadow {
  position: absolute;
  inset: 0;
  z-index: 0;
  background-color: var(--event-color);
  border-radius: var(--radius-subtle);
  box-shadow: 0 0 0 1px var(--color-card);
  opacity: 0.7;
  transform: translate(calc(var(--depth) * 4px), calc(var(--depth) * 4px));
}

.calendar-stack__more {
  position: absolute;
  top: 3px;
  right: 3px;
  padding: 0 6px;
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  line-height: 18px;
  color: var(--color-foreground);
  background-color: var(--color-night);
  border-radius: var(--radius-pill);
}

/* ---- Month ---- */

.calendar-month {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background-color: var(--color-card);
  border-radius: var(--radius-comfortable);
}

.calendar-month__weekdays {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  padding: var(--space-2) 0;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-badge);
  text-transform: uppercase;
  text-align: center;
  color: var(--color-comment);
  border-bottom: 1px solid var(--color-border);
}

.calendar-month__grid {
  display: grid;
  flex: 1;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: repeat(var(--weeks), minmax(72px, 1fr));
  min-height: 0;
  overflow-y: auto;
}

.calendar-month__day {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  padding: var(--space-1);
  border-right: 1px solid var(--color-border-subtle);
  border-bottom: 1px solid var(--color-border);
}

.calendar-month__day--outside {
  background-color: color-mix(in srgb, var(--color-night) 50%, transparent);
}

.calendar-month__day--outside .calendar-month__number {
  color: var(--color-comment);
}

/* The empty area of a day: add a meeting (sits behind the day's links). */
.calendar-month__add {
  position: absolute;
  inset: 0;
  z-index: 0;
}

.calendar-month__add:hover {
  background-color: color-mix(in srgb, var(--color-purple) 8%, transparent);
}

.calendar-month__number,
.calendar-month__event,
.calendar-month__more {
  position: relative;
  z-index: 1;
}

.calendar-month__number {
  align-self: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  color: var(--color-foreground);
  text-decoration: none;
  border-radius: var(--radius-circle);
}

.calendar-month__number:hover {
  text-decoration: none;
  background-color: var(--color-selection);
}

.calendar-month__day--today .calendar-month__number {
  color: var(--color-night);
  background-color: var(--color-purple);
}

.calendar-month__event {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
  padding: 1px 4px;
  font-size: var(--text-xs);
  color: var(--color-foreground);
  text-decoration: none;
  border-radius: var(--radius-subtle);
}

.calendar-month__event:hover {
  text-decoration: none;
  background-color: var(--color-selection);
}

.calendar-month__event--past {
  opacity: 0.55;
}

.calendar-month__dot {
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  background-color: var(--event-color);
  border-radius: var(--radius-circle);
}

.calendar-month__event--emergency .calendar-month__dot {
  box-shadow: 0 0 0 2px var(--color-red);
}

.calendar-month__time {
  flex-shrink: 0;
  font-family: var(--font-mono);
  color: var(--color-comment);
}

.calendar-month__title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.calendar-month__more {
  align-self: flex-start;
  padding: 0 4px;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--color-purple);
  text-decoration: none;
}

/* Phones: meetings are dots in a row; the day opens for details. */
@media (max-width: 639px) {
  .calendar-month__day {
    flex-flow: row wrap;
    align-content: flex-start;
    justify-content: center;
  }

  .calendar-month__number {
    flex-basis: 100%;
    width: auto;
  }

  .calendar-month__event {
    padding: 2px;
  }

  .calendar-month__time,
  .calendar-month__title {
    display: none;
  }

  .calendar-month__more {
    flex-basis: 100%;
    text-align: center;
  }
}

/* ---- Year ---- */

.calendar-year {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-4);
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-2);
}

@media (min-width: 640px) {
  .calendar-year {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 1024px) {
  .calendar-year {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

.calendar-year .mini-month {
  padding: var(--space-3);
  background-color: var(--color-card);
  border-radius: var(--radius-comfortable);
}

/* ---- Mini month (panel and Year) ---- */

.mini-month__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-2);
}

.mini-month__title {
  margin: 0;
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.mini-month__steps {
  display: inline-flex;
  gap: 2px;
}

.mini-month__step {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  color: var(--color-foreground);
  border-radius: var(--radius-circle);
}

.mini-month__step:hover {
  background-color: var(--color-selection);
}

.mini-month__grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  justify-items: center;
  row-gap: 2px;
}

.mini-month__weekday {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  line-height: 24px;
  color: var(--color-comment);
}

.mini-month__day {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  font-size: var(--text-sm);
  color: var(--color-foreground);
  text-decoration: none;
  border-radius: var(--radius-circle);
}

.mini-month__day:hover {
  text-decoration: none;
  background-color: var(--color-selection);
}

.mini-month__day:focus-visible,
.mini-month__step:focus-visible {
  outline: none;
  box-shadow: var(--glow-purple);
}

.mini-month__day--outside {
  color: var(--color-comment);
  opacity: 0.6;
}

.mini-month__day--selected {
  box-shadow: inset 0 0 0 2px var(--color-purple);
}

.mini-month__day--today {
  font-weight: var(--weight-bold);
  color: var(--color-night);
  background-color: var(--color-purple);
}

.mini-month__day--has-meetings::after {
  content: "";
  position: absolute;
  bottom: 2px;
  width: 4px;
  height: 4px;
  background-color: var(--color-cyan);
  border-radius: var(--radius-circle);
}

.mini-month__day--today.mini-month__day--has-meetings::after {
  background-color: var(--color-night);
}

