:root {
  --blue: #0a84ff;
  --green: #30d158;
  --red: #ff453a;
  --orange: #ff9f0a;
  --text: #1d1d1f;
  --radius: 22px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* Input file nascosto ma attivabile anche su iOS (non usa display:none). */
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%);
  white-space: nowrap; border: 0;
}
html, body {
  margin: 0;
  min-height: 100%;
  height: 100%;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
#map {
  position: fixed;
  inset: 0 auto auto 0;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  background: #e8edf0;
}
body.dragging #map { outline: 3px dashed var(--blue); outline-offset: -6px; }

.glass {
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: saturate(180%) blur(24px);
  -webkit-backdrop-filter: saturate(180%) blur(24px);
  border: 0.5px solid rgba(0, 0, 0, 0.08);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

/* ---------- Caricamento iniziale ---------- */
.loader {
  position: fixed; inset: 0; z-index: 40;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px;
  background: #fff;
  transition: opacity .35s ease;
}
.loader.hidden { opacity: 0; pointer-events: none; }
.loader-logo {
  width: 88px; height: 88px; border-radius: 22px; display: block;
  box-shadow: 0 14px 40px rgba(0, 34, 255, .28);
  animation: loader-pop .45s cubic-bezier(.2, .9, .3, 1.3) both;
}
.loader-ring {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.5px solid rgba(10, 132, 255, .2); border-top-color: var(--blue);
  animation: loader-spin .7s linear infinite;
}
@keyframes loader-pop {
  from { opacity: 0; transform: scale(.82); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes loader-spin { to { transform: rotate(360deg); } }
@media (prefers-color-scheme: dark) {
  .loader { background: #000; }
}
@media (prefers-reduced-motion: reduce) {
  .loader-logo { animation: none; }
  .loader-ring { animation-duration: 1.6s; }
}

/* ---------- Splash ---------- */
.splash {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: rgba(255,255,255,.85);
  backdrop-filter: blur(20px);
  transition: opacity .4s ease;
}
.splash.hidden { opacity: 0; pointer-events: none; }
.splash-card {
  text-align: center; max-width: 340px; padding: 32px 28px;
  border-radius: 28px; background: #fff;
  box-shadow: 0 20px 60px rgba(0,0,0,.15);
}
.logo-icon {
  width: 76px; height: 76px; display: block; margin: 0 auto 14px;
  border-radius: 19px; box-shadow: 0 10px 28px rgba(0, 34, 255, .22);
}
.logo { font-size: 34px; font-weight: 700; letter-spacing: -0.5px; margin-bottom: 12px; }
.splash-card p { font-size: 15px; line-height: 1.45; color: #555; margin: 0 0 20px; }
.splash-card .note { font-size: 12px; color: #999; margin: 14px 0 0; }
.btn {
  appearance: none; border: 0; cursor: pointer; font: inherit; font-weight: 600;
  padding: 12px 20px; border-radius: 999px; background: rgba(120,120,128,.16); color: var(--text);
  touch-action: manipulation;
  transition: transform .15s ease, opacity .15s ease;
}
.btn:active { transform: scale(.96); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--blue); color: #fff; }
.btn.big { padding: 14px 40px; font-size: 17px; }

/* ---------- Dock ---------- */
.dock {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 16px);
  display: flex; gap: 6px; padding: 8px 10px; border-radius: 999px; z-index: 5;
  max-width: calc(100vw - 16px); overflow-x: auto; flex-wrap: nowrap;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.dock::-webkit-scrollbar { display: none; }
.dock-btn {
  appearance: none; border: 0; cursor: pointer; background: transparent; color: var(--text);
  width: 48px; height: 48px; border-radius: 14px; flex: none; touch-action: manipulation;
  display: flex; align-items: center; justify-content: center;
  transition: transform .18s cubic-bezier(.2,.9,.3,1.4), background .18s ease;
}
.dock-btn:active { transform: scale(.88); }
.dock-btn:disabled { opacity: .3; cursor: default; }
.dock-btn.active { background: rgba(10,132,255,.15); color: var(--blue); }

/* ---------- Accessibilità: focus visibile ---------- */
.dock-btn:focus-visible,
.btn:focus-visible,
.close-btn:focus-visible,
#elev-chart:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
  border-radius: 12px;
}

/* ---------- Banner ---------- */
.banner {
  position: fixed; top: calc(env(safe-area-inset-top) + 12px); left: 12px; right: 12px;
  max-width: 420px; margin: 0 auto; z-index: 6; cursor: grab;
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-radius: var(--radius);
  touch-action: none; -webkit-user-select: none; user-select: none;
  transition: transform .35s cubic-bezier(.2,.9,.3,1.2), opacity .25s ease;
}
.banner.hidden { transform: translateY(-140%); opacity: 0; pointer-events: none; }
.banner-icon { width: 36px; height: 36px; border-radius: 50%; flex: none; background: var(--green); }
.banner-text { display: flex; flex-direction: column; line-height: 1.25; }
.banner-text strong { font-size: 15px; }
.banner-text span { font-size: 13px; opacity: .65; }
.banner.ok .banner-icon { background: var(--green); }
.banner.warn .banner-icon { background: var(--orange); }
.banner.off .banner-icon { background: var(--red); }

/* ---------- Pannelli a scomparsa e profilo altimetrico ---------- */
.sheet {
  position: fixed; left: 12px; right: 12px;
  bottom: calc(env(safe-area-inset-bottom) + 88px);
  max-width: 420px; max-height: min(62vh, 560px); margin: 0 auto; padding: 16px;
  overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  border-radius: var(--radius); z-index: 7;
  transition: transform .35s cubic-bezier(.2,.9,.3,1.2), opacity .25s ease;
}
.sheet.hidden { transform: translateY(120%); opacity: 0; pointer-events: none; }
.sheet::before {
  content: ""; display: block; width: 40px; height: 4px; border-radius: 999px;
  background: rgba(120,120,128,.45); margin: -4px auto 12px;
}
.info-header, .sheet-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.info-header strong, .sheet-header strong { font-size: 17px; }
.close-btn {
  appearance: none; border: 0; background: transparent; color: var(--text);
  font: inherit; font-size: 26px; line-height: 1; cursor: pointer; padding: 2px 6px;
  touch-action: manipulation;
}
.info-stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 14px; }
.stat {
  display: flex; flex-direction: column; gap: 3px; min-width: 0;
  padding: 9px 10px; border-radius: 12px; background: rgba(120,120,128,.1);
}
.stat span { font-size: 11px; opacity: .65; }
.stat strong { font-size: 14px; overflow-wrap: anywhere; }
.chart-wrap { position: relative; width: 100%; }
#elev-chart { display: block; width: 100%; height: 160px; overflow: visible; touch-action: pan-y; }
.chart-extreme { font-size: 9px; fill: currentColor; }
.chart-tooltip {
  position: absolute; top: -5px; transform: translateX(-50%);
  padding: 4px 8px; border-radius: 8px; color: #fff;
  background: rgba(0,0,0,.78); font-size: 11px; white-space: nowrap; pointer-events: none;
}
.chart-tooltip.hidden { display: none; }
.chart-hint { margin: 5px 0 0; text-align: center; font-size: 11px; opacity: .6; }

/* ---------- Tracciati salvati ---------- */
.library-list { display: flex; flex-direction: column; gap: 8px; }
.library-item { display: flex; align-items: stretch; gap: 6px; }
.library-load {
  flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; text-align: left;
  appearance: none; border: 0; cursor: pointer; font: inherit; color: var(--text);
  padding: 10px 12px; border-radius: 12px; background: rgba(120,120,128,.1);
  touch-action: manipulation;
}
.library-load strong { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-load span { font-size: 11px; opacity: .65; }
.library-item.current .library-load { box-shadow: inset 0 0 0 2px var(--blue); }
.library-delete {
  appearance: none; border: 0; cursor: pointer; flex: none; width: 44px;
  border-radius: 12px; background: rgba(120,120,128,.1); color: var(--red);
  font-size: 22px; line-height: 1; touch-action: manipulation;
}
.btn.danger { background: rgba(255,69,58,.14); color: var(--red); width: 100%; margin-top: 12px; }
#library-empty { margin: 4px 0 0; }

/* ---------- Pannello Altro: vista mappa e azioni ---------- */
.sheet-label {
  margin: 0 0 8px; font-size: 12px; font-weight: 600; opacity: .6;
  text-transform: uppercase; letter-spacing: .04em;
}
.segmented { display: flex; gap: 4px; padding: 4px; border-radius: 14px; background: rgba(120,120,128,.12); margin-bottom: 14px; }
.seg-btn {
  flex: 1; appearance: none; border: 0; cursor: pointer; font: inherit; font-size: 13px;
  font-weight: 600; color: var(--text); padding: 9px 6px; border-radius: 10px;
  background: transparent; touch-action: manipulation;
  transition: background .18s ease, color .18s ease;
}
.seg-btn.active { background: #fff; color: var(--blue); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.more-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.more-action {
  display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center;
  appearance: none; border: 0; cursor: pointer; font: inherit; font-size: 12px; font-weight: 600;
  color: var(--text); padding: 14px 10px; border-radius: 16px; background: rgba(120,120,128,.1);
  touch-action: manipulation; transition: background .18s ease, color .18s ease;
}
.more-action.active { background: rgba(10,132,255,.15); color: var(--blue); }
.more-action svg { opacity: .85; }

/* ---------- Interruttore (impostazioni) ---------- */
.toggle-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 12px; border-radius: 14px; background: rgba(120,120,128,.1);
  cursor: pointer; touch-action: manipulation;
}
.toggle-label { display: flex; flex-direction: column; gap: 2px; font-size: 14px; font-weight: 600; }
.toggle-label small { font-weight: 400; font-size: 11px; opacity: .6; }
.switch {
  appearance: none; -webkit-appearance: none; flex: none; width: 48px; height: 28px;
  border-radius: 999px; background: rgba(120,120,128,.35); position: relative;
  cursor: pointer; margin: 0; transition: background .2s ease;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 24px; height: 24px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3);
  transition: transform .2s ease;
}
.switch:checked { background: var(--green); }
.switch:checked::after { transform: translateX(20px); }
.switch:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

/* ---------- Meteo ---------- */
.chart-wrap.hidden { display: none; }
.weather { margin-top: 14px; padding-top: 12px; border-top: .5px solid rgba(120,120,128,.25); }
.weather-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.weather-head strong { font-size: 14px; }
.weather-place { font-size: 11px; opacity: .55; font-variant-numeric: tabular-nums; }
.weather-body { display: flex; flex-direction: column; gap: 8px; font-size: 13px; }
.weather-now { display: flex; align-items: center; gap: 8px; }
.weather-now .weather-wind { margin-left: auto; font-size: 11px; opacity: .6; }
.weather-icon { font-size: 22px; line-height: 1; }
.weather-now-text strong { font-size: 17px; margin-right: 4px; }
.weather-days { display: flex; flex-direction: column; gap: 5px; }
.weather-day { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 10px; background: rgba(120,120,128,.1); }
.weather-day.warn { background: rgba(255,159,10,.16); }
.weather-day.off { background: rgba(255,69,58,.16); }
.weather-day-name { width: 48px; text-transform: capitalize; opacity: .75; }
.weather-day .weather-icon { font-size: 16px; }
.weather-day-temp { margin-left: auto; font-variant-numeric: tabular-nums; }
.weather-rain { width: 38px; text-align: right; font-size: 11px; opacity: .6; }
.weather-alert { margin: 10px 0 0; padding: 8px 10px; border-radius: 10px; font-size: 12px; font-weight: 600; }
.weather-alert.hidden { display: none; }
.weather-alert.warn { background: rgba(255,159,10,.18); color: #a35f00; }
.weather-alert.off { background: rgba(255,69,58,.18); color: #b3241a; }

/* ---------- Marker utente ---------- */
.user-dot {
  width: 18px; height: 18px; border-radius: 50%; background: var(--blue);
  border: 3px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.1), 0 2px 8px rgba(0,0,0,.35);
  position: relative; z-index: 1;
}
.user-dot::before {
  content: ""; display: none; position: absolute; z-index: -1;
  width: var(--accuracy-diameter, 0px); height: var(--accuracy-diameter, 0px);
  left: 50%; top: 50%; transform: translate(-50%, -50%);
  border: 1px solid rgba(10,132,255,.55); border-radius: 50%;
  background: rgba(10,132,255,.14); pointer-events: none;
}
.user-dot.has-accuracy::before { display: block; }
.user-dot.stale { opacity: .45; }
.user-dot .cone {
  display: none; position: absolute; z-index: 2; top: -22px; left: 50%;
  width: 0; height: 0; border-left: 7px solid transparent;
  border-right: 7px solid transparent; border-bottom: 14px solid var(--blue);
  transform-origin: 50% 100%; filter: drop-shadow(0 1px 1px rgba(0,0,0,.3));
}
.user-dot .cone.visible { display: block; }
.user-dot .cone.visible::after {
  content: ""; position: absolute; top: 9px; left: -18px; width: 36px; height: 28px;
  background: rgba(10,132,255,.16); clip-path: polygon(50% 0, 0 100%, 100% 100%);
  transform: translateY(4px); z-index: -1;
}

@media (prefers-color-scheme: dark) {
  :root { --text: #f5f5f7; }
  .glass { background: rgba(30,30,32,.7); border-color: rgba(255,255,255,.1); }
  .splash { background: rgba(20,20,22,.85); }
  .splash-card { background: #1c1c1e; }
  .splash-card p { color: #bbb; }
  .logo-icon { box-shadow: 0 0 0 1px rgba(255,255,255,.12), 0 10px 28px rgba(0, 0, 0, .5); }
  .stat { background: rgba(255,255,255,.1); }
  .chart-extreme { fill: #ddd; }
  .seg-btn.active { background: rgba(255,255,255,.14); }
  .weather-alert.warn { color: #ffb340; }
  .weather-alert.off { color: #ff6961; }
}

@media (max-height: 600px) {
  .sheet { max-height: 52vh; }
  #elev-chart { height: 125px; }
}

/* ---------- Schermi piccoli: dock piu compatto, uso a una mano ---------- */
@media (max-width: 480px) {
  .dock { gap: 2px; padding: 6px; bottom: calc(env(safe-area-inset-bottom) + 10px); }
  .dock-btn { width: 42px; height: 42px; border-radius: 12px; }
  .sheet { bottom: calc(env(safe-area-inset-bottom) + 62px); }
  .close-btn { padding: 6px 10px; }
}