:root {
  --forest: #123b2a;
  --forest-2: #1d5a40;
  --leaf: #73a942;
  --lime: #cde77f;
  --cream: #f5f2e8;
  --paper: #fffdf7;
  --ink: #173126;
  --muted: #6f7e75;
  --line: #dce3dc;
  --water: #197a91;
  --danger: #b24b3f;
  --warning: #b87921;
  --shadow: 0 14px 40px rgba(18, 59, 42, .10);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--ink); background: var(--cream); min-height: 100vh; }
button, input, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { min-height: 1.3rem; color: var(--danger); font-size: .9rem; }
.eyebrow { margin: 0 0 .25rem; color: var(--leaf); font-weight: 800; letter-spacing: .14em; text-transform: uppercase; font-size: .7rem; }
.eyebrow.light { color: var(--lime); }

.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; background: radial-gradient(circle at 80% 10%, #2d684b, transparent 36%), var(--forest); }
.login-card { width: min(100%, 420px); background: var(--paper); border-radius: 28px; padding: 2rem; box-shadow: var(--shadow); }
.login-card h1 { font-family: Georgia, serif; font-size: 2.3rem; line-height: 1.02; margin: .5rem 0 1rem; }
.brand-mark { width: 52px; height: 52px; border-radius: 17px 17px 17px 5px; display: grid; place-items: center; background: var(--lime); color: var(--forest); font-family: Georgia, serif; font-size: 1.8rem; font-weight: 800; margin-bottom: 1.5rem; }
label { display: block; font-weight: 750; font-size: .84rem; margin: 1rem 0 .45rem; }
input, select { width: 100%; border: 1px solid var(--line); background: white; color: var(--ink); border-radius: 12px; padding: .8rem .9rem; outline: none; }
input:focus, select:focus { border-color: var(--leaf); box-shadow: 0 0 0 3px rgba(115, 169, 66, .18); }

.button { border: 0; border-radius: 14px; padding: .9rem 1rem; font-weight: 800; margin-top: 1rem; text-decoration: none; text-align: center; }
.button.primary { background: var(--forest); color: white; }
.button.water { background: var(--water); color: white; box-shadow: 0 10px 25px rgba(25, 122, 145, .18); }
.button.secondary { background: #e6efe9; color: var(--forest); }
.button.warning { background: #a64b32; color: white; }
.button:disabled { opacity: .55; cursor: wait; }
.full { width: 100%; }
.text-button { background: transparent; border: 0; color: var(--forest-2); font-weight: 800; text-decoration: none; }

.app-shell { max-width: 760px; min-height: 100vh; margin: auto; background: var(--cream); padding-bottom: calc(90px + env(safe-area-inset-bottom)); }
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 1.25rem 1.25rem .75rem; }
.topbar h1 { margin: 0; font-family: Georgia, serif; font-size: 2rem; }
.status-pill { border-radius: 999px; padding: .45rem .7rem; font-size: .75rem; font-weight: 800; }
.status-pill.ok { background: #dcebc4; color: #31551f; }
.status-pill.bad { background: #f3d7d3; color: #783229; }
.status-pill.neutral { background: #e7ebe8; color: #657168; }
main { padding: 0 1rem 1rem; }
.view { display: none; animation: enter .22s ease-out; }
.view.active { display: block; }
@keyframes enter { from { opacity: 0; transform: translateY(5px); } }

.hero-card { min-height: 150px; display: flex; align-items: center; justify-content: space-between; gap: 1rem; border-radius: 24px; padding: 1.4rem; color: white; background: linear-gradient(135deg, var(--forest), #2f6a4b); box-shadow: var(--shadow); overflow: hidden; }
.hero-card h2 { margin: .25rem 0; font-family: Georgia, serif; font-size: 1.8rem; }
.hero-meta { color: #d6e6dc; font-size: .82rem; }
.health-orb { width: 72px; height: 72px; flex: 0 0 72px; border-radius: 50%; background: var(--lime); box-shadow: 0 0 0 14px rgba(205, 231, 127, .12); }
.health-orb.bad { background: #efaa88; box-shadow: 0 0 0 14px rgba(239, 170, 136, .13); }

.section-heading { display: flex; justify-content: space-between; align-items: center; margin: 1.5rem .2rem .75rem; }
.section-heading.top { margin-top: .5rem; }
.section-heading h2, .section-title { margin: 0; font-family: Georgia, serif; font-size: 1.55rem; }
.section-title { margin: 1.5rem .2rem .75rem; }
.rack-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; }
.rack-card { position: relative; background: var(--paper); border: 1px solid rgba(220, 227, 220, .8); border-radius: 18px; padding: 1rem; box-shadow: 0 5px 18px rgba(18, 59, 42, .05); }
.rack-card h3 { margin: 0 0 .8rem; font-size: .95rem; }
.environment-rack-card { border-color: rgba(197, 155, 37, .35); }
.environment-rack-card .rack-values { justify-content: space-between; }
.rack-values { display: flex; gap: .9rem; }
.rack-value strong { display: block; font-size: 1.35rem; }
.rack-value span { color: var(--muted); font-size: .7rem; }
.health-dot { position: absolute; top: 1rem; right: 1rem; width: 9px; height: 9px; border-radius: 50%; background: var(--leaf); }
.health-dot.congelado { background: var(--warning); }
.health-dot.no_disponible { background: var(--danger); }
.freshness { margin: .7rem 0 0; color: var(--muted); font-size: .72rem; }

.metric-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: .75rem; }
.metric-card { background: var(--paper); border-radius: 16px; padding: 1rem; border: 1px solid var(--line); }
.metric-card span { display: block; color: var(--muted); font-size: .75rem; margin-bottom: .35rem; }
.metric-card strong { font-size: 1rem; }

.live-heading { align-items: flex-end; }
.live-updated { margin: .25rem 0 0; color: var(--muted); font-size: .72rem; }
.live-controls { display: flex; gap: .4rem; }
.live-controls select { width: auto; min-width: 76px; padding: .52rem .6rem; font-size: .75rem; }
.live-stack { display: grid; gap: .8rem; }
.live-card { background: var(--paper); border: 1px solid var(--line); border-radius: 20px; padding: .8rem; box-shadow: 0 5px 18px rgba(18, 59, 42, .05); }
.live-card h3 { margin: .1rem .25rem .55rem; font-family: Georgia, serif; font-size: 1rem; }
.live-card[data-live-type="tanque"] { border-color: rgba(25, 122, 145, .35); }
.live-legend { display: flex; flex-wrap: wrap; gap: .35rem .75rem; min-height: 22px; margin: 0 .25rem .45rem; color: var(--muted); font-size: .68rem; }
.live-legend span { display: inline-flex; align-items: center; gap: .3rem; }
.live-legend i { width: 9px; height: 3px; border-radius: 2px; }
.series-swatch.swatch-0 { background: #197a91; }
.series-swatch.swatch-1 { background: #d56a2c; }
.series-swatch.swatch-2 { background: #6a9f36; }
.series-swatch.swatch-3 { background: #7b61a8; }
.series-swatch.swatch-4 { background: #b24b3f; }
.series-swatch.swatch-5 { background: #c29b25; }
.series-swatch.swatch-6 { background: #386b55; }
.series-swatch.swatch-7 { background: #315d9a; }
.series-swatch.light-series.swatch-0 { background: #ffd000; }
.series-swatch.light-series.swatch-1 { background: #ff4fa3; }
.live-legend i.light-key { width: 11px; height: 8px; border: 1px solid; border-radius: 3px; }
.live-legend i.light-key.light-0 { background: #ffd00033; border-color: #ffd000; }
.live-legend i.light-key.light-1 { background: #ff4fa333; border-color: #ff4fa3; }
.live-legend i.pump-key { width: 11px; height: 8px; border-radius: 3px; background: #0097b247; }
.live-legend i.tank-fill-key { width: 2px; height: 10px; border-radius: 0; border-left: 2px dashed var(--water); }
.live-canvas-wrap { position: relative; width: 100%; height: 260px; }
.live-card canvas { display: block; width: 100%; height: 260px; }
.live-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: .82rem; text-align: center; pointer-events: none; }

.control-card, .chart-card { background: var(--paper); border: 1px solid var(--line); border-radius: 20px; padding: 1rem; box-shadow: var(--shadow); }
.chart-card { margin-top: 1rem; overflow: auto; min-height: 220px; display: grid; place-items: center; }
.chart-card img { width: 100%; min-width: 620px; display: block; }
.empty-state { color: var(--muted); text-align: center; }
.compact-select { width: auto; min-width: 84px; padding: .55rem .7rem; }
.topbar-actions { display: flex; align-items: center; gap: .65rem; }

.stack-list { display: grid; gap: .75rem; }
.irrigation-card { background: var(--paper); border-radius: 18px; border: 1px solid var(--line); overflow: hidden; }
.irrigation-card summary { list-style: none; padding: 1rem; cursor: pointer; display: flex; justify-content: space-between; gap: 1rem; }
.irrigation-card summary::-webkit-details-marker { display: none; }
.irrigation-card h3 { margin: 0 0 .25rem; font-size: .95rem; }
.irrigation-card .session-meta { color: var(--muted); font-size: .78rem; }
.irrigation-summary-actions { display: grid; justify-items: end; gap: .35rem; }
.response-count { color: var(--forest-2); font-weight: 850; white-space: nowrap; }
.details-cue { display: inline-flex; align-items: center; gap: .3rem; color: var(--muted); font-size: .7rem; font-weight: 800; white-space: nowrap; }
.details-cue i { font-size: .9rem; font-style: normal; transition: transform .2s; }
.details-hide { display: none; }
.irrigation-card[open] .details-show { display: none; }
.irrigation-card[open] .details-hide { display: inline; }
.irrigation-card[open] .details-cue i { transform: rotate(180deg); }
.result-list { border-top: 1px solid var(--line); padding: .55rem 1rem .9rem; }
.result-row { display: grid; grid-template-columns: 1fr auto auto; gap: .65rem; padding: .55rem 0; font-size: .82rem; border-bottom: 1px dashed var(--line); }
.result-row:last-child { border: 0; }
.delta.positive { color: #3a7625; font-weight: 800; }
.delta.negative { color: var(--danger); font-weight: 800; }

.timeline { position: relative; display: grid; gap: .65rem; }
.event-card { display: grid; grid-template-columns: 48px 1fr; gap: .75rem; background: var(--paper); border: 1px solid var(--line); border-radius: 16px; padding: .85rem; }
.event-time { color: var(--muted); font-size: .72rem; font-weight: 750; }
.event-summary { font-size: .86rem; font-weight: 700; }
.event-type { color: var(--muted); font-size: .7rem; margin-top: .3rem; }

.alarm-form { display: grid; gap: .8rem; }
.alarm-group h3 { margin: 0 0 .25rem; font-family: Georgia, serif; font-size: 1.1rem; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 .75rem; }
.form-grid label { min-width: 0; }
.form-grid input { margin-top: .45rem; }
.form-help { margin: .2rem .25rem 0; color: var(--muted); font-size: .78rem; line-height: 1.45; }
.tank-settings-card { margin-top: .8rem; }
.tank-settings-card h3 { margin: 0; font-family: Georgia, serif; }
.system-actions-card { margin-top: .8rem; }
.system-actions-card h3 { margin: 0; font-family: Georgia, serif; }
.system-actions-grid { display: grid; grid-template-columns: 1fr; gap: .7rem; }
.system-actions-grid .button { margin-top: .75rem; }
.settings-help-card { margin-top: .8rem; }
.settings-help-card h3 { margin: 0 0 .75rem; font-family: Georgia, serif; }
.settings-help-list { margin: 0; display: grid; gap: .75rem; }
.settings-help-list div { padding-bottom: .75rem; border-bottom: 1px dashed var(--line); }
.settings-help-list div:last-child { padding-bottom: 0; border-bottom: 0; }
.settings-help-list dt { font-size: .82rem; font-weight: 850; }
.settings-help-list dd { margin: .2rem 0 0; color: var(--muted); font-size: .78rem; line-height: 1.45; }

.active-alerts-card { margin: 0 0 .8rem; }
.active-alerts-card h3 { margin: 0; font-family: Georgia, serif; }
.active-alerts-list { display: grid; gap: .65rem; margin-top: 1rem; }
.active-alert { display: flex; align-items: center; justify-content: space-between; gap: .8rem; border: 1px solid var(--line); border-left: 4px solid var(--danger); border-radius: 14px; padding: .8rem; }
.active-alert.silenced { border-left-color: var(--muted); opacity: .78; }
.active-alert strong, .active-alert span, .active-alert em { display: block; }
.active-alert strong { font-size: .82rem; }
.active-alert span { margin-top: .25rem; color: var(--muted); font-size: .7rem; }
.active-alert em { margin-top: .25rem; color: var(--warning); font-size: .7rem; font-style: normal; font-weight: 800; }
.alert-toggle { flex: 0 0 auto; border: 1px solid var(--line); border-radius: 10px; padding: .55rem .65rem; background: white; color: var(--danger); font-size: .72rem; font-weight: 800; }
.alert-toggle.reactivate { color: var(--forest-2); }

.bottom-nav { position: fixed; z-index: 20; left: 50%; bottom: 0; transform: translateX(-50%); width: min(100%, 760px); display: grid; grid-template-columns: repeat(5, 1fr); background: rgba(255, 253, 247, .94); backdrop-filter: blur(18px); border-top: 1px solid var(--line); padding: .55rem .5rem calc(.55rem + env(safe-area-inset-bottom)); }
.nav-item { border: 0; background: transparent; color: var(--muted); font-size: .68rem; font-weight: 750; padding: .25rem; }
.nav-item span { display: block; font-size: 1.35rem; line-height: 1.2; }
.nav-item.active { color: var(--forest-2); }
.toast { position: fixed; z-index: 50; left: 50%; bottom: 95px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; max-width: calc(100% - 2rem); background: var(--ink); color: white; padding: .75rem 1rem; border-radius: 12px; transition: .2s; font-size: .85rem; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 640px) {
  main { padding-inline: 1.5rem; }
  .rack-grid { grid-template-columns: repeat(3, 1fr); }
  .metric-grid { grid-template-columns: repeat(4, 1fr); }
  .system-actions-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 430px) {
  .live-heading { align-items: flex-start; }
  .live-heading > div:first-child { max-width: 35%; }
  .live-controls { max-width: 65%; }
  .nav-item { font-size: .61rem; }
}
