:root {
    --bg: #f8f8f8;
    --bg-card: #f8f8f8;
    --text: #0b0b0b;
    --text-sec: #0b0b0b;
    --border: #555;
    --hist: #4a7ab5;
    --hist-fill: rgba(74, 122, 181, 0.15);
    --fore: #4a7ab5;
    --fore-fill: rgba(74, 122, 181, 0.15);
    --marker: #b55a5a;
    --grid: #000;
    --accent: #4a7ab5;
    --stat-bg: #f0f0f0;
    --precip: #4a7ab5;
    --precip-fill: rgba(74, 122, 181, 0.15);
    --diff-pos: #5a8a5a;
    --diff-pos-fill: rgba(90, 138, 90, 0.18);
    --diff-neg: #b55a5a;
    --diff-neg-fill: rgba(181, 90, 90, 0.18);
    --temp-high: #b55a5a;
    --temp-low: #4a7ab5;
    --humidity: #5a8a5a;
    --wind: #8a7a5a;
    --solar: #b5985a;
    --pressure: #7a5a8a;
    --status-good: #1a7f37; --status-good-track: #d5e8d9;
    --status-warn: #c28206; --status-warn-track: #f2e3c4;
    --status-bad: #96231f; --status-bad-track: #f1d6d3;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111;
        --bg-card: #1a1a1a;
        --text: #e5e5e5;
        --text-sec: #999;
        --border: #999;
        --hist: #7a9ec0;
        --hist-fill: rgba(122, 158, 192, 0.18);
        --fore: #7a9ec0;
        --fore-fill: rgba(122, 158, 192, 0.18);
        --marker: #c08080;
        --grid: #333;
        --accent: #7a9ec0;
        --stat-bg: #222;
        --precip: #7a9ec0;
        --precip-fill: rgba(122, 158, 192, 0.18);
        --diff-pos: #80b080;
        --diff-pos-fill: rgba(128, 176, 128, 0.18);
        --diff-neg: #c08080;
        --diff-neg-fill: rgba(192, 128, 128, 0.18);
        --temp-high: #c08080;
        --temp-low: #7a9ec0;
        --humidity: #80b080;
        --wind: #b0a080;
        --solar: #c0b080;
        --pressure: #a080b0;
        --status-good: #3f8f4a; --status-good-track: #223424;
        --status-warn: #b7952f; --status-warn-track: #362e14;
        --status-bad: #e0625a; --status-bad-track: #3c211e;
    }
}

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

body {
    font-family: Helvetica, Arial, sans-serif;
    background: var(--bg); color: var(--text);
    line-height: 1.6; min-height: 100vh;
}

.container { max-width: 75rem; margin: 0 auto; padding: 1rem; }

/* The device: one bezel containing masthead and panels */
.device {
    background: var(--bg-card); overflow: clip;
}

header {
    display: flex; align-items: flex-end; justify-content: space-between;
    padding: 0.6rem 1rem 0.35rem; flex-wrap: wrap; gap: 0.5rem;
    border-bottom: 2px solid var(--text);
}
header h1 { font-size: 2.2rem; font-weight: 700; letter-spacing: -0.03em; line-height: 1.1; }
.masthead-text { min-width: 0; display: flex; align-items: flex-end; gap: 1rem; }
.dek {
    color: var(--text-sec); font-size: 0.9rem; line-height: 1.45;
    max-width: 44ch;
}

.header-controls {
    display: flex; align-items: center; gap: 0.5rem;
    flex-wrap: wrap; flex: 1; justify-content: flex-end;
}

@media (max-width: 768px) {
    header { flex-direction: column; align-items: stretch; padding: 0.5rem; }
    header h1 { font-size: 1.5rem; }
    .masthead-text { display: block; }
    .map-section { padding: 0.5rem; }
    .header-controls { justify-content: flex-start; }
    input, select, textarea { font-size: 16px !important; }
    .container { padding: 0.5rem; }
    .chart-wrap { padding: 0 0.5rem; margin-bottom: 1.75rem; }
    .loc-header { padding: 0.5rem; flex-wrap: wrap; }
    .loc-header-text { width: 100%; }
    .status-callout { padding: 0.5rem; gap: 0.5rem; }
    .status-hero { font-size: 2.25rem; }
    .chart-title-row { flex-wrap: wrap; }
    .bucket-controls { width: 100%; justify-content: space-between; gap: 0.5rem; }
    .bucket-control { white-space: nowrap; }
}

/* Units chip: sits on the data card's top-right frame */
.unit-toggle {
    position: absolute; top: 0; right: 1rem; transform: translateY(-50%);
    display: flex; align-items: center;
    background: var(--bg-card);
    border: 1px solid var(--text); border-radius: 4px;
    overflow: hidden; z-index: 10;
}
.unit-toggle button {
    padding: 0.15rem 0.6rem; border: none; font-family: inherit;
    font-size: 0.9rem; cursor: pointer; font-weight: 400;
    background: transparent; color: var(--text);
    white-space: nowrap; line-height: 1.4;
}
.unit-toggle button + button { border-left: 1px solid var(--text); }
.unit-toggle button.active { background: var(--text); color: var(--bg); }
.unit-toggle button:focus-visible { outline: 2px dotted var(--text); outline-offset: -4px; }

.search-wrap { position: relative; margin-bottom: 0.5rem; }
.search-wrap input {
    width: 100%; padding: 0.4rem 1.6rem 0.4rem 1.8rem;
    border: 1px solid var(--border); border-radius: 0.5rem;
    font-family: inherit; font-size: 1rem;
    background: var(--bg-card); color: var(--text); outline: none;
}
.search-wrap input:focus { border-color: var(--text); }
.search-wrap input::placeholder { color: var(--text-sec); }
.search-icon {
    position: absolute; left: 0.5rem; top: 50%; transform: translateY(-50%);
    color: var(--text-sec); font-size: 1rem; pointer-events: none;
}
.search-clear {
    position: absolute; right: 0.4rem; top: 50%; transform: translateY(-50%);
    border: none; background: none; color: var(--text-sec);
    font-size: 1rem; cursor: pointer; padding: 0 0.2rem;
    display: none; line-height: 1;
}
.search-clear:hover { color: var(--text); }
.search-results {
    position: absolute; top: calc(100% + 2px); left: 0; right: 0;
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: 0.5rem; z-index: 1100; display: none;
    max-height: 260px; overflow-y: auto;
}
.search-results.visible { display: block; }
.search-result-item {
    padding: 0.5rem 0.65rem; cursor: pointer;
    border-bottom: 1px solid var(--border); font-size: 1rem;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--stat-bg); }
.search-result-item .name { font-weight: 700; }
.search-result-item .detail { color: var(--text-sec); font-size: 1rem; }

a {
    color: inherit; text-decoration: underline;
    text-decoration-thickness: 1px; text-underline-offset: 0.18em;
}
a:hover { background: var(--text); color: var(--bg); text-decoration: none; }
a:focus-visible { outline: 2px dotted var(--text); outline-offset: 2px; }

.nav-link { font-size: 1rem; }
.nav-sep { color: var(--border); font-size: 1rem; }

.reset-wrap { margin-top: 1.5rem; }
.reset-btn {
    padding: 0.4rem 0.8rem; border: 1px solid var(--border);
    border-radius: 0.4rem; background: none; color: var(--text-sec);
    font-family: inherit; font-size: 1rem; cursor: pointer;
}
.reset-btn:hover { color: var(--text); border-color: var(--text); }

.main { display: flex; flex-direction: column; }
.map-section { padding: 0.75rem; border-bottom: 1px solid var(--border); }
@media (min-width: 960px) {
    .main { flex-direction: row-reverse; align-items: flex-start; gap: 2rem; }
    .map-section { flex: 0 0 380px; border-bottom: none; }
    .data-section { flex: 1; min-width: 0; }
    .about-section { max-width: calc(100% - 380px); }
}

.card { position: relative; background: transparent; border: none; border-radius: 0; }
.map-card { height: 520px; overflow: hidden; border: 1px solid var(--text); }
.map-card #map { height: 100%; width: 100%; }
@media (max-width: 768px) {
    .map-card { height: 35vh; min-height: 200px; max-height: 300px; }
}

.data-section { display: flex; flex-direction: column; }
.data-section.hidden { display: none; }

.loc-header {
    padding: 0.75rem 1rem;
    display: flex; align-items: flex-start; justify-content: space-between; gap: 0.5rem;
}
.loc-header-text { min-width: 0; }
.loc-header-text h2 { font-size: 1rem; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.loc-header-text p { color: var(--text-sec); font-size: 1rem; }

.status-callout {
    padding: 0.75rem 1rem;
    display: flex; flex-direction: column; justify-content: center; gap: 0.6rem;
}
/* readout region: same offsets whether or not there is a status cell */
.loc-status-row, .card > .loc-header { margin-top: 1.25rem; margin-bottom: 1.5rem; }
.loc-status-row .loc-header { margin: 0; }
.status-callout:empty { display: none; }
@media (min-width: 960px) {
    .loc-status-row { display: flex; align-items: stretch; gap: 1rem; }
    .loc-status-row .status-callout { flex: 1.6 1 0; min-width: 0; order: -1; }
    .loc-status-row .loc-header { flex: 1 1 0; min-width: 0; }
}
.status-main { display: flex; align-items: center; gap: 1rem; }
.status-hero { font-size: 2.75rem; font-weight: 700; line-height: 1; letter-spacing: -0.02em; }
.status-text { flex: 1; min-width: 0; }
.status-headline { font-size: 1rem; font-weight: 700; margin-bottom: 0.15rem; }
.status-detail { font-size: 1rem; color: var(--text-sec); }
.status-meter { position: relative; height: 10px; }
.status-meter-fill { height: 100%; }
.status-meter-tick { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text); }
.status-callout.green .status-meter { background: var(--status-good-track); }
.status-callout.green .status-meter-fill { background: var(--status-good); }
.status-callout.yellow .status-meter { background: var(--status-warn-track); }
.status-callout.yellow .status-meter-fill { background: var(--status-warn); }
.status-callout.red .status-meter { background: var(--status-bad-track); }
.status-callout.red .status-meter-fill { background: var(--status-bad); }

.chart-wrap { padding: 0 1rem; margin-bottom: 2.25rem; }
#chart-content > .chart-wrap:first-child { padding-top: 1rem; }
#chart-content > .chart-wrap:last-child { margin-bottom: 1rem; }
.chart-wrap canvas { display: block; width: 100%; height: auto; }
.chart-title { font-size: 1rem; font-weight: 600; margin-bottom: 0.15rem; color: var(--text-sec); }
.chart-title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.15rem; }
.chart-title-row .chart-title { margin-bottom: 0; }

.bucket-controls { display: flex; gap: 1.1rem; flex-wrap: wrap; align-items: center; }
.soil-select {
    padding: 0.25rem 0.3rem; border: 1px solid var(--border);
    border-radius: 0.4rem; font-family: inherit; font-size: 1rem;
    background: var(--bg); color: var(--text); cursor: pointer;
}
.bucket-control {
    font-size: 1rem; color: var(--text-sec);
    display: flex; align-items: center; gap: 0.2rem;
}
.bucket-control input {
    width: 3.6em; padding: 0.25rem 0.3rem; border: 1px solid var(--border);
    border-radius: 0.4rem; font-family: inherit; font-size: 1rem;
    text-align: center; background: var(--bg); color: var(--text);
}

/* Small screens / large text: stacked control grid — select gets its own
   row, Roots and ET share the second. Survives any text scale. */
@media (max-width: 520px) {
    .bucket-controls {
        display: grid; grid-template-columns: auto auto;
        justify-content: space-between; gap: 0.4rem 1rem;
    }
    .bucket-controls .soil-select { grid-column: 1 / -1; width: 100%; }
}

.irrigation-hint { font-size: 0.85rem; color: var(--text-sec); text-align: center; padding: 0.25rem 0 0; cursor: pointer; }
.irrigation-hint.has-events { color: #3a7ca5; font-weight: 600; }


.about-section {
    padding: 1rem 1rem 1.5rem;
    border-top: 1px solid var(--border); font-size: 1rem;
    line-height: 1.7; color: var(--text);
}
.about-section h2 { font-size: 1rem; font-weight: 700; margin-bottom: 0.75rem; }
.about-section h3 { font-size: 1rem; font-weight: 600; margin: 1.25rem 0 0.4rem; }
.about-section p { margin-bottom: 0.7rem; }

.loading-inline { display: none; padding: 3rem; text-align: center; }
.loading-inline.visible { display: block; }
.spinner {
    width: 36px; height: 36px;
    border: 3px solid transparent;
    border-top-color: var(--text); border-right-color: var(--text);
    border-radius: 50%;
    animation: spin 0.8s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
