:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11,11,11,0.10);
  --now-line: #e34948;

  /* Couleurs des matieres : charte graphique de l'ecole.
     series-1 sert aussi d'accent principal de l'interface,
     series-6 d'indicateur "cours en cours". */
  --series-1: #2b317f; /* bleu principal   pantone 6105 */
  --series-2: #9c5099; /* mauve            pantone 7655 */
  --series-3: #de2285; /* magenta                       */
  --series-4: #b54663; /* framboise                     */
  --series-5: #e17e0a; /* orange           pantone 138  */
  --series-6: #817741; /* olive assombri, role texte    */
  --series-7: #b46747; /* terracotta                    */
  --series-8: #583811; /* brun             pantone 7596 */

  /* Bandeau : etats « en cours » (vert) et « a venir » (jaune). Hors palette
     des matieres, qui suit la charte et sert a tout autre chose.
     -mark = pastille et fond teinte ; la variable sans suffixe est une
     couleur de TEXTE, donc assombrie pour tenir le 4,5:1 sur surface-1. */
  --status-ongoing: #1a7f37;
  --status-ongoing-mark: #2ea043;
  --status-upcoming: #8a5a00;
  --status-upcoming-mark: #f0a202;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface-1: #1a1a19;
    --surface-2: #201f1e;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255,255,255,0.10);
    --now-line: #e66767;

    /* Memes teintes de charte, eclaircies pour rester lisibles sur fond sombre */
    --series-1: #6f76ce;
    --series-2: #af62ac;
    --series-3: #de2285;
    --series-4: #c15e78;
    --series-5: #e17e0a;
    --series-6: #988c4c;
    --series-7: #b46747;
    --series-8: #af6f22;

    /* Versions eclaircies : sur #1a1a19 le vert et le jaune sombres du
       theme clair tombent sous le seuil de lisibilite. */
    --status-ongoing: #3fb950;
    --status-ongoing-mark: #3fb950;
    --status-upcoming: #e3b341;
    --status-upcoming-mark: #e3b341;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --surface-2: #201f1e;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255,255,255,0.10);
  --now-line: #e66767;

  /* Memes teintes de charte, eclaircies pour rester lisibles sur fond sombre */
  --series-1: #6f76ce;
  --series-2: #af62ac;
  --series-3: #de2285;
  --series-4: #c15e78;
  --series-5: #e17e0a;
  --series-6: #988c4c;
  --series-7: #b46747;
  --series-8: #af6f22;

  /* Versions eclaircies : sur #1a1a19 le vert et le jaune sombres du
     theme clair tombent sous le seuil de lisibilite. */
  --status-ongoing: #3fb950;
  --status-ongoing-mark: #3fb950;
  --status-upcoming: #e3b341;
  --status-upcoming-mark: #e3b341;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--page);
  color: var(--text-primary);
  font-family: "Manrope", system-ui, -apple-system, "Segoe UI", sans-serif;
}

button {
  font-family: inherit;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}

.tag {
  font-size: 12px;
  font-weight: 600;
  color: var(--series-1);
  background: color-mix(in srgb, var(--series-1) 16%, transparent);
  padding: 2px 8px;
  border-radius: 999px;
}

.filiere-select {
  position: relative;
}

.filiere-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.filiere-btn:hover {
  background: var(--gridline);
}

.filiere-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
  padding: 6px;
  z-index: 30;
  display: flex;
  flex-direction: column;
  min-width: 140px;
}

.filiere-menu[hidden] {
  display: none;
}

.filiere-option {
  border: none;
  background: transparent;
  color: var(--text-primary);
  text-align: left;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.filiere-option:hover {
  background: var(--gridline);
}

.filiere-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 6px;
}

.topbar-center {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: nowrap;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

/* La date est sortie du flux : sa largeur ne doit pas decaler le trio
   « precedent / Aujourd'hui / suivant », qui reste centre sur la topbar. */
.week-label {
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  margin-left: 8px;
  color: var(--text-secondary);
  font-size: 14px;
  white-space: nowrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn, .text-btn, .primary-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  border-radius: 8px;
  cursor: pointer;
}

.icon-btn {
  width: 32px;
  height: 32px;
  font-size: 16px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.text-btn {
  padding: 0 12px;
  height: 32px;
  font-size: 13px;
}

.icon-btn:hover, .text-btn:hover {
  background: var(--gridline);
}

.primary-btn {
  background: var(--series-1);
  color: #ffffff;
  border-color: transparent;
  padding: 0 16px;
  height: 36px;
  font-weight: 600;
}

.primary-btn:hover {
  filter: brightness(1.08);
}

.status-banner {
  margin: 10px 20px 0;
  padding: 8px 14px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--series-4) 18%, var(--surface-1));
  color: var(--text-primary);
  font-size: 13px;
}

.next-class-banner {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  margin: 16px 0 18px;
  padding: 14px 18px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--status-upcoming-mark) 10%, var(--surface-1));
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  color: var(--text-primary);
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}

.next-class-banner:hover {
  background: color-mix(in srgb, var(--status-upcoming-mark) 18%, var(--surface-1));
  box-shadow: 0 4px 14px rgba(0,0,0,0.12);
  transform: translateY(-1px);
}

.next-class-banner.is-ongoing {
  background: color-mix(in srgb, var(--status-ongoing-mark) 10%, var(--surface-1));
}

.next-class-banner.is-ongoing:hover {
  background: color-mix(in srgb, var(--status-ongoing-mark) 18%, var(--surface-1));
}

.next-class-banner[hidden] {
  display: none;
}

.next-class-banner .ncb-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--status-upcoming-mark);
  flex-shrink: 0;
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--status-upcoming-mark) 18%, transparent);
}

.next-class-banner.is-ongoing .ncb-dot {
  background: var(--status-ongoing-mark);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--status-ongoing-mark) 20%, transparent);
  animation: ncb-pulse 2s ease-in-out infinite;
}

@keyframes ncb-pulse {
  0%, 100% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--status-ongoing-mark) 20%, transparent); }
  50% { box-shadow: 0 0 0 7px color-mix(in srgb, var(--status-ongoing-mark) 8%, transparent); }
}

.next-class-banner .ncb-body {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.next-class-banner .ncb-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--status-upcoming);
}

.next-class-banner.is-ongoing .ncb-label {
  color: var(--status-ongoing);
}

.next-class-banner .ncb-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.next-class-banner .ncb-meta {
  font-size: 12.5px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.calendar-root {
  padding: 12px 20px 40px;
}

.calendar-wrap[hidden] {
  display: none;
}

.calendar-wrap {
  display: grid;
  grid-template-columns: 56px 1fr;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.time-col {
  border-right: 1px solid var(--gridline);
  position: relative;
}

.time-slot {
  position: absolute;
  left: 0;
  right: 0;
  font-size: 11px;
  color: var(--muted);
  text-align: right;
  padding-right: 8px;
  transform: translateY(-50%);
}

.days-grid {
  display: grid;
  overflow-x: auto;
  min-width: 0;
}

.day-header {
  padding: 8px 6px;
  box-sizing: border-box;
  text-align: center;
  border-left: 1px solid var(--gridline);
  border-bottom: 1px solid var(--gridline);
  background: var(--surface-1);
  overflow: hidden;
}

.day-header .dow {
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.day-header .dom {
  font-size: 16px;
  font-weight: 600;
  margin-top: 2px;
}

.day-header.is-today .dom {
  color: var(--series-1);
}

.day-col {
  position: relative;
  border-left: 1px solid var(--gridline);
  background-image: repeating-linear-gradient(
    to bottom,
    var(--gridline) 0,
    var(--gridline) 1px,
    transparent 1px,
    transparent var(--hour-px)
  );
}

.now-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--now-line);
  z-index: 6;
}
.now-line::before {
  content: "";
  position: absolute;
  left: -4px;
  top: -3px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--now-line);
}

.event-chip {
  position: absolute;
  border-radius: 6px;
  padding: 4px 6px;
  overflow: hidden;
  cursor: pointer;
  border: 1px solid var(--border);
  font-size: 11.5px;
  line-height: 1.25;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.event-chip .ev-title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.event-chip .ev-meta {
  opacity: 0.9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.agenda-list {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.agenda-list[hidden] {
  display: none;
}

.agenda-day {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.agenda-day-header {
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--gridline);
  background: var(--surface-1);
}

.agenda-day-header.is-today {
  color: var(--series-1);
}

.agenda-day-events {
  display: flex;
  flex-direction: column;
}

.agenda-event {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 10px 14px;
  margin: 0;
  border: none;
  border-left: 3px solid var(--series-1);
  border-bottom: 1px solid var(--gridline);
  background: var(--surface-2);
  cursor: pointer;
  text-align: left;
  width: 100%;
  font-family: inherit;
  font-size: 13px;
  color: var(--text-primary);
}

.agenda-event:last-child {
  border-bottom: none;
}

.agenda-event:hover {
  background: var(--gridline);
}

.agenda-time {
  flex-shrink: 0;
  width: 92px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  font-size: 12.5px;
}

.agenda-title {
  font-weight: 600;
}

.agenda-loc {
  color: var(--text-secondary);
  font-size: 12.5px;
  margin-top: 2px;
}

.agenda-empty {
  padding: 10px 14px;
  color: var(--muted);
  font-size: 13px;
  font-style: italic;
}

/* En mode menu, la navigation de semaine n'a pas de cible : l'API myMRS ne
   renvoie que la semaine courante. On masque le trio + la date. */
body.menu-mode .topbar-center {
  display: none;
}

.menu-view {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.menu-view[hidden] {
  display: none;
}

.menu-day {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.menu-day-header {
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--gridline);
  background: var(--surface-1);
}

.menu-day-header.is-today {
  color: var(--series-1);
}

.menu-dishes {
  display: flex;
  flex-direction: column;
}

.menu-dish {
  padding: 9px 14px;
  border-left: 3px solid var(--series-1);
  border-bottom: 1px solid var(--gridline);
  background: var(--surface-2);
}

.menu-dish:last-child {
  border-bottom: none;
}

.menu-cat {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-secondary);
  margin-bottom: 2px;
}

.menu-name {
  font-size: 13.5px;
  font-weight: 600;
}

.menu-empty {
  padding: 16px 14px;
  color: var(--muted);
  font-size: 13px;
  font-style: italic;
  text-align: center;
}

.icon-btn.is-active {
  background: var(--series-1);
  color: #ffffff;
  border-color: transparent;
}

.site-footer {
  padding: 20px 20px 28px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

.event-popover {
  position: fixed;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  max-width: min(280px, calc(100vw - 24px));
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
  z-index: 200;
  font-size: 13px;
}

.event-popover h3 {
  margin: 0 0 8px;
  font-size: 14px;
}

.event-popover .ep-row {
  display: flex;
  gap: 6px;
  margin-bottom: 4px;
  color: var(--text-secondary);
}

.event-popover .ep-close {
  position: absolute;
  top: 8px;
  right: 10px;
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
}

.days-grid {
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 860px) {
  .topbar {
    align-items: center;
    row-gap: 10px;
    padding: 10px 14px;
  }
  .topbar-left {
    order: 1;
  }
  .topbar-right {
    order: 2;
  }
  .topbar-center {
    order: 3;
    flex-basis: 100%;
    justify-content: center;
    position: static;
    transform: none;
    flex-wrap: wrap;
    row-gap: 6px;
  }
  /* Trop etroit pour loger la date a droite du trio sans deborder :
     elle passe sur sa propre ligne, le trio reste centre. */
  .week-label {
    position: static;
    transform: none;
    flex-basis: 100%;
    text-align: center;
    margin-left: 0;
  }
}

@media (max-width: 480px) {
  .topbar h1 {
    font-size: 16px;
  }
  .week-label {
    font-size: 12.5px;
  }
  .calendar-root { padding: 8px 8px 30px; }
  .day-col, .day-header { min-width: 92px; }
  .time-col { min-width: 44px; }
  .calendar-wrap { grid-template-columns: 44px 1fr; }
  .event-chip {
    font-size: 10.5px;
    padding: 3px 5px;
  }
  .icon-btn {
    width: 36px;
    height: 36px;
  }
  .next-class-banner {
    margin: 12px 0 14px;
    padding: 11px 14px;
    gap: 10px;
  }
  .next-class-banner .ncb-title {
    font-size: 13.5px;
  }
  .next-class-banner .ncb-meta {
    font-size: 11.5px;
  }
  .agenda-time {
    width: 74px;
    font-size: 11.5px;
  }
}
