:root {
  --ink: #172231;
  --muted: #607080;
  --paper: #fbfaf6;
  --surface: #ffffff;
  --line: #dce2e5;
  --primary: #1b5e55;
  --primary-dark: #12473f;
  --primary-soft: #e9f3ef;
  --accent: #d47b3f;
  --danger: #a63c3c;
  --shadow: 0 18px 50px rgba(30, 45, 55, .08);
  --radius: 18px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 10% 0%, rgba(27, 94, 85, .08), transparent 28rem),
    var(--paper);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.55;
}
button, input, select { font: inherit; }
a { color: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 2rem;
  padding: .85rem max(1.25rem, calc((100vw - 1440px) / 2));
  background: rgba(251, 250, 246, .9);
  border-bottom: 1px solid rgba(220, 226, 229, .8);
  backdrop-filter: blur(16px);
}
.brand { display: inline-flex; align-items: center; gap: .75rem; text-decoration: none; }
.brand-mark {
  display: grid; place-items: center; width: 2.5rem; height: 2.5rem;
  color: #fff; background: var(--primary); border-radius: 12px; font-weight: 800;
}
.brand strong, .brand small { display: block; }
.brand small { color: var(--muted); font-size: .74rem; }
.topbar nav { display: flex; gap: 1.25rem; font-size: .92rem; }
.topbar nav a { text-decoration: none; color: var(--muted); }
.topbar nav a:hover { color: var(--primary); }

main { width: min(1440px, calc(100% - 2rem)); margin: 0 auto; }
.hero {
  display: grid; grid-template-columns: 1.5fr .75fr; align-items: end; gap: 4rem;
  padding: 5.5rem 1rem 3.5rem;
}
.eyebrow { margin: 0 0 .35rem; color: var(--primary); font-size: .76rem; font-weight: 800; letter-spacing: .13em; text-transform: uppercase; }
h1, h2, h3 { margin-top: 0; line-height: 1.12; letter-spacing: -.025em; }
h1 { margin-bottom: 1.25rem; font-family: Georgia, "Times New Roman", serif; font-size: clamp(2.35rem, 4.5vw, 4.8rem); font-weight: 500; }
h2 { margin-bottom: 0; font-size: clamp(1.45rem, 2vw, 2rem); }
.hero-copy { max-width: 52rem; margin: 0; color: var(--muted); font-size: 1.08rem; }
.hero-card {
  display: grid; grid-template-columns: repeat(3, 1fr); overflow: hidden;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
.hero-card div { padding: 1.2rem .9rem; text-align: center; }
.hero-card div + div { border-left: 1px solid var(--line); }
.hero-card strong, .hero-card span { display: block; }
.hero-card strong { color: var(--primary); font-size: 1.35rem; }
.hero-card span { margin-top: .2rem; color: var(--muted); font-size: .76rem; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.workspace { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(340px, .75fr); gap: 1.25rem; align-items: start; }
.catalog-panel, .builder-panel { padding: 1.35rem; }
.builder-panel { position: sticky; top: 5.25rem; }
.panel-heading, .section-heading, .result-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; }
.filters { display: grid; grid-template-columns: .55fr .85fr 1.15fr 1.15fr; gap: .8rem; margin: 1.4rem 0; }
label { display: grid; gap: .35rem; color: var(--muted); font-size: .82rem; font-weight: 700; }
input, select {
  width: 100%; min-height: 2.65rem; padding: .62rem .75rem;
  color: var(--ink); background: #fff; border: 1px solid #cfd7db; border-radius: 10px; outline: none;
}
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(27, 94, 85, .12); }
.form-row { display: grid; grid-template-columns: .65fr 1.35fr; gap: .75rem; }
.builder-panel form { display: grid; gap: .9rem; margin-top: 1.25rem; }
.optional { color: #89949d; font-weight: 500; }

.catalog-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .8rem; }
.exercise-card { display: grid; gap: .8rem; padding: 1rem; border: 1px solid var(--line); border-radius: 14px; background: #fff; }
.exercise-card:hover { border-color: #b9c9c5; box-shadow: 0 8px 25px rgba(30, 45, 55, .06); }
.exercise-card h3 { margin-bottom: .25rem; font-size: 1.04rem; }
.exercise-card p { margin: 0; color: var(--muted); font-size: .86rem; }
.card-topline { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.badge { display: inline-flex; width: fit-content; padding: .22rem .55rem; color: var(--primary-dark); background: var(--primary-soft); border-radius: 999px; font-size: .72rem; font-weight: 800; }
.grades { color: var(--muted); font-size: .72rem; }
.card-controls { display: grid; grid-template-columns: 1fr 75px auto; gap: .5rem; align-items: end; }
.card-controls label { font-size: .7rem; }

.button {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 2.5rem; padding: .58rem .9rem; border: 1px solid transparent; border-radius: 10px;
  font-weight: 800; font-size: .84rem; text-decoration: none; cursor: pointer; transition: transform .15s, background .15s, border-color .15s;
}
.button:hover { transform: translateY(-1px); }
.button:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.button-primary { color: #fff; background: var(--primary); }
.button-primary:hover { background: var(--primary-dark); }
.button-secondary { color: var(--primary-dark); background: var(--primary-soft); border-color: #c9ddd5; }
.button-ghost { min-height: 2.2rem; color: var(--muted); background: transparent; border-color: var(--line); }
.button-wide { width: 100%; min-height: 3rem; }

.counter { padding: .3rem .6rem; color: var(--primary); background: var(--primary-soft); border-radius: 999px; font-size: .78rem; font-weight: 800; white-space: nowrap; }
.selection-list { display: grid; gap: .55rem; min-height: 5rem; padding: .7rem; background: #f7f8f7; border: 1px dashed #cbd4d5; border-radius: 12px; }
.selection-row { display: grid; grid-template-columns: minmax(0, 1fr) 70px 100px auto; gap: .55rem; align-items: center; padding: .65rem; background: #fff; border: 1px solid var(--line); border-radius: 10px; }
.selection-row strong { font-size: .83rem; }
.selection-row small { display: block; color: var(--muted); }
.icon-button { width: 2.25rem; height: 2.25rem; color: var(--danger); background: #fff; border: 1px solid #eed4d4; border-radius: 8px; cursor: pointer; }
.empty-state { align-self: center; margin: 0; color: var(--muted); text-align: center; font-size: .84rem; }
.message { padding: .7rem .8rem; color: #7a2424; background: #fbecec; border: 1px solid #efc7c7; border-radius: 10px; font-size: .84rem; }

.result-section { margin-top: 1.25rem; padding: 1.5rem; }
.download-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: .55rem; }
.muted { color: var(--muted); }
.tabs { display: flex; gap: .3rem; margin: 0; border-bottom: 1px solid var(--line); }
.tab { padding: .65rem .9rem; color: var(--muted); background: transparent; border: 0; border-bottom: 3px solid transparent; font-weight: 800; cursor: pointer; }
.tab.is-active { color: var(--primary); border-bottom-color: var(--primary); }
.pdf-preview-toolbar { display: flex; align-items: end; justify-content: space-between; gap: 1rem; margin: 1.4rem 0 .75rem; border-bottom: 1px solid var(--line); }
.pdf-preview-actions { display: flex; align-items: center; gap: .55rem; padding-bottom: .35rem; }
.pdf-engine-badge { display: inline-flex; align-items: center; min-height: 2rem; padding: .35rem .6rem; color: var(--primary-dark); background: var(--primary-soft); border: 1px solid #c9ddd5; border-radius: 999px; font-size: .72rem; font-weight: 800; white-space: nowrap; }
.pdf-engine-badge[hidden] { display: none; }
.preview.pdf-preview { position: relative; max-width: none; min-height: 74vh; margin: 0; padding: 0; overflow: hidden; background: #eceff1; border: 1px solid #cfd7db; border-radius: 12px; box-shadow: 0 10px 35px rgba(30, 45, 55, .07); }
.pdf-preview-frame { display: block; width: 100%; height: 78vh; min-height: 680px; border: 0; background: #eceff1; }
.pdf-preview-frame[hidden] { display: none; }
.pdf-preview-status, .pdf-preview-error { position: absolute; inset: 0; display: grid; place-content: center; gap: .45rem; padding: 2rem; text-align: center; }
.pdf-preview-status { color: var(--muted); font-weight: 700; }
.pdf-preview-status::before { content: ""; width: 2rem; height: 2rem; margin: 0 auto .5rem; border: 3px solid #d5e3de; border-top-color: var(--primary); border-radius: 50%; animation: pdf-preview-spin .8s linear infinite; }
.pdf-preview-status[hidden], .pdf-preview-error[hidden] { display: none; }
.pdf-preview-error { color: #7a2424; background: #fff8f8; }
.pdf-preview-error p { max-width: 48rem; margin: 0; color: #8f4b4b; }
.pdf-preview-help { margin: .7rem 0 0; color: var(--muted); font-size: .8rem; text-align: center; }
#open-preview[aria-disabled="true"] { pointer-events: none; opacity: .45; }
@keyframes pdf-preview-spin { to { transform: rotate(360deg); } }

.history-section { padding: 4rem 0 3rem; }
.history-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .8rem; margin-top: 1.2rem; }
.history-card { display: grid; gap: .6rem; padding: 1rem; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; }
.history-card h3 { margin: 0; font-size: 1rem; }
.history-meta { display: flex; flex-wrap: wrap; gap: .45rem; color: var(--muted); font-size: .76rem; }
.history-actions { display: flex; gap: .45rem; }
.history-actions .button { flex: 1; }

footer { padding: 1.8rem; color: var(--muted); border-top: 1px solid var(--line); text-align: center; font-size: .82rem; }
footer p { margin: 0; }
.footer-separator { display: inline-block; margin: 0 .35rem; color: var(--line-strong, var(--muted)); }
.footer-admin-link, .footer-license-link { color: var(--primary-dark); font-weight: 700; text-decoration: none; text-underline-offset: .16em; }
.footer-admin-link:hover, .footer-admin-link:focus-visible, .footer-license-link:hover, .footer-license-link:focus-visible { text-decoration: underline; }

@media (max-width: 1050px) {
  .hero { grid-template-columns: 1fr; gap: 2rem; }
  .workspace { grid-template-columns: 1fr; }
  .builder-panel { position: static; }
  .history-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 700px) {
  main { width: min(100% - 1rem, 1440px); }
  .topbar nav { display: none; }
  .hero { padding: 3.2rem .4rem 2rem; }
  .hero-card { grid-template-columns: 1fr; }
  .hero-card div + div { border-left: 0; border-top: 1px solid var(--line); }
  .filters, .catalog-grid, .form-row, .history-grid { grid-template-columns: 1fr; }
  .selection-row { grid-template-columns: 1fr 70px 95px auto; }
  .result-header { display: grid; }
  .download-actions { justify-content: flex-start; }
  .pdf-preview-toolbar { align-items: stretch; flex-direction: column; gap: .35rem; }
  .pdf-preview-actions { justify-content: space-between; padding-bottom: .5rem; }
  .pdf-preview-frame { height: 68vh; min-height: 520px; }
}

/* Progressive Web App */
.brand-icon { width: 2.5rem; height: 2.5rem; border-radius: 12px; box-shadow: 0 4px 14px rgba(18, 71, 63, .18); }
.topbar-right { display: flex; align-items: center; gap: 1rem; }
.connection-status {
  display: inline-flex; align-items: center; gap: .4rem; padding: .35rem .6rem;
  color: var(--primary-dark); background: var(--primary-soft); border-radius: 999px;
  font-size: .72rem; font-weight: 800; white-space: nowrap;
}
.connection-status > span { width: .48rem; height: .48rem; background: #2d8d67; border-radius: 50%; box-shadow: 0 0 0 3px rgba(45, 141, 103, .13); }
.connection-status.is-offline { color: #7a4d18; background: #fff2df; }
.connection-status.is-offline > span { background: #c47a22; box-shadow: 0 0 0 3px rgba(196, 122, 34, .14); }
.install-button { min-height: 2.25rem; padding: .45rem .75rem; }
.hero-install-button { margin-top: 1.2rem; }
.noscript-message { padding: .8rem 1rem; color: #7a2424; background: #fbecec; border-bottom: 1px solid #efc7c7; text-align: center; }
.offline-note { grid-column: 1 / -1; margin: 0; padding: .75rem .9rem; color: #7a4d18; background: #fff2df; border: 1px solid #efd5ac; border-radius: 12px; font-size: .84rem; }
.icon-button:disabled { opacity: .4; cursor: not-allowed; }

.pwa-banner {
  position: fixed; z-index: 50; right: 1rem; bottom: max(1rem, env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: .65rem; max-width: min(92vw, 620px); padding: .8rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 18px 55px rgba(23, 34, 49, .2);
}
.pwa-banner[hidden] { display: none; }
.pwa-banner span { margin-right: .25rem; font-size: .86rem; font-weight: 700; }

.install-dialog { width: min(92vw, 470px); padding: 0; border: 0; border-radius: 18px; box-shadow: 0 30px 90px rgba(23, 34, 49, .28); }
.install-dialog::backdrop { background: rgba(23, 34, 49, .48); backdrop-filter: blur(4px); }
.install-dialog form { position: relative; display: grid; gap: .8rem; padding: 1.5rem; }
.install-dialog img { border-radius: 18px; }
.install-dialog h2 { margin-bottom: 0; }
.install-dialog p, .install-dialog ol { margin-top: 0; color: var(--muted); }
.install-dialog ol { padding-left: 1.25rem; }
.install-dialog li { margin-bottom: .45rem; }
.dialog-close { position: absolute; top: .7rem; right: .7rem; width: 2.25rem; height: 2.25rem; color: var(--muted); background: transparent; border: 0; border-radius: 50%; font-size: 1.5rem; cursor: pointer; }
.dialog-close:hover { background: #f1f3f2; }

.offline-page { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.offline-card { width: min(100%, 620px); padding: clamp(1.5rem, 5vw, 3rem); text-align: center; }
.offline-card img { border-radius: 22px; box-shadow: 0 12px 35px rgba(18, 71, 63, .2); }
.offline-card h1 { margin-top: .4rem; font-size: clamp(2rem, 6vw, 3.5rem); }
.offline-card > p:not(.eyebrow) { color: var(--muted); }
.offline-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: .65rem; margin-top: 1.25rem; }

@media (display-mode: standalone) {
  .install-button, .hero-install-button { display: none !important; }
  .topbar { padding-top: calc(.85rem + env(safe-area-inset-top)); }
}

@media (max-width: 900px) {
  .topbar-right { gap: .55rem; }
  .connection-status { display: none; }
}

@media (max-width: 700px) {
  .topbar { gap: .6rem; padding-inline: .75rem; }
  .brand small { display: none; }
  .brand strong { font-size: .92rem; }
  .brand-icon { width: 2.2rem; height: 2.2rem; }
  .install-button { min-height: 2.1rem; padding: .4rem .62rem; font-size: .78rem; }
  .selection-row { grid-template-columns: 1fr 62px 88px auto; gap: .35rem; }
  .pwa-banner { left: .5rem; right: .5rem; bottom: max(.5rem, env(safe-area-inset-bottom)); flex-wrap: wrap; }
  .pwa-banner span { flex: 1 1 100%; }
}
.exercise-card .card-competencies { margin-top: .65rem; color: var(--text); font-size: .78rem; }
.exercise-card .card-program { margin-top: .35rem; font-size: .7rem; line-height: 1.35; }

/* MathsFiches 3.4.4 - base mobile conservée ; licence CC BY-NC 4.0 */
@media (max-width: 700px) {
  html { -webkit-text-size-adjust: 100%; }
  body { overflow-x: hidden; }
  button, .button, select, input { touch-action: manipulation; }

  main {
    width: 100%;
    padding-left: max(.65rem, env(safe-area-inset-left));
    padding-right: max(.65rem, env(safe-area-inset-right));
  }

  .topbar {
    min-height: 3.7rem;
    padding-left: max(.7rem, env(safe-area-inset-left));
    padding-right: max(.7rem, env(safe-area-inset-right));
    padding-bottom: .65rem;
  }
  .brand { min-width: 0; gap: .6rem; }
  .brand span { min-width: 0; }
  .brand strong { font-size: .98rem; line-height: 1.1; }
  .brand small { display: block; max-width: 12rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .66rem; }
  .brand-icon { flex: 0 0 auto; width: 2.25rem; height: 2.25rem; }
  .topbar-right { flex: 0 0 auto; }

  .hero {
    gap: 1rem;
    padding: 2.15rem 0 1.35rem;
  }
  h1 {
    margin-bottom: .9rem;
    font-size: clamp(2rem, 9.8vw, 2.65rem);
    line-height: 1.03;
    letter-spacing: -.035em;
  }
  .hero-copy { font-size: .98rem; line-height: 1.52; }
  .hero-card { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .hero-card div { min-width: 0; padding: .9rem .35rem; }
  .hero-card div + div { border-top: 0; border-left: 1px solid var(--line); }
  .hero-card strong { font-size: 1.12rem; }
  .hero-card span { font-size: .68rem; line-height: 1.25; }

  .workspace { gap: .8rem; }
  .catalog-panel, .builder-panel, .result-section { min-width: 0; padding: 1rem; }
  .panel-heading, .section-heading, .result-header { min-width: 0; gap: .65rem; }
  .panel-heading > div, .section-heading > div, .result-header > div { min-width: 0; }
  .panel-heading h2, .section-heading h2, .result-header h2 { font-size: 1.42rem; }

  .filters { gap: .65rem; margin: 1rem 0; }
  label { gap: .3rem; font-size: .88rem; }
  input, select {
    min-height: 2.9rem;
    padding: .68rem .72rem;
    font-size: 1rem;
  }

  .catalog-grid { gap: .65rem; }
  .exercise-card { min-width: 0; gap: .75rem; padding: .9rem; }
  .card-topline { align-items: flex-start; }
  .badge {
    max-width: 78%;
    white-space: normal;
    line-height: 1.2;
  }
  .grades { flex: 0 0 auto; padding-top: .15rem; }
  .exercise-card h3 { margin-top: .1rem; font-size: 1.08rem; line-height: 1.2; }
  .exercise-card p { font-size: .9rem; line-height: 1.48; }
  .exercise-card .card-competencies { font-size: .83rem; }
  .exercise-card .card-program { font-size: .76rem; line-height: 1.42; }
  .card-controls {
    grid-template-columns: minmax(0, 1fr) 5.6rem;
    grid-template-areas:
      "difficulty quantity"
      "add add";
    gap: .55rem;
  }
  .card-controls label:first-child { grid-area: difficulty; min-width: 0; }
  .card-controls label:nth-child(2) { grid-area: quantity; min-width: 0; }
  .card-controls .button { grid-area: add; width: 100%; min-height: 2.75rem; }

  .builder-panel form { gap: .8rem; margin-top: 1rem; }
  .selection-list { min-width: 0; padding: .55rem; }
  .selection-row {
    grid-template-columns: minmax(0, 1fr) 2.75rem;
    grid-template-areas:
      "selection-title selection-remove"
      "selection-quantity selection-difficulty";
    gap: .55rem;
    align-items: center;
    padding: .7rem;
  }
  .selection-row > div:first-child { grid-area: selection-title; min-width: 0; }
  .selection-row > div:first-child strong,
  .selection-row > div:first-child small { overflow-wrap: anywhere; }
  .selection-row > input { grid-area: selection-quantity; min-width: 0; }
  .selection-row > select { grid-area: selection-difficulty; min-width: 0; }
  .selection-row > .icon-button { grid-area: selection-remove; justify-self: end; width: 2.75rem; height: 2.75rem; }

  .button { min-height: 2.75rem; }
  .button-wide { min-height: 3rem; }

  .result-section { margin-top: .8rem; }
  .result-header { display: grid; }
  .download-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
    gap: .45rem;
  }
  .download-actions .button { width: 100%; min-width: 0; min-height: 2.75rem; padding-inline: .55rem; }
  #download-complete, #download-latex { grid-column: 1 / -1; }

  .pdf-preview-toolbar { margin: 1rem 0 .6rem; }
  .tabs {
    width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { flex: 1 0 auto; min-height: 2.75rem; padding: .62rem .72rem; white-space: nowrap; }
  .pdf-preview-actions {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) minmax(0, 1fr);
    width: 100%;
    gap: .45rem;
    align-items: center;
    padding-bottom: .5rem;
  }
  .pdf-preview-actions .button { width: 100%; min-width: 0; min-height: 2.75rem; padding-inline: .5rem; }
  .pdf-engine-badge { max-width: 9.5rem; overflow: hidden; text-overflow: ellipsis; }
  .preview.pdf-preview { min-height: 64vh; border-radius: 10px; }
  .pdf-preview-frame { height: 70vh; min-height: 480px; }
  .pdf-preview-status, .pdf-preview-error { padding: 1rem; }
  .pdf-preview-help { font-size: .78rem; line-height: 1.45; }

  .history-section { padding: 2.4rem 0 2rem; }
  .history-card { min-width: 0; padding: .9rem; }
  .history-actions { display: grid; grid-template-columns: 1fr 1fr 2.75rem; gap: .45rem; }
  .history-actions .button { min-width: 0; min-height: 2.75rem; }
  .history-actions .icon-button { width: 2.75rem; height: 2.75rem; }

  footer {
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }
}

@media (max-width: 430px) {
  .topbar { gap: .45rem; }
  .brand small { max-width: 9.5rem; }
  .install-button { padding-inline: .55rem; }
  .download-actions { grid-template-columns: 1fr; }
  #download-complete, #download-latex { grid-column: auto; }
  .pdf-preview-actions { grid-template-columns: 1fr 1fr; }
  .pdf-engine-badge { grid-column: 1 / -1; justify-self: center; max-width: 100%; }
  .preview.pdf-preview { min-height: 60vh; }
  .pdf-preview-frame { height: 66vh; min-height: 440px; }
}

@media (max-width: 370px) {
  .brand small { display: none; }
  .hero-card span { font-size: .63rem; }
  .selection-row {
    grid-template-columns: 1fr 2.75rem;
    grid-template-areas:
      "selection-title selection-remove"
      "selection-quantity selection-quantity"
      "selection-difficulty selection-difficulty";
  }
}
