:root {
  --fcbq-orange: #e8590c;
  --fcbq-orange-soft: #fff1e6;
  --fcbq-blue: #143d6b;
  --fcbq-blue-ink: #0b2545;
  --fcbq-red: #c92a2a;
  --ink: #212529;
  --muted: #6b7280;
  --line: #e5e7eb;
  --bg-soft: #f8fafc;
  --sidebar-w: 300px;
  --topbar-h: 64px;
  --shadow: 0 1px 3px rgba(11, 37, 69, 0.12), 0 1px 2px rgba(11, 37, 69, 0.08);
  --radius: 8px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: #fff;
  line-height: 1.6;
}

/* ---------- capçalera ---------- */
.topbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--topbar-h);
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0 1.25rem;
  background: linear-gradient(90deg, var(--fcbq-blue-ink), var(--fcbq-blue) 70%);
  color: #fff;
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.brand-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: var(--fcbq-orange);
  color: #fff;
  font-weight: 800;
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  flex: none;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.25;
}

.brand-title {
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-sub {
  font-size: 0.78rem;
  color: rgba(255, 255, 255, 0.78);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-tabs {
  display: flex;
  gap: 0.4rem;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  padding: 4px;
}

.doc-tab {
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.85);
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  padding: 0.5rem 1.1rem;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.doc-tab:hover { color: #fff; }

.doc-tab.active {
  background: var(--fcbq-orange);
  color: #fff;
}

/* ---------- disposició ---------- */
.layout {
  display: flex;
  height: 100vh;
  padding-top: var(--topbar-h);
  overflow: hidden;
}

.layout > main {
  flex: 1;
  min-width: 0;
  display: flex;
}

/* ---------- sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex: none;
  border-right: 1px solid var(--line);
  background: var(--bg-soft);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: margin-left 0.2s ease, width 0.2s ease;
}

.sidebar-inner {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.sidebar-toggle-wrap {
  display: none;
  padding: 0.5rem 0.75rem 0;
}

.sidebar-toggle {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 7px;
  padding: 0.4rem 0.75rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.sidebar-search {
  padding: 0.85rem 0.85rem 0.5rem;
  position: relative;
}

.sidebar-search input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  font: inherit;
  font-size: 0.88rem;
  background: #fff;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.sidebar-search input:focus {
  border-color: var(--fcbq-orange);
  box-shadow: 0 0 0 3px rgba(232, 89, 12, 0.15);
}

/* resultats de cerca */
.search-results {
  position: absolute;
  top: 100%;
  left: 0.85rem;
  right: 0.85rem;
  margin-top: 0.35rem;
  max-height: 62vh;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 60;
}

.search-count,
.search-msg {
  padding: 0.45rem 0.7rem;
  font-size: 0.8rem;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
}

.search-msg { color: var(--fcbq-orange); font-weight: 600; }

.search-result {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 0.55rem 0.7rem;
  cursor: pointer;
  font: inherit;
  border-bottom: 1px solid #f1f3f6;
}

.search-result:hover { background: var(--fcbq-orange-soft); }

.search-result-title {
  display: block;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--fcbq-blue-ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-result-snippet {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.35;
  margin-top: 0.15rem;
  max-height: 3.2em;
  overflow: hidden;
}

.search-hit {
  background: #ffcf4e;
  color: #212529;
  border-radius: 2px;
  padding: 0 2px;
  box-shadow: 0 0 0 1px rgba(232, 89, 12, 0.35), 0 1px 2px rgba(11, 37, 69, 0.18);
  font-weight: 600;
}

.epigraph-hit {
  background: rgba(232, 89, 12, 0.10);
  border-radius: 6px;
  box-shadow: inset 0 0 0 1px rgba(232, 89, 12, 0.22);
  transition: background-color 0.2s;
}

@keyframes flash-outline {
  0%   { box-shadow: 0 0 0 3px rgba(232,89,12,.5); }
  100% { box-shadow: none; }
}

.search-flash { animation: flash-outline 1.3s ease-out; }

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.25rem 0.5rem 1.25rem;
}

.sidebar-tree { list-style: none; margin: 0; padding: 0; }

.sidebar-tree ul {
  list-style: none;
  margin: 0;
  padding: 0 0 0 0.85rem;
  border-left: 1px solid var(--line);
  margin-left: 0.55rem;
}

.sidebar-link {
  display: block;
  padding: 0.32rem 0.6rem;
  border-radius: 6px;
  color: #374151;
  text-decoration: none;
  font-size: 0.86rem;
  line-height: 1.35;
  border-left: 2px solid transparent;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-link:hover { background: #eef2f7; color: var(--fcbq-blue-ink); }

.sidebar-link.active {
  background: var(--fcbq-orange-soft);
  color: var(--fcbq-orange);
  font-weight: 600;
  border-left-color: var(--fcbq-orange);
}

.sidebar-link.disabled {
  color: var(--muted);
  cursor: default;
  pointer-events: none;
}

.sidebar-msg {
  padding: 0.75rem 0.6rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.hidden { display: none !important; }

[hidden] { display: none !important; }

/* ---------- contingut ---------- */
.content {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.markdown-body {
  max-width: 840px;
  margin: 0 auto;
  padding: 2rem 1.75rem 4rem;
  font-size: 1rem;
}

.markdown-body:focus { outline: none; }

.markdown-body p {
  margin: 0.9em 0;
  text-align: justify;
  hyphens: auto;
}

.markdown-body ul {
  margin: 0.7em 0;
  padding-left: 1.4em;
}

.markdown-body li { margin: 0.35em 0; }

/* títols */
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  line-height: 1.3;
  scroll-margin-top: var(--topbar-h);
  font-weight: 700;
  color: var(--fcbq-blue-ink);
}

.markdown-body h1 {
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 1.55rem;
  padding: 0.6rem 0 0.7rem;
  border-bottom: 3px solid var(--fcbq-orange);
  margin: 2.2rem auto 1.6rem;
}

.markdown-body h1 .secnum { display: block; }

.markdown-body .article {
  margin: 1.4rem 0 1.8rem;
  padding-top: 0.2rem;
}

.markdown-body .article > h2 {
  font-size: 1.22rem;
  padding: 0.35rem 0.85rem;
  border-left: 4px solid var(--fcbq-orange);
  background: linear-gradient(90deg, var(--fcbq-orange-soft), transparent);
  border-radius: 0 var(--radius) var(--radius) 0;
  margin: 0 0 0.6rem;
}

.markdown-body h3,
.markdown-body h4 {
  font-size: 1.04rem;
  margin: 1.5rem 0 0.35rem;
  padding-left: 0.6rem;
  border-left: 3px solid var(--fcbq-blue);
}

.markdown-body h4 {
  font-size: 0.98rem;
  border-left-color: #94a3b8;
}

.secnum {
  font-weight: 800;
  color: var(--fcbq-orange);
  margin-right: 0.35rem;
  white-space: nowrap;
}

.markdown-body h2 .secnum,
.markdown-body h3 .secnum,
.markdown-body h4 .secnum {
  font-variant-numeric: tabular-nums;
}

/* blocs */
.markdown-body strong { color: var(--fcbq-blue-ink); }

.markdown-body .annex {
  background: var(--fcbq-orange-soft);
  border: 1px solid #f5cfa9;
  border-left: 4px solid var(--fcbq-orange);
  border-radius: var(--radius);
  padding: 0.85rem 1.1rem;
  margin: 1.4rem 0;
  font-style: italic;
  color: #4c2b14;
}

.markdown-body .annex::before {
  content: "ANNEX FCBQ";
  display: block;
  font-style: normal;
  font-weight: 800;
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  color: var(--fcbq-orange);
  margin-bottom: 0.4rem;
}

.markdown-body .sancio {
  background: #fdf0f0;
  border: 1px solid #f0c4c4;
  border-left: 4px solid var(--fcbq-red);
  border-radius: var(--radius);
  padding: 0.85rem 1.1rem;
  margin: 1.4rem 0;
}

.markdown-body .sancio::before {
  content: "SANCIÓ";
  display: block;
  font-weight: 800;
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  color: var(--fcbq-red);
  margin-bottom: 0.4rem;
}

/* figures */
.markdown-body img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.4rem auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.markdown-body p img { margin: 1.4rem auto; }

/* ---------- peu ---------- */
.foot {
  border-top: 1px solid var(--line);
  background: var(--bg-soft);
  text-align: center;
  padding: 1.1rem 1rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.foot p { margin: 0.15rem 0; }

/* ---------- selector de mode de vista ---------- */
.view-mode-wrap { margin-left: auto; }

.view-mode-select {
  position: relative;
  font-size: 0.85rem;
}

.view-mode-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border-radius: 999px;
  padding: 0.42rem 0.85rem;
  font: inherit;
  cursor: pointer;
  transition: background-color 0.15s;
}

.view-mode-btn:hover { background: rgba(255, 255, 255, 0.22); }

.view-mode-btn svg:last-child { transition: transform 0.15s; }
.view-mode-select.open .view-mode-btn svg:last-child { transform: rotate(180deg); }

.view-mode-options {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  list-style: none;
  margin: 0;
  padding: 0.3rem;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 170px;
  z-index: 70;
  display: none;
}

.view-mode-select.open .view-mode-options { display: block; }

.view-mode-option {
  padding: 0.45rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.view-mode-option:hover { background: var(--fcbq-orange-soft); }

.view-mode-option.active {
  background: var(--fcbq-blue-ink);
  color: #fff;
  font-weight: 600;
}

/* ---------- vista side-by-side ---------- */
.view-normal {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.view-side {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.side-top {
  flex: none;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: linear-gradient(90deg, var(--fcbq-blue-ink), var(--fcbq-blue) 70%);
}

.side-top-label {
  padding: 0.6rem 1rem;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  text-align: center;
  color: var(--fcbq-orange);
}

.side-top-label + .side-top-label { border-left: 1px solid rgba(255, 255, 255, 0.25); }

.side-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.side-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.side-cell { min-width: 0; }

.side-cell-left { border-right: 1px solid var(--line); }

.side-pair .markdown-body {
  max-width: none;
  margin: 0;
  padding: 1.2rem 1.2rem 1.5rem;
  font-size: 0.95rem;
}

.side-pair .markdown-body h1 { margin: 0.4rem auto 1.1rem; font-size: 1.35rem; }

.side-banner {
  grid-column: 1 / -1;
  background: var(--bg-soft);
}

.side-banner .markdown-body h1 {
  margin: 0;
  border-bottom-color: #d6d9dd;
  font-size: 1.2rem;
  letter-spacing: 0.1em;
}

.side-missing {
  margin: 1.2rem 1.2rem 0;
  padding: 0.7rem 0.9rem;
  border: 1px dashed #f0b98b;
  background: var(--fcbq-orange-soft);
  border-radius: var(--radius);
  color: #7a3d0e;
  font-size: 0.85rem;
  font-style: italic;
}

/* ---------- responsiu ---------- */
@media (max-width: 960px) {
  .side-top { display: none; }
  .side-pair { grid-template-columns: 1fr; }
  .side-cell-left { border-right: none; border-bottom: 1px solid var(--line); }
}

@media (max-width: 768px) {
  .topbar {
    height: auto;
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    padding: 0.75rem;
  }
  .layout { padding-top: 108px; }
  .markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4 { scroll-margin-top: 110px; }

  .sidebar {
    position: fixed;
    top: 108px;
    bottom: 0;
    left: 0;
    width: min(300px, 86vw);
    z-index: 30;
    margin-left: -100%;
    box-shadow: var(--shadow);
  }
  .sidebar.open { margin-left: 0; }
  .sidebar-toggle-wrap { display: block; }
}

/* ---------- impressió ---------- */
@media print {
  .topbar, .sidebar, .sidebar-toggle-wrap { display: none; }
  .layout { display: block; height: auto; overflow: visible; padding-top: 0; }
  .content { overflow: visible; }
  .markdown-body { padding: 0; max-width: none; }
  .markdown-body p { text-align: left; }
  .foot { display: none; }
}