/*
 * Mise en page : une colonne sur l'écran externe du Fold, deux colonnes
 * (courbe à gauche, régularité et carnet à droite) sur l'écran déplié.
 * Thème sombre par défaut, comme l'app Garmin ; thème clair si le système le demande.
 */
:root {
  --bg: #111214;
  --surface: #1a1c1f;
  --surface-2: #24272b;
  --line: #31353a;
  --fg: #f1efe9;
  --fg-2: #a8a69f;
  --brass: #d8a840;
  --brass-ink: #1b1406;
  --low: #5fa8e8;
  --optimal: #4fc56b;
  --high: #f09a3e;
  --very-high: #e8584f;
  --band: rgba(79, 197, 107, 0.30);
  --zone-alpha: 0.16;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --font-display: "Big Shoulders Display", "Arial Narrow", "Roboto Condensed", sans-serif;
  --font-body: "Figtree", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 14px;
  --bar-h: 64px;
  --tab-h: 58px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f2f2ef;
    --surface: #ffffff;
    --surface-2: #ebeae5;
    --line: #d9d7d0;
    --fg: #17181a;
    --fg-2: #5d5b55;
    --brass: #94690f;
    --brass-ink: #ffffff;
    --low: #2271b3;
    --optimal: #1f8a3c;
    --high: #b4610f;
    --very-high: #c03a31;
    --band: rgba(31, 138, 60, 0.20);
    --zone-alpha: 0.12;
    --shadow: 0 10px 30px rgba(30, 28, 20, 0.16);
    color-scheme: light;
  }
}

:root[data-theme="light"] {
  --bg: #f2f2ef;
  --surface: #ffffff;
  --surface-2: #ebeae5;
  --line: #d9d7d0;
  --fg: #17181a;
  --fg-2: #5d5b55;
  --brass: #94690f;
  --brass-ink: #ffffff;
  --low: #2271b3;
  --optimal: #1f8a3c;
  --high: #b4610f;
  --very-high: #c03a31;
  --band: rgba(31, 138, 60, 0.20);
  --zone-alpha: 0.12;
  --shadow: 0 10px 30px rgba(30, 28, 20, 0.16);
  color-scheme: light;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}

body.sheet-open { overflow: hidden; }

button, input, textarea { font: inherit; color: inherit; }

button { cursor: pointer; }

:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }

h1, h2, h3 { margin: 0; text-wrap: balance; }

p { margin: 0; }

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 12px calc(var(--bar-h) + var(--tab-h) + 28px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- En-tête ---------- */

.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding-top: 6px;
}

.brand { display: flex; align-items: center; gap: 10px; }

.brand h1 {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 30px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  line-height: 1;
}

.brand-mark { width: 26px; height: 26px; align-self: center; }
.brand-mark path { fill: none; stroke: var(--brass); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }

.topbar-date { color: var(--fg-2); font-size: 13px; }
.topbar-date::first-letter { text-transform: uppercase; }

.topbar-side { display: flex; align-items: baseline; justify-content: flex-end; gap: 4px 12px; flex-wrap: wrap; }

/* N'apparaît que si quelque chose n'est pas encore enregistré sur le serveur. */
.sync-badge { font-size: 12px; font-weight: 600; color: var(--high); }
.sync-badge::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: currentColor;
  vertical-align: 1px;
}

.demo-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  padding: 12px 14px;
  border: 1px dashed var(--brass);
  border-radius: var(--radius);
  background: var(--surface);
}

.demo-banner p { flex: 1 1 240px; min-width: 0; }

/* ---------- Tableau de bord ---------- */

.dash { display: grid; gap: 14px; }
.dash-main, .dash-side { display: grid; gap: 14px; align-content: start; min-width: 0; }

@media (min-width: 680px) {
  .dash { grid-template-columns: minmax(0, 1.45fr) minmax(0, 1fr); }
}

.section-title {
  margin: 26px 0 12px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fg-2);
}

.link-btn {
  border: 0;
  background: transparent;
  padding: 4px 0;
  color: var(--brass);
  font-weight: 600;
  font-size: 13px;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.card h2 {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fg-2);
}

.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }

.muted { color: var(--fg-2); font-size: 13px; }

.inline-num { color: var(--fg); font-variant-numeric: tabular-nums; }

/* Carte de statut */
.status-card {
  --status: var(--optimal);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-top: 3px solid var(--status);
}

.status-card[data-status="sous"] { --status: var(--low); }
.status-card[data-status="dans"] { --status: var(--optimal); }
.status-card[data-status="dessus"] { --status: var(--brass); }

.eyebrow { font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--fg-2); font-weight: 600; }

.status-row { display: flex; align-items: center; justify-content: space-between; gap: 10px 14px; flex-wrap: wrap; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 15px;
  color: var(--status);
  border: 1.5px solid var(--status);
}

.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--status); }

.gauge { position: relative; display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; height: 8px; margin-top: 4px; }
.seg { border-radius: 2px; opacity: 0.85; }
.seg-low { background: var(--low); }
.seg-opt { background: var(--optimal); }
.seg-up { background: var(--brass); }

.gauge-marker {
  position: absolute;
  top: -6px;
  width: 4px;
  height: 20px;
  margin-left: -2px;
  border-radius: 2px;
  background: var(--fg);
  box-shadow: 0 0 0 2px var(--surface);
  transition: left 0.4s ease;
}

.gauge-labels { display: grid; grid-template-columns: repeat(3, 1fr); font-size: 11px; color: var(--fg-2); text-align: center; }

.status-text { color: var(--fg); max-width: 60ch; }

/* Graphique */
.segmented { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border-radius: 999px; padding: 3px; }

.segmented button {
  border: 0;
  background: transparent;
  border-radius: 999px;
  padding: 8px 10px;
  font-weight: 600;
  color: var(--fg-2);
}

.segmented button[aria-selected="true"] { background: var(--fg); color: var(--bg); }

.card-sub { font-size: 13px; color: var(--fg-2); }

.chart { width: 100%; min-height: 190px; touch-action: pan-y; }
.chart svg { display: block; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--fg-2); font-size: 11px; font-family: var(--font-body); font-variant-numeric: tabular-nums; }
.chart .band { fill: var(--band); stroke: none; }
.chart .line { fill: none; stroke: var(--fg); stroke-width: 2.2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--fg); stroke: none; }
.chart .dot-last { fill: var(--brass); stroke: var(--surface); stroke-width: 2; }
.chart .dot-sel { fill: var(--brass); }
.chart .tick { fill: var(--fg-2); opacity: 0.6; }
.chart .tick-end { opacity: 1; }
.chart .guide { stroke: var(--brass); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .hit { fill: transparent; stroke: none; }
.chart .z-low { fill: var(--low); fill-opacity: var(--zone-alpha); }
.chart .z-opt { fill: var(--band); }
.chart .z-high { fill: var(--brass); fill-opacity: var(--zone-alpha); }

.legend { display: flex; align-items: center; gap: 6px 8px; flex-wrap: wrap; font-size: 12px; color: var(--fg-2); }
.key { display: inline-block; width: 14px; height: 10px; border-radius: 3px; }
.key-band { background: var(--band); }
.key-line { height: 3px; background: var(--fg); margin-left: 8px; }

.chart-detail { font-size: 13px; color: var(--fg-2); min-height: 1.45em; }
.chart-detail strong { color: var(--fg); font-weight: 600; }

.facts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 10px; margin: 0; padding-top: 12px; border-top: 1px solid var(--line); }
.facts div { min-width: 0; }
.facts dt { font-size: 12px; color: var(--fg-2); }
.facts dd { margin: 2px 0 0; font-weight: 700; font-size: 17px; font-variant-numeric: tabular-nums; }

/* Régularité */
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.stats div { display: flex; flex-direction: column; min-width: 0; }
.stat-num { font-family: var(--font-display); font-weight: 800; font-size: 40px; line-height: 1; font-variant-numeric: tabular-nums; }
.stat-unit { font-size: 12px; color: var(--fg-2); }

.reg-grid { display: grid; grid-template-columns: repeat(14, minmax(0, 1fr)); gap: 4px; }
.reg-grid .cell { aspect-ratio: 1; max-width: 100%; border-radius: 4px; background: var(--surface-2); }
.reg-grid .cell.on { background: var(--brass); }
.reg-grid .cell.today { box-shadow: 0 0 0 1.5px var(--fg); }

/* Nouveautés */
.new-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.new-list li { display: grid; grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "tag date" "label date"; align-items: center; gap: 0 10px; }
.new-list .tag { grid-area: tag; }
.new-list .new-label { grid-area: label; overflow-wrap: break-word; }
.new-list li > .muted { grid-area: date; font-size: 13px; }
.new-list li.empty { display: block; color: var(--fg-2); font-size: 13px; }
.new-label { min-width: 0; overflow-wrap: anywhere; font-weight: 500; }
.tag { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--brass); }

/* Morceaux */
.piece-list { list-style: none; margin: 0; padding: 0; display: grid; }
.piece-list li { border-top: 1px solid var(--line); }
.piece-list li:first-child { border-top: 0; }
.piece-list li.empty { border: 0; color: var(--fg-2); font-size: 13px; padding: 2px 0; }

.piece-row {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 2px 12px;
  padding: 10px 0;
  border: 0;
  background: transparent;
  text-align: left;
  color: inherit;
}

.piece-title { min-width: 0; overflow-wrap: anywhere; font-weight: 600; }
.piece-dates { grid-column: 1 / -1; }

.level { display: inline-flex; gap: 4px; align-items: center; }
.level i { width: 10px; height: 10px; border-radius: 50%; background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--line); }
.level i.on { background: var(--brass); box-shadow: none; }
.level-none { font-size: 12px; color: var(--brass); font-weight: 600; }

.chip-level { min-width: 48px; justify-content: center; font-variant-numeric: tabular-nums; }

.pieces-note { margin-top: 14px; max-width: 65ch; }

/* Répartition */
.focus { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(0, 8.5em) minmax(0, 1fr) 4.5em; align-items: center; gap: 10px; font-size: 13px; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: var(--brass); border-radius: 4px; }
.bar-row.none .bar-fill { background: var(--fg-2); }
.bar-value { text-align: right; color: var(--fg-2); font-variant-numeric: tabular-nums; }

.empty { color: var(--fg-2); font-size: 13px; }

/* ---------- Autres vues ---------- */

.view-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }

.view-title { font-family: var(--font-display); font-weight: 800; font-size: 28px; text-transform: uppercase; letter-spacing: 0.02em; }

#view-reglages { display: flex; flex-direction: column; gap: 14px; }
#view-reglages[hidden] { display: none; }

.carnet-grid { display: grid; gap: 14px; }
@media (min-width: 680px) { .carnet-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; } }

.carnet-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.carnet-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.carnet-list li { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); }
.carnet-list li:first-child { border-top: 0; }
.carnet-item { display: flex; flex-direction: column; min-width: 0; }

.session-list { display: grid; gap: 12px; }
.day-group { background: var(--surface); border-radius: var(--radius); padding: 12px 16px; }
.day-group ul { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 4px; }
.day-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.day-head h3 { font-size: 15px; font-weight: 600; }
.day-head h3::first-letter { text-transform: uppercase; }
.session-row { display: grid; grid-template-columns: 4.5em minmax(0, 1fr) auto; align-items: center; gap: 8px; }
.session-min { font-weight: 700; font-variant-numeric: tabular-nums; }
.session-doms { overflow-wrap: anywhere; }

#more-sessions { margin-top: 12px; }

.form h3 { font-size: 16px; }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span, .field-label { font-size: 13px; color: var(--fg-2); font-weight: 600; }
.optional { font-weight: 400; }

input[type="number"], input[type="text"], input[type="date"], textarea {
  width: 100%;
  min-width: 0;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 16px;
}

textarea { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; font-size: 12px; resize: vertical; }

.about summary { cursor: pointer; font-weight: 600; }
.about p { color: var(--fg-2); font-size: 14px; max-width: 65ch; }

/* ---------- Boutons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-weight: 700;
  white-space: nowrap;
}

.btn-brass { background: var(--brass); color: var(--brass-ink); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--fg); }
.btn-danger { background: transparent; border-color: var(--very-high); color: var(--very-high); }
.btn-danger.confirm { background: var(--very-high); color: var(--bg); }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.chips input[type="date"] { flex: 1 1 150px; width: auto; }

.chip {
  min-height: 40px;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-weight: 600;
  font-size: 14px;
}

.chip[aria-pressed="true"] { background: var(--brass); color: var(--brass-ink); border-color: var(--brass); }
.chip-small { min-height: 32px; padding: 4px 10px; font-size: 12px; }

.icon-btn {
  min-width: 36px;
  min-height: 36px;
  padding: 4px 8px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--fg-2);
  font-size: 18px;
  line-height: 1;
}

.icon-btn.confirm { font-size: 12px; font-weight: 700; color: var(--very-high); border-color: var(--very-high); }

/* ---------- Barres fixes ---------- */

.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom, 0px));
  z-index: 20;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px max(16px, env(safe-area-inset-right, 0px)) 10px max(16px, env(safe-area-inset-left, 0px));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.actionbar[hidden] { display: none; }
.btn-wide { flex: 1 1 auto; }

.timer-running { display: flex; align-items: center; gap: 10px; }
.timer-running[hidden] { display: none; }
.timer-clock { font-family: var(--font-display); font-weight: 800; font-size: 26px; font-variant-numeric: tabular-nums; min-width: 3.2em; }
.rec { width: 10px; height: 10px; border-radius: 50%; background: var(--very-high); animation: pulse 1.4s ease-in-out infinite; }

@keyframes pulse { 50% { opacity: 0.25; } }

@media (min-width: 680px) {
  .actionbar { max-width: 1100px; margin: 0 auto; }
  .btn-wide { flex: 0 0 auto; }
}

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 21;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  height: calc(var(--tab-h) + env(safe-area-inset-bottom, 0px));
  padding: 0 max(8px, env(safe-area-inset-right, 0px)) env(safe-area-inset-bottom, 0px) max(8px, env(safe-area-inset-left, 0px));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.tabbar button {
  border: 0;
  background: transparent;
  color: var(--fg-2);
  font-weight: 600;
  font-size: 13px;
  padding: 0 2px;
  position: relative;
}

@media (max-width: 360px) {
  .tabbar button { font-size: 12px; }
}

.tabbar button[aria-current="page"] { color: var(--fg); }
.tabbar button[aria-current="page"]::after {
  content: "";
  position: absolute;
  left: 30%;
  right: 30%;
  top: 0;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: var(--brass);
}

/* ---------- Feuilles ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55);
}

.sheet-backdrop[hidden] { display: none; }

#sheet-login { z-index: 45; background: rgba(0, 0, 0, 0.78); }

.sheet {
  width: 100%;
  max-width: 560px;
  max-height: calc(100% - 24px - env(safe-area-inset-top, 0px));
  overflow-y: auto;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 20px max(16px, env(safe-area-inset-right, 0px)) calc(16px + env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: var(--shadow);
}

@media (min-width: 680px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 20px; }
}

.sheet h2 { font-family: var(--font-display); font-weight: 800; font-size: 26px; text-transform: uppercase; letter-spacing: 0.02em; }

.sheet-actions { display: flex; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }

.chips-scroll { max-height: 9.5em; overflow-y: auto; }

.form-error { color: var(--very-high); font-size: 14px; font-weight: 600; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-h) + var(--bar-h) + 14px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 50;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--fg);
  color: var(--bg);
  font-weight: 600;
  font-size: 14px;
  box-shadow: var(--shadow);
}

.toast[hidden] { display: none; }

@media (prefers-reduced-motion: reduce) {
  .gauge-marker { transition: none; }
  .rec { animation: none; }
}
