:root {
  --navy: #2b2b69;
  --navy-deep: #1b1b44;
  --accent: #e67e22;

  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #eef0f7;
  --text: #1d1f33;
  --muted: #5d6178;
  --border: #dfe2ec;
  --brand: var(--navy);
  --brand-contrast: #ffffff;
  --mark: #ffe08a;
  --mark-text: #1d1f33;
  --focus: #4d6bff;
  --shadow: 0 1px 2px rgb(20 22 60 / 6%), 0 2px 8px rgb(20 22 60 / 5%);
  --radius: 10px;
  --mono: ui-monospace, 'SF Mono', 'Cascadia Mono', 'Roboto Mono', Menlo, Consolas, monospace;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12131f;
    --surface: #1b1d2d;
    --surface-2: #24273b;
    --text: #e8e9f3;
    --muted: #a3a7be;
    --border: #2f3350;
    --brand: #9fa6ff;
    --brand-contrast: #12131f;
    --mark: #6b5310;
    --mark-text: #fff4d1;
    --focus: #8ea0ff;
    --shadow: none;
    color-scheme: dark;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

/* L'attribut hidden doit toujours gagner, même face à un `display` posé
   par une règle de même spécificité plus bas dans la feuille de style. */
[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  min-height: 100vh;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

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

code {
  font-family: var(--mono);
  font-size: 0.95em;
}

mark {
  background: var(--mark);
  color: var(--mark-text);
  border-radius: 3px;
  padding: 0 1px;
}

button {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

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

.skip {
  position: absolute;
  left: -9999px;
}
.skip:focus {
  left: 16px;
  top: 8px;
  z-index: 10;
  background: var(--surface);
  padding: 8px 12px;
  border-radius: var(--radius);
}

.wrap {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 16px;
}

.muted {
  color: var(--muted);
}

/* ---------- En-tête ---------- */

.site-header {
  background: var(--navy);
  color: #fff;
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 20px;
  padding-top: 20px;
  padding-bottom: 16px;
}

.logo {
  flex: none;
  display: block;
  background: #fff;
  border-radius: 8px;
  padding: 4px 6px;
  line-height: 0;
}

.logo img {
  display: block;
  height: 47px;
  width: auto;
}

.titles h1 {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

.titles p {
  margin: 4px 0 0;
  color: #c9cbef;
  font-size: 0.92rem;
}

.tabs {
  display: flex;
  gap: 4px;
}

.tabs button {
  border: 0;
  background: transparent;
  color: #c9cbef;
  padding: 10px 16px;
  border-radius: 8px 8px 0 0;
  cursor: pointer;
  font-weight: 500;
}

.tabs button:hover {
  color: #fff;
  background: rgb(255 255 255 / 8%);
}

.tabs button.active {
  background: var(--bg);
  color: var(--text);
}

@media (max-width: 640px) {
  .header-inner {
    gap: 12px;
    padding-top: 14px;
  }
  .logo img {
    height: 36px;
  }
  .titles h1 {
    font-size: 1.15rem;
  }
  .titles p {
    display: none;
  }
  .tabs button {
    flex: 1;
    padding: 10px 8px;
    font-size: 0.9rem;
  }
}

main {
  flex: 1;
  padding-top: 24px;
  padding-bottom: 48px;
}

/* ---------- Barre de recherche ---------- */

.searchbar {
  position: relative;
  display: flex;
  align-items: center;
}

.searchbar-icon {
  position: absolute;
  left: 16px;
  width: 22px;
  height: 22px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-linecap: round;
  pointer-events: none;
}

.searchbar input {
  width: 100%;
  font: inherit;
  font-size: 1.15rem;
  padding: 14px 48px 14px 50px;
  border: 1.5px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  -webkit-appearance: none;
  appearance: none;
}

.searchbar input::-webkit-search-cancel-button {
  display: none;
}

.searchbar input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 20%, transparent);
}

.searchbar.small input {
  font-size: 1rem;
  padding: 10px 40px 10px 44px;
}
.searchbar.small .searchbar-icon {
  left: 14px;
  width: 18px;
  height: 18px;
}

.clear {
  position: absolute;
  right: 8px;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}
.clear:hover {
  background: var(--surface-2);
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  margin: 14px 0 6px;
}

.filters > label {
  flex: 1 1 220px;
  min-width: 0;
  max-width: 420px;
}

select {
  width: 100%;
  font: inherit;
  font-size: 0.92rem;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  max-width: 100%;
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.segmented label {
  padding: 6px 12px;
  font-size: 0.92rem;
  cursor: pointer;
  color: var(--muted);
}

.segmented label + label {
  border-left: 1px solid var(--border);
}

.segmented label.active {
  background: var(--brand);
  color: var(--brand-contrast);
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented label:has(input:focus-visible) {
  outline: 2px solid var(--focus);
  outline-offset: -2px;
}

.count {
  margin: 14px 0 10px;
  color: var(--muted);
}
.count strong {
  color: var(--text);
}
.note {
  font-size: 0.92rem;
}

.empty {
  margin-top: 28px;
  color: var(--muted);
}

.examples {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 4px 12px;
  cursor: pointer;
  font-size: 0.9rem;
}
.chip:hover {
  border-color: var(--brand);
  color: var(--brand);
}

/* ---------- Résultats ---------- */

.results {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.result {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.result header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
}

.result .code {
  font-family: var(--mono);
  font-size: 1.08rem;
  font-weight: 700;
  color: var(--brand);
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
.result .code:hover {
  text-decoration: underline;
}

.famille-tag {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--surface-2);
  padding: 2px 8px;
  border-radius: 999px;
}

.geo {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
}

.result .path {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  padding: 0;
  margin: 6px 0 0;
  cursor: pointer;
  line-height: 1.45;
}

.path .sep {
  color: var(--muted);
  margin: 0 6px;
}

.path .step {
  color: var(--muted);
}

.path .step:last-child {
  color: var(--text);
  font-weight: 600;
}

.result footer {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  font-size: 0.88rem;
  color: var(--muted);
}

.nchfs abbr {
  text-decoration: none;
  font-weight: 600;
  font-size: 0.75rem;
  margin-right: 4px;
}

.nchfs code,
.compte code {
  color: var(--text);
  font-weight: 600;
}

.nchfs-label {
  margin-left: 4px;
}

.more {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 16px;
}

.atc-line {
  margin: 6px 0 0;
  font-size: 0.86rem;
  color: var(--muted);
}
.atc-line abbr,
.atc-code abbr {
  text-decoration: none;
  font-weight: 600;
  font-size: 0.75rem;
  margin-right: 4px;
}
.atc-line code {
  color: var(--text);
  font-weight: 600;
}
.atc-line .sep {
  margin: 0 6px;
}

.result.orphan {
  border-style: dashed;
}
.atc-code code {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--brand);
}
.molecule {
  font-weight: 600;
}
.orphan-note {
  margin: 6px 0 0;
  font-size: 0.92rem;
}
.parent-code {
  font-weight: 700;
  color: var(--brand);
}

.atc-list h3 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 18px 0 6px;
}
.atc-list ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.atc-list li {
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}

/* ---------- Boutons ---------- */

.btn {
  border: 1px solid var(--brand);
  background: var(--brand);
  color: var(--brand-contrast);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
}
.btn:hover {
  filter: brightness(1.1);
}
.btn.small {
  padding: 4px 10px;
  font-size: 0.85rem;
}
.btn.ghost {
  background: transparent;
  color: var(--brand);
  border-color: var(--border);
}

.copy {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  padding: 3px 5px;
  cursor: pointer;
  font-size: 0.78rem;
  vertical-align: middle;
}
.copy svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.copy:hover {
  border-color: var(--border);
  color: var(--brand);
}
.copy.ok {
  color: #1f8a4c;
}

/* ---------- Arborescence ---------- */

.tree-toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
}
.tree-toolbar .searchbar {
  flex: 1;
}

.tree-root,
.tree ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tree-root {
  margin-top: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  box-shadow: var(--shadow);
}

.tree ul[role='group'] {
  margin-left: 13px;
  padding-left: 10px;
  border-left: 1px solid var(--border);
}

.node {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.node:hover {
  background: var(--surface-2);
}

.chevron {
  flex: none;
  width: 16px;
  height: 16px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 2.5;
  transition: transform 0.12s;
}
.chevron.open {
  transform: rotate(90deg);
}

.node .level {
  flex: none;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  width: 86px;
}

.node .label {
  flex: 1;
  min-width: 0;
}

.depth-0 > .node .label {
  font-weight: 700;
}

.badge {
  flex: none;
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  background: var(--surface-2);
  color: var(--muted);
  padding: 1px 8px;
  border-radius: 999px;
}

.inline-code {
  flex: none;
  font-family: var(--mono);
  font-size: 0.88rem;
  color: var(--brand);
  font-weight: 700;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
.inline-code:hover,
.linkish:hover {
  text-decoration: underline;
}
.linkish {
  background: none;
  border: 0;
  padding: 0;
  text-align: left;
  cursor: pointer;
}
.leaf-node > .node {
  cursor: default;
}
.leaf-node.flash > .node {
  animation: flash 2.4s ease-out;
}
.dot {
  flex: none;
  width: 16px;
  display: flex;
  justify-content: center;
}
.dot::before {
  content: '';
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--border);
}

.leaf {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px 3px 32px;
  border-radius: 6px;
  font-size: 0.92rem;
}
.leaf.flash {
  animation: flash 2.4s ease-out;
}
@keyframes flash {
  0%,
  40% {
    background: var(--mark);
  }
  100% {
    background: transparent;
  }
}

.leaf-code {
  font-family: var(--mono);
  font-weight: 700;
  color: var(--brand);
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
.leaf-code:hover {
  text-decoration: underline;
}

.leaf-nchfs {
  color: var(--muted);
  font-size: 0.82rem;
}

@media (max-width: 640px) {
  .node .level {
    display: none;
  }
  .tree ul[role='group'] {
    margin-left: 8px;
    padding-left: 6px;
  }
  .leaf {
    padding-left: 24px;
  }
}

/* ---------- Fiche détail ---------- */

dialog.detail {
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgb(0 0 0 / 30%);
}
dialog.detail::backdrop {
  background: rgb(15 16 40 / 55%);
}

.detail-body {
  padding: 20px 22px;
}

.detail header {
  position: relative;
  padding-right: 40px;
}

.eyebrow {
  margin: 0;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.detail h2 {
  margin: 2px 0 0;
  font-size: 1.6rem;
  color: var(--brand);
}

.close {
  position: absolute;
  top: -6px;
  right: -8px;
  width: 38px;
  height: 38px;
  border: 0;
  background: transparent;
  font-size: 1.7rem;
  border-radius: 50%;
  cursor: pointer;
  color: var(--muted);
}
.close:hover {
  background: var(--surface-2);
}

.hierarchy {
  list-style: none;
  margin: 16px 0;
  padding: 0;
}

.hierarchy li {
  display: flex;
  gap: 10px;
  padding: 5px 0;
  border-bottom: 1px solid var(--border);
}

.hierarchy .level {
  flex: none;
  width: 130px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding-top: 3px;
}

.hierarchy li:last-child span:last-child {
  font-weight: 600;
}

.fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 20px;
  margin: 0;
}
.fields .wide {
  grid-column: 1 / -1;
}
.fields dt {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.fields dd {
  margin: 2px 0 0;
}

.detail footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
}

@media (max-width: 640px) {
  .hierarchy li {
    flex-direction: column;
    gap: 0;
  }
  .hierarchy .level {
    width: auto;
  }
  .fields {
    grid-template-columns: 1fr;
  }
}

/* ---------- États ---------- */

.loading {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  padding: 40px 0;
}

.spinner {
  width: 22px;
  height: 22px;
  border: 3px solid var(--border);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.error {
  background: var(--surface);
  border: 1px solid #d64545;
  border-radius: var(--radius);
  padding: 16px;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ---------- Pied de page ---------- */

.site-footer {
  border-top: 1px solid var(--border);
  padding: 18px 0 28px;
  font-size: 0.85rem;
}

.versions summary {
  cursor: pointer;
}

.versions table {
  margin-top: 10px;
  border-collapse: collapse;
  font-size: 0.85rem;
  width: 100%;
}
.versions th,
.versions td {
  text-align: left;
  padding: 6px 10px 6px 0;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
