/**
 * Cap Permis — Optimisations iPhone / mobile
 * Safe areas, zones tactiles 44px, simulateur plein écran
 */

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --nav-offset: calc(76px + var(--safe-bottom));
}

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

body {
  min-height: 100vh;
  min-height: -webkit-fill-available;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  overscroll-behavior-y: none;
}

.app {
  padding-bottom: var(--nav-offset);
  padding-left: max(12px, var(--safe-left));
  padding-right: max(12px, var(--safe-right));
}

/* Zones tactiles Apple HIG ≥ 44px */
button,
.answer,
.menu-item,
.lesson-item,
.sim-scenario-item,
.exam-dot {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.nav {
  bottom: calc(8px + var(--safe-bottom));
  left: max(12px, var(--safe-left));
  right: max(12px, var(--safe-right));
  width: auto;
  transform: none;
  padding: 6px 6px calc(6px + var(--safe-bottom));
  grid-template-columns: repeat(6, 1fr);
  gap: 2px;
}

.nav button {
  min-height: 48px;
  padding: 6px 2px;
  font-size: 0.65rem;
  line-height: 1.15;
}

.nav button span {
  font-size: 1.25rem;
  margin-bottom: 3px;
}

/* Mode simulateur : masquer nav pour gagner de la place */
body.sim-mode .nav {
  display: none;
}

body.sim-mode .app {
  padding-bottom: calc(12px + var(--safe-bottom));
}

body.sim-mode #simulator.view.active {
  min-height: calc(100dvh - var(--safe-top) - 24px);
}

/* iPhone portrait ≤ 430px (14/15/16) */
@media (max-width: 430px) {
  .app {
    padding: 10px 10px var(--nav-offset);
  }

  header {
    padding-top: var(--safe-top);
  }

  .card {
    padding: 16px;
    border-radius: 18px;
  }

  .hero h2 {
    font-size: 1.55rem;
  }

  .hero .actions {
    flex-direction: column;
  }

  .hero .actions .btn {
    width: 100%;
    justify-content: center;
    min-height: 48px;
  }

  .stats {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .stat {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 14px;
  }

  .stat b {
    font-size: 1.15rem;
  }

  .menu-grid,
  .grid,
  .grid-2,
  .code-modes {
    grid-template-columns: 1fr;
  }

  .menu-item {
    min-height: 56px;
    padding: 14px 16px;
  }

  .section-head {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .section-head .btn {
    width: 100%;
  }

  .lesson-layout {
    gap: 12px;
  }

  .lesson-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    max-height: 200px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .lesson-item {
    min-height: 44px;
    font-size: 0.82rem;
    padding: 10px;
  }

  .lesson-content {
    overflow-x: hidden;
  }

  .lesson-content table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: 0.85rem;
  }

  .visual-box {
    gap: 10px;
    padding: 12px;
  }

  .visual-box svg {
    width: 44vw !important;
    max-width: 130px;
    height: auto !important;
  }

  .question {
    font-size: 1.05rem;
  }

  .answer {
    min-height: 48px;
    padding: 14px 16px;
    font-size: 0.95rem;
  }

  .quiz-controls {
    flex-direction: column;
  }

  .quiz-controls .btn,
  .quiz-controls > div {
    width: 100%;
  }

  .quiz-controls > div {
    display: flex;
    gap: 8px;
  }

  .exam-nav {
    gap: 5px;
    max-height: 120px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 4px;
  }

  .exam-dot {
    min-width: 34px;
    min-height: 34px;
    font-size: 0.7rem;
  }

  .flashcard {
    min-height: 240px;
  }

  .face {
    padding: 20px;
  }

  .face strong {
    font-size: 1.15rem;
  }

  .result .score {
    font-size: 2.6rem;
  }

  .source-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -8px;
    padding: 0 8px;
  }

  .source-table {
    font-size: 0.78rem;
    min-width: 560px;
  }

  .code-modes .btn {
    width: 100%;
    min-height: 48px;
  }

  .actions .btn {
    min-height: 44px;
  }

  .error-list li {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: flex-start;
  }
}

/* iPhone très petit (SE, 360px) */
@media (max-width: 375px) {
  .nav button {
    font-size: 0.58rem;
    min-height: 46px;
  }

  .brand h1 {
    font-size: 1rem;
  }

  .subtitle {
    font-size: 0.75rem;
  }

  .streak {
    font-size: 0.75rem;
    padding: 7px 10px;
  }
}

/* Simulateur mobile */
@media (max-width: 768px) {
  .sim-touch {
    display: grid;
    position: sticky;
    bottom: calc(4px + var(--safe-bottom));
    background: linear-gradient(transparent, rgba(6, 19, 31, 0.92) 20%);
    padding-top: 12px;
    z-index: 5;
  }

  .sim-touch button {
    min-height: 54px;
    font-size: 0.88rem;
    user-select: none;
    -webkit-user-select: none;
  }

  .sim-canvas-wrap {
    height: min(42dvh, 360px);
    min-height: 220px;
  }

  .sim-hud {
    font-size: 0.72rem;
    gap: 6px 10px;
    padding: 10px;
  }

  .sim-hud span {
    flex: 1 1 calc(50% - 8px);
    min-width: 0;
  }

  .sim-hud b {
    display: inline-block;
    word-break: break-word;
  }

  .sim-landscape-hint {
    display: block;
  }

  .sim-scenario-item {
    min-height: 56px;
    padding: 16px;
    width: 100%;
  }

  .sim-result .actions {
    flex-direction: column;
  }

  .sim-result .actions .btn {
    width: 100%;
  }
}

/* Simulateur paysage iPhone */
@media (max-width: 900px) and (orientation: landscape) {
  body.sim-mode .app {
    padding-bottom: calc(6px + var(--safe-bottom));
  }

  .sim-canvas-wrap {
    height: min(58dvh, 280px);
    min-height: 180px;
  }

  .sim-touch {
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
  }

  .sim-touch .wide {
    grid-column: span 1;
  }

  .sim-touch button {
    min-height: 44px;
    padding: 10px 6px;
    font-size: 0.72rem;
  }

  .sim-landscape-hint {
    display: none;
  }
}

/* Masquer légende clavier sur mobile (inutile) */
@media (hover: none) and (pointer: coarse) {
  .sim-canvas-wrap + .sim-hud + .sim-touch ~ * {
    /* noop */
  }
}
