@layer reset, tokens, base, layout, components, utilities, overrides;

@layer tokens {
  :root {
    color-scheme: dark;
    --color-bg: oklch(16% 0.01 250);
    --color-surface: oklch(22% 0.01 250);
    --color-text: oklch(95% 0.005 250);
    --color-muted: oklch(72% 0.01 250);
    --color-border: oklch(34% 0.01 250);
    --color-accent: oklch(80% 0.12 85);
    --space-xs: .35rem;
    --space-s: .6rem;
    --space-m: 1rem;
    --space-l: 1.5rem;
    --space-xl: 2.5rem;
    --radius-s: .4rem;
    --radius-m: .75rem;
    --leading-tight: 1.25;
    --leading-body: 1.5;
    --text-s: .85rem;
    --text-m: 1rem;
    --size-target-min: 24px;
  }

  @function --when-supported(--value) {
    result: var(--value);
  }
}

@layer reset {
  * {
    box-sizing: border-box;
  }

  body {
    margin: 0;
  }

  :where(input, button) {
    font: inherit;
  }
}

@layer base {
  html,
  body {
    background: var(--color-bg);
    color: var(--color-text);
    font: var(--text-m) / var(--leading-body) system-ui, sans-serif;
  }

  header {
    max-inline-size: 900px;
    margin-block: var(--space-xl) 0;
    margin-inline: auto;
    padding-inline: var(--space-m);

    & h1 {
      margin-block-end: var(--space-xs);
      line-height: var(--leading-tight);
    }

    & p {
      max-inline-size: 70ch;
      margin-block-start: 0;
      color: var(--color-muted);
    }

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

  :is(h1, h2, h3) {
    text-wrap: balance;
  }

  p {
    text-wrap: pretty;
  }

  :where(a, button, input, [tabindex]):focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
  }
}

@layer utilities {
  .visually-hidden {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    overflow: clip;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

@layer components {
  .support-banner {
    display: block;
    display: --when-supported(none);
    max-inline-size: 900px;
    margin-block: var(--space-m);
    margin-inline: auto;
    padding: var(--space-s) var(--space-m);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-s);
    background: var(--color-surface);
    color: var(--color-muted);
    font-size: var(--text-s);

    & strong {
      color: var(--color-text);
    }
  }

  .chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-xs);
    margin: 0;
    padding: 0;
    border: 0;

    & legend {
      inline-size: 100%;
      margin-block-end: var(--space-xs);
      color: var(--color-muted);
      font-size: var(--text-s);
    }

    & label {
      display: inline-flex;
      align-items: center;
      gap: var(--space-xs);
      min-block-size: var(--size-target-min);
      padding: var(--space-xs) var(--space-s);
      border: 1px solid var(--color-border);
      border-radius: 999px;
      background: var(--color-surface);
      font-size: var(--text-s);
      cursor: pointer;

      &:has(:checked) {
        border-color: var(--color-accent);
        color: var(--color-accent);
      }
    }

    & input {
      accent-color: var(--color-accent);
      margin: 0;
    }
  }
}

@layer tokens {
  :root {
    --cal-bg: oklch(99% 0.002 250);
    --cal-text: oklch(24% 0.01 250);
    --cal-muted: oklch(52% 0.012 250);
    --cal-line: oklch(90% 0.005 250);
    --cal-weekend: oklch(95.5% 0.008 250);
    --cal-today: oklch(57% 0.2 27);
    --cal-event: oklch(46% 0.1 245);
    --cal-event-bg: oklch(93% 0.035 245);
    --cal-row: minmax(4.25rem, auto);
  }

  @property --y { syntax: "<integer>"; inherits: true; initial-value: 2026; }
  @property --m { syntax: "<integer>"; inherits: true; initial-value: 11; }
  @property --first { syntax: "<integer>"; inherits: true; initial-value: 0; }
  @property --days { syntax: "<integer>"; inherits: true; initial-value: 31; }
  @property --leap { syntax: "<integer>"; inherits: true; initial-value: 0; }
  @property --extra { syntax: "<integer>"; inherits: false; initial-value: 0; }
  @property --in-month { syntax: "<integer>"; inherits: false; initial-value: 0; }
  @property --col { syntax: "<integer>"; inherits: false; initial-value: 1; }
  @property --row { syntax: "<integer>"; inherits: false; initial-value: 1; }
  @property --is-today { syntax: "<integer>"; inherits: false; initial-value: 0; }

  @counter-style month-name {
    system: fixed;
    symbols: "January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December";
  }

  @counter-style weekday-name {
    system: fixed 0;
    symbols: "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday";
  }

  @counter-style is-or-not {
    system: fixed 0;
    symbols: "is not" "is";
  }

  @function --is-zero(--x <integer>) returns <integer> {
    result: calc(1 - min(1, max(var(--x), -1 * var(--x))));
  }

  @function --is-leap(--y <integer>) returns <integer> {
    --by100: --is-zero(mod(var(--y), 100));
    result: calc(--is-zero(mod(var(--y), 4)) * (1 - var(--by100) + var(--by100) * --is-zero(mod(var(--y), 400))));
  }

  @function --days-in-month(--y <integer>, --m <integer>) returns <integer> {
    result: calc(28 + mod(var(--m) + round(down, var(--m) / 8, 1), 2) + mod(2, var(--m)) + 2 * round(down, 1 / var(--m), 1) + --is-leap(var(--y)) * --is-zero(calc(var(--m) - 2)));
  }

  @function --weekday(--y <integer>, --m <integer>, --d <integer>) returns <integer> {
    --jan-feb: clamp(0, 3 - var(--m), 1);
    --mm: calc(var(--m) + 12 * var(--jan-feb));
    --yy: calc(var(--y) - var(--jan-feb));
    --k: mod(var(--yy), 100);
    --j: round(down, var(--yy) / 100, 1);
    --h: mod(var(--d) + round(down, 13 * (var(--mm) + 1) / 5, 1) + var(--k) + round(down, var(--k) / 4, 1) + round(down, var(--j) / 4, 1) + 5 * var(--j), 7);
    result: mod(var(--h) + 5, 7);
  }

  @function --year-of(--date <integer>) returns <integer> {
    result: round(down, var(--date) / 10000, 1);
  }

  @function --month-of(--date <integer>) returns <integer> {
    result: mod(round(down, var(--date) / 100, 1), 100);
  }

  @function --day-of(--date <integer>) returns <integer> {
    result: mod(var(--date), 100);
  }
}

@layer layout {
  .calendar {
    --today: 20261121;
    display: grid;
    gap: var(--space-l);
    max-inline-size: 860px;
    margin-block: var(--space-l) var(--space-xl);
    margin-inline: auto;
    padding-inline: var(--space-m);
  }

  .controls {
    display: grid;
    gap: var(--space-m);
  }

  .month {
    --y: attr(data-year type(<integer>), 2026);
    --m: attr(data-month type(<integer>), 11);
    --first: --weekday(var(--y), var(--m), 1);
    --days: --days-in-month(var(--y), var(--m));
    --leap: --is-leap(var(--y));
    padding: var(--space-l);
    border-radius: var(--radius-m);
    background: var(--cal-bg);
    color: var(--cal-text);
    overflow-x: auto;

    .calendar:has([value="y2000"]:checked) & { --y: 2000; }
    .calendar:has([value="y2024"]:checked) & { --y: 2024; }
    .calendar:has([value="y2100"]:checked) & { --y: 2100; }
    .calendar:has([value="m1"]:checked) & { --m: 1; }
    .calendar:has([value="m2"]:checked) & { --m: 2; }
    .calendar:has([value="m3"]:checked) & { --m: 3; }
    .calendar:has([value="m4"]:checked) & { --m: 4; }
    .calendar:has([value="m5"]:checked) & { --m: 5; }
    .calendar:has([value="m6"]:checked) & { --m: 6; }
    .calendar:has([value="m7"]:checked) & { --m: 7; }
    .calendar:has([value="m8"]:checked) & { --m: 8; }
    .calendar:has([value="m9"]:checked) & { --m: 9; }
    .calendar:has([value="m10"]:checked) & { --m: 10; }
    .calendar:has([value="m12"]:checked) & { --m: 12; }
  }

  .weekdays,
  .sheet {
    display: grid;
    grid-template-columns: repeat(7, minmax(4.5rem, 1fr));
  }

  .sheet {
    grid-template-rows: repeat(6, var(--cal-row));
    border-block-start: 1px solid var(--cal-line);
    border-inline-start: 1px solid var(--cal-line);
  }

  .days,
  .events {
    display: grid;
    grid-column: 1 / -1;
    grid-row: 1 / -1;
    grid-template-columns: subgrid;
    grid-template-rows: subgrid;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .events {
    pointer-events: none;
  }
}

@layer components {
  .month-title {
    margin: 0;
    font-size: 1.5rem;
    line-height: var(--leading-tight);
    counter-reset: mo var(--m) yr var(--y);

    &::before {
      content: counter(mo, month-name) " " counter(yr);
    }
  }

  .facts {
    margin-block: var(--space-xs) var(--space-m);
    color: var(--cal-muted);
    font-size: var(--text-s);
    counter-reset: wd var(--first) dd var(--days) yr var(--y) lp var(--leap);

    &::before {
      content: "Starts on a " counter(wd, weekday-name) ", " counter(dd) " days. " counter(yr) " " counter(lp, is-or-not) " a leap year.";
    }
  }

  .weekdays {
    margin: 0;
    padding: 0;
    list-style: none;

    & li {
      padding: var(--space-xs) var(--space-s);
      color: var(--cal-muted);
      font-size: var(--text-s);
      font-weight: 600;
    }
  }

  .days li {
    --wd: mod(var(--first) + sibling-index() - 1, 7);
    --extra: clamp(0, sibling-index() - var(--days), 1);
    --is-today: --is-zero(calc(var(--y) * 10000 + var(--m) * 100 + sibling-index() - var(--today)));
    display: if(style(--extra: 1): none; else: block);
    padding: var(--space-xs) var(--space-s);
    border-block-end: 1px solid var(--cal-line);
    border-inline-end: 1px solid var(--cal-line);
    background: color-mix(in oklch, var(--cal-weekend) calc(clamp(0, var(--wd) - 4, 1) * 100%), var(--cal-bg));
    box-shadow: inset 0 0 0 calc(var(--is-today) * 2px) var(--cal-today);
    color: color-mix(in oklch, var(--cal-today) calc(var(--is-today) * 100%), var(--cal-text));
    font-weight: if(style(--is-today: 1): 800; else: 400);
    font-variant-numeric: tabular-nums;

    &:first-child {
      grid-column-start: calc(var(--first) + 1);
    }
  }

  .events li {
    --date: attr(data-date type(<integer>), 0);
    --ed: --day-of(var(--date));
    --in-month: calc(--is-zero(calc(--year-of(var(--date)) - var(--y))) * --is-zero(calc(--month-of(var(--date)) - var(--m))));
    --col: calc(mod(var(--first) + var(--ed) - 1, 7) + 1);
    --row: calc(round(down, (var(--first) + var(--ed) - 1) / 7, 1) + 1);
    display: if(style(--in-month: 1): block; else: none);
    grid-column: var(--col);
    grid-row: var(--row);
    align-self: end;
    margin: var(--space-xs);
    padding: 0.15rem var(--space-xs);
    border-radius: var(--radius-s);
    background: var(--cal-event-bg);
    color: var(--cal-event);
    font-size: 0.75rem;
    font-weight: 600;
    line-height: var(--leading-tight);
  }
}
