:root {
  --bg: #fafaf7;
  --ink: #222;
  --black-cell: #1a1a1a;
  --hint: #1565C0;
  --user: #333;
  --selected: #fff9c4;
  --highlight: #c8e6c9;
  --grid-line: #444;
  --grid-thin: #aaa;
  --error: #d32f2f;
  --success: #388e3c;
  --muted: #777;
  --tap-target: 44px;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.5;
}

/* ═══════════════════════════════════════════════════════════════
   MOBILE FIRST (base = smartphone ~360-414px)
   ═══════════════════════════════════════════════════════════════ */

/* ─── Header ─── */
header {
  background: #fff;
  padding: 12px 16px;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  position: relative;
}
header h1 {
  margin: 0; font-size: 20px; letter-spacing: 0.5px;
}
header h1 a { color: inherit; text-decoration: none; }
header .subtitle {
  color: var(--muted); font-style: italic; font-size: 13px;
  margin-left: 6px; flex-grow: 1;
}

/* Bouton hamburger visible uniquement sur mobile */
.nav-toggle {
  display: block;
  background: none; border: none;
  font-size: 24px; padding: 4px 8px;
  cursor: pointer; color: var(--ink);
  min-height: var(--tap-target); min-width: var(--tap-target);
  border-radius: 6px;
}
.nav-toggle:active { background: #f0f0ed; }

/* Nav cachée par défaut sur mobile, affichée en dropdown si .open */
header nav {
  display: none;
  position: absolute;
  top: 100%; left: 0; right: 0;
  background: #fff;
  flex-direction: column;
  padding: 8px 16px 16px;
  border-bottom: 1px solid #eee;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}
header nav.open { display: flex; }
header nav a {
  padding: 14px 4px !important;
  font-size: 16px !important;
  border-bottom: 1px solid #f5f5f5;
  margin: 0 !important;
  min-height: var(--tap-target);
  display: flex;
  align-items: center;
}
header nav a:last-child { border-bottom: none; }

/* ─── Main ─── */
main {
  max-width: 100%;
  margin: 0 auto;
  padding: 16px;
}

/* ─── Boutons ─── */
button, .btn {
  padding: 12px 14px;
  border: none;
  border-radius: 6px;
  font-size: 15px;
  cursor: pointer;
  transition: all 0.1s;
  font-weight: 500;
  min-height: var(--tap-target);
  font-family: inherit;
}
button.primary { background: var(--hint); color: #fff; }
button.primary:active { background: #0d47a1; }
button.secondary { background: #f0f0ed; color: var(--ink); }
button.secondary:active { background: #e0e0dd; }
button.danger { background: #fff; color: var(--error); border: 1px solid var(--error); }
button.danger:active { background: #ffebee; }

/* ─── Accueil : grille puzzles (mobile = 1 colonne) ─── */
.puzzle-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
.puzzle-card {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 16px;
  cursor: pointer;
  text-align: center;
  min-height: var(--tap-target);
}
.puzzle-card h3 { margin: 0 0 6px 0; font-size: 16px; }
.puzzle-card .meta { color: var(--muted); font-size: 13px; }
.puzzle-card .stars { color: #f9a825; }

/* ─── Page de jeu (mobile = colonne) ─── */
.game {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.game-grid {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 12px;
}
.game-grid svg {
  width: 100%;
  height: auto;
  max-width: 100%;
  display: block;
  margin: 0 auto;
}
.game-info {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 16px;
}
.game-info h2 { margin: 0 0 4px 0; font-size: 18px; }
.game-info .puzzle-meta {
  color: var(--muted); font-size: 13px; margin-bottom: 12px;
}

.timer {
  font-size: 26px;
  font-family: "SF Mono", Menlo, monospace;
  text-align: center;
  margin: 12px 0;
  padding: 10px;
  background: #f8f8f5;
  border-radius: 6px;
}

.btn-group {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}
.btn-group button { width: 100%; }

/* ─── Numpad (mobile = boutons gros) ─── */
.numpad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  margin-top: 12px;
}
.numpad button {
  padding: 16px 0;
  background: #fff;
  border: 1px solid #ddd;
  font-size: 20px;
  font-family: "SF Mono", Menlo, monospace;
  cursor: pointer;
  min-height: 48px;
}
.numpad button:active { background: #f0f0ed; }
.numpad button.erase { background: #fff5f5; color: var(--error); }

/* ─── Cell SVG ─── */
.cell-rect {
  cursor: pointer;
  transition: fill 0.1s;
}
.cell-rect.selected { stroke: var(--hint, #1976d2); stroke-width: 3; }
.cell-rect.hint-target { fill: var(--highlight); animation: pulse 1.2s ease-in-out infinite; }
.cell-rect.correct { fill: #c8e6c9 !important; }
.cell-rect.incorrect { fill: #ffcdd2 !important; }
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}
.cell-text { user-select: none; pointer-events: none; }
.cell-text.hint { fill: var(--hint); }
.cell-text.user { fill: var(--user); }

/* ─── Hint box, feedback ─── */
.hint-explanation {
  background: #fff9c4;
  border-left: 4px solid #f9a825;
  padding: 12px;
  border-radius: 6px;
  margin: 12px 0;
  font-size: 13px;
  line-height: 1.5;
}
.hint-explanation .tech-tag {
  display: inline-block;
  background: #f9a825;
  color: #fff;
  padding: 2px 8px;
  border-radius: 3px;
  font-weight: bold;
  font-size: 11px;
  margin-right: 6px;
}
.feedback {
  padding: 12px;
  border-radius: 6px;
  margin: 12px 0;
  font-size: 14px;
}
.feedback.ok { background: #c8e6c9; color: #1b5e20; }
.feedback.err { background: #ffcdd2; color: #b71c1c; }

/* ─── Tables : scroll horizontal sur mobile ─── */
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
table { font-size: 13px; width: 100%; }

/* ─── Footer ─── */
footer {
  text-align: center;
  padding: 20px 16px 32px;
  color: var(--muted);
  font-size: 12px;
}
footer a { color: var(--muted); text-decoration: none; }
footer a:active { text-decoration: underline; }


/* ═══════════════════════════════════════════════════════════════
   TABLET (≥ 600px)
   ═══════════════════════════════════════════════════════════════ */
@media (min-width: 600px) {
  body { font-size: 16px; }
  
  header { padding: 14px 20px; }
  header h1 { font-size: 22px; }
  header .subtitle { font-size: 14px; }
  
  /* Nav inline, hamburger caché */
  .nav-toggle { display: none; }
  header nav {
    display: flex !important;
    position: static;
    flex-direction: row;
    padding: 0;
    border: none;
    background: transparent;
    box-shadow: none;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px;
  }
  header nav a {
    padding: 6px 10px !important;
    font-size: 14px !important;
    border: none;
    min-height: auto;
  }
  
  main { padding: 24px; }
  
  .puzzle-list { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
  
  .game { display: grid; grid-template-columns: 1fr; gap: 20px; }
  .game-grid { padding: 16px; }
  
  .btn-group { grid-template-columns: 1fr; }
}


/* ═══════════════════════════════════════════════════════════════
   DESKTOP (≥ 1024px)
   ═══════════════════════════════════════════════════════════════ */
@media (min-width: 1024px) {
  header { padding: 16px 24px; }
  
  main { max-width: 1100px; padding: 24px; }
  
  /* Page de jeu : grille + sidebar */
  .game {
    grid-template-columns: 1fr 320px;
    gap: 32px;
    align-items: start;
  }
  .game-grid { padding: 24px; }
  .game-grid svg { max-width: 600px; }
}


/* ═══════════════════════════════════════════════════════════════
   Utilitaires globaux
   ═══════════════════════════════════════════════════════════════ */

/* Inputs touch-friendly */
input[type=text], input[type=email], input[type=number], select, textarea {
  font-size: 16px;  /* évite le zoom auto iOS sur focus */
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-family: inherit;
  min-height: var(--tap-target);
  width: 100%;
  box-sizing: border-box;
}

/* Sticky top sur mobile (timer en haut quand on scrolle) */
.game-info { position: relative; }

/* Hide-on-mobile / hide-on-desktop */
@media (max-width: 599px) {
  .hide-mobile { display: none !important; }
}
@media (min-width: 600px) {
  .show-mobile-only { display: none !important; }
}


/* ═══════════════════════════════════════════════════════════════
   Endgame modal (fin de grille)
   ═══════════════════════════════════════════════════════════════ */

.egm-overlay {
  position: fixed; inset: 0;
  background: rgba(20, 20, 30, 0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  padding: 16px;
  animation: egm-fade-in 0.18s ease-out;
}
@keyframes egm-fade-in { from { opacity: 0; } to { opacity: 1; } }

.egm-modal {
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25), 0 4px 12px rgba(0,0,0,0.08);
  width: 100%;
  max-width: 460px;
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 24px 20px 20px;
  position: relative;
  animation: egm-pop-in 0.22s cubic-bezier(0.18, 1.2, 0.4, 1);
}
@keyframes egm-pop-in { from { transform: scale(0.92); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.egm-close {
  position: absolute;
  top: 8px; right: 10px;
  background: transparent; border: none;
  font-size: 26px; line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 6px;
  min-height: var(--tap-target); min-width: var(--tap-target);
}
.egm-close:active { background: #f0f0ed; }

.egm-header {
  text-align: center;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0ed;
  margin-bottom: 16px;
}
.egm-emoji { font-size: 42px; line-height: 1; margin-bottom: 6px; }
.egm-title { margin: 0 0 4px; font-size: 20px; font-weight: 600; }
.egm-subtitle { color: var(--muted); font-size: 13px; }

.egm-stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}
.egm-stat-card {
  background: #f8f8f5;
  border-radius: 8px;
  padding: 10px 6px;
  text-align: center;
}
.egm-stat-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; }
.egm-stat-value {
  font-size: 26px; font-weight: 700; color: var(--hint);
  font-family: "SF Mono", Menlo, monospace;
  margin: 4px 0 2px;
}
.egm-stat-sub { font-size: 10px; color: var(--muted); }

.egm-details {
  list-style: none;
  padding: 0;
  margin: 0 0 14px;
  font-size: 13px;
  color: #444;
}
.egm-details li {
  padding: 6px 10px;
  background: #fafaf7;
  border-radius: 6px;
  margin-bottom: 4px;
}

.egm-auth-prompt {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  background: linear-gradient(135deg, #e3f2fd, #fff9c4);
  border-left: 4px solid var(--hint);
  padding: 14px;
  border-radius: 8px;
  margin-bottom: 14px;
}
.egm-auth-icon { font-size: 28px; line-height: 1; flex-shrink: 0; }
.egm-auth-body { flex: 1; min-width: 0; }
.egm-auth-title { font-weight: 600; font-size: 14px; margin-bottom: 4px; }
.egm-auth-text { font-size: 12px; color: #555; line-height: 1.5; margin-bottom: 10px; }

.egm-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}
.egm-btn {
  display: block;
  text-align: center;
  text-decoration: none;
  padding: 12px 14px;
  border-radius: 6px;
  font-size: 15px; font-weight: 500;
  border: none; cursor: pointer;
  min-height: var(--tap-target);
  font-family: inherit;
  transition: opacity 0.1s, background 0.1s;
}
.egm-btn:disabled { opacity: 0.6; cursor: wait; }
.egm-btn.primary { background: var(--hint); color: #fff; }
.egm-btn.primary:active { background: #0d47a1; }
.egm-btn.secondary { background: #f0f0ed; color: var(--ink); }
.egm-btn.secondary:active { background: #e0e0dd; }
.egm-btn.ghost { background: transparent; color: var(--muted); padding: 10px; font-size: 13px; }
.egm-btn.ghost:active { background: #f8f8f5; }

@media (min-width: 600px) {
  .egm-modal { padding: 28px 28px 22px; max-width: 500px; }
  .egm-title { font-size: 22px; }
  .egm-emoji { font-size: 48px; }
  .egm-actions { grid-template-columns: 2fr 1fr 1fr; }
  .egm-btn.ghost { grid-column: 1 / -1; }
}

/* Cache les noms de techniques quand la préférence est désactivée */
body.hide-tech-tags .tech-tag { display: none; }
body.hide-tech-tags .hint-explanation { padding-left: 12px; }

/* Bouton "J'ai fini !" — apparait quand la grille est complète */
.btn-finish-cta {
    background: linear-gradient(135deg, #43a047, #2e7d32) !important;
    box-shadow: 0 3px 8px rgba(67,160,71,0.4);
    animation: finish-pulse 1.8s ease-in-out infinite;
    min-height: 52px !important;
}
.btn-finish-cta:disabled {
    background: #aaa !important;
    box-shadow: none;
    animation: none;
    cursor: wait;
}
.btn-finish-cta:active:not(:disabled) {
    background: #1b5e20 !important;
    transform: translateY(1px);
}
@keyframes finish-pulse {
    0%, 100% { box-shadow: 0 3px 8px rgba(67,160,71,0.4); }
    50% { box-shadow: 0 3px 14px rgba(67,160,71,0.7), 0 0 0 4px rgba(67,160,71,0.15); }
}

/* Spinner inline pour les boutons en chargement */
.btn-spinner {
    display: inline-block;
    width: 14px; height: 14px;
    border: 2px solid rgba(255,255,255,0.35);
    border-top-color: #fff;
    border-radius: 50%;
    animation: btn-spin 0.6s linear infinite;
    vertical-align: middle;
    margin-right: 6px;
}
@keyframes btn-spin { to { transform: rotate(360deg); } }

/* Stats en haut de la sidebar play (score max + cases restantes) */
.game-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin-bottom: 12px;
}
.game-stats .stat-block {
    background: #f8f8f5;
    border-radius: 6px;
    padding: 8px 10px;
    text-align: center;
}
.game-stats .stat-label {
    font-size: 10px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.4px;
}
.game-stats .stat-value {
    font-size: 18px;
    font-weight: 600;
    color: var(--ink);
    font-family: "SF Mono", Menlo, monospace;
    margin-top: 2px;
}
.game-stats .stat-block.completed .stat-value { color: var(--success); }


/* ─── Mode compagnon (suggestion + erreur) ───────────────────────────── */
.cell.companion-suggest, rect.companion-suggest {
    fill: #fff59d !important;  /* jaune plus marqué (entre #fff9c4 et #ffeb3b) */
    transition: fill 0.2s ease;
}
.cell.companion-error, rect.companion-error {
    fill: #ffe0b2 !important;  /* orange clair */
    stroke: #fb8c00 !important;
    stroke-width: 2 !important;
}
.cell.companion-suggest.companion-error, rect.companion-suggest.companion-error {
    fill: #ffe0b2 !important;  /* erreur prime sur suggestion */
}

#companion-explain {
    background: #fff3e0;
    border-left: 4px solid #fb8c00;
    border-radius: 6px;
    padding: 12px 16px;
    margin-top: 10px;
    font-size: 14px;
    line-height: 1.45;
    color: #424242;
    display: none;
}
#companion-explain strong { color: #e65100; }
#companion-explain .companion-close {
    float: right;
    background: transparent;
    border: none;
    cursor: pointer;
    color: #888;
    font-size: 18px;
    line-height: 1;
    padding: 0;
    margin-left: 8px;
}
