:root {
  color-scheme: light;
  --bg: #f7f5f0;
  --surface: #ffffff;
  --text: #2b2419;
  --text-muted: #6b6152;
  --border: #e2ddd0;
  --accent: #b5651d;
  --accent-dark: #8f4e15;
  --success-bg: #eaf6ea;
  --success-text: #2c6b2f;
  --error-bg: #fbe9e7;
  --error-text: #9b2c1f;
  --danger: #b3261e;
  --warn-bg: #fff4e0;
  --warn-text: #9a6a12;
  /* Palette catégorielle des graphiques (ordre fixe, validé daltonisme) : une race garde sa couleur. */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;
  --chart-grid: #ece8de;
  --chart-axis: #c9c2b2;
  --chart-muted: #a39a88;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

a { color: var(--accent-dark); }

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-weight: 700;
  text-decoration: none;
  color: var(--text);
  margin-right: auto;
}

.nav { display: flex; gap: 14px; flex-wrap: wrap; }
.nav a { text-decoration: none; color: var(--text-muted); padding: 6px 4px; }
.nav a.active, .nav a:hover { color: var(--accent-dark); border-bottom: 2px solid var(--accent); }

.logout-form { margin-left: 8px; }

.link-button {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  padding: 0;
}

.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 16px;
}

.flash {
  padding: 10px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
  font-size: 14px;
}
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}

.page-header h1 { margin: 0; font-size: 1.4rem; }

.btn {
  display: inline-block;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  font-size: 15px;
  cursor: pointer;
}
.btn:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-danger { background: var(--surface); color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: var(--error-bg); }
.btn-block { display: block; width: 100%; text-align: center; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 14px;
}

.card-link {
  display: block;
  text-decoration: none;
  color: inherit;
}
.card-link:hover { border-color: var(--accent); }

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
}
th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
th { color: var(--text-muted); font-weight: 600; }
.table-wrap { overflow-x: auto; }

form.stacked { display: flex; flex-direction: column; gap: 14px; }

label { display: block; font-weight: 600; margin-bottom: 6px; font-size: 14px; }
.hint { color: var(--text-muted); font-weight: 400; font-size: 13px; }

input[type=text], input[type=number], input[type=date], input[type=email],
input[type=password], input[type=file], select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  background: var(--surface);
  color: var(--text);
}
textarea { min-height: 80px; resize: vertical; }

.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input { width: auto; }
.checkbox-row label { margin: 0; font-weight: 500; }

.form-row { display: flex; gap: 12px; flex-wrap: wrap; }
.form-row > * { flex: 1 1 200px; }

.actions { display: flex; gap: 10px; flex-wrap: wrap; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge-ok { background: var(--success-bg); color: var(--success-text); }
.badge-warn { background: #fff4e0; color: #9a6a12; }
.badge-danger { background: var(--error-bg); color: var(--error-text); }
.badge-muted { background: var(--bg); color: var(--text-muted); }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
}
.stat-tile .value { font-size: 1.6rem; font-weight: 700; }
.stat-tile .label { color: var(--text-muted); font-size: 13px; }

.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
  margin: 12px 0;
}
.photo-grid img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.photo-item { position: relative; }
.photo-item form { position: absolute; top: 4px; right: 4px; }
.photo-item .btn-danger { padding: 2px 8px; font-size: 12px; }

.login-wrap {
  max-width: 380px;
  margin: 60px auto;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 16px;
}

/* ------------------------------------------------------------------ */
/* Mise en page                                                        */
/* ------------------------------------------------------------------ */

.container { max-width: 1140px; }

h2 { font-size: 1.1rem; margin: 0 0 12px; }
h3 { font-size: 1rem; margin: 18px 0 10px; }
.card > h2:first-child, .card > .card-head:first-child { margin-top: 0; }

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

.split { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 900px) {
  .split { grid-template-columns: 1fr 1fr; }
  .split-wide { grid-template-columns: 2fr 1fr; }
}
.split > .card { margin-bottom: 0; }
.split + .card, .split + .split { margin-top: 14px; }

.subtitle { color: var(--text-muted); margin: 4px 0 0; font-size: 14px; }
.subtitle a { color: inherit; }
.muted { color: var(--text-muted); }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
th.num { text-align: right; }

.btn-small { padding: 6px 10px; font-size: 13px; }
.btn[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }

.stat-grid.compact { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.stat-grid.compact .stat-tile { padding: 10px 12px; }
.stat-grid.compact .value { font-size: 1.3rem; }
.stat-tile .sub { color: var(--text-muted); font-size: 12px; }
a.stat-tile { text-decoration: none; color: inherit; }
a.stat-tile:hover { border-color: var(--accent); }

/* ------------------------------------------------------------------ */
/* Filtres instantanés                                                 */
/* ------------------------------------------------------------------ */

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 12px;
  align-items: flex-end;
}
.filters > div { flex: 1 1 150px; min-width: 0; }
.filters > .filter-search { flex: 2 1 220px; }
/* Un groupe de puces a besoin de place pour tenir sur une ligne. */
.filters > div:has(> .chips) { flex: 2 1 260px; }
.filters label { font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.filters select, .filters input[type=search] { padding: 9px 10px; font-size: 15px; }
.filters input[type=search] {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.filters .filter-meta {
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  align-items: center;
  font-size: 13px;
  color: var(--text-muted);
  padding-bottom: 8px;
}
.filters .checkbox-row { padding-bottom: 8px; flex: 0 0 auto; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
}
.chips button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Puces à bascule multiple (ex. races incluses) : l'état actif se lit à la bordure,
   pour ne pas masquer la pastille de couleur de la race. */
.chips.toggles button[aria-pressed="true"] { background: var(--surface); color: var(--text); border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.chips.toggles button[aria-pressed="false"] { opacity: 0.55; text-decoration: line-through; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button {
  border: 0;
  background: var(--surface);
  color: var(--text-muted);
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
}
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button[aria-pressed="true"] { background: var(--bg); color: var(--text); font-weight: 600; }

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--text-muted);
}

/* ------------------------------------------------------------------ */
/* Tableaux                                                            */
/* ------------------------------------------------------------------ */

.table-wrap { border: 1px solid var(--border); border-radius: 10px; }
.card .table-wrap { border: 0; border-radius: 0; margin: 0 -4px; }
tr[data-href] { cursor: pointer; }
tr[data-href]:hover td { background: var(--bg); }
th[data-sort] { cursor: pointer; user-select: none; white-space: nowrap; }
th[data-sort]::after { content: " ↕"; color: var(--border); font-size: 11px; }
th[aria-sort="ascending"]::after { content: " ↑"; color: var(--accent); }
th[aria-sort="descending"]::after { content: " ↓"; color: var(--accent); }
td.ring { font-weight: 600; white-space: nowrap; }
tr.is-dead td { color: var(--text-muted); }
tr.is-dead td.ring { text-decoration: line-through; }

.sex { display: inline-block; min-width: 1.2em; font-weight: 700; text-align: center; font-size: 1.1em; }
.small { font-size: 12px; }
.sex-male { color: var(--series-1); }
.sex-femelle { color: var(--series-5); }
.sex-inconnu { color: var(--text-muted); }

.delta-up { color: var(--success-text); }
.delta-down { color: var(--error-text); }

.rank {
  display: inline-block;
  min-width: 26px;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--bg);
  font-weight: 700;
  font-size: 12px;
  text-align: center;
}
.rank-top { background: var(--success-bg); color: var(--success-text); }

.flag { font-size: 12px; color: var(--warn-text); white-space: nowrap; }

.badge-warn { background: var(--warn-bg); color: var(--warn-text); }

/* ------------------------------------------------------------------ */
/* Grilles de cartes                                                   */
/* ------------------------------------------------------------------ */

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
}
.card-grid.small { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.item-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.item-card[data-href] { cursor: pointer; }
.item-card[data-href]:hover { border-color: var(--accent); }
.item-card .title { font-weight: 700; display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.item-card .title .muted { font-weight: 400; font-size: 13px; }
.item-card .meta { font-size: 13px; color: var(--text-muted); }
.item-card .figures { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; }
.item-card .figures strong { display: block; font-size: 1.05rem; color: var(--text); }
.item-card .thumb {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: 8px;
  background: var(--bg);
}
.item-card .thumb-empty {
  display: grid;
  place-items: center;
  font-size: 2rem;
  color: var(--text-muted);
}

.breed-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: baseline;
  background: var(--chart-muted);
}
.breed-dot[data-slot="1"] { background: var(--series-1); }
.breed-dot[data-slot="2"] { background: var(--series-2); }
.breed-dot[data-slot="3"] { background: var(--series-3); }
.breed-dot[data-slot="4"] { background: var(--series-4); }
.breed-dot[data-slot="5"] { background: var(--series-5); }
.breed-dot[data-slot="6"] { background: var(--series-6); }
.breed-dot[data-slot="7"] { background: var(--series-7); }
.breed-dot[data-slot="8"] { background: var(--series-8); }

/* ------------------------------------------------------------------ */
/* Âge d'un lot : frise jusqu'à 120 j (objectif) et 135 j (limite)     */
/* ------------------------------------------------------------------ */

.age-bar { position: relative; height: 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); }
.age-bar .fill { position: absolute; inset: 0 auto 0 0; border-radius: 999px; background: var(--accent); max-width: 100%; }
.age-bar .fill.over { background: var(--danger); }
.age-bar .tick { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text-muted); }
.age-bar-legend { position: relative; display: flex; justify-content: space-between; font-size: 12px; color: var(--text-muted); margin-top: 3px; }
.age-bar-legend .at { position: absolute; top: 0; transform: translateX(-50%); }
.spark-row { color: var(--accent); }

.timeline { display: flex; flex-direction: column; gap: 12px; }
.timeline-row { display: grid; grid-template-columns: minmax(130px, 220px) 1fr auto; gap: 12px; align-items: center; font-size: 14px; }
.timeline-row a { text-decoration: none; color: var(--text); font-weight: 600; }
.timeline-row .muted { font-size: 12px; }
@media (max-width: 600px) {
  .timeline-row { grid-template-columns: 1fr auto; }
  .timeline-row .age-bar-wrap { grid-column: 1 / -1; }
}

/* ------------------------------------------------------------------ */
/* Graphiques                                                          */
/* ------------------------------------------------------------------ */

.chart-box { position: relative; height: 300px; }
.chart-box.tall { height: 380px; }
.chart-box.short { height: 220px; }
.chart-note { font-size: 12px; color: var(--text-muted); margin-top: 8px; }

/* ------------------------------------------------------------------ */
/* À faire (tableau de bord)                                           */
/* ------------------------------------------------------------------ */

.todo { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.todo li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  flex-wrap: wrap;
}
.todo li .what { display: flex; gap: 10px; align-items: center; }
.todo .icon { font-size: 1.1rem; }

/* ------------------------------------------------------------------ */
/* Généalogie                                                          */
/* ------------------------------------------------------------------ */

.pedigree-wrap { overflow-x: auto; }
.pedigree {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  grid-template-rows: repeat(8, minmax(44px, auto));
  gap: 6px 14px;
  min-width: 640px;
}
.pedigree .node {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background: var(--surface);
  min-width: 0;
}
.pedigree .node a { font-weight: 700; text-decoration: none; }
.pedigree .node .muted { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pedigree .node.unknown { border-style: dashed; color: var(--text-muted); }
.pedigree .node.subject { border-color: var(--accent); border-width: 2px; }
.pedigree .node.male { border-left: 3px solid var(--series-1); }
.pedigree .node.femelle { border-left: 3px solid var(--series-5); }

.composition { display: flex; height: 14px; border-radius: 4px; overflow: hidden; gap: 2px; background: var(--surface); }
.composition span { display: block; height: 100%; background: var(--chart-muted); }
.composition span[data-slot="1"] { background: var(--series-1); }
.composition span[data-slot="2"] { background: var(--series-2); }
.composition span[data-slot="3"] { background: var(--series-3); }
.composition span[data-slot="4"] { background: var(--series-4); }
.composition span[data-slot="5"] { background: var(--series-5); }
.composition span[data-slot="6"] { background: var(--series-6); }
.composition span[data-slot="7"] { background: var(--series-7); }
.composition span[data-slot="8"] { background: var(--series-8); }
.composition-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; margin-top: 6px; }

.inbreeding-none, .inbreeding-low { color: var(--success-text); }
.inbreeding-medium { color: var(--warn-text); }
.inbreeding-high, .inbreeding-critical { color: var(--error-text); font-weight: 700; }

/* Matrice de consanguinité mâles × femelles */
.matrix-wrap { overflow: auto; max-height: 70vh; }
.matrix { border-collapse: separate; border-spacing: 2px; width: auto; background: transparent; }
.matrix th, .matrix td { border: 0; padding: 6px 8px; font-size: 12px; text-align: center; white-space: nowrap; }
.matrix thead th { position: sticky; top: 0; background: var(--surface); z-index: 1; }
.matrix tbody th { position: sticky; left: 0; background: var(--surface); text-align: left; }
.matrix td button {
  width: 100%;
  border: 0;
  border-radius: 4px;
  padding: 6px 4px;
  cursor: pointer;
  font: inherit;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  background: var(--bg);
}
.matrix td button[data-level="low"] { background: var(--success-bg); }
.matrix td button[data-level="medium"] { background: var(--warn-bg); color: var(--warn-text); }
.matrix td button[data-level="high"], .matrix td button[data-level="critical"] { background: var(--error-bg); color: var(--error-text); font-weight: 700; }
.matrix td button[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ------------------------------------------------------------------ */
/* Saisie en série (pesée du lot, création de poussins, sélection)     */
/* ------------------------------------------------------------------ */

.bulk-table input[type=number] { max-width: 130px; padding: 10px; font-size: 17px; }
.bulk-table input.is-lower { border-color: var(--danger); background: var(--error-bg); }
.bulk-table td { vertical-align: middle; }
.bulk-table select { min-width: 150px; padding: 8px; font-size: 15px; }
.sticky-actions {
  position: sticky;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 0 4px;
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.ring-preview { display: flex; flex-wrap: wrap; gap: 6px; }
.ring-preview span { font-size: 13px; padding: 2px 8px; border-radius: 6px; background: var(--bg); border: 1px solid var(--border); }
.ring-preview span.conflict { background: var(--error-bg); color: var(--error-text); border-color: var(--danger); }

/* ------------------------------------------------------------------ */
/* Thème sombre                                                        */
/* ------------------------------------------------------------------ */

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #1c1a16;
    --surface: #262319;
    --text: #f0ece2;
    --text-muted: #a89f8d;
    --border: #3a362c;
    --accent: #d98a3d;
    --accent-dark: #f0a55c;
    --success-bg: #1f3a20;
    --success-text: #a4e0a6;
    --error-bg: #3a221e;
    --error-text: #f2a99e;
    --danger: #f2a99e;
    --warn-bg: #3a2f17;
    --warn-text: #f0c46a;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --chart-grid: #332f26;
    --chart-axis: #4a4538;
    --chart-muted: #6f6857;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1c1a16;
  --surface: #262319;
  --text: #f0ece2;
  --text-muted: #a89f8d;
  --border: #3a362c;
  --accent: #d98a3d;
  --accent-dark: #f0a55c;
  --success-bg: #1f3a20;
  --success-text: #a4e0a6;
  --error-bg: #3a221e;
  --error-text: #f2a99e;
  --danger: #f2a99e;
  --warn-bg: #3a2f17;
  --warn-text: #f0c46a;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
  --chart-grid: #332f26;
  --chart-axis: #4a4538;
  --chart-muted: #6f6857;
}
