/* Calendar Print — on-screen look (loaded with media="screen"). See CONTRACT.md. */

:root {
  color-scheme: light;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-weekend: #f9fafb;
  --surface-outside: #eff1f4;
  --surface-hover: #f3f6fd;
  --surface-has-text: #fbfcff;
  --line: #dfe3e8;
  --text: #1c2230;
  --text-muted: #667085;
  --text-faint: #98a1b0;
  --accent: #2f6fde;
  --accent-strong: #2259bd;
  --accent-soft: #e6eefc;
  --on-accent: #ffffff;
  --danger: #c0362c;

  --radius: 12px;
  --radius-small: 8px;
  --tap: 44px;
  --gutter: 8px;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.05), 0 1px 3px rgb(16 24 40 / 0.06);

  /* Per-cell metrics; enlarged on wider screens below. */
  --num-size: 1.5rem;
  --num-inset: 3px;
  --cell-pad: 4px;
  --text-size: 0.75rem;
  --row-min: 5.5rem;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Toolbar ---------- */

.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 8px var(--gutter);
  padding-top: max(8px, env(safe-area-inset-top));
  padding-left: max(var(--gutter), env(safe-area-inset-left));
  padding-right: max(var(--gutter), env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.nav,
.actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1 1 100%;
  min-width: 0;
}

.actions {
  flex-wrap: wrap;
  gap: 8px;
}

.title,
#title {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-size: 1.125rem;
  font-weight: 650;
  line-height: 1.15;
  letter-spacing: -0.01em;
  text-align: center;
  text-wrap: balance;
  overflow-wrap: anywhere;
  font-variant-numeric: tabular-nums;
}

.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-width: var(--tap);
  min-height: var(--tap);
  margin: 0;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 500;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

/* Phone: three buttons on the first row, the two print buttons on the second. */
.actions .btn {
  flex: 1 1 30%;
  padding: 0 8px;
}

.btn[hidden] {
  display: none;
}

.btn:hover {
  background: var(--surface-hover);
  border-color: #c9d1dc;
}

.btn:active {
  background: var(--accent-soft);
}

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

.btn-icon {
  width: var(--tap);
  padding: 0 0 2px;
  border-color: transparent;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.5rem;
}

.btn-icon:hover {
  border-color: transparent;
  color: var(--text);
}

/* Year jumps are secondary to the month steps. */
#prev-year,
#next-year {
  color: var(--text-faint);
  font-size: 1.25rem;
}

#prev-year:hover,
#next-year:hover {
  color: var(--text);
}

.btn-primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

.btn-primary:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.btn-primary:active {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.btn-danger,
.btn-danger:hover,
.btn-danger:active {
  border-color: var(--danger);
  background: var(--danger);
  color: var(--on-accent);
  font-weight: 600;
}

/* ---------- Dialog ---------- */

.dialog {
  width: min(26rem, calc(100vw - 32px));
  padding: 20px;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgb(16 24 40 / 0.25);
}

.dialog::backdrop {
  background: rgb(16 24 40 / 0.4);
}

.dialog-title {
  margin: 0 0 8px;
  font-size: 1.125rem;
  font-weight: 650;
  line-height: 1.25;
}

.dialog-message {
  margin: 0 0 20px;
  color: var(--text-muted);
  overflow-wrap: anywhere;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* ---------- Calendar ---------- */

.calendar,
#calendar {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: var(--gutter);
  padding-left: max(var(--gutter), env(safe-area-inset-left));
  padding-right: max(var(--gutter), env(safe-area-inset-right));
  padding-bottom: max(var(--gutter), env(safe-area-inset-bottom));
  /* Vertical scrolling stays native; horizontal swipes go to app.js. */
  touch-action: pan-y pinch-zoom;
}

.month {
  display: none;
}

.month.is-current {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

/* #title already shows the month and year on screen. */
.month-head,
.month-name,
.month-year {
  display: none;
}

.weekdays,
.days {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
}

.weekdays {
  padding: 2px 1px 6px;
}

.weekday {
  overflow: hidden;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.weekday:nth-child(n + 6) {
  color: var(--text-faint);
}

/* The 1px gaps over a line-coloured background draw the grid lines. */
.days {
  flex: 1 1 auto;
  grid-auto-rows: minmax(var(--row-min), auto);
  gap: 1px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--line);
  box-shadow: var(--shadow);
}

.day {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: var(--tap);
  background: var(--surface);
  cursor: text;
  transition: background-color 0.12s ease;
}

.day.is-weekend {
  background: var(--surface-weekend);
}

.day.has-text {
  background: var(--surface-has-text);
}

.day.is-outside {
  background: var(--surface-outside);
  cursor: default;
}

@media (hover: hover) {
  .day:not(.is-outside):hover {
    background: var(--surface-hover);
  }
}

/* The cell is the input: the focus ring sits on the cell, not the text. */
.day:focus-within {
  z-index: 1;
  background: var(--surface);
  box-shadow: inset 0 0 0 2px var(--accent);
}

.day.is-today {
  box-shadow: inset 0 3px 0 var(--accent);
}

.day.is-today:focus-within {
  box-shadow: inset 0 0 0 2px var(--accent);
}

/* The number floats over the text area so taps on it still land in .day-text. */
.day-num {
  position: absolute;
  top: var(--num-inset);
  left: var(--num-inset);
  z-index: 1;
  display: grid;
  place-items: center;
  min-width: var(--num-size);
  height: var(--num-size);
  padding: 0 4px;
  border-radius: 999px;
  color: var(--text-muted);
  font-size: 0.8125rem;
  font-weight: 500;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

.day.is-weekend .day-num {
  color: var(--text-faint);
}

.day.has-text .day-num {
  color: var(--text);
  font-weight: 650;
}

.day.is-today .day-num {
  top: calc(var(--num-inset) + 2px);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
}

.day-text {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  margin: 0;
  padding: calc(var(--num-size) + var(--num-inset) + 4px) var(--cell-pad) var(--cell-pad);
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: var(--text-size);
  line-height: 1.3;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  caret-color: var(--accent);
  cursor: text;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}

.day-text::selection {
  background: var(--accent-soft);
}

/* Rendered Markdown (a day that is not being edited). */
.day-text p,
.day-text ul,
.day-text ol {
  margin: 0;
}

.day-text > * + * {
  margin-top: 0.35em;
}

.day-text ul,
.day-text ol {
  padding-left: 1.3em;
}

.day-text .md-task {
  margin-left: -1.3em;
  list-style: none;
}

.day-text .md-box {
  display: inline-block;
  width: 1.3em;
}

.day-text .md-task.is-done {
  color: var(--text-muted);
}

.day-text .md-h {
  font-weight: 650;
}

.day-text .md-h1 {
  font-size: 1.15em;
}

.day-text .md-h2 {
  font-size: 1.05em;
}

.day-text code {
  padding: 0 0.25em;
  border-radius: 4px;
  background: var(--accent-soft);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}

.day-text a {
  color: var(--accent-strong);
}

.day-text del {
  color: var(--text-muted);
}

.day-text hr {
  margin: 0.4em 0;
  border: 0;
  border-top: 1px solid var(--line);
}

/* ---------- Tablet and up: single toolbar row, roomier cells ---------- */

@media (min-width: 720px) {
  :root {
    --gutter: 16px;
    --num-size: 1.75rem;
    --num-inset: 6px;
    --cell-pad: 10px;
    --text-size: 0.875rem;
    --row-min: 6.5rem;
  }

  /* One row when it fits; otherwise the actions drop to a second row. */
  .toolbar {
    padding-top: max(10px, env(safe-area-inset-top));
    padding-bottom: 10px;
  }

  .nav,
  .actions {
    flex: 0 1 auto;
  }

  .actions {
    flex-wrap: nowrap;
    margin-left: auto;
  }

  .actions .btn {
    flex: 0 0 auto;
    padding: 0 14px;
  }

  .title,
  #title {
    min-width: 11ch;
    padding: 0 8px;
    font-size: 1.375rem;
  }

  .weekdays {
    padding-bottom: 8px;
  }

  .weekday {
    font-size: 0.8125rem;
  }

  .day-num {
    font-size: 0.875rem;
  }
}

/*
 * Desktop: the grid fills the viewport below the toolbar. Rows share the height
 * equally and long notes scroll inside their cell instead of growing it.
 */
@media (min-width: 720px) and (min-height: 600px) {
  body {
    height: 100vh;
    height: 100dvh;
  }

  .calendar,
  #calendar {
    min-height: 0;
  }

  .days {
    min-height: 0;
    grid-auto-rows: auto;
    grid-template-rows: repeat(var(--weeks, 6), minmax(0, 1fr));
  }

  .days[data-weeks="4"] {
    grid-template-rows: repeat(4, minmax(0, 1fr));
  }

  .days[data-weeks="5"] {
    grid-template-rows: repeat(5, minmax(0, 1fr));
  }

  .days[data-weeks="6"] {
    grid-template-rows: repeat(6, minmax(0, 1fr));
  }

  .day {
    min-height: 0;
    overflow: hidden;
  }

  .day-text {
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: var(--line) transparent;
  }
}

@media (min-width: 1200px) {
  :root {
    --gutter: 24px;
    --text-size: 0.9375rem;
  }

  .calendar,
  #calendar {
    width: 100%;
    max-width: 1680px;
    margin: 0 auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  .btn,
  .day {
    transition: none;
  }
}
