:root {
  --cream: #faf6ef;
  --cream2: #f3ead9;
  --panel: #efe6d8;
  --border: #c9b8a0;
  --text: #1a1a1a;
  --muted: #4a4a4a;
  --accent: #1a5f4a;
  --accent2: #2d6f5a;
  --danger: #a63a3a;
  --input-bg: #fffcf7;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  /* Mois : une seule gamme (nuances « passé ») pour toutes les colonnes */
  --zone-l: #f0ebe3;
  --zone-l-even: #ebe6de;
  --safe-l: env(safe-area-inset-left, 0);
  --safe-r: env(safe-area-inset-right, 0);
  --safe-b: env(safe-area-inset-bottom, 0);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: var(--font);
  background: var(--cream);
  color: var(--text);
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
  padding-bottom: var(--safe-b);
}

a {
  color: var(--accent2);
}

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

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
  padding: 0.65rem 0.9rem;
  padding-top: max(0.65rem, env(safe-area-inset-top, 0.5rem));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  flex-shrink: 0;
}

.app-header h1 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 600;
  margin-right: auto;
  color: var(--text);
}

.app-header .btn,
.app-header label.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 44px;
  min-width: 44px;
  padding: 0.4rem 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
  -webkit-tap-highlight-color: transparent;
}

.status-pill {
  font-size: 0.8rem;
  color: var(--muted);
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  background: var(--input-bg);
  max-width: 8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid var(--border);
}

.status-pill.saving {
  color: #8a5a00;
}

.status-pill.saved {
  color: var(--accent2);
}

#btn-menu {
  font-size: 1.3rem;
  line-height: 1;
  font-weight: 700;
  letter-spacing: 0.1em;
}

.panel-menu {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 0.9rem 1rem;
  background: var(--cream2);
  border-bottom: 1px solid var(--border);
  max-height: 70vh;
  overflow-y: auto;
  flex-shrink: 0;
}

.panel-menu[hidden] {
  display: none;
}

.panel-menu .btn,
.panel-menu label.btn {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
  padding: 0.5rem 1rem;
  -webkit-tap-highlight-color: transparent;
}

.panel-menu .btn.full,
.panel-menu label.btn.full {
  width: 100%;
}

.panel-menu .btn.primary,
.panel-menu label.btn.primary {
  background: var(--accent);
  border-color: var(--accent2);
  color: #fff;
}

.panel-menu__top {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}

.panel-menu__section-title {
  margin: 0.35rem 0 0.25rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.releve-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.releve-list__item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.5rem 0.55rem;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  min-height: 48px;
}

.releve-list__text {
  min-width: 0;
  flex: 1;
}

.releve-list__name {
  font-size: 0.9rem;
  line-height: 1.3;
  word-break: break-word;
}

.releve-list__meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.releve-list__empty {
  font-size: 0.9rem;
  color: var(--muted);
  padding: 0.5rem 0;
  list-style: none;
}

.releve-list__remove {
  flex-shrink: 0;
  min-width: 44px;
  min-height: 44px;
  border: 0;
  background: transparent;
  color: var(--danger);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 6px;
  -webkit-tap-highlight-color: transparent;
}

.releve-list__remove:hover,
.releve-list__remove:active {
  background: rgba(166, 58, 58, 0.12);
}

/* Deux volets : 6 colonnes (snap) | mois */
.grid-split {
  display: flex;
  flex: 1;
  min-height: 0;
  width: 100%;
  border-top: 1px solid var(--border);
}

.scroller--left5 {
  flex: 0 0 38%;
  max-width: 11.5rem;
  min-width: 6.5rem;
  overflow-x: auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
  border-right: 1px solid var(--border);
  background: var(--cream);
  touch-action: pan-x pan-y;
}

.scroller--right {
  flex: 1;
  min-width: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--cream);
  max-height: calc(100dvh - 3.2rem);
  touch-action: pan-x pan-y;
}

.data-table {
  border-collapse: separate;
  border-spacing: 0;
  width: max-content;
}

.data-table th,
.data-table td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 0;
  vertical-align: top;
  background: var(--cream);
  color: var(--text);
}

.data-table th {
  background: var(--panel);
  color: var(--text);
  font-weight: 600;
  text-transform: none;
  font-size: 0.78rem;
  position: sticky;
  top: 0;
  z-index: 5;
  padding: 0.4rem 0.3rem;
}

.data-table th .th-inner {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  line-height: 1.2;
  white-space: normal;
  max-width: 6rem;
}

/* 6 colonnes : snap horizontal (une « page » par colonne sur mobile) */
.data-table--left5 th.th-snap-col,
.data-table--left5 td.td-snap-col {
  scroll-snap-align: start;
  min-width: 9.25rem;
  width: 9.25rem;
  max-width: 9.25rem;
}

/* Propriétaire (1re colonne) : −25 % de largeur */
.data-table--left5 th.th-snap-col:nth-child(1),
.data-table--left5 td.td-snap-col:nth-child(1) {
  min-width: 6.9375rem;
  width: 6.9375rem;
  max-width: 6.9375rem;
}

.data-table--left5 td.td-snap-col:nth-child(5) textarea {
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.data-table td input,
.data-table td textarea {
  display: block;
  width: 100%;
  min-width: 2.5rem;
  min-height: 44px;
  border: 0;
  margin: 0;
  padding: 0.4rem 0.25rem;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  resize: vertical;
  min-height: 2.75rem;
  max-height: 7.5rem;
  line-height: 1.25;
}

.data-table td input:focus,
.data-table td textarea:focus {
  outline: 2px solid var(--accent2);
  outline-offset: -1px;
}

.data-table th.month-col {
  min-width: 5.25rem;
  max-width: 6.5rem;
  background: var(--zone-l);
  color: var(--text);
}

.data-table td.cell-month,
.data-table td.cell-month.cell-zone-left,
.data-table td.cell-month.cell-zone-center,
.data-table td.cell-month.cell-zone-right {
  background: var(--zone-l);
}

.data-table tr:nth-child(even) td.cell-month,
.data-table tr:nth-child(even) td.cell-month.cell-zone-left,
.data-table tr:nth-child(even) td.cell-month.cell-zone-center,
.data-table tr:nth-child(even) td.cell-month.cell-zone-right {
  background: var(--zone-l-even);
}

.data-table--right { min-width: 320px; }

/* Menu contextuel (appui long) */
.row-context {
  position: fixed;
  z-index: 200;
  min-width: 12.5rem;
  max-width: min(100vw - 1rem, 20rem);
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.row-context[hidden] {
  display: none;
}

.row-context__btn {
  min-height: 48px;
  padding: 0.5rem 0.6rem;
  text-align: left;
  font-size: 0.95rem;
  border: 0;
  border-radius: 6px;
  background: var(--cream);
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.row-context__btn:hover,
.row-context__btn:active {
  background: var(--panel);
}

.row-context__btn--danger {
  color: var(--danger);
  font-weight: 600;
}

/* Ligne cliquable (hors champs) */
tr.row-data {
  -webkit-user-select: none;
  user-select: none;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(100%, 420px);
  max-width: 100vw;
  height: 100%;
  max-height: 100dvh;
  background: var(--cream);
  border-left: 1px solid var(--border);
  z-index: 100;
  display: flex;
  flex-direction: column;
  box-shadow: -6px 0 20px rgba(0, 0, 0, 0.12);
  transform: translateX(100%);
  transition: transform 0.2s ease;
  color: var(--text);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1rem;
  padding-top: max(0.9rem, env(safe-area-inset-top, 0.5rem));
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  background: var(--panel);
}

.drawer-x {
  min-width: 48px;
  min-height: 48px;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.drawer-body {
  padding: 0.8rem 1rem 2rem;
  padding-bottom: max(1rem, env(safe-area-inset-bottom, 0.5rem));
  overflow: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
  background: var(--cream);
}

.op-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.op-item {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.45rem 0.55rem;
  font-size: 0.85rem;
  line-height: 1.35;
  color: var(--text);
}

.op-item .amt { color: var(--accent2); }
.op-item .line { color: var(--muted); }

.match-list { margin: 0.5rem 0; padding: 0; list-style: none; }
.match-list li {
  margin-bottom: 0.4rem;
  font-size: 0.8rem;
  display: flex;
  align-items: flex-start;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.match-list button.small {
  min-height: 40px;
  padding: 0.15rem 0.4rem;
  font-size: 0.8rem;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.menu-adv { margin: 0.4rem 0; }
.menu-adv label { font-size: 0.9rem; color: var(--muted); }
.menu-adv select {
  min-height: 44px;
  width: 100%;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 1rem;
  margin-top: 0.3rem;
}

.ghost { opacity: 0.5; }

@media (min-width: 900px) {
  .scroller--right {
    max-height: calc(100vh - 3.2rem);
  }
  .scroller--left5 {
    max-width: 14rem;
  }
  .data-table--left5 th.th-snap-col,
  .data-table--left5 td.td-snap-col {
    min-width: 8rem;
    width: 8rem;
  }

  .data-table--left5 th.th-snap-col:nth-child(1),
  .data-table--left5 td.td-snap-col:nth-child(1) {
    min-width: 6rem;
    width: 6rem;
    max-width: 6rem;
  }
}
