:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #16181d;
  --muted: #5d6470;
  --border: #dde1e6;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --neg: #c62828;
  --pos: #1b7f3b;
  --section: #e6f2f0;
  --total: #f2f4f6;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1a1d23;
    --surface-2: #242830;
    --text: #e9ebef;
    --muted: #9aa1ad;
    --border: #2e333c;
    --accent: #2bb3a3;
    --accent-text: #08201d;
    --neg: #ff6b6b;
    --pos: #5fd38a;
    --section: #17302d;
    --total: #20242b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
.num, td input, .card .v { font-variant-numeric: tabular-nums; }
.neg { color: var(--neg) !important; }

.top {
  position: sticky; top: 0; z-index: 5;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: calc(env(safe-area-inset-top) + 10px) 16px 0;
}
.top h1 { margin: 0 0 8px; font-size: 1.25rem; }
.tabs { display: flex; position: relative; }
.tabs button {
  flex: 1; border: 0; background: none; color: var(--muted);
  font: inherit; font-weight: 600; padding: 10px 0 13px; cursor: pointer; transition: color .25s;
}
.tabs button[aria-selected="true"] { color: var(--accent); }
.tab-ink {
  position: absolute; left: 0; bottom: 0; width: 50%; height: 3px; border-radius: 3px 3px 0 0;
  background: var(--accent); transition: transform .3s cubic-bezier(.2, .8, .2, 1);
}
.tab-ink.dragging { transition: none; }

main { padding: 16px; max-width: 1100px; margin: 0 auto; }

.cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px; }
.card .k { color: var(--muted); font-size: .85rem; }
.card .v { font-size: 1.35rem; font-weight: 700; margin-top: 2px; }

.param { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; color: var(--muted); font-size: .95rem; }
.param input { width: 6rem; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Tableau : 1re colonne figée, défilement horizontal */
.table-wrap {
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
table { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
th, td { padding: 4px 6px; border-bottom: 1px solid var(--border); white-space: nowrap; text-align: right; }
th:first-child, td:first-child {
  position: sticky; left: 0; z-index: 1; text-align: left;
  background: var(--surface); border-right: 1px solid var(--border);
  max-width: 44vw; overflow: hidden; text-overflow: ellipsis;
}
thead th { background: var(--surface-2); font-size: .85rem; padding: 0; }
thead th:first-child { background: var(--surface-2); padding: 8px; }
thead button.month {
  border: 0; background: none; font: inherit; font-weight: 600; color: var(--text);
  padding: 8px 6px; width: 100%; text-align: right; cursor: pointer;
}
thead button.month small { display: block; color: var(--muted); font-weight: 400; }
tr.section td { background: var(--section); font-weight: 700; font-size: .85rem; text-transform: uppercase; letter-spacing: .04em; padding: 8px; }
tr.section td:first-child { background: var(--section); }
tr.total td, tr.total td:first-child { background: var(--total); font-weight: 700; }
tr.res td { font-weight: 700; }
td.calc { padding-right: 12px; }
td input {
  width: 6.5rem; text-align: right; border-color: transparent; background: transparent; padding: 7px 6px;
}
td input:hover { border-color: var(--border); }
button.line {
  border: 0; background: none; color: inherit; font: inherit; text-align: left;
  padding: 6px 2px; cursor: pointer; max-width: 100%; overflow: hidden; text-overflow: ellipsis;
}
button.line .note-dot { color: var(--accent); margin-left: 4px; }
.note-text { display: block; font-size: .75rem; color: var(--muted); font-weight: 400; white-space: normal; max-width: 40vw; }
button.add, button.add-month {
  border: 1px dashed var(--border); background: none; color: var(--accent); font: inherit; font-size: .9rem;
  border-radius: 8px; padding: 6px 10px; cursor: pointer;
}
button.add-month { margin: 6px; }
.hint { color: var(--muted); font-size: .85rem; }

/* Reventes */
#resale-list { display: grid; gap: 10px; margin-top: 12px; }
@media (min-width: 700px) { #resale-list { grid-template-columns: 1fr 1fr; } }
.resale {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px;
  text-align: left; font: inherit; color: inherit; cursor: pointer; width: 100%;
}
.resale .row1 { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.resale .name { font-weight: 700; }
.badge { font-size: .75rem; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); white-space: nowrap; }
.badge.Vendu { background: var(--pos); color: var(--surface); }
.badge.En.vente { background: var(--accent); color: var(--accent-text); }
.resale dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 8px 0 0; font-size: .9rem; }
.resale dt { color: var(--muted); }
.resale dd { margin: 0; text-align: right; }
.resale .notes { margin: 8px 0 0; font-size: .85rem; color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 20px; }

/* Boutons */
.btn {
  font: inherit; font-weight: 600; border-radius: 10px; padding: 10px 14px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--neg); }
.btn.wide { width: 100%; }

/* Fenêtres */
dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(520px, calc(100vw - 24px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog form { padding: 18px; display: grid; gap: 12px; }
dialog h2 { margin: 0; font-size: 1.1rem; }
dialog label { display: grid; gap: 4px; font-size: .9rem; color: var(--muted); }
dialog label input, dialog label select, dialog label textarea { width: 100%; }
dialog output { font-size: 1rem; font-weight: 700; color: var(--text); padding: 8px 0; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.grow { flex: 1; }

#toast {
  position: fixed; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; }
