:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f2f7;
  --text: #14161c;
  --muted: #6b7180;
  --faint: #a3a8b4;
  --line: #e7e9ef;
  --line-strong: #d9dce4;
  --accent: #4f7cff;
  --accent-ink: #ffffff;
  --danger: #e5484d;
  --shadow: 0 1px 2px rgba(20, 22, 28, .05), 0 8px 24px rgba(20, 22, 28, .06);
  --shadow-lg: 0 -4px 40px rgba(20, 22, 28, .14);
  --radius: 16px;
  --hour: 52px;
  --gutter: 46px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --text: #eef0f4;
    --muted: #979dab;
    --faint: #5d6370;
    --line: #252931;
    --line-strong: #323743;
    --accent: #6d93ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    --shadow-lg: 0 -4px 40px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Inter, Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
input, select, textarea { -webkit-appearance: none; appearance: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }
svg { display: block; }

/* ---------- auth screens ---------- */
.auth {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px;
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface); border-radius: 24px;
  padding: 32px 24px 24px; box-shadow: var(--shadow);
}
.auth-logo { width: 56px; height: 56px; margin-bottom: 20px; }
.auth h1 { font-size: 24px; letter-spacing: -.02em; margin: 0 0 6px; }
.auth p.sub { margin: 0 0 24px; color: var(--muted); }
.auth .btn-primary { width: 100%; margin-top: 8px; }

/* ---------- form controls ---------- */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.input {
  width: 100%; height: 46px; padding: 0 14px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid transparent; outline: none;
  transition: border-color .15s, background .15s;
}
textarea.input { height: auto; min-height: 84px; padding: 12px 14px; resize: vertical; }
.input:focus { border-color: var(--accent); background: var(--surface); }
select.input {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%23888' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 14px center; padding-right: 36px;
}
.input-title { font-size: 20px; font-weight: 600; height: 54px; background: transparent; padding: 0 2px; border-radius: 0; border-bottom: 1px solid var(--line); }
.input-title:focus { background: transparent; border-color: transparent; border-bottom-color: var(--accent); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }

.btn {
  height: 46px; padding: 0 18px; border-radius: 12px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: transform .08s, opacity .15s, background .15s;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: var(--surface-2); }
.btn-danger { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.btn[disabled] { opacity: .5; pointer-events: none; }
.error-msg { color: var(--danger); font-size: 14px; min-height: 20px; margin: 4px 0 0; }

.seg {
  display: flex; background: var(--surface-2); border-radius: 11px; padding: 3px; gap: 2px;
}
.seg button {
  flex: 1; height: 32px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--muted);
  transition: background .15s, color .15s;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.switch { position: relative; width: 48px; height: 28px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.switch i { position: absolute; inset: 0; border-radius: 99px; background: var(--line-strong); transition: background .2s; pointer-events: none; }
.switch i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.2); transition: transform .2s; }
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(20px); }
.toggle-row { display: flex; align-items: center; justify-content: space-between; min-height: 46px; margin-bottom: 8px; font-weight: 500; }

/* ---------- app shell ---------- */
.app { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  background: var(--bg); padding: calc(var(--safe-t) + 10px) 16px 8px; flex: none;
  position: relative; z-index: 5;
}
.topbar-row { display: flex; align-items: center; gap: 8px; }
.title-btn { display: flex; align-items: baseline; gap: 6px; font-size: 24px; font-weight: 700; letter-spacing: -.025em; padding: 4px 0; min-width: 0; }
.title-btn .yr { color: var(--muted); font-weight: 500; }
.title-btn span { white-space: nowrap; }
.spacer { flex: 1; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; color: var(--text);
  transition: background .15s;
}
.icon-btn:active { background: var(--surface-2); }
.today-btn { height: 32px; padding: 0 12px; border-radius: 99px; border: 1px solid var(--line-strong); font-size: 13px; font-weight: 600; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; flex: none;
  color: #fff; font-size: 13px; font-weight: 700; letter-spacing: -.01em;
}
.avatar.sm { width: 20px; height: 20px; font-size: 10px; }
.avatar.lg { width: 44px; height: 44px; font-size: 17px; }
.controls { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.controls .seg { flex: 1; max-width: 300px; }
.nav-arrows { display: flex; margin-left: auto; }

.people {
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; margin: 10px -16px 0; padding: 0 16px 2px;
}
.people::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 12px 0 4px;
  border-radius: 99px; background: var(--surface); border: 1px solid var(--line); font-size: 14px; font-weight: 600;
  transition: opacity .15s, background .15s, border-color .15s;
}
.chip.all { padding: 0 14px; }
.chip .avatar { width: 26px; height: 26px; font-size: 11px; }
.chip[aria-pressed="false"] { opacity: .45; }
.chip[aria-pressed="false"] .avatar { background: var(--faint) !important; }
.chip[aria-pressed="true"]:not(.all) { border-color: color-mix(in srgb, var(--c) 45%, transparent); background: color-mix(in srgb, var(--c) 10%, var(--surface)); }

.main { flex: 1; min-height: 0; position: relative; display: flex; flex-direction: column; }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--safe-b) + 20px); width: 58px; height: 58px; border-radius: 20px;
  background: var(--accent); color: #fff; display: grid; place-items: center; z-index: 20;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 45%, transparent);
  transition: transform .12s;
}
.fab:active { transform: scale(.94); }

/* ---------- time grid (day / week) ---------- */
.tg { flex: 1; min-height: 0; display: flex; flex-direction: column; background: var(--surface); border-top: 1px solid var(--line); }
.tg-head { display: flex; flex: none; border-bottom: 1px solid var(--line); }
.tg-head .gut { width: var(--gutter); flex: none; font-size: 10px; color: var(--faint); display: flex; align-items: flex-end; justify-content: center; padding-bottom: 6px; }
.tg-cols { flex: 1; display: grid; grid-template-columns: repeat(var(--n), 1fr); min-width: 0; }
.tg-day { text-align: center; padding: 8px 0 6px; }
.tg-day .dn { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.tg-day .dd { width: 32px; height: 32px; margin: 2px auto 0; display: grid; place-items: center; border-radius: 50%; font-size: 17px; font-weight: 600; }
.tg-day.today .dd { background: var(--accent); color: #fff; }
.tg-day.today .dn { color: var(--accent); }
.tg-allday { display: flex; flex: none; border-bottom: 1px solid var(--line); max-height: 96px; overflow-y: auto; }
.tg-allday .gut { width: var(--gutter); flex: none; font-size: 10px; color: var(--faint); text-align: center; padding-top: 6px; }
.tg-allday .tg-cols > div { padding: 3px 2px; display: flex; flex-direction: column; gap: 2px; min-width: 0; border-left: 1px solid var(--line); }
.tg-body { flex: 1; overflow-y: auto; overscroll-behavior: contain; position: relative; }
.tg-inner { display: flex; position: relative; height: calc(var(--hour) * 24); }
.tg-hours { width: var(--gutter); flex: none; position: relative; }
.tg-hours span { position: absolute; right: 8px; transform: translateY(-50%); font-size: 11px; color: var(--faint); font-variant-numeric: tabular-nums; }
.tg-grid { flex: 1; position: relative; display: grid; grid-template-columns: repeat(var(--n), 1fr); min-width: 0;
  background-image: linear-gradient(var(--line) 1px, transparent 1px);
  background-size: 100% var(--hour);
}
.tg-col { position: relative; border-left: 1px solid var(--line); min-width: 0; }
.tg-col.today { background: color-mix(in srgb, var(--accent) 4%, transparent); }
.now-line { position: absolute; left: 0; right: 0; height: 2px; background: var(--danger); z-index: 3; pointer-events: none; }
.now-line::before { content: ""; position: absolute; left: -5px; top: -4px; width: 10px; height: 10px; border-radius: 50%; background: var(--danger); }

.ev {
  position: absolute; border-radius: 7px; padding: 3px 6px; overflow: hidden; z-index: 2;
  display: flex; flex-direction: column; justify-content: flex-start; align-items: stretch;
  background: color-mix(in srgb, var(--c) 16%, var(--surface));
  border-left: 3px solid var(--c); color: var(--text); text-align: left;
  font-size: 12px; line-height: 1.25; box-shadow: 0 0 0 1px var(--surface);
}
.ev .t { font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; }
.ev .s { color: var(--muted); font-size: 11px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev.busy, .pill.busy, .agenda-item.busy .bar {
  background:
    repeating-linear-gradient(135deg, color-mix(in srgb, var(--c) 18%, transparent) 0 6px, transparent 6px 12px),
    color-mix(in srgb, var(--c) 7%, var(--surface));
}
.ev .dots { position: absolute; right: 4px; bottom: 4px; display: flex; gap: 2px; }
.ev .dots i, .pill .dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--d); }
.week .ev { padding: 2px 3px; font-size: 10.5px; border-left-width: 2px; border-radius: 5px; }
.week .ev .s { display: none; }
.week .ev .t { overflow-wrap: normal; word-break: normal; hyphens: auto; }
.pill {
  display: flex; align-items: center; gap: 4px; height: 22px; border-radius: 6px; padding: 0 6px; font-size: 11.5px; font-weight: 600;
  background: color-mix(in srgb, var(--c) 18%, var(--surface)); border-left: 3px solid var(--c);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: left; width: 100%;
}
.pill span { overflow: hidden; text-overflow: ellipsis; }
.week .pill { font-size: 10px; padding: 0 3px; border-left-width: 2px; }

/* ---------- month ---------- */
.month { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.mgrid { background: var(--surface); border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.mgrid-head { display: grid; grid-template-columns: repeat(7, 1fr); border-bottom: 1px solid var(--line); }
.mgrid-head div { text-align: center; font-size: 11px; font-weight: 600; color: var(--muted); padding: 8px 0; text-transform: uppercase; letter-spacing: .04em; }
.mgrid-body { display: grid; grid-template-columns: repeat(7, 1fr); }
.mcell {
  min-height: 58px; padding: 5px 2px 6px; display: flex; flex-direction: column; align-items: center; gap: 4px;
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); min-width: 0; position: relative;
}
.mcell:nth-child(7n) { border-right: 0; }
.mcell:nth-last-child(-n + 7) { border-bottom: 0; }
.mcell .num { width: 28px; height: 28px; display: grid; place-items: center; border-radius: 50%; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.mcell.out .num { color: var(--faint); font-weight: 500; }
.mcell.today .num { background: var(--accent); color: #fff; }
.mcell.sel { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.mcell.sel:not(.today) .num { box-shadow: inset 0 0 0 2px var(--accent); color: var(--accent); }
.mdots { display: flex; flex-wrap: wrap; justify-content: center; gap: 3px; max-width: 34px; }
.mdots i { width: 6px; height: 6px; border-radius: 50%; background: var(--c); }
.mdots i.busy { background: transparent; box-shadow: inset 0 0 0 1.5px var(--c); }
.mdots em { font-style: normal; font-size: 9px; color: var(--muted); line-height: 6px; }
.mbars { display: none; width: 100%; flex-direction: column; gap: 2px; padding: 0 3px; }
.mbars .more { font-size: 11px; color: var(--muted); padding-left: 4px; }

.agenda { padding: 18px 16px calc(var(--safe-b) + 100px); }
.agenda h3 { margin: 0 0 12px; font-size: 15px; font-weight: 700; display: flex; align-items: baseline; gap: 8px; }
.agenda h3 small { color: var(--muted); font-weight: 500; font-size: 13px; }
.agenda-list { display: flex; flex-direction: column; gap: 8px; }
.agenda-item {
  display: flex; gap: 12px; align-items: stretch; background: var(--surface); border-radius: 14px; padding: 12px 14px;
  text-align: left; box-shadow: var(--shadow); width: 100%;
}
.agenda-item .bar { width: 4px; border-radius: 4px; background: var(--c); flex: none; }
.agenda-item.busy .bar { width: 6px; }
.agenda-item .when { min-width: 52px; white-space: nowrap; flex: none; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.agenda-item .when small { display: block; color: var(--muted); font-weight: 500; }
.agenda-item .what { flex: 1; min-width: 0; }
.agenda-item .what b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agenda-item .what small { color: var(--muted); font-size: 13px; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avstack { display: flex; align-items: center; }
.avstack .avatar { box-shadow: 0 0 0 2px var(--surface); }
.avstack .avatar + .avatar { margin-left: -6px; }
.empty { color: var(--muted); text-align: center; padding: 28px 0; font-size: 14px; }
.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); vertical-align: middle; }

/* ---------- sheets ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(10, 12, 16, .4); z-index: 50; opacity: 0; transition: opacity .22s;
  display: flex; align-items: flex-end; justify-content: center;
}
.overlay.open { opacity: 1; }
.sheet {
  width: 100%; max-width: 560px; max-height: calc(100dvh - var(--safe-t) - 24px); background: var(--surface);
  border-radius: 22px 22px 0 0; box-shadow: var(--shadow-lg); display: flex; flex-direction: column;
  transform: translateY(100%); transition: transform .28s cubic-bezier(.2, .8, .2, 1);
}
.overlay.open .sheet { transform: none; }
.sheet-grip { width: 38px; height: 5px; border-radius: 99px; background: var(--line-strong); margin: 8px auto 0; flex: none; }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px 4px 20px; flex: none; }
.sheet-head h2 { font-size: 18px; margin: 0; flex: 1; letter-spacing: -.01em; }
.sheet-body { padding: 8px 20px calc(var(--safe-b) + 20px); overflow-y: auto; overscroll-behavior: contain; }
.sheet-actions { display: flex; gap: 10px; margin-top: 18px; }
.sheet-actions .btn { flex: 1; }
@media (min-width: 700px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 22px; transform: translateY(24px) scale(.98); opacity: 0; transition: transform .22s, opacity .22s; }
  .overlay.open .sheet { opacity: 1; }
  .sheet-grip { display: none; }
  .sheet-head { padding-top: 16px; }
}

.pick-people { display: flex; flex-wrap: wrap; gap: 8px; }
.pick-people .chip[aria-pressed="false"] { opacity: .6; }
.section-label { font-size: 13px; font-weight: 600; color: var(--muted); margin: 18px 0 8px; }
.section-label:first-child { margin-top: 4px; }
.hint { font-size: 13px; color: var(--muted); margin: -4px 0 12px; }

.list { background: var(--surface-2); border-radius: 14px; overflow: hidden; }
.list-item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; width: 100%; text-align: left; min-height: 56px; }
.list-item + .list-item { border-top: 1px solid var(--line); }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .grow b { display: block; font-weight: 600; }
.list-item .grow small { color: var(--muted); font-size: 13px; }
.list-item .chev { color: var(--faint); }
.list-item.danger { color: var(--danger); }

.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatches button { width: 34px; height: 34px; border-radius: 50%; background: var(--c); position: relative; }
.swatches button[aria-pressed="true"]::after { content: ""; position: absolute; inset: -4px; border-radius: 50%; border: 2px solid var(--c); }

.detail-title { font-size: 22px; font-weight: 700; letter-spacing: -.02em; margin: 6px 0 4px; display: flex; gap: 10px; align-items: flex-start; }
.detail-title i { width: 14px; height: 14px; border-radius: 4px; background: var(--c); flex: none; margin-top: 8px; }
.detail-row { display: flex; gap: 12px; align-items: flex-start; padding: 10px 0; color: var(--text); }
.detail-row svg { color: var(--muted); flex: none; margin-top: 1px; }
.detail-row .people-list { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.detail-row .people-list > span { display: inline-flex; align-items: center; gap: 6px; }
.notes { white-space: pre-wrap; word-break: break-word; }

.toast-show { opacity: 1 !important; transform: translate(-50%, 0) !important; }
#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 92px); transform: translate(-50%, 10px); opacity: 0; z-index: 100;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 500;
  transition: opacity .2s, transform .2s; pointer-events: none; max-width: calc(100% - 32px);
}

/* ---------- wider screens ---------- */
@media (min-width: 700px) {
  :root { --hour: 56px; --gutter: 56px; }
  .topbar { padding-left: 24px; padding-right: 24px; }
  .people { margin-left: -24px; margin-right: -24px; padding-left: 24px; padding-right: 24px; }
  .mcell { min-height: 108px; align-items: stretch; padding: 4px 0; }
  .mcell .num { margin-left: 4px; width: 26px; height: 26px; font-size: 13px; }
  .mdots { display: none; }
  .mbars { display: flex; }
  .week .ev { font-size: 12px; padding: 3px 5px; }
  .week .ev .s { display: block; }
  .week .pill { font-size: 11.5px; padding: 0 6px; }
  .main-split { flex-direction: row; }
  .agenda { max-width: 760px; margin: 0 auto; width: 100%; }
}
@media (min-width: 1100px) {
  .month { display: grid; grid-template-columns: 1fr 360px; overflow: hidden; }
  .month .mgrid { overflow-y: auto; border-right: 1px solid var(--line); }
  .month .agenda { overflow-y: auto; padding-top: 18px; }
}

.howto { background: var(--surface-2); border-radius: 14px; padding: 12px 14px; margin-bottom: 16px; font-size: 14px; }
.howto summary { font-weight: 600; cursor: pointer; }
.howto ol { margin: 10px 0 8px; padding-left: 20px; }
.howto li { margin-bottom: 4px; }
.howto p { margin: 8px 0 0; color: var(--muted); font-size: 13px; }
