:root {
  color-scheme: light;
  --border: #d8dbe0;
  --text-muted: #6b7280;
  --accent: #2563eb;
}

body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  color: #1a1a1a;
  margin: 0;
  padding: 2rem 1rem;
}

.page {
  max-width: 40rem;
  margin: 0 auto;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

h1 {
  font-size: 1.4rem;
  margin: 0;
}

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

.muted {
  color: var(--text-muted);
  font-size: 0.9rem;
}

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

.subject-list li + li {
  margin-top: 0.5rem;
}

.subject-list a {
  display: block;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  padding: 0.6rem 0.9rem;
  text-decoration: none;
  color: inherit;
}

.subject-list a:hover {
  background: #f7f8fa;
}

.tree {
  list-style: none;
  margin: 0;
  padding-left: 1.1rem;
}

.tree > li:first-child {
  padding-left: 0;
}

#tree-root {
  padding-left: 0;
}

.node-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  border-radius: 0.3rem;
  padding: 0.25rem 0.4rem;
}

.node-row:hover {
  background: #f7f8fa;
}

.node-edit {
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.4rem;
  background: #fafafa;
}

.node-edit textarea {
  width: 100%;
  box-sizing: border-box;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.3rem;
}

button {
  font: inherit;
  cursor: pointer;
}

.btn {
  border: none;
  border-radius: 0.3rem;
  padding: 0.3rem 0.7rem;
  background: var(--accent);
  color: white;
}

.btn-link {
  background: none;
  border: none;
  color: var(--accent);
  padding: 0;
  font-size: 0.85rem;
}

.btn-muted {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.page-wide {
  max-width: 60rem;
}

.upload-form {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.upload-form select,
.upload-form input[type="file"] {
  font: inherit;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.3rem 0.5rem;
}

.doc-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.doc-table th,
.doc-table td {
  text-align: left;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(4.5rem, 1fr));
  gap: 0.5rem;
}

.page-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.5rem 0.3rem;
  text-decoration: none;
  color: inherit;
  font-size: 0.8rem;
}

.page-tile:hover {
  background: #f7f8fa;
}

.page-tile.pending {
  opacity: 0.6;
}

.badge {
  display: inline-block;
  border-radius: 999px;
  padding: 0.1rem 0.5rem;
  font-size: 0.75rem;
  color: white;
}

.badge-green { background: #16a34a; }
.badge-orange { background: #ea580c; }
.badge-red { background: #dc2626; }
.badge-muted { background: #9ca3af; }

.page-view {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
  flex-wrap: wrap;
}

.page-image {
  max-width: 22rem;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
}

.page-text {
  flex: 1;
  min-width: 20rem;
}

.md-final,
.md-compare pre {
  white-space: pre-wrap;
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.6rem;
  font-size: 0.85rem;
  max-height: 24rem;
  overflow-y: auto;
}

.md-compare {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.solution {
  white-space: pre-wrap;
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.6rem;
}

.solution .katex-display {
  margin: 0.3em 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  background: #fafafa;
}

.topbar .brand {
  font-weight: 700;
  text-decoration: none;
}

.topbar-links {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.inline-form {
  display: inline;
  margin: 0;
}

.auth-card {
  max-width: 24rem;
  margin: 2rem auto;
}

.auth-card input[type="text"],
.auth-card input[type="password"] {
  width: 100%;
  box-sizing: border-box;
  font-size: 1rem;
  padding: 0.6rem;
  margin: 0.25rem 0 0.75rem;
}

.auth-card .check {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
  margin: 0.5rem 0 1rem;
}

.auth-error {
  background: #fee2e2;
  color: #991b1b;
  border-radius: 0.3rem;
  padding: 0.5rem 0.75rem;
}

.math-block {
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.3rem 0.6rem;
  overflow-x: auto;
}

.reasons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.tag {
  display: inline-block;
  background: #fef3c7;
  color: #92400e;
  border-radius: 0.3rem;
  padding: 0.1rem 0.4rem;
  font-size: 0.75rem;
}

.dot-review {
  display: inline-block;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 999px;
  background: #dc2626;
  margin-left: 0.2rem;
}

.review-actions {
  margin: 0.5rem 0;
}

.figure-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
  gap: 1rem;
  margin-top: 0.5rem;
}

.figure-card {
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  padding: 0.6rem;
}

.figure-image {
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
}

#atom-form label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.6rem;
}

#atom-form input[type="text"],
#atom-form select,
#atom-form textarea {
  width: 100%;
  box-sizing: border-box;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.3rem;
}

/* ---- Lot 8.3 : navigation commune, mobile d'abord, accueil ---- */
:root {
  --nav-h: 3.5rem;
  --tap: 2.75rem; /* 44 px : cible tactile minimale */
}

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

body {
  overflow-wrap: break-word;
}

main {
  display: block;
}

img {
  max-width: 100%;
  height: auto;
}

input,
select,
textarea {
  font-size: 16px; /* 16 px minimum : iOS ne zoome pas au focus */
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: inherit;
}

.nav-desktop {
  display: flex;
  flex: 1;
  flex-wrap: wrap;
  gap: 0.25rem 1rem;
}

.nav-desktop a {
  text-decoration: none;
  color: inherit;
  padding: 0.3rem 0.1rem;
  border-bottom: 2px solid transparent;
}

.nav-desktop a[aria-current="page"] {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.bottom-nav {
  display: none;
}

a.btn {
  display: inline-block;
  text-decoration: none;
}

.btn-block {
  display: block;
  width: 100%;
  box-sizing: border-box;
  text-align: center;
  padding: 0.7rem 1rem;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1rem;
}

.card {
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.9rem 1rem;
}

.card h2 {
  font-size: 1rem;
  margin: 0 0 0.5rem;
}

.big {
  font-size: 2.5rem;
  font-weight: 700;
  line-height: 1;
  margin: 0.2rem 0;
}

.todo-list,
.menu-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.todo-list a,
.menu-list a,
.menu-button {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  min-height: var(--tap);
  box-sizing: border-box;
  padding: 0.4rem 0.2rem;
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  color: inherit;
}

.menu-button {
  width: 100%;
  border: none;
  border-bottom: 1px solid var(--border);
  background: none;
  font: inherit;
  text-align: left;
  color: #b91c1c;
}

.answer-row {
  display: flex;
  gap: 0.5rem;
}

.answer-row input {
  flex: 1;
  min-width: 0;
}

#answer-form input[type="text"] {
  box-sizing: border-box;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  padding: 0.6rem;
  margin: 0.25rem 0 0.6rem;
}

#answer-form > input[type="text"] {
  width: 100%;
}

.rating-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.rating-actions .rating-label {
  flex-basis: 100%;
}

.rating-actions button {
  flex: 1;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: 0.3rem;
}

.rating-actions .btn {
  border-color: var(--accent);
}

.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
}

.md-final,
.md-compare pre {
  overflow-x: auto;
}

/* Écrans tactiles et téléphones : cibles de 44 px */
@media (max-width: 800px), (pointer: coarse) {
  button,
  .btn,
  .btn-muted,
  .btn-link,
  select,
  input[type="text"],
  input[type="password"],
  input[type="file"] {
    min-height: var(--tap);
  }

  .btn,
  .btn-muted {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .btn-link {
    padding: 0 0.5rem;
  }

  .review-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
  }

  .review-actions .btn,
  .review-actions .btn-muted {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    text-decoration: none;
  }

  .review-actions .btn {
    border-color: var(--accent);
  }
}

/* Téléphone : colonnes empilées, navigation en bas */
@media (max-width: 800px) {
  body {
    padding: 0.75rem 0.75rem calc(var(--nav-h) + 1.25rem + env(safe-area-inset-bottom));
  }

  .page,
  .page-wide {
    max-width: 100%;
  }

  .topbar {
    margin: -0.75rem -0.75rem 0.75rem;
    padding: 0.4rem 0.75rem;
  }

  .nav-desktop,
  .topbar-links {
    display: none;
  }

  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    height: calc(var(--nav-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: #fff;
    border-top: 1px solid var(--border);
  }

  .bottom-nav a {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    text-decoration: none;
    color: var(--text-muted);
    font-size: 0.8rem;
  }

  .bottom-nav a[aria-current="page"] {
    color: var(--accent);
    font-weight: 700;
    box-shadow: inset 0 3px 0 var(--accent);
  }

  .page-header {
    flex-wrap: wrap;
    gap: 0.25rem 1rem;
  }

  .page-header a {
    display: inline-block;
    padding: 0.5rem 0.25rem;
  }

  .page-view {
    flex-direction: column;
  }

  .page-text {
    min-width: 0;
    width: 100%;
    flex: 1 1 auto;
  }

  /* Triage des atomes : le formulaire d'abord, la page d'origine (contexte) dessous */
  body:has(#atom-form) .page-view > .page-text:first-child {
    order: 2;
  }

  .page-image {
    max-width: 100%;
  }

  .md-compare {
    grid-template-columns: 1fr;
  }

  .doc-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .upload-form {
    flex-direction: column;
    align-items: stretch;
  }

  .upload-form select,
  .upload-form input {
    width: 100%;
    box-sizing: border-box;
    font-size: 16px;
  }

  .figure-grid {
    grid-template-columns: 1fr;
  }

  #atom-form input,
  #atom-form select,
  #atom-form textarea,
  .node-edit textarea {
    font-size: 16px;
  }

  /* Triage des atomes : Valider / Rejeter / Passer restent à portée de pouce */
  #atom-form .review-actions {
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
    z-index: 25;
    margin: 0;
    padding: 0.5rem 0.75rem;
    background: #fff;
    border-top: 1px solid var(--border);
    flex-wrap: nowrap;
  }

  #atom-form .review-actions .btn,
  #atom-form .review-actions .btn-muted {
    flex: 1;
    padding-left: 0.25rem;
    padding-right: 0.25rem;
    font-size: 0.85rem;
  }

  body:has(#atom-form) {
    padding-bottom: calc(var(--nav-h) + 5rem + env(safe-area-inset-bottom));
  }
}