:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #eef1f8;
  --text: #1d2233;
  --muted: #687089;
  --line: #dfe3ee;
  --accent: #3b5bdb;
  --accent-soft: #e5eafc;
  --accent-text: #ffffff;
  --today: #3b5bdb;
  --danger: #c92a2a;
  --yes: #2b8a3e;
  --maybe: #e67700;
  --no: #868e96;
  --shadow: 0 6px 24px rgba(20, 30, 60, 0.12);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141c;
    --surface: #1b1e29;
    --surface-2: #252937;
    --text: #e8eaf2;
    --muted: #9aa1b8;
    --line: #2e3344;
    --accent: #7b93f5;
    --accent-soft: #2a3256;
    --accent-text: #0e1220;
    --today: #7b93f5;
    --danger: #ff7b7b;
    --yes: #63c27a;
    --maybe: #ffb347;
    --no: #9aa1b8;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  min-height: 100vh;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 4px;
  padding: calc(8px + env(safe-area-inset-top)) max(12px, calc((100% - 1076px) / 2)) 8px;
  background: var(--bg);
}
.topbar h1 {
  font-size: 1.25rem; font-weight: 700; margin: 0 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.spacer { flex: 1; }
.icon-btn {
  width: 36px; height: 36px; border: 0; border-radius: 50%;
  background: transparent; font-size: 1.6rem; line-height: 1; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); }
.pill-btn {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 999px; padding: 6px 12px; font-size: 0.9rem; font-weight: 600;
}
.avatar {
  width: 36px; height: 36px; border-radius: 50%; border: 0; margin-left: 6px;
  background: var(--muted); color: #fff; font-weight: 700; font-size: 0.95rem;
  flex: none;
}
body.view-upcoming #prev, body.view-upcoming #next { display: none; }

/* ---------- Tabs ---------- */
.tabs {
  display: flex; gap: 4px; margin: 0 auto 10px; padding: 4px; max-width: 1076px;
  width: calc(100% - 24px);
  background: var(--surface-2); border-radius: 12px;
}
.tabs button {
  flex: 1; border: 0; background: transparent; padding: 7px 0;
  border-radius: 9px; font-weight: 600; color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

main { padding: 0 12px; max-width: 1100px; margin: 0 auto; }
.loading, .empty { color: var(--muted); text-align: center; padding: 32px 0; }

/* ---------- Month grid ---------- */
.month {
  background: var(--surface); border-radius: var(--radius);
  overflow: hidden; border: 1px solid var(--line);
}
.weekdays, .grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); }
.weekdays div {
  text-align: center; font-size: 0.72rem; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em; padding: 8px 0 6px;
}
.day {
  border: 0; border-top: 1px solid var(--line); background: transparent;
  min-height: 58px; padding: 4px 2px 6px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  min-width: 0;
}
.day:not(:nth-child(7n+1)) { border-left: 1px solid var(--line); }
.day.other { color: var(--muted); opacity: .55; }
.day .num {
  width: 28px; height: 28px; line-height: 28px; border-radius: 50%;
  font-size: 0.9rem; font-weight: 600;
}
.day.today .num { background: var(--today); color: var(--accent-text); }
.day.selected { background: var(--accent-soft); }
.dots { display: flex; gap: 3px; flex-wrap: wrap; justify-content: center; max-width: 100%; }
.dot { width: 7px; height: 7px; border-radius: 50%; }
.more { font-size: 0.65rem; color: var(--muted); line-height: 7px; }
.chips { display: none; width: 100%; flex-direction: column; gap: 2px; }
.chip {
  display: block; font-size: 0.75rem; text-align: left; padding: 1px 6px;
  border-radius: 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  color: #fff;
}

@media (min-width: 720px) {
  .day { min-height: 104px; align-items: stretch; padding: 4px; }
  .day .num { align-self: flex-end; }
  .dots { display: none; }
  .chips { display: flex; }
}

/* ---------- Agenda lists ---------- */
.agenda { margin-top: 16px; }
.agenda h2 {
  font-size: 0.85rem; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); margin: 18px 4px 8px;
}
.agenda h2:first-child { margin-top: 0; }
.item {
  display: flex; gap: 12px; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px; margin-bottom: 8px; align-items: stretch;
}
.item .bar { width: 5px; border-radius: 3px; flex: none; }
.item .body { min-width: 0; flex: 1; }
.item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.item .meta { color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
.item .rsvp-mini { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }
.item .rsvp-mini .mine { font-weight: 700; }

/* ---------- Floating add button ---------- */
.fab {
  position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 6;
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text); font-size: 2rem; line-height: 1;
  box-shadow: var(--shadow);
}

/* ---------- Bottom sheet dialog ---------- */
.sheet {
  border: 0; padding: 0; color: var(--text); background: var(--surface);
  width: 100%; max-width: 560px; max-height: 92vh;
  margin: auto auto 0; border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow);
}
@media (min-width: 720px) { .sheet { margin: auto; border-radius: 20px; } }
.sheet::backdrop { background: rgba(10, 14, 30, 0.45); }
.sheet-inner { padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); }
.sheet-head { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 12px; }
.sheet-head h2 { margin: 0; font-size: 1.3rem; flex: 1; overflow-wrap: anywhere; }
.close { flex: none; }

.detail-row { display: flex; gap: 10px; margin: 10px 0; color: var(--text); }
.detail-row .ico { width: 22px; text-align: center; flex: none; }
.detail-row a { color: var(--accent); }
.notes { white-space: pre-wrap; overflow-wrap: anywhere; }
.byline { color: var(--muted); font-size: 0.85rem; margin-top: 14px; }

.rsvp-buttons { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 18px 0 10px; }
.rsvp-buttons button {
  border: 1.5px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 10px 4px; font-weight: 600;
}
.rsvp-buttons button[aria-pressed="true"][data-status="yes"] { border-color: var(--yes); background: color-mix(in srgb, var(--yes) 15%, transparent); color: var(--yes); }
.rsvp-buttons button[aria-pressed="true"][data-status="maybe"] { border-color: var(--maybe); background: color-mix(in srgb, var(--maybe) 15%, transparent); color: var(--maybe); }
.rsvp-buttons button[aria-pressed="true"][data-status="no"] { border-color: var(--no); background: color-mix(in srgb, var(--no) 15%, transparent); color: var(--no); }
.rsvp-group { margin: 8px 0; font-size: 0.9rem; }
.rsvp-group strong { display: inline-block; min-width: 92px; color: var(--muted); font-weight: 600; }
.person { display: inline-flex; align-items: center; gap: 5px; margin: 2px 10px 2px 0; }
.person i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }

.actions { display: flex; gap: 10px; margin-top: 20px; }
.btn {
  flex: 1; border: 0; border-radius: 12px; padding: 12px; font-weight: 700;
  background: var(--surface-2);
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .6; }

/* ---------- Forms ---------- */
.segmented { display: flex; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 14px; }
.segmented button { flex: 1; border: 0; background: transparent; padding: 7px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 0.85rem; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.field input[type="text"], .field input[type="date"], .field input[type="time"], .field textarea {
  width: 100%; border: 1.5px solid var(--line); border-radius: 10px; padding: 10px 12px;
  background: var(--bg); min-height: 44px; min-width: 0; max-width: 100%;
  -webkit-appearance: none; appearance: none;
}
.field textarea { min-height: 80px; resize: vertical; }
.field input:focus, .field textarea:focus { outline: none; border-color: var(--accent); }
.row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.check { display: flex; align-items: center; gap: 10px; margin: 4px 0 14px; font-weight: 500; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.form-error { color: var(--danger); font-weight: 600; margin: 4px 0 0; min-height: 1.4em; }

.swatches { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 6px; }
.swatches label { position: relative; }
.swatches input { position: absolute; opacity: 0; inset: 0; margin: 0; }
.swatches span {
  display: block; width: 38px; height: 38px; border-radius: 50%;
  border: 3px solid transparent; box-shadow: inset 0 0 0 3px var(--surface);
  background-clip: padding-box;
}
.swatches input:checked + span { border-color: var(--text); }
.swatches input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Toast & full-screen notices ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 20;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  box-shadow: var(--shadow); max-width: calc(100% - 32px); font-weight: 600;
}
.notice {
  max-width: 420px; margin: 40px auto; text-align: center; background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 28px 20px;
}
.notice h2 { margin-top: 0; }
.notice p { color: var(--muted); }
