/* ── Base ───────────────────────────────────────────────────────────────── */

html {
  scroll-behavior: smooth;
}

body {
  font-family: 'IBM Plex Sans', system-ui, -apple-system, sans-serif;
  max-width: 50rem;
  margin: auto;
  padding: 2rem;
  background: #fff;
  color: #1a1a1a;
}

/* Els salts del TOC no enganxen el títol al límit superior */
h1, h2, h3, h4 {
  scroll-margin-top: 1.25rem;
}

/* Enllaç de tornada a l'índex */
.backlink {
  margin: 0 0 1rem;
  font-size: 0.9rem;
}

.backlink a {
  color: #2563eb;
  text-decoration: none;
}

.backlink a:hover {
  text-decoration: underline;
}

/* ── Taules ─────────────────────────────────────────────────────────────── */

table {
  border-collapse: collapse;
  width: 100%;
  margin: 1.5rem 0;
}

th, td {
  border: 1px solid #999;
  padding: 0.5rem 0.75rem;
  text-align: left;
}

th {
  background-color: #f0f0f0;
  font-weight: bold;
}

tr:nth-child(even) {
  background-color: #f9f9f9;
}

/* ── Imatges i figures ──────────────────────────────────────────────────── */

img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.5rem auto;
}

figure {
  margin: 1.5rem auto;
  text-align: center;
  max-width: 90%;
}

figcaption {
  font-size: 0.9rem;
  color: #888;
  margin-top: 0.5rem;
  font-style: italic;
}

figure img {
  display: block;
  margin: 0 auto;
  max-width: 100%;
  height: auto;
}

/* ── Lightbox (ampliació d'imatges en clicar) ──────────────────────────── */

figure img,
article img,
main img {
  cursor: zoom-in;
}

.lightbox-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  z-index: 9999;
  justify-content: center;
  align-items: center;
  cursor: zoom-out;
  padding: 2rem;
  box-sizing: border-box;
}

.lightbox-overlay.active {
  display: flex;
}

.lightbox-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 95vw;
  max-height: 90vh;
}

.lightbox-overlay img {
  cursor: default;
  max-width: 95vw;
  max-height: 85vh;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
  margin: 0;
}

.lightbox-caption {
  color: #eee;
  text-align: center;
  margin-top: 1rem;
  font-size: 0.95rem;
  font-style: italic;
}

.lightbox-close {
  position: absolute;
  top: 1rem;
  right: 1.5rem;
  color: #fff;
  font-size: 2.25rem;
  line-height: 1;
  font-family: 'IBM Plex Sans', sans-serif;
  cursor: pointer;
  user-select: none;
  opacity: 0.85;
  transition: opacity 0.15s ease;
}

.lightbox-close:hover {
  opacity: 1;
}

/* ── Blocs de codi ──────────────────────────────────────────────────────── */

div.sourceCode {
  background-color: #f5f5f5;
  border-radius: 8px;
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
  overflow-x: auto;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  border-left: 4px solid #4a90d9;
  position: relative;
}

div.sourceCode pre {
  margin: 0;
  padding: 0;
  background: transparent;
}

/* Quan pandoc wrapa pre.ini dins div.sourceCode, cancel·lem l'estil bash */
div.sourceCode:has(pre.ini) {
  background-color: transparent;
  border-left: none;
  box-shadow: none;
  padding: 0;
  margin: 0;
}

div.sourceCode:has(pre.ini) pre.ini {
  background-color: #f0faf0 !important;
  color: #1e3a5f !important;
}

div.sourceCode:has(pre.ini) code {
  color: #1e3a5f !important;
  font-size: 0.85rem !important;
}

div.sourceCode code {
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 1.05rem;
  line-height: 1.6;
  color: #2d2d2d;
}

pre.nano {
  background-color: #eeeedd;
  border-radius: 8px;
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
  overflow-x: auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  border-left: 4px solid #e94560;
  position: relative;
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 1rem;
  line-height: 1.5;
  color: #2d2d2d;
}

pre.nano code {
  line-height: 1.5 !important;
}

pre.resposta {
  background-color: #ccccff;
  border-radius: 8px;
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
  overflow-x: auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  border-left: 4px solid #00ff00;
  position: relative;
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 1rem;
  line-height: 1.5;
  color: #2d2d2d;
}

pre.resposta code {
  line-height: 1.5 !important;
}

pre.ini {
  background-color: #f0faf0;
  border-radius: 8px;
  padding: 1.25rem 1.5rem !important;
  margin: 1.5rem 0 !important;
  overflow-x: auto;
  box-shadow: 0 2px 8px rgba(0, 100, 60, 0.12);
  border-left: 4px solid #2d7a4f;
  position: relative;
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  color: #1e3a5f;
}

pre.ini code {
  line-height: 1.5 !important;
}

pre {
  overflow-x: auto;
  white-space: pre;
  display: block;
  max-width: 100%;
  box-sizing: border-box;
}

/* Botó "Copia" (injectat per md2html.sh) */
.copy-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  font-family: 'IBM Plex Sans', sans-serif;
  font-size: 0.72rem;
  font-weight: 500;
  color: #555;
  background: rgba(255, 255, 255, 0.85);
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 5px;
  padding: 3px 9px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease, background 0.15s ease;
  line-height: 1.4;
  white-space: normal;
}

div.sourceCode:hover .copy-btn,
pre.nano:hover .copy-btn,
pre.resposta:hover .copy-btn,
pre.ini:hover .copy-btn,
.copy-btn:focus-visible {
  opacity: 1;
}

.copy-btn:hover {
  background: #fff;
  color: #111;
}

@media (hover: none) {
  .copy-btn {
    opacity: 1; /* sempre visible en pantalles tàctils */
  }
}

/* Code inline (dins de paràgrafs, títols, llistes) */
code {
  overflow-x: auto;
  white-space: pre;
  display: inline;
  max-width: 100%;
  box-sizing: border-box;
  vertical-align: baseline;
  line-height: inherit;
}

/* Code dins de blocs pre: comportament de bloc */
pre code {
  display: block;
}

/* Barra de desplaçament (WebKit) */
pre::-webkit-scrollbar,
code::-webkit-scrollbar {
  height: 6px;
}

pre::-webkit-scrollbar-track,
code::-webkit-scrollbar-track {
  background: transparent;
}

pre::-webkit-scrollbar-thumb,
code::-webkit-scrollbar-thumb {
  background: rgba(128, 128, 128, 0.4);
  border-radius: 3px;
}

pre::-webkit-scrollbar-thumb:hover,
code::-webkit-scrollbar-thumb:hover {
  background: rgba(128, 128, 128, 0.7);
}

/* ── Llistes ordenades (CSS pur, sense post-processament) ──────────────── */

ol {
  padding-left: 0;
  list-style: none;
  counter-reset: ol-counter;
  margin: 0.5rem 0;
}

ol li {
  counter-increment: ol-counter;
  display: block;
  position: relative;
  padding: 8px 0 8px 36px;
  border-bottom: 0.5px solid rgba(0, 0, 0, 0.08);
  line-height: 1.6;
}

ol li:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

ol li::before {
  content: counter(ol-counter);
  position: absolute;
  left: 0;
  top: calc(8px + 0.8em - 12px);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #f5f5f5;
  border: 0.5px solid rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 500;
  color: #666;
  box-sizing: border-box;
}

/* Compatibilitat amb pàgines antigues que duen span.li-content */
ol li > span.li-content {
  display: inline;
}

ol li code {
  display: inline;
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.85em;
  background: #f5f5f5;
  border: 0.5px solid rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  padding: 1px 0;
  vertical-align: baseline;
  line-height: 1.4;
}

/* Code inline dins de paràgrafs, títols i taules */
p code,
h1 code, h2 code, h3 code, h4 code,
blockquote code,
td code, th code {
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.88em;
  background: #f5f5f5;
  border: 0.5px solid rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  padding: 0 1px;
  display: inline;
  vertical-align: baseline;
  line-height: inherit;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

/* Code inline dins de llistes: sense padding lateral per evitar espai visual */
li code {
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.88em;
  background: #f5f5f5;
  border: 0.5px solid rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  padding: 0 2px;
  display: inline;
  vertical-align: baseline;
  line-height: inherit;
}

h2 code, h3 code {
  position: relative;
  top: -0.1em;
}

/* ── Títols i paràgrafs ─────────────────────────────────────────────────── */

h1 {
  font-size: 1.75rem;
  font-weight: 500;
  letter-spacing: -0.3px;
  margin: 3.5rem 0 1rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  color: #111;
}

h2 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: 0;
  color: #111;
  margin: 3rem 0 0.5rem;
}

h3 {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0;
  color: #111;
  margin: 3rem 0 0.5rem;
}

p {
  line-height: 1.7;
  margin: 0 0 0.85rem;
  color: #1a1a1a;
}

/* ── Llistes no ordenades ───────────────────────────────────────────────── */

ul {
  list-style: none;
  padding: 0;
  margin: 0.25rem 0 0.85rem;
}

ul li {
  display: block;
  position: relative;
  padding: 7px 0 7px 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  line-height: 1.6;
}

ul li:last-child {
  border-bottom: none;
}

ul li::before {
  content: '';
  position: absolute;
  left: 0;
  top: calc(7px + 0.8em - 3px);
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #bbb;
}

/* ── GFM Alerts ─────────────────────────────────────────────────────────── */

.alert {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  border-radius: 8px;
  padding: 1rem 1.25rem;
  margin: 1.5rem 0;
  border-left: 4px solid;
}

.alert-icon {
  font-size: 1.15rem;
  line-height: 1.6;
  flex-shrink: 0;
}

.alert-content {
  flex: 1;
  min-width: 0;
}

.alert-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 0.35rem;
}

.alert-content p {
  margin: 0 0 0.5rem;
  line-height: 1.6;
}

.alert-content p:last-child {
  margin-bottom: 0;
}

/* NOTE – blau */
.alert-note {
  background: #eff6ff;
  border-color: #3b82f6;
}
.alert-note .alert-label { color: #1d4ed8; }

/* TIP – verd */
.alert-tip {
  background: #f0fdf4;
  border-color: #22c55e;
}
.alert-tip .alert-label { color: #15803d; }

/* IMPORTANT – violeta */
.alert-important {
  background: #faf5ff;
  border-color: #a855f7;
}
.alert-important .alert-label { color: #7e22ce; }

/* WARNING – groc ambre */
.alert-warning {
  background: #fffbeb;
  border-color: #f59e0b;
}
.alert-warning .alert-label { color: #b45309; }

/* CAUTION – vermell */
.alert-caution {
  background: #fff1f2;
  border-color: #ef4444;
}
.alert-caution .alert-label { color: #b91c1c; }

/* ── Taula de continguts (TOC) ──────────────────────────────────────────── */

nav#TOC {
  background: #f8f9fa;
  border-left: 3px solid #4a90d9;
  border-radius: 6px;
  padding: 1rem 1.25rem 1rem 1.5rem;
  margin: 1.5rem 0 2rem;
  display: inline-block;
  min-width: 280px;
  max-width: 100%;
}

nav#TOC > h2 {
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #666;
  margin: 0 0 0.75rem;
  border: none;
  padding: 0;
}

nav#TOC ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

nav#TOC li {
  display: block;
  padding: 0;
  border: none;
  line-height: 1.5;
}

nav#TOC li::before {
  display: none;
}

/* Nivell 1 */
nav#TOC > ul > li {
  margin: 0.3rem 0;
}

/* Nivell 2 */
nav#TOC > ul > li > ul {
  margin: 0.2rem 0 0.2rem 1rem;
}

/* Nivell 3 */
nav#TOC > ul > li > ul > li > ul {
  margin: 0.1rem 0 0.1rem 1rem;
}

nav#TOC a {
  color: #2563eb;
  text-decoration: none;
  font-size: 0.92rem;
}

nav#TOC a:hover {
  text-decoration: underline;
}

nav#TOC > ul > li > a {
  font-weight: 600;
  font-size: 0.95rem;
}

nav#TOC > ul > li > ul > li > a {
  font-weight: 400;
  color: #444;
}

/* TOC lateral fix en pantalles molt amples */
@media (min-width: 1560px) {
  nav#TOC {
    position: fixed;
    top: 2rem;
    /* body: 50rem + 4rem de padding = 54rem (864px) centrats.
       El TOC (320px, border-box) acaba 16px abans del marge esquerre del body:
       left = 50% - 432px - 16px - 320px = 50% - 768px */
    left: calc(50% - 768px);
    width: 320px;
    box-sizing: border-box;
    min-width: 0;
    max-height: calc(100vh - 4rem);
    overflow-y: auto;
    margin: 0;
  }
}

/* ── Mòbil ──────────────────────────────────────────────────────────────── */

@media (max-width: 700px) {
  body {
    padding: 1rem;
    max-width: 100%;
  }

  h1 {
    font-size: 1.45rem;
  }

  div.sourceCode,
  pre.nano,
  pre.resposta {
    padding: 1rem;
    font-size: 0.9rem;
  }

  pre.ini {
    padding: 1rem !important;
    font-size: 0.9rem;
  }

  div.sourceCode code {
    font-size: 0.9rem;
  }

  /* Taules amples: desplaçament horitzontal en lloc de desbordar */
  table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }

  figure {
    max-width: 100%;
  }

  img {
    max-width: 100%;
  }
}

/* ── Mode fosc ──────────────────────────────────────────────────────────── */

@media (prefers-color-scheme: dark) {
  body {
    background: #16181d;
    color: #d6d6d6;
  }

  p, ul li, ol li {
    color: #d0d0d0;
  }

  h1, h2, h3, h4 {
    color: #ececec;
  }

  h1 {
    border-bottom-color: rgba(255, 255, 255, 0.12);
  }

  a {
    color: #7aa7ff;
  }

  a:visited {
    color: #b39ddb;
  }

  a {
    color: #7aa7ff;
  }

  .backlink a, nav#TOC a {
    color: #7aa7ff;
  }

  th {
    background-color: #23262d;
  }

  th, td {
    border-color: #4a4d55;
  }

  tr:nth-child(even) {
    background-color: #1c1f25;
  }

  div.sourceCode {
    background-color: #1e2128;
    border-left-color: #4a90d9;
    box-shadow: none;
  }

  div.sourceCode code {
    color: #d8d8d8;
  }

  pre.nano {
    background-color: #2a2a22;
    color: #ddddcc;
    box-shadow: none;
  }

  pre.resposta {
    background-color: #232338;
    color: #d6d6ee;
    box-shadow: none;
  }

  pre.ini {
    background-color: #0f1f17;
    color: #a8c8b8;
    border-left-color: #2d7a4f;
    box-shadow: none;
  }

  p code, h1 code, h2 code, h3 code, h4 code,
  blockquote code, td code, th code, li code, ol li code {
    background: #262a31;
    border-color: rgba(255, 255, 255, 0.12);
    color: #e3e3e3;
  }

  ol li::before {
    background: #262a31;
    border-color: rgba(255, 255, 255, 0.15);
    color: #aaa;
  }

  ul li, ol li {
    border-bottom-color: rgba(255, 255, 255, 0.07);
  }

  ul li::before {
    background: #555;
  }

  nav#TOC {
    background: #1c1f25;
  }

  nav#TOC > h2 {
    color: #999;
  }

  nav#TOC > ul > li > ul > li > a {
    color: #b9b9b9;
  }

  .copy-btn {
    background: rgba(40, 44, 52, 0.9);
    border-color: rgba(255, 255, 255, 0.18);
    color: #bbb;
  }

  .copy-btn:hover {
    background: #32363e;
    color: #fff;
  }

  .alert-note      { background: #14202f; }
  .alert-tip       { background: #122418; }
  .alert-important { background: #221a2c; }
  .alert-warning   { background: #28220f; }
  .alert-caution   { background: #2a1518; }

  .alert-content p {
    color: #d0d0d0;
  }

  figcaption {
    color: #999;
  }

  .lightbox-overlay {
    background: rgba(0, 0, 0, 0.94);
  }
}
