:root {
  --c-day12: #3E6C4A; --c-night12: #1d3557; --c-day10: #95d5b2;
  --c-conges: #f4a261; --c-rtt: #ffd166;
  --c-empty: #ffffff; --c-outside: #f1f3f4;
  --fg: #1a1a1a; --bg: #ffffff; --accent: #3F67A6;
  --border: #dadce0;
  --surface: #f6f8fa;
  --on-accent: #ffffff;
  --on-night: #ffffff;
  --danger: #d93025;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --c-day12: #2b6a8a; --c-night12: #457b9d; --c-day10: #2d6a4f;
    --c-conges: #b56a22; --c-rtt: #9a7d1e;
    --c-empty: #1e1f22; --c-outside: #141516;
    --fg: #e8eaed; --bg: #121212; --accent: #8ab4f8;
    --border: #3c4043;
    --surface: #202124;
    --on-accent: #10161f;
    --on-night: #ffffff;
    --danger: #f28b82;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  overflow: hidden;
  overscroll-behavior: none;
}

.hidden { display: none !important; }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }

/* Bannières */
.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--accent);
  color: var(--on-accent);
  font-size: 14px;
}
.banner.warning { background: var(--danger); color: #fff; justify-content: center; text-align: center; }
.banner button {
  min-height: 36px; padding: 0 14px; border: 1px solid currentColor; border-radius: 8px;
  background: transparent; color: inherit; font-weight: 600;
}

/* Barre supérieure */
#topbar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.banner:not(.hidden) ~ #topbar { padding-top: 8px; }

.row { display: flex; align-items: center; gap: 8px; }
.row-month { justify-content: space-between; }
.row-toggles { justify-content: center; gap: 12px; }
.row-actions { gap: 8px; }

.icon-btn {
  width: 44px; height: 44px; flex: 0 0 auto;
  border: none; border-radius: 50%;
  background: transparent; font-size: 26px; line-height: 1;
}
.icon-btn:active { background: var(--surface); }

#month-label {
  flex: 1 1 auto; min-height: 44px;
  border: none; background: transparent;
  font-size: 20px; font-weight: 600; text-align: center;
}
#month-label:active { background: var(--surface); border-radius: 8px; }

/* Toggles segmentés */
.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface);
}
.seg-btn {
  min-width: 64px; min-height: 44px; padding: 0 16px;
  border: none; background: transparent; font-weight: 500;
}
.seg-btn + .seg-btn { border-left: 1px solid var(--border); }
.seg-btn.active { background: var(--accent); color: var(--on-accent); font-weight: 700; }
.segmented.disabled { opacity: 0.4; }
.segmented.disabled .seg-btn,
.seg-btn.disabled { pointer-events: none; cursor: not-allowed; }
.seg-btn.disabled { opacity: 0.4; }

/* Barre de type à 4 choix : segments à largeur égale pour tenir sur mobile */
#toggle-mode { width: 100%; max-width: 440px; }
#toggle-mode .seg-btn { flex: 1 1 0; min-width: 0; padding: 0 8px; }

.action-btn {
  flex: 1 1 0; min-height: 44px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); font-weight: 600;
}
.action-btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.action-btn:active { filter: brightness(0.92); }

/* Calendrier */
#calendar {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: env(safe-area-inset-bottom);
  touch-action: pan-y;
}

#weekdays {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  position: sticky; top: 0; z-index: 1;
  background: var(--bg);
  padding: 6px 4px 4px;
  text-align: center;
  font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.03em;
  opacity: 0.7;
}

#grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-auto-rows: auto;
  gap: 3px;
  padding: 4px;
  flex: 0 0 auto; /* la grille garde sa hauteur naturelle (cellules carrées) et défile si besoin */
}

.day-cell {
  position: relative;
  min-width: 44px; min-height: 44px;
  aspect-ratio: 1 / 1; /* cellules (quasi) carrées quelle que soit la fenêtre */
  display: flex; flex-direction: column;
  align-items: flex-start; justify-content: space-between;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--c-empty);
  color: var(--fg);
  text-align: left;
  user-select: none; -webkit-user-select: none;
}
button.day-cell:active { transform: scale(0.96); }

.day-num { font-size: 12px; font-weight: 600; line-height: 1; }
.shift-label {
  align-self: center; margin-top: auto;
  font-size: 14px; font-weight: 700; line-height: 1;
}

.day-cell.shift-day12 { background: var(--c-day12); border-color: transparent; color: var(--on-night); }
.day-cell.shift-night12 { background: var(--c-night12); border-color: transparent; color: var(--on-night); }
.day-cell.shift-day10 { background: var(--c-day10); border-color: transparent; }
.day-cell.shift-conges { background: var(--c-conges); border-color: transparent; }
.day-cell.shift-rtt { background: var(--c-rtt); border-color: transparent; }
.shift-day12, .shift-day10, .shift-conges, .shift-rtt { color: #10202a; }
@media (prefers-color-scheme: dark) {
  .shift-day12, .shift-day10, .shift-conges, .shift-rtt { color: #ffffff; }
}
.shift-night12 { color: var(--on-night); }

.day-cell.outside {
  background: var(--c-outside);
  color: var(--fg);
  opacity: 0.45;
  border-color: transparent;
  pointer-events: none;
}

/* Marqueur jour férié : encart hachuré « F » en haut à droite */
.holiday-badge {
  position: absolute; top: 3px; right: 3px; z-index: 1;
  min-width: 15px; height: 15px; padding: 0 2px;
  display: flex; align-items: center; justify-content: center;
  font-size: 9px; font-weight: 800; line-height: 1;
  color: var(--fg);
  border: 1px solid var(--border); border-radius: 3px;
  background-color: var(--bg);
  background-image: repeating-linear-gradient(45deg, var(--danger) 0 1px, transparent 1px 3px);
}

/* Overlays (paramètres + synthèse) */
#settings-overlay, #stats-overlay {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0, 0, 0, 0.5);
}
.settings-panel {
  width: 100%; max-width: 520px; max-height: 100%;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 16px;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  padding-top: 16px;
  background: var(--bg);
  border-radius: 16px 16px 0 0;
}
@media (min-width: 600px) {
  #settings-overlay, #stats-overlay { align-items: center; }
  .settings-panel { border-radius: 16px; }
}
.settings-head { display: flex; align-items: center; justify-content: space-between; }
.settings-head h2 { margin: 0; font-size: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
.field input, .field select {
  min-height: 44px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); font-size: 16px; font-weight: 400;
}
.field input:focus, .field select:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.danger-btn {
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--danger); border-radius: 10px;
  background: transparent; color: var(--danger); font-weight: 600;
}
.danger-btn:active { background: var(--surface); }

/* Synthèse des heures */
.stats-yearnav {
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
#stats-year { min-width: 96px; text-align: center; font-size: 20px; font-weight: 700; }

.stats-cards { display: flex; flex-wrap: wrap; gap: 10px; }
.stat-card {
  flex: 1 1 92px; min-width: 0;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 14px 8px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); text-align: center;
}
.stat-card .stat-val { font-size: 22px; font-weight: 700; line-height: 1.1; }
.stat-card .stat-lbl { font-size: 12px; font-weight: 600; opacity: 0.75; }
.stat-card.jour { background: var(--c-day12); border-color: transparent; color: #10202a; }
.stat-card.nuit { background: var(--c-night12); border-color: transparent; color: var(--on-night); }
.stat-card.conges { background: var(--c-conges); border-color: transparent; color: #10202a; }
.stat-card.rtt { background: var(--c-rtt); border-color: transparent; color: #10202a; }
@media (prefers-color-scheme: dark) {
  .stat-card.jour, .stat-card.conges, .stat-card.rtt { color: #ffffff; }
}

.stats-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.stats-table { width: 100%; min-width: 320px; border-collapse: collapse; font-size: 14px; }
.stats-table th, .stats-table td { padding: 8px 5px; text-align: right; white-space: nowrap; }
.stats-table th:first-child, .stats-table td:first-child { text-align: left; }
.stats-table thead th {
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em;
  opacity: 0.7; border-bottom: 1px solid var(--border);
}
.stats-table tbody td { border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; }
.stats-table tr.empty td { opacity: 0.4; }
.stats-table tr.current td { background: color-mix(in srgb, var(--accent) 14%, transparent); font-weight: 600; }
.stats-table tr.total td {
  border-top: 2px solid var(--border); border-bottom: none;
  font-weight: 700; font-size: 16px;
}
.stats-subhead { margin: 2px 0 -4px; font-size: 15px; }
.stats-note { margin: 0; font-size: 12px; opacity: 0.6; text-align: center; line-height: 1.5; }

/* Toast */
#toast {
  position: fixed; left: 50%; z-index: 100;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 20px;
  border-radius: 24px;
  background: var(--fg); color: var(--bg);
  font-size: 15px; font-weight: 500; text-align: center;
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s ease;
}
#toast.show { opacity: 1; }

@media (max-width: 360px) {
  .seg-btn { min-width: 52px; padding: 0 10px; }
  #month-label { font-size: 18px; }
}
@media (prefers-reduced-motion: reduce) {
  #toast { transition: none; }
  button.day-cell:active { transform: none; }
}
