:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --text: #1c1917;
  --text-muted: #78716c;
  --accent: #c2410c;
  --accent-hover: #9a3412;
  --border: #e7e5e4;
  --success: #15803d;
  --radius: 12px;
  --shadow: 0 2px 12px rgba(28, 25, 23, 0.06);
  --font-sans: "DM Sans", system-ui, sans-serif;
  --font-head: "Outfit", "DM Sans", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font-sans);
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 12px 16px 24px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}

.header {
  padding: 16px 0 12px;
  text-align: center;
  flex-shrink: 0;
}

.title {
  font-family: var(--font-head);
  font-size: clamp(1.35rem, 5vw, 1.75rem);
  font-weight: 700;
  margin: 0 0 2px;
  letter-spacing: -0.02em;
  color: var(--text);
}

.subtitle {
  font-size: clamp(0.8125rem, 2.5vw, 0.9375rem);
  color: var(--text-muted);
  margin: 0;
}

.main {
  flex: 1;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
}

@media (min-width: 400px) {
  .card {
    padding: 20px;
    margin-bottom: 16px;
  }
}

.card-head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.card-head-row .card-title {
  margin: 0;
  flex: 1;
}

.btn-refresh-compact {
  flex-shrink: 0;
  min-height: 40px;
  padding: 8px 14px;
  font-size: 0.875rem;
  background: var(--bg);
  color: var(--accent);
  border: 1px solid var(--border);
}

.btn-refresh-compact:hover,
.btn-refresh-compact:focus {
  background: rgba(194, 65, 12, 0.08);
  border-color: var(--accent);
}

.btn-refresh-main {
  margin-top: 12px;
}

.card-title {
  font-family: var(--font-head);
  font-size: 1.125rem;
  font-weight: 600;
  margin: 0 0 4px;
}

.card-desc {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin: 0 0 16px;
}

.label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.input-wrap {
  position: relative;
}

.input {
  width: 100%;
  min-height: 48px;
  padding: 14px 44px 14px 14px;
  font: inherit;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.2s, box-shadow 0.2s;
}

@media (max-width: 360px) {
  .input {
    padding-left: 12px;
    padding-right: 40px;
  }
}

.input::placeholder {
  color: var(--text-muted);
}

.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(194, 65, 12, 0.15);
}

.input-icon {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1.1rem;
  pointer-events: none;
}

.suggestions {
  margin-top: 6px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  max-height: min(50vh, 260px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.suggestions button {
  display: block;
  width: 100%;
  min-height: 48px;
  padding: 14px 16px;
  text-align: left;
  font: inherit;
  font-size: 16px;
  color: var(--text);
  background: none;
  border: none;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.suggestions button:last-child {
  border-bottom: none;
}

.btn-find-route {
  margin-top: 12px;
  width: 100%;
}

.dest-hint {
  margin: 8px 0 0;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.suggestions button:hover,
.suggestions button:focus {
  background: var(--bg);
}

.position-modes {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.btn-mode {
  flex: 1;
  min-height: 48px;
  padding: 12px 10px;
  font-size: 0.8125rem;
  background: var(--bg);
  color: var(--text-muted);
  border: 1px solid var(--border);
  -webkit-tap-highlight-color: transparent;
}

@media (min-width: 380px) {
  .btn-mode {
    font-size: 0.875rem;
    padding: 12px 14px;
  }
}

.btn-mode:hover,
.btn-mode:focus {
  background: var(--border);
  color: var(--text);
}

.btn-mode.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.position-device,
.position-manual {
  margin-top: 0;
}

.position-text {
  margin: 0 0 12px;
  font-size: 0.9375rem;
  color: var(--text-muted);
}

.manual-row {
  margin-bottom: 12px;
}

.manual-row-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.manual-row-inline .manual-label {
  margin-bottom: 0;
  min-width: 2rem;
}

.manual-row-inline .input-small {
  flex: 1;
  min-width: 80px;
}

.manual-label {
  display: block;
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.input-small {
  min-height: 44px;
  padding: 12px 12px;
  font-size: 16px;
}

.position-manual .btn-primary {
  margin-top: 4px;
}

.manual-divider {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin: 16px 0 8px;
  text-align: center;
}

.btn-small {
  padding: 10px 16px;
  font-size: 0.875rem;
}

.position-hint {
  margin: 10px 0 0;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.position-set {
  margin: 8px 0 0;
  font-size: 0.8125rem;
  color: var(--success);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 14px 20px;
  font: inherit;
  font-size: 16px;
  font-weight: 500;
  border-radius: 10px;
  cursor: pointer;
  border: none;
  transition: background 0.2s, color 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  width: 100%;
}

.btn-primary:hover,
.btn-primary:focus {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.btn-icon {
  font-size: 1rem;
  opacity: 0.9;
}

.btn-ghost {
  background: transparent;
  color: var(--accent);
  margin-top: 8px;
  width: 100%;
}

.btn-ghost:hover,
.btn-ghost:focus {
  background: rgba(194, 65, 12, 0.08);
}

.results-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stop-card {
  padding: 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
}

@media (min-width: 400px) {
  .stop-card {
    padding: 16px;
  }
}

.stop-name {
  font-weight: 600;
  font-size: 1rem;
  margin: 0 0 6px;
  line-height: 1.3;
}

.stop-meta {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin: 0 0 12px;
}

.eta-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.eta-list-graphic {
  flex-direction: column;
  gap: 8px;
}

/* Intro sopra l’elenco mezzi */
.mezzi-intro {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin: 0 0 10px;
  font-weight: 500;
}

.mezzi-intro-inline {
  margin-bottom: 8px;
  font-size: 0.75rem;
}

.eta-item {
  display: flex;
  align-items: stretch;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  min-height: 56px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.eta-item:focus-within,
.eta-item:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(194, 65, 12, 0.1);
}

/* Blocco sinistro: linea + tipo mezzo */
.eta-item-mezzo {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  flex-shrink: 0;
  min-width: 0;
}

.eta-item .eta-line-num {
  font-weight: 700;
  font-size: 1.25rem;
  color: var(--accent);
  line-height: 1.2;
  letter-spacing: -0.02em;
}

.eta-item .eta-direction {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text);
  margin-top: 2px;
  line-height: 1.25;
}

.eta-item .eta-type-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--bg);
  padding: 3px 8px;
  border-radius: 6px;
  width: fit-content;
  border: 1px solid var(--border);
}

.eta-item .eta-type-icon-only {
  font-size: 1rem;
  line-height: 1;
}

.eta-item .type-tag {
  flex-shrink: 0;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Blocco destra: barra attesa + tempo */
.eta-item-attesa {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
}

.eta-time-wrap {
  flex: 1;
  min-width: 0;
  height: 8px;
  border-radius: 4px;
  background: var(--border);
  overflow: hidden;
  max-width: 100%;
}

.eta-time-bar {
  display: block;
  height: 100%;
  border-radius: 4px;
  min-width: 4px;
  transition: width 0.3s ease, background 0.2s;
}

.eta-time-bar.imminente {
  background: var(--success);
}

.eta-time-bar.tra-poco {
  background: #ca8a04;
}

.eta-time-bar.attesa {
  background: var(--accent);
}

.eta-item .eta-min {
  flex-shrink: 0;
  font-weight: 600;
  color: var(--text);
  font-size: 0.875rem;
  text-align: right;
  white-space: nowrap;
}

.eta-empty {
  margin: 0;
  padding: 12px;
  font-size: 0.9375rem;
  color: var(--text-muted);
  text-align: center;
}

.eta-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px;
  min-height: 40px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}

.eta-icon,
.type-icon {
  font-size: 1.1em;
  line-height: 1;
}

.eta-pill .line {
  color: var(--accent);
  font-weight: 600;
}

.eta-pill .min {
  color: var(--success);
}

.eta-pill .type-tag {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 500;
}

/* Percorso / itinerario */
.card-itinerary .card-title {
  margin-bottom: 4px;
}

.itinerary-dest {
  margin-bottom: 12px;
  font-weight: 500;
  color: var(--text);
}

.itinerary-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.itinerary-option {
  padding: 14px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
}

.itinerary-option-title {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--accent);
  margin: 0 0 10px;
}

.itinerary-steps {
  margin: 0 0 10px;
  padding-left: 1.4em;
  font-size: 0.9375rem;
  line-height: 1.5;
  color: var(--text);
}

.itinerary-step {
  margin-bottom: 8px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.itinerary-step:last-child {
  margin-bottom: 0;
}

.itinerary-step-icon {
  flex-shrink: 0;
  font-size: 1.1rem;
}

.itinerary-eta {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.itinerary-eta strong {
  color: var(--success);
}

.mezzi-da-utilizzare {
  margin: 0 0 12px;
  padding: 10px 12px;
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text);
  background: rgba(194, 65, 12, 0.08);
  border-radius: 8px;
  border-left: 3px solid var(--accent);
}

.card-nearest .card-title {
  margin-bottom: 8px;
}

.nearest-stop-name {
  font-weight: 600;
  font-size: 1.0625rem;
  margin: 0 0 4px;
}

.nearest-stop-meta {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin: 0 0 12px;
}

.card-nearest .eta-list {
  margin-bottom: 8px;
}

.time-legend {
  margin: 8px 0 0;
  font-size: 0.7rem;
  color: var(--text-muted);
}

.nearest-updated {
  margin: 4px 0 0;
  font-size: 0.75rem;
}

.type-summary {
  margin: 0 0 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.type-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 600;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
}

.type-pill .type-icon {
  margin-right: 2px;
}

.type-label {
  color: var(--accent);
}

.type-count {
  color: var(--success);
}

.empty-text {
  margin: 0;
  font-size: 0.9375rem;
  color: var(--text-muted);
  text-align: center;
}

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  font-size: 0.9375rem;
  color: var(--text-muted);
}

.spinner {
  width: 22px;
  height: 22px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.footer {
  padding: 16px 0;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

.footer a {
  color: var(--accent);
  text-decoration: none;
}

.footer a:hover {
  text-decoration: underline;
}

.hidden {
  display: none !important;
}

@media (max-width: 480px) {
  .app {
    padding-top: 8px;
  }
  .header {
    padding: 12px 0 10px;
  }
  .eta-list {
    gap: 6px;
  }
  .type-summary {
    gap: 6px;
  }
}

@media (pointer: coarse) {
  .btn:active,
  .btn-mode:active,
  .suggestions button:active {
    opacity: 0.9;
  }
}
