:root {
  --bg: #f5f5fb;
  --card: #ffffff;
  --text: #1b1b2f;
  --muted: #6b6b80;
  --line: #e6e6ef;
  --brand: #4f46e5;
  --brand-soft: #eef0ff;
  --ok: #059669;
  --ok-soft: #e7f7f0;
  --bad: #dc2626;
  --bad-soft: #fdecec;
  --gold: #d97706;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 20, 50, .06), 0 4px 16px rgba(20, 20, 50, .05);
  --tabbar-h: 64px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111118;
    --card: #1c1c26;
    --text: #ececf5;
    --muted: #9a9ab0;
    --line: #2c2c3a;
    --brand: #8b85ff;
    --brand-soft: #25234a;
    --ok: #34d399;
    --ok-soft: #12352a;
    --bad: #f87171;
    --bad-soft: #3a1d1d;
    --gold: #fbbf24;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
}
body.authed { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); }
a { color: var(--brand); }
h2, h3 { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.grow { flex: 1; min-width: 0; }
[hidden] { display: none !important; }

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: baseline; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 12px) 20px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.topbar h1 { font-size: 1.35rem; margin: 0; }
.topbar-sub { color: var(--muted); font-size: .9rem; margin-left: auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

main { max-width: 640px; margin: 0 auto; padding: 4px 16px 24px; outline: none; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---- tab bar ---- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 500;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--brand); }
.tabbar a.active svg { stroke-width: 2.3; }

/* ---- cards, buttons, forms ---- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin: 12px 0; }
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
  font: inherit; font-weight: 600; padding: 12px 18px; border-radius: 12px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none;
}
.btn:disabled { opacity: .55; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.block { display: flex; width: 100%; margin: 12px 0; }
.danger-text { color: var(--bad); }
.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; border-radius: 12px;
  font-size: 1.7rem; line-height: 1; color: var(--text); text-decoration: none;
  background: var(--card); border: 1px solid var(--line); cursor: pointer; font-family: inherit;
}
.icon-btn.disabled { opacity: .3; }
.icon-btn.sm { width: 34px; height: 34px; font-size: 1rem; border-radius: 9px; }

.form { display: flex; flex-direction: column; gap: 14px; }
.form label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: .92rem; }
.form label .muted { font-weight: 400; }
input, select, textarea {
  font: inherit; font-size: 16px; /* stops iOS zooming */
  padding: 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
.form .check-row { flex-direction: row; align-items: center; gap: 10px; font-weight: 500; }
.check-row input { width: 22px; height: 22px; accent-color: var(--brand); flex: none; }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 600; font-size: .92rem; margin-bottom: 8px; padding: 0; }
legend .muted { font-weight: 400; }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; }
.error { color: var(--bad); background: var(--bad-soft); padding: 10px 12px; border-radius: 10px; margin: 0; }

.swatches > div, .swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatches legend { width: 100%; }
.swatch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.swatch span { display: block; width: 34px; height: 34px; border-radius: 50%; background: var(--c); border: 3px solid var(--card); box-shadow: 0 0 0 2px transparent; }
.swatch input:checked + span { box-shadow: 0 0 0 2px var(--c); }
.swatch input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 3px; }

/* ---- avatars ---- */
.avatar {
  flex: none; display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 50%;
  background: var(--c); color: #fff; font-size: .78rem; font-weight: 700; letter-spacing: .02em;
}
.avatar.sm { width: 24px; height: 24px; font-size: .62rem; }
.avatar.lg { width: 52px; height: 52px; font-size: 1.1rem; }
.avatars { display: inline-flex; gap: 2px; margin-top: 6px; align-items: center; }

/* ---- auth ---- */
.auth { max-width: 400px; margin: 24px auto; text-align: center; }
.auth .form { text-align: left; }
.auth-logo { border-radius: 20px; margin-bottom: 12px; }
.auth h2 { font-size: 1.5rem; }

/* ---- rota ---- */
.period { display: flex; align-items: center; gap: 12px; margin: 8px 0 14px; }
.period-title { flex: 1; text-align: center; }
.period-title h2 { font-size: 1.5rem; }
.period-title p { margin: 2px 0 0; font-size: .9rem; }

.segmented { display: flex; background: var(--line); border-radius: 12px; padding: 3px; margin-bottom: 14px; }
.segmented button {
  flex: 1; border: 0; background: transparent; font: inherit; font-weight: 600; font-size: .9rem; color: var(--muted);
  padding: 8px; border-radius: 9px; cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.progress { height: 10px; background: var(--line); border-radius: 99px; overflow: hidden; }
.progress div { height: 100%; background: linear-gradient(90deg, var(--brand), var(--ok)); border-radius: 99px; transition: width .4s ease; }
.progress-label { margin: 6px 0 12px; font-size: .88rem; color: var(--muted); font-weight: 600; }

.tasks { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.task {
  display: flex; align-items: center; gap: 14px; padding: 14px; background: var(--card);
  border-radius: var(--radius); box-shadow: var(--shadow); transition: background .25s;
}
.task.done { background: var(--ok-soft); }
.task.done .task-name { text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--text) 40%, transparent); }
.task.missed { box-shadow: inset 3px 0 0 var(--bad), var(--shadow); }
.check {
  flex: none; width: 44px; height: 44px; border-radius: 50%; border: 2.5px solid var(--line); background: transparent;
  display: grid; place-items: center; cursor: pointer; padding: 0; transition: transform .15s, background .2s, border-color .2s;
}
.check svg { width: 24px; height: 24px; fill: none; stroke: transparent; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.check:not(:disabled):active { transform: scale(.9); }
.task.done .check { background: var(--ok); border-color: var(--ok); animation: pop .3s ease; }
.task.done .check svg { stroke: #fff; }
.check:disabled { opacity: .45; cursor: default; }
.task.done .check:disabled { opacity: 1; }
@keyframes pop { 50% { transform: scale(1.18); } }
.task-body { flex: 1; min-width: 0; }
.task-name { font-weight: 650; }
.task-desc { color: var(--muted); font-size: .88rem; margin-top: 2px; }
.task-flag { color: var(--bad); font-size: .78rem; font-weight: 700; margin-top: 4px; text-transform: uppercase; letter-spacing: .04em; }
.tag { font-size: .8rem; }
.task-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; }
.who {
  display: flex; align-items: center; gap: 6px; border: 0; background: transparent; font: inherit; font-size: .82rem; color: var(--muted);
  padding: 0; cursor: pointer; max-width: 110px;
}
.who span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who:disabled { cursor: default; color: var(--muted); }
.points { font-size: .78rem; font-weight: 700; color: var(--gold); white-space: nowrap; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty-emoji { font-size: 3rem; }

/* ---- scores ---- */
.scores { list-style: none; padding: 6px 16px; }
.scores li { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.scores li:last-child { border-bottom: 0; }
.scores li.me .score-name::after { content: " (you)"; color: var(--muted); font-weight: 400; }
.medal { width: 28px; text-align: center; font-size: 1.4rem; }
.rank { font-size: 1rem; color: var(--muted); font-weight: 700; }
.score-body { flex: 1; min-width: 0; }
.score-name { font-weight: 650; }
.bar { height: 8px; background: var(--line); border-radius: 99px; margin-top: 6px; overflow: hidden; }
.bar div { height: 100%; border-radius: 99px; transition: width .5s ease; }
.score-pts { text-align: right; color: var(--gold); white-space: nowrap; }
.score-pts strong { font-size: 1.2rem; }

/* ---- menus / lists ---- */
.me-card { display: flex; align-items: center; gap: 14px; }
.menu { padding: 0; list-style: none; overflow: hidden; }
.menu > a, .menu li > button {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 16px; text-align: left;
  border: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--text);
  font: inherit; text-decoration: none; cursor: pointer;
}
.menu > a { justify-content: space-between; font-weight: 600; }
.menu > a:last-child, .menu li:last-child > button { border-bottom: 0; }
.menu span:last-child:not(.grow) { color: var(--muted); font-size: 1.3rem; }
.menu button.inactive { opacity: .5; }
.menu .pad { padding: 16px; }
.group-title { font-size: .85rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 4px 0; }
.back { display: inline-block; text-decoration: none; font-weight: 600; margin: 4px 0; }
.install h3 { margin-bottom: 6px; }
.install p { margin: 0; }

.rot-list { list-style: none; padding: 0; margin: 0; border: 1px solid var(--line); border-radius: 12px; }
.rot-list li { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.rot-list li:last-child { border-bottom: 0; }
.rot-list .check-row { font-weight: 500 !important; }

/* ---- bottom sheet ---- */
dialog#sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 640px; max-height: 92vh;
  background: var(--card); color: var(--text); border-radius: 22px 22px 0 0;
  animation: up .22s ease-out;
}
@media (min-width: 700px) { dialog#sheet { margin: auto; border-radius: 22px; } }
dialog#sheet::backdrop { background: rgba(10, 10, 30, .45); }
@keyframes up { from { transform: translateY(40px); opacity: .6; } }
.sheet-inner { padding: 22px 18px calc(18px + env(safe-area-inset-bottom)); }
.sheet-inner > h3 { margin-bottom: 14px; font-size: 1.2rem; }
.pick-list { display: flex; flex-direction: column; gap: 8px; margin: 14px 0; }
.pick {
  display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 12px; border: 1px solid var(--line);
  background: transparent; color: var(--text); font: inherit; font-weight: 600; text-align: left; cursor: pointer;
}
.pick span:nth-child(2) { flex: 1; }
.pick.current { border-color: var(--brand); background: var(--brand-soft); }

/* ---- toast & celebration ---- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 99px; font-weight: 600; font-size: .92rem;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.pop {
  position: fixed; z-index: 40; transform: translate(-50%, 0); pointer-events: none;
  font-weight: 800; color: var(--gold); font-size: 1.1rem; animation: floatup 1s ease-out forwards;
}
@keyframes floatup { to { transform: translate(-50%, -60px); opacity: 0; } }
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 60; overflow: hidden; }
.confetti i {
  position: absolute; top: -12px; width: 9px; height: 14px; border-radius: 2px;
  animation: fall 2.2s cubic-bezier(.2, .6, .4, 1) forwards;
}
@keyframes fall { to { transform: translate(var(--x), 105vh) rotate(var(--r)); opacity: .9; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
