:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --primary: #1d4ed8;
  --primary-text: #ffffff;
  --overdue: #dc2626;
  --overdue-bg: #fef2f2;
  --tomorrow: #b45309;
  --acc: #1d4ed8;
  --acc-bg: #dbeafe;
  --p1: #dc2626;
  --p2: #d97706;
  --p3: #ca8a04;
  --p4: #6b7280;
  --done: #9ca3af;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

/* Several components below (.sheet-backdrop, .toast, .filter-row, .sheet-danger-actions)
   set their own `display` value directly on an element that's also toggled via the
   `hidden` attribute/property. An author-stylesheet `display` rule always beats the
   browser's built-in `[hidden] { display: none }` default regardless of specificity or
   source order, so without this line those elements never actually hide -- which is
   exactly what broke the Add-task sheet's Cancel button. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  /* A single overflowing element ANYWHERE on the page -- including inside a
     position:fixed overlay like the sheet -- can force the browser's layout viewport
     wider than the screen, shifting the entire page (header included) sideways. This is
     the backstop: even if something overflows again in the future, it clips here instead
     of shifting the whole page. The real fixes are below (min-width:0 on shrinking flex
     children), this is defense in depth, not a substitute for them. */
  overflow-x: hidden;
}

body { padding-bottom: calc(90px + var(--safe-bottom)); }

/* ---- Login page ---- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}
.login-card {
  background: var(--card);
  border-radius: 16px;
  padding: 32px 24px;
  max-width: 360px;
  width: 100%;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  text-align: center;
}
.login-card h1 { margin: 0 0 8px; font-size: 1.4rem; }
.login-sub { color: var(--text-muted); margin: 0 0 20px; }
#login-form { display: flex; flex-direction: column; gap: 12px; }
#passcode {
  font-size: 1.5rem;
  text-align: center;
  letter-spacing: 0.2em;
  padding: 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
}
.login-error { color: var(--overdue); margin-top: 14px; }
.login-help { margin-top: 18px; font-size: 0.85rem; }
.login-help a { color: var(--text-muted); }

/* ---- App shell ---- */
.topbar {
  background: var(--primary);
  color: var(--primary-text);
  padding: 16px 16px 12px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { margin: 0 0 12px; font-size: 1.25rem; }

.list-toggle, .filter-row, .segmented {
  display: flex;
  gap: 8px;
}
.filter-row {
  padding: 10px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.show-all-row {
  padding: 8px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
}
.show-all-label { display: flex; align-items: center; gap: 8px; }
.show-all-label input { width: 18px; height: 18px; }

.toggle-btn, .filter-btn {
  flex: 1;
  min-width: 0; /* same overflow class as the sheet's buttons -- cheap insurance here too */
  padding: 12px;
  border: none;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.toggle-btn {
  background: rgba(255,255,255,0.15);
  color: var(--primary-text);
}
.toggle-btn.active { background: #ffffff; color: var(--primary); }

.filter-btn {
  background: var(--bg);
  color: var(--text);
}
.filter-btn.active { background: var(--acc-bg); color: var(--acc); }

/* ---- Sections ---- */
.sections { padding: 8px 12px 20px; }

/* ---- Shopping list ---- */
.shopping-view { padding: 12px 12px 20px; }

.shopping-add-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.shopping-add-row input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  font-size: 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
}

.shopping-list { list-style: none; margin: 0; padding: 0; }

.shopping-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 10px 10px 8px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.shopping-item.checked { opacity: 0.6; }

.shopping-item.checked .check-tap { background: var(--done); border-color: var(--done); color: white; }

.shopping-item-name { flex: 1; min-width: 0; font-size: 1rem; word-break: break-word; }
.shopping-item.checked .shopping-item-name { text-decoration: line-through; color: var(--text-muted); }

.shopping-item-delete {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}

#shopping-clear-btn { width: 100%; margin-top: 4px; }

/* ---- Calendar Review ---- */
.calendar-view { padding: 12px 12px 20px; }
.bookings-view { padding: 12px 12px 20px; }

.calendar-subnav { display: flex; gap: 8px; margin-bottom: 16px; }
.calendar-subview-btn.active { background: var(--acc-bg); color: var(--acc); }

.calendar-sync-error {
  background: var(--overdue-bg);
  color: var(--overdue);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.85rem;
  margin-bottom: 12px;
}

.calendar-list { list-style: none; margin: 0 0 8px; padding: 0; }

/* Compact by design: this card used to stack badge/time/title/buffer-labels/buttons as
   five separate full-size blocks, which is what made every event feel oversized. Badge
   and time now share one line, buffer fields are inline label+input pairs instead of
   stacked with their own row each, and the buttons are sized for a dense list, not a
   48px mobile touch target. */
.calendar-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.calendar-item-meta { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.calendar-item-when { font-size: 0.78rem; color: var(--text-muted); }
.calendar-item-title { font-size: 0.92rem; font-weight: 600; word-break: break-word; }
.badge-cal { background: var(--acc-bg); color: var(--acc); }

.calendar-item-actions { display: flex; gap: 6px; }
.calendar-item-actions button {
  flex: 1;
  padding: 7px 8px;
  min-height: 34px;
  font-size: 0.82rem;
  border-radius: 8px;
  font-weight: 600;
}

.calendar-buffer-fields { display: flex; gap: 12px; }
.calendar-buffer-field {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.calendar-buffer-field input {
  font: inherit;
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  width: 56px;
  flex: none;
}

.calendar-item-blocked { font-size: 0.85rem; font-weight: 600; color: var(--acc); }
.calendar-item-buffer-applied { font-size: 0.78rem; color: var(--text-muted); margin-top: 2px; }

.app-footer { text-align: center; padding: 8px 20px 24px; }
.app-footer a { color: var(--text-muted); font-size: 0.82rem; text-decoration: none; }
.app-footer a:hover { text-decoration: underline; }

.section {
  margin-top: 18px;
}
.section-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 4px 4px 8px;
  font-size: 0.95rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}
.section-header .count {
  font-weight: 500;
  text-transform: none;
  letter-spacing: normal;
}
.section-header.overdue { color: var(--overdue); }

.date-group-header {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-muted);
  margin: 12px 4px 6px;
  text-transform: none;
  letter-spacing: normal;
}
.date-group-header:first-of-type { margin-top: 6px; }

.section-empty {
  color: var(--text-muted);
  font-size: 0.9rem;
  padding: 6px 4px 0;
}

.task-card {
  background: var(--card);
  border-radius: 12px;
  padding: 12px 12px 12px 10px;
  margin-bottom: 8px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  border: 1px solid var(--border);
}
.task-card.overdue { border-color: var(--overdue); background: var(--overdue-bg); }
.task-card.done { opacity: 0.6; }

.check-tap {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--card);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  color: transparent;
  margin-top: 2px;
}
.task-card.done .check-tap { background: var(--done); border-color: var(--done); color: white; }
.task-card.overdue .check-tap { border-color: var(--overdue); }

.task-body { flex: 1; min-width: 0; }
.task-name { font-size: 1rem; font-weight: 600; word-break: break-word; }
.task-card.done .task-name { text-decoration: line-through; color: var(--text-muted); }

.task-meta {
  margin-top: 4px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.due-label.overdue { color: var(--overdue); font-weight: 600; }
.due-label.tomorrow { color: var(--tomorrow); font-weight: 600; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
}
.badge-acc { background: var(--acc-bg); color: var(--acc); }
.badge-p1 { background: #fee2e2; color: var(--p1); }
.badge-p2 { background: #fef3c7; color: var(--p2); }
.badge-p3 { background: #fef9c3; color: var(--p3); }
.badge-p4 { background: #f3f4f6; color: var(--p4); }

.task-notes {
  margin-top: 4px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* ---- FAB ---- */
.fab {
  position: fixed;
  right: 20px;
  bottom: calc(20px + var(--safe-bottom));
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  font-size: 2rem;
  line-height: 1;
  border: none;
  box-shadow: 0 4px 14px rgba(0,0,0,0.25);
  cursor: pointer;
  z-index: 20;
}

/* ---- Toast ---- */
.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(90px + var(--safe-bottom));
  background: #1a1d23;
  color: white;
  padding: 12px 16px;
  border-radius: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  font-size: 0.9rem;
  z-index: 30;
}
.toast button {
  background: none;
  border: none;
  color: #93c5fd;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  flex: none;
}

/* ---- Sheet (add/edit) ---- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  max-width: 100vw;
  overflow-x: hidden;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: flex-end;
  z-index: 40;
}
.sheet {
  background: var(--card);
  width: 100%;
  max-width: 100%;
  max-height: 88vh;
  overflow-x: hidden;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(20px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-sizing: border-box;
}
.sheet h2 { margin: 0; }
.sheet label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--text-muted);
  min-width: 0;
}
.field-row {
  display: flex;
  gap: 12px;
  min-width: 0; /* .field-row is itself a flex item of .sheet's column layout */
}
.field-row label { flex: 1; min-width: 0; }
/* Below ~400px, a date input and a time input side by side get too little room each
   even with min-width:0 letting them shrink -- their native browser-drawn segments
   (day/month/year, hour/minute/AM-PM) have a real floor they won't render smaller than.
   Stack them instead of squeezing both into half-width. */
@media (max-width: 400px) {
  .field-row { flex-direction: column; }
}
.sheet input[type="text"],
.sheet input[type="date"],
.sheet input[type="time"],
.sheet input[type="number"],
.sheet textarea {
  font-size: 1rem;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-family: inherit;
  color: var(--text);
  width: 100%;
  min-width: 0;
}
.field-label { display: block; font-size: 0.85rem; color: var(--text-muted); margin-bottom: 6px; }

.seg-btn, .preset-btn {
  flex: 1;
  min-width: 0; /* without this, "365 days"/"Personal" etc. hold their full min-content
                   width and force the row (and the whole page, via the fixed-position
                   sheet) to overflow horizontally instead of shrinking to fit */
  padding: 10px 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
  font-weight: 600;
  font-size: 0.85rem;
  min-height: 44px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.seg-btn.active { background: var(--acc-bg); border-color: var(--acc); color: var(--acc); }

.recurrence-presets {
  display: flex;
  gap: 8px;
  margin-top: -6px;
}
.preset-btn { flex: 1; font-size: 0.78rem; padding: 8px 4px; min-height: 38px; }

.sheet-actions, .sheet-danger-actions {
  display: flex;
  gap: 10px;
  margin-top: 6px;
}
.btn-primary, .btn-secondary, .btn-danger {
  flex: 1;
  padding: 14px;
  border-radius: 12px;
  border: none;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  min-height: 48px;
}
.btn-primary { background: var(--primary); color: white; }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: #fee2e2; color: var(--overdue); }

/* ---- All-day + time controls in the add/edit sheet ---- */
/* Scoped under .sheet to outrank `.sheet label`, which is flex-direction:column for the
   stacked "caption above field" pattern -- a checkbox row is the one label that needs to
   run horizontally instead. */
.sheet .all-day-label {
  flex-direction: row;
  align-items: center;
  gap: 10px;
  font-size: 1rem;
  color: var(--text);
}
.sheet .all-day-label input { width: 20px; height: 20px; flex: none; }

.time-field { flex: 1; min-width: 0; }
.time-control { display: flex; align-items: center; gap: 6px; }
.time-btn {
  flex: 1;
  min-width: 0;
  font-size: 1rem;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  min-height: 48px;
}
.time-btn:disabled { background: var(--bg); color: var(--text-muted); cursor: not-allowed; }
.time-clear {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 16px;
  border: none;
  background: var(--bg);
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}

.all-day-badge {
  display: inline-block;
  background: var(--acc-bg);
  color: var(--primary);
  font-size: 11px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 10px;
}

/* ---- Time picker (static/js/timePicker.js) ----
   Sits above .sheet-backdrop (z-index 40), since it opens from inside the open sheet. */
.tp-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}
.tp-card {
  background: var(--card);
  border-radius: 20px;
  padding: 20px;
  width: 100%;
  max-width: 340px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
}
.tp-title { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 12px; }

.tp-header { display: flex; align-items: center; gap: 6px; margin-bottom: 16px; }
.tp-seg {
  font-size: 2.6rem;
  font-weight: 400;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  padding: 8px 10px;
  border-radius: 10px;
  border: none;
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  font-family: inherit;
}
.tp-seg-active { background: var(--acc-bg); color: var(--primary); }
.tp-colon { font-size: 2.2rem; color: var(--text-muted); }
.tp-meridiem { display: flex; flex-direction: column; margin-left: auto; }
.tp-ampm {
  padding: 7px 14px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}
.tp-ampm:first-child { border-radius: 8px 8px 0 0; }
.tp-ampm:last-child { border-radius: 0 0 8px 8px; border-top: none; }
.tp-ampm-active { background: var(--acc-bg); color: var(--primary); border-color: var(--primary); }

.tp-dial {
  display: block;
  width: 100%;
  max-width: 260px;
  margin: 0 auto;
  /* The dial is drag-driven; without this, a drag scrolls the page on touch instead. */
  touch-action: none;
  user-select: none;
}
.tp-face { fill: var(--bg); }
.tp-hand { stroke: var(--primary); stroke-width: 2; }
.tp-knob { fill: var(--primary); }
.tp-hub { fill: var(--primary); }
.tp-num { font-size: 17px; fill: var(--text); font-family: inherit; }
.tp-num-selected { fill: var(--primary-text); font-weight: 700; }

.tp-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.tp-btn {
  padding: 11px 18px;
  border-radius: 10px;
  border: none;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  min-height: 44px;
}
.tp-btn-text { background: transparent; color: var(--text-muted); }
.tp-btn-primary { background: var(--primary); color: var(--primary-text); }

/* ==========================================================================
   Desktop layout (>= 900px)

   Everything above this point is the original mobile design, untouched --
   phones keep exactly the layout they always had. Below this width, none of
   this applies at all. This block used to just be `body { max-width: 480px;
   margin: 0 auto; }`, which on an actual desktop monitor produced a tiny
   phone-width column floating in a sea of empty background -- not a desktop
   layout, just a mobile one with padding. This replaces that with real
   desktop treatment: a wide centered shell, an inline top nav instead of
   full-width stacked tab buttons, multi-column card grids so several
   tasks/events are visible at once instead of one long single column, and a
   centered dialog instead of a mobile bottom-sheet for the add/edit form.
   ========================================================================== */
@media (min-width: 900px) {
  body { padding-bottom: 40px; }

  .login-body { padding: 40px; }

  /* ---- Shell ---- */
  .topbar {
    position: static;
    max-width: 1100px;
    margin: 0 auto;
    padding: 24px 24px 0;
    background: transparent;
    color: var(--text);
    border-bottom: 1px solid var(--border);
  }
  .topbar h1 { margin: 0 0 16px; font-size: 1.6rem; color: var(--text); }

  .list-toggle {
    gap: 4px;
    padding-bottom: 0;
  }
  .toggle-btn {
    flex: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: 10px 10px 0 0;
    padding: 12px 22px;
    min-height: auto;
  }
  .toggle-btn.active {
    background: var(--card);
    color: var(--primary);
    box-shadow: inset 0 2px 0 var(--primary);
  }

  .filter-row, .show-all-row {
    max-width: 1100px;
    margin: 0 auto;
    background: transparent;
    border-bottom: none;
    padding-left: 24px;
    padding-right: 24px;
  }
  .filter-row { gap: 10px; padding-top: 16px; }
  .filter-btn { flex: none; padding: 8px 18px; }

  .sections, .shopping-view, .calendar-view, .bookings-view {
    max-width: 1100px;
    margin: 0 auto;
    padding: 20px 24px 40px;
  }

  #bookings-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 8px;
    align-items: start;
  }

  /* ---- Multi-column card grids ---- */
  /* Section headers and date-group headers stay as direct children mixed in with the
     cards themselves (see renderSection/renderDateGroupedSection in app.js) -- spanning
     them across every column is what makes a header still read as "a header" instead of
     becoming a grid cell of its own. */
  .section {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 10px;
    align-items: start;
  }
  .section-header, .date-group-header { grid-column: 1 / -1; }
  .task-card { margin-bottom: 0; }

  .shopping-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 10px;
  }
  .shopping-item { margin-bottom: 0; }
  .shopping-add-row { max-width: 480px; }
  #shopping-clear-btn { max-width: 240px; }

  .calendar-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 8px;
    align-items: start;
  }
  .calendar-item { margin-bottom: 0; }
  .calendar-subnav, .calendar-sync-error { max-width: 1100px; }

  /* ---- Add/edit sheet: a centered dialog, not a mobile bottom-sheet ---- */
  .sheet-backdrop { align-items: center; justify-content: center; }
  .sheet {
    width: 100%;
    max-width: 520px;
    max-height: 85vh;
    border-radius: 20px;
  }
}
