:root {
  --bg: #f3efe6;
  --bg-accent: #e7eedf;
  --ink: #1c2418;
  --muted: #5c6756;
  --line: #c9d0bf;
  --panel: rgba(255, 252, 246, 0.88);
  --brand: #2f5d3a;
  --brand-dark: #1f3f28;
  --warn: #8a4b12;
  --done: #2f5d3a;
  --todo: #7a3b2e;
  --shadow: 0 18px 40px rgba(28, 36, 24, 0.08);
  --radius: 18px;
  --font: "DM Sans", sans-serif;
  --display: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, #dfe9d4 0%, transparent 42%),
    radial-gradient(circle at 88% 0%, #f0e2c8 0%, transparent 36%),
    linear-gradient(165deg, var(--bg), var(--bg-accent));
}

body { line-height: 1.45; }

.page {
  width: min(1100px, calc(100% - 2rem));
  margin: 0 auto;
  padding: 2rem 0 4rem;
}

.top {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  align-items: end;
  margin-bottom: 1.25rem;
}

.brand {
  margin: 0 0 0.35rem;
  font-family: var(--display);
  font-size: 1.6rem;
  color: var(--brand);
  letter-spacing: -0.02em;
}

h1 {
  margin: 0;
  font-family: var(--display);
  font-size: clamp(1.8rem, 4vw, 2.6rem);
  letter-spacing: -0.03em;
  font-weight: 700;
}

h2 {
  margin: 0;
  font-family: var(--display);
  font-size: 1.3rem;
}

.lead {
  margin: 0.55rem 0 0;
  color: var(--muted);
  max-width: 40rem;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(4.5rem, 1fr));
  gap: 0.75rem;
  padding: 0.9rem 1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
}

.stats div { text-align: center; }

.stats span {
  display: block;
  font-family: var(--display);
  font-size: 1.55rem;
  font-weight: 700;
}

.stats small {
  color: var(--muted);
  font-size: 0.78rem;
}

.tabs {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 1rem;
  padding: 0.3rem;
  background: color-mix(in srgb, white 55%, transparent);
  border: 1px solid var(--line);
  border-radius: 999px;
  width: fit-content;
}

.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  padding: 0.55rem 1.1rem;
  border-radius: 999px;
  cursor: pointer;
}

.tab.is-active {
  background: var(--brand);
  color: #f7faf4;
}

.home-bar,
.card,
.row {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
}

.home-bar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: end;
  padding: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.zone-select,
.sort-select {
  min-width: min(100%, 220px);
}

.home-meta {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  justify-content: flex-end;
}

.card {
  padding: 1.1rem 1.2rem 1.25rem;
  margin-bottom: 1rem;
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.35rem;
}

.options-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.85rem;
  align-items: end;
  margin-top: 0.85rem;
}

label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
}

select,
input[type="date"],
input[type="text"],
input[type="number"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.7rem 0.8rem;
  font: inherit;
  color: var(--ink);
  background: #fffdf8;
}

select:focus,
input:focus {
  outline: 2px solid color-mix(in srgb, var(--brand) 45%, white);
  outline-offset: 1px;
}

.btn {
  border: 0;
  border-radius: 999px;
  padding: 0.75rem 1.1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.btn:hover { transform: translateY(-1px); }

.btn.primary {
  background: var(--brand);
  color: #f7faf4;
}

.btn.primary:hover { background: var(--brand-dark); }

.btn.ghost {
  background: transparent;
  color: var(--brand-dark);
  border: 1px solid var(--line);
}

.btn.tiny {
  padding: 0.25rem 0.65rem;
  font-size: 0.78rem;
}

.btn.danger { color: var(--todo); }

.btn.toggle { min-width: 9.5rem; }

.btn.toggle.done {
  background: color-mix(in srgb, var(--done) 16%, white);
  color: var(--done);
  border: 1px solid color-mix(in srgb, var(--done) 35%, white);
}

.btn.toggle.todo {
  background: color-mix(in srgb, var(--todo) 12%, white);
  color: var(--todo);
  border: 1px solid color-mix(in srgb, var(--todo) 30%, white);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 0 0 1rem;
  min-height: 2.5rem;
  justify-content: space-between;
}

.hint {
  margin: 0.55rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.hint.muted { margin-top: 0.25rem; margin-bottom: 0.9rem; }

.hint.error { color: var(--todo); }

.zone-form {
  margin: 0.5rem 0 1rem;
  padding: 1rem;
  border: 1px dashed var(--line);
  border-radius: 14px;
  background: color-mix(in srgb, white 55%, transparent);
}

.manual-grid,
.zone-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.85rem;
}

.zone-grid .span-2 { grid-column: span 2; }

.manual-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.9rem;
}

.empty {
  padding: 2.4rem 1.2rem;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: color-mix(in srgb, white 55%, transparent);
}

.empty.compact { padding: 1.4rem 1rem; }

.list,
.zones-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.75rem;
}

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 1rem;
  padding: 1rem 1.1rem;
  animation: rise 0.35s ease both;
}

.row.is-done { opacity: 0.78; }

.row-top {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  align-items: center;
  margin-bottom: 0.35rem;
}

.nom-adresse {
  font-size: 1.05rem;
  color: var(--brand-dark);
}

.nom-adresse:empty { display: none; }

.nom-adresse:not(:empty)::after {
  content: " — ";
  color: var(--muted);
  font-weight: 500;
}

.adresse { font-size: 1.05rem; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  border: 1px solid var(--line);
  color: var(--muted);
  background: #fff;
}

.badge.source.manuel {
  color: var(--warn);
  border-color: color-mix(in srgb, var(--warn) 35%, white);
}

.badge.distance {
  color: var(--brand);
  border-color: color-mix(in srgb, var(--brand) 30%, white);
}

.meta, .demandeur, .notes-line {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.demandeur {
  margin-top: 0.25rem;
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.demandeur:empty { display: none; }

.notes-line {
  margin-top: 0.35rem;
  color: var(--ink);
}

.notes-edit { margin-top: 0.7rem; }

.row-actions {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  justify-content: start;
}

.compact-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.compact-row {
  display: grid;
  grid-template-columns: 2fr 1.2fr 1.15fr auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.55rem 0.9rem;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
}

.compact-row:last-child { border-bottom: 0; }

.compact-head {
  background: color-mix(in srgb, var(--brand) 7%, #fff);
  border-bottom: 1px solid var(--line);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--muted);
  padding-top: 0.65rem;
  padding-bottom: 0.65rem;
}

.compact-row .c-adresse {
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.compact-row .c-commune,
.compact-row .c-date {
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.zone-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 1rem;
  padding: 0.95rem 1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.zone-info .z-name { font-size: 1.05rem; }

.zone-info .z-address,
.zone-info .z-meta {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.zone-info .z-error {
  margin: 0.35rem 0 0;
  color: var(--todo);
  font-size: 0.85rem;
}

.route-controls {
  display: grid;
  grid-template-columns: 1.4fr 1.4fr 1fr 1fr;
  gap: 0.85rem;
  align-items: end;
  margin-top: 0.85rem;
}

label.check {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.88rem;
  padding-bottom: 0.55rem;
}

label.check input {
  width: auto;
}

.commune-picker {
  margin-top: 1rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: color-mix(in srgb, white 55%, transparent);
  overflow: hidden;
}

.commune-picker-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--line);
}

.commune-list {
  max-height: 220px;
  overflow: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.35rem;
  padding: 0.75rem;
}

.commune-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.88rem;
  color: var(--ink);
  font-weight: 500;
}

.commune-item input { width: auto; }
.commune-item small { color: var(--muted); font-weight: 400; }

.route-map {
  height: 380px;
  margin: 0 0 0.85rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  overflow: hidden;
  z-index: 1;
  position: relative;
}

.trip-map-slot {
  min-height: 0;
}

.trip-map-slot:empty {
  display: none;
}

.trip-list-section {
  margin-top: 0.25rem;
}

.route-legend {
  background: rgba(255, 252, 246, 0.95);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.55rem 0.7rem;
  box-shadow: var(--shadow);
  min-width: 160px;
  max-width: 220px;
  font-size: 0.82rem;
}

.route-legend strong {
  display: block;
  margin-bottom: 0.4rem;
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.route-legend label {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0.28rem 0;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
}

.route-legend input { width: auto; margin: 0; }

.route-legend .swatch {
  width: 0.85rem;
  height: 0.85rem;
  border-radius: 3px;
  flex-shrink: 0;
}

.marker-num {
  background: var(--brand);
  color: #f7faf4;
  border: 2px solid #fff;
  border-radius: 999px;
  width: 1.55rem;
  height: 1.55rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 700;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}

.marker-num.start {
  background: #8a4b12;
  width: 1.7rem;
  height: 1.7rem;
}

.trip-card-head {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.65rem;
  align-items: start;
}

.trip-card-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  height: 1.75rem;
  margin: 0.1rem 0 0;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 0.45rem;
  background: color-mix(in srgb, var(--brand) 6%, #fff);
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.trip-card-toggle:hover {
  color: var(--brand-dark);
  border-color: color-mix(in srgb, var(--brand) 35%, var(--line));
}

.trip-card.is-open .trip-card-toggle {
  color: var(--brand-dark);
}

.trip-card-summary {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.trip-card-summary:hover h3 {
  color: var(--brand-dark);
}

.trip-card-body {
  margin-top: 0.75rem;
}

.trip-card-body[hidden] {
  display: none !important;
}

.trip-stops li {
  display: grid;
  grid-template-columns: auto 2.2rem 1fr auto;
  gap: 0.45rem;
  align-items: center;
  padding: 0.55rem 0;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
  font-size: 0.9rem;
}

.trip-stops li.is-done {
  background: color-mix(in srgb, #2f5d3a 8%, transparent);
}

.trip-stops li.is-skip {
  background: color-mix(in srgb, #a33b2b 7%, transparent);
}

.trip-stops .t-stop-main {
  min-width: 0;
}

.t-flyer-actions {
  display: flex;
  gap: 0.35rem;
  flex-shrink: 0;
}

.btn-flyer {
  width: 2.35rem;
  height: 2.35rem;
  padding: 0;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-flyer:disabled {
  opacity: 0.55;
}

.btn-flyer-ok {
  color: #2f5d3a;
  border-color: color-mix(in srgb, #2f5d3a 35%, var(--line));
}

.btn-flyer-ok.is-active {
  background: #2f5d3a;
  border-color: #2f5d3a;
  color: #f7faf4;
}

.btn-flyer-no {
  color: #a33b2b;
  border-color: color-mix(in srgb, #a33b2b 40%, var(--line));
}

.btn-flyer-no.is-active {
  background: #a33b2b;
  border-color: #a33b2b;
  color: #fff8f6;
}

.btn-maps {
  width: 2rem;
  height: 2rem;
  padding: 0;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  line-height: 1;
  flex-shrink: 0;
}

.route-results {
  display: grid;
  gap: 1rem;
}

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

.trip-card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: flex-end;
}

.trip-card h3 {
  margin: 0 0 0.35rem;
  font-family: var(--display);
  font-size: 1.2rem;
}

.trip-stops.editable-stops li {
  grid-template-columns: 1.1rem auto 2.2rem 1fr auto;
}

.trip-stops .drag-handle {
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1;
  user-select: none;
  cursor: grab;
  text-align: center;
}

.trip-stops li.is-start-stop .drag-handle {
  cursor: default;
  opacity: 0.45;
}

.trip-stops li.is-draggable {
  cursor: grab;
}

.trip-stops li.is-dragging {
  opacity: 0.45;
}

.trip-stops li.drag-over {
  background: color-mix(in srgb, var(--brand) 10%, transparent);
  box-shadow: inset 0 2px 0 var(--brand);
}

.stop-movers {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.btn-move {
  width: 1.85rem;
  height: 1.55rem;
  padding: 0;
  line-height: 1;
  font-size: 0.85rem;
}

.trip-meta {
  margin: 0 0 0.75rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.trip-stops {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.35rem;
}

.trip-stops li:last-child { border-bottom: 0; }

.trip-stops .n {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.7rem;
  height: 1.7rem;
  border-radius: 999px;
  background: var(--brand);
  color: #f7faf4;
  font-size: 0.75rem;
  font-weight: 700;
}

.trip-stops .n.start {
  background: var(--warn);
}

.trip-stops .sub {
  color: var(--muted);
  font-size: 0.82rem;
}

@media (max-width: 960px) {
  .route-controls { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 640px) {
  .route-controls { grid-template-columns: 1fr; }
  .route-map { height: 280px; }
  .trip-card-head {
    grid-template-columns: auto 1fr;
  }
  .trip-card-actions {
    grid-column: 1 / -1;
    justify-content: stretch;
  }
  .trip-card-actions .btn {
    flex: 1;
  }
}

.zone-actions .btn:disabled {
  opacity: 0.55;
  cursor: wait;
  transform: none;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 960px) {
  .manual-grid, .zone-grid { grid-template-columns: 1fr 1fr; }
  .zone-grid .span-2 { grid-column: span 2; }
  .compact-row { grid-template-columns: 1fr 1fr; }
  .top { flex-direction: column; align-items: start; }
  .options-grid { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .page {
    width: min(100% - 1.2rem, 1100px);
    padding-top: 1.2rem;
  }
  .manual-grid, .zone-grid, .row, .zone-row { grid-template-columns: 1fr; }
  .zone-grid .span-2 { grid-column: span 1; }
  .compact-row { grid-template-columns: 1fr; gap: 0.25rem; }
  .compact-head { display: none; }
  .row-actions { flex-direction: row; flex-wrap: wrap; }
  .tabs { width: 100%; }
  .tab { flex: 1; }
  .home-bar { align-items: stretch; }
  .home-meta { justify-content: space-between; }
}
