:root {
  /* Brand palette: orange #fe5c00, black, white, light grey #dddddd, dark grey #757575 */
  --brand: #fe5c00;       /* primary orange */
  --green: #fe5c00;       /* legacy alias -> brand orange (primary actions/active state) */
  --green-dark: #d44e00;  /* darker orange for pressed/emphasis */
  --green-light: #ff8240; /* lighter orange */
  --header: #000000;      /* black top bar */
  --tan: #f4f4f4;         /* app background (near light grey) */
  --tan-dark: #e6e6e6;    /* secondary button / muted fill */
  --ink: #111111;         /* near-black text */
  --muted: #757575;       /* dark grey */
  --line: #dddddd;        /* light grey borders */
  --card: #ffffff;        /* white cards */
  --danger: #c0392b;      /* red — kept for true alerts (overdue) */
  --danger-bg: #fcebe9;
  --warn: #fe5c00;        /* warnings use brand orange */
  --warn-bg: #fff0e6;
  --ok: #2e7d4f;          /* green — kept for income / all-clear */
  --ok-bg: #e7f3ec;
  --accent: #fe5c00;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --radius: 12px;
  --tab-h: 60px;
  --top-h: 56px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--tan);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--header); color: #fff;
  padding-top: env(safe-area-inset-top);
  box-shadow: var(--shadow);
}
.topbar-inner {
  height: var(--top-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; max-width: 900px; margin: 0 auto;
}
.topbar h1 { font-size: 1.2rem; margin: 0; font-weight: 600; }
.topbar h1.clickable { cursor: pointer; }
.topbar h1.clickable:active { opacity: .7; }
.topbar-actions { display: flex; gap: 8px; }

/* Signed-in indicator strip (below the header, only when signed in) */
.signed-in-bar {
  font-size: .72rem; color: var(--muted); background: var(--tan-dark);
  padding: 5px 16px; text-align: center; cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.signed-in-bar strong { color: var(--ink); font-weight: 600; }

/* Content */
.content {
  max-width: 900px; margin: 0 auto;
  padding: 16px 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 24px);
}

/* Tab bar */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex; background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -1px 4px rgba(0,0,0,.05);
}
.tab {
  flex: 1; height: var(--tab-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; text-decoration: none; color: var(--muted); font-size: .7rem;
}
.tab-ico { font-size: 1.35rem; line-height: 1; filter: grayscale(.3); }
.tab.active { color: var(--green); }
.tab.active .tab-ico { filter: none; }

/* Cards */
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 12px; font-size: 1.05rem; }
.card-row { display: flex; align-items: center; justify-content: space-between; }

/* Stat grid */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 14px; }
@media (min-width: 560px) { .stat-grid { grid-template-columns: repeat(4, 1fr); } }
.stat {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow);
}
.stat .num { font-size: 1.7rem; font-weight: 700; color: var(--ink); line-height: 1.1; }
.stat .lbl { font-size: .78rem; color: var(--muted); margin-top: 2px; }
/* Clickable stat cards (e.g. dashboard herd snapshot → filtered herd page) */
.stat-link { cursor: pointer; transition: border-color .12s, box-shadow .12s, transform .05s; }
.stat-link:hover { border-color: var(--brand); }
.stat-link:active { transform: translateY(1px); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: none; border-radius: 10px; padding: 10px 16px;
  font-size: .95rem; font-weight: 600; cursor: pointer;
  background: var(--green); color: #fff; text-decoration: none;
}
.btn:active { transform: translateY(1px); }
.btn.secondary { background: var(--tan-dark); color: var(--ink); }
.btn.ghost { background: transparent; color: #fff; border: 1px solid rgba(255,255,255,.5); }
.btn.danger { background: var(--danger); }
.btn.sm { padding: 6px 12px; font-size: .85rem; }
.btn.block { width: 100%; }
.btn-icon {
  width: 38px; height: 38px; border-radius: 10px; padding: 0;
  font-size: 1.3rem; background: rgba(255,255,255,.15); color: #fff; border: none; cursor: pointer;
}

/* Floating action button: primary add/edit action, always reachable bottom-right
   above the tab bar (mirrors the top-bar icon, set via setTopbarAdd). */
.fab {
  position: fixed; z-index: 40;
  right: 16px; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 16px);
  width: 56px; height: 56px; border-radius: 50%; padding: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.9rem; line-height: 1; color: #fff;
  background: var(--brand); border: none; cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,.28);
}
.fab:active { transform: translateY(1px); filter: brightness(.95); }

/* Lists */
.list { display: flex; flex-direction: column; gap: 10px; }
.item {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 12px; cursor: pointer; text-decoration: none; color: inherit;
}
.item:active { background: #fafaf7; }
.item-main { flex: 1; min-width: 0; }
.item-title { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.item-sub { font-size: .82rem; color: var(--muted); margin-top: 2px; }
.item-meta { text-align: right; font-size: .82rem; color: var(--muted); white-space: nowrap; }
.avatar {
  width: 42px; height: 42px; border-radius: 10px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 1.3rem;
  background: var(--tan-dark);
}

/* Badges */
.badge {
  display: inline-block; font-size: .7rem; font-weight: 700; padding: 2px 8px;
  border-radius: 999px; text-transform: uppercase; letter-spacing: .03em;
}
.badge.active { background: var(--ok-bg); color: var(--ok); }
.badge.sold, .badge.dead, .badge.culled { background: var(--tan-dark); color: var(--muted); }
.badge.bull { background: #e2e2e2; color: #000; }
.badge.cow, .badge.heifer { background: var(--warn-bg); color: var(--green-dark); }
.badge.steer, .badge.calf { background: #ededed; color: var(--muted); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }

/* Alerts */
.alert {
  border-radius: 10px; padding: 10px 12px; font-size: .88rem; display: flex; gap: 8px; align-items: flex-start;
  margin-bottom: 8px;
}
.alert.warn { background: var(--warn-bg); color: #7a5414; }
.alert.danger { background: var(--danger-bg); color: var(--danger); }
.alert.ok { background: var(--ok-bg); color: var(--ok); }

/* Drag-and-drop file/image pickers */
.dropzone.dragover { outline: 2px dashed var(--brand); outline-offset: 3px; filter: brightness(.97); }
.drop-hint { display: none; text-align: center; margin-top: 6px; }
@media (hover: hover) and (pointer: fine) { .drop-hint { display: block; } } /* desktop only */

/* News / announcement banner (dashboard, below the title bar) */
.news {
  position: relative; background: var(--card); border: 1px solid var(--line);
  border-left: 4px solid var(--brand); border-radius: var(--radius);
  padding: 12px 38px 12px 14px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.news.success { border-left-color: var(--ok); }
.news.warn { border-left-color: var(--warn, #d8920f); }
.news.info { border-left-color: var(--brand); }
.news-title { font-weight: 700; font-size: .98rem; }
.news-body { font-size: .9rem; color: var(--muted); margin-top: 3px; line-height: 1.35; }
.news-x {
  position: absolute; top: 6px; right: 6px; width: 28px; height: 28px;
  border: none; background: transparent; color: var(--muted);
  font-size: 1.3rem; line-height: 1; cursor: pointer; border-radius: 8px;
}
.news-x:hover { background: var(--tan-dark); color: var(--ink); }

/* Empty state */
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .big { font-size: 2.4rem; margin-bottom: 8px; }

/* Section header */
.section-head { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 12px; }
.section-head h2 { margin: 0; font-size: 1.1rem; }

/* Search / filter */
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.search {
  flex: 1; min-width: 160px; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 10px; font-size: .95rem; background: var(--card);
}
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; -webkit-overflow-scrolling: touch; }
.chip {
  white-space: nowrap; border: 1px solid var(--line); background: var(--card);
  border-radius: 999px; padding: 6px 12px; font-size: .82rem; cursor: pointer; color: var(--muted);
}
.chip.active { background: var(--green); color: #fff; border-color: var(--green); }

/* Forms / modal */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end; justify-content: center;
}
@media (min-width: 560px) { .modal-overlay { align-items: center; } }
.modal {
  background: var(--tan); width: 100%; max-width: 560px;
  max-height: 92vh; overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding: 0 0 calc(env(safe-area-inset-bottom) + 16px);
}
@media (min-width: 560px) { .modal { border-radius: 18px; } }
.modal-head {
  position: sticky; top: 0; background: var(--tan); z-index: 2;
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px; border-bottom: 1px solid var(--line);
}
.modal-head h3 { margin: 0; font-size: 1.1rem; }
.modal-body { padding: 16px; }
.modal-x { background: none; border: none; font-size: 1.5rem; cursor: pointer; color: var(--muted); line-height: 1; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: .8rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px;
  font-size: 1rem; background: var(--card); color: var(--ink); font-family: inherit;
}
.field textarea { min-height: 70px; resize: vertical; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }
/* One offspring line in the multi-birth (twins/triplets) recorder */
.birthrow { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; }
.birthrow input, .birthrow select {
  padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px;
  font-size: .95rem; background: var(--card); color: var(--ink); font-family: inherit; min-width: 0;
}
.form-actions { display: flex; gap: 10px; margin-top: 8px; }
.form-actions .btn { flex: 1; }

/* Detail */
.detail-head { display: flex; gap: 14px; align-items: center; margin-bottom: 16px; }
.detail-head .avatar { width: 56px; height: 56px; font-size: 1.7rem; }
.detail-head h2 { margin: 0; font-size: 1.3rem; }
/* Calvings-by-month bar chart (calving report) */
.calchart { display: flex; align-items: flex-end; gap: 6px; margin: 10px 0 18px; }
.calchart-col { flex: 1; display: flex; flex-direction: column; align-items: center; }
.calchart-count { font-size: .7rem; color: var(--muted); min-height: .95em; }
.calchart-bar {
  width: 70%; max-width: 34px; background: var(--brand); border-radius: 4px 4px 0 0;
  print-color-adjust: exact; -webkit-print-color-adjust: exact;
}
.calchart-month { font-size: .68rem; color: var(--muted); margin-top: 4px; }

/* Prev / next bar on the animal detail page */
.detailnav { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.detailnav .disabled { opacity: .4; pointer-events: none; }

.kv { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; font-size: .92rem; }
.kv-item { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.kv dt { color: var(--muted); font-size: .78rem; }
.kv dd { margin: 0; font-weight: 500; word-break: break-word; }
.subhead { font-size: .9rem; font-weight: 700; color: var(--muted); margin: 18px 0 8px; text-transform: uppercase; letter-spacing: .04em; }

.tagdot {
  display: inline-block; width: 11px; height: 11px; border-radius: 50%;
  vertical-align: middle; margin-right: 6px; box-shadow: inset 0 0 0 1px rgba(0,0,0,.25);
}

.linklike { cursor: pointer; font-weight: 600; color: var(--brand); }
.linklike.danger { color: var(--danger); }

.mini { font-size: .82rem; color: var(--muted); }
.right { text-align: right; }
.amt-pos { color: var(--ok); font-weight: 600; }
.amt-neg { color: var(--danger); font-weight: 600; }
.text-center { text-align: center; }
hr.sep { border: none; border-top: 1px solid var(--line); margin: 14px 0; }

/* Toast */
#toast-root { position: fixed; bottom: calc(var(--tab-h) + 16px); left: 0; right: 0; z-index: 60; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.toast {
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: .9rem; box-shadow: var(--shadow); opacity: 0; transform: translateY(10px);
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.err { background: var(--danger); }

/* Calendar */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.cal-title { font-size: 1.15rem; font-weight: 700; }
.btn-icon.dark { background: var(--tan-dark); color: var(--ink); }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.cal-dow { margin-bottom: 5px; }
.cal-dow-cell { text-align: center; font-size: .72rem; color: var(--muted); font-weight: 600; }
.cal-cell {
  position: relative; aspect-ratio: 1 / 1; border: 1px solid var(--line); border-radius: 9px;
  background: var(--card); cursor: pointer; padding: 0; display: flex; align-items: flex-start; justify-content: flex-start;
  font: inherit; color: var(--ink);
}
.cal-cell.empty { border: none; background: transparent; cursor: default; }
.cal-num { font-size: .82rem; padding: 4px 0 0 6px; }
.cal-cell.today { border-color: var(--brand); }
.cal-cell.today .cal-num { color: var(--brand); font-weight: 700; }
.cal-cell.selected { background: var(--brand); border-color: var(--brand); }
.cal-cell.selected .cal-num { color: #fff; font-weight: 700; }
.cal-dot {
  position: absolute; bottom: 6px; left: 50%; transform: translateX(-50%);
  width: 6px; height: 6px; border-radius: 50%; background: var(--brand);
}
.cal-cell.selected .cal-dot { background: #fff; }
.cal-dot[data-n="2"] { box-shadow: 8px 0 0 -1px var(--brand), -8px 0 0 -1px var(--brand); }
.cal-dot[data-n="3"] { box-shadow: 8px 0 0 -1px var(--brand), -8px 0 0 -1px var(--brand); }

/* Photos */
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.photo-thumb { position: relative; cursor: pointer; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.photo-thumb img { width: 100%; aspect-ratio: 1 / 1; object-fit: cover; display: block; }
.photo-date {
  position: absolute; bottom: 0; left: 0; right: 0; font-size: .68rem; color: #fff;
  background: linear-gradient(transparent, rgba(0,0,0,.6)); padding: 10px 6px 3px; text-align: center;
}
.img-preview { width: 100%; border-radius: 10px; margin-top: 8px; }
.journal-img { width: 100%; border-radius: 10px; margin-top: 8px; cursor: zoom-in; }

/* Access gate (login / paywall) */
.gate-overlay {
  position: fixed; inset: 0; z-index: 80; background: var(--tan);
  display: flex; align-items: flex-start; justify-content: center; overflow-y: auto;
  padding: calc(env(safe-area-inset-top) + 40px) 16px 40px;
}
.gate-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 24px; width: 100%; max-width: 400px;
}
.gate-brand { font-weight: 700; color: var(--brand); margin-bottom: 6px; }
.gate-card h2 { margin: 0 0 12px; font-size: 1.25rem; }
.gate-card input { width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 1rem; background: var(--card); margin-bottom: 8px; box-sizing: border-box; }

/* Reports / printouts */
.report { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.report-ranch { font-weight: 700; font-size: 1.05rem; }
.report-title { font-size: 1.5rem; margin: 4px 0 2px; }
.report-sub { color: var(--muted); margin-bottom: 14px; }
.report-h2 { font-size: 1rem; margin: 18px 0 6px; border-bottom: 2px solid var(--ink); padding-bottom: 3px; }
.report-stats { display: flex; flex-wrap: wrap; gap: 16px; margin: 8px 0 4px; color: var(--ink); }
.report-net { margin-top: 14px; font-size: 1.05rem; }
.report-note { color: var(--muted); font-size: .82rem; margin-top: 10px; }
.rtable { width: 100%; border-collapse: collapse; font-size: .9rem; margin: 6px 0 14px; }
.rtable th, .rtable td { border: 1px solid var(--line); padding: 10px 14px; text-align: left; vertical-align: top; }
.rtable th { background: var(--tan-dark); font-size: .76rem; text-transform: uppercase; letter-spacing: .03em; }
.rtable tbody tr:nth-child(even) { background: #faf9f5; }
.rtable td:last-child, .rtable th:last-child { text-align: right; }

@media print {
  .topbar, .tabbar, #toast-root, #modal-root, .no-print, .tabbar *, .fab { display: none !important; }
  body { background: #fff; }
  .content { padding: 0; max-width: none; }
  .report { border: none; box-shadow: none; border-radius: 0; padding: 0; }
  .report-h2 { break-after: avoid; }
  .rtable { font-size: 11px; }
  .rtable th, .rtable td { padding: 6px 9px; }
  .rtable th, .rtable tbody tr:nth-child(even) { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .rtable th { background: #eee !important; }
  .rtable tr { break-inside: avoid; }
}

/* simple bar viz */
.barline { display: flex; align-items: center; gap: 10px; margin: 6px 0; }
.barline .bl-lbl { width: 90px; font-size: .82rem; color: var(--muted); }
.barline .bl-track { flex: 1; height: 10px; background: var(--tan-dark); border-radius: 999px; overflow: hidden; }
.barline .bl-fill { height: 100%; background: var(--green-light); }
.barline .bl-val { width: 64px; text-align: right; font-size: .82rem; font-weight: 600; }
