:root {
  --accent: #8ab4f8; /* the one accent colour */
  --bg: #0e0f13;
  --bar: #16171c;
  --sheet: #232329;
  --sheet-head: #2e2e35;
  --line: rgba(255, 255, 255, 0.09);
  --text: #e8e8ec;
  --muted: #9a9aa5;
  --today: color-mix(in srgb, var(--accent) 22%, var(--sheet-head));
  --fs: 16px;
  --lh: calc(var(--fs) * 1.5);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 12px;
  --cols: 2;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100dvh;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: currentColor; flex: none; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 8px;
  padding-top: max(4px, env(safe-area-inset-top));
  background: var(--bar);
  overflow-x: auto;
  scrollbar-width: none;
}
.topbar::-webkit-scrollbar { display: none; }
.tb {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  height: 42px;
  padding: 0 6px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  color: var(--text);
}
.tb:hover { background: color-mix(in srgb, var(--text) 10%, transparent); }
.tb[aria-pressed="true"] { color: var(--accent); }
.tb:focus-visible, .nav:focus-visible, .sheet-menu:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.tb[data-act="menu"] { position: sticky; right: -8px; background: var(--bar); border-radius: 0; }
.tb-label { font-size: 11px; line-height: 1; margin-top: 1px; }

/* back-to-today pill */
.today-pill {
  display: inline-flex; align-items: center; gap: 4px; flex: none;
  height: 40px; padding: 0 12px 0 8px; margin-right: 4px;
  border: 0; border-radius: 999px; cursor: pointer;
  background: #a83228; color: #fff;
  animation: pillIn 0.18s ease-out;
}
.today-pill[hidden] { display: none; }
.today-pill:hover { background: #bf3b2f; }
.today-pill:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.today-pill svg { width: 22px; height: 22px; }
.today-pill.ahead svg { transform: scaleX(-1); } /* today is later: arrow points right */
.tp-date { display: flex; flex-direction: column; align-items: center; line-height: 1; }
.tp-date small { font-size: 13px; font-weight: 700; }
.tp-date b { font-size: 19px; font-weight: 700; }
@keyframes pillIn { from { opacity: 0; transform: scale(0.85); } }
.tb svg { width: 22px; height: 22px; }
.year input[type="date"] {
  position: absolute; inset: 0; opacity: 0; pointer-events: none; width: 100%;
}
.brand {
  margin-left: auto;
  display: flex; flex-direction: column; line-height: 0.95; padding: 0 8px;
  color: var(--text); user-select: none;
}
.brand small { font-size: 12px; color: var(--muted); }
.brand b { font-size: 17px; }
.brand b::after { content: "."; color: var(--accent); }

/* ---------- stage / week ---------- */
.stage {
  position: relative;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  min-height: 0;
}
.nav {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  display: grid; place-items: center;
}
.nav:hover { color: var(--text); background: color-mix(in srgb, var(--text) 5%, transparent); }
.nav svg { width: 30px; height: 30px; }

.week {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-rows: minmax(140px, 1fr);
  gap: 8px;
  padding: 6px 0;
  min-height: 0;
  overflow: auto;
  touch-action: pan-y; /* vertical scroll stays native; horizontal moves are our swipe */
  overscroll-behavior-x: none; /* don't trigger the browser's back/forward gesture */
}
.sheet-head { user-select: none; -webkit-user-select: none; }

/* ----- views ----- */
/* Weekly · 7 sheets: the default two-column grid above */
/* Weekly · 2 sheets / 1 sheet: one column, each sheet half / all of the screen, snapping */
.week.view-week2, .week.view-week1 { grid-template-columns: minmax(0, 1fr); scroll-snap-type: y mandatory; }
.week.view-week2 { grid-auto-rows: calc((100% - 8px) / 2); }
.week.view-week1 { grid-auto-rows: 100%; }
.week.view-week2 .sheet, .week.view-week1 .sheet { scroll-snap-align: start; }
/* 2 days */
.week.view-side { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-auto-rows: minmax(0, 1fr); }
.week.view-stack { grid-template-columns: minmax(0, 1fr); grid-auto-rows: minmax(0, 1fr); }
/* Monthly calendar */
.week.view-month {
  --fs: 13px; --lh: 19px;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: auto;
  grid-auto-rows: minmax(70px, 1fr);
  gap: 4px;
}
.month-dow { text-align: center; color: var(--muted); font-size: 12px; font-weight: 600; padding: 2px 0; text-transform: uppercase; }
.view-month .sheet { border-radius: 8px; }
.view-month .sheet-head { min-height: 22px; padding: 0 6px; }
.view-month .sheet-head .dname { font-size: 13px; font-weight: 600; }
.view-month .sheet.today .dname::after { display: none; }
.view-month .sheet.today .dname { color: var(--accent); }
.view-month .sheet-head .count { font-size: 10px; margin-left: auto; }
.view-month .editor { padding: 0 5px; }
.view-month .editor .task { padding-left: 1.4em; }
.view-month .other-month { opacity: 0.45; }
body.swiping, body.swiping * { user-select: none !important; cursor: grabbing !important; }
.week.anim-next { animation: slideNext 0.22s ease-out; }
.week.anim-prev { animation: slidePrev 0.22s ease-out; }
@keyframes slideNext { from { opacity: 0.2; transform: translateX(30px); } }
@keyframes slidePrev { from { opacity: 0.2; transform: translateX(-30px); } }

.sheet {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--sheet);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid transparent;
  transition: border-color 0.15s;
}
.sheet.span2 { grid-row: span 2; }
.sheet:focus-within { border-color: color-mix(in srgb, var(--accent) 60%, transparent); }
.sheet-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 6px 2px 10px;
  background: var(--sheet-head);
  min-height: 28px;
}
.sheet.today .sheet-head { background: var(--today); }
.sheet-head .dname { font-weight: 500; font-size: 17px; }
.sheet.today .dname { font-weight: 700; }
.sheet.today .dname::after {
  content: ""; display: inline-block; width: 9px; height: 12px; margin-left: 5px;
  background: var(--accent);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 72%, 0 100%);
}
.sheet-head .ddate { margin-left: auto; color: var(--muted); font-size: 14px; font-weight: 600; }
.sheet.today .ddate { color: var(--text); }
.sheet-head .count { font-size: 12px; color: var(--muted); }
.sheet-menu {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  width: 28px; height: 26px; border-radius: 6px; display: grid; place-items: center;
}
.sheet-menu:hover { color: var(--text); background: color-mix(in srgb, var(--text) 10%, transparent); }
.sheet-menu svg { width: 18px; height: 18px; }

.sheet-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-content: stretch;
  background-image: repeating-linear-gradient(
    to bottom, transparent 0, transparent calc(var(--lh) - 1px), var(--line) calc(var(--lh) - 1px), var(--line) var(--lh)
  );
  background-attachment: local;
  cursor: text;
  scrollbar-width: thin;
  scrollbar-color: color-mix(in srgb, var(--text) 25%, transparent) transparent;
}
.sheet-body.with-times { grid-template-columns: 66px minmax(0, 1fr); }
.times {
  width: 66px;
  white-space: nowrap;
  border-right: 1px solid var(--line);
  color: var(--muted);
  font-size: calc(var(--fs) * 0.72);
  user-select: none;
  padding-right: 4px;
  text-align: right;
}
.times div { height: var(--lh); line-height: var(--lh); font-variant-numeric: tabular-nums; }
.editor {
  flex: 1;
  min-width: 0;
  outline: none;
  padding: 0 10px;
  font-size: var(--fs);
  line-height: var(--lh);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.editor > div, .editor p { margin: 0; min-height: var(--lh); }
.editor ul, .editor ol { margin: 0; padding-left: 1.4em; }
.editor img { max-width: 100%; height: auto; border-radius: 6px; vertical-align: top; }
.editor[contenteditable="false"] { cursor: default; }
.editor .task { position: relative; padding-left: 1.6em; }
.editor .task::before {
  content: "";
  position: absolute; left: 0.1em; top: calc((var(--lh) - 1em) / 2);
  width: 1em; height: 1em; box-sizing: border-box;
  border: 2px solid var(--muted); border-radius: 4px; cursor: pointer;
}
.editor .task.done::before {
  background: var(--accent); border-color: var(--accent);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23111' d='M9 16.2 4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4z'/%3E%3C/svg%3E");
  background-size: 100%;
}
.editor .task.done { color: var(--muted); text-decoration: line-through; }
mark.hit { background: var(--accent); color: #111; border-radius: 3px; }
.sheet.flash { animation: flash 1.4s ease-out; }
@keyframes flash { 0%, 40% { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 40%, transparent); } }

/* ---------- dialogs ---------- */
dialog {
  border: 0; border-radius: 20px; padding: 0;
  background: var(--sheet-head); color: var(--text);
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100dvh - 40px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog form { padding: 20px 22px 14px; display: flex; flex-direction: column; max-height: calc(100dvh - 40px); }
dialog h2 { margin: 0 0 14px; font-size: 20px; font-weight: 500; }
#dlgBody { overflow: auto; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 14px 0 0; padding: 0; flex-wrap: wrap; }
.btn {
  border: 0; border-radius: 999px; padding: 8px 18px; cursor: pointer;
  background: color-mix(in srgb, var(--text) 10%, transparent); color: var(--accent);
}
.btn.primary { background: var(--accent); color: #111; }
.btn.danger { color: #ff8a80; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; }
.row + .row { border-top: 1px solid var(--line); }
.row input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }
.field { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; font-size: 14px; color: var(--muted); }
.field input, .field select, .search-input {
  background: var(--sheet); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; font-size: 16px;
}
.choices { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line); background: var(--sheet); border-radius: 999px; padding: 7px 14px; cursor: pointer;
}
.chip.on { background: var(--accent); color: #111; border-color: var(--accent); }
.swatch { width: 36px; height: 36px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; padding: 0; }
.swatch.on { border-color: var(--text); }
/* view picker */
.views { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--line); }
.view-col { display: flex; flex-direction: column; align-items: center; padding: 6px 4px 10px; }
.view-col + .view-col { border-left: 1px solid var(--line); }
.view-col h3 { margin: 6px 0 4px; font-size: 16px; font-weight: 600; color: var(--accent); }
.view-opt {
  display: flex; align-items: center; justify-content: center; gap: 10px; width: 100%;
  border: 0; background: transparent; padding: 10px 8px; border-radius: 10px; cursor: pointer; font-size: 16px;
}
.view-opt:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.view-opt.on { color: var(--accent); font-weight: 600; }
.view-opt .check { font-size: 14px; }
.view-opt.wide { border-top: 1px solid var(--line); border-radius: 0 0 12px 12px; padding: 14px; font-size: 17px; }
.view-opt.wide.on { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.menu-list { list-style: none; margin: 0; padding: 0; }
.menu-list button {
  width: 100%; text-align: left; border: 0; background: transparent; padding: 11px 8px; border-radius: 10px; cursor: pointer;
  display: flex; gap: 14px; align-items: center;
}
.menu-list button:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.menu-list small { display: block; color: var(--muted); font-size: 12px; }
.menu-list .mi { width: 22px; height: 22px; flex: none; color: var(--muted); }
.menu-list button.danger, .menu-list button.danger .mi { color: #f28b82; }
/* focus rings only for keyboard use, and drawn inside the row so they fit it */
#dlgForm:focus { outline: none; }
dialog button:focus:not(:focus-visible) { outline: none; }
.menu-list button:focus-visible, .view-opt:focus-visible, .results button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: -2px;
}
.results { list-style: none; margin: 10px 0 0; padding: 0; max-height: 50vh; overflow: auto; }
.results li button {
  width: 100%; text-align: left; border: 0; background: transparent; padding: 8px; border-radius: 10px; cursor: pointer;
}
.results li button:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.results .rdate { font-size: 12px; color: var(--accent); }
.results .rsnip { font-size: 14px; }
.muted { color: var(--muted); font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: 70px; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  opacity: 0; pointer-events: none; transition: 0.2s; z-index: 50; font-size: 14px; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast button { margin-left: 10px; border: 0; background: none; color: var(--accent); font-weight: 600; cursor: pointer; pointer-events: auto; }

/* ---------- responsive ---------- */
@media (max-width: 700px) {
  .stage { grid-template-columns: 1fr; }
  .nav { display: none; }
  .week { padding: 6px 8px; }
  .week.view-week7 { grid-template-columns: minmax(0, 1fr); grid-auto-rows: minmax(220px, auto); }
  .week.view-month { --fs: 11px; --lh: 16px; gap: 2px; padding: 4px; grid-auto-rows: minmax(56px, 1fr); }
  .view-month .sheet-head .count { display: none; }
  .sheet.span2 { grid-row: auto; }
  .brand { display: none; }
}

/* sync status icon: hidden until sync is set up */
.sync-btn[hidden] { display: none; }
.sync-btn svg { color: var(--muted); }
.sync-dot {
  position: absolute; right: 7px; bottom: 9px; width: 9px; height: 9px; border-radius: 50%;
  border: 2px solid var(--bar); background: var(--muted);
}
.sync-btn[data-state="ok"] .sync-dot { background: #6dd58c; }
.sync-btn[data-state="ok"]:not(.live) .sync-dot { background: var(--accent); }
.sync-btn[data-state="syncing"] .sync-dot { background: var(--accent); animation: syncPulse 0.9s ease-in-out infinite alternate; }
.sync-btn[data-state="offline"] .sync-dot { background: var(--muted); }
.sync-btn[data-state="error"] svg { color: #f28b82; }
.sync-btn[data-state="error"] .sync-dot { background: #f28b82; }
@keyframes syncPulse { to { opacity: 0.25; } }
