/* ============================================================
   AgApp — mobile-first styles
   Calm agronomy palette: greens + earth tones on warm off-white.
   No external fonts/CDNs — system font stack only.
   ============================================================ */

/* The [hidden] attribute must always win, even over flex/grid display rules. */
[hidden] { display: none !important; }

:root {
  /* Palette */
  --bg:            #f5f3ea;   /* warm off-white */
  --surface:       #ffffff;
  --surface-2:     #f0ede1;
  --ink:           #23271f;   /* near-black green-brown */
  --ink-soft:      #55584d;
  --ink-faint:     #7c7f74;
  --line:          #ded9c8;

  --green:         #2f6b3c;   /* primary */
  --green-dk:      #234f2d;
  --green-lt:      #e7efe1;
  --earth:         #7a5c3e;   /* accent */

  /* Category chip colors */
  --cat-disease:   #b23b3b;
  --cat-insect:    #c98a12;
  --cat-nutrient:  #2c6fb0;
  --cat-herbicide: #6f4bab;
  --cat-abiotic:   #6b7280;
  --cat-weed:      #4d7c4d;
  --cat-unknown:   #8a8d82;

  --danger:        #b23b3b;
  --caution:       #9a6a12;

  --radius:        14px;
  --radius-sm:     10px;
  --shadow:        0 1px 2px rgba(35, 39, 31, 0.06), 0 4px 14px rgba(35, 39, 31, 0.06);
  --tap:           52px;       /* minimum tap target */

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "Noto Sans", sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* Accessibility helpers */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}
.skip-link {
  position: absolute;
  left: 50%; transform: translateX(-50%) translateY(-140%);
  top: 8px; z-index: 100;
  background: var(--green-dk); color: #fff;
  padding: 10px 16px; border-radius: 8px;
  transition: transform .15s ease;
}
.skip-link:focus { transform: translateX(-50%) translateY(0); }

:focus-visible {
  outline: 3px solid var(--green);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- Header ---------- */
.app-header {
  background: linear-gradient(160deg, var(--green) 0%, var(--green-dk) 100%);
  color: #fff;
  padding: calc(env(safe-area-inset-top, 0px) + 16px) 18px 14px;
  box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 20;
}
.app-header__bar { display: flex; align-items: center; gap: 12px; }
.app-logo { display: inline-flex; flex: none; }
.app-header__titles { min-width: 0; }
.app-title { margin: 0; font-size: 1.5rem; font-weight: 700; letter-spacing: .2px; }
.app-tagline { margin: 1px 0 0; font-size: .9rem; opacity: .92; }
.app-header__note {
  margin: 10px 0 0;
  font-size: .8rem;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.22);
  padding: 6px 10px; border-radius: 8px;
  display: inline-block;
}

/* ---------- Page layout ---------- */
.page {
  max-width: 620px;
  margin: 0 auto;
  padding: 16px 14px 40px;
}

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin: 0 0 16px;
}
.card__heading { margin: 0 0 4px; font-size: 1.1rem; color: var(--green-dk); }
.card__hint { margin: 0 0 14px; font-size: .88rem; color: var(--ink-soft); }

/* ---------- Photo capture ---------- */
.photo-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%;
  min-height: var(--tap);
  padding: 18px;
  background: var(--green-lt);
  border: 2px dashed var(--green);
  border-radius: var(--radius-sm);
  color: var(--green-dk);
  font-size: 1.05rem; font-weight: 600;
  cursor: pointer;
  text-align: center;
  transition: background .15s ease, border-color .15s ease;
}
.photo-btn:hover { background: #dceada; }
.photo-btn:active { background: #cfe2cc; }
.photo-btn__icon { flex: none; }
.photo-btn input[type="file"] {
  position: absolute; width: 1px; height: 1px;
  opacity: 0; overflow: hidden; clip: rect(0 0 0 0);
}
/* keyboard focus on the hidden input reflects on the label */
.photo-btn:focus-within {
  outline: 3px solid var(--green);
  outline-offset: 2px;
}

.preview { margin-top: 14px; display: flex; gap: 12px; align-items: flex-start; }
.preview__img {
  width: 96px; height: 96px; flex: none;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface-2);
}
.preview__meta { display: flex; flex-direction: column; gap: 6px; padding-top: 2px; }
.preview__info { font-size: .82rem; color: var(--ink-faint); }

.link-btn {
  background: none; border: none; padding: 4px 0;
  color: var(--green); font-weight: 600; font-size: .92rem;
  cursor: pointer; text-align: left;
  align-self: flex-start;
  text-decoration: underline;
}

/* ---------- Form fields ---------- */
.field { margin: 0 0 14px; }
.field:last-child { margin-bottom: 0; }
.field label,
.field legend {
  display: block;
  font-size: .9rem; font-weight: 600;
  color: var(--ink); margin-bottom: 6px;
  padding: 0;
}
.req { color: var(--danger); }

input[type="text"],
input[type="date"],
select,
textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 12px 14px;
  font-family: inherit; font-size: 1rem;
  color: var(--ink);
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius-sm);
  -webkit-appearance: none; appearance: none;
}
textarea { min-height: 84px; resize: vertical; line-height: 1.45; }
input:focus, select:focus, textarea:focus { border-color: var(--green); }
input::placeholder, textarea::placeholder { color: var(--ink-faint); }

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cpath d='M4 6l4 4 4-4' fill='none' stroke='%2355584d' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 40px;
}

.field--split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field--split > div { margin: 0; }

.field--fieldset { border: none; padding: 0; margin: 0 0 14px; }

/* Weather chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.chip {
  display: inline-flex; align-items: center;
  min-height: 44px; padding: 8px 14px;
  border: 1.5px solid var(--line); border-radius: 999px;
  background: var(--surface); cursor: pointer;
  font-size: .92rem; user-select: none;
  transition: background .12s ease, border-color .12s ease, color .12s ease;
}
.chip input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chip:has(input:checked) {
  background: var(--green-lt); border-color: var(--green); color: var(--green-dk);
  font-weight: 600;
}
.chip:has(input:focus-visible) { outline: 3px solid var(--green); outline-offset: 2px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap);
  padding: 14px 22px;
  font-family: inherit; font-size: 1.05rem; font-weight: 700;
  border-radius: var(--radius-sm);
  border: 1.5px solid transparent;
  cursor: pointer;
  transition: background .15s ease, opacity .15s ease, box-shadow .15s ease;
}
.btn--primary { background: var(--green); color: #fff; width: 100%; box-shadow: var(--shadow); }
.btn--primary:hover:not(:disabled) { background: var(--green-dk); }
.btn--secondary { background: var(--green-dk); color: #fff; }
.btn--secondary:hover:not(:disabled) { background: #1c3f24; }
.btn--ghost { background: var(--surface); color: var(--green-dk); border-color: var(--green); width: 100%; }
.btn--ghost:hover { background: var(--green-lt); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.diagnose-bar { margin: 4px 0 8px; }
.diagnose-bar__help { margin: 8px 2px 0; font-size: .82rem; color: var(--ink-faint); text-align: center; }

.startover-bar { margin-top: 20px; }

/* ---------- Alerts ---------- */
.alert {
  border-radius: var(--radius-sm);
  padding: 12px 14px; margin: 14px 0 0;
  font-size: .92rem;
  border: 1px solid transparent;
}
.alert--error { background: #fbeaea; border-color: #e6bcbc; color: #7d2020; }
.alert--caution { background: #fdf3dd; border-color: #ecd79a; color: #6f4e0e; }

/* ---------- Loading ---------- */
.loading {
  text-align: center;
  padding: 56px 20px;
}
.spinner {
  width: 46px; height: 46px; margin: 0 auto 18px;
  border: 4px solid var(--green-lt);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading__title { margin: 0 0 4px; font-size: 1.2rem; font-weight: 700; color: var(--green-dk); }
.loading__sub { margin: 0; color: var(--ink-soft); font-size: .92rem; }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
}

/* ---------- Results ---------- */
.disclaimer {
  background: var(--green-dk);
  color: #f2f6ee;
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: .92rem;
  line-height: 1.45;
  box-shadow: var(--shadow);
}
.disclaimer strong { color: #fff; display: block; margin-bottom: 2px; }

.results-heading { margin: 20px 2px 12px; font-size: 1.15rem; color: var(--green-dk); }

.candidates { display: flex; flex-direction: column; gap: 12px; }

/* Candidate card */
.cand {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 5px solid var(--cat-unknown);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.cand[data-category="disease"]   { border-left-color: var(--cat-disease); }
.cand[data-category="insect"]    { border-left-color: var(--cat-insect); }
.cand[data-category="nutrient"]  { border-left-color: var(--cat-nutrient); }
.cand[data-category="herbicide"] { border-left-color: var(--cat-herbicide); }
.cand[data-category="abiotic"]   { border-left-color: var(--cat-abiotic); }
.cand[data-category="weed"]      { border-left-color: var(--cat-weed); }
.cand[data-category="unknown"]   { border-left-color: var(--cat-unknown); }

.cand__summary {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  padding: 14px 16px;
  background: none; border: none; cursor: pointer;
  text-align: left; font-family: inherit;
  min-height: var(--tap);
}
.cand__rank {
  flex: none;
  width: 30px; height: 30px;
  display: grid; place-items: center;
  background: var(--surface-2); color: var(--ink-soft);
  border-radius: 50%; font-weight: 700; font-size: .95rem;
}
.cand__head { flex: 1 1 auto; min-width: 0; }
.cand__label {
  margin: 0; font-size: 1.12rem; font-weight: 700; color: var(--ink);
  line-height: 1.25;
}
.cand__tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.cand__caret {
  flex: none; color: var(--ink-faint);
  transition: transform .18s ease;
}
.cand[open] .cand__caret { transform: rotate(180deg); }

/* Chips + badges */
.chip-cat, .badge-conf {
  display: inline-flex; align-items: center;
  font-size: .74rem; font-weight: 700;
  padding: 3px 9px; border-radius: 999px;
  text-transform: capitalize; letter-spacing: .2px;
  color: #fff;
}
.chip-cat[data-category="disease"]   { background: var(--cat-disease); }
.chip-cat[data-category="insect"]    { background: var(--cat-insect); }
.chip-cat[data-category="nutrient"]  { background: var(--cat-nutrient); }
.chip-cat[data-category="herbicide"] { background: var(--cat-herbicide); }
.chip-cat[data-category="abiotic"]   { background: var(--cat-abiotic); }
.chip-cat[data-category="weed"]      { background: var(--cat-weed); }
.chip-cat[data-category="unknown"]   { background: var(--cat-unknown); }

.badge-conf { border: 1.5px solid transparent; }
.badge-conf[data-conf="high"]   { background: #e7efe1; color: var(--green-dk); border-color: var(--green); }
.badge-conf[data-conf="medium"] { background: #fdf3dd; color: #6f4e0e; border-color: #ecd79a; }
.badge-conf[data-conf="low"]    { background: var(--surface-2); color: var(--ink-soft); border-color: var(--line); }

/* Card body */
.cand__body {
  padding: 4px 16px 18px;
  border-top: 1px solid var(--line);
}
.cand__section { margin-top: 14px; }
.cand__section:first-child { margin-top: 12px; }
.cand__section h4 {
  margin: 0 0 6px; font-size: .82rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .5px;
  color: var(--ink-faint);
}
.cand__section--checks {
  background: var(--green-lt);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
}
.cand__section--checks h4 { color: var(--green-dk); }
.cand__section ul { margin: 0; padding-left: 20px; }
.cand__section li { margin: 0 0 5px; }
.cand__section li:last-child { margin-bottom: 0; }
.cand__section p { margin: 0; color: var(--ink-soft); }

/* References */
.refs { list-style: none; margin: 0; padding: 0; }
.refs li { margin: 0 0 8px; }
.ref-link {
  color: var(--green); font-weight: 600; text-decoration: underline;
  word-break: break-word;
}
.ref-perm {
  display: block; font-size: .76rem; color: var(--ink-faint); margin-top: 1px;
}

/* Next steps callout */
.callout {
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: 4px solid var(--earth);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin: 18px 0 0;
}
.callout__title { margin: 0 0 6px; font-size: 1.05rem; color: var(--earth); }
.callout__body { margin: 0; }

/* Feedback */
.feedback { margin-top: 18px; }
.toggle { display: flex; gap: 10px; margin: 0 0 16px; }
.toggle__opt {
  flex: 1; min-height: var(--tap);
  border: 1.5px solid var(--line); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--ink);
  font-family: inherit; font-size: 1rem; font-weight: 600; cursor: pointer;
  transition: background .12s ease, border-color .12s ease, color .12s ease;
}
.toggle__opt[aria-pressed="true"][data-agree="true"] {
  background: var(--green-lt); border-color: var(--green); color: var(--green-dk);
}
.toggle__opt[aria-pressed="true"][data-agree="false"] {
  background: #fbeaea; border-color: var(--danger); color: #7d2020;
}
.feedback__msg { margin: 12px 0 0; font-weight: 600; color: var(--green-dk); }
.feedback__msg.is-error { color: #7d2020; }

/* Footer */
.app-footer {
  margin-top: 26px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.app-footer p { margin: 0; font-size: .78rem; color: var(--ink-faint); line-height: 1.5; }

/* Small phones */
@media (max-width: 360px) {
  body { font-size: 16px; }
  .field--split { grid-template-columns: 1fr; }
}
