:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --other: #f8f7f3;
  --hover: #f1efe9;
  --ink: #111418;     /* near-black for readability */
  --muted: #4f545b;
  --line: #e2ded5;
  --accent: #2d6a5a;
  --on-accent: #ffffff;
  --g: #1d63a3;       /* Glenn's lines (blue) */
  --m: #2e7d32;       /* Mike's lines (green) */
  --rec: #a24d00;     /* yearly entries */
  --mark: #ffe58a;
  --shadow: 0 10px 30px rgba(0, 0, 0, .14);
  color-scheme: light;
}
/* Dark colours: used when the device is in dark mode (unless Light was chosen under ? → Appearance),
   or always when Dark was chosen. Keep the two blocks below identical. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141619; --surface: #1c1f23; --other: #181a1e; --hover: #24282d;
    --ink: #f3f1ec; --muted: #b3b8bf; --line: #2c3036;
    --accent: #6cc2a6; --on-accent: #0d1f19;
    --g: #82b8ea; --m: #86d38f; --rec: #f0a35e; --mark: #6b5a12;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141619; --surface: #1c1f23; --other: #181a1e; --hover: #24282d;
  --ink: #f3f1ec; --muted: #b3b8bf; --line: #2c3036;
  --accent: #6cc2a6; --on-accent: #0d1f19;
  --g: #82b8ea; --m: #86d38f; --rec: #f0a35e; --mark: #6b5a12;
  --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
/* Bigger, bolder type for readability: everything sized in rem scales from here. */
/* Text size (? → slider): Small · Medium (default) · Large · Extra large · Largest */
html { font-size: 16.5px; }
html[data-size="small"] { font-size: 15.5px; }
html[data-size="large"] { font-size: 18.5px; }
html[data-size="xlarge"] { font-size: 20.5px; }
html[data-size="xxlarge"] { font-size: 22.5px; }
@media (max-width: 760px) {   /* phones: Medium = iPhone standard reading size */
  html { font-size: 17px; }
  html[data-size="small"] { font-size: 16px; }
  html[data-size="large"] { font-size: 19px; }
  html[data-size="xlarge"] { font-size: 21px; }
  html[data-size="xxlarge"] { font-size: 23px; }
}
body {
  font: 500 1rem/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }
mark { background: var(--mark); color: inherit; border-radius: 2px; }

button {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
button:hover { background: var(--hover); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.icon { width: 34px; height: 34px; padding: 0; font-size: 1.15rem; line-height: 1; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Screens ── */
.center-screen { min-height: 100dvh; display: grid; place-items: center; padding: 16px; color: var(--muted); }
.signin-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 32px 28px; text-align: center; max-width: 360px; width: 100%; color: var(--ink);
}
.signin-card h1 { margin: 0 0 6px; font-size: 1.6rem; }
.signin-card p { color: var(--muted); margin: 0 0 20px; }

#app { display: flex; flex-direction: column; height: 100dvh; }
main { flex: 1; min-height: 0; position: relative; overflow: hidden; }
/* Pull-to-refresh (home-screen app on iPhone and iPad) */
.ptr { position: absolute; top: 0; left: 50%; width: 30px; height: 30px; margin-left: -15px; z-index: 5;
  opacity: 0; transform: translateY(-40px); pointer-events: none; }
.ptr-ring { width: 100%; height: 100%; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent);
  background: var(--surface); box-shadow: 0 1px 4px rgba(0,0,0,.15); box-sizing: border-box; }
.ptr.spinning .ptr-ring { animation: ptr-spin .7s linear infinite; }
@keyframes ptr-spin { to { transform: rotate(360deg); } }
.ptr.settle, .agenda.settle, .month.settle { transition: transform .25s ease, opacity .25s ease; }

/* ── Header ── */
.bar {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  /* Home-screen app on iPhone/iPad: clear the clock / status-bar area and its fade.
     On computers the safe-area inset is 0, so this adds nothing there. */
  padding-top: calc(10px + env(safe-area-inset-top, 0px) + min(14px, env(safe-area-inset-top, 0px)));
}
/* Home-screen app (iPhone/iPad): always leave at least a status bar's height plus a buffer,
   even when the device reports no safe-area space. Never applies in a normal browser tab. */
html.home-app .bar { padding-top: calc(max(24px, env(safe-area-inset-top, 0px)) + 16px); }
.nav { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.nav-icon { width: 34px; height: 34px; border-radius: 22.5%; box-shadow: 0 0 0 1px var(--line); margin-right: 4px; flex: none; }
.nav h1 { margin: 0 4px; font-size: 1.35rem; font-weight: 650; min-width: 11ch; text-align: center; }
.tools { display: flex; align-items: center; gap: 8px; }
.search { position: relative; }
#search {
  font: inherit; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px 6px 32px; width: 240px;
}
/* Magnifying glass inside the search box, like Apple's search fields */
.search-icon { position: absolute; left: 10px; top: 50%; width: 16px; height: 16px; margin-top: -8px;
  color: var(--muted); pointer-events: none; }
.results {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20;
  width: min(460px, calc(100vw - 32px)); max-height: 70vh; overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
  padding: 6px;
}
.rh { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 8px 8px 4px; }
.res {
  display: block; width: 100%; text-align: left; border: 0; border-radius: 8px;
  padding: 8px; background: transparent;
}
.res:hover, .res:focus { background: var(--hover); }
.rd { display: block; font-weight: 600; font-size: .85rem; margin-bottom: 2px; }
.rs { display: block; font-size: .85rem; }

/* ── Day text ── */
.ln { white-space: pre-wrap; overflow-wrap: anywhere; }
.ln.blank { height: .45em; }
.ln.g { color: var(--g); }
.ln.m { color: var(--m); }

.rec { color: var(--rec); font-style: italic; }
.ct .rec + * { margin-top: 2px; }
.rec-swatch { color: var(--rec); font-style: italic; }

/* ── Month view (desktop) ── */
.month {
  height: 100%;
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: auto repeat(var(--weeks, 5), minmax(0, 1fr));
  gap: 1px; background: var(--line);
}
.wd {
  background: var(--surface); padding: 5px 8px;
  font-size: .72rem; letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
}
.cell {
  background: var(--surface); padding: 4px 7px 6px; min-height: 0;
  display: flex; flex-direction: column; cursor: pointer;
}
.cell:hover { background: var(--hover); }
.cell:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.cell.other { background: var(--other); }
.cell.other .dn { color: var(--muted); font-weight: 600; }
.cell.other .ct { opacity: .72; }
.cell.today { box-shadow: inset 0 0 0 2px var(--accent); }
.cell.today .dn { color: var(--accent); }
.dn { font-size: .92rem; font-weight: 700; margin-bottom: 2px; }
.cell .ct { flex: 1; min-height: 0; overflow-y: auto; font-size: .86rem; line-height: 1.3; scrollbar-width: thin; }

/* ── Day list (mobile) ── */
.agenda { height: 100%; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: 40px; }
.mh {
  position: sticky; top: 0; z-index: 2; margin: 0; padding: 8px 16px;
  font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.day { padding: 10px 16px 12px; background: var(--surface); border-bottom: 1px solid var(--line); cursor: pointer; }
.day.empty { padding-top: 7px; padding-bottom: 7px; background: var(--other); }
.day.empty .dh { color: var(--muted); }
.day.today { border-left: 4px solid var(--accent); padding-left: 12px; }
.dh { display: flex; align-items: baseline; gap: 8px; font-weight: 700; }
.day:not(.empty) .dh { margin-bottom: 4px; }
.dow { width: 2.6em; color: var(--muted); font-weight: 600; font-size: .9rem; }
.dnum { font-size: 1.1rem; }
.badge { font-size: .7rem; background: var(--accent); color: var(--on-accent); border-radius: 99px; padding: 1px 8px; }
.toggle { font-size: .85rem; color: var(--muted); white-space: nowrap; display: flex; gap: 6px; align-items: center; }

/* ── Dialogs ── */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 20px;
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
  width: min(600px, 94vw);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog h2 { margin: 0 0 12px; font-size: 1.15rem; }
dialog h2[tabindex="-1"]:focus { outline: none; }
.editor form { display: flex; flex-direction: column; gap: 12px; }
#editor-text {
  width: 100%; min-height: 16rem; resize: vertical;
  font: inherit; font-size: 1.05rem; line-height: 1.45; color: var(--ink);
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
}
.editor-actions { display: flex; align-items: center; gap: 8px; }
.meta { flex: 1; font-size: .8rem; color: var(--muted); }
.note {
  font-size: .85rem; padding: 8px 10px; border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 14%, var(--surface)); border: 1px solid color-mix(in srgb, var(--accent) 40%, var(--line));
}
.conflict { border-top: 1px solid var(--line); padding-top: 12px; }
.conflict p { margin: 0 0 8px; font-weight: 600; }
.theirs {
  max-height: 12rem; overflow: auto; font-size: .9rem;
  background: var(--bg); border: 1px dashed var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px;
}
.conflict-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.help { width: min(640px, 94vw); max-height: 88vh; }
.help h3 { margin: 18px 0 6px; font-size: .95rem; }
/* Title row with a Close button that stays pinned while the panel scrolls */
.dialog-head { position: sticky; top: -20px; z-index: 2; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin: -20px -20px 0; padding: 16px 20px 10px; background: var(--surface); border-bottom: 1px solid var(--line); }
.dialog-head h2 { margin: 0; }
/* Text size slider, like iOS: small A — track — big A */
.size-slider { display: flex; align-items: center; gap: 12px; max-width: 360px; }
.size-slider input { flex: 1; accent-color: var(--accent); height: 28px; margin: 0; }
.size-a { font-weight: 700; line-height: 1; color: var(--ink); }
.size-a.small { font-size: 13px; } .size-a.big { font-size: 24px; }
.size-name { font-size: .85rem; color: var(--muted); margin-top: 2px; }
.keys { border-collapse: collapse; font-size: .9rem; }
.keys td { padding: 4px 12px 4px 0; vertical-align: top; }
.keys td:first-child { white-space: nowrap; }
kbd {
  display: inline-block; min-width: 1.6em; text-align: center; font: 600 .8rem ui-monospace, Menlo, monospace;
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px; margin-right: 2px;
  background: var(--bg);
}
.codes { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: .82rem; color: var(--muted); }
.codes b { font-size: 1rem; }
code { font: .8rem ui-monospace, Menlo, monospace; }
.help-actions { display: flex; justify-content: space-between; margin-top: 18px; }

.sub { margin: -8px 0 0; font-size: .85rem; color: var(--rec); font-style: italic; }
.editor.is-rec #editor-text { border-color: color-mix(in srgb, var(--rec) 45%, var(--line)); }
.yearly { border-top: 1px solid var(--line); padding-top: 10px; display: flex; flex-direction: column; gap: 8px; font-size: .9rem; }
.yearly-item { display: flex; flex-direction: column; gap: 2px; }
button.link { border: 0; background: none; padding: 2px 0; color: var(--accent); text-align: left; text-decoration: underline; text-underline-offset: 2px; }
button.link:hover { background: none; filter: brightness(1.15); }

.import { width: min(680px, 94vw); max-height: 90vh; }
.import p { margin: 0 0 12px; font-size: .9rem; }
#import-file { font: inherit; margin-bottom: 8px; max-width: 100%; }
.import-preview { font-size: .9rem; }
.import-preview ul { padding-left: 20px; margin: 8px 0 12px; }
.import-preview li { margin-bottom: 4px; }
.import-preview .skipped { max-height: 9rem; overflow: auto; font-size: .8rem; color: var(--muted); }
.import-preview .warn { color: #c0392b; }
.import-preview .summary { font-size: .95rem; margin-top: 8px; }
#import-progress { width: 100%; margin: 8px 0; accent-color: var(--accent); }

.history-wrap:has(#history-btn[hidden]) { display: none; }
.history { margin-top: 8px; max-height: 16rem; overflow: auto; display: flex; flex-direction: column; gap: 8px; }
.hv { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: var(--bg); font-size: .85rem; }
.hv-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; color: var(--muted); font-size: .8rem; margin-bottom: 4px; }
.hv-text { max-height: 6rem; overflow: auto; }

.banner {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 8px 16px; font-size: .88rem;
  background: color-mix(in srgb, #e6a700 18%, var(--surface)); border-bottom: 1px solid color-mix(in srgb, #e6a700 45%, var(--line));
}
.banner span { flex: 1; min-width: 12rem; }
.banner.update { background: color-mix(in srgb, var(--accent) 16%, var(--surface)); border-bottom-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
.banner.offline { background: color-mix(in srgb, var(--muted) 16%, var(--surface)); border-bottom-color: var(--line); }
.banner button { padding: 4px 10px; font-size: .85rem; }
.banner button.icon { width: 28px; height: 28px; }

/* ── Floating bits ── */
.kb-hint, .toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 50;
  border-radius: 99px; padding: 8px 16px; box-shadow: var(--shadow);
}
.kb-hint { bottom: 24px; background: var(--ink); color: var(--bg); font: 600 1rem ui-monospace, Menlo, monospace; letter-spacing: .1em; }
.toast {
  bottom: 72px; background: var(--surface); border: 1px solid var(--line); font-size: .9rem; max-width: calc(100vw - 32px);
  display: flex; align-items: center; gap: 14px;
}
.toast[hidden] { display: none; }
.toast-btn { white-space: nowrap; border: 0; background: none; padding: 2px 4px; color: var(--accent); font-weight: 700; }
.toast-btn:hover { background: none; text-decoration: underline; }
.toast.error { border-color: #c0392b; color: #c0392b; }

/* ── Desktop / mobile switch (keep in sync with app.js: 760px) ── */
@media (min-width: 761px) { .mobile-only { display: none !important; } }
@media (max-width: 760px) {
  .desktop-only { display: none !important; }
  /* Extra room at the top so the title clears the iPhone clock / Dynamic Island area and its fade. */
  .bar { flex-wrap: wrap; padding: 10px 16px; gap: 8px; padding-top: calc(max(10px, env(safe-area-inset-top)) + 14px); }
  html.home-app .bar { padding-top: calc(max(24px, env(safe-area-inset-top, 0px)) + 14px); }
  .app-title { margin: 0; font-size: 1.2rem; flex: 1; }
  .app-title button { font: inherit; font-weight: 700; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; -webkit-tap-highlight-color: transparent;
    display: inline-flex; align-items: center; gap: 8px; }
  .title-icon { width: 28px; height: 28px; border-radius: 22.5%; box-shadow: 0 0 0 1px var(--line); }
  .tools { width: 100%; flex-wrap: wrap; }
  .search { flex: 1; min-width: 0; }
  #search { width: 100%; font-size: max(16px, 1rem); } /* 16px+ stops iOS zooming */
  .mobile-opts { display: flex; align-items: center; } /* sits at the right of the title row */
  .seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; padding: 2px; background: var(--bg); }
  .seg button { border: 0; background: transparent; padding: 4px 18px; font-size: 1.15rem; line-height: 1.2; font-weight: 700; color: var(--ink); border-radius: 7px; } /* both arrows full strength: both are tappable */
  .seg button.on { background: var(--accent); color: var(--on-accent); box-shadow: 0 1px 3px rgba(0,0,0,.18); } /* selected: filled green */
  dialog.editor {
    width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh;
    margin: 0; border-radius: 0; border: 0; padding: 16px;
    padding-top: calc(max(16px, env(safe-area-inset-top)) + 14px);
  }
  .editor form { height: 100%; }
  #editor-text { flex: 1; min-height: 8rem; font-size: max(16px, 1rem); }
  .editor-actions { flex-wrap: wrap; }
  .meta { width: 100%; flex-basis: 100%; }
}

/* ── Desktop: roomier day editor (phones already use the full screen) ── */
@media (min-width: 761px) {
  dialog.editor { width: min(880px, 94vw); max-height: 94vh; }
  #editor-text { min-height: max(20rem, 55vh); }
  .history { max-height: 40vh; }
}

/* Appearance choice in ? (all screen sizes) */
.theme-seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; padding: 2px; background: var(--bg); }
.theme-seg button { border: 0; background: transparent; padding: 5px 14px; font-size: .9rem; font-weight: 500; color: var(--ink); border-radius: 7px; box-shadow: none; }
.theme-seg button.on { background: var(--accent); color: var(--on-accent); font-weight: 600; }
