:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #efede7;
  --border: #e2dfd6;
  --text: #1b1c21;
  --muted: #6d6b66;
  --accent: #c8102e;
  --accent-hover: #a90d26;
  --accent-ink: #ffffff;
  --accent-soft: rgba(200, 16, 46, 0.09);
  --ok: #1d7a46;
  --warn-bg: #fff5d9;
  --warn-ink: #6b4e00;
  --shadow: 0 1px 2px rgba(20, 20, 30, 0.06), 0 6px 18px rgba(20, 20, 30, 0.06);
  --radius: 14px;
  --radius-sm: 9px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111217;
    --surface: #1a1c23;
    --surface-2: #22252e;
    --border: #2d303a;
    --text: #edece8;
    --muted: #9b9ba4;
    --accent: #e5364f;
    --accent-hover: #f04d64;
    --accent-soft: rgba(229, 54, 79, 0.14);
    --ok: #4cc485;
    --warn-bg: #3a3014;
    --warn-ink: #f3d68a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2 { margin: 0; line-height: 1.2; letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.15rem; }
.muted { color: var(--muted); }
small { font-size: 0.82em; }

/* ---------- Boutons & champs ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.45em;
  min-height: 40px; padding: 0 16px;
  border: 1px solid transparent; border-radius: 999px;
  font: inherit; font-weight: 600; text-decoration: none; white-space: nowrap;
  cursor: pointer; transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.btn-sm { min-height: 32px; padding: 0 12px; font-size: 0.88rem; }
.btn-block { width: 100%; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { border-color: var(--muted); }
.btn-danger-ghost { background: transparent; color: var(--accent); border-color: transparent; }
.btn-danger-ghost:hover { background: var(--accent-soft); }
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn:focus-visible, .icon-btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .tree button:focus-visible, .card:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; flex: none;
  border: 1px solid var(--border); border-radius: 50%;
  background: var(--surface); color: var(--text); font: inherit; font-size: 1rem; cursor: pointer;
}
.icon-btn:hover { border-color: var(--muted); }

input, select, textarea {
  width: 100%; min-height: 42px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font: inherit;
}
textarea { resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 0.8; }

.field { display: grid; gap: 5px; min-width: 0; }
.field > span { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.stack { display: grid; gap: 14px; }
.hint { margin: -6px 0 0; font-size: 0.82rem; color: var(--muted); }
.form-error { margin: 0; color: var(--accent); font-weight: 600; min-height: 0; }
.form-error:empty { display: none; }
.notice {
  margin: 0; padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--warn-bg); color: var(--warn-ink); font-size: 0.9rem;
}
.notice.is-ok { background: rgba(29, 122, 70, 0.12); color: var(--ok); }

/* ---------- Marque ---------- */

.brand { display: inline-flex; align-items: center; gap: 10px; white-space: nowrap; font-weight: 800; font-size: 1.1rem; letter-spacing: -0.01em; }
.brand-mark {
  display: inline-grid; place-items: center; width: 32px; height: 32px;
  border-radius: 9px; background: var(--accent); color: #fff; font-size: 1.15rem;
}
.brand-lg { font-size: 1.5rem; }
.brand-lg .brand-mark { width: 42px; height: 42px; font-size: 1.5rem; border-radius: 12px; }

/* ---------- Authentification ---------- */

.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: min(420px, 100%); padding: 32px 28px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px; box-shadow: var(--shadow);
}
.auth-tagline { margin: 10px 0 22px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; padding: 4px; margin-bottom: 20px; background: var(--surface-2); border-radius: 999px; }
.tab { min-height: 36px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); font: inherit; font-weight: 600; cursor: pointer; }
.tab.is-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- Structure ---------- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 24px; padding-top: max(12px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.topbar-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 180px; margin-right: 4px; }

.layout { display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 28px; max-width: 1400px; margin: 0 auto; padding: 24px; }

.sidebar { position: sticky; top: 84px; align-self: start; max-height: calc(100dvh - 100px); overflow: auto; }
.sidebar-title { margin: 0 0 8px 10px; font-size: 0.78rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { margin: 2px 0 6px 14px; padding-left: 8px; border-left: 1px solid var(--border); }
.tree button {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 7px 10px; border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.tree button:hover { background: var(--surface-2); }
.tree button.is-active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.tree .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .count { font-size: 0.8rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.tree .top > button { font-weight: 600; }

.content { min-width: 0; }
.content-head { display: grid; gap: 6px; margin-bottom: 18px; }
.crumbs { font-size: 0.85rem; color: var(--muted); min-height: 1.2em; }
.crumbs button { padding: 0; border: 0; background: none; color: inherit; font: inherit; cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.title-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.stats { display: flex; flex-wrap: wrap; gap: 6px 18px; color: var(--muted); font-size: 0.92rem; }
.stats strong { color: var(--text); font-variant-numeric: tabular-nums; }
.series-info { margin: 4px 0 0; font-size: 0.92rem; }
.series-info .missing { color: var(--accent); font-weight: 600; }

.toolbar { display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap; }
.toolbar input[type="search"] { flex: 1 1 260px; border-radius: 999px; padding-left: 16px; }
.toolbar select { width: auto; flex: 0 0 auto; border-radius: 999px; }

/* ---------- Grille ---------- */

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 16px; }
.card {
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  cursor: pointer; text-align: left; color: inherit; font: inherit; padding: 0;
  transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow); border-color: color-mix(in srgb, var(--border) 60%, var(--muted)); }
.card-media { position: relative; aspect-ratio: 3 / 4; background: var(--surface-2); display: grid; place-items: center; overflow: hidden; }
.card-media img { width: 100%; height: 100%; object-fit: contain; padding: 10px; }
.card-media img.is-photo { object-fit: cover; padding: 0; }
.card-placeholder { font-size: 2.6rem; color: var(--muted); opacity: 0.55; }
.badge {
  position: absolute; padding: 2px 9px; border-radius: 999px;
  font-size: 0.78rem; font-weight: 700; font-variant-numeric: tabular-nums;
}
.badge-volume { top: 8px; left: 8px; background: var(--text); color: var(--bg); }
.badge-qty { top: 8px; right: 8px; background: var(--accent); color: #fff; }
.card-body { display: grid; gap: 3px; padding: 10px 12px 12px; }
.card-title {
  font-weight: 650; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card-meta { font-size: 0.82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip {
  justify-self: start; margin-top: 4px; padding: 1px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); font-size: 0.75rem; font-weight: 600;
}

.empty { text-align: center; padding: 56px 16px; max-width: 460px; margin: 0 auto; }
.empty-icon { font-size: 3rem; color: var(--muted); }
.empty h2 { margin: 8px 0 6px; }
.empty p { margin: 0 0 18px; }

.fab {
  position: fixed; right: 18px; bottom: max(18px, env(safe-area-inset-bottom));
  display: none; place-items: center; width: 62px; height: 62px;
  border: 0; border-radius: 50%; background: var(--accent); color: #fff;
  box-shadow: 0 8px 24px rgba(200, 16, 46, 0.4); cursor: pointer;
}

/* ---------- Dialogues ---------- */

.dialog {
  width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
  padding: 0; border: 1px solid var(--border); border-radius: 18px;
  background: var(--surface); color: var(--text); box-shadow: 0 24px 60px rgba(0, 0, 0, 0.3);
}
.dialog-wide { width: min(860px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(10, 10, 15, 0.55); backdrop-filter: blur(2px); }
.dialog[open] { display: flex; flex-direction: column; }
.dialog > form { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.dialog-body { padding: 18px 20px; overflow: auto; flex: 1; min-height: 0; }
.dialog-foot { display: flex; align-items: center; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.spacer { flex: 1; }

.item-form-grid { display: grid; grid-template-columns: 200px minmax(0, 1fr); gap: 22px; }
.fields-col { display: grid; gap: 14px; align-content: start; }
.photo-col { display: grid; gap: 8px; align-content: start; }
.photo-preview {
  aspect-ratio: 3 / 4; border-radius: var(--radius); overflow: hidden;
  display: grid; place-items: center; background: var(--surface-2); border: 1px dashed var(--border);
  color: var(--muted); font-size: 0.85rem;
}
.photo-preview img { width: 100%; height: 100%; object-fit: contain; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper input { text-align: center; min-width: 0; padding: 8px 4px; }
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; }
.file-btn { position: relative; }

/* ---------- Scanner ---------- */

.reader-wrap { display: grid; gap: 8px; }
#reader { width: 100%; min-height: 220px; border-radius: var(--radius); overflow: hidden; background: #000; }
#reader video { width: 100% !important; height: auto; display: block; }
#reader img { display: none; } /* image d'aperçu de scanFile */
.reader-hint { margin: 0; text-align: center; font-size: 0.88rem; color: var(--muted); }
.scan-alt { display: grid; gap: 10px; }
.inline-form { display: flex; gap: 8px; }
.scan-result { display: grid; gap: 10px; padding: 14px; border-radius: var(--radius); background: var(--surface-2); }
.scan-result .owned { display: flex; gap: 12px; align-items: center; }
.scan-result img { width: 54px; height: 72px; object-fit: contain; border-radius: 6px; background: var(--surface); }
.scan-result .actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Toast ---------- */

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); z-index: 100;
  transform: translateX(-50%); max-width: calc(100vw - 32px);
  padding: 11px 18px; border-radius: 999px;
  background: var(--text); color: var(--bg); font-weight: 600; box-shadow: var(--shadow);
}

/* ---------- Mobile ---------- */

@media (max-width: 820px) {
  .topbar { padding: 10px 16px; }
  .user-name { display: none; }
  .layout { grid-template-columns: minmax(0, 1fr); gap: 14px; padding: 14px 16px 100px; }
  .sidebar { position: static; max-height: none; overflow: visible; margin: 0 -16px; }
  .sidebar-title { display: none; }
  .tree { display: flex; gap: 8px; overflow-x: auto; padding: 2px 16px 6px; scrollbar-width: none; }
  .tree::-webkit-scrollbar { display: none; }
  .tree li { display: contents; }
  .tree ul { display: contents; }
  .tree button { width: auto; flex: none; padding: 6px 14px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); white-space: nowrap; }
  .tree .sub > button { font-size: 0.9rem; }
  .tree .sub > button::before { content: "› "; color: var(--muted); }
  .tree .label { overflow: visible; }
  h1 { font-size: 1.35rem; }
  .toolbar #scan-btn { display: none; }
  .toolbar #add-btn { order: 3; }
  .toolbar input[type="search"] { flex-basis: 100%; min-width: 0; }
  .toolbar select { flex: 1; min-width: 0; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
  .fab { display: grid; }
  .item-form-grid { grid-template-columns: 1fr; }
  .photo-col { grid-template-columns: 110px 1fr; align-items: start; }
  .photo-col .photo-preview { grid-row: span 3; }
  .row-3 { grid-template-columns: 1fr 1fr; }
  .row-3 > :last-child:nth-child(odd) { grid-column: span 2; }
  .dialog, .dialog-wide { width: 100vw; max-width: 100vw; max-height: 100dvh; height: 100dvh; border-radius: 0; border: 0; margin: 0; }
  .dialog-foot .btn { flex: 1; }
  .dialog-foot .spacer { display: none; }
}

@media (max-width: 420px) {
  .row-2 { grid-template-columns: 1fr; }
  .topbar .brand > span:last-child { display: none; }
  .topbar-actions .btn { padding: 0 10px; }
}
