:root {
  --bg: #f7f4ee;
  --card: #ffffff;
  --ink: #2a2622;
  --ink-2: #6b635a;
  --line: #e6e0d6;
  --brand: #2f6b4f;
  --brand-soft: #e3efe8;
  --danger: #b3412f;
  --star: #e0a526;
  --radius: 14px;
  --tabbar-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171a18;
    --card: #222624;
    --ink: #eeeae4;
    --ink-2: #a39d94;
    --line: #353a37;
    --brand: #6fbf95;
    --brand-soft: #24362d;
    --danger: #e0715f;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 20px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { font-size: 30px; line-height: 1; background: none; border: 0; color: var(--brand); padding: 0 6px 0 0; cursor: pointer; }

main { padding: 16px; max-width: 640px; margin: 0 auto; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; 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;
  font-size: 11px; color: var(--ink-2); text-decoration: none; gap: 2px;
}
.tabbar a span { font-size: 22px; filter: grayscale(1); opacity: .6; }
.tabbar a.active { color: var(--brand); font-weight: 600; }
.tabbar a.active span { filter: none; opacity: 1; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px;
}
a.card { display: block; color: inherit; text-decoration: none; }
a.card.row { display: flex; }
.row { display: flex; align-items: center; gap: 10px; }
.row .grow { flex: 1; min-width: 0; }
.muted { color: var(--ink-2); font-size: 14px; }
.small { font-size: 13px; }
.chev { color: var(--ink-2); font-size: 22px; }
h2 { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2); margin: 22px 4px 8px; }
h2:first-child { margin-top: 4px; }
.title { font-weight: 600; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chip {
  display: inline-block; padding: 2px 8px; border-radius: 99px;
  background: var(--brand-soft); color: var(--brand); font-size: 12px; font-weight: 600;
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #10231a; } }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.sm { padding: 6px 10px; font-size: 14px; }
.actions { display: flex; gap: 10px; margin-top: 16px; }
.actions .btn { flex: 1; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 14px; color: var(--ink-2); margin-bottom: 6px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--ink);
  padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card);
  -webkit-appearance: none; appearance: none;
}
input[type="date"] { min-height: 46px; }
select {
  padding-right: 36px; cursor: pointer;
  background: var(--card) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%238a8279' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") no-repeat right 14px center / 12px 8px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; }

.stars { display: inline-flex; gap: 2px; }
.stars button {
  background: none; border: 0; padding: 4px 3px; font-size: 26px; line-height: 1; cursor: pointer;
  color: var(--line);
}
.stars button.on { color: var(--star); }
.stars-ro { color: var(--star); letter-spacing: 1px; }
.stars-ro .off { color: var(--line); }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  padding: 8px 12px; border-radius: 99px; border: 1px solid var(--line); background: var(--card);
  color: var(--ink); font: inherit; font-size: 14px; cursor: pointer;
}
.seg button.on { background: var(--brand); border-color: var(--brand); color: #fff; }

.item-edit { border-left: 4px solid var(--brand); }
.item-edit .remove { background: none; border: 0; color: var(--ink-2); font-size: 22px; cursor: pointer; padding: 0 4px; }

.empty { text-align: center; padding: 40px 16px; color: var(--ink-2); }
.empty .big { font-size: 48px; margin-bottom: 8px; }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 18px);
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--brand); color: #fff; font-size: 30px; line-height: 1;
  box-shadow: 0 6px 18px rgba(0,0,0,.2); cursor: pointer;
  display: flex; align-items: center; justify-content: center; text-decoration: none;
}

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; text-align: center; }
.stat b { display: block; font-size: 22px; }
.stat span { font-size: 12px; color: var(--ink-2); }

.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--ink-2); font-size: 11px; }
.chart .line { fill: none; stroke: var(--brand); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--brand); stroke: var(--card); stroke-width: 2; }
.chart .hit { fill: transparent; cursor: pointer; }
.tip {
  position: absolute; pointer-events: none; transform: translate(-50%, -110%);
  background: var(--ink); color: var(--bg); font-size: 12px; padding: 5px 8px; border-radius: 8px; white-space: nowrap;
}

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 99px; font-size: 14px; z-index: 20;
}

/* ---------- Connexion ---------- */
body.auth .tabbar, body.auth .topbar { display: none; }
body.auth { padding-bottom: 0; }
.auth-box { max-width: 380px; margin: 0 auto; padding: calc(40px + env(safe-area-inset-top)) 4px 40px; }
.auth-box .logo { display: block; width: 84px; height: 84px; border-radius: 20px; margin: 0 auto 12px; }
.auth-box h1 { text-align: center; margin: 0 0 4px; font-size: 28px; }
.auth-box .sub { text-align: center; color: var(--ink-2); margin: 0 0 24px; }
.error { color: var(--danger); font-size: 14px; margin: 0 0 12px; }
.link { background: none; border: 0; color: var(--brand); font: inherit; font-size: 14px; cursor: pointer; padding: 8px; display: block; margin: 8px auto 0; }

.horse-bar { display: flex; gap: 6px; overflow-x: auto; margin: -4px -16px 12px; padding: 0 16px 4px; scrollbar-width: none; }
.horse-bar::-webkit-scrollbar { display: none; }
.horse-bar .seg button { white-space: nowrap; }
.offline { background: #fff3d6; color: #6b4d00; font-size: 13px; text-align: center; padding: 6px 12px; border-radius: 10px; margin-bottom: 12px; }
@media (prefers-color-scheme: dark) { .offline { background: #3a3020; color: #f0d79a; } }

.update {
  position: fixed; left: 50%; top: calc(12px + env(safe-area-inset-top)); transform: translateX(-50%); z-index: 25;
  display: flex; align-items: center; gap: 12px; padding: 8px 8px 8px 14px; width: max-content; max-width: calc(100vw - 32px);
  background: var(--ink); color: var(--bg); border-radius: 99px; font-size: 14px; box-shadow: 0 6px 18px rgba(0,0,0,.2);
}
.update[hidden] { display: none; }
.update button { font: inherit; font-weight: 700; border: 0; border-radius: 99px; padding: 6px 12px; background: var(--brand); color: #fff; cursor: pointer; }
.version { text-align: center; color: var(--ink-2); font-size: 12px; margin-top: 8px; }

/* ---------- Bandeau d'installation ---------- */
.install {
  position: fixed; left: 12px; right: 12px; z-index: 15;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 10px);
  max-width: 616px; margin: 0 auto;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 8px 10px 10px; border-radius: 16px;
  background: var(--card); border: 1px solid var(--line); box-shadow: 0 6px 24px rgba(0,0,0,.15);
}
.install[hidden] { display: none; }
.install img { width: 40px; height: 40px; border-radius: 10px; flex: none; }
.install-text { flex: 1; min-width: 0; font-size: 13px; color: var(--ink-2); line-height: 1.3; }
.install-text > b { display: block; font-size: 15px; color: var(--ink); }
.install-close { background: none; border: 0; color: var(--ink-2); font-size: 18px; padding: 6px; cursor: pointer; flex: none; }
body.auth .install { bottom: calc(16px + env(safe-area-inset-bottom)); }
/* le bandeau ne doit rien cacher : on remonte le bouton + et les messages, et on laisse de la place en bas */
body.has-install main { padding-bottom: 90px; }
body.has-install .fab { bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 92px); }
body.has-install .toast { bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 92px); }
.share-ico { width: 16px; height: 16px; vertical-align: -3px; color: var(--brand); }

/* ---------- Photo des chevaux ---------- */
.avatar { display: inline-flex; align-items: center; justify-content: center; flex: none; border-radius: 50%; object-fit: cover; vertical-align: middle; background: var(--brand-soft); }
.avatar.sm { width: 18px; height: 18px; font-size: 11px; margin-right: 4px; }
.avatar.md { width: 24px; height: 24px; font-size: 14px; }
.avatar.lg { width: 48px; height: 48px; font-size: 26px; }
.avatar.xl { width: 128px; height: 128px; font-size: 64px; border: 3px solid var(--card); box-shadow: 0 2px 10px rgba(0,0,0,.12); }
.avatar-empty { line-height: 1; }
.seg button { display: inline-flex; align-items: center; gap: 6px; }
.chip { display: inline-flex; align-items: center; vertical-align: middle; }
.photo-pick { display: flex; flex-direction: column; align-items: center; gap: 12px; margin: 4px 0 20px; }

/* ---------- Recadrage de la photo ---------- */
.cropper {
  position: fixed; inset: 0; z-index: 30; background: #111; color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
}
.cropper-help { margin: 0; font-size: 14px; color: #bbb; text-align: center; }
.cropper-frame {
  position: relative; overflow: hidden; touch-action: none; cursor: grab;
  width: min(calc(100vw - 32px), 420px); aspect-ratio: 1; background: #000; border-radius: 8px;
}
.cropper-frame:active { cursor: grabbing; }
.cropper-frame img { position: absolute; left: 0; top: 0; transform-origin: 0 0; max-width: none; user-select: none; -webkit-user-drag: none; pointer-events: none; }
/* cercle = ce qui sera visible dans la pastille ronde */
.cropper-mask { position: absolute; inset: 0; border-radius: 50%; box-shadow: 0 0 0 999px rgba(0,0,0,.55); border: 2px solid rgba(255,255,255,.8); pointer-events: none; }
.cropper-zoom { display: flex; align-items: center; gap: 10px; width: min(calc(100vw - 32px), 420px); color: #bbb; }
.cropper-zoom input { flex: 1; padding: 0; border: 0; background: none; accent-color: var(--brand); -webkit-appearance: auto; appearance: auto; }
.cropper .actions { width: min(calc(100vw - 32px), 420px); margin-top: 0; }
.cropper .btn:not(.primary) { background: #2a2a2a; border-color: #2a2a2a; color: #fff; }

/* ---------- Durée d'un exercice : roue façon minuteur ---------- */
.dur { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 2px 0 10px; }
.wheel { position: relative; display: flex; align-items: center; gap: 2px; height: 108px; padding: 0 6px; }
/* bandeau de la valeur choisie (ligne du milieu) */
.wheel::before { content: ''; position: absolute; left: 0; right: 0; top: 36px; height: 36px; background: var(--brand-soft); border-radius: 10px; }
.wheel-col {
  position: relative; width: 52px; height: 108px; padding: 36px 0; overflow-y: scroll; overscroll-behavior: contain;
  scroll-snap-type: y mandatory; scrollbar-width: none;
  -webkit-mask-image: linear-gradient(transparent, #000 32%, #000 68%, transparent);
  mask-image: linear-gradient(transparent, #000 32%, #000 68%, transparent);
}
.wheel-col::-webkit-scrollbar { display: none; }
.wheel-item { height: 36px; line-height: 36px; text-align: center; font-size: 20px; font-weight: 600; color: var(--ink-2); scroll-snap-align: center; cursor: pointer; user-select: none; }
.wheel-item.on { color: var(--brand); }
.wheel-unit { position: relative; font-weight: 600; color: var(--ink-2); padding-right: 8px; }
