/* Theme variables with accessible contrast */
:root {
  /* Light theme */
  --page-bg: #f5f7fb;
  --surface: #ffffff;
  --text: #0f172a;    /* slate-900 */
  --muted: #475569;   /* slate-600 */
  --border: rgba(15,23,42,0.12);
  --stripe: rgba(15,23,42,0.04);
  --primary: #0d6efd; /* bootstrap blue */
  --link: #0d6efd;
}

/* Dark & Black theme variables override — Black is Dark with a darker page/surface,
   everything else (text, badges, semantic colors) is shared between the two. */
:root[data-theme="dark"], :root[data-theme="black"] {
  --text: #e5e7eb;      /* slate-200 */
  --muted: #94a3b8;     /* slate-400 */
  --border: rgba(148,163,184,0.25);
  --stripe: rgba(148,163,184,0.08);
  --primary: #6ea8fe;
  --link: #82b1ff;

  /* Sync Bootstrap body vars so components that reference --bs-body-bg get the dark surface */
  --bs-body-bg: var(--surface);
  --bs-body-color: var(--text);

  /* Semantic subtle colors — Bootstrap uses these for table-danger, table-secondary, etc.
     Light-mode defaults are bright pastels (#f8d7da, #e2e3e5) that look terrible on dark. */
  --bs-danger-bg-subtle: rgba(220, 53, 69, 0.18);
  --bs-danger-text-emphasis: #ea868f;
  --bs-danger-border-subtle: rgba(220, 53, 69, 0.3);
  --bs-secondary-bg-subtle: rgba(108, 117, 125, 0.15);
  --bs-secondary-text-emphasis: #a7acb1;
  --bs-secondary-border-subtle: rgba(108, 117, 125, 0.2);
  --bs-success-bg-subtle: rgba(25, 135, 84, 0.18);
  --bs-success-text-emphasis: #75b798;
  --bs-success-border-subtle: rgba(25, 135, 84, 0.3);

  /* list-group items (used in ADG chart section) inherit the card surface instead of white */
  --bs-list-group-bg: transparent;
  --bs-list-group-color: var(--text);
  --bs-list-group-border-color: var(--border);
  --bs-list-group-action-hover-bg: var(--stripe);
  --bs-list-group-action-hover-color: var(--text);
  --bs-list-group-action-active-bg: var(--stripe);
  --bs-list-group-action-active-color: var(--text);
  --bs-list-group-disabled-color: var(--muted);
}
:root[data-theme="dark"] {
  --page-bg: #0d1117;
  --surface: #111827;   /* slate-900-ish */
}
:root[data-theme="black"] {
  --page-bg: #000000;
  --surface: #0a0a0d;
}

html, body { height: 100%; }
body { background: var(--page-bg); color: var(--text); padding-top: 64px; }

/* Large forms should always remain usable on short screens, even if a modal
   misses Bootstrap's modal-dialog-scrollable class. */
.modal-dialog {
  max-height: calc(100vh - var(--bs-modal-margin, 1.75rem) * 2);
}
.modal-content {
  max-height: calc(100vh - var(--bs-modal-margin, 1.75rem) * 2);
}
.modal-body {
  overflow-y: auto;
}

/* Typography: subtle console vibe on titles & brand */
h1, h2, h3, .navbar-brand {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  letter-spacing: 0.2px;
}

/* Navbar remains dark for strong contrast in both themes */
.navbar.bg-dark { background: #101317 !important; }
.navbar .nav-link { color: #d1d5db; }
.navbar .nav-link:hover, .navbar .nav-link.active { color: #ffffff; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.table { color: var(--text); background: transparent; }
.table thead th { color: var(--muted); border-color: var(--border); background: transparent; }
.table tbody td { border-color: var(--border); }
.table-striped>tbody>tr:nth-of-type(odd) { --bs-table-accent-bg: var(--stripe); }
.table > :not(caption) > * > * { background-color: transparent; box-shadow: none; }

/* Hover state */
.table tbody tr:hover { background-color: rgba(15,23,42,0.03); }

/* Make IDs/numbers feel technical */
.table td:first-child, .table th:first-child { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
.table-responsive { border-radius: 10px; overflow-x: auto; }

.btn-primary { background-color: var(--primary); border-color: var(--primary); }
.btn-primary:hover { filter: brightness(0.95); }
.btn-outline-secondary { color: var(--text); border-color: var(--border); }
.btn-outline-secondary:hover { background: var(--stripe); }

.badge.bg-light { background: rgba(0,0,0,0.06) !important; color: var(--text) !important; }
.badge.bg-info { background: rgba(13,110,253,0.15) !important; color: var(--text) !important; }
.badge.bg-secondary { background: rgba(100,116,139,0.18) !important; color: var(--text) !important; }

/* Tweak badges for dark */
:root[data-theme="dark"], :root[data-theme="black"] .badge.bg-light { background: rgba(255,255,255,0.12) !important; }
:root[data-theme="dark"], :root[data-theme="black"] .badge.bg-info { background: rgba(110,168,254,0.22) !important; }
:root[data-theme="dark"], :root[data-theme="black"] .badge.bg-secondary { background: rgba(148,163,184,0.22) !important; }

/* Restore semantic table row tints stripped by our cell background-color: transparent rule.
   Apply in both themes so deceased/processed rows are visually distinct everywhere. */
.table tr.table-danger > td,
.table tr.table-danger > th {
  background-color: rgba(220, 53, 69, 0.1);
}
.table tr.table-secondary > td,
.table tr.table-secondary > th {
  background-color: rgba(108, 117, 125, 0.08);
}
/* Dark mode: Bootstrap's default text-emphasis for these variants is a dark color — override. */
:root[data-theme="dark"], :root[data-theme="black"] .table tr.table-danger > td,
:root[data-theme="dark"], :root[data-theme="black"] .table tr.table-danger > th {
  color: var(--text);
  background-color: rgba(220, 53, 69, 0.12);
}
:root[data-theme="dark"], :root[data-theme="black"] .table tr.table-secondary > td,
:root[data-theme="dark"], :root[data-theme="black"] .table tr.table-secondary > th {
  color: var(--text);
  background-color: rgba(108, 117, 125, 0.1);
}

.form-control, .form-select { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.form-control::placeholder { color: var(--muted); }
.form-label { color: var(--muted); }

.modal-content { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.modal-header { border-bottom-color: var(--border); }
.modal-footer { border-top-color: var(--border); }

/* Photo grid */
.photo-thumb { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 8px; }

/* Gallery (inline, accessible) */
.gallery { display: grid; gap: 10px; }
.gallery-preview { position: relative; display: flex; align-items: center; justify-content: center; min-height: 320px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.gallery-preview img { max-width: 100%; max-height: 60vh; object-fit: contain; }
.gallery-nav { position: absolute; top: 50%; transform: translateY(-50%); opacity: 0.9; }
.gallery-nav.prev { left: 10px; }
.gallery-nav.next { right: 10px; }
.gallery-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.gallery-thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; max-height: 160px; overflow-y: auto; padding: 2px; }
.gallery-thumbs .thumb { display: block; padding: 0; border: 2px solid transparent; background: transparent; border-radius: 8px; line-height: 0; }
.gallery-thumbs .thumb img { width: 100%; aspect-ratio: 1/1; object-fit: cover; border-radius: 6px; }
.gallery-thumbs .thumb.active { border-color: var(--primary); box-shadow: 0 0 0 2px color-mix(in oklab, var(--primary), transparent 70%); }
.gallery-thumbs .thumb:focus { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Pig legend (scrollable pills) */
.pig-legend { display: flex; gap: 8px; flex-wrap: nowrap; overflow-x: auto; padding: 4px 2px; }
.pig-legend .btn { white-space: nowrap; }
.pig-legend .btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }
/* Gallery thumbs highlight */
#photoThumbs .thumb-btn { border: 2px solid transparent !important; border-radius: 8px; }
#photoThumbs .thumb-btn.active { border-color: var(--primary) !important; box-shadow: 0 0 0 2px color-mix(in oklab, var(--primary), transparent 70%); }

/* Chart canvas container */
.chart-card { padding: 16px; }
.chart-fixed { height: 320px; position: relative; }
@media (min-width: 768px) {
  .chart-fixed { height: 380px; }
}
@media (min-width: 1200px) {
  .chart-fixed { height: 440px; }
}

/* Utility spacing tweaks */
.page-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.page-header h2 { margin: 0; }
.page-form-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.page-form-header h2 { margin: 0; }

/* Links */
a { color: var(--link); }
a:hover { filter: brightness(0.9); }

/* Subtle inline links to match muted UI */
.link-quiet { color: inherit; text-decoration: none; border-bottom: 1px dashed transparent; padding: 0 2px; border-radius: 4px; }
.link-quiet:hover { color: inherit; background: var(--stripe); border-bottom-color: var(--border); }

/* Compact icon buttons */
.icon-btn { padding: 0; line-height: 1; }

/* Responsive containers */
@media (max-width: 576px) { .container { padding-left: 16px; padding-right: 16px; } }

/* Narrow page width for a more focused dashboard layout — widens a couple of
   steps on large screens (matching Bootstrap's own xl/xxl container caps) so
   pages aren't stranded in a tiny column on wide/ultra-wide monitors. */
.container-narrow { max-width: 900px; }
@media (min-width: 1200px) { .container-narrow { max-width: 1140px; } }
@media (min-width: 1400px) { .container-narrow { max-width: 1320px; } }

/* Compact stats grid */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.stat { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--surface); }
.stat .label { color: var(--muted); font-size: 0.85rem; }
.stat .value { font-weight: 600; }

/* Tiny uppercase column label used inside list-style cards */
.list-card-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 2px;
  line-height: 1.2;
}

/* Animals page filter toolbar */
#animalSpeciesSwitch .btn {
  flex-shrink: 0;
}
.animals-filter-label {
  min-width: 3rem;
  white-space: nowrap;
}
.animal-tag-filter-btn {
  background: #fff;
  color: #212529;
  border: 1px solid #ced4da;
}
.animal-tag-filter-btn:hover,
.animal-tag-filter-btn:focus {
  background: #f8f9fa;
  color: #0f5967;
  border-color: #0f5967;
}
.btn-check:checked + .animal-tag-filter-btn,
.animal-tag-filter-btn.is-selected {
  background: #fff;
  color: #0f5967;
  border-color: #0f5967;
  box-shadow: inset 0 0 0 1px #0f5967;
}
.animal-tag-role-btn {
  background: #f8f9fa;
  color: #6c757d;
  border: 1px solid #dee2e6;
}
.animal-tag-role-btn:hover,
.animal-tag-role-btn:focus {
  background: #f8f9fa;
  color: #0f5967;
  border-color: #0f5967;
}
.btn-check:checked + .animal-tag-role-btn,
.animal-tag-role-btn.is-selected {
  background: #f8f9fa;
  color: #0f5967;
  border-color: #0f5967;
  box-shadow: inset 0 0 0 1px #0f5967;
}
.animal-filter-attention-btn {
  background: #fff;
  color: #856404;
  border: 1px solid #ffc107;
}
.animal-filter-attention-btn:hover,
.animal-filter-attention-btn:focus {
  background: #fff3cd;
  color: #856404;
  border-color: #e0a800;
}
.btn-check:checked + .animal-filter-attention-btn,
.animal-filter-attention-btn.is-selected {
  background: #fff3cd;
  color: #856404;
  border-color: #e0a800;
  box-shadow: inset 0 0 0 1px #e0a800;
}
.animal-tag-badge {
  background: #fff;
  color: #212529;
  border: 1px solid #ced4da;
}
.animal-derived-tag-badge {
  background: #fff;
  color: #0f5967;
  border: 1px solid #0f5967;
}

/* Dark mode overrides for filter chips and tag badges */
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-filter-btn {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-filter-btn:hover,
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-filter-btn:focus {
  background: var(--stripe);
  color: var(--primary);
  border-color: var(--primary);
}
:root[data-theme="dark"], :root[data-theme="black"] .btn-check:checked + .animal-tag-filter-btn,
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-filter-btn.is-selected {
  background: rgba(110,168,254,0.15);
  color: var(--primary);
  border-color: var(--primary);
  box-shadow: inset 0 0 0 1px var(--primary);
}
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-role-btn {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
}
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-role-btn:hover,
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-role-btn:focus {
  background: var(--stripe);
  color: var(--primary);
  border-color: var(--primary);
}
:root[data-theme="dark"], :root[data-theme="black"] .btn-check:checked + .animal-tag-role-btn,
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-role-btn.is-selected {
  background: rgba(110,168,254,0.12);
  color: var(--primary);
  border-color: var(--primary);
  box-shadow: inset 0 0 0 1px var(--primary);
}
:root[data-theme="dark"], :root[data-theme="black"] .animal-filter-attention-btn {
  background: transparent;
  color: #ffc107;
  border-color: rgba(255,193,7,0.45);
}
:root[data-theme="dark"], :root[data-theme="black"] .animal-filter-attention-btn:hover,
:root[data-theme="dark"], :root[data-theme="black"] .animal-filter-attention-btn:focus {
  background: rgba(255,193,7,0.1);
  color: #ffd54f;
  border-color: #ffc107;
}
:root[data-theme="dark"], :root[data-theme="black"] .animal-filter-attention-btn.is-selected {
  background: rgba(255,193,7,0.18);
  color: #ffd54f;
  border-color: #ffc107;
  box-shadow: inset 0 0 0 1px #ffc107;
}
:root[data-theme="dark"], :root[data-theme="black"] .animal-tag-badge {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
:root[data-theme="dark"], :root[data-theme="black"] .animal-derived-tag-badge {
  background: transparent;
  color: var(--primary);
  border-color: var(--primary);
}

/* Calendar layout: fixed grid, equal columns */
.calendar-table { table-layout: fixed; width: 100%; }
.calendar-table th, .calendar-table td { width: 14.285%; vertical-align: top; }
.calendar-cell { padding: 6px; }
.calendar-day { min-height: 22px; }
.calendar-events { overflow-y: auto; padding-right: 2px; }
.calendar-chips { display: flex; flex-wrap: wrap; gap: 4px; align-items: flex-start; }
.event-chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 6px; font-size: 12px; line-height: 1.2; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); color: inherit; text-decoration: none; max-width: 100%; }
.event-chip .bi { opacity: 0.9; }
.event-chip .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Type color accents via icon color */
.event-chip.evt-weight .bi { color: var(--primary); }
.event-chip.evt-medical .bi { color: #dc3545; }
.event-chip.evt-ai .bi { color: #6f42c1; }
.event-chip.evt-pregnancy .bi { color: #d63384; }
.event-chip.evt-farrow .bi { color: #198754; }
.event-chip.evt-standing .bi { color: #fd7e14; }
.event-chip.evt-death .bi { color: #6c757d; }
.event-chip.evt-processing .bi { color: #0d6efd; }
.event-chip.evt-note .bi { color: #20c997; }
.field-test-results {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-width: 100%;
}
.field-test-result {
  display: inline-flex;
  flex-direction: column;
  flex: 1 1 118px;
  min-width: 0;
  max-width: 170px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  overflow-wrap: anywhere;
  white-space: normal;
}
.field-test-result-key {
  color: var(--muted);
  font-size: 0.74rem;
  line-height: 1.1;
}
.field-test-result-value {
  font-size: 0.98rem;
  font-weight: 700;
  line-height: 1.2;
  overflow-wrap: anywhere;
}
.field-test-result-healthy {
  border-color: #75b798;
  background: #e9f7ef;
  color: #0f5132;
}
.field-test-result-caution {
  border-color: #ffda6a;
  background: #fff8db;
  color: #664d03;
}
.field-test-result-poor {
  border-color: #f1aeb5;
  background: #f8d7da;
  color: #842029;
}
@media (max-width: 992px) {
  .calendar-events { max-height: none; }
}
@media (max-width: 576px) {
  .container-narrow { max-width: 100%; }
  .calendar-events { max-height: none; }
}
@media (max-width: 576px) {
  .animals-toolbar-compact {
    grid-template-columns: 1fr;
  }
}

/* Fix calendar section height on Events page */
.calendar-fixed { height: 540px; }
.calendar-fixed .table-responsive { height: 100%; }
/* Force a non-expanding 6-row grid even with many events */
.calendar-fixed .calendar-table { height: 100%; table-layout: fixed; border-collapse: separate; border-spacing: 0; }
.calendar-fixed .calendar-table thead { display: table; width: 100%; table-layout: fixed; }
/* Reserve space for 6 body rows below the header */
.calendar-fixed .calendar-table tbody { display: block; height: calc(100% - 40px); overflow: hidden; }
.calendar-fixed .calendar-table tbody tr { display: table; width: 100%; table-layout: fixed; height: calc(100% / 6); }
.calendar-fixed .calendar-table td { vertical-align: top; overflow: hidden; }
.cal-grid { display: grid; grid-template-rows: auto 1fr; gap: 8px; }
.cal-header { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-body { display: grid; grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(6, 1fr); gap: 4px; height: 720px; }
.cal-body .cal-cell { border: 1px solid var(--border); border-radius: 8px; padding: 6px; overflow: hidden; background: var(--surface); }
.cal-body .calendar-events { height: calc(100% - 26px); overflow-y: auto; }

/* Animal chip hover popup */
.animal-card-popover .popover-body { padding: 0; min-width: 195px; max-width: 245px; overflow: hidden; border-radius: inherit; }
.animal-card-popover .ac-header { padding: 0.65rem 0.8rem 0.5rem; }
.animal-card-popover .ac-name { font-weight: 600; font-size: 0.9rem; line-height: 1.2; }
.animal-card-popover .ac-sub { font-size: 0.75rem; color: var(--muted); margin-top: 0.1rem; }
.animal-card-popover .ac-badges { display: flex; flex-wrap: wrap; gap: 0.25rem; margin-top: 0.35rem; }
.animal-card-popover .ac-badges .badge { font-size: 0.7rem; }
.animal-card-popover .ac-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(80px, 1fr)); border-top: 1px solid var(--border); }
.animal-card-popover .ac-stat { padding: 0.4rem 0.8rem; }
.animal-card-popover .ac-stat + .ac-stat { border-left: 1px solid var(--border); }
.animal-card-popover .ac-stat-label { font-size: 0.68rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.animal-card-popover .ac-stat-value { font-size: 0.85rem; font-weight: 600; margin-top: 0.05rem; }
.animal-card-popover .ac-view-link { display: flex; align-items: center; justify-content: center; gap: 0.3rem; padding: 0.4rem 0.8rem; border-top: 1px solid var(--border); font-size: 0.78rem; font-weight: 500; color: var(--link); text-decoration: none; background: color-mix(in oklab, var(--surface), var(--primary) 3%); transition: background 0.12s; }
.animal-card-popover .ac-view-link:hover { background: color-mix(in oklab, var(--surface), var(--primary) 10%); color: var(--link); text-decoration: none; }
.animal-card-popover .ac-loading { display: flex; align-items: center; gap: 0.4rem; font-size: 0.8rem; color: var(--muted); padding: 0.65rem 0.8rem; }
