:root {
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --paper: #f3f4f6;
  --card: #ffffff;
  --green: #166534;
  --green-2: #15803d;
  --green-soft: #ecfdf3;
  --gold: #ca8a04;
  --cream: #fefce8;
  --danger: #b91c1c;
  --better: #15803d;
  --worse: #b91c1c;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font: 15px/1.45 system-ui, "Segoe UI", "Leelawadee UI", sans-serif;
}
h1, h2 { font-weight: 600; letter-spacing: -0.02em; line-height: 1.25; }
h1 { margin: 0; font-size: 1.35rem; }
h2 { margin: 0 0 8px; font-size: 1rem; }
a { color: var(--green-2); }
p { margin: 0 0 10px; }
main { width: min(1080px, calc(100% - 24px)); margin: 16px auto 48px; }

.top {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 10px 16px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  color: var(--ink);
}
.brand { color: inherit; text-decoration: none; font-weight: 700; font-size: 15px; letter-spacing: -0.01em; }
.nav-chips { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.nav-chip {
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid transparent;
}
.nav-chip:hover { color: var(--ink); background: #f9fafb; }
.nav-chip.active {
  color: var(--green);
  background: var(--green-soft);
  border-color: #bbf7d0;
}
.who { margin-left: auto; display: flex; align-items: center; gap: 8px; font-size: 13px; flex-wrap: wrap; color: var(--muted); }
.lang-switch {
  display: inline-flex;
  gap: 2px;
  padding: 2px;
  border-radius: 999px;
  background: #f3f4f6;
  font-size: 11px;
  font-weight: 600;
}
.lang-switch a {
  color: var(--muted);
  text-decoration: none;
  padding: 3px 7px;
  border-radius: 999px;
}
.lang-switch a.active { color: var(--ink); background: #fff; box-shadow: var(--shadow); }
.login-card .lang-switch { margin-bottom: 12px; }
.who em { font-style: normal; background: #f3f4f6; color: var(--ink); border-radius: 999px; padding: 2px 7px; font-size: 12px; }
.who a, .who .link { color: var(--ink); }
.who form { margin: 0; }
.who .link { font-size: 13px; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.page-actions { display: flex; gap: 8px; flex-shrink: 0; align-items: center; }
.who-inline { font-size: 13px; color: var(--muted); font-weight: 500; }

.lead { color: var(--muted); font-size: 13px; max-width: 48ch; }
.muted { color: var(--muted); font-size: 12px; }
.split { display: flex; justify-content: space-between; gap: 12px; align-items: center; margin-bottom: 12px; }
.split .btn { align-self: center; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
  margin: 12px 0;
}
.card-tight { padding: 12px 14px; }
.card-tight h2 { margin-bottom: 10px; }
.card-head-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.card-head-row h2 { margin: 0; }
.text-link { font-size: 13px; font-weight: 500; text-decoration: none; }
.cell-sub { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }
.empty-state { margin: 8px 0 0; font-size: 13px; color: var(--muted); }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 14px; }
.chip {
  display: inline-flex;
  align-items: center;
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
}
.chip:hover { border-color: #d1d5db; background: #fafafa; }
.chip-accent { background: var(--green); border-color: var(--green); color: #fff; }
.chip-accent:hover { background: var(--green-2); border-color: var(--green-2); color: #fff; }
.credit-hint { margin: 10px 0 0; font-size: 12px; line-height: 1.4; color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin: 0 0 14px; }
.stats-3 { grid-template-columns: repeat(3, 1fr); }
.stats-inline .stat { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; padding: 10px 14px; }
.stats-inline .stat .v { margin: 0; font-size: 1.25rem; }
.stat {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
}
.stat .k { font-size: 12px; font-weight: 500; margin: 0; color: var(--muted); }
.stat .k small { display: block; font-weight: 450; color: var(--muted); }
label small, legend small {
  display: inline;
  margin-left: 0.35em;
  font-weight: 450;
  color: var(--muted);
  font-size: 12px;
}
.grid-2 { align-items: start; }
.grid-2 > div { display: flex; flex-direction: column; }
.stat .v { font-size: 1.5rem; font-weight: 700; margin: 4px 0 0; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.stat .n, .stat .d { margin: 0; font-size: 13px; }
.better { color: var(--better); }
.worse { color: var(--worse); }
.same { color: var(--muted); }

label { display: block; font-size: 14px; font-weight: 650; margin: 12px 0 4px; }
input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d9d0c2;
  border-radius: 10px;
  background: white;
  color: var(--ink);
  font: inherit;
}
textarea { resize: vertical; }
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin: 12px 0 18px; }
.filters label { margin: 0; min-width: 180px; }
.form-inline { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; }
.form-inline > div { min-width: 200px; flex: 1; }
.form-narrow { max-width: 460px; }
.form-actions { display: flex; gap: 14px; align-items: center; margin-top: 16px; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 550; }
.check input { width: auto; }
.checks { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 16px; }
fieldset { border: 1px solid var(--line); border-radius: 12px; margin-top: 16px; }
legend { padding: 0 6px; font-weight: 650; }
.help { min-height: 1.4em; }

.btn, button.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 8px;
  padding: 8px 14px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-2); border-color: var(--green-2); }
.btn-secondary { background: #fff; border-color: var(--line); color: var(--ink); }
.btn-secondary:hover { background: #f9fafb; border-color: #d1d5db; }
.btn-danger { border-color: var(--danger); color: var(--danger); background: white; }
.btn-sm {
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 8px;
}
button.link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--green-2);
  font: inherit;
  cursor: pointer;
}
button.link.danger { color: var(--danger); }
.row-actions { display: flex; gap: 10px; align-items: center; }
.row-actions form { margin: 0; }

.flash { border-radius: 12px; padding: 10px 12px; margin: 0 0 14px; }
.flash.ok { background: #e5f2e6; color: #1d4d2b; }
.flash.err { background: #f8e8e4; color: #6d2c24; }
.note {
  background: #f9fafb;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--muted);
}
.tag {
  display: inline-block;
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 12px;
  font-weight: 650;
  background: #eee7da;
}
.tag-gestation { background: #e7f0d8; color: #23472b; }
.tag-lactation { background: #f8e7d2; color: #6a3e12; }
.tag-off { background: #eee; color: #666; }
.tag-thin { background: #f8e8e4; color: var(--danger); margin-top: 4px; }
.steps { margin: 0; padding-left: 18px; }
.steps li { margin: 6px 0; }
.unit-list { list-style: none; padding: 0; margin: 0 0 12px; }
.unit-list li { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.unit-list-compact li { padding: 6px 0; }

.add-farm-panel > summary {
  cursor: pointer;
  font-weight: 650;
  list-style: none;
}
.add-farm-panel > summary::-webkit-details-marker { display: none; }
.add-farm-panel[open] > summary { margin-bottom: 12px; }

.farm-browser { padding: 0; overflow: hidden; }
.farm-browser-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: #faf7f1;
}
.farm-search { flex: 1; min-width: 200px; margin: 0; }
.farm-search input { width: 100%; margin: 0; }
.farm-browser-meta { margin: 0; font-size: 13px; color: var(--muted); white-space: nowrap; }
.farm-browser-body {
  display: grid;
  grid-template-columns: minmax(220px, 280px) minmax(0, 1fr);
  min-height: 360px;
}
.farm-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  max-height: min(70vh, 640px);
  border-right: 1px solid var(--line);
  background: #f9f6ef;
}
.farm-list-item {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 2px 8px;
  padding: 10px 12px;
  border-radius: 10px;
  text-decoration: none;
  color: inherit;
  border: 1px solid transparent;
}
.farm-list-item:hover { background: #fff; border-color: var(--line); }
.farm-list-item.active {
  background: var(--card);
  border-color: var(--green-2);
  box-shadow: inset 3px 0 0 var(--green-2);
}
.farm-list-name { font-weight: 650; grid-column: 1; }
.farm-list-code {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.03em;
  grid-column: 2;
  grid-row: 1;
}
.farm-list-units { font-size: 12px; color: var(--muted); grid-column: 1 / -1; }
.farm-list-empty { padding: 16px 12px; margin: 0; font-size: 14px; color: var(--muted); }
.farm-detail { padding: 16px 18px; overflow-y: auto; max-height: min(70vh, 640px); }
.farm-detail-head { margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.farm-detail-title { margin: 0 0 8px; font-size: 22px; }
.farm-edit-panel > summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  color: var(--green-2);
  list-style: none;
  display: inline-block;
  margin-bottom: 4px;
}
.farm-edit-panel > summary::-webkit-details-marker { display: none; }
.farm-edit-panel[open] > summary { margin-bottom: 10px; }
.farm-edit-panel .farm-detail-fields { margin-top: 4px; }
.farm-detail-fields {
  display: grid;
  grid-template-columns: 1fr 140px;
  gap: 12px;
  margin-bottom: 12px;
}
.farm-detail-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.farm-remove-form { display: none; }
.farm-units-section h3 { margin: 0; font-size: 17px; }
.farm-add-unit { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.hidden { display: none !important; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.crumb { margin: 0 0 8px; color: var(--muted); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 13px; color: var(--muted); font-weight: 650; }
td.num, th.num { text-align: right; }
.compare th span, .compare td span { display: block; }
td.best { background: #eef6ee; }
.bar { display: block; height: 6px; background: #eee6d8; border-radius: 99px; margin-top: 6px; }
.bar > span { display: block; height: 100%; background: var(--green-2); border-radius: 99px; }

.login-body { min-height: 100vh; display: grid; place-items: center; }
.login-card { width: min(380px, calc(100% - 32px)); background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 24px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 1.25rem; margin-bottom: 16px; }
.brand-mark { margin: 0 0 4px; font-weight: 700; font-size: 14px; letter-spacing: -0.01em; color: var(--green); }
.login-card .btn { width: 100%; margin-top: 16px; }

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

@media (max-width: 900px) {
  .top { flex-wrap: wrap; }
  .who { width: 100%; margin-left: 0; }
  .grid-2, .stats, .stats-3, .checks { grid-template-columns: 1fr; }
  h1 { font-size: 26px; }
  .split { flex-direction: column; align-items: stretch; }
  .farm-browser-body { grid-template-columns: 1fr; }
  .farm-list {
    max-height: 220px;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
  .farm-detail { max-height: none; }
  .farm-detail-fields { grid-template-columns: 1fr; }
}

/* —— Monthly entry (all-in-one, dense columns) —— */
main:has(.entry-page),
body:has(.entry-page) main {
  width: calc(100% - 32px);
  max-width: none;
  margin: 10px auto 24px;
}
main:has(.entry-page) .page-head,
body:has(.entry-page) main .page-head { margin-bottom: 8px; }
main:has(.entry-page) .flash,
body:has(.entry-page) main .flash { margin-bottom: 8px; }

.entry-page.entry-shell {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 12px 16px 16px;
  margin: 0;
}
.entry-page label {
  display: block;
  margin: 0 0 4px;
  font-size: 11px;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.3;
  height: 1.3em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry-page input,
.entry-page select,
.entry-page textarea {
  padding: 0 8px;
  font-size: 13px;
  border-radius: 8px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  min-height: 2rem;
  height: 2rem;
}
.entry-page textarea {
  height: auto;
  min-height: 2rem;
  padding-top: 5px;
  padding-bottom: 5px;
}
.entry-page .entry-field-num input {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.entry-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(14rem, 38%);
  gap: 12px 16px;
  align-items: end;
  width: 100%;
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--line);
}
.entry-loc {
  display: grid;
  grid-template-columns: minmax(10rem, 1fr) minmax(10rem, 1fr) auto;
  gap: 10px 12px;
  align-items: end;
  min-width: 0;
  width: 100%;
}
.entry-when {
  display: grid;
  grid-template-columns: 4.5rem 4.75rem;
  gap: 8px;
  align-items: end;
}
.entry-field-year input {
  text-align: center;
  font-variant-numeric: tabular-nums;
  padding-left: 4px;
  padding-right: 4px;
}
.entry-field-month select {
  padding-right: 1.5rem;
}
.entry-credit {
  margin: 0;
  font-size: 11px;
  line-height: 2rem;
  color: var(--muted);
  background: var(--cream);
  border: 1px solid #fde68a;
  border-radius: 8px;
  padding: 0 8px;
  min-height: 2rem;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px 14px;
  align-items: stretch;
  width: 100%;
}
.entry-pane {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px 12px;
  background: #fafafa;
  min-width: 0;
}
.entry-pane-feeds { background: #fff; }
.entry-pane h2 {
  flex: 0 0 auto;
  margin: 0 0 6px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  cursor: help;
  line-height: 1.3;
  height: 1.3em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry-field {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  min-width: 0;
}
.entry-feeds-grid,
.entry-metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 12px;
  align-items: end;
  width: 100%;
}
.entry-pane-feeds .entry-feeds-grid {
  grid-template-columns: minmax(0, 1.35fr) minmax(0, 0.85fr);
}
.entry-hint {
  margin: 0 0 6px;
  font-size: 11px;
  line-height: 1.35;
  color: var(--muted);
}
.entry-hint-inline { display: none; }
.entry-footer {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  width: 100%;
}
.entry-footer-title {
  display: block;
  margin: 0 0 6px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.entry-ctx-grid {
  display: grid;
  grid-template-columns: minmax(5.5rem, 7rem) minmax(5.5rem, 7rem) minmax(9rem, 12rem) minmax(12rem, 1fr);
  gap: 8px 12px;
  align-items: start;
  width: 100%;
}

@media (min-width: 1280px) {
  .entry-when {
    grid-template-columns: 5rem 5.25rem;
  }
}
.entry-field-notes textarea {
  resize: none;
  min-height: 2rem;
  height: 2rem;
  line-height: 1.3;
}

@media (max-width: 1100px) {
  .entry-toolbar {
    grid-template-columns: 1fr;
  }
  .entry-loc {
    grid-template-columns: 1fr 1fr;
  }
  .entry-when {
    grid-column: 1 / -1;
    grid-template-columns: 1fr 1fr;
    max-width: 12rem;
  }
  .entry-grid {
    grid-template-columns: 1fr;
  }
  .entry-feeds-grid,
  .entry-metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .entry-pane-feeds .entry-feeds-grid {
    grid-template-columns: minmax(0, 1.2fr) minmax(0, 0.9fr);
  }
  .entry-ctx-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
@media (max-width: 640px) {
  .entry-loc,
  .entry-feeds-grid,
  .entry-metric-grid,
  .entry-ctx-grid {
    grid-template-columns: 1fr 1fr;
  }
  .entry-field-notes { grid-column: 1 / -1; }
}
