:root {
  --bg: #ffffff;
  --bg2: #f2f2f7;
  --page: #f2f2f7;
  --card: #ffffff;
  --text: #111114;
  --muted: #6e6e76;
  --line: #e3e3e8;
  --line-soft: #efeff3;
  --accent: #0a66ff;
  --danger: #e5383b;
  --now: #ea4335;
  --gym: #ff6a2b;
  --focus: #6d5dfc;
  --ok: #24a148;
  --hour: 52px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.16);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg2: #2c2c2e;
    --page: #000000;
    --card: #1c1c1e;
    --text: #f5f5f7;
    --muted: #98989f;
    --line: #2e2e31;
    --line-soft: #1d1d1f;
    --accent: #3d8bff;
    --shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, select, textarea { font: inherit; font-size: 16px; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

/* Login */
.login {
  min-height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  padding: 24px; padding-top: max(24px, env(safe-area-inset-top));
  text-align: center; max-width: 380px; margin: 0 auto;
}
.login-icon { width: 76px; height: 76px; border-radius: 18px; }
.login h1 { margin: 4px 0 0; font-size: 28px; letter-spacing: -0.02em; }
.login p { margin: 0; color: var(--muted); }
.login input {
  width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg2);
}
.btn { width: 100%; padding: 13px; border-radius: 12px; font-weight: 600; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: var(--card); color: var(--danger); }
.error { color: var(--danger); font-size: 14px; min-height: 1em; margin: 0; }

/* App shell */
.shell { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  padding: max(10px, env(safe-area-inset-top)) 16px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 10px;
}
.topbar-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.title { font-size: 24px; font-weight: 700; letter-spacing: -0.02em; text-align: left; }
.nav-btns { display: flex; align-items: center; gap: 2px; }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; color: var(--accent); }
.icon-btn:active, .pill-btn:active { background: var(--bg2); }
.pill-btn { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); font-size: 14px; font-weight: 600; margin-right: 4px; }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--bg2); border-radius: 10px; padding: 2px; }
.segmented button { padding: 6px 0; border-radius: 8px; font-size: 13px; font-weight: 600; color: var(--muted); }
.segmented button[aria-selected="true"] { background: var(--card); color: var(--text); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); }

.mode-banner {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-radius: 12px; font-size: 14px; font-weight: 600; color: #fff; text-align: left;
}
.mode-banner.gym { background: var(--gym); }
.mode-banner.focus { background: var(--focus); }
.mode-banner .dot { width: 8px; height: 8px; border-radius: 50%; background: #fff; animation: pulse 1.6s infinite; }
.mode-banner .end { margin-left: auto; opacity: 0.85; font-weight: 500; }
@keyframes pulse { 50% { opacity: 0.35; } }

main { flex: 1; min-height: 0; overflow: auto; }

/* Time grid (Day / 3 Day) */
.grid-wrap { height: 100%; display: flex; flex-direction: column; }
.grid-head, .grid-allday, .grid-body { display: grid; grid-template-columns: 46px repeat(var(--n), 1fr); }
.grid-head { border-bottom: 1px solid var(--line); }
.dayhead { padding: 6px 0 8px; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.dayhead span { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.dayhead b { font-size: 20px; font-weight: 600; width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%; }
.dayhead.today span { color: var(--accent); }
.dayhead.today b { background: var(--accent); color: #fff; }
.grid-allday { border-bottom: 1px solid var(--line); }
.allday-cell { padding: 3px 2px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.grid-body { flex: 1; overflow-y: auto; position: relative; }
.hours { position: relative; }
.hour { height: var(--hour); position: relative; }
.hour span { position: absolute; top: -7px; right: 6px; font-size: 11px; color: var(--muted); }
.col {
  position: relative; height: calc(var(--hour) * 24); border-left: 1px solid var(--line-soft);
  background: repeating-linear-gradient(to bottom, var(--line-soft) 0 1px, transparent 1px var(--hour));
}
.event {
  position: absolute; overflow: hidden; text-align: left;
  background: var(--c); color: #fff; border-radius: 6px; padding: 3px 5px;
  font-size: 12px; line-height: 1.25; display: flex; flex-direction: column;
  border: 1px solid var(--bg);
}
.event b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.event span { opacity: 0.9; font-size: 11px; }
.now { position: absolute; left: -4px; right: 0; height: 2px; background: var(--now); z-index: 2; pointer-events: none; }
.now::before { content: ""; position: absolute; left: 0; top: -4px; width: 10px; height: 10px; border-radius: 50%; background: var(--now); }

.chip {
  display: block; width: 100%; text-align: left; font-size: 11px; font-weight: 600; line-height: 1.3;
  padding: 1px 5px; border-radius: 4px; background: var(--c); color: #fff;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chip.timed { background: transparent; color: var(--text); padding-left: 2px; font-weight: 500; }
.chip.timed::before { content: ""; display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--c); margin-right: 4px; vertical-align: 1px; }

/* Month */
.month { height: 100%; display: flex; flex-direction: column; }
.month-head { display: grid; grid-template-columns: repeat(7, 1fr); border-bottom: 1px solid var(--line); }
.month-head span { text-align: center; font-size: 11px; color: var(--muted); padding: 6px 0; text-transform: uppercase; letter-spacing: 0.04em; }
.month-grid { flex: 1; display: grid; grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(6, minmax(72px, 1fr)); }
.cell {
  border-right: 1px solid var(--line-soft); border-bottom: 1px solid var(--line-soft);
  padding: 3px 2px; display: flex; flex-direction: column; gap: 2px; min-width: 0; overflow: hidden; text-align: left;
}
.cell .num { align-self: center; font-size: 13px; font-weight: 600; width: 24px; height: 24px; display: grid; place-items: center; border-radius: 50%; }
.cell.other .num { color: var(--muted); font-weight: 400; }
.cell.today .num { background: var(--accent); color: #fff; }
.cell .more { font-size: 10px; color: var(--muted); padding-left: 3px; }

/* Schedule */
.schedule { padding: 8px 16px 120px; }
.sched-day { display: grid; grid-template-columns: 52px 1fr; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line-soft); }
.sched-date { text-align: center; }
.sched-date span { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; }
.sched-date b { display: grid; place-items: center; margin: 2px auto 0; width: 34px; height: 34px; border-radius: 50%; font-size: 19px; font-weight: 600; }
.sched-day.today .sched-date b { background: var(--accent); color: #fff; }
.sched-list { display: flex; flex-direction: column; gap: 6px; }
.sched-item { background: var(--c); color: #fff; border-radius: 10px; padding: 9px 12px; text-align: left; }
.sched-item b { display: block; font-weight: 600; }
.sched-item span { font-size: 13px; opacity: 0.92; }
.empty { text-align: center; color: var(--muted); padding: 64px 24px; }

/* Modes */
.modes { padding: 16px 16px 120px; background: var(--page); display: flex; flex-direction: column; gap: 14px; }
.modes > * { flex-shrink: 0; }
.status { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); padding: 0 4px; }
.status i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.connected i { background: var(--ok); }
.status.needs-qr i, .status.disconnected i { background: var(--danger); }
.mode-card {
  display: flex; flex-direction: column; gap: 10px; text-align: left;
  background: var(--card); border-radius: 18px; padding: 16px; border: 2px solid transparent;
  transition: transform 0.12s ease;
}
.mode-card:active { transform: scale(0.98); }
.mode-card.active { border-color: var(--mc); }
.mode-top { display: flex; align-items: center; gap: 12px; }
.mode-icon { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; background: var(--mc); color: #fff; }
.mode-icon svg { width: 24px; height: 24px; }
.mode-name { font-size: 18px; font-weight: 700; }
.mode-sub { font-size: 13px; color: var(--muted); }
.mode-state { margin-left: auto; font-size: 13px; font-weight: 700; padding: 5px 11px; border-radius: 999px; background: var(--bg2); color: var(--muted); }
.mode-card.active .mode-state { background: var(--mc); color: #fff; }
.quote { margin: 0; padding: 10px 12px; border-radius: 12px; background: var(--bg2); font-size: 14px; color: var(--text); }
.gym-card { --mc: var(--gym); }
.focus-card { --mc: var(--focus); }
.off-btn { padding: 14px; border-radius: 14px; background: var(--card); color: var(--danger); font-weight: 600; }
.settings { background: var(--card); border-radius: 14px; overflow: hidden; }
.settings .row { padding: 12px 14px; }
.settings .row + .row { border-top: 1px solid var(--line-soft); }
.hint { font-size: 12px; color: var(--muted); padding: 0 4px; margin: -6px 0 0; }

/* Floating add button + tab bar */
.fab {
  position: fixed; right: 18px; bottom: calc(76px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 18px; background: var(--accent); color: #fff;
  display: grid; place-items: center; box-shadow: var(--shadow); z-index: 5;
}
.fab svg { width: 26px; height: 26px; stroke-width: 2.4; }
.tabbar {
  display: grid; grid-template-columns: 1fr 1fr 1fr;
  border-top: 1px solid var(--line); background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px; font-size: 11px; font-weight: 600; color: var(--muted); }
.tab.active { color: var(--accent); }

/* Event sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); z-index: 10; display: flex; align-items: flex-end; }
.sheet {
  width: 100%; max-height: 92dvh; overflow-y: auto; background: var(--page);
  border-radius: 16px 16px 0 0; padding: 0 16px calc(20px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px; animation: up 0.22s ease;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-bar { position: sticky; top: 0; background: var(--page); display: flex; justify-content: space-between; align-items: center; padding: 14px 0 6px; z-index: 1; }
.link-btn { color: var(--accent); font-size: 16px; }
.link-btn.bold { font-weight: 700; }
.title-input { border: 0; background: var(--card); border-radius: 12px; padding: 14px; font-size: 20px; font-weight: 600; }
.group { background: var(--card); border-radius: 12px; overflow: hidden; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 14px; min-height: 46px; }
.group .row + .row { border-top: 1px solid var(--line-soft); }
.inputs { display: flex; gap: 6px; }
.row input[type="date"], .row input[type="time"], .row select {
  border: 0; background: var(--bg2); border-radius: 8px; padding: 6px 8px; font-size: 15px;
}
.row input[type="time"] { min-width: 112px; }
.group textarea { width: 100%; border: 0; background: transparent; padding: 12px 14px; resize: none; display: block; }
.colors { justify-content: flex-start; flex-wrap: wrap; gap: 10px; }
.swatch { width: 28px; height: 28px; border-radius: 50%; background: var(--c); position: relative; }
.swatch[aria-checked="true"]::after { content: ""; position: absolute; inset: -4px; border-radius: 50%; border: 2px solid var(--c); }
.switch {
  appearance: none; -webkit-appearance: none; width: 51px; height: 31px; border-radius: 999px; background: var(--line);
  position: relative; transition: background 0.2s; flex-shrink: 0; margin: 0;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); transition: transform 0.2s;
}
.switch:checked { background: var(--ok); }
.switch:checked::after { transform: translateX(20px); }

/* Linking WhatsApp */
.link-card { background: var(--card); border-radius: 18px; padding: 16px; display: flex; flex-direction: column; gap: 12px; align-items: center; text-align: center; }
.link-card ol { margin: 0; padding-left: 20px; text-align: left; font-size: 14px; color: var(--muted); display: flex; flex-direction: column; gap: 6px; }
.link-card b, .link-card em { color: var(--text); }
.pair-code { font: 700 32px/1 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 0.08em; padding: 14px 18px; border-radius: 12px; background: var(--bg2); user-select: all; -webkit-user-select: all; }
.qr { width: 220px; height: 220px; border-radius: 12px; background: #fff; padding: 8px; }
.status.needs-code i { background: var(--danger); }

/* Messages: who wrote during Gym/Focus */
.inbox { padding: 12px 16px 120px; background: var(--page); display: flex; flex-direction: column; gap: 16px; }
.inbox > * { flex-shrink: 0; }
.session-head { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); padding: 0 4px 8px; }
.session-count { margin-left: auto; }
.mode-chip { font-size: 12px; font-weight: 700; color: #fff; padding: 3px 9px; border-radius: 999px; background: var(--muted); }
.mode-chip.gym { background: var(--gym); }
.mode-chip.focus { background: var(--focus); }
.people { background: var(--card); border-radius: 14px; overflow: hidden; }
.person { display: flex; gap: 12px; align-items: center; padding: 11px 14px; color: inherit; text-decoration: none; }
.person + .person { border-top: 1px solid var(--line-soft); }
a.person:active { background: var(--bg2); }
.avatar { width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; display: grid; place-items: center; font-weight: 700; color: #fff; background: #25a35a; }
.who { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 2px; }
.who-top { display: flex; justify-content: space-between; gap: 8px; }
.who-top b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who-top time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.who-msg { font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who-msg em { font-style: normal; font-weight: 600; color: var(--text); }
.tab-icon { position: relative; display: inline-flex; }
.badge { position: absolute; top: -4px; right: -10px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px; background: var(--danger); color: #fff; font: 700 11px/18px -apple-system, sans-serif; font-style: normal; text-align: center; }
