:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --ink: #1f2328;
  --ink-2: #4a5059;
  --muted: #6b7280;
  --line: #e2dfd7;
  --accent: #1d5c63;
  --accent-ink: #ffffff;
  --accent-soft: #e3efef;
  --ok: #2e6b3a;
  --ok-soft: #e4f1e6;
  --warn: #8a5a00;
  --warn-soft: #fbf0d9;
  --danger: #a3302a;
  --danger-soft: #f9e3e1;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(31, 35, 40, .06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1d2024;
    --surface-2: #24282d;
    --ink: #e9e7e2;
    --ink-2: #c3c0b9;
    --muted: #9a9890;
    --line: #33373d;
    --accent: #6fb3b8;
    --accent-ink: #0f2a2c;
    --accent-soft: #1f3a3c;
    --ok: #8fcf9a;
    --ok-soft: #1f3324;
    --warn: #e7c06b;
    --warn-soft: #3a2f17;
    --danger: #f0938c;
    --danger-soft: #3d201e;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { font-size: 15px; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
  display: grid;
  grid-template-columns: 210px 1fr;
  grid-template-rows: auto 1fr;
  min-height: 100vh;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.55rem; margin: 0 0 .4rem; font-weight: 650; letter-spacing: -.01em; }
h1 a { color: inherit; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; font-weight: 600; }
h2 small, h1 small { color: var(--muted); font-weight: 400; font-size: .85rem; margin-left: .35rem; }
h3 { font-size: .95rem; margin: 1rem 0 .4rem; }
.muted { color: var(--muted); }
.notes { white-space: pre-wrap; background: var(--surface-2); padding: .6rem .8rem; border-radius: 8px; }

/* Structure */
.topbar {
  grid-column: 1 / -1;
  display: flex; align-items: center; gap: 1.5rem;
  padding: .6rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; color: var(--ink); font-size: 1.05rem; display: flex; flex-direction: column; line-height: 1.1; min-width: 170px; }
.brand small { font-weight: 400; color: var(--muted); font-size: .75rem; letter-spacing: .08em; text-transform: uppercase; }
.brand:hover { text-decoration: none; }
.search { flex: 1; max-width: 520px; }
.search input { width: 100%; }
.user { margin-left: auto; display: flex; gap: .75rem; align-items: center; color: var(--ink-2); }
.sidebar {
  padding: 1rem .6rem;
  display: flex; flex-direction: column; gap: 2px;
  border-right: 1px solid var(--line);
  background: var(--surface);
}
.sidebar a { padding: .5rem .75rem; border-radius: 8px; color: var(--ink-2); }
.sidebar a:hover { background: var(--surface-2); text-decoration: none; }
.sidebar a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.content { padding: 1.5rem 1.75rem 3rem; min-width: 0; max-width: 1280px; }
.content.bare { grid-column: 1 / -1; grid-row: 1 / -1; display: grid; place-items: center; max-width: none; }

.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.buttons { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
  min-width: 0;
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }
.grid2 > .card, .grid3 > .card { margin-bottom: 1rem; }

/* Tuiles */
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin: 1rem 0; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; display: flex; flex-direction: column; gap: .2rem; box-shadow: var(--shadow); }
.tile span { color: var(--muted); font-size: .85rem; }
.tile strong { font-size: 1.6rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.tile small { color: var(--muted); }
.stats p { margin: .3rem 0; }

/* Formulaires */
input, select, textarea, button, .button {
  font: inherit;
  color: var(--ink);
}
input, select, textarea {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 7px;
  padding: .45rem .6rem;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--ink-2); }
label.inline { flex-direction: row; align-items: center; gap: .4rem; }
label.check { flex-direction: row; align-items: center; gap: .45rem; font-size: .95rem; color: var(--ink); display: inline-flex; margin-right: 1.25rem; }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: .6rem .9rem; }
legend { color: var(--ink-2); font-size: .85rem; padding: 0 .3rem; }
.form-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: .85rem 1rem; }
.form-grid .span2 { grid-column: span 2; }
.form-grid .span3 { grid-column: 1 / -1; }
.form-row { display: flex; gap: .6rem; align-items: flex-end; flex-wrap: wrap; margin-top: .75rem; }
.form-row label { flex: 1; min-width: 110px; }
.actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; }
button, .button {
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: .45rem .9rem;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none;
}
button:hover, .button:hover { background: var(--surface-2); text-decoration: none; }
.primary, button.primary, .button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.primary:hover, .button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.link, .link { background: none; border: none; padding: 0; color: var(--accent); cursor: pointer; }
button.link:hover { text-decoration: underline; background: none; }
form.inline { display: inline; }
.danger-zone { margin-top: 1.5rem; }
.danger-zone .link, .danger-zone button { color: var(--danger); }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .75rem; align-items: center; }
.filters input[type=search] { flex: 1; min-width: 220px; max-width: 420px; }

/* Tableaux */
table { border-collapse: collapse; width: 100%; }
.list th, .list td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.list thead th { font-size: .8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.list tbody tr:hover { background: var(--surface-2); }
.list tfoot td { border-bottom: none; padding: .3rem .6rem; }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.due { color: var(--danger); font-weight: 600; }
.inactive td { color: var(--muted); }
.inactive a { color: var(--muted); }
.ellipsis { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 2rem; }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 1rem; color: var(--muted); }
.card > .list { margin: 0 -.6rem; width: calc(100% + 1.2rem); }

/* Tableau de mesures */
.rx { width: auto; min-width: 340px; margin: .5rem 0; }
.rx th, .rx td { padding: .35rem .75rem; border-bottom: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
.rx thead th { font-size: .8rem; color: var(--muted); font-weight: 600; }
.rx tbody th { text-align: left; font-weight: 500; color: var(--ink-2); }
.rx-form input { width: 120px; text-align: right; }
.rx-form td[colspan] input { width: 100%; text-align: left; }

dl { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; margin: 0; }
dt { color: var(--muted); }
dd { margin: 0; }
dl.inline { margin-bottom: .75rem; }

.badge { display: inline-block; font-size: .75rem; padding: .1rem .5rem; border-radius: 999px; font-weight: 600; vertical-align: middle; }
.badge-neutral { background: var(--surface-2); color: var(--ink-2); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-muted { background: var(--surface-2); color: var(--muted); text-decoration: line-through; }

.flash { background: var(--ok-soft); color: var(--ok); padding: .6rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-weight: 500; }
.error { background: var(--danger-soft); color: var(--danger); padding: .5rem .8rem; border-radius: 7px; }

/* Lignes de vente */
.lines th { font-size: .8rem; color: var(--muted); text-align: left; padding: .4rem; font-weight: 600; }
.lines td { padding: .3rem .4rem; vertical-align: top; }
.lines input, .lines select { width: 100%; }
.lines .qty { width: 70px; }
.lines .price { width: 110px; }
.lines .discount { width: 80px; }
.product-cell { position: relative; min-width: 280px; }
.suggestions {
  position: absolute; left: .4rem; right: .4rem; top: 100%; z-index: 10;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.12);
  max-height: 300px; overflow: auto;
}
.suggestions button { display: flex; width: 100%; justify-content: space-between; gap: 1rem; border: 0; border-radius: 0; text-align: left; padding: .45rem .7rem; }
.suggestions button small { color: var(--muted); }
.line-total { padding-top: .75rem !important; }
.pick { list-style: none; padding: 0; }
.pick li { padding: .4rem 0; border-bottom: 1px solid var(--line); display: flex; gap: 1rem; justify-content: space-between; }

/* Graphique */
.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .baseline { stroke: var(--ink-2); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .bar path { fill: var(--accent); }
.chart .bar .hit { fill: transparent; }
.chart .bar .value { fill: var(--ink-2); font-size: 11px; opacity: 0; }
.chart .bar:hover .value, .chart .bar:focus .value { opacity: 1; }
.chart .bar:hover path, .chart .bar:focus path { filter: brightness(1.15); }
.chart .bar:focus { outline: none; }

/* Connexion */
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 2rem; width: min(380px, 92vw); box-shadow: var(--shadow); }
.login-card form { display: flex; flex-direction: column; gap: .9rem; margin-top: 1rem; }

/* Tablettes et téléphones */
@media (pointer: coarse) {
  input, select, textarea { font-size: 16px; } /* évite le zoom automatique sur iPhone/iPad */
  button, .button, input, select { min-height: 42px; }
  .sidebar a { padding: .65rem .85rem; }
  .list td, .list th { padding-top: .65rem; padding-bottom: .65rem; }
}

@media (max-width: 1024px) {
  body { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; gap: .5rem 1rem; padding: .5rem 1rem; padding-top: max(.5rem, env(safe-area-inset-top)); }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .sidebar {
    flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--line);
    padding: .4rem .6rem; position: sticky; top: 0; z-index: 4; scrollbar-width: none;
  }
  .sidebar a { white-space: nowrap; }
  .content { padding: 1.1rem; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .grid3 { grid-template-columns: 1fr 1fr; }
  .grid3 > .card:last-child { grid-column: 1 / -1; }
  .card > .list { margin: 0; width: 100%; }
}

@media (max-width: 700px) {
  html { font-size: 15px; }
  .brand { min-width: 0; }
  .user { font-size: .9rem; }
  h1 { font-size: 1.3rem; }
  .content { padding: .9rem .75rem 3rem; }
  .tiles, .grid2, .grid3, .form-grid { grid-template-columns: 1fr; }
  .tile strong { font-size: 1.35rem; }
  .form-grid .span2 { grid-column: auto; }
  .page-head .buttons { width: 100%; }
  .page-head .buttons > * { flex: 1 1 auto; }
  .card { padding: .9rem; }
  .hide-sm { display: none; }
  .filters input[type=search] { min-width: 0; max-width: none; flex-basis: 100%; }
  .filters > * { flex: 1 1 auto; }
  .ellipsis { max-width: 160px; }
  .rx { min-width: 0; width: 100%; }
  .rx-form input { width: 100%; }
  .pager { font-size: .85rem; }

  /* Lignes de vente empilées */
  .lines thead { display: none; }
  .lines, .lines tbody, .lines tfoot { display: block; }
  .lines tr.line {
    display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .4rem;
    border: 1px solid var(--line); border-radius: 8px; padding: .6rem; margin-bottom: .6rem; position: relative;
  }
  .lines tr.line td { padding: 0; display: block; }
  .lines tr.line td[data-label]::before { content: attr(data-label); display: block; font-size: .75rem; color: var(--muted); margin-bottom: .15rem; }
  .lines tr.line td:first-child { grid-column: 1 / -1; padding-right: 2rem; }
  .lines tr.line .product-cell { grid-column: 1 / -1; min-width: 0; }
  .lines tr.line td:last-child { position: absolute; top: .7rem; right: .6rem; }
  .lines .qty, .lines .price, .lines .discount { width: 100%; }
  .lines .line-total { grid-column: 1 / -1; text-align: right; padding: 0 !important; font-weight: 600; }
  .lines tfoot tr { display: flex; justify-content: space-between; align-items: center; }
  .lines tfoot td { display: block; }
  .lines tfoot td:empty { display: none; }
}

@media print {
  .topbar, .sidebar, .buttons, .danger-zone, .actions { display: none !important; }
  body { display: block; background: #fff; }
  .card { border: none; box-shadow: none; }
}

/* Formulaires empilés, code de sécurité */
form.stack { display: flex; flex-direction: column; gap: .75rem; }
.twofa ol { padding-left: 1.2rem; }
.twofa li { margin-bottom: .9rem; }
.qr { width: 220px; max-width: 100%; margin: .6rem 0; background: #fff; border-radius: 8px; }
.qr svg { display: block; width: 100%; height: auto; }
code { background: var(--surface-2); padding: .1rem .35rem; border-radius: 4px; font-size: .95em; letter-spacing: .05em; }

/* Fenêtres */
.dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 1.4rem 1.5rem; width: min(520px, 94vw);
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.dialog::backdrop { background: rgba(20, 22, 26, .45); }
.dialog form { display: flex; flex-direction: column; gap: .9rem; }
.dialog h2 { margin: 0; }
.pay-lines th { text-align: left; font-size: .8rem; color: var(--muted); font-weight: 600; padding: .2rem .3rem; }
.pay-lines td { padding: .25rem .3rem; }
.pay-lines input, .pay-lines select { width: 100%; }
.pay-check { color: var(--ink-2); font-size: .9rem; padding-top: .5rem !important; }

/* Hauteur de montage */
.mount-heights { margin: 1rem 0; }
.mount-heights h3 { margin-top: 0; }
.mount-heights h3 small { color: var(--muted); font-weight: 400; }
.mh { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.mh-side { position: relative; display: flex; flex-direction: column; gap: .25rem; }
.mh-label { font-size: .85rem; color: var(--ink-2); }
.mh-value { justify-content: space-between; font-size: 1.1rem; font-weight: 600; min-height: 44px; }
.mh-pop {
  position: absolute; top: 100%; left: 0; z-index: 20; width: min(440px, 92vw); margin-top: .3rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: .7rem; box-shadow: 0 10px 30px rgba(0,0,0,.18);
}
.mh-side[data-side="l"] .mh-pop { left: auto; right: 0; }
.grid-h { display: grid; grid-template-columns: repeat(9, 1fr); gap: .3rem; }
.hc { min-height: 42px; padding: 0; font-variant-numeric: tabular-nums; }
.hc.sel { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.mh-other { display: flex; gap: .5rem; align-items: center; margin-top: .6rem; flex-wrap: wrap; }
.mh-other input { width: 110px; }
@media (max-width: 700px) {
  .grid-h { grid-template-columns: repeat(7, 1fr); }
  .mh-pop { width: calc(200% + 1rem); }
}

[hidden] { display: none !important; }

/* Lien discret sur un titre + point rouge (remarque client) */
.title-link { background: none; border: 0; padding: 0; font: inherit; color: inherit; cursor: pointer; display: inline-flex; align-items: center; gap: .4rem; min-height: 0; }
.title-link:hover { background: none; text-decoration: underline dotted; text-underline-offset: 3px; }
.note-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); display: inline-block; }
.note-form { margin-top: 1rem; border-top: 1px solid var(--line); padding-top: .8rem; }
.note-form textarea { width: 100%; }
