:root {
  --bg: #f4f6f2;
  --card: #ffffff;
  --text: #15201a;
  --muted: #5f6b63;
  --line: #d6ddd5;
  --brand: #1f7a3f;
  --brand-dark: #155a2d;
  --btn: #f2f5f1;
  --ok: #2fa84f;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
}

img, svg { max-width: 100%; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.85rem; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 14px 0 0; }

.container {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 16px;
}

/* Header */
.site-header {
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.nav-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 64px;
  flex-wrap: wrap;
  padding-top: 8px;
  padding-bottom: 8px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--brand-dark);
  font-weight: 800;
  font-size: 1.1rem;
  text-decoration: none;
  letter-spacing: 0.01em;
}
.logo { width: 28px; height: 28px; color: var(--brand); }
.nav-links { display: flex; gap: 18px; flex-wrap: wrap; }
.nav-links a {
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.92rem;
}
.nav-links a:hover { color: var(--brand); }

/* Hero */
.hero {
  background: linear-gradient(135deg, #1f7a3f 0%, #2fa84f 100%);
  color: #fff;
  padding: 56px 0 48px;
}
.eyebrow {
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.78rem;
  font-weight: 700;
  opacity: 0.9;
}
.hero h1 {
  margin: 0 0 12px;
  font-size: clamp(1.7rem, 4vw, 2.6rem);
  line-height: 1.15;
  font-weight: 800;
  max-width: 720px;
}
.lead {
  margin: 0;
  font-size: 1.05rem;
  max-width: 640px;
  opacity: 0.95;
}

/* Sections */
.section { padding: 40px 16px; }
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin-bottom: 18px;
}
.section h2 {
  margin: 0;
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--brand-dark);
}

.status-line {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
}
.dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--ok);
  animation: pulse 2s infinite;
}
.dot.stale { background: var(--muted); animation: none; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(47, 168, 79, 0.5); }
  70% { box-shadow: 0 0 0 7px rgba(47, 168, 79, 0); }
  100% { box-shadow: 0 0 0 0 rgba(47, 168, 79, 0); }
}

/* Sensor cards */
.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}
.tile {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 12px 16px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 170px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
.tile .label {
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1.25;
  min-height: 2.5em;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tile .icon { width: 40px; height: 40px; margin: 8px 0; }
.tile .value { font-size: 1.6rem; font-weight: 800; line-height: 1.1; }
.tile .unit {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  margin-top: 4px;
}

/* Buttons */
.actions, .row {
  display: flex;
  gap: 12px;
  margin-top: 22px;
  flex-wrap: wrap;
}
.row.between { justify-content: space-between; align-items: center; margin-top: 0; }
.btn {
  padding: 11px 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--btn);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { border-color: var(--brand); color: var(--brand-dark); }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn:focus-visible, .nav-links a:focus-visible, .icon-btn:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}
.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.6rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
}

/* Calibration and language cards */
.panel-wrap { padding-top: 0; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  max-width: 640px;
}
.card h2 { margin: 0 0 14px; font-size: 1.15rem; font-weight: 800; color: var(--brand-dark); }

.offset-form { display: grid; gap: 12px; }
.offset-form label {
  display: grid;
  grid-template-columns: 1fr 140px;
  align-items: center;
  gap: 12px;
  font-size: 0.92rem;
}
.offset-form input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  text-align: right;
}

.lang-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px 16px;
  font-size: 0.95rem;
}
.lang-list label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}
.lang-list input { accent-color: var(--brand); }

/* Review modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
}
.modal-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  width: 100%;
  max-width: 720px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.modal-card h2 { margin: 0; font-size: 1.1rem; color: var(--brand-dark); }
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 6px; }
table { border-collapse: collapse; font-size: 0.85rem; width: 100%; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
th { background: var(--btn); position: sticky; top: 0; }
td:first-child, th:first-child { text-align: left; }

/* Footer */
.site-footer {
  margin-top: auto;
  background: var(--card);
  border-top: 1px solid var(--line);
  padding: 18px 0;
  color: var(--muted);
  font-size: 0.9rem;
}

/* Tablet */
@media (max-width: 960px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}

/* Phone: single column tiles, 16px gutter, no horizontal scroll */
@media (max-width: 480px) {
  .hero { padding: 40px 0 36px; }
  .nav-links { gap: 12px; }
  .nav-links a { font-size: 0.85rem; }
  .grid { gap: 12px; }
  .tile { min-height: 150px; padding: 14px 8px; }
  .tile .value { font-size: 1.35rem; }
  .btn { flex: 1; text-align: center; }
  .card { padding: 18px; }
  .offset-form label { grid-template-columns: 1fr 110px; }
}

/* App wrapper: keeps the footer at the bottom, like the body did before sign-in was added */
#appView { display: flex; flex-direction: column; flex: 1; }

/* Sign-in page */
.auth {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 32px 16px;
  background: linear-gradient(135deg, #1f7a3f 0%, #2fa84f 100%);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
}
.auth-brand { margin-bottom: 18px; }
.auth-card h1 { margin: 0; font-size: 1.5rem; font-weight: 800; color: var(--brand-dark); }
.auth-lead { margin: 6px 0 18px; }
.auth-form { display: grid; gap: 6px; }
.auth-form label { font-weight: 600; font-size: 0.9rem; margin-top: 6px; }
.auth-form input, .select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font-size: 0.95rem;
}
.auth-form input:focus, .select:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.auth-form .btn { margin-top: 14px; }
.auth-msg { min-height: 1.4em; margin: 12px 0 0; font-size: 0.9rem; font-weight: 600; color: #b3261e; }
.auth-msg.ok { color: var(--brand-dark); }
.auth-links { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-top: 8px; }
#previewBtn { width: 100%; margin-top: 16px; }
.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--brand-dark);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.link-btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.nav-signout { text-decoration: none; color: var(--muted); font-size: 0.92rem; }
.nav-signout:hover { color: var(--brand); }

/* Crop comparison */
.card-wide { max-width: none; }
.crop-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px 20px; flex-wrap: wrap; margin-bottom: 16px; }
.crop-head h2 { margin-bottom: 0; }
.crop-lead { margin-top: 4px; }
.crop-head .select { width: auto; min-width: 200px; }
.crop-table th, .crop-table td { text-align: left; white-space: normal; }
.crop-table td:nth-child(2) { font-weight: 700; }
.level {
  display: inline-block;
  min-width: 78px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-align: center;
}
.level.low { background: #fde8e6; color: #a1261b; }
.level.medium { background: #e3f4e8; color: #17692f; }
.level.high { background: #fff1d6; color: #8a5300; }
.level.missing { background: var(--btn); color: var(--muted); }
.verdict { margin: 12px 0 0; font-weight: 800; }
.verdict.good { color: #17692f; }
.verdict.warn { color: #8a5300; }
.verdict.bad { color: #a1261b; }

/* Gemini analysis */
.ai-box { margin-top: 20px; border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; background: #f9fbf8; }
.ai-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px 20px; flex-wrap: wrap; }
.ai-head h3 { margin: 0; font-size: 1.05rem; color: var(--brand-dark); }
.ai-note { margin: 4px 0 0; max-width: 520px; }
.ai-controls { display: flex; gap: 10px; flex-wrap: wrap; }
.ai-controls .select { width: auto; min-width: 150px; }
.ai-status { margin: 14px 0 0; color: var(--muted); }
.ai-status.error { color: #a1261b; font-weight: 600; }
.ai-summary { margin: 14px 0 10px; font-size: 1.02rem; }
.ai-actions { margin: 0; padding-left: 22px; }
.ai-actions li { margin-bottom: 12px; }
.ai-actions p { margin: 2px 0 0; }
.ai-when { display: inline-block; margin-right: 8px; font-size: 0.72rem; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; color: var(--brand); }
.ai-box h4 { margin: 14px 0 4px; font-size: 0.95rem; }
.ai-cautions { margin: 0; padding-left: 20px; color: var(--muted); }
.btn:disabled { opacity: 0.6; cursor: wait; }

@media (max-width: 480px) {
  .crop-head .select, .ai-controls .select { width: 100%; }
  .ai-controls { width: 100%; }
  .auth-card { padding: 22px 18px; }
  .crop-table th, .crop-table td { padding: 8px 6px; }
  .level { min-width: 0; padding: 3px 7px; font-size: 0.7rem; }
  .ai-box { padding: 14px; }
}

/* Sensor picker and "no sensor yet" note */
.device-row { display: flex; align-items: center; gap: 10px; margin: -6px 0 16px; font-weight: 600; font-size: 0.9rem; }
.device-row .select { width: auto; min-width: 180px; }
.device-note {
  margin: 0 0 16px;
  padding: 12px 14px;
  border-left: 4px solid var(--brand);
  background: #e9f5ec;
  border-radius: 0 8px 8px 0;
  font-size: 0.92rem;
}
.device-note code { display: inline-block; margin-top: 4px; padding: 2px 6px; background: #fff; border-radius: 4px; user-select: all; word-break: break-all; }
