:root {
  --ink: #1f3a2e;          /* musgo oscuro */
  --ink-2: #4d5f52;
  --fern: #5e7a45;
  --bark: #4a3a2c;
  --mist: #eef1ec;
  --paper: #ffffff;
  --line: #d3dbd2;
  --focus: #2f6fb3;
  --shadow: 0 1px 2px rgba(31, 58, 46, .18), 0 8px 24px rgba(31, 58, 46, .14);
  --l1: #f1e2a2; --l2: #e6ad3e; --l3: #c4651b; --l4: #7f2f0e; --mapbg: #e8ece6;
  --font: "Schibsted Grotesk", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink: #e4ece3; --ink-2: #a9b8ab; --fern: #9cbb7f; --bark: #d4bfa8;
    --mist: #18221c; --paper: #202c25; --line: #34443a; --focus: #8bbcf0;
    --shadow: 0 1px 2px rgba(0,0,0,.5), 0 8px 24px rgba(0,0,0,.4);
    --l1: #6a4a28; --l2: #b8742b; --l3: #f0a23c; --l4: #ffd27a; --mapbg: #141b17;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --ink: #e4ece3; --ink-2: #a9b8ab; --fern: #9cbb7f; --bark: #d4bfa8;
  --mist: #18221c; --paper: #202c25; --line: #34443a; --focus: #8bbcf0;
  --shadow: 0 1px 2px rgba(0,0,0,.5), 0 8px 24px rgba(0,0,0,.4);
  --l1: #6a4a28; --l2: #b8742b; --l3: #f0a23c; --l4: #ffd27a; --mapbg: #141b17;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); color: var(--ink); background: var(--mist); font-size: 15px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
#map { position: fixed; inset: 0; background: var(--mapbg); }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
a { color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* Panel */
#panel {
  position: fixed; top: 16px; left: 16px; bottom: 16px; width: 356px; z-index: 5;
  background: var(--paper); border-radius: 14px; box-shadow: var(--shadow);
  overflow-y: auto; overscroll-behavior: contain; padding: 20px 20px 16px;
}
.sheet-handle { display: none; }
.brand-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand h1 { margin: 0; font-size: 30px; line-height: 1; font-weight: 800; letter-spacing: -0.03em; }
.brand p { margin: 8px 0 0; color: var(--ink-2); font-size: 14px; max-width: 30ch; }
.block { margin-top: 22px; }
.block-title { margin: 0 0 10px; font-size: 15px; font-weight: 700; }
.note { margin: 8px 0 0; color: var(--ink-2); font-size: 13px; }

/* Especies */
.species { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.sp {
  min-width: 0; appearance: none; text-align: left; font: inherit; color: var(--ink); cursor: pointer;
  border: 1.5px solid var(--line); background: transparent; border-radius: 10px; padding: 9px 11px;
}
.sp b { display: block; font-size: 16px; font-weight: 700; }
.sp i { display: block; font-size: 12px; color: var(--ink-2); font-style: italic; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sp[aria-checked="true"] { border-color: var(--ink); background: var(--ink); color: var(--paper); }
.sp[aria-checked="true"] i { color: var(--paper); opacity: .8; }

/* Semana: la pieza protagonista */
.week { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 4px; align-items: end; }
.day {
  appearance: none; font: inherit; color: var(--ink); cursor: pointer; background: none; border: 0; padding: 0;
  display: grid; grid-template-rows: 84px auto auto; justify-items: center; gap: 4px; border-radius: 8px;
}
.day .bar { width: 100%; height: 84px; display: flex; align-items: flex-end; border-radius: 6px; background: var(--mist); overflow: hidden; }
.day .bar span { display: block; width: 100%; background: var(--l3); border-radius: 6px 6px 0 0; transition: height .35s ease; }
.day.forecast .bar span { background: repeating-linear-gradient(135deg, var(--l3) 0 5px, var(--l2) 5px 9px); }
.day .dname { font-size: 12px; font-weight: 600; text-transform: lowercase; }
.day .dnum { font-size: 12px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.day[aria-checked="true"] .bar { outline: 2px solid var(--ink); outline-offset: 1px; }
.day[aria-checked="true"] .dname { text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 2px; }

/* Leyenda */
.legend { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px 12px; }
.legend li { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.legend i { width: 18px; height: 18px; border-radius: 4px; flex: none; border: 1px solid rgba(0,0,0,.12); }

/* Comarcas */
.comarques { list-style: none; margin: 0; padding: 0; }
.comarques li { display: grid; grid-template-columns: 1fr 88px; align-items: center; gap: 10px; padding: 6px 0; border-top: 1px solid var(--line); }
.comarques li:first-child { border-top: 0; }
.comarques button { appearance: none; border: 0; background: none; font: inherit; color: inherit; text-align: left; padding: 0; cursor: pointer; font-weight: 600; }
.comarques button:hover { text-decoration: underline; }
.comarques .lv { font-size: 12px; color: var(--ink-2); display: block; font-weight: 400; }
.comarques .meter { height: 8px; background: var(--mist); border-radius: 4px; overflow: hidden; }
.comarques .meter span { display: block; height: 100%; background: var(--l3); }

.warn .warnings { margin: 0; padding-left: 18px; font-size: 14px; }
.warn .warnings li + li { margin-top: 6px; }
.method summary { cursor: pointer; font-weight: 700; }
.method p { font-size: 14px; margin: 8px 0 0; }
.credits { margin-top: 22px; font-size: 12px; color: var(--ink-2); }
.credits p { margin: 6px 0 0; }
.status { color: var(--ink); font-weight: 600; }

/* Ficha de celda */
.cell-card {
  position: fixed; right: 16px; top: 16px; width: 330px; max-height: calc(100% - 32px); overflow-y: auto; z-index: 6;
  background: var(--paper); border-radius: 14px; box-shadow: var(--shadow); padding: 18px 18px 16px;
}
.cell-card .close { position: absolute; right: 8px; top: 6px; appearance: none; border: 0; background: none; font-size: 26px; line-height: 1; color: var(--ink-2); cursor: pointer; padding: 4px 8px; border-radius: 8px; }
.cc-level { display: inline-flex; align-items: center; gap: 8px; font-size: 22px; font-weight: 800; letter-spacing: -0.02em; margin: 0; }
.cc-level i { width: 20px; height: 20px; border-radius: 5px; border: 1px solid rgba(0,0,0,.12); }
.cc-place { margin: 4px 0 0; color: var(--ink-2); font-size: 14px; }
.cc-why { margin: 12px 0 0; font-size: 15px; }
.cc-rows { margin: 14px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 14px; }
.cc-rows dt { color: var(--ink-2); }
.cc-rows dd { margin: 0; font-variant-numeric: tabular-nums; }
.factors { margin-top: 14px; display: grid; gap: 8px; }
.factor { display: grid; grid-template-columns: 74px 1fr 34px; align-items: center; gap: 8px; font-size: 13px; }
.factor .meter { height: 8px; border-radius: 4px; background: var(--mist); overflow: hidden; }
.factor .meter span { display: block; height: 100%; background: var(--fern); }
.factor b { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }

.maplibregl-ctrl-attrib { font-family: var(--font); }

@media (max-width: 760px) {
  #panel {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; max-height: 72vh; border-radius: 16px 16px 0 0;
    padding: 6px 16px calc(14px + env(safe-area-inset-bottom));
  }
  .sheet-handle {
    display: flex; flex-direction: column; align-items: center; gap: 2px; width: 100%;
    appearance: none; border: 0; background: none; color: var(--ink-2); font: inherit; font-size: 12px; padding: 4px 0 6px; cursor: pointer;
  }
  .sheet-handle span { width: 40px; height: 4px; border-radius: 2px; background: var(--line); }
  .brand { display: block; }
  .brand h1 { font-size: 22px; }
  .brand p { display: none; }
  .block { margin-top: 12px; }
  .species { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .sp { padding: 7px 6px; }
  .sp b { font-size: 14px; }
  .sp i { display: none; }
  .day { grid-template-rows: 48px auto auto; }
  .day .bar { height: 48px; }
  #week-note { display: none; }
  .panel-body { display: none; }
  #panel.open .panel-body { display: block; }
  .cell-card { left: 12px; right: 12px; top: 12px; width: auto; max-height: 50vh; }
}
@media (prefers-reduced-motion: reduce) { .day .bar span { transition: none; } }

.legend i.none, .cc-level i.none { background: transparent; border-style: dashed; border-color: var(--ink-2); }

/* Botón de icono y ajustes */
.icon-btn {
  appearance: none; border: 0; background: transparent; color: var(--ink-2); cursor: pointer;
  width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; font-size: 26px; line-height: 1; flex: none;
}
.icon-btn:hover { background: var(--mist); color: var(--ink); }
.settings { position: fixed; inset: 0; z-index: 20; background: rgba(10, 20, 14, .45); display: grid; place-items: center; padding: 16px; }
.settings-card { width: min(420px, 100%); background: var(--paper); color: var(--ink); border-radius: 16px; box-shadow: var(--shadow); padding: 18px 20px 20px; }
.settings-head { display: flex; align-items: center; justify-content: space-between; }
.settings-head h2 { margin: 0; font-size: 20px; font-weight: 800; letter-spacing: -0.02em; }
.settings fieldset { border: 0; margin: 16px 0 0; padding: 0; }
.settings legend { font-weight: 700; font-size: 15px; padding: 0; margin-bottom: 8px; }
.choice { display: grid; gap: 6px; }
.choice label { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 10px; cursor: pointer; }
.choice input { accent-color: var(--ink); width: 18px; height: 18px; margin: 0; }
.choice label:has(input:checked) { border-color: var(--ink); }

/* Controles del mapa en modo oscuro */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .maplibregl-ctrl-group, :root:not([data-theme="light"]) .maplibregl-ctrl-attrib { background: var(--paper); color: var(--ink); }
  :root:not([data-theme="light"]) .maplibregl-ctrl-group button + button { border-top-color: var(--line); }
  :root:not([data-theme="light"]) .maplibregl-ctrl-icon, :root:not([data-theme="light"]) .maplibregl-ctrl-attrib-button { filter: invert(1); }
  :root:not([data-theme="light"]) .maplibregl-ctrl-attrib a { color: var(--ink); }
}
:root[data-theme="dark"] .maplibregl-ctrl-group, :root[data-theme="dark"] .maplibregl-ctrl-attrib { background: var(--paper); color: var(--ink); }
:root[data-theme="dark"] .maplibregl-ctrl-group button + button { border-top-color: var(--line); }
:root[data-theme="dark"] .maplibregl-ctrl-icon, :root[data-theme="dark"] .maplibregl-ctrl-attrib-button { filter: invert(1); }
:root[data-theme="dark"] .maplibregl-ctrl-attrib a { color: var(--ink); }
