/* ── Variables ── */
:root {
  --primary: #1B4F72;
  --primary-light: #2E86C1;
  --accent: #F39C12;
  --danger: #C0392B;
  --warning: #E67E22;
  --success: #27AE60;
  --dark: #2C3E50;
  --muted: #7F8C8D;
  --light: #ECF0F1;
  --border: #D5D8DC;
  --white: #FFFFFF;
  --bg: #F4F6F9;
  --sidebar-w: 220px;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,.08);
  --level-bajo: #27AE60;
  --level-medio: #F39C12;
  --level-alto: #E67E22;
  --level-critico: #C0392B;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--dark);
  display: flex;
  min-height: 100vh;
  font-size: 14px;
}

/* ── SIDEBAR ── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--primary);
  color: white;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  overflow-y: auto;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 16px 16px;
  border-bottom: 1px solid rgba(255,255,255,.1);
}

.logo-icon {
  width: 38px; height: 38px;
  background: var(--accent);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 12px;
  flex-shrink: 0;
}

.logo-text { display: flex; flex-direction: column; }
.logo-main { font-size: 13px; font-weight: 700; line-height: 1.2; }
.logo-sub { font-size: 10px; opacity: .65; }

.sidebar-nav { list-style: none; padding: 8px 0; flex: 1; }

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  color: rgba(255,255,255,.7);
  text-decoration: none;
  font-size: 13px;
  transition: all .15s;
  border-radius: 0;
}

.sidebar-nav a:hover { color: white; background: rgba(255,255,255,.08); }
.sidebar-nav a.active { color: white; background: rgba(255,255,255,.15); border-left: 3px solid var(--accent); }

.nav-icon { font-size: 14px; flex-shrink: 0; }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid rgba(255,255,255,.1);
  font-size: 11px;
  opacity: .6;
}

/* ── MAIN ── */
.main-content {
  margin-left: var(--sidebar-w);
  flex: 1;
  min-height: 100vh;
  overflow-y: auto;
}

.section { display: none; padding: 24px 28px; }
.section.active { display: block; }

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.section-header h1 {
  font-size: 22px;
  font-weight: 700;
  color: var(--dark);
}

.header-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── CARDS ── */
.card {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.card-header h2 {
  font-size: 15px;
  font-weight: 600;
  color: var(--dark);
}

.card > *:not(.card-header) { padding: 16px 20px; }
.card > .table-wrapper { padding: 0; }
.card > .chart-container { padding: 12px 16px; }

/* ── KPI GRID ── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 20px;
}

.kpi-card {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 20px;
  text-align: center;
}

.kpi-label { font-size: 12px; color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: .5px; }
.kpi-value { font-size: 28px; font-weight: 700; color: var(--primary); margin: 4px 0; }
.kpi-sub { font-size: 11px; color: var(--muted); }

/* ── PREDICTION BOXES ── */
.prediction-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-top: 4px;
}

.pred-box {
  border-radius: var(--radius);
  padding: 14px;
  color: white;
  text-align: center;
  position: relative;
}

.pred-box .pred-week { font-size: 11px; font-weight: 600; opacity: .85; text-transform: uppercase; }
.pred-box .pred-date { font-size: 12px; opacity: .8; margin: 2px 0 6px; }
.pred-box .pred-num { font-size: 32px; font-weight: 800; line-height: 1; }
.pred-box .pred-level { font-size: 11px; font-weight: 700; margin-top: 6px; background: rgba(0,0,0,.2); border-radius: 4px; padding: 2px 6px; display: inline-block; }
.pred-box .pred-factor { font-size: 10px; opacity: .75; margin-top: 4px; }
.pred-box .pred-confidence { font-size: 10px; opacity: .7; }

.pred-box.bajo { background: var(--level-bajo); }
.pred-box.medio { background: var(--level-medio); }
.pred-box.alto { background: var(--level-alto); }
.pred-box.critico { background: var(--level-critico); }

.pred-skeleton { color: var(--muted); font-size: 13px; padding: 20px; text-align: center; }

.pred-analysis {
  margin-top: 8px;
  padding: 12px;
  background: var(--light);
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.6;
}

.pred-analysis h4 { font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin-bottom: 4px; margin-top: 8px; }
.pred-analysis h4:first-child { margin-top: 0; }

/* ── ALERT BANNER ── */
.alert-banner {
  background: var(--danger);
  color: white;
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-weight: 600;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.alert-banner.warning { background: var(--warning); }

/* ── LAYOUTS ── */
.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
  align-items: start;
}

/* ── FORMS ── */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 12px; font-weight: 600; color: var(--dark); margin-bottom: 5px; text-transform: uppercase; letter-spacing: .4px; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  color: var(--dark);
  background: white;
  transition: border-color .15s;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none;
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(46,134,193,.1);
}

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.total-preview { text-align: right; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.total-preview strong { font-size: 18px; color: var(--primary); }

/* ── BUTTONS ── */
.btn {
  padding: 8px 16px;
  border-radius: 6px;
  border: none;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all .15s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-light); }
.btn-secondary { background: var(--light); color: var(--dark); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: var(--danger); color: white; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-full { width: 100%; justify-content: center; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── TABLES ── */
.table-wrapper { overflow-x: auto; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th {
  background: var(--light);
  padding: 10px 14px;
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  white-space: nowrap;
}

.data-table td {
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: var(--bg); }
.loading-row { text-align: center; color: var(--muted); padding: 20px !important; }

/* ── LEVEL BADGES ── */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  color: white;
}
.badge-bajo { background: var(--level-bajo); }
.badge-medio { background: var(--level-medio); }
.badge-alto { background: var(--level-alto); }
.badge-critico { background: var(--level-critico); }
.badge-excelente { background: var(--success); }
.badge-bueno { background: var(--primary-light); }
.badge-aceptable { background: var(--warning); }
.badge-pobre { background: var(--danger); }

/* ── WEATHER LIST ── */
.weather-list { display: flex; flex-direction: column; gap: 6px; }
.weather-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: var(--bg);
  border-radius: 6px;
  gap: 10px;
  font-size: 13px;
}
.weather-item .w-date { font-weight: 600; width: 80px; flex-shrink: 0; }
.weather-item .w-temps { color: var(--danger); font-weight: 600; min-width: 70px; }
.weather-item .w-hum { color: var(--primary-light); min-width: 40px; }
.weather-item .w-rain { color: var(--success); min-width: 40px; }
.weather-item .w-wind { color: var(--muted); font-size: 12px; }

/* ── FARM STATUS ── */
.farm-status-card {
  background: var(--bg);
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 8px;
}
.farm-status-card .lot-header { display: flex; align-items: center; justify-content: space-between; }
.farm-status-card .lot-number { font-weight: 700; font-size: 15px; color: var(--primary); }
.lot-badge { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 700; }
.lot-badge-active { background: var(--success); color: white; }
.lot-badge-planned { background: var(--primary-light); color: white; }
.lot-badge-completed { background: var(--muted); color: white; }

.event-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.event-item:last-child { border-bottom: none; }
.event-date { color: var(--muted); font-size: 12px; min-width: 80px; }
.event-type-badge { font-size: 11px; padding: 2px 6px; border-radius: 4px; background: var(--light); font-weight: 600; }

/* ── CHART ── */
.chart-container { position: relative; }
#uvChart { max-height: 300px; }

/* ── MODAL ── */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 1000;
  display: flex; align-items: center; justify-content: center;
}
.modal.hidden { display: none; }
.modal-content {
  background: white;
  border-radius: var(--radius);
  width: 460px;
  max-width: 95vw;
  box-shadow: 0 8px 32px rgba(0,0,0,.15);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 16px; font-weight: 700; }
.modal-close { background: none; border: none; font-size: 18px; cursor: pointer; color: var(--muted); }
.modal-content form { padding: 20px; }

/* ── TOAST ── */
.toast-container { position: fixed; bottom: 24px; right: 24px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; }

.toast {
  background: var(--dark);
  color: white;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,.2);
  animation: slideIn .2s ease;
  max-width: 320px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-warning { background: var(--warning); }

@keyframes slideIn { from { transform: translateX(40px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ── LOADING ── */
.loading-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 9000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: white;
  font-size: 15px;
  font-weight: 600;
}
.loading-overlay.hidden { display: none; }

.spinner {
  width: 44px; height: 44px;
  border: 4px solid rgba(255,255,255,.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── UTILS ── */
.hidden { display: none !important; }
.text-muted { color: var(--muted); }
.small { font-size: 12px; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.gap-8 { gap: 8px; }
code { background: var(--light); padding: 2px 6px; border-radius: 4px; font-size: 12px; }

.filter-row { display: flex; gap: 8px; align-items: center; }
.filter-row input { padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; }

.field-hint { display: block; font-size: 10px; color: var(--muted); margin-top: 3px; font-style: italic; }

.api-status { padding: 12px; background: var(--bg); border-radius: 6px; font-size: 13px; }

.actions-list-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.actions-list-item:last-child { border-bottom: none; }

/* ── LOGIN SCREEN ── */
.login-screen {
  position: fixed;
  inset: 0;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.login-screen.hidden { display: none !important; }

.login-card {
  background: white;
  border-radius: 12px;
  padding: 40px 36px;
  width: 340px;
  box-shadow: 0 8px 32px rgba(0,0,0,.25);
  text-align: center;
}

.login-logo {
  margin-bottom: 16px;
}

.login-logo img {
  height: 64px;
  object-fit: contain;
}

.login-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 4px;
}

.login-sub {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 24px;
}

.login-error {
  background: #fdecea;
  color: var(--danger);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  margin-bottom: 12px;
  text-align: left;
}

.btn-full { width: 100%; }

/* ── APP WRAPPER ── */
#app-wrapper {
  display: flex;
  width: 100%;
  min-height: 100vh;
}

#app-wrapper.hidden { display: none !important; }

/* ── SIDEBAR LOGO IMG ── */
.sidebar-logo-img {
  height: 38px;
  width: 38px;
  object-fit: contain;
  border-radius: 6px;
}

/* ── LOGOUT BUTTON ── */
.btn-logout {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 7px 0;
  background: rgba(255,255,255,.1);
  color: rgba(255,255,255,.75);
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: background .15s;
}

.btn-logout:hover {
  background: rgba(255,255,255,.2);
  color: white;
}

/* ── RESPONSIVE ── */
@media (max-width: 1024px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .prediction-grid { grid-template-columns: repeat(2, 1fr); }
  .two-col { grid-template-columns: 1fr; }
}

@media (max-width: 700px) {
  .sidebar { transform: translateX(-100%); }
  .main-content { margin-left: 0; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}
