:root {
  color-scheme: light;
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efea;
  --text: #15140f;
  --text-2: #5a5852;
  --muted: #8a887f;
  --line: #e3e1d9;
  --accent: #fc4c02;
  --accent-ink: #ffffff;
  --accent-soft: #ffe5d7;
  --good: #0ca30c;
  --bad: #d03b3b;
  --warn: #c98500;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f0f0e; --surface: #1a1a19; --surface-2: #242422; --text: #f4f3ee; --text-2: #c3c2b7; --muted: #8a887f; --line: #2e2e2b;
    --accent-soft: #3a2316; --shadow: 0 1px 2px rgba(0,0,0,.4);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px)); }
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
img { max-width: 100%; display: block; }
[hidden] { display: none !important; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---- Layout ---- */
.topbar { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--line); padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px; display: flex; align-items: center; gap: 12px; }
.topbar .brand { font-weight: 800; letter-spacing: -.02em; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.topbar .brand .logo { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 16px; }
.topbar .spacer { flex: 1; }
.group-switch { border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 6px 12px; cursor: pointer; max-width: 55vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page { max-width: 1040px; margin: 0 auto; padding: 16px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card h2, .card h3 { margin: 0 0 10px; font-size: 16px; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; }
.card h2 .more { margin-left: auto; font-size: 13px; color: var(--accent); text-decoration: none; font-weight: 600; }
.sub { color: var(--text-2); font-size: 13px; }
.muted { color: var(--muted); }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack { display: grid; gap: 10px; }
.sep { height: 1px; background: var(--line); margin: 12px 0; }
h1 { font-size: 24px; letter-spacing: -.02em; margin: 4px 0 14px; }
h1 small { font-size: 14px; color: var(--text-2); font-weight: 500; display: block; margin-top: 2px; }

/* ---- Navegação inferior ---- */
.nav { position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; background: var(--surface); border-top: 1px solid var(--line); display: flex; justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px)); }
.nav a { flex: 1; text-align: center; text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 600; padding: 4px 0; border-radius: 10px; }
.nav a .ic { font-size: 20px; display: block; line-height: 1.1; }
.nav a.active { color: var(--accent); background: var(--accent-soft); }
@media (min-width: 900px) {
  body { padding-bottom: 24px; }
  .nav { position: sticky; top: 0; bottom: auto; border-top: 0; border-bottom: 1px solid var(--line); justify-content: center; gap: 4px; padding: 6px 16px; }
  .nav a { flex: 0 0 auto; padding: 6px 14px; font-size: 13px; display: flex; gap: 6px; align-items: center; }
  .nav a .ic { font-size: 16px; }
}

/* ---- Botões e formulários ---- */
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 14px; cursor: pointer; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; line-height: 1.2; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn.strava { background: #fc4c02; color: #fff; border-color: #fc4c02; }
input, select, textarea { width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 12px; }
textarea { min-height: 80px; resize: vertical; }
label.f { display: grid; gap: 4px; font-size: 13px; color: var(--text-2); font-weight: 600; }
label.f > * { font-weight: 400; color: var(--text); }
.form { display: grid; gap: 12px; }
.form .cols { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--text-2); }
.pill.on { background: var(--accent-soft); color: var(--accent); }
.pill.good { background: #e3f5e3; color: #0a6b0a; }
.pill.bad { background: #fde3e3; color: #a12c2c; }
.pill.warn { background: #fff1d6; color: #8a5a00; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .pill.good { background: #163a16; color: #7fd47f; } :root:not([data-theme="light"]) .pill.bad { background: #3f1a1a; color: #f0a0a0; } :root:not([data-theme="light"]) .pill.warn { background: #3d2d08; color: #f3c56a; } }
.tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
.tabs button { border: 1px solid var(--line); background: var(--surface); padding: 6px 12px; border-radius: 999px; cursor: pointer; white-space: nowrap; font-weight: 600; font-size: 13px; }
.tabs button.on { background: var(--text); color: var(--bg); border-color: var(--text); }

/* ---- Avatares / pessoas ---- */
.avatar { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; flex: 0 0 auto; display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 14px; background: var(--s1); border: 2px solid var(--surface); }
.avatar.sm { width: 26px; height: 26px; font-size: 11px; }
.avatar.lg { width: 56px; height: 56px; font-size: 20px; }
.person { display: flex; align-items: center; gap: 10px; min-width: 0; }
.person .nm { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.person .nk { font-size: 12px; color: var(--muted); }
.c0 { background: var(--s1); } .c1 { background: var(--s2); } .c2 { background: var(--s3); } .c3 { background: var(--s4); } .c4 { background: var(--s5); } .c5 { background: var(--s6); } .c6 { background: var(--s7); } .c7 { background: var(--s8); }

/* ---- Rankings ---- */
.board { display: grid; gap: 6px; }
.board .r { display: grid; grid-template-columns: 28px 1fr auto; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); }
.board .r .pos { font-weight: 800; color: var(--muted); text-align: center; }
.board .r.me { outline: 2px solid var(--accent); outline-offset: -2px; }
.board .r .val { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.board .r.first .pos { color: var(--warn); font-size: 18px; }
.board .r.last .pos { color: var(--bad); }
.bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin-top: 4px; }
.bar > i { display: block; height: 100%; border-radius: 3px; }

/* ---- Pódio ---- */
.podium { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; align-items: end; margin: 8px 0 4px; }
.podium .p { text-align: center; display: grid; justify-items: center; gap: 4px; }
.podium .p .step { width: 100%; border-radius: 10px 10px 0 0; background: var(--surface-2); display: grid; place-items: center; font-weight: 800; color: var(--muted); }
.podium .p:nth-child(1) .step { height: 46px; } .podium .p:nth-child(2) .step { height: 64px; background: var(--accent-soft); color: var(--accent); } .podium .p:nth-child(3) .step { height: 34px; }
.podium .nm { font-size: 12px; font-weight: 600; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium .vl { font-size: 12px; color: var(--text-2); font-variant-numeric: tabular-nums; }

/* ---- Stat tiles ---- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.tile { background: var(--surface-2); border-radius: 12px; padding: 12px; }
.tile .v { font-size: 22px; font-weight: 800; letter-spacing: -.02em; line-height: 1.1; }
.tile .l { font-size: 12px; color: var(--text-2); margin-top: 2px; }

/* ---- Prémios / medalhas ---- */
.awards { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.award { background: var(--surface-2); border-radius: 12px; padding: 12px; display: grid; gap: 6px; }
.award .em { font-size: 28px; line-height: 1; }
.award .t { font-weight: 700; font-size: 14px; }
.award .d { font-size: 12px; color: var(--text-2); }
.award .w { display: flex; align-items: center; gap: 6px; font-size: 13px; margin-top: 2px; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.badge { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 22px; background: var(--surface-2); cursor: default; }
.badge.locked { filter: grayscale(1); opacity: .35; }

/* ---- Desafios ---- */
.chal { display: grid; gap: 10px; }
.chal .head { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.chal .title { font-weight: 800; font-size: 17px; letter-spacing: -.01em; }
.chal .meta { display: flex; gap: 6px; flex-wrap: wrap; }
.chal .stake { background: var(--accent-soft); color: var(--text); border-radius: 10px; padding: 8px 10px; font-size: 13px; }
.progress { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); }
.result-banner { border-radius: 12px; padding: 12px 14px; background: linear-gradient(135deg, var(--accent-soft), var(--surface-2)); display: grid; gap: 4px; }

/* ---- Mural ---- */
.post { display: grid; gap: 10px; }
.post .txt { white-space: pre-wrap; word-wrap: break-word; }
.post .photo { border-radius: 12px; overflow: hidden; max-height: 480px; background: var(--surface-2); }
.post .photo img { width: 100%; height: 100%; object-fit: cover; }
.post .type { font-size: 12px; font-weight: 700; }
.post.excuse { border-left: 4px solid var(--warn); }
.post.brag { border-left: 4px solid var(--accent); }
.reacts { display: flex; gap: 6px; flex-wrap: wrap; }
.reacts button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 3px 10px; cursor: pointer; font-size: 14px; }
.reacts button.mine { border-color: var(--accent); background: var(--accent-soft); }
.comment { display: flex; gap: 8px; font-size: 14px; align-items: flex-start; }
.comment .b { background: var(--surface-2); border-radius: 10px; padding: 6px 10px; }
.comment .b b { font-size: 13px; }
.compose { display: grid; gap: 8px; }
.compose .actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.compose .preview { max-height: 180px; border-radius: 10px; width: auto; }

/* ---- Atividades ---- */
.act { display: grid; grid-template-columns: 64px 1fr; gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.act:last-child { border-bottom: 0; }
.act .route { width: 64px; height: 64px; border-radius: 12px; background: var(--surface-2); display: grid; place-items: center; overflow: hidden; }
.act .route svg { width: 100%; height: 100%; }
.act .route img { width: 100%; height: 100%; object-fit: cover; }
.act .nm { font-weight: 700; }
.act .st { display: flex; gap: 12px; font-size: 13px; color: var(--text-2); flex-wrap: wrap; font-variant-numeric: tabular-nums; }

/* ---- Galeria ---- */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.gallery .ph { position: relative; aspect-ratio: 1; border-radius: 12px; overflow: hidden; background: var(--surface-2); cursor: pointer; }
.gallery .ph img { width: 100%; height: 100%; object-fit: cover; }
.gallery .ph .cap { position: absolute; left: 0; right: 0; bottom: 0; padding: 20px 8px 6px; font-size: 11px; color: #fff; background: linear-gradient(transparent, rgba(0,0,0,.7)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gallery .ph .lk { position: absolute; top: 6px; right: 6px; background: rgba(0,0,0,.55); color: #fff; font-size: 11px; border-radius: 999px; padding: 2px 7px; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 50; display: grid; place-items: center; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 80vh; border-radius: 8px; }
.lightbox .cap { color: #fff; text-align: center; margin-top: 10px; }

/* ---- Gráficos ---- */
.chart { width: 100%; overflow: hidden; }
.chart svg { width: 100%; height: auto; display: block; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.legend { display: flex; flex-wrap: wrap; gap: 8px 14px; font-size: 12px; color: var(--text-2); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.tt { position: fixed; z-index: 60; pointer-events: none; background: var(--text); color: var(--bg); padding: 6px 10px; border-radius: 8px; font-size: 12px; white-space: nowrap; transform: translate(-50%, calc(-100% - 10px)); }

/* ---- Tabela ---- */
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th, .tbl td { padding: 8px 6px; text-align: right; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl th { font-size: 12px; color: var(--muted); font-weight: 600; }
.tbl th:first-child, .tbl td:first-child { text-align: left; }
.tbl-wrap { overflow-x: auto; }

/* ---- Modal / toast ---- */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; display: grid; place-items: end center; }
@media (min-width: 700px) { .modal-bg { place-items: center; } }
.modal { background: var(--surface); width: min(100%, 560px); max-height: 92vh; overflow: auto; border-radius: 18px 18px 0 0; padding: 18px 18px calc(18px + env(safe-area-inset-bottom, 0px)); }
@media (min-width: 700px) { .modal { border-radius: 18px; } }
.modal h2 { margin: 0 0 12px; }
.toast { position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 70; font-weight: 600; max-width: 90vw; }
.toast.err { background: var(--bad); color: #fff; }

/* ---- Login ---- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(1200px 600px at 20% -10%, var(--accent-soft), transparent), var(--bg); }
.login .box { width: min(100%, 440px); display: grid; gap: 16px; }
.login h1 { font-size: 34px; margin: 0; }
.login .feat { display: grid; gap: 8px; font-size: 14px; color: var(--text-2); }
.demo-users { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.demo-users button { display: grid; justify-items: center; gap: 6px; padding: 10px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }

/* ---- Roleta ---- */
.roulette { text-align: center; padding: 18px; display: grid; gap: 12px; }
.roulette .wheel { font-size: 56px; display: inline-block; transition: transform 2.4s cubic-bezier(.2,.8,.2,1); }
.roulette .res { font-size: 18px; font-weight: 800; min-height: 2.6em; }
.empty { padding: 24px; text-align: center; color: var(--muted); }
.copy { font-family: ui-monospace, Menlo, monospace; letter-spacing: .12em; font-weight: 800; font-size: 20px; background: var(--surface-2); padding: 6px 12px; border-radius: 10px; }
.dbt { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.dbt.settled { opacity: .55; text-decoration: line-through; }
.dbt:last-child { border-bottom: 0; }

/* ---- Avisos ---- */
.banner { margin-bottom: 14px; border-left: 4px solid var(--accent); }
.banner.err { border-left-color: var(--bad); background: #fff4f2; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .banner.err { background: #2b1714; } }
.rec { display: grid; grid-template-columns: 26px 1fr auto; gap: 8px; align-items: center; }
.rec-v { white-space: nowrap; font-variant-numeric: tabular-nums; }

/* ---- Telemóvel ---- */
@media (max-width: 520px) {
  .topbar { padding-left: 12px; padding-right: 12px; gap: 8px; }
  .brand-text { display: none; }
  .group-switch { max-width: none; flex: 1; text-align: left; }
  .page { padding: 12px; }
  .card { padding: 14px; border-radius: 12px; }
  h1 { font-size: 21px; margin-bottom: 10px; }
  .grid, .grid.two { gap: 12px; }
  .tiles { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .tile { padding: 10px; }
  .tile .v { font-size: 17px; }
  .tile .l { font-size: 11px; }
  .tabs button { padding: 6px 10px; font-size: 12px; }
  .hide-sm { display: none; }
  .tbl th, .tbl td { padding: 8px 4px; font-size: 13px; }
  .badge { width: 34px; height: 34px; font-size: 18px; border-radius: 10px; }
  .awards { grid-template-columns: 1fr 1fr; gap: 8px; }
  .award { padding: 10px; }
  .award .em { font-size: 24px; }
  .act { grid-template-columns: 52px 1fr; gap: 10px; }
  .act .route { width: 52px; height: 52px; }
  .act .st { gap: 8px; font-size: 12px; }
  .board .r { padding: 8px; gap: 8px; }
  .board .r .val { font-size: 14px; }
  .podium .p .step { font-size: 14px; }
  .avatar.lg { width: 48px; height: 48px; font-size: 17px; }
  .chal .title { font-size: 16px; }
  .btn { padding: 9px 12px; }
  .modal { padding: 16px 14px calc(16px + env(safe-area-inset-bottom, 0px)); }
  .form .cols { grid-template-columns: 1fr 1fr; }
  .gallery { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .gallery .ph { border-radius: 8px; }
  .gallery .ph .cap { font-size: 10px; padding: 14px 6px 4px; }
}
@media (max-width: 380px) { .tiles { grid-template-columns: repeat(2, 1fr); } .form .cols { grid-template-columns: 1fr; } }
/* Área de toque confortável em ecrãs táteis */
@media (pointer: coarse) { .reacts button { padding: 6px 12px; } .nav a { padding: 6px 0; } }
