:root {
  --bg: #ffffff;
  --surface: #f3f5f8;
  --text: #1b1f24;
  --muted: #656d76;
  --line: #dde2e7;
  --accent: #1f6feb;
  --danger: #cf222e;
  --hour: 48px; /* keep in sync with HOUR_PX in views/timegrid.js */
  --tabbar: 3.25rem;
  /* Background-colour tints for all-day events: pastel mixes of the event colour */
  --tint-base: #ffffff;
  --tint-soft: 22%;
  --tint-strong: 42%;
  --tint-label: rgb(255 255 255 / .7);
  color-scheme: light dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --surface: #161b22;
    --text: #e6edf3;
    --muted: #8d96a0;
    --line: #2d333b;
    --accent: #4493f8;
    --danger: #f85149;
    --tint-base: #0d1117;
    --tint-soft: 40%;
    --tint-strong: 62%;
    --tint-label: rgb(0 0 0 / .3);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { overscroll-behavior: none; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; margin: 0; }
.error { color: var(--danger); margin: 0; }

/* ---------- Login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 1.5rem; }
.login-card { width: 100%; max-width: 22rem; text-align: center; }
.login-card h1 { margin: .5rem 0 .25rem; }
.login-providers { display: flex; flex-direction: column; gap: .75rem; margin: 1.5rem 0 1rem; }
.provider-btn {
  display: block; padding: .85rem 1rem; border-radius: .75rem; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); text-decoration: none; font-weight: 600;
}
.provider-btn:hover { border-color: var(--accent); }

/* ---------- App shell ---------- */
#app { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: flex; align-items: center; gap: .25rem;
  padding: calc(env(safe-area-inset-top) + .4rem) .5rem .4rem;
  border-bottom: 1px solid var(--line);
}
.title { flex: 1; margin: 0 .25rem; font-size: 1.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 2.5rem; height: 2.5rem; border-radius: 50%; font-size: 1.5rem; line-height: 1; display: grid; place-items: center; }
.icon-btn:hover, .pill:hover { background: var(--surface); }
.pill { border: 1px solid var(--line); border-radius: 999px; padding: .3rem .8rem; font-size: .85rem; }
.view { flex: 1; min-height: 0; overflow: hidden; position: relative; }
.tabbar {
  display: grid; grid-template-columns: repeat(5, 1fr);
  border-top: 1px solid var(--line); background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button { height: var(--tabbar); font-size: .85rem; color: var(--muted); }
.tabbar button[aria-current="true"] { color: var(--accent); font-weight: 700; }
.fab {
  position: fixed; right: 1rem; z-index: 5;
  bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 1rem);
  width: 3.5rem; height: 3.5rem; border-radius: 1rem;
  background: var(--accent); color: #fff; font-size: 2rem; line-height: 1;
  box-shadow: 0 4px 14px rgb(0 0 0 / .25);
}

/* Shared event pieces */
.chip {
  display: block; width: 100%; flex: none; text-align: left;
  font-size: .68rem; line-height: 1.35; padding: 0 .25rem; border-radius: 3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  border-left: 3px solid var(--c);
  background: color-mix(in srgb, var(--c) 16%, transparent);
}
.chip.solid { background: var(--c); border-left-color: transparent; }
.bg-label {
  display: block; flex: none; align-self: flex-start; max-width: 100%; text-align: left;
  font-size: .66rem; font-weight: 700; line-height: 1.4;
  padding: 0 .4rem; border-radius: 999px; background: var(--tint-label);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* All-day background tints: soft pastel gradient of the event colour.
   Bars that start or end inside the visible row get rounded ends (.start / .end). */
.tint-band {
  border-radius: 10px;
  background: linear-gradient(135deg,
    color-mix(in oklab, var(--c) var(--tint-soft), var(--tint-base)),
    color-mix(in oklab, var(--c) var(--tint-strong), var(--tint-base)));
}

/* ---------- Year ---------- */
/* The 12 strips share the available height (and scroll if they can't fit) */
.year {
  --yr-num-h: .95rem; --yr-lane-h: 5px; --yr-dot-h: 7px;
  display: grid; grid-template-rows: auto repeat(12, minmax(min-content, 1fr)); row-gap: 3px;
  height: 100%; overflow-y: auto; padding: 0 .25rem 5.5rem;
}
.yr-row { display: grid; column-gap: 1px; }
.yr-head { position: sticky; top: 0; z-index: 3; background: var(--bg); padding: .3rem 0 2px; }
.yr-dow { font-size: .55rem; line-height: 1.4; text-align: center; color: var(--muted); border-radius: 2px; }
.yr-label { grid-column: 1; align-self: start; text-align: left; font-size: .7rem; font-weight: 700; line-height: var(--yr-num-h); }
.yr-day {
  min-width: 0; display: flex; justify-content: center; border-radius: 3px;
  background: var(--surface);
}
.yr-day.weekend, .yr-dow.weekend { background: color-mix(in srgb, var(--accent) 14%, var(--surface)); }
.yr-num { font-size: .5rem; line-height: var(--yr-num-h); color: var(--muted); font-variant-numeric: tabular-nums; }
.yr-day.today { box-shadow: inset 0 0 0 1.5px var(--accent); }
.yr-day.today .yr-num { color: var(--accent); font-weight: 700; }
/* Lines and dots sit on top of the day boxes but let taps through to them */
.yr-line, .yr-dots { z-index: 1; pointer-events: none; }
.yr-line { align-self: center; height: 3px; background: var(--c); }
.yr-line.start { margin-left: 1px; border-top-left-radius: 2px; border-bottom-left-radius: 2px; }
.yr-line.end { margin-right: 1px; border-top-right-radius: 2px; border-bottom-right-radius: 2px; }
.yr-dots { display: flex; flex-wrap: wrap; justify-content: center; align-content: start; gap: 1px; padding-bottom: 1px; }
.yr-dot { width: 4px; height: 4px; border-radius: 50%; background: var(--c); }

/* ---------- Month ---------- */
.month { display: grid; grid-template-rows: auto 1fr; height: 100%; }
.month-dow {
  display: grid; grid-template-columns: repeat(7, 1fr); text-align: center;
  font-size: .7rem; color: var(--muted); padding: .3rem 0; border-bottom: 1px solid var(--line);
}
.month-grid { --num-h: 1.75rem; --bar-h: 1rem; --bar-step: calc(var(--bar-h) + 2px); display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); grid-template-rows: repeat(6, minmax(0, 1fr)); }
.mcell {
  min-height: 0; overflow: hidden; padding: 2px; text-align: left;
  display: flex; flex-direction: column; gap: 1px;
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.mcell:nth-child(7n) { border-right: 0; }
/* Cell content sits above the background bands, which sit above the grid lines */
.mcell > * { position: relative; z-index: 2; }
.mband { position: relative; z-index: 1; pointer-events: none; }
.mband .tint-band { position: absolute; left: 0; right: 0; border-radius: 0; container-type: size; }
/* Crowded weeks make bands thin; drop the label rather than let it spill over other bands */
@container (max-height: 1.2rem) { .mband .bg-label { display: none; } }
/* The top lane's label sits under the date number (--num-h) and any solid bars (--bar-step each) */
@container (max-height: 2.9rem) { .mband .first.bars-0 .bg-label { display: none; } }
@container (max-height: 4.05rem) { .mband .first.bars-1 .bg-label { display: none; } }
@container (max-height: 5.2rem) { .mband .first.bars-2 .bg-label { display: none; } }
@container (max-height: 6.35rem) { .mband .first.bars-3 .bg-label { display: none; } }
.mband .tint-band.start { left: 3px; border-top-left-radius: 10px; border-bottom-left-radius: 10px; }
.mband .bg-label { position: absolute; left: 3px; bottom: 3px; max-width: calc(100% - 6px); }
/* Solid all-day bars, above the bands and cell content; taps fall through to the day cell */
.mbar {
  position: relative; z-index: 3; pointer-events: none; align-self: start; min-width: 0;
  height: var(--bar-h); padding: 0 .3rem; background: var(--c);
  font-size: .66rem; font-weight: 600; line-height: var(--bar-h);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.mbar.start { margin-left: 3px; border-top-left-radius: 4px; border-bottom-left-radius: 4px; }
.mbar.end { margin-right: 3px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; }
.mcell .bar-space { flex: none; }
.mband .tint-band.end { right: 3px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; }
.mcell.other > * { opacity: .45; }
.mcell .num {
  align-self: center; flex: none; width: 1.5rem; height: 1.5rem; line-height: 1.5rem;
  text-align: center; border-radius: 50%; font-size: .75rem; font-weight: 600;
}
.mcell.today .num { background: var(--accent); color: #fff; }
.mcell .more { font-size: .65rem; color: var(--muted); padding-left: .25rem; }

/* ---------- Week / day ---------- */
.tg { display: flex; flex-direction: column; height: 100%; }
.tg-head, .tg-allday, .tg-body { display: grid; }
.tg-head { border-bottom: 1px solid var(--line); }
.tg-day { display: flex; flex-direction: column; align-items: center; padding: .2rem 0; min-width: 0; }
.tg-day .dow { font-size: .65rem; color: var(--muted); text-transform: uppercase; }
.tg-day .dnum { width: 1.9rem; height: 1.9rem; line-height: 1.9rem; border-radius: 50%; font-weight: 600; }
.tg-day.today .dnum { background: var(--accent); color: #fff; }
/* Grows a row per overlapping all-day event; scrolls only past half the screen */
.tg-allday { border-bottom: 1px solid var(--line); max-height: 50%; overflow-y: auto; padding-bottom: 2px; }
.tg-allday-cell { min-width: 0; min-height: 1.8rem; border-left: 1px solid var(--line); }
.adbar {
  min-width: 0; height: 1.3rem; margin-top: 2px; padding: 0 .4rem; border-radius: 0;
  font-size: .68rem; font-weight: 600; line-height: 1.3rem; text-align: left;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.adbar.tint-band { border-radius: 0; color: var(--text); }
.adbar.solid { background: var(--c); }
.adbar.start { margin-left: 3px; border-top-left-radius: 6px; border-bottom-left-radius: 6px; }
.adbar.end { margin-right: 3px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; }
.tg-gutter-label { font-size: .6rem; color: var(--muted); text-align: right; padding: .35rem .3rem 0 0; }
.tg-scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.tg-body { position: relative; }
.tg-hours { position: relative; }
.tg-hours span {
  position: absolute; right: .3rem; transform: translateY(-50%);
  font-size: .62rem; color: var(--muted); white-space: nowrap;
}
.tg-col {
  position: relative; min-width: 0; border-left: 1px solid var(--line);
  background-image: repeating-linear-gradient(to bottom, var(--line) 0 1px, transparent 1px var(--hour));
}
.ev {
  position: absolute; z-index: 1; overflow: hidden; text-align: left;
  padding: 1px 4px; border-radius: 4px; border: 1px solid var(--bg);
  background: var(--c); font-size: .7rem; line-height: 1.25;
}
.ev-title { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev-time { display: block; opacity: .85; white-space: nowrap; }
.now { position: absolute; left: 0; right: 0; z-index: 2; height: 2px; background: var(--danger); pointer-events: none; }
.now::before {
  content: ""; position: absolute; left: -5px; top: -4px;
  width: 10px; height: 10px; border-radius: 50%; background: var(--danger);
}

/* ---------- Agenda ---------- */
.agenda { height: 100%; overflow-y: auto; padding-bottom: 6rem; }
/* One grid row per day; background bands are grid items spanning their days' rows */
.ag-grid { display: grid; grid-template-columns: minmax(0, 1fr); }
.ag-day { grid-column: 1; border-bottom: 1px solid var(--line); padding: .5rem 1rem; }
/* Day content sits above the bands, which sit above the day dividers */
.ag-day > * { position: relative; z-index: 2; }
.ag-bandcol { grid-column: 1; position: relative; z-index: 1; pointer-events: none; }
.ag-bandcol .tint-band { position: absolute; top: 0; bottom: 0; border-radius: 0; }
.ag-bandcol .tint-band.start { top: 3px; border-top-left-radius: 10px; border-top-right-radius: 10px; }
.ag-bandcol .tint-band.end { bottom: 3px; border-bottom-left-radius: 10px; border-bottom-right-radius: 10px; }
.ag-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: .25rem .75rem; padding: .25rem 0; }
.ag-date { font-weight: 700; font-size: .9rem; }
.ag-day.today .ag-date { color: var(--accent); }
/* Background event names, each with a dot in its band's colour */
.ag-bglabel { font-size: .75rem; }
.ag-bglabel::before {
  content: ""; display: inline-block; width: .55rem; height: .55rem; margin-right: .3rem;
  border-radius: 50%; background: var(--c);
}
.ag-item {
  display: flex; gap: .75rem; width: 100%; text-align: left;
  padding: .5rem .5rem .5rem .75rem; margin: .25rem 0; border-radius: .5rem;
  border-left: 4px solid var(--c); background: color-mix(in srgb, var(--surface) 80%, transparent);
}
.ag-time { flex: none; width: 5.75rem; font-size: .8rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.ag-text { min-width: 0; display: flex; flex-direction: column; }
.ag-title { font-weight: 600; overflow-wrap: anywhere; }
.ag-loc { font-size: .8rem; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 3rem 1rem; }

/* ---------- Dialogs (bottom sheets on phones) ---------- */
dialog.sheet {
  width: 100%; max-width: 34rem; max-height: 92dvh; overflow-y: auto;
  margin: auto auto 0; padding: 0; border: 0; border-radius: 1rem 1rem 0 0;
  background: var(--bg); color: var(--text);
}
@media (min-width: 40rem) { dialog.sheet { margin: auto; border-radius: 1rem; } }
dialog::backdrop { background: rgb(0 0 0 / .45); }
.sheet-head {
  position: sticky; top: 0; z-index: 1; background: var(--bg);
  display: flex; align-items: center; justify-content: space-between;
  padding: .75rem 1rem; border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 1rem; }
.sheet-body { display: flex; flex-direction: column; gap: .75rem; padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom)); }
.text-btn { color: var(--accent); padding: .3rem .5rem; min-width: 4rem; }
.text-btn:first-child { text-align: left; }
.text-btn.primary { font-weight: 700; text-align: right; }

input, select, textarea {
  font: inherit; font-size: 16px; /* 16px stops iOS zooming on focus */
  color: inherit; background: var(--surface);
  border: 1px solid var(--line); border-radius: .5rem; padding: .5rem .6rem; min-width: 0;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
textarea { resize: vertical; }
.big-input { font-size: 1.2rem; font-weight: 600; }
.field { display: flex; align-items: center; flex-wrap: wrap; gap: .5rem; min-height: 2.5rem; }
.field > span:first-child { flex: 1 1 6rem; }
.field small { display: block; font-size: .75rem; }
.field input[type="number"] { width: 5rem; }
.unit { flex: none; }

.switch input {
  appearance: none; flex: none; position: relative; cursor: pointer;
  width: 2.75rem; height: 1.6rem; padding: 0; border: 0; border-radius: 999px;
  background: var(--line); transition: background .15s;
}
.switch input::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: calc(1.6rem - 6px); height: calc(1.6rem - 6px); border-radius: 50%;
  background: #fff; transition: transform .15s;
}
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(1.15rem); }

dialog:not(.all-day) .only-all-day { display: none; }
dialog.all-day input[type="time"] { display: none; }
dialog:not(.repeats) .only-repeat { display: none; }

.colours input[type="color"] { width: 2.5rem; height: 2rem; padding: 2px; }
.swatches { display: flex; flex-wrap: wrap; gap: .4rem; }
.swatch { width: 1.75rem; height: 1.75rem; border-radius: 50%; background: var(--c); }
.swatch[aria-pressed="true"] { outline: 2px solid var(--text); outline-offset: 2px; }

.delete-zone { display: flex; flex-direction: column; gap: .5rem; margin-top: .5rem; }
.danger { width: 100%; padding: .7rem; border: 1px solid var(--line); border-radius: .6rem; color: var(--danger); font-weight: 600; }
