/* AgResource Dashboard — Comer dark-theme palette.
   Cards stay cream in both modes; page + sidebar are deep brown. */

:root {
  /* Brand */
  --brand-green:     #6b8e23;
  --brand-blue:      #2a7ca0;
  --brand-brown:     #40351e;
  --brand-brown-deep:#2d2518;
  --brand-cream:     #f9eddc;
  --brand-gold:      #ba7c2b;
  --brand-burgundy:  #440008;

  /* Status */
  --status-ok:        var(--brand-green);
  --status-warning:   var(--brand-gold);
  --status-critical:  var(--brand-burgundy);
  --status-pending:   #8a8175;
  --status-info:      var(--brand-blue);

  /* Surfaces */
  --surface-page:    var(--brand-brown);
  --surface-card:    var(--brand-cream);
  --surface-nested:  #f0e3c8;
  --surface-sidebar: #1a1510;
  --surface-header:  #2d2518;

  /* Text — opacity-based muted variants were too soft on both surfaces.
     Solid hex tones a couple steps off the base keep visual hierarchy while
     staying readable on cream cards (≥7:1 contrast) and dark page (≥6:1). */
  --text-body:        var(--brand-cream);
  --text-body-muted:  #d6c9aa;            /* solid warm tan on dark — was 65% alpha */
  --text-card:        #2a210f;            /* deeper than brand brown for body text on cream */
  --text-card-muted:  #4f4128;            /* solid dark olive-brown — was 62% alpha */
  --text-card-strong: #1a1408;            /* near-black for primary card values */
  --text-sidebar:     var(--brand-cream);

  /* Lines & shadows */
  --line:            rgba(64, 53, 30, 0.18);
  --line-on-page:    rgba(249, 237, 220, 0.16);
  --shadow:          0 1px 2px rgba(0, 0, 0, 0.25), 0 8px 24px rgba(0, 0, 0, 0.28);
  --gridline:        rgba(64, 53, 30, 0.10);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Helvetica, sans-serif;
       background: var(--surface-page); color: var(--text-body); min-height: 100vh; }

/* ---- Login ----------------------------------------------------------- */
.login-bg { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--surface-page); }
.login-card { background: var(--surface-card); color: var(--text-card); padding: 40px 36px; border-radius: 12px;
              box-shadow: var(--shadow); width: 380px; }
.login-card h1 { color: var(--brand-brown); font-size: 22px; margin-bottom: 6px; }
.login-card p { color: var(--text-card-muted); font-size: 13px; margin-bottom: 22px; }
.login-card input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px;
                    font-size: 14px; margin-bottom: 14px; background: white; color: var(--text-card); }
.login-card button { width: 100%; padding: 11px; background: var(--brand-blue); color: white; border: none;
                     border-radius: 6px; font-size: 15px; font-weight: 600; cursor: pointer; }
.login-card button:hover { background: #246e8c; }
.login-card .error { background: rgba(68, 0, 8, 0.10); color: var(--brand-burgundy); padding: 8px 10px;
                     border-radius: 6px; font-size: 13px; margin-bottom: 12px; }

/* ---- Layout ---------------------------------------------------------- */
.dashboard-layout { display: flex; flex-direction: column; min-height: 100vh; }

.dashboard-header { background: var(--surface-header); padding: 14px 24px; display: flex;
    justify-content: space-between; align-items: center; color: var(--text-body);
    border-bottom: 1px solid var(--line-on-page); }
.header-left { display: flex; align-items: center; gap: 16px; }
.header-title h1 { font-size: 18px; font-weight: 700; letter-spacing: 0.01em; color: var(--brand-cream); }
.header-title .header-subtitle { font-size: 11px; opacity: .75; display: block; margin-top: 2px; }
.header-right { display: flex; align-items: center; gap: 14px; font-size: 13px; color: var(--text-body-muted); }
.header-right a { color: var(--brand-cream); text-decoration: none;
    padding: 6px 12px; background: rgba(186,124,43,.18); border: 1px solid rgba(186,124,43,.5); border-radius: 4px; font-size: 12px; }
.header-right a:hover { background: rgba(186,124,43,.32); border-color: rgba(186,124,43,.95); }

.dashboard-content { display: flex; flex: 1; }

aside.sidebar { width: 240px; background: var(--surface-sidebar); border-right: 1px solid var(--line-on-page);
    display: flex; flex-direction: column; color: var(--text-sidebar); }
.sidebar-nav { padding: 14px 0; flex: 1; }
.nav-item { width: 100%; display: flex; align-items: center; gap: 11px; padding: 11px 20px;
    border: none; background: transparent; color: var(--text-sidebar); font-size: 14px; cursor: pointer;
    text-align: left; border-left: 3px solid transparent; font-weight: 500; }
.nav-item:hover { background: rgba(186,124,43,.12); }
.nav-item.active { background: rgba(42,124,160,.22); border-left-color: var(--brand-blue);
    font-weight: 700; }
.nav-icon { display: inline-flex; align-items: center; justify-content: center; width: 22px; height: 22px; font-size: 16px; }
.sidebar-footer { padding: 14px 18px; border-top: 1px solid var(--line-on-page); font-size: 12px; color: var(--text-body-muted); }
.sidebar-footer .stat { display: flex; justify-content: space-between; padding: 3px 0; }
.sidebar-footer .stat span:last-child { color: var(--brand-cream); font-weight: 600; }

main.main-panel { flex: 1; padding: 22px 28px; overflow-y: auto; color: var(--text-body); }

/* ---- Alert banner ---------------------------------------------------- */
.alert-banner { background: rgba(186,124,43,.18); border-left: 4px solid var(--brand-gold); padding: 11px 16px;
    display: flex; align-items: center; gap: 12px; margin-bottom: 18px; border-radius: 4px;
    font-size: 14px; color: var(--brand-cream); }
.alert-banner.critical { background: rgba(68,0,8,.22); border-left-color: var(--brand-burgundy); }
.alert-banner button { margin-left: auto; padding: 5px 14px; background: var(--brand-blue); color: white;
    border: none; border-radius: 4px; font-size: 12px; cursor: pointer; font-weight: 600; }

/* ---- View shared ----------------------------------------------------- */
.view { display: none; }
.view.active { display: block; }
h2.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .08em;
    color: var(--text-body); margin: 24px 0 12px; font-weight: 700; }
h2.section-title:first-child { margin-top: 0; }

/* KPI cards */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.kpi-card { background: var(--surface-card); color: var(--text-card); border-radius: 8px; padding: 16px 20px;
    border: 1px solid var(--line); box-shadow: var(--shadow); }
.kpi-card .label { font-size: 11px; color: var(--text-card); text-transform: uppercase; letter-spacing: .06em; font-weight: 700; }
.kpi-card .value { font-size: 32px; font-weight: 700; margin-top: 4px; line-height: 1; color: var(--text-card-strong); }
.kpi-card.ok .value      { color: var(--status-ok); }
.kpi-card.stale .value   { color: var(--status-warning); }
.kpi-card.pending .value { color: var(--status-pending); }
.kpi-card.critical .value{ color: var(--status-critical); }
.kpi-card .sub { font-size: 12px; color: var(--text-card-muted); margin-top: 5px; }

/* Sub-location cards */
.sub-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.sub-card { background: var(--surface-card); color: var(--text-card); border-radius: 8px; padding: 16px 18px;
    border: 1px solid var(--line); box-shadow: var(--shadow); }
.sub-card .sub-name { font-size: 16px; font-weight: 700; color: var(--text-card-strong); }
.sub-card .sub-meta { font-size: 12px; color: var(--text-card-muted); margin-top: 3px; font-weight: 500; }
.sub-card .badges { margin-top: 10px; display: flex; gap: 6px; flex-wrap: wrap; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 11px; font-weight: 700; font-size: 11px;
    letter-spacing: 0.02em; }
.badge.ok       { background: rgba(107,142,35,.18);  color: #4d6818; border: 1px solid rgba(107,142,35,.35); }
.badge.stale    { background: rgba(186,124,43,.22);  color: #7c5219; border: 1px solid rgba(186,124,43,.45); }
.badge.pending  { background: rgba(64,53,30,.10);    color: #5a4d34; border: 1px solid rgba(64,53,30,.25); }
.badge.critical { background: rgba(68,0,8,.20);      color: var(--brand-burgundy); border: 1px solid rgba(68,0,8,.40); }

/* Sensor cards (per-metric views) */
.sensor-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.sensor-card { background: var(--surface-card); color: var(--text-card); border-radius: 8px; padding: 14px 16px;
    border: 1px solid var(--line); border-left: 4px solid var(--status-ok); cursor: pointer;
    transition: transform .12s, box-shadow .12s; box-shadow: var(--shadow); }
.sensor-card:hover { transform: translateY(-1px); box-shadow: 0 1px 2px rgba(0,0,0,.3), 0 12px 30px rgba(0,0,0,.35); }
.sensor-card.stale    { border-left-color: var(--status-warning); }
.sensor-card.pending  { border-left-color: var(--status-pending); }
.sensor-card.critical { border-left-color: var(--status-critical); }
.sensor-card .sc-name { font-size: 13px; font-weight: 700; color: var(--text-card-strong);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sensor-card .sc-sub { font-size: 11px; color: var(--text-card-muted); margin-top: 2px; margin-bottom: 9px; font-weight: 500; }
.sensor-card .sc-row { display: flex; justify-content: space-between; padding: 3px 0; font-size: 13px; }
.sensor-card .sc-row .k { color: var(--text-card-muted); font-weight: 500; }
.sensor-card .sc-row .v { color: var(--text-card-strong); font-weight: 700; }
.sensor-card .sc-foot { margin-top: 10px; padding-top: 8px; border-top: 1px dotted var(--line);
    display: flex; justify-content: space-between; font-size: 11px; color: var(--text-card-muted); align-items: center; }

/* Per-sub-location wrapper */
.sub-section { margin-bottom: 28px; }
.sub-section .sub-header { display: flex; align-items: baseline; gap: 12px; margin-bottom: 10px; }
.sub-section .sub-header h3 { font-size: 16px; font-weight: 700; color: var(--brand-cream); }
.sub-section .sub-header .meta { font-size: 12px; color: var(--text-body-muted); font-weight: 500; }

/* Table (Sensor Status) */
table.diag { width: 100%; border-collapse: collapse; background: var(--surface-card); color: var(--text-card);
    border: 1px solid var(--line); border-radius: 8px; overflow: hidden; box-shadow: var(--shadow); }
table.diag th, table.diag td { padding: 10px 12px; text-align: left; font-size: 13px;
    border-bottom: 1px solid var(--line); color: var(--text-card-strong); }
table.diag th { background: var(--surface-nested); font-weight: 700; cursor: pointer; user-select: none;
    color: var(--text-card-strong); position: sticky; top: 0; font-size: 12px;
    text-transform: uppercase; letter-spacing: 0.04em; }
table.diag th .arrow { opacity: .4; margin-left: 4px; font-size: 9px; }
table.diag th.sorted .arrow { opacity: 1; }
table.diag tbody tr { cursor: pointer; }
table.diag tbody tr:hover { background: rgba(42,124,160,.08); }
table.diag td.mono { font-family: ui-monospace, 'SF Mono', Menlo, monospace; font-size: 12px; color: var(--text-card); }

/* Map */
#map { height: calc(100vh - 200px); border-radius: 8px; border: 1px solid var(--line-on-page);
    box-shadow: var(--shadow); }
.leaflet-popup-content { font-family: inherit; min-width: 200px; }
.leaflet-popup-content .pop-name { font-weight: 700; color: var(--brand-brown); margin-bottom: 4px; }
.leaflet-popup-content .pop-row { display: flex; justify-content: space-between; font-size: 12px; padding: 2px 0;
    color: var(--text-card); }
.leaflet-popup-content a { color: var(--brand-blue); }

/* Drill modal */
#drill-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: none;
    align-items: center; justify-content: center; z-index: 1000; }
#drill-overlay.open { display: flex; }
#drill { background: var(--surface-card); color: var(--text-card); border-radius: 12px;
    padding: 22px 24px; width: 860px; max-width: 90vw; max-height: 88vh; overflow-y: auto;
    box-shadow: var(--shadow); }
#drill h3 { color: var(--text-card-strong); }
#drill .drill-close { float: right; cursor: pointer; color: var(--text-card-muted); font-size: 24px;
    line-height: 1; background: none; border: none; }
#drill .drill-meta { margin-top: 10px; }
#drill .drill-meta .mrow { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13px;
    border-bottom: 1px dotted var(--line); color: var(--text-card-strong); }
#drill .drill-meta .mrow .k { color: var(--text-card-muted); font-weight: 500; }
#drill .drill-meta .mrow .mono { font-family: ui-monospace, monospace; font-size: 12px; }
#drill .chart-area { margin-top: 16px; }
#drill .metric-chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 12px 0; }
#drill .chip { padding: 5px 12px; background: var(--surface-nested); color: var(--text-card);
    border: 1px solid var(--line); border-radius: 16px; font-size: 12px; cursor: pointer; font-weight: 600; }
#drill .chip:hover { background: rgba(42,124,160,.12); }
#drill .chip.active { background: var(--brand-blue); color: white; border-color: var(--brand-blue); }

/* Trend cards (per-metric pages) */
.trend-grid { display: grid; grid-template-columns: 1fr; gap: 14px; margin-bottom: 8px; }
@media (min-width: 1100px) { .trend-grid { grid-template-columns: 1fr 1fr; } }
.trend-card { background: var(--surface-card); color: var(--text-card-strong); border-radius: 8px;
    padding: 14px 16px 8px; border: 1px solid var(--line); box-shadow: var(--shadow); }
.trend-card-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 6px; }
.trend-title { font-size: 13px; font-weight: 700; color: var(--text-card-strong); }
.trend-meta  { font-size: 11px; color: var(--text-card-muted); }
.trend-chart { width: 100%; }

/* Range bar (24h / 3d / 7d) */
.range-bar { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; }
.range-btn { padding: 5px 14px; background: rgba(249,237,220,0.10); color: var(--text-body);
    border: 1px solid var(--line-on-page); border-radius: 16px; font-size: 12px; cursor: pointer;
    font-weight: 600; }
.range-btn:hover { background: rgba(249,237,220,0.20); }
.range-btn.active { background: var(--brand-blue); color: white; border-color: var(--brand-blue); }
.range-note { font-size: 11px; }

/* Misc */
.muted { color: var(--text-body-muted); font-size: 13px; }
.loading { padding: 30px; text-align: center; color: var(--text-body-muted); }
.empty { padding: 40px; text-align: center; color: var(--text-card-muted); background: var(--surface-card);
    border: 1px dashed var(--line); border-radius: 8px; }
