:root{
  --bg: #0b1220;
  --card: rgba(255,255,255,.06);
  --card2: rgba(255,255,255,.08);
  --text: rgba(255,255,255,.92);
  --muted: rgba(255,255,255,.70);
  --muted2: rgba(255,255,255,.55);
  --line: rgba(255,255,255,.12);
  --ok: #22c55e;
  --warn: #fbbf24;
  --bad: #fb7185;
  --accent: #60a5fa;
  --shadow: 0 10px 30px rgba(0,0,0,.35);
  --radius: 18px;

  --fs-0: clamp(14px, 1.1vw, 16px);
  --fs-1: clamp(16px, 1.4vw, 18px);
  --fs-2: clamp(18px, 1.8vw, 22px);
  --fs-3: clamp(24px, 2.8vw, 34px);
  --fs-4: clamp(34px, 4.5vw, 54px);
}

@media (prefers-color-scheme: light) {
  :root{
    --bg: #f6f7fb;
    --card: rgba(0,0,0,.04);
    --card2: rgba(0,0,0,.06);
    --text: rgba(0,0,0,.88);
    --muted: rgba(0,0,0,.62);
    --muted2: rgba(0,0,0,.46);
    --line: rgba(0,0,0,.10);
    --shadow: 0 10px 26px rgba(0,0,0,.12);
  }
}

*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, "Apple Color Emoji","Segoe UI Emoji";
  font-size: var(--fs-0);
  line-height: 1.4;
  background: radial-gradient(1200px 600px at 20% -10%, rgba(96,165,250,.35), transparent 55%),
              radial-gradient(1000px 600px at 110% 10%, rgba(74,222,128,.22), transparent 55%),
              radial-gradient(900px 600px at 50% 110%, rgba(251,191,36,.18), transparent 60%),
              var(--bg);
  color: var(--text);
}

.wrap{
  max-width: 1180px;
  margin: 0 auto;
  padding: 18px 14px 100px; /* més marge inferior per al menú */
}

header{
  display:flex;
  flex-wrap:wrap;
  gap:12px;
  align-items:flex-end;
  justify-content:space-between;
  margin-bottom: 14px;
}

.brand{
  display:flex;
  flex-direction:column;
  gap:6px;
  min-width: 260px;
}

.title{
  font-size: var(--fs-3);
  letter-spacing: -0.02em;
  margin:0;
}

.subtitle{
  margin:0;
  color: var(--muted);
  font-size: var(--fs-1);
}

.panel{
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
}

.controls{
  display:flex;
  flex-wrap:wrap;
  gap:10px;
  align-items:center;
  justify-content:flex-end;
  min-width: 320px;
}

label{
  display:flex;
  align-items:center;
  gap:8px;
  color: var(--muted);
  font-size: var(--fs-0);
}

input[type="text"], input[type="number"]{
  background: rgba(255,255,255,.08);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 12px;
  outline:none;
  min-width: 140px;
}
input[type="number"]{ min-width: 96px; width: 96px; }

@media (prefers-color-scheme: light) {
  input[type="text"], input[type="number"]{ background: rgba(0,0,0,.03); }
}

input:focus, button:focus{
  outline: 3px solid rgba(96,165,250,.35);
  outline-offset: 2px;
}

.btn{
  border: 1px solid var(--line);
  background: rgba(96,165,250,.18);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 12px;
  cursor:pointer;
  font-weight: 650;
}
.btn:hover{ background: rgba(96,165,250,.26); }

.btn.secondary{
  background: rgba(255,255,255,.06);
}

.tog{
  display:flex;
  align-items:center;
  gap:10px;
  user-select:none;
}

.pill{
  display:inline-flex;
  align-items:center;
  gap:8px;
  padding: 8px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card2);
  color: var(--muted);
  font-size: var(--fs-0);
  white-space: nowrap;
}

.status-row{
  display:flex;
  flex-wrap:wrap;
  gap:10px;
  align-items:center;
  justify-content:space-between;
  margin: 12px 0 14px;
}

.err{ color: var(--bad); font-weight: 700; }

.grid{
  display:grid;
  gap: 12px;
  grid-template-columns: repeat(12, 1fr);
  grid-auto-rows: minmax(110px, auto);
  grid-auto-flow: dense;
}

.card--wind{
  grid-row: span 1;
}

.card--wind .card-content{
  height: 100%;
  display:flex;
  flex-direction: column;
}

.card--wind .sub{
  flex: 1;
  display:flex;
  flex-direction: column;
  gap: 10px;
  align-items: stretch;
}

.wind-block{
  margin-top: 10px;
  width: 100%;
  flex: 1;
  display:flex;
  align-items:center;
  justify-content:center;
}

.wind-rose{
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  max-width: none;
}

.card{
  grid-column: span 6;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  position: relative;
  overflow:hidden;
  min-height: 110px;
}

@media (min-width: 700px){
  .card{ grid-column: span 4; }
}
@media (min-width: 980px){
  .card{ grid-column: span 3; }
}
/* Mòbil vertical: 1 sola columna de cards */
@media (max-width: 560px) and (orientation: portrait){
  .card{
    grid-column: span 12;
  }
}

.k{
  display:flex;
  align-items:baseline;
  justify-content:space-between;
  gap:10px;
  margin-bottom: 10px;
}
.k h3{
  margin:0;
  font-size: var(--fs-1);
  color: var(--muted);
  font-weight: 750;
}
.badge{
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--line);
  background: var(--card2);
  padding: 6px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.v{
  font-size: var(--fs-4);
  font-weight: 850;
  letter-spacing: -0.03em;
  margin: 0;
  display:flex;
  align-items:baseline;
  gap:8px;
  flex-wrap:wrap;
}

.unit{
  font-size: var(--fs-1);
  color: var(--muted);
  font-weight: 650;
}

.sub{
  margin-top: 6px;
  color: var(--muted2);
  font-size: var(--fs-0);
  display:flex;
  gap:10px;
  flex-wrap:wrap;
  align-items:center;
}

.sep{
  width: 100%;
  height: 1px;
  background: var(--line);
  margin: 6px 0;
}

.delta{ font-weight: 800; }
.delta.ok{ color: var(--ok); }
.delta.bad{ color: var(--bad); }

.dot{
  width: 10px; height: 10px; border-radius: 999px;
  background: var(--accent);
  display:inline-block;
  margin-right: 8px;
}

.section-title{
  margin: 18px 0 10px;
  display:flex;
  align-items:baseline;
  justify-content:space-between;
  gap:10px;
  flex-wrap:wrap;
}
.section-title h2{
  margin:0;
  font-size: var(--fs-2);
  letter-spacing: -0.02em;
}
.section-title p{
  margin:0;
  color: var(--muted);
}

details{
  margin-top: 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow:hidden;
}
summary{
  cursor:pointer;
  padding: 14px;
  font-weight: 800;
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  user-select:none;
}
summary::-webkit-details-marker{ display:none; }
.detail-body{ padding: 0 14px 14px; color: var(--muted); }

.table-wrap{
  overflow:auto;
  border-top: 1px solid var(--line);
}

table{
  border-collapse: collapse;
  width: 100%;
  min-width: 920px;
  background: transparent;
  font-size: var(--fs-0);
}
th, td{
  padding: 10px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}
th{
  position: sticky;
  top: 0;
  background: rgba(0,0,0,.12);
  color: var(--muted);
  font-weight: 850;
  z-index: 2;
}
@media (prefers-color-scheme: light){
  th{ background: rgba(0,0,0,.06); }
}

.smallnote{
  color: var(--muted2);
  margin-top: 10px;
}
:focus-visible{
  outline: 3px solid rgba(96,165,250,.55);
  outline-offset: 3px;
}

.na{
  color: var(--muted2);
  font-style: italic;
}


.tl-header{
  width: 100%;
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255,255,255,.55);
  border-bottom: 1px solid rgba(0,0,0,.08);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

@media (prefers-color-scheme: dark){
  .tl-header{
    background: rgba(10,14,20,.55);
    border-bottom: 1px solid rgba(255,255,255,.10);
  }
}

.tl-header__inner{
  width: 100%;
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 14px;
  padding: 12px 18px;
}

.tl-brand{
  display:flex;
  align-items:center;
  gap: 12px;
  text-decoration:none;
  color: var(--text);
  min-width: 0;
}

.tl-logoWrap{
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--card);
  display:inline-flex;
  align-items:center;
  justify-content:center;
  overflow:hidden;
  flex: 0 0 auto;
  position: relative;
}

.tl-logo{
  width: 44px;
  height: 44px;
  object-fit: cover;
  display:block;
}

.tl-logoFallback{
  display: none;
  width: 44px;
  height: 44px;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 14px;
  letter-spacing: .02em;
  color: var(--muted);
}

.tl-logoWrap.is-missing .tl-logoFallback{
  display: inline-flex;
}

.tl-brandtext{
  display:flex;
  flex-direction:column;
  gap: 2px;
  min-width: 0;
}

.tl-title{
  font-size: var(--fs-1);
  font-weight: 900;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.tl-subtitle{
  font-size: 12px;
  color: var(--muted);
  line-height: 1.1;
}

.tl-right{
  display:flex;
  align-items:center;
  gap: 10px;
}

.wrap{
  padding-top: 16px;
}

.cards-group{
  display: contents;
}
.muted{
  opacity: .80;
}
.sub { opacity: 1; }
.wind-meta { opacity: .90; }
.sep { opacity: .70; }

.temp-max { color: rgba(239,68,68,.95); font-weight: 800; }
.temp-min { color: rgba(59,130,246,.95); font-weight: 800; }

/* =========================
   MENÚ NAVEGACIÓ INFERIOR
   ========================= */

.bottom-nav{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 30;
  background: rgba(255,255,255,.75);
  border-top: 1px solid rgba(0,0,0,.08);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 8px 0;
  box-shadow: 0 -4px 20px rgba(0,0,0,.08);
}

@media (prefers-color-scheme: dark){
  .bottom-nav{
    background: rgba(10,14,20,.75);
    border-top: 1px solid rgba(255,255,255,.10);
    box-shadow: 0 -4px 20px rgba(0,0,0,.35);
  }
}

.bottom-nav__inner{
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-around;
  gap: 8px;
  padding: 0 12px;
}

.nav-btn{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px 16px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 12px;
  transition: all 0.2s ease;
  min-width: 70px;
  flex: 1;
  max-width: 120px;
}

.nav-btn:hover{
  background: rgba(96,165,250,.12);
  color: var(--text);
}

.nav-btn.active{
  background: rgba(96,165,250,.18);
  color: var(--accent);
}

.nav-btn__icon{
  font-size: 24px;
  line-height: 1;
}

.nav-btn__label{
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1;
}

/* Screens: per defecte amagats */
.screen{
  display: none;
}

.screen.active{
  display: block;
}

/* Ajust responsive per tablets/desktop */
@media (min-width: 768px){
  .nav-btn{
    min-width: 90px;
    padding: 10px 20px;
  }
  
  .nav-btn__icon{
    font-size: 26px;
  }
  
  .nav-btn__label{
    font-size: 12px;
  }
}
/* Mòbil vertical: 1 sola columna + millor centrament (excepte cards de vent) */
@media (max-width: 560px) and (orientation: portrait){
  .card{
    grid-column: span 12;
  }

  .card:not(.card--wind) .k{
    justify-content: center;
    text-align: center;
  }
  .card:not(.card--wind) .v{
    justify-content: center;
    text-align: center;
  }
  .card:not(.card--wind) .sub{
    justify-content: center;
    text-align: center;
  }
}
.card canvas { color: inherit; }
/* Meta compacta dins les cards (Pluja) */
.meta-row{
  display:flex;
  gap:8px;
  flex-wrap:wrap;
  align-items:baseline;
}

.dot-sep{
  opacity:.55;
}

/* Details “Més acumulats” en petit i discret */
.tl-details summary{
  cursor:pointer;
  opacity:.85;
}
.tl-details__body{
  margin-top:6px;
  opacity:.85;
  display:flex;
  gap:8px;
  flex-wrap:wrap;
}
/* Card vent: centrat com la resta (sense tocar les altres cards) */
.card--wind .k,
.card--wind .v,
.card--wind .sub{
  justify-content: center;
  text-align: center;
}
.card--wind .wind-meta{
  text-align: center;
}
/* Meteo header: "Dades actualitzades..." al costat del títol */
.section-title h2{
  display:flex;
  align-items:baseline;
  gap:10px;
  flex-wrap:wrap;
}

.last-inline{
  font-size: 0.9rem;
  font-weight: 600;
  opacity: .72;
}
/* Vent NO ha d'ocupar 2 files encara que existeixi card--tall o altres regles */
.card--wind{
  grid-row: span 1 !important;
}
.meteo-h2{
  display:flex;
  align-items:baseline;
  gap:10px;
  flex-wrap:wrap;
}

.meteo-last{
  font-weight: 600;   /* o 500 si vols encara més suau */
  opacity: .60;       /* més clar que el títol */
  font-size: 0.95rem; /* una mica més petit */
}
.card--wind .wind-block{
  display:flex;
  justify-content:center;
  margin-top: 6px;
  margin-bottom: 6px;
}

.card--wind .wind-rose{
  width: 80%;
  height: auto;
  max-width: 260px;  /* ajusta: 220-300 segons gust */
  display:block;
} 
/* Estat dades antigues (>24h) */
.is-stale {
  --accent: #f59e0b;
}

.is-stale .v,
.is-stale .unit {
  color: var(--accent);
}

.alert-stale {
  margin-top: 6px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(245,158,11,.15);
  border: 1px solid rgba(245,158,11,.35);
  color: #f59e0b;
  font-weight: 700;
  font-size: 0.9rem;
}
/* =========================
   HISTÒRICS - Selector de períodes
   ========================= */

.period-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 16px 0;
  padding: 0;
}

.period-btn {
  padding: 10px 20px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  border-radius: 12px;
  cursor: pointer;
  font-weight: 650;
  font-size: var(--fs-0);
  transition: all 0.2s ease;
}

.period-btn:hover {
  background: rgba(96,165,250,.12);
  border-color: var(--accent);
}

.period-btn.active {
  background: rgba(96,165,250,.18);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 750;
}

.custom-dates {
  display: none;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  padding: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  margin-bottom: 16px;
}

.custom-dates label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--muted);
}

.custom-dates input[type="date"] {
  background: rgba(255,255,255,.08);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  outline: none;
  min-width: 160px;
  font-size: var(--fs-0);
}

@media (prefers-color-scheme: light) {
  .custom-dates input[type="date"] {
    background: rgba(0,0,0,.03);
  }
}

.custom-dates input[type="date"]:focus {
  outline: 2px solid rgba(96,165,250,.35);
  outline-offset: 2px;
}

/* =========================
   HISTÒRICS - Estadístiques
   ========================= */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
  margin: 20px 0;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.stat-label {
  font-size: var(--fs-1);
  font-weight: 750;
  color: var(--muted);
  margin-bottom: 12px;
}

.stat-values {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat-values span {
  font-size: var(--fs-0);
  display: flex;
  align-items: center;
  gap: 6px;
}

.stat-max {
  color: rgba(239,68,68,.95);
  font-weight: 700;
}

.stat-min {
  color: rgba(59,130,246,.95);
  font-weight: 700;
}

.stat-avg {
  color: var(--text);
  font-weight: 600;
}

/* =========================
   HISTÒRICS - Gràfiques
   ========================= */

.charts-section {
  margin: 24px 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.charts-section h3 {
  margin: 0 0 16px 0;
  font-size: var(--fs-2);
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.02em;
}

.chart-container {
  position: relative;
  width: 100%;
  background: rgba(0,0,0,.02);
  border-radius: 12px;
  padding: 12px;
}

@media (prefers-color-scheme: dark) {
  .chart-container {
    background: rgba(255,255,255,.02);
  }
}

.chart-container canvas {
  display: block;
  max-width: 100%;
}

/* Responsive adjustments */
@media (max-width: 600px) {
  .period-btn {
    flex: 1 1 calc(50% - 8px);
    min-width: 0;
  }
  
  .custom-dates {
    flex-direction: column;
    align-items: stretch;
  }
  
  .custom-dates label {
    width: 100%;
  }
  
  .custom-dates input[type="date"] {
    width: 100%;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
}
/* ──────────────────────────────────────────────────────────
   Controls de període (Meteo/Cabals/Històrics)
────────────────────────────────────────────────────────── */
.period-row{
  display:flex;
  flex-wrap:wrap;
  gap:10px;
  align-items:flex-end;
  margin:10px 0 0;
}
.field{
  display:flex;
  flex-direction:column;
  gap:6px;
  min-width:160px;
}
.input{
  padding:8px 10px;
  border:1px solid rgba(255,255,255,.15);
  border-radius:10px;
  background:rgba(0,0,0,.18);
  color:inherit;
}
.btn{
  padding:8px 12px;
  border:1px solid rgba(255,255,255,.18);
  border-radius:12px;
  background:rgba(255,255,255,.10);
  color:inherit;
  cursor:pointer;
}
.btn--small{ padding:8px 12px; }
.is-hidden{ display:none !important; }

.err.warn{
  color:#ffb86b;
}

.card--stale{
  border-color: rgba(255, 184, 107, .6) !important;
  box-shadow: 0 0 0 1px rgba(255, 184, 107, .25) inset;
}

.stale-tag{
  display:inline-block;
  padding:2px 8px;
  border-radius:999px;
  font-size:12px;
  line-height:1.4;
  border:1px solid rgba(255,184,107,.55);
  color:#ffb86b;
}
.tl-modalOpen,
.tl-modalOpen body { overflow: hidden; }

.tl-chartModal {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
}
.tl-chartModal.is-open { display: block; }

.tl-chartModal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.90);
}

.tl-chartModal__panel {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.86);
}

.tl-chartModal__close {
  position: absolute;
  top: 10px;
  right: 10px;
  border: 0;
  background: rgba(255,255,255,0.12);
  color: #fff;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  font-size: 18px;
  cursor: pointer;
  z-index: 5;
}

/* content NO centra: serveix de “canvas holder” */
.tl-chartModal__content {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

/* stage sempre ancorat a dalt-esquerra */
.tl-chartModal__stage {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
}

/* canvas omple el stage */
.tl-chartModal__canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Rotació 90º ancorada a top-left (NO centrat) */
.tl-chartModal__panel.is-rot90 .tl-chartModal__stage {
  transform: rotate(90deg);
  transform-origin: top left;
}
