/* =====================================================================
   Siège des Âges — Feuille de style
   Mobile d'abord (iPhone paysage), commandes tactiles larges.
   ===================================================================== */
:root {
  --bg: #1b120c;
  --panel: rgba(42, 27, 17, 0.92);
  --panel-2: rgba(66, 44, 28, 0.95);
  --line: rgba(255, 220, 160, 0.22);
  --text: #fbefd9;
  --muted: #cdb796;
  --gold: #ffd166;
  --accent: #f2994a;
  --accent-2: #e0662f;
  --ok: #6fd36f;
  --bad: #ff6b55;
  --player: #2f7de1;
  --enemy: #d8432f;
  --next: #a77dff;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --ctrl-h: 76px;
}

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

html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  overflow: hidden; position: fixed; top: 0; right: 0; bottom: 0; left: 0;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, sans-serif;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

#app { position: fixed; top: 0; right: 0; bottom: 0; left: 0; overflow: hidden; }
#game { position: absolute; left: 0; top: 0; display: block; touch-action: none; }
.hidden { display: none !important; }

button {
  font: inherit; color: inherit; border: none; cursor: pointer;
  touch-action: manipulation;
}

/* --------------------------------- Boutons --------------------------------- */
.btn {
  min-height: 46px; padding: 10px 16px; border-radius: 12px;
  background: linear-gradient(#6b4a2e, #4d3320);
  border: 2px solid rgba(255, 220, 160, 0.35);
  box-shadow: 0 3px 0 #2a1a0e, 0 4px 10px rgba(0, 0, 0, 0.35);
  font-weight: 700; font-size: 16px; letter-spacing: 0.2px;
  transition: transform 0.06s, filter 0.1s;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #2a1a0e; filter: brightness(1.1); }
.btn.primary { background: linear-gradient(#f5a54a, #d8692b); border-color: #ffd9a3; color: #2a1406; }
.btn.danger { background: linear-gradient(#b5452f, #7d2a1b); }
.btn.next { background: linear-gradient(#9a6ff0, #6a43c4); border-color: #d6c2ff; }
.btn.big { min-height: 56px; font-size: 19px; }
.btn[disabled] { filter: grayscale(0.8) brightness(0.65); pointer-events: none; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1; }

.icon-btn {
  width: 46px; height: 46px; border-radius: 50%;
  background: rgba(30, 18, 10, 0.75); border: 2px solid rgba(255, 220, 160, 0.4);
  font-size: 16px; font-weight: 800;
}
.icon-btn.small { width: 42px; height: 42px; font-size: 14px; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 9px; border-radius: 999px; font-size: 13px; font-weight: 700;
  background: rgba(20, 12, 6, 0.62); border: 1px solid rgba(255, 220, 160, 0.25);
  white-space: nowrap;
}
.chip.big { font-size: 15px; padding: 6px 12px; }
.chip.age { color: var(--gold); }

/* ---------------------------------- HUD ----------------------------------- */
#hud {
  position: absolute; left: 0; right: 0; top: 0;
  padding: calc(4px + var(--safe-t)) calc(8px + var(--safe-r)) 0 calc(8px + var(--safe-l));
  display: flex; align-items: flex-start; gap: 8px; pointer-events: none;
}
#hud > * { pointer-events: auto; }
.hud-base { flex: 1 1 0; min-width: 0; background: rgba(20, 12, 6, 0.55); padding: 4px 8px 6px; border-radius: 10px; }
.hud-base-label { display: flex; justify-content: space-between; font-size: 12px; font-weight: 700; margin-bottom: 3px; gap: 6px; }
.hud-base-label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hpbar { height: 10px; background: rgba(0, 0, 0, 0.55); border-radius: 6px; overflow: hidden; border: 1px solid rgba(255, 255, 255, 0.2); }
.hpbar .fill { height: 100%; width: 100%; transition: width 0.15s; }
.hpbar .fill.player { background: linear-gradient(90deg, #1d64c4, #57a3ff); }
.hpbar .fill.enemy { background: linear-gradient(90deg, #ff6b55, #b8301e); margin-left: auto; }
.hud-base.right .hpbar { display: flex; }
.hud-center { flex: 0 1 auto; display: flex; flex-direction: column; align-items: center; gap: 3px; }
.hud-row { display: flex; gap: 4px; justify-content: center; }
.hud-row.small .chip { font-size: 11px; padding: 2px 7px; }
#btn-pause { flex: 0 0 auto; }

/* -------------------------------- Commandes -------------------------------- */
#controls {
  position: absolute; left: 0; right: 0; bottom: 0; height: calc(var(--ctrl-h) + var(--safe-b));
  padding: 0 calc(8px + var(--safe-r)) var(--safe-b) calc(8px + var(--safe-l));
  display: flex; align-items: center; gap: 8px;
  background: linear-gradient(rgba(20, 12, 6, 0), rgba(20, 12, 6, 0.55) 35%);
  pointer-events: none;
}
#controls > * { pointer-events: auto; }
.fire-zone { display: flex; align-items: center; gap: 8px; }
#aim-pad {
  position: relative; width: 70px; height: 70px; border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.12), rgba(0, 0, 0, 0.35));
  border: 2px solid rgba(255, 220, 160, 0.45); touch-action: none;
}
#aim-pad .aim-ring { position: absolute; top: 22px; right: 22px; bottom: 22px; left: 22px; border-radius: 50%; border: 1px dashed rgba(255, 255, 255, 0.35); }
#aim-knob {
  position: absolute; left: 50%; top: 50%; width: 26px; height: 26px; margin: -13px 0 0 -13px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #ffe1a8, #e08a3a); border: 2px solid #fff3d6;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5); pointer-events: none;
}
#btn-fire {
  position: relative; width: 64px; height: 64px; border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, #ff9c5a, #c4421f); border: 3px solid #ffd9a3;
  font-weight: 900; font-size: 15px; color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
  box-shadow: 0 3px 0 #6a2210, 0 4px 10px rgba(0, 0, 0, 0.4);
}
#btn-fire.reloading { filter: grayscale(0.6) brightness(0.8); }
#btn-fire:active { transform: scale(0.95); }
.reload-ring { position: absolute; top: -3px; right: -3px; bottom: -3px; left: -3px; width: calc(100% + 6px); height: calc(100% + 6px); transform: rotate(-90deg); pointer-events: none; }
.reload-ring circle { fill: none; stroke-width: 5; }
.reload-ring .bg { stroke: rgba(0, 0, 0, 0.25); }
.reload-ring .fg { stroke: #fff3b0; stroke-dasharray: 175.9; stroke-dashoffset: 0; stroke-linecap: round; }
#fire-label { position: relative; }
#aim-info { display: flex; flex-direction: column; font-size: 12px; font-weight: 700; line-height: 1.3; text-shadow: 0 1px 2px #000; min-width: 40px; }
.supply { text-align: center; min-width: 56px; background: rgba(20, 12, 6, 0.6); border-radius: 12px; padding: 4px 6px; border: 1px solid rgba(255, 220, 160, 0.3); }
#supply-val { font-size: 22px; font-weight: 900; color: var(--gold); line-height: 1.05; }
.supply .lbl { font-size: 10px; color: var(--muted); }
#unit-buttons { flex: 1; display: flex; gap: 6px; justify-content: flex-end; min-width: 0; }
.unit-btn {
  position: relative; width: 62px; height: 66px; flex: 0 1 62px; min-width: 50px; border-radius: 12px; overflow: hidden;
  background: linear-gradient(#5a3d26, #3c2817); border: 2px solid rgba(255, 220, 160, 0.45);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2px;
  box-shadow: 0 3px 0 #22140a;
}
.unit-btn .ico { font-size: 24px; line-height: 1; }
.unit-btn .nm { font-size: 9px; line-height: 1.1; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.unit-btn .cost { font-size: 12px; font-weight: 800; color: var(--gold); }
.unit-btn .cd { position: absolute; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.6); pointer-events: none; }
.unit-btn.special { border-color: #d6c2ff; background: linear-gradient(#5b3f8a, #362255); }
.unit-btn.poor .cost { color: var(--bad); }
.unit-btn.poor, .unit-btn.cooldown { filter: brightness(0.8); }
.unit-btn.locked { filter: grayscale(1) brightness(0.55); }
.unit-btn.locked::after { content: '🔒'; position: absolute; top: 3px; right: 4px; font-size: 13px; }
.unit-btn:active { transform: translateY(2px); }
.unit-btn.flash { animation: flash 0.25s; }
@keyframes flash { 50% { filter: brightness(1.7); } }

/* --------------------------------- Écrans --------------------------------- */
.screen {
  position: absolute; top: 0; right: 0; bottom: 0; left: 0;
  background:
    radial-gradient(ellipse at 50% 120%, rgba(120, 70, 30, 0.55), rgba(0, 0, 0, 0) 60%),
    linear-gradient(#3b2618, #1b120c);
  padding: calc(8px + var(--safe-t)) calc(12px + var(--safe-r)) calc(8px + var(--safe-b)) calc(12px + var(--safe-l));
}
.screen.over-battle { background: rgba(15, 9, 5, 0.82); }
.screen-inner { height: 100%; display: flex; flex-direction: column; gap: 8px; max-width: 1100px; margin: 0 auto; }
.screen-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex: 0 0 auto; }
.screen-header h2 { margin: 0; font-size: 21px; text-align: center; flex: 1; }
.screen-header h2 small { color: var(--muted); font-size: 14px; font-weight: 600; }
.scrollable { flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y; overscroll-behavior: contain; padding-bottom: 8px; }

.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}
.card h3 { margin: 0 0 6px; font-size: 16px; color: var(--gold); }
.card p { margin: 4px 0; font-size: 14px; line-height: 1.4; color: var(--text); }
.note { font-size: 13px; color: var(--muted); margin-top: 4px; }
.stat-row { display: flex; justify-content: space-between; gap: 10px; font-size: 15px; padding: 3px 0; }
.stat-row b { color: var(--gold); }
.progress { height: 12px; border-radius: 7px; background: rgba(0, 0, 0, 0.45); overflow: hidden; border: 1px solid rgba(255, 255, 255, 0.15); margin-top: 4px; }
.progress .fill { height: 100%; background: linear-gradient(90deg, #f5a54a, #ffd166); width: 0; transition: width 0.6s; }
.progress .fill.full { background: linear-gradient(90deg, #7ee07e, #c6ff8a); }

/* Accueil */
.home { justify-content: center; }
.home-title { text-align: center; }
.home-title h1 {
  margin: 0; font-size: clamp(30px, 8vh, 52px); letter-spacing: 1px; color: var(--gold);
  text-shadow: 0 3px 0 #6a3416, 0 6px 18px rgba(0, 0, 0, 0.6);
}
.subtitle { margin: 2px 0 6px; color: var(--muted); font-size: 14px; }
.home-grid { display: grid; grid-template-columns: minmax(220px, 1fr) minmax(240px, 1.1fr); gap: 12px; align-items: stretch; }
.home-buttons { display: flex; flex-direction: column; gap: 8px; justify-content: center; }
.age-badge { font-size: 34px; text-align: center; }
.age-name { text-align: center; font-size: 20px; font-weight: 800; margin-bottom: 4px; }

/* Préparation */
.pre-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 10px; }
.pre-grid .big-icon { font-size: 28px; float: right; }
.roster { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.roster .r { background: rgba(0, 0, 0, 0.3); border-radius: 8px; padding: 4px 6px; font-size: 12px; }
.roster .r.locked { opacity: 0.5; }

/* Résultats */
.results-head { text-align: center; margin: 4px 0 8px; }
.results-head h1 { margin: 0; font-size: clamp(30px, 9vh, 50px); }
.results-head h1.win { color: #9dff8a; text-shadow: 0 3px 0 #2c6a1e; }
.results-head h1.lose { color: #ff8f7a; text-shadow: 0 3px 0 #6a1e14; }
.results-grid { display: grid; grid-template-columns: 1.2fr 1fr; gap: 10px; }
.score-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.score-table td { padding: 3px 4px; border-bottom: 1px dashed rgba(255, 255, 255, 0.1); }
.score-table td:last-child { text-align: right; font-weight: 800; color: var(--gold); }
.score-table tr.total td { font-size: 17px; border-bottom: none; padding-top: 6px; }
.reward { display: flex; gap: 10px; margin: 6px 0; }
.reward .rw { flex: 1; text-align: center; background: rgba(0, 0, 0, 0.3); border-radius: 10px; padding: 6px; }
.reward .rw b { display: block; font-size: 22px; color: var(--gold); }
.threshold-box { border: 2px solid var(--next); background: rgba(90, 60, 160, 0.3); border-radius: 14px; padding: 8px 10px; margin: 0 0 8px; text-align: center; }
.threshold-box p { margin: 3px 0; font-size: 14px; }
.threshold-box h3 { color: #e0d2ff; margin: 0 0 6px; }
.choice-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.choice-row .btn { flex: 1; }

/* Améliorations */
.tabs { display: flex; gap: 6px; flex: 0 0 auto; }
.tab { flex: 1; min-height: 44px; border-radius: 12px 12px 4px 4px; background: rgba(60, 40, 25, 0.8); border: 1px solid var(--line); font-weight: 800; font-size: 15px; }
.tab.active { background: linear-gradient(#f5a54a, #d8692b); color: #2a1406; }
.tab .count { font-size: 11px; font-weight: 700; opacity: 0.8; display: block; }
.legend { display: flex; gap: 6px; flex-wrap: wrap; font-size: 11px; flex: 0 0 auto; }
.lg { padding: 2px 8px; border-radius: 999px; border: 1px solid; }
.lg.owned { border-color: var(--ok); color: var(--ok); }
.lg.available { border-color: var(--gold); color: var(--gold); }
.lg.expensive { border-color: #c9a36b; color: #c9a36b; }
.lg.locked { border-color: #888; color: #aaa; }
.lg.next { border-color: var(--next); color: #cbb6ff; }
.upg-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; }
.upg {
  position: relative; background: var(--panel); border: 2px solid rgba(255, 255, 255, 0.12); border-radius: 12px;
  padding: 8px 10px 8px; display: flex; flex-direction: column; gap: 4px; min-height: 128px;
}
.upg.available { border-color: var(--gold); box-shadow: 0 0 0 1px rgba(255, 209, 102, 0.3), 0 0 14px rgba(255, 209, 102, 0.18); }
.upg.expensive { border-color: #8a6a3e; }
.upg.locked { opacity: 0.55; }
.upg.maxed { border-color: var(--ok); }
.upg.next { background: linear-gradient(rgba(90, 60, 160, 0.35), var(--panel)); }
.upg.next::before { content: 'PRÉPARATION'; position: absolute; top: -9px; right: 8px; font-size: 9px; font-weight: 800; letter-spacing: 1px; padding: 2px 6px; border-radius: 6px; background: var(--next); }
.upg .top { display: flex; gap: 8px; align-items: center; }
.upg .ico { font-size: 24px; }
.upg .name { font-weight: 800; font-size: 14px; line-height: 1.15; }
.upg .desc { font-size: 12px; color: var(--muted); line-height: 1.3; flex: 1; }
.upg .req { font-size: 11px; color: #ffb3a3; }
.pips { display: flex; gap: 3px; }
.pip { width: 12px; height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.15); }
.pip.on { background: var(--ok); }
.upg .buy { min-height: 38px; padding: 6px 10px; font-size: 14px; }

/* Âges */
.ages-row { display: grid; grid-template-columns: repeat(3, minmax(200px, 1fr)); gap: 10px; }
.age-card { display: flex; flex-direction: column; gap: 6px; }
.age-card.current { border: 2px solid var(--gold); }
.age-card.past { opacity: 0.7; }
.age-card.future { border-style: dashed; }
.age-card .head { display: flex; align-items: center; gap: 8px; }
.age-card .head .ic { font-size: 32px; }
.age-card .status { font-size: 12px; font-weight: 800; padding: 2px 8px; border-radius: 999px; background: rgba(0, 0, 0, 0.35); align-self: flex-start; }
.age-card .units { font-size: 13px; color: var(--muted); }

/* Démo */
.demo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 8px; }
.demo-grid .card .btn { width: 100%; margin-top: 6px; }
.opt-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; padding: 6px 0; }
.seg { display: flex; gap: 4px; }
.seg button { min-width: 52px; min-height: 40px; border-radius: 10px; background: rgba(0, 0, 0, 0.35); border: 1px solid var(--line); font-weight: 800; }
.seg button.on { background: var(--accent); color: #2a1406; }

.help-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }

/* Overlays */
.overlay { position: absolute; top: 0; right: 0; bottom: 0; left: 0; background: rgba(10, 5, 2, 0.7); display: flex; align-items: center; justify-content: center; z-index: 20; padding: 12px; }
.dialog {
  background: var(--panel-2); border: 2px solid rgba(255, 220, 160, 0.35); border-radius: 16px; padding: 14px 18px;
  min-width: 280px; max-width: 600px; max-height: 96%; overflow: hidden; display: flex; flex-direction: column; gap: 8px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}
.dialog h2 { margin: 0; text-align: center; }
#modal-body { font-size: 14px; line-height: 1.4; overflow-y: auto; min-height: 0; flex: 1 1 auto; -webkit-overflow-scrolling: touch; touch-action: pan-y; }
#modal-body p { margin: 4px 0; }
#modal .btn-row { flex: 0 0 auto; }
#modal-body ul { margin: 4px 0; padding-left: 18px; }

#toast {
  position: absolute; left: 50%; top: calc(56px + var(--safe-t)); transform: translateX(-50%);
  background: rgba(20, 12, 6, 0.92); border: 1px solid var(--gold); color: var(--text);
  padding: 8px 14px; border-radius: 10px; font-size: 14px; font-weight: 700; z-index: 30; pointer-events: none;
  max-width: 80%; text-align: center;
}

/* Orientation : invite à tourner en paysage */
#rotate { display: none; position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 100; background: #1b120c; align-items: center; justify-content: center; text-align: center; padding: 24px; }
.rotate-box p { font-size: 18px; }
.rotate-icon { font-size: 54px; animation: rot 2s infinite ease-in-out; }
@keyframes rot { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(90deg); } }
@media (orientation: portrait) and (max-width: 900px) {
  #rotate { display: flex; }
}

/* Écrans peu hauts (iPhone SE paysage, etc.) */
@media (max-height: 380px) {
  :root { --ctrl-h: 70px; }
  .btn { min-height: 42px; padding: 8px 12px; font-size: 15px; }
  .btn.big { min-height: 48px; font-size: 17px; }
  .unit-btn { height: 60px; width: 56px; flex-basis: 56px; }
  .unit-btn .ico { font-size: 21px; }
  #aim-pad { width: 62px; height: 62px; }
  #btn-fire { width: 58px; height: 58px; }
  .upg { min-height: 118px; }
  .home-title h1 { font-size: 30px; }
  .subtitle { display: none; }
}
@media (max-width: 700px) {
  .home-grid, .results-grid { grid-template-columns: 1fr 1fr; }
  #aim-info { display: none; }
  .ages-row { grid-template-columns: repeat(3, minmax(180px, 1fr)); overflow-x: auto; }
}
