:root {
  color-scheme: light;
  --bg: #f0f2f8;
  --card: #ffffff;
  --text: #1b2230;
  --muted: #5b6476;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #e0e7ff;
  --accent-soft-hover: #c7d2fe;
  --ok: #0d9488;
  --border: #e2e8f0;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 12px rgba(30, 41, 59, 0.08);
  --shadow-focus: 0 0 0 3px var(--accent-soft);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.container {
  width: min(1600px, 98vw);
  margin: 0 auto;
  padding: 1.5rem 0 3rem;
}

.page-header {
  margin-bottom: 1.5rem;
}

.page-header h1 {
  margin: 0 0 0.35rem 0;
  font-size: 1.6rem;
  font-weight: 700;
}

.intro {
  margin: 0;
  color: var(--muted);
  font-size: 1rem;
}

/* ----- Contrôles et étapes ----- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  box-shadow: var(--shadow);
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  margin-bottom: 1.25rem;
}

.step {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
}

.step-num {
  flex-shrink: 0;
  width: 2rem;
  height: 2rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 0.9rem;
  border-radius: 50%;
}

.step-content {
  flex: 1;
  min-width: 0;
}

.file-picker {
  display: block;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

/* Zone de dépôt ----- */
.drop-zone {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 120px;
  padding: 1.25rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s;
}

.drop-zone:hover,
.drop-zone:focus-visible {
  border-color: var(--accent);
  background: var(--accent-soft);
  outline: none;
}

.drop-zone.drag-over {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.drop-zone.has-file {
  border-style: solid;
  border-color: var(--ok);
  background: rgba(13, 148, 136, 0.08);
}

.drop-zone input[type='file'] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.drop-zone-text {
  color: var(--muted);
  text-align: center;
  pointer-events: none;
}

.drop-zone.has-file .drop-zone-text {
  display: none;
}

.drop-zone-file {
  color: var(--ok);
  font-weight: 600;
  pointer-events: none;
}

.drop-zone:not(.has-file) .drop-zone-file {
  display: none !important;
}

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

/* Champs et bouton ----- */
.inline-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
}

.percent-label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.field-label {
  font-weight: 600;
  font-size: 0.95rem;
}

.tier-choice {
  border: 0;
  padding: 0;
  margin: 0;
}

.tier-choice legend {
  margin-bottom: 0.5rem;
}

.radio-label {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  margin-right: 1rem;
  cursor: pointer;
}

.radio-label input {
  accent-color: var(--accent);
}

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

.percent-label input {
  width: 5rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
}

.percent-label input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--shadow-focus);
}

button {
  border: 0;
  border-radius: var(--radius-sm);
  padding: 0.6rem 1.25rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s, transform 0.05s;
}

button:active:not(:disabled) {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  min-height: 44px;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-primary:focus-visible {
  outline: none;
  box-shadow: var(--shadow-focus);
}

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

/* Statut ----- */
.status {
  min-height: 1.5rem;
  margin: 0 0 1rem 0;
  padding: 0.5rem 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.status.ok {
  color: var(--ok);
  font-weight: 500;
}

/* Section résultats (401 / 411) ----- */
#results {
  scroll-margin-top: 1rem;
}

.auxiliary-section-header {
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid var(--accent-soft);
}

.auxiliary-section-header h2 {
  margin: 0 0 0.4rem 0;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text);
}

.auxiliary-desc {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.selection-justification {
  margin: 0.75rem 0 0 0;
  padding: 0.65rem 0.85rem;
  background: #f8fafc;
  border-left: 4px solid var(--accent);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 0.9rem;
  color: var(--text);
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.25rem;
}

.card-result {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.card-result h3 {
  margin: 0 0 0.75rem 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.coverage-block {
  margin-bottom: 0.75rem;
}

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

.tier-count-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
}

.tier-count-label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.95rem;
}

.tier-count-input {
  width: 4rem;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
}

.checkbox-row {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.5rem 0 0 0;
  cursor: pointer;
  font-size: 0.95rem;
}

/* Toggle "Masquer les tiers non inclus" */
.filter-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0.75rem 0 0 0;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--card);
  cursor: pointer;
  font-size: 0.95rem;
  user-select: none;
  transition: border-color 0.2s, background-color 0.2s, box-shadow 0.2s;
}

.filter-toggle:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.filter-toggle:focus-within {
  outline: none;
  box-shadow: var(--shadow-focus);
}

.filter-toggle-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.filter-toggle-switch {
  flex-shrink: 0;
  width: 2.5rem;
  height: 1.25rem;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  transition: background-color 0.2s;
}

.filter-toggle-switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: calc(1.25rem - 4px);
  height: calc(1.25rem - 4px);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s;
}

.filter-toggle-input:checked ~ .filter-toggle-switch {
  background: var(--accent);
}

.filter-toggle-input:checked ~ .filter-toggle-switch::after {
  transform: translateX(1.25rem);
}

.filter-toggle-input:checked ~ .filter-toggle-label {
  font-weight: 600;
}

.action-row {
  margin: 0.5rem 0 0 0;
}

.btn-secondary {
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--accent-soft-hover);
}

.coverage-stat {
  margin: 0;
  font-size: 1rem;
}

.coverage-stat strong {
  color: var(--accent);
}

.coverage-bar {
  height: 10px;
  background: var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  max-width: 320px;
  margin-bottom: 0.35rem;
}

.coverage-fill {
  height: 100%;
  background: var(--accent);
  border-radius: var(--radius-sm);
  transition: width 0.2s ease;
}

.coverage-block .hint {
  margin: 0.25rem 0 0 0;
}

.summary {
  background: var(--accent-soft);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.85rem;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.selection-desc {
  margin: 0 0 0.75rem 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.summary-merged {
  line-height: 1.5;
}

.table-wrap tbody.hide-unselected-rows tr:not(.selected) {
  display: none;
}

.table-wrap {
  overflow: auto;
  max-height: 55vh;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

thead {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--bg);
  box-shadow: 0 1px 0 var(--border);
}

th {
  padding: 0.6rem 0.5rem;
  text-align: left;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.th-checkbox {
  font-weight: 600;
}

td {
  padding: 0.5rem;
  border-bottom: 1px solid var(--border);
}

tbody tr:nth-child(even) {
  background: rgba(0, 0, 0, 0.02);
}

tbody tr:hover {
  background: var(--accent-soft);
}

tr.selected {
  background: var(--accent-soft) !important;
}

tbody tr.selected:hover {
  background: var(--accent-soft-hover) !important;
}

/* Ligne en orange : solde créditeur pour clients, solde débiteur pour fournisseurs */
tr.row-anomaly {
  background: #fff7ed !important;
}

tr.row-anomaly:hover {
  background: #ffedd5 !important;
}

/* Checkbox plus grande pour le touch ----- */
.table-wrap input[type='checkbox'] {
  width: 1.1rem;
  height: 1.1rem;
  cursor: pointer;
  accent-color: var(--accent);
}

/* Responsive ----- */
@media (max-width: 960px) {
  .container {
    padding: 1rem 0 2rem;
  }

  .grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .step {
    flex-direction: column;
    align-items: stretch;
  }

  .step-num {
    align-self: flex-start;
  }

  .drop-zone {
    min-height: 100px;
  }
}

@media (max-width: 480px) {
  .inline-fields {
    flex-direction: column;
    align-items: stretch;
  }

  .btn-primary {
    width: 100%;
  }
}
