:root {
  --navy:       #1F3A5F;
  --blue:       #3FA9F5;
  --light-blue: #E6F2FA;
  --green:      #4CAF50;
  --mint:       #E8F5E9;
  --white:      #FFFFFF;
  --gray:       #6B7280;
  --light-gray: #F3F4F6;
  --border:     #D1D5DB;
  --red:        #EF4444;
  --calc-bg:    #FFFBEB;
  --calc-border:#F59E0B;
}

/* ── Reset ─────────────────────────────────────────── */
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Segoe UI', Arial, sans-serif;
  /* Field-friendly: bumped twice (14 → 15 → 16.5) for outdoor readability
     through gloves at arm's length on a tablet under direct sunlight. */
  font-size: 16.5px;
  background: var(--light-gray);
  color: #1a1a1a;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ─────────────────────────────────────────── */
header {
  background: var(--navy);
  color: white;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,.3);
}
header h1 { font-size: 20px; font-weight: 700; letter-spacing: .5px; }
header span.subtitle { font-size: 12px; color: #a0b4cc; margin-left: auto; }

/* ── Font-size scale buttons (accessibility) ─────────────── */
.fz-buttons {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  flex-shrink: 0;
}
.fz-btn {
  background: rgba(255, 255, 255, .08);
  border: 1px solid rgba(255, 255, 255, .15);
  color: #e5e7eb;
  border-radius: 6px;
  padding: 4px 0;
  cursor: pointer;
  font-weight: 700;
  font-family: inherit;
  line-height: 1;
  min-width: 38px;
  min-height: 36px;
  transition: background .15s, color .15s, border-color .15s;
}
.fz-btn:hover { background: rgba(255, 255, 255, .18); color: white; }
.fz-btn.active {
  background: var(--blue);
  color: white;
  border-color: var(--blue);
}
.fz-btn-sm { font-size: 12px; }
.fz-btn-md { font-size: 16px; }
.fz-btn-lg { font-size: 20px; }

/* ── Connection-status badge (right side of header) ────── */
.conn-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 600;
  background: rgba(255,255,255,.08);
  color: #cbd5e1;
  transition: background .25s, color .25s;
  white-space: nowrap;
  flex-shrink: 0;
}
.conn-badge .conn-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  background: #94a3b8;
}
.conn-badge.online {
  background: rgba(74, 222, 128, .18);
  color: #bbf7d0;
}
.conn-badge.online .conn-dot {
  background: #4ade80;
  animation: conn-pulse-green 2.2s infinite;
}
.conn-badge.offline {
  background: rgba(239, 68, 68, .22);
  color: #fecaca;
}
.conn-badge.offline .conn-dot {
  background: #ef4444;
  animation: conn-pulse-red 1.1s infinite;
}
@keyframes conn-pulse-green {
  0%, 100% { box-shadow: 0 0 0 0 rgba(74, 222, 128, .55); }
  50%      { box-shadow: 0 0 0 7px rgba(74, 222, 128, 0);   }
}
@keyframes conn-pulse-red {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, .65); }
  50%      { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0);   }
}

/* ── App layout ─────────────────────────────────────── */
.app { display: flex; flex: 1; overflow: hidden; }

/* ── Sidebar ─────────────────────────────────────────── */
.sidebar {
  width: 220px;
  background: var(--navy);
  display: flex;
  flex-direction: column;
  padding: 16px 0;
  gap: 4px;
  flex-shrink: 0;
}
.sidebar-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  color: #a0b4cc;
  cursor: pointer;
  border: none;
  background: none;
  text-align: left;
  font-size: 16px;
  line-height: 1.3;
  width: 100%;
  min-height: 52px;
  transition: background .15s, color .15s;
}
.sidebar-btn:hover  { background: rgba(255,255,255,.08); color: white; }
.sidebar-btn.active { background: var(--blue); color: white; font-weight: 600; }
.sidebar-btn .icon  { font-size: 18px; flex-shrink: 0; }

/* ── Main content ────────────────────────────────────── */
.main-area { flex: 1; display: flex; overflow: hidden; }

/* ── Forms panel (left column) ──────────────────────── */
.forms-panel {
  width: 240px;
  background: white;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.forms-panel-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.forms-panel-header h3 { font-size: 13px; font-weight: 600; color: var(--navy); }
.forms-list { flex: 1; overflow-y: auto; padding: 8px; }

.form-item {
  padding: 13px 15px;
  border-radius: 8px;
  cursor: pointer;
  border: 2px solid transparent;
  margin-bottom: 6px;
  min-height: 52px;
  transition: all .15s;
}
.form-item:hover  { background: var(--light-blue); }
.form-item.active { border-color: var(--blue); background: var(--light-blue); }
.form-item-name   { font-weight: 600; font-size: 15px; color: var(--navy); }
.form-item-date   { font-size: 13px; color: var(--gray); margin-top: 3px; }

/* ── Buttons ─────────────────────────────────────────── */
.btn {
  padding: 10px 18px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
  min-height: 44px;       /* WCAG 2.5.5 enhanced — generous touch target for field use */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background .15s;
}
.btn-primary { background: var(--blue);  color: white; }
.btn-primary:hover { background: #2d95e0; }
.btn-green   { background: var(--green); color: white; }
.btn-green:hover   { background: #3d9140; }
.btn-danger  { background: var(--red);   color: white; }
.btn-danger:hover  { background: #dc2626; }
.btn-outline { background: white; color: var(--navy); border: 1px solid var(--border); }
.btn-outline:hover { background: var(--light-blue); }
.btn-sm {
  padding: 7px 13px;
  font-size: 14px;
  min-height: 38px;
}

/* ── Editor area ─────────────────────────────────────── */
.editor-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.editor-toolbar {
  padding: 12px 20px;
  background: white;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.editor-toolbar input[type=text] {
  flex: 1;
  max-width: 380px;
  padding: 10px 13px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 15px;
  min-height: 44px;
}

/* ── Tab bar ─────────────────────────────────────────── */
.form-tabs {
  display: flex;
  flex-wrap: nowrap;          /* keep all tabs on one line */
  overflow-x: auto;           /* scroll horizontally when they don't fit */
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;       /* less visual noise on Firefox */
  background: white;
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
}
.form-tab {
  padding: 14px 24px;
  cursor: pointer;
  font-size: 15px;
  color: var(--gray);
  border-bottom: 3px solid transparent;
  transition: all .15s;
  font-weight: 500;
  min-height: 50px;
  white-space: nowrap;         /* never wrap a tab label */
  flex-shrink: 0;              /* keep natural width even when row overflows */
}
.form-tab:hover  { color: var(--navy); }
.form-tab.active { color: var(--blue); border-bottom-color: var(--blue); font-weight: 700; }

.form-body { flex: 1; overflow-y: auto; padding: 20px; }

/* ── Section cards ───────────────────────────────────── */
.section {
  background: white;
  border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
  margin-bottom: 16px;
  overflow: hidden;
}
.section-header {
  background: var(--navy);
  color: white;
  padding: 10px 16px;
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.section-header.green { background: var(--green); }
.section-header.blue  { background: var(--blue);  }
.section-body { padding: 16px; }

/* ── Field rows ──────────────────────────────────────── */
.field-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid #f0f0f0;
}
.field-row:last-child { border-bottom: none; }
.field-label {
  flex: 0 0 320px;
  font-size: 13px;
  color: #374151;
  line-height: 1.3;
}
.field-label small { display: block; color: var(--gray); font-size: 11px; }
.field-unit  { flex: 0 0 80px; font-size: 12px; color: var(--gray); text-align: center; }
.field-input { flex: 1; }

.field-input input,
.field-input select {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  outline: none;
  transition: border .15s;
}
.field-input input:focus,
.field-input select:focus { border-color: var(--blue); }

.field-input input.calc,
.field-input input[readonly] {
  background: var(--calc-bg);
  border-color: var(--calc-border);
  color: #92400e;
  font-weight: 600;
  cursor: default;
}

/* ── Combobox ────────────────────────────────────────── */
.combobox { position: relative; width: 100%; }

.combobox-input-wrap {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  transition: border .15s;
}
.combobox-input-wrap:focus-within { border-color: var(--blue); }

.combobox input {
  flex: 1;
  border: none;
  padding: 6px 8px;
  font-size: 13px;
  outline: none;
  min-width: 0;
}
.combobox-btn {
  background: var(--light-gray);
  border: none;
  border-left: 1px solid var(--border);
  padding: 0 10px;
  cursor: pointer;
  color: var(--gray);
  font-size: 11px;
  flex-shrink: 0;
}
.combobox-btn:hover { background: var(--light-blue); }

.combobox-dropdown {
  position: absolute;
  top: 100%;
  left: 0; right: 0;
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  z-index: 100;
  max-height: 220px;
  overflow-y: auto;
  margin-top: 2px;
  display: none;
}
.combobox-dropdown.open { display: block; }

.combobox-option { padding: 8px 12px; cursor: pointer; font-size: 13px; transition: background .1s; }
.combobox-option:hover,
.combobox-option.highlighted { background: var(--light-blue); }

.combobox-add {
  padding: 8px 12px;
  font-size: 12px;
  color: var(--green);
  cursor: pointer;
  border-top: 1px solid var(--border);
  font-weight: 600;
}
.combobox-add:hover { background: var(--mint); }

/* ── Table inside sections ───────────────────────────── */
.table-section { width: 100%; border-collapse: collapse; font-size: 12px; }
.table-section th {
  background: var(--light-blue);
  color: var(--navy);
  padding: 6px 8px;
  text-align: left;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}
.table-section td { padding: 5px 6px; border-bottom: 1px solid #f0f0f0; vertical-align: middle; }
.table-section tr:last-child td { border-bottom: none; }
.table-section input {
  width: 100%;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 12px;
  outline: none;
}
.table-section input:focus { border-color: var(--blue); }
.table-section input[readonly] {
  background: var(--calc-bg);
  border-color: var(--calc-border);
  color: #92400e;
  font-weight: 600;
}
.table-section select {
  width: 100%;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 12px;
  outline: none;
  background: white;
}
.table-section select:focus { border-color: var(--blue); }

/* ── Dynamic table add/remove row controls ───────────── */
.dyn-table-wrap { overflow-x: auto; }
.dyn-add-row {
  margin-top: 8px;
  background: var(--mint);
  border: 1px dashed var(--green);
  color: var(--green);
  font-weight: 600;
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: background .15s;
}
.dyn-add-row:hover { background: #c8e6c9; }
.dyn-del-row {
  background: none;
  border: none;
  color: var(--red);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background .15s;
}
.dyn-del-row:hover { background: #fee2e2; }

/* ── Marginal income summary card ────────────────────── */
.md-summary {
  background: linear-gradient(135deg, var(--navy), #2d5a8e);
  color: white;
  padding: 16px 20px;
  border-radius: 10px;
  margin-top: 16px;
}
.md-summary h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: .8;
  margin-bottom: 8px;
}
.md-value          { font-size: 28px; font-weight: 700; }
.md-value.positive { color: #86efac; }
.md-value.negative { color: #fca5a5; }
.md-per-unit       { font-size: 13px; opacity: .8; margin-top: 4px; }

/* ── Empty state ─────────────────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--gray);
  gap: 12px;
}
.empty-state .big-icon { font-size: 48px; }

/* ── Toast notification ──────────────────────────────── */
.toast {
  position: fixed;
  bottom: 24px; right: 24px;
  background: var(--navy);
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
  z-index: 9999;
  opacity: 0;
  transform: translateY(20px);
  transition: all .3s;
}
.toast.show    { opacity: 1; transform: translateY(0); }
.toast.success { background: var(--green); }

/* ── Cascade profit waterfall ────────────────────────────── */
.cascade-table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.cascade-table tr { border-bottom: 1px solid #f0f0f0; }
.cascade-table td { padding: 6px 8px; vertical-align: middle; }
.cascade-sign { width: 28px; text-align: center; font-weight: 700; color: var(--gray); font-size: 15px; }
.cascade-label { font-size: 13px; color: #374151; }
.cascade-label small { display: block; color: var(--gray); font-size: 11px; }
.cascade-value { width: 170px; }
.cascade-indent td:nth-child(2) { padding-left: 20px; color: var(--gray); font-size: 12px; }
.cascade-result { background: var(--light-blue); }
.cascade-result td { border-top: 2px solid var(--blue) !important; border-bottom: 2px solid var(--blue) !important; padding: 8px !important; }
.cascade-result .cascade-label { font-weight: 700; color: var(--navy); font-size: 13px; }
.cascade-result .cascade-sign { color: var(--blue); }
.cascade-profit { background: var(--mint); }
.cascade-profit td { border-top: 2px solid var(--green) !important; padding: 10px 8px !important; }
.cascade-profit .cascade-label { font-weight: 700; color: #166534; font-size: 14px; }
.cascade-profit .cascade-sign { color: var(--green); }
.cascade-loss { background: #fef2f2; }
.cascade-loss td { border-top: 2px solid var(--red) !important; padding: 10px 8px !important; }
.cascade-loss .cascade-label { font-weight: 700; color: #991b1b; font-size: 14px; }
.cascade-loss .cascade-sign { color: var(--red); }
.cascade-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* ── Manure nutrient table ───────────────────────────────── */
.manure-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 6px; }
.manure-table th { background: #f0fdf4; color: #166534; padding: 5px 7px; text-align: left; font-weight: 600; border-bottom: 1px solid #bbf7d0; }
.manure-table td { padding: 4px 6px; border-bottom: 1px solid #f0f0f0; vertical-align: middle; }
.manure-table input { width: 100%; padding: 3px 5px; border: 1px solid var(--border); border-radius: 4px; font-size: 12px; outline: none; }
.manure-table input:focus { border-color: var(--green); }
.manure-table input[readonly] { background: var(--calc-bg); border-color: var(--calc-border); color: #92400e; font-weight: 600; }

/* ── Mobile overlay backdrop ─────────────────────────────── */
.mobile-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 800;
  touch-action: none;
}
.mobile-overlay.show { display: block; }

/* Mobile toggle buttons (hidden on desktop) */
.mobile-menu-btn,
.mobile-forms-btn,
.mobile-close-btn { display: none; }

/* ── TABLET LANDSCAPE (≤ 1024px) ─────────────────────────── */
@media (max-width: 1024px) {
  .sidebar   { width: 180px; }
  .forms-panel { width: 220px; }
  .field-label { flex: 0 0 260px; }
}

/* ──────────────────────────────────────────────────────────
   PROGRESSIVE DISCLOSURE LAYOUT (tablet & desktop, ≥ 769px)

   .app[data-layout="module"] — module is selected, no form open
     → sidebar collapses to icon-only rail (~64px), forms-panel
       remains expanded.
   .app[data-layout="form"]   — a form is open
     → both sidebar AND forms-panel collapse, editor takes the rest.

   Phones (≤ 768px) ignore data-layout and use the slide-out overlay
   paradigm defined further below.
   ────────────────────────────────────────────────────────── */
@media (min-width: 769px) {
  .app[data-layout="module"] .sidebar,
  .app[data-layout="form"]   .sidebar {
    width: 64px;
    padding: 12px 0;
  }
  .app[data-layout="module"] .sidebar .label,
  .app[data-layout="form"]   .sidebar .label {
    display: none;
  }
  .app[data-layout="module"] .sidebar-btn,
  .app[data-layout="form"]   .sidebar-btn {
    justify-content: center;
    padding: 14px 0;
    min-height: 56px;       /* WCAG 2.5.5 enhanced — large touch target */
  }
  .app[data-layout="module"] .sidebar-btn .icon,
  .app[data-layout="form"]   .sidebar-btn .icon {
    font-size: 26px;
  }

  /* Forms-panel collapse: only when a form is open */
  .app[data-layout="form"] .forms-panel {
    width: 44px;
    overflow: hidden;
  }
  .app[data-layout="form"] .forms-panel-header,
  .app[data-layout="form"] .forms-list {
    display: none;
  }
  .app[data-layout="form"] .forms-panel-strip {
    display: flex;
  }

  /* Inline collapse-button (visible only when panel is expanded on tablet+) */
  .forms-collapse-btn {
    display: inline-flex;
  }
}

/* Vertical strip — visible only when forms-panel is collapsed */
.forms-panel-strip {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 14px;
  padding: 18px 0;
  background: white;
  border: none;
  cursor: pointer;
  width: 100%;
  height: 100%;
  font-size: 12px;
  color: var(--gray);
  transition: background .15s, color .15s;
}
.forms-panel-strip:hover {
  background: var(--light-blue);
  color: var(--navy);
}
.forms-panel-strip .strip-icon { font-size: 22px; }
.forms-panel-strip .strip-text {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-weight: 700;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--navy);
}

/* Inline collapse button (hidden by default; tablet+ media query shows it) */
.forms-collapse-btn {
  display: none;
  align-items: center;
  justify-content: center;
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  width: 30px; height: 30px;
  cursor: pointer;
  color: var(--gray);
  font-size: 16px;
  line-height: 1;
  transition: background .15s, color .15s, border-color .15s;
}
.forms-collapse-btn:hover {
  background: var(--light-blue);
  color: var(--navy);
  border-color: var(--blue);
}

/* ──────────────────────────────────────────────────────────
   ACCESSIBILITY — focus rings, reduced motion, screen reader
   ────────────────────────────────────────────────────────── */
.sidebar-btn:focus-visible,
.btn:focus-visible,
.form-tab:focus-visible,
.form-item:focus-visible,
.forms-panel-strip:focus-visible,
.forms-collapse-btn:focus-visible,
.dyn-del-btn:focus-visible,
.add-row-btn:focus-visible,
.mobile-menu-btn:focus-visible,
.mobile-forms-btn:focus-visible,
.mobile-close-btn:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 2px;
}
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 1px;
}

/* Make form-items keyboard focusable with a visible state */
.form-item {
  outline: none;
}
.form-item:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: -2px;
}

/* Visually-hidden helper for screen readers */
.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;
}

/* Respect users who prefer less motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ── TABLET PORTRAIT (≤ 768px) ───────────────────────────── */
@media (max-width: 768px) {

  /* ── Header ── */
  header { padding: 10px 14px; gap: 10px; }
  header h1 { font-size: 17px; }
  header .subtitle { display: none; }
  .mobile-menu-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255,255,255,.15);
    border: none;
    color: white;
    font-size: 20px;
    width: 36px; height: 36px;
    border-radius: 6px;
    cursor: pointer;
    flex-shrink: 0;
    margin-left: auto;
  }

  /* ── App layout: column ── */
  .app { flex-direction: column; }

  /* ── Sidebar: slide-in overlay from left ── */
  .sidebar {
    position: fixed;
    left: -260px;
    top: 0; bottom: 0;
    width: 240px !important;
    z-index: 1000;
    transition: left .25s ease;
    padding-top: 56px;
  }
  .sidebar.open { left: 0; box-shadow: 6px 0 24px rgba(0,0,0,.35); }
  .sidebar-btn  { padding: 14px 20px; font-size: 14px; }

  /* ── Main area: fill width ── */
  .main-area { flex-direction: row; flex: 1; overflow: hidden; }

  /* ── Forms panel: slide-in overlay from left, below header ── */
  .forms-panel {
    position: fixed;
    left: -280px;
    top: 53px; bottom: 0;
    width: 260px !important;
    z-index: 900;
    transition: left .25s ease;
    box-shadow: none;
  }
  .forms-panel.open {
    left: 0;
    box-shadow: 6px 0 24px rgba(0,0,0,.25);
  }
  .forms-panel-header {
    padding: 12px 14px;
    justify-content: space-between;
  }
  .mobile-close-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--gray);
    font-size: 14px;
    width: 26px; height: 26px;
    cursor: pointer;
  }

  /* ── Editor area: full width ── */
  .editor-area { flex: 1; width: 100%; }

  /* Mobile forms-open button floating */
  .mobile-forms-btn {
    display: flex;
    position: fixed;
    bottom: 72px; right: 16px;
    background: var(--navy);
    color: white;
    border: none;
    border-radius: 50%;
    width: 48px; height: 48px;
    font-size: 20px;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(0,0,0,.3);
    z-index: 700;
    cursor: pointer;
  }

  /* ── Toolbar ── */
  .editor-toolbar { padding: 10px 12px; flex-wrap: wrap; gap: 6px; }
  .editor-toolbar input[type=text] { max-width: none; flex: 1 1 100%; order: -1; }

  /* ── Tab bar ── */
  .form-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap; padding: 0 10px; }
  .form-tab  { white-space: nowrap; flex-shrink: 0; padding: 10px 14px; }

  /* ── Form body ── */
  .form-body { padding: 12px; }
  .section-body { padding: 12px; }
  .section-header { font-size: 12px; padding: 9px 14px; }

  /* ── Field rows: stack label above input ── */
  .field-row { flex-wrap: wrap; gap: 4px; padding: 8px 0; }
  .field-label {
    flex: 0 0 100% !important;
    font-size: 13px;
    color: #374151;
    margin-bottom: 0;
  }
  .field-label small { font-size: 11px; color: var(--gray); }
  .field-input { flex: 1 1 0; min-width: 0; }
  .field-input input,
  .field-input select { font-size: 15px; padding: 8px 10px; }
  .field-unit  { flex: 0 0 auto; align-self: center; font-size: 12px; }

  /* ── Two-column grids → single column ── */
  .cascade-cols { grid-template-columns: 1fr !important; }

  /* ── Cascade params grid ── */
  .section-body > div[style*="grid-template-columns:1fr 1fr"] {
    grid-template-columns: 1fr !important;
    display: grid !important;
  }

  /* ── Tables scroll horizontally ── */
  .dyn-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table-section  { font-size: 12px; min-width: 460px; }
  .table-section input, .table-section select { font-size: 13px; padding: 5px 7px; }
  .manure-table   { font-size: 12px; min-width: 420px; }
  .manure-table input { font-size: 13px; }

  /* ── Cascade table ── */
  .cascade-value { width: 130px; }
  .cascade-label { font-size: 13px; }

  /* ── MD summary card ── */
  .md-summary { padding: 12px 14px; }
  .md-value   { font-size: 22px; }

  /* ── Toast: bottom center on mobile ── */
  .toast { left: 16px; right: 16px; bottom: 16px; text-align: center; }
}

/* ── Validation flash ────────────────────────────────────── */
@keyframes clamp-flash {
  0%   { background: #fef2f2; border-color: var(--red); }
  50%  { background: #fee2e2; border-color: var(--red); box-shadow: 0 0 0 3px rgba(239,68,68,.25); }
  100% { background: inherit; border-color: var(--border); }
}
.input-clamped { animation: clamp-flash .7s ease-out forwards; }

/* ── Persistent invalid field state ─────────────────────── */
input.field-invalid,
select.field-invalid,
.field-invalid {
  border-color: var(--red) !important;
  background: #fef2f2 !important;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, .18) !important;
  outline: none !important;
}

/* ── Validation error popup (red, distinct from grey tooltip) ── */
.field-error-pop {
  position: absolute;
  background: var(--red);
  color: white;
  padding: 8px 12px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.4;
  z-index: 9991;
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .15s ease, transform .15s ease;
  box-shadow: 0 4px 14px rgba(239, 68, 68, .45);
  max-width: 280px;
  word-break: break-word;
}
.field-error-pop::before {
  content: '⚠ ';
  display: inline-block;
}
.field-error-pop::after {       /* arrow pointing up to the field */
  content: '';
  position: absolute;
  top: -6px; left: 14px;
  border: 6px solid transparent;
  border-top: none;
  border-bottom-color: var(--red);
}
.field-error-pop.above::after {
  top: auto; bottom: -6px;
  border-bottom: none;
  border-top-color: var(--red);
}
.field-error-pop.visible {
  opacity: 1;
  transform: translateY(0);
}

/* ── Tooltip popup near field ─────────────────────────────── */
.field-tip-pop {
  position: absolute;
  background: #1F3A5F;
  color: #e8f0fb;
  padding: 7px 11px;
  border-radius: 7px;
  font-size: 12px;
  line-height: 1.45;
  z-index: 9990;
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .15s ease, transform .15s ease;
  box-shadow: 0 4px 14px rgba(0,0,0,.28);
  max-width: 260px;
  word-break: break-word;
}
.field-tip-pop::before {
  content: '';
  position: absolute;
  top: -6px; left: 14px;
  border: 6px solid transparent;
  border-top: none;
  border-bottom-color: #1F3A5F;
}
.field-tip-pop.above::before {
  top: auto; bottom: -6px;
  border-bottom: none;
  border-top-color: #1F3A5F;
}
.field-tip-pop.visible {
  opacity: 1;
  transform: translateY(0);
}

/* ── PHONE (≤ 480px) ────────────────────────────────────── */
@media (max-width: 480px) {
  header h1 { font-size: 15px; }
  .form-body, .section-body { padding: 8px; }
  .field-label { font-size: 13px; color: #374151; }
  .field-input input,
  .field-input select { font-size: 16px; padding: 9px 10px; }
  .btn { padding: 9px 12px; font-size: 13px; min-height: 42px; }
  .btn-sm { padding: 6px 10px; font-size: 12px; }
  .section-header { font-size: 12px; padding: 10px 12px; }
  .cascade-table td { padding: 6px 5px; }
  .cascade-value { width: 110px; }
  .cascade-label { font-size: 13px; }
  .cascade-label small { display: none; }
  .form-tab { font-size: 11px; padding: 8px 10px; }
  .editor-toolbar { padding: 8px; }
  .md-value { font-size: 18px; }
}
