:root {
  color-scheme: light;
  --bg: #f6f7f3;
  --panel: #ffffff;
  --ink: #18201f;
  --muted: #63706c;
  --line: #dce2dc;
  --accent: #12675f;
  --accent-2: #b45309;
  --accent-soft: #e7f2ef;
  --danger: #a73333;
  --shadow: 0 12px 34px rgba(24, 32, 31, 0.08);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 14px;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  min-height: 36px;
  padding: 0 12px;
  cursor: pointer;
}

button:hover {
  border-color: #9eb1ad;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button.primary,
#calculateBtn {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
  background: #fff;
  position: sticky;
  top: 0;
  z-index: 10;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 22px;
  line-height: 1.2;
  letter-spacing: 0;
}

h2 {
  font-size: 16px;
  line-height: 1.2;
  letter-spacing: 0;
}

.topbar p,
.muted {
  color: var(--muted);
}

.topbar__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.layout {
  display: grid;
  grid-template-columns: minmax(300px, 360px) minmax(320px, 420px) minmax(520px, 1fr);
  gap: 16px;
  padding: 16px;
}

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

.controls,
.candidates,
.results {
  padding: 16px;
}

.control-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

input,
select {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 0 10px;
}

input:focus,
select:focus,
button:focus {
  outline: 2px solid rgba(18, 103, 95, 0.28);
  outline-offset: 2px;
}

.coords {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.location-row {
  display: grid;
  grid-template-columns: minmax(140px, auto) 1fr;
  align-items: center;
  gap: 10px;
}

#locationStatus {
  color: var(--muted);
  font-size: 12px;
}

.chains {
  border: 1px solid var(--line);
  border-radius: 8px;
  margin: 16px 0 0;
  padding: 12px;
}

.chains legend {
  color: var(--muted);
  padding: 0 6px;
}

#chainFilters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
}

.checkline {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  font-size: 13px;
}

.checkline input {
  width: 16px;
  min-height: 16px;
}

.switches {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.section-actions {
  display: flex;
  gap: 8px;
}

.section-actions button {
  min-height: 32px;
  padding: 0 10px;
  font-size: 12px;
}

.candidate-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
  max-height: calc(100vh - 220px);
  overflow: auto;
  padding-right: 2px;
}

.candidate {
  display: grid;
  grid-template-columns: 26px 1fr auto;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px;
  min-height: 58px;
}

.candidate input {
  width: 16px;
  min-height: 16px;
}

.candidate strong {
  display: block;
  font-size: 13px;
  line-height: 1.25;
}

.candidate span {
  color: var(--muted);
  font-size: 12px;
}

.candidate em {
  font-style: normal;
  font-size: 12px;
  color: var(--accent-2);
  white-space: nowrap;
}

.badge {
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 6px 10px;
  font-weight: 700;
  font-size: 12px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}

.summary-grid > div {
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 10px;
  min-height: 68px;
}

.metric-label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.summary-grid strong {
  font-size: 18px;
}

.calendar-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 14px;
  background: #fbfcfa;
}

.calendar-list {
  display: grid;
  gap: 8px;
  margin-top: 10px;
  max-height: 150px;
  overflow: auto;
}

.calendar-event {
  display: grid;
  grid-template-columns: 92px 1fr auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 8px;
}

.calendar-event time {
  color: var(--accent);
  font-weight: 700;
  font-size: 12px;
}

.calendar-event strong {
  display: block;
  font-size: 12px;
  line-height: 1.25;
}

.calendar-event span {
  color: var(--muted);
  font-size: 12px;
}

.calendar-event em {
  color: var(--muted);
  font-size: 11px;
  font-style: normal;
}

.calendar-event.is-transparent {
  opacity: 0.72;
}

.saved-routes {
  display: grid;
  grid-template-columns: minmax(150px, 1.2fr) auto minmax(160px, 1fr) auto auto;
  align-items: end;
  gap: 10px;
  margin-bottom: 14px;
}

.saved-routes button {
  min-height: 38px;
}

.route-visual-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #fbfcfa;
  margin-bottom: 14px;
  position: relative;
}

#routeVisual {
  display: block;
  width: 100%;
  height: min(58vh, 520px);
  min-height: 380px;
  cursor: grab;
  touch-action: none;
  background:
    radial-gradient(circle at 68% 34%, rgba(18, 103, 95, 0.08), transparent 28%),
    linear-gradient(180deg, #ffffff 0%, #f7faf7 100%);
}

#routeVisual.is-panning {
  cursor: grabbing;
}

.map-toolbar {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  display: flex;
  gap: 6px;
}

.icon-button {
  width: 32px;
  min-height: 32px;
  padding: 0;
  line-height: 1;
  font-weight: 700;
}

.map-details {
  min-height: 38px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  padding: 10px 12px;
  font-size: 12px;
}

.map-details strong {
  color: var(--ink);
}

.maps-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.maps-links a {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 0 12px;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
}

.maps-links a.secondary {
  background: #354b48;
}

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

table {
  border-collapse: collapse;
  width: 100%;
  min-width: 760px;
}

th,
td {
  padding: 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-weight: 700;
  font-size: 12px;
  background: #fbfcfa;
}

td {
  font-size: 13px;
}

tr:last-child td {
  border-bottom: 0;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 26px 10px;
}

.chain-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.chain-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: 0 0 auto;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: #18201f;
  color: #fff;
  border-radius: 8px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
  z-index: 20;
}

@media (max-width: 1200px) {
  .layout {
    grid-template-columns: 340px 1fr;
  }

  .results {
    grid-column: 1 / -1;
  }
}

@media (max-width: 760px) {
  body {
    padding-bottom: calc(124px + env(safe-area-inset-bottom, 0px));
    overflow-x: hidden;
    font-size: 15px;
  }

  .topbar {
    position: sticky;
    top: 0;
    align-items: flex-start;
    flex-direction: column;
    gap: 6px;
    padding: 12px 14px;
    background: rgba(255, 255, 255, 0.96);
    box-shadow: 0 8px 22px rgba(24, 32, 31, 0.08);
  }

  h1 {
    font-size: 20px;
  }

  .topbar p {
    display: -webkit-box;
    max-height: 38px;
    overflow: hidden;
    font-size: 12px;
    line-height: 1.35;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  button,
  input,
  select {
    min-height: 44px;
    font-size: 16px;
  }

  .topbar__actions {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.97);
    box-shadow: 0 -10px 30px rgba(24, 32, 31, 0.14);
    backdrop-filter: blur(14px);
  }

  .topbar__actions button {
    width: 100%;
    min-height: 44px;
  }

  #calculateBtn {
    grid-column: 1 / -1;
    min-height: 48px;
    font-weight: 700;
  }

  .layout {
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 10px 8px 0;
  }

  .panel {
    border-radius: 10px;
    box-shadow: 0 8px 22px rgba(24, 32, 31, 0.07);
  }

  .controls,
  .candidates,
  .results {
    padding: 12px;
  }

  .control-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .control-grid > label:nth-child(1),
  .control-grid > label:nth-child(2),
  .control-grid > div:nth-child(3),
  .control-grid > div:nth-child(4),
  .control-grid > label:nth-child(5),
  .control-grid > label:nth-child(6) {
    grid-column: 1 / -1;
  }

  label {
    gap: 5px;
  }

  .summary-grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .summary-grid > div {
    min-height: 62px;
    padding: 9px;
  }

  .metric-label {
    margin-bottom: 5px;
  }

  .summary-grid strong {
    font-size: 17px;
  }

  .section-head {
    align-items: flex-start;
    gap: 8px;
  }

  .section-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .section-actions button {
    min-height: 38px;
    font-size: 13px;
  }

  .saved-routes {
    grid-template-columns: 1fr 1fr;
    align-items: stretch;
  }

  .saved-routes label {
    grid-column: 1 / -1;
  }

  .saved-routes button {
    min-height: 44px;
  }

  .coords,
  .location-row {
    grid-template-columns: 1fr;
  }

  #chainFilters {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .checkline {
    min-height: 30px;
    align-items: flex-start;
    line-height: 1.25;
  }

  .candidate-list {
    max-height: 48vh;
    gap: 7px;
  }

  .candidate {
    grid-template-columns: 30px minmax(0, 1fr);
    min-height: 62px;
    padding: 9px;
  }

  .candidate em {
    grid-column: 2;
    justify-self: start;
  }

  #routeVisual {
    height: min(46vh, 340px);
    min-height: 280px;
  }

  .map-toolbar {
    top: 8px;
    right: 8px;
  }

  .icon-button {
    width: 38px;
    min-height: 38px;
  }

  .calendar-panel {
    padding: 10px;
  }

  .calendar-list {
    max-height: 220px;
  }

  .calendar-event {
    grid-template-columns: 78px minmax(0, 1fr);
    align-items: start;
    gap: 7px;
  }

  .calendar-event em {
    grid-column: 2;
  }

  .maps-links a {
    justify-content: center;
    width: 100%;
    min-height: 42px;
  }

  .table-wrap {
    overflow: visible;
    border: 0;
  }

  table {
    min-width: 0;
  }

  table,
  thead,
  tbody,
  tr,
  td {
    display: block;
    width: 100%;
  }

  thead {
    display: none;
  }

  tr {
    margin-bottom: 10px;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
  }

  td {
    display: grid;
    grid-template-columns: 82px minmax(0, 1fr);
    gap: 10px;
    padding: 9px 10px;
    border-bottom: 1px solid var(--line);
    font-size: 13px;
  }

  td::before {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
  }

  td:nth-child(1)::before { content: "#"; }
  td:nth-child(2)::before { content: "Magasin"; }
  td:nth-child(3)::before { content: "Groupe"; }
  td:nth-child(4)::before { content: "Arrivee"; }
  td:nth-child(5)::before { content: "Depart"; }
  td:nth-child(6)::before { content: "Km"; }
  td:nth-child(7)::before { content: "Cumul"; }
  td:nth-child(8)::before { content: "Agenda"; }

  td[colspan] {
    display: block;
  }

  td[colspan]::before {
    content: none;
  }

  .empty {
    padding: 18px 12px;
  }

  .toast {
    bottom: calc(118px + env(safe-area-inset-bottom, 0px));
    width: calc(100% - 28px);
    max-width: 360px;
    text-align: center;
  }
}

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

  #chainFilters,
  .summary-grid,
  .saved-routes {
    grid-template-columns: 1fr;
  }

  .section-head {
    flex-direction: column;
  }

  .section-actions,
  .section-actions button {
    width: 100%;
  }
}
