:root {
  color-scheme: dark;
  --bg: #111418;
  --surface: #1a1f26;
  --surface-2: #20262f;
  --input: #11161d;
  --text: #edf0f5;
  --title: #f5f7fa;
  --muted: #aeb7c4;
  --line: #343b45;
  --input-line: #3b4451;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --focus: #75a7ff;
  --danger: #ff8b82;
  font: 16px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f3f7;
  --input: #ffffff;
  --text: #172033;
  --title: #172033;
  --muted: #667085;
  --line: #dfe3e8;
  --input-line: #cbd2dc;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --focus: #1d4ed8;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); }
button, .file-button, input, select, textarea { font: inherit; }
button, .file-button {
  min-height: 2.75rem;
  border: 1px solid var(--line);
  border-radius: .62rem;
  padding: .62rem .9rem;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: .87rem;
  font-weight: 750;
  line-height: 1.2;
  text-align: center;
}
button:hover, .file-button:hover { border-color: #526071; }
button:focus-visible, .file-button:focus-within, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
button:disabled { cursor: not-allowed; opacity: .46; }
.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.secondary { background: var(--surface); }
.icon-button { width: 2.75rem; padding: 0; font-size: 1.18rem; }

.topbar {
  min-height: 4.25rem;
  width: 100%;
  margin: auto;
  padding-inline: max(1rem, calc((100% - 1120px) / 2));
  display: flex;
  align-items: center;
  gap: 1.25rem;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 4;
  border-bottom: 1px solid var(--line);
}
body::before { content: none; }
.brand { z-index: 2; color: inherit; text-decoration: none; display: grid; grid-template-columns: auto auto; gap: 0 .45rem; line-height: 1.05; white-space: nowrap; }
.brand span { grid-row: span 2; color: var(--accent); font-size: 1.65rem; }
.brand small { font-size: .72rem; color: var(--muted); }
nav { z-index: 2; display: flex; gap: .2rem; flex: 1; }
nav button { min-height: 2.25rem; border: 0; padding: .45rem .7rem; background: transparent; color: var(--muted); }
nav button.active { color: var(--title); background: var(--surface); }
.top-actions, .actions { display: flex; align-items: center; gap: .5rem; }
.top-actions { z-index: 2; margin-left: auto; }
.skip-link { position: absolute; left: -9999px; }
.skip-link:focus { left: .75rem; top: .75rem; z-index: 5; background: var(--accent); color: #fff; padding: .5rem; border-radius: .4rem; }

main { width: min(1120px, calc(100% - 2rem)); margin: auto; padding: 2rem 0 4rem; }
.route-stage { min-height: 420px; transition: opacity 180ms ease, transform 180ms ease; }
.route-stage.route-leave { opacity: 0; transform: translateY(.6rem); pointer-events: none; }
.route-stage.route-enter { animation: route-enter 240ms ease both; }
@keyframes route-enter { from { opacity: 0; transform: translateY(-.45rem); } to { opacity: 1; transform: translateY(0); } }

.route-page { position: relative; isolation: isolate; --stage-background: url("public/assets/backgrounds/montanas-serenas.webp"); }
.route-page::before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 2.5rem -1.4rem -1.4rem;
  pointer-events: none;
  opacity: .74;
  background-image: linear-gradient(105deg, var(--bg) 0%, color-mix(in srgb, var(--bg) 88%, transparent) 57%, color-mix(in srgb, var(--bg) 67%, transparent) 100%), var(--stage-background);
  background-size: cover;
  background-position: center;
  border-radius: 1rem;
}
.route-page[data-stage="enraizado"] { --stage-background: url("public/assets/backgrounds/montanas-serenas.webp"); }
.route-page[data-stage="vegetativo-temprano"] { --stage-background: url("public/assets/backgrounds/sombras-botanicas.webp"); }
.route-page[data-stage="vegetativo-tardio"] { --stage-background: url("public/assets/backgrounds/bosque-en-niebla.webp"); }
.route-page[data-stage="preflora"] { --stage-background: url("public/assets/backgrounds/oceano-profundo.webp"); }
.route-page[data-stage="flora-estiramiento"] { --stage-background: url("public/assets/backgrounds/dunas-doradas.webp"); }
.route-page[data-stage="flora-engorde"] { --stage-background: url("public/assets/backgrounds/oceano-profundo.webp"); }
.route-page[data-stage="maduracion"] { --stage-background: url("public/assets/backgrounds/cielo-nocturno.webp"); }
.route-page[data-stage="lavado"] { --stage-background: url("public/assets/backgrounds/cielo-nocturno.webp"); }
.route-page[data-stage="cosecha"] { --stage-background: url("public/assets/backgrounds/cielo-nocturno.webp"); }

.page-heading, .welcome { display: flex; justify-content: space-between; align-items: start; gap: 1.5rem; margin-bottom: 1.75rem; }
.page-heading h1, .welcome h1 { margin: .1rem 0; color: var(--title); font-size: clamp(1.75rem, 4vw, 2.5rem); letter-spacing: -.035em; }
.welcome { min-height: 65vh; align-items: center; }
.welcome > div { max-width: 40rem; }
.welcome p { max-width: 37rem; font-size: 1.05rem; color: var(--muted); }
.welcome h1 em, .eyebrow { color: var(--accent); }
.welcome h1 em { font-style: normal; }
.welcome-card, .card { background: var(--surface); border: 1px solid var(--line); border-radius: .9rem; }
.welcome-card { min-width: 19rem; padding: 1.5rem; }
.welcome-card h2 { margin: .3rem 0; color: var(--title); }
.metric-row { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: 1.5rem; }
.metric-row span { background: var(--surface-2); padding: .6rem; border-radius: .62rem; font-weight: 750; }
.eyebrow { margin: 0; text-transform: uppercase; font-size: .72rem; letter-spacing: .09em; font-weight: 750; }
.page-heading p:not(.eyebrow) { margin: .25rem 0; color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 1rem; }
.card { padding: 1.15rem; }
.span-8 { grid-column: span 8; }
.span-4 { grid-column: span 4; }
.span-12 { grid-column: span 12; }
.card h2, .card h3 { margin: 0 0 .75rem; color: var(--title); font-size: 1.05rem; }
.card h3 { font-size: .95rem; }
.dwc-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; }
.dwc { padding: .95rem; background: var(--surface-2); border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: .75rem; }
.dwc header, .list-row { display: flex; justify-content: space-between; gap: .75rem; align-items: center; }
.dwc strong { color: var(--title); font-size: 1rem; }
.muted, small { color: var(--muted); font-size: .82rem; }
.numbers { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .5rem; margin: .75rem 0; }
.numbers div { min-width: 0; padding: .55rem; background: var(--input); border: 1px solid transparent; border-radius: .55rem; }
.numbers b { display: block; color: var(--title); font-size: 1rem; overflow-wrap: anywhere; }
.numbers small { display: block; }
.dwc-actions { margin-top: .75rem; flex-wrap: wrap; }
.task, .alert, .list-row { padding: .7rem 0; border-top: 1px solid var(--line); }
.task:first-child, .alert:first-child, .list-row:first-child { border-top: 0; }
.task label { display: flex; gap: .5rem; align-items: start; }
.task input { margin-top: .25rem; width: 1.1rem; height: 1.1rem; accent-color: var(--accent); }
.badge { display: inline-block; border-radius: 999px; padding: .2rem .45rem; background: var(--surface-2); font-size: .72rem; }
.badge.alerta { color: var(--danger); }
.badge.aviso { color: #e2b65c; }
.week-strip { display: flex; gap: .28rem; flex-wrap: wrap; }
.week-strip button { width: 2.25rem; min-height: 2.25rem; padding: .35rem; border-radius: .5rem; }
.week-strip button.calendar-reached:not(.active) { border-color: #586578; }
.week-strip button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.stage-notice { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: .75rem; margin-top: .85rem; padding: .75rem; border: 1px solid #43516a; border-radius: .65rem; background: rgb(29 78 216 / .09); font-size: .87rem; }
.stage-notice.quiet { grid-template-columns: auto 1fr; background: var(--input); border-color: var(--line); }
.chart { width: 100%; height: 10rem; display: block; }
.connector-status { min-height: 2.6rem; margin: .75rem 0; color: var(--muted); font-size: .82rem; }
.connector-status.connected { color: #70d6a5; }
.connector-status.error { color: var(--danger); }
.connector-status.connecting { color: #9fb8f8; }
.modal-note { color: var(--muted); font-size: .87rem; }
.template-grid { display: grid; gap: .75rem; margin-top: 1rem; }
.template-card { padding: 1rem; border: 1px solid var(--line); border-radius: .75rem; background: var(--surface-2); }
.template-card h3 { margin: .2rem 0 .35rem; color: var(--title); font-size: 1rem; }
.template-card > p:not(.eyebrow) { margin: 0; color: var(--muted); font-size: .87rem; }
.template-facts { display: flex; flex-wrap: wrap; gap: .35rem .75rem; margin: .75rem 0; padding-left: 1.05rem; color: var(--text); font-size: .82rem; }
.template-card-actions { flex-wrap: wrap; }
.template-warning { margin: .85rem 0 0; padding: .65rem .7rem; border-left: 3px solid var(--accent); border-radius: .35rem; background: rgb(29 78 216 / .09); color: var(--text); font-size: .82rem; }
.prompt-field { display: grid; gap: .35rem; color: var(--muted); font-size: .82rem; margin-top: 1rem; }
.prompt-field textarea { min-height: 18rem; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .76rem; line-height: 1.45; }
.sync-summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .5rem; margin: 1rem 0; }
.sync-summary span { padding: .6rem; border: 1px solid var(--line); border-radius: .55rem; background: var(--input); color: var(--muted); font-size: .8rem; }
.sync-summary b { display: block; color: var(--title); font-size: 1.1rem; }
.sync-conflict { margin: .75rem 0; padding: .7rem; border: 1px solid var(--line); border-radius: .6rem; }
.sync-conflict legend { padding: 0 .3rem; color: var(--title); font-weight: 700; font-size: .88rem; }
.sync-conflict label { display: block; margin-top: .4rem; color: var(--muted); }
.sync-conflict input { min-height: auto; }
code { padding: .1rem .25rem; border-radius: .25rem; background: var(--input); color: var(--text); overflow-wrap: anywhere; }
.empty { color: var(--muted); padding: 1.25rem 0; }
.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; }
.settings-grid label, .form-grid label { display: grid; gap: .35rem; color: var(--muted); font-size: .82rem; }
.settings-grid input, .form-grid input, .form-grid textarea, .form-grid select {
  min-width: 0; min-height: 2.75rem; padding: .62rem .7rem; border-radius: .62rem; border: 1px solid var(--input-line); background: var(--input); color: var(--text);
}
textarea { resize: vertical; }
dialog { max-width: 37.5rem; width: calc(100% - 2rem); padding: 1rem; background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: .9rem; }
dialog::backdrop { background: rgb(0 0 0 / .65); }
dialog form { margin: 0; }
dialog header, dialog footer { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
dialog header { border-bottom: 1px solid var(--line); padding-bottom: .75rem; }
dialog header h2 { margin: 0; color: var(--title); font-size: 1.2rem; }
dialog footer { border-top: 1px solid var(--line); padding-top: .9rem; margin-top: 1rem; justify-content: end; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; margin-top: 1rem; }
.form-grid .full { grid-column: span 2; }
.file-button input { display: none; }
#toast { position: fixed; z-index: 5; right: 1rem; bottom: 1rem; max-width: min(22.5rem, calc(100% - 2rem)); background: var(--title); color: var(--bg); padding: .75rem 1rem; border-radius: .62rem; opacity: 0; transform: translateY(.6rem); transition: .2s; pointer-events: none; }
#toast.show { opacity: 1; transform: none; }

@media (max-width: 760px) {
  main { width: min(100% - 1rem, 1120px); }
  .topbar { min-height: auto; padding: .35rem .5rem; gap: .25rem .5rem; flex-wrap: wrap; }
  main { padding-top: 1.25rem; }
  nav { order: 3; flex: 0 0 100%; width: 100%; overflow: hidden; padding-bottom: .1rem; }
  nav button { flex: 1 1 0; min-width: 0; padding: .45rem .35rem; font-size: .78rem; white-space: nowrap; }
  .page-heading, .welcome { display: block; }
  .route-page::before { inset: 2.5rem 0 -1rem; }
  .page-actions { margin-top: 1rem; }
  .page-actions > button, .page-actions > .file-button { flex: 1 1 10rem; }
  .welcome-card { min-width: 0; margin-top: 1.5rem; }
  .grid, .dwc-grid { display: block; }
  .card { margin-bottom: .75rem; padding: 1rem; }
  .span-8, .span-4 { grid-column: span 12; }
  .settings-grid, .form-grid { grid-template-columns: 1fr; }
  .form-grid .full { grid-column: span 1; }
  .actions:not(.top-actions) { flex-wrap: wrap; }
  .actions:not(.top-actions) > button, .actions:not(.top-actions) > .file-button { flex: 1 1 8.5rem; }
  .dwc-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sync-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dwc-actions > button { width: 100%; }
  .stage-notice { grid-template-columns: 1fr; align-items: start; }
  .stage-notice button { width: 100%; }
}

@media (max-width: 390px) {
  .brand small { display: none; }
  .topbar { padding-block: .25rem; }
  nav button { min-height: 2.1rem; padding: .35rem .2rem; font-size: .73rem; }
  .numbers { gap: .35rem; }
  .numbers div { padding: .45rem; }
  .numbers b { font-size: .94rem; }
  .dwc-actions { grid-template-columns: 1fr; }
  dialog { width: calc(100% - 1rem); padding: .8rem; }
  dialog footer { display: grid; grid-template-columns: 1fr; }
  dialog footer button { width: 100%; }
}

@media (max-width: 340px) {
  nav { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .25rem; }
  nav button { width: 100%; padding: .45rem .3rem; font-size: .8rem; }
  .numbers { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .page-actions { display: grid; grid-template-columns: 1fr; }
  .page-actions > button, .page-actions > .file-button { width: 100%; }
}

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; } }
