/* ============================================================
   Madrid Ayuntamiento · Contaminación Acústica
   Design tokens inspired by the Ayuntamiento de Madrid brand.
   WCAG 2.1 AA compliant colour contrasts throughout.
   ============================================================ */

/* ── Tokens ──────────────────────────────────────────────────── */
:root {
	--color-primary: #003082; /* Azul corporativo Madrid */
	--color-primary-dark: #001f5c;
	--color-primary-light: #e8eef7;
	--color-accent: #c8102e; /* Rojo escudo */
	--color-text: #1a1a1a;
	--color-text-muted: #555555;
	--color-bg: #f5f6f8;
	--color-surface: #ffffff;
	--color-border: #d0d5dd;

	--color-low: #2e7d32; /* < 55 dB */
	--color-mid: #ca8a04; /* 55–65 dB */
	--color-high: #c62828; /* ≥ 65 dB  */

	--radius: 6px;
	--shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.12);
	--shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
	--font-sans: "Source Sans Pro", system-ui, -apple-system, sans-serif;
	--header-height: 64px;
	--controls-height: 60px;
}

/* ── Reset ───────────────────────────────────────────────────── */
*,
*::before,
*::after {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}

/* ── Base ────────────────────────────────────────────────────── */
@import url("https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600;700&display=swap");

html {
	font-size: 16px;
	scroll-behavior: smooth;
}

body {
	font-family: var(--font-sans);
	color: var(--color-text);
	background: var(--color-bg);
	min-height: 100vh;
	display: flex;
	flex-direction: column;
}

a {
	color: var(--color-primary);
	text-decoration: underline;
}
a:hover,
a:focus {
	color: var(--color-primary-dark);
}
a:focus-visible {
	outline: 3px solid var(--color-primary);
	outline-offset: 2px;
	border-radius: 2px;
}

/* ── Skip link (WCAG 2.4.1) ─────────────────────────────────── */
.skip-link {
	position: absolute;
	top: -100%;
	left: 0;
	padding: 8px 16px;
	background: var(--color-primary);
	color: #fff;
	font-weight: 600;
	z-index: 9999;
	border-radius: 0 0 var(--radius) 0;
	text-decoration: none;
}
.skip-link:focus {
	top: 0;
}

/* ── Screen-reader only ──────────────────────────────────────── */
.sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

/* ── Header ──────────────────────────────────────────────────── */
.site-header {
	background: var(--color-primary);
	color: #fff;
	height: var(--header-height);
	position: sticky;
	top: 0;
	z-index: 100;
	box-shadow: var(--shadow-md);
}

.header-inner {
	max-width: 1400px;
	margin: 0 auto;
	padding: 0 20px;
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 16px;
}

.header-brand {
	display: flex;
	align-items: center;
	gap: 12px;
}

.brand-logo {
	width: 44px;
	height: 44px;
	flex-shrink: 0;
	border-radius: 4px;
}

.brand-text {
	display: flex;
	flex-direction: column;
	line-height: 1.2;
}

.brand-city {
	font-size: 0.75rem;
	font-weight: 600;
	text-transform: uppercase;
	letter-spacing: 0.08em;
	opacity: 0.85;
}

.brand-title {
	font-size: 1.05rem;
	font-weight: 700;
}

.header-actions {
	display: flex;
	align-items: center;
	gap: 16px;
}

.header-meta {
	font-size: 0.8rem;
	opacity: 0.9;
	white-space: nowrap;
}

.lang-switcher {
	display: flex;
	border: 1.5px solid rgba(255, 255, 255, 0.45);
	border-radius: var(--radius);
	overflow: hidden;
	flex-shrink: 0;
}

.lang-opt {
	background: transparent;
	border: none;
	color: rgba(255, 255, 255, 0.6);
	font-family: var(--font-sans);
	font-size: 0.78rem;
	font-weight: 700;
	padding: 5px 11px;
	cursor: pointer;
	letter-spacing: 0.07em;
	transition: background 0.15s, color 0.15s;
}

.lang-opt + .lang-opt {
	border-left: 1.5px solid rgba(255, 255, 255, 0.45);
}

.lang-opt[aria-pressed="true"] {
	background: rgba(255, 255, 255, 0.22);
	color: #fff;
	cursor: default;
}

.lang-opt:hover:not([aria-pressed="true"]) {
	background: rgba(255, 255, 255, 0.12);
	color: rgba(255, 255, 255, 0.9);
}

.meta-value {
	font-weight: 600;
	margin-left: 4px;
}

/* ── Controls bar ────────────────────────────────────────────── */
.controls-bar {
	background: var(--color-surface);
	border-bottom: 1px solid var(--color-border);
	position: sticky;
	top: var(--header-height);
	z-index: 90;
	box-shadow: var(--shadow-sm);
}

.controls-inner {
	max-width: 1400px;
	margin: 0 auto;
	padding: 0 20px;
	height: var(--controls-height);
	display: flex;
	align-items: center;
	gap: 32px;
}

.control-group {
	display: flex;
	align-items: center;
	gap: 12px;
	border: none;
}

.control-label {
	font-size: 0.85rem;
	font-weight: 600;
	color: var(--color-text-muted);
	white-space: nowrap;
}

/* Radio buttons */
.radio-group {
	display: flex;
	gap: 8px;
}

.radio-btn {
	display: flex;
	align-items: center;
	gap: 6px;
	cursor: pointer;
	padding: 5px 12px;
	border: 1.5px solid var(--color-border);
	border-radius: 20px;
	font-size: 0.85rem;
	transition:
		border-color 0.15s,
		background 0.15s;
	user-select: none;
}

.radio-btn input[type="radio"] {
	accent-color: var(--color-primary);
}

.radio-btn:has(input:checked) {
	border-color: var(--color-primary);
	background: var(--color-primary-light);
	color: var(--color-primary);
	font-weight: 600;
}

.radio-btn:hover {
	border-color: var(--color-primary);
}

.radio-btn small {
	font-size: 0.75rem;
	opacity: 0.75;
}

/* Day slider */
#day-slider {
	width: 180px;
	accent-color: var(--color-primary);
	cursor: pointer;
}

#day-label {
	color: var(--color-primary);
}

/* ── Alerts ──────────────────────────────────────────────────── */
.alert {
	max-width: 1400px;
	margin: 20px auto;
	padding: 14px 20px;
	border-radius: var(--radius);
	font-size: 0.9rem;
}

.alert-error {
	background: #fdecea;
	border: 1px solid #f5c6cb;
	color: #721c24;
}

/* ── Loading ─────────────────────────────────────────────────── */
.loading-overlay {
	display: flex;
}
.loading-overlay[hidden] {
	display: none;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 16px;
	min-height: 300px;
	color: var(--color-text-muted);
}

.spinner {
	width: 44px;
	height: 44px;
	border: 4px solid var(--color-border);
	border-top-color: var(--color-primary);
	border-radius: 50%;
	animation: spin 0.8s linear infinite;
}

@keyframes spin {
	to {
		transform: rotate(360deg);
	}
}

/* ── Main ────────────────────────────────────────────────────── */
.main-content {
	flex: 1;
	max-width: 1400px;
	width: 100%;
	margin: 0 auto;
	padding: 20px;
	display: flex;
	flex-direction: column;
	gap: 20px;
}

/* ── Dashboard grid ──────────────────────────────────────────── */
.dashboard-grid {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 20px;
}

@media (max-width: 900px) {
	.dashboard-grid {
		grid-template-columns: 1fr;
	}
}

/* ── Panels ──────────────────────────────────────────────────── */
.panel {
	background: var(--color-surface);
	border: 1px solid var(--color-border);
	border-radius: var(--radius);
	box-shadow: var(--shadow-sm);
	overflow: hidden;
}

.panel-title {
	font-size: 1rem;
	font-weight: 700;
	color: var(--color-primary);
	padding: 14px 18px 10px;
	border-bottom: 2px solid var(--color-primary);
	display: flex;
	align-items: baseline;
	gap: 8px;
}

.panel-subtitle {
	font-size: 0.8rem;
	font-weight: 400;
	color: var(--color-text-muted);
}

/* ── Map ─────────────────────────────────────────────────────── */
.panel-map {
	display: flex;
	flex-direction: column;
}

#map {
	flex: 1;
	height: 420px;
	min-height: 300px;
	z-index: 1;
}

/* Leaflet popup override */
.leaflet-popup-content-wrapper {
	border-radius: var(--radius);
	font-family: var(--font-sans);
	box-shadow: var(--shadow-md);
}

.leaflet-popup-content {
	font-size: 0.85rem;
	line-height: 1.5;
}

.popup-title {
	font-weight: 700;
	color: var(--color-primary);
	margin-bottom: 6px;
	font-size: 0.9rem;
	line-height: 1.3;
}

.popup-laeq-row {
	display: flex;
	align-items: center;
	gap: 8px;
	margin-bottom: 8px;
}

.popup-db {
	font-size: 1.5rem;
	font-weight: 700;
	color: var(--color-text);
	line-height: 1;
}

.popup-level-badge {
	display: inline-block;
	padding: 3px 9px;
	border-radius: 12px;
	font-size: 0.78rem;
	font-weight: 700;
	color: #fff;
	letter-spacing: 0.02em;
}

.popup-meta {
	display: grid;
	grid-template-columns: auto 1fr;
	gap: 3px 10px;
	font-size: 0.82rem;
	border-top: 1px solid var(--color-border);
	padding-top: 6px;
	margin-top: 2px;
}

.popup-meta dt {
	font-weight: 600;
	color: var(--color-text-muted);
}

.popup-meta dd {
	color: var(--color-text);
	margin: 0;
}

/* Map legend */
.map-legend {
	padding: 12px 18px;
	border-top: 1px solid var(--color-border);
	background: #fafbfc;
}

.legend-title {
	font-size: 0.8rem;
	font-weight: 700;
	color: var(--color-text-muted);
	text-transform: uppercase;
	letter-spacing: 0.05em;
	margin-bottom: 8px;
}

.legend-list {
	list-style: none;
	display: flex;
	gap: 16px;
	flex-wrap: wrap;
	font-size: 0.82rem;
	align-items: center;
}

.legend-dot {
	display: inline-block;
	width: 12px;
	height: 12px;
	border-radius: 50%;
	margin-right: 5px;
	vertical-align: middle;
}

.dot-green {
	background: var(--color-low);
}
.dot-amber {
	background: var(--color-mid);
}
.dot-red {
	background: var(--color-high);
}

.legend-note {
	margin-top: 6px;
	font-size: 0.72rem;
	color: var(--color-text-muted);
}

/* ── Chart panel ─────────────────────────────────────────────── */
.panel-chart {
	display: flex;
	flex-direction: column;
	overflow: visible; /* allow cluster info tooltips to float above the stats bar */
}

.chart-hint {
	padding: 20px 18px;
	color: var(--color-text-muted);
	font-size: 0.9rem;
	text-align: center;
	flex: 1;
	display: flex;
	align-items: center;
	justify-content: center;
}

.chart-wrapper {
	padding: 10px 18px 14px;
	flex: 1;
	min-height: 320px;
	position: relative;
}

#forecast-chart {
	width: 100% !important;
}

/* Noise-level coloring for stat values */
.noise-low {
	color: var(--color-low) !important;
}
.noise-mid {
	color: var(--color-mid) !important;
}
.noise-high {
	color: var(--color-high) !important;
}

/* Cluster info icon + popover */
.info-wrap {
	position: relative;
	display: inline-flex;
	align-items: center;
	vertical-align: middle;
	margin-left: 3px;
}

.info-btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 16px;
	height: 16px;
	flex-shrink: 0;
	border-radius: 50%;
	border: 1.5px solid currentColor;
	background: none;
	cursor: pointer;
	color: var(--color-text-muted);
	font-size: 0.6rem;
	font-style: italic;
	font-weight: 700;
	text-transform: none;
	letter-spacing: 0;
	padding: 0;
	line-height: 1;
	opacity: 0.6;
	transition: color 0.15s, opacity 0.15s;
}

.info-btn:hover,
.info-btn:focus-visible {
	color: var(--color-primary);
	opacity: 1;
}

.info-btn--sm {
	width: 12px;
	height: 12px;
	font-size: 0.5rem;
	border-width: 1px;
}

.stat-dd-with-info {
	display: inline-flex;
	align-items: center;
	gap: 4px;
}

.info-popover {
	display: none;
	position: absolute;
	bottom: calc(100% + 8px);
	left: 50%;
	transform: translateX(-50%);
	width: 220px;
	background: #2d3748;
	color: #fff;
	font-size: 0.75rem;
	font-weight: 400;
	text-transform: none;
	letter-spacing: 0;
	padding: 8px 11px;
	border-radius: var(--radius);
	z-index: 300;
	pointer-events: none;
	line-height: 1.45;
	box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
	white-space: normal;
}

.info-popover::after {
	content: '';
	position: absolute;
	top: 100%;
	left: 50%;
	transform: translateX(-50%);
	border: 5px solid transparent;
	border-top-color: #2d3748;
}

.info-wrap:hover .info-popover,
.info-wrap:focus-within .info-popover,
.info-wrap.active .info-popover {
	display: block;
}

/* Station stats */
.station-stats {
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	gap: 0;
	border-top: 1px solid var(--color-border);
}

.stat-item {
	padding: 10px 14px;
	border-right: 1px solid var(--color-border);
	text-align: center;
}

.stat-item:last-child {
	border-right: none;
}

.station-stats dt {
	font-size: 0.72rem;
	font-weight: 600;
	text-transform: uppercase;
	color: var(--color-text-muted);
	letter-spacing: 0.05em;
	margin-bottom: 4px;
}

.station-stats dd {
	font-size: 1rem;
	font-weight: 700;
	color: var(--color-primary);
}

/* ── Table panel ─────────────────────────────────────────────── */
.panel-table {
	overflow: visible;
}

.table-scroll {
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
}

.noise-table {
	width: 100%;
	min-width: 480px;
	border-collapse: collapse;
	font-size: 0.87rem;
	table-layout: fixed;
}

/* Pinned widths — Estación (col 2) gets the remaining ~40% automatically */
.noise-table th:nth-child(1) {
	width: 5%;
}
.noise-table th:nth-child(3) {
	width: 35%;
}
.noise-table th:nth-child(4) {
	width: 20%;
}

.noise-table caption {
	caption-side: top;
}

.noise-table th {
	background: var(--color-primary);
	color: #fff;
	font-weight: 600;
	padding: 10px 14px;
	text-align: left;
	white-space: nowrap;
}

.noise-table td {
	padding: 9px 14px;
	border-bottom: 1px solid var(--color-border);
}

.noise-table tbody tr:hover {
	background: var(--color-primary-light);
}
.noise-table tbody tr:last-child td {
	border-bottom: none;
}

/* Sortable headers */
.noise-table th.sortable {
	cursor: pointer;
	user-select: none;
	white-space: nowrap;
}
.noise-table th.sortable:hover {
	background: var(--color-primary-dark);
}
.sort-icon::after {
	content: " ⇅";
	opacity: 0.45;
	font-size: 0.8em;
}
th.sort-asc .sort-icon::after {
	content: " ▲";
	opacity: 1;
}
th.sort-desc .sort-icon::after {
	content: " ▼";
	opacity: 1;
}

/* Pagination */
.pagination {
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 16px;
	padding: 12px 18px;
	border-top: 1px solid var(--color-border);
}
.page-btn {
	background: none;
	border: 1.5px solid var(--color-border);
	border-radius: var(--radius);
	padding: 4px 14px;
	font-size: 1.3rem;
	line-height: 1;
	cursor: pointer;
	color: var(--color-primary);
	transition:
		background 0.15s,
		border-color 0.15s;
}
.page-btn:hover:not(:disabled) {
	background: var(--color-primary-light);
	border-color: var(--color-primary);
}
.page-btn:disabled {
	opacity: 0.35;
	cursor: default;
}
.page-info {
	font-size: 0.85rem;
	font-weight: 600;
	color: var(--color-text-muted);
	min-width: 64px;
	text-align: center;
}

/* Noise level badge */
.badge {
	display: inline-block;
	padding: 2px 8px;
	border-radius: 12px;
	font-size: 0.78rem;
	font-weight: 600;
	color: #fff;
}

.badge-low {
	background: var(--color-low);
}
.badge-mid {
	background: var(--color-mid);
}
.badge-high {
	background: var(--color-high);
}

/* ── Footer ──────────────────────────────────────────────────── */
.site-footer {
	background: var(--color-primary-dark);
	color: rgba(255, 255, 255, 0.8);
	margin-top: auto;
}

.footer-inner {
	max-width: 1400px;
	margin: 0 auto;
	padding: 16px 20px;
	font-size: 0.78rem;
	display: flex;
	flex-direction: column;
	gap: 4px;
}

.site-footer a {
	color: rgba(255, 255, 255, 0.9);
}

.footer-updated-line {
	opacity: 0.6;
}

/* ── Focus styles (WCAG 2.4.7) ───────────────────────────────── */
:focus-visible {
	outline: 3px solid #ffd700;
	outline-offset: 2px;
}

/* ── Responsive: tablet (≤ 768px) ───────────────────────────── */
@media (max-width: 768px) {
	:root {
		--header-height: auto;
		--controls-height: auto;
	}

	.header-inner {
		flex-wrap: wrap;
		padding: 10px 16px;
		gap: 8px;
	}

	.header-meta {
		font-size: 0.75rem;
		white-space: normal;
	}

	.controls-inner {
		flex-wrap: wrap;
		height: auto;
		padding: 10px 16px;
		gap: 12px;
	}

	.controls-bar {
		position: static; /* avoid covering map content when stacked */
	}

	#day-slider {
		width: 140px;
	}

	.station-stats {
		grid-template-columns: repeat(2, 1fr);
	}

	.stat-item:nth-child(2) {
		border-right: none;
	}

	/* Cluster nocturno is top-right on mobile — flip popover leftward to avoid overflow */
	.stat-item:nth-child(2) .info-popover {
		left: auto;
		right: 0;
		transform: none;
	}

	.stat-item:nth-child(2) .info-popover::after {
		left: auto;
		right: 8px;
		transform: none;
	}
	.stat-item:nth-child(3) {
		border-top: 1px solid var(--color-border);
	}
	.stat-item:nth-child(4) {
		border-top: 1px solid var(--color-border);
	}
}

/* ── Responsive: phone (≤ 480px) ────────────────────────────── */
@media (max-width: 480px) {
	.brand-city {
		display: none;
	}

	.brand-title {
		font-size: 0.9rem;
	}

	.brand-logo {
		width: 36px;
		height: 36px;
	}

	.header-meta {
		width: 100%;
		text-align: right;
	}

	.control-group {
		width: 100%;
	}

	.radio-group {
		flex-wrap: wrap;
	}

	#day-slider {
		width: 100%;
	}

	#map {
		height: 300px;
		min-height: 240px;
	}

	.chart-wrapper {
		min-height: 240px;
	}

	.main-content {
		padding: 12px;
		gap: 12px;
	}

	.panel-title {
		font-size: 0.9rem;
		padding: 10px 14px 8px;
	}

	.noise-table th,
	.noise-table td {
		padding: 7px 10px;
		font-size: 0.82rem;
	}
}

/* ── Print ───────────────────────────────────────────────────── */
@media print {
	.controls-bar,
	.site-footer,
	.skip-link,
	.lang-switcher {
		display: none;
	}
	.panel {
		box-shadow: none;
		border: 1px solid #ccc;
	}
	#map {
		height: 300px;
	}
}
