@font-face { font-family: "Inter"; src: url("../assets/ui/Inter-Medium.otf") format("opentype"); font-weight: 500; font-display: swap; }
@font-face { font-family: "Inter"; src: url("../assets/ui/Inter-SemiBold.otf") format("opentype"); font-weight: 600; font-display: swap; }

:root {
  --bg: #0d1017;
  --panel: rgba(20, 23, 32, 0.92);
  --panel-2: rgba(34, 38, 52, 0.9);
  --line: rgba(255, 255, 255, 0.1);
  --text: #eef1f7;
  --muted: #98a1b3;
  --accent: #4aa3ff;
  --gold: #ffd24a;
  --danger: #ff6b6b;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-weight: 500;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
button:disabled { cursor: default; }

#app { position: fixed; inset: 0; }
.screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  padding: var(--safe-t) env(safe-area-inset-right, 0px) var(--safe-b) env(safe-area-inset-left, 0px);
  background: radial-gradient(120% 90% at 30% 20%, #2a3550 0%, #121622 55%, #07090e 100%);
}

/* ------------------------------------------------------------ Boutons */
.btn {
  display: block; width: 100%;
  padding: 14px 18px; margin-top: 10px;
  border-radius: 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  font-weight: 600; font-size: 17px;
  transition: transform 0.08s, background 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: #06121f; border-color: transparent; }
.btn.danger { color: var(--danger); }
@media (hover: hover) { .btn:hover { filter: brightness(1.12); } }
.icon-btn {
  width: 44px; height: 44px; flex: none;
  border-radius: 12px; background: var(--panel-2); border: 1px solid var(--line);
  font-size: 24px; line-height: 1;
}
.icon-btn:disabled { opacity: 0.3; }

/* ------------------------------------------------------------ Menu */
#menu { align-items: center; justify-content: center; text-align: center; padding-left: 24px; padding-right: 24px; }
.logo { width: 112px; height: 112px; border-radius: 26px; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55); }
h1 { font-size: clamp(40px, 11vw, 64px); margin: 18px 0 4px; font-weight: 600; letter-spacing: -0.02em; color: var(--accent); }
.tagline { color: var(--muted); margin: 0 0 22px; font-size: 17px; }
.menu-buttons { width: min(340px, 100%); }

/* ------------------------------------------------------------ Barres */
.bar { display: flex; align-items: center; gap: 10px; padding: 10px 12px; flex: none; }
.tabs { display: flex; gap: 6px; flex: 1; min-width: 0; }
.tab {
  flex: 1; padding: 11px 6px; border-radius: 12px;
  background: var(--panel-2); border: 1px solid var(--line);
  font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tab.on { background: var(--accent); color: #06121f; border-color: transparent; }
.stars-total { color: var(--gold); font-weight: 600; font-size: 14px; margin-top: 4px; }

/* ------------------------------------------------------------ Sélection des niveaux */
#campaign, #codes { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 12px 20px; width: 100%; max-width: 760px; margin: 0 auto; -webkit-overflow-scrolling: touch; }
.world-nav { display: flex; align-items: center; gap: 10px; margin: 6px 0 14px; }
.world-title { flex: 1; text-align: center; min-width: 0; }
#world-name { font-size: 20px; font-weight: 600; color: var(--accent); }
#world-theme { color: var(--muted); font-size: 14px; margin-top: 2px; }
#level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.level {
  position: relative; text-align: left;
  padding: 12px 12px 10px; border-radius: 14px; min-height: 92px;
  background: var(--panel); border: 1px solid var(--line);
  display: flex; flex-direction: column; justify-content: space-between;
}
.level .num { font-size: 13px; color: var(--muted); }
.level .name { font-weight: 600; font-size: 16px; margin: 2px 0 8px; line-height: 1.2; }
.level .stars img { width: 20px; height: 20px; margin-right: 2px; }
.level.locked { opacity: 0.42; }
.level.locked .name::after { content: " 🔒"; }
.level.last { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.level:not(.locked):active { transform: scale(0.97); }

#codes h2 { font-size: 17px; margin: 18px 0 8px; }
#code-input {
  width: 100%; resize: vertical; padding: 12px; border-radius: 12px;
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
  font: 14px ui-monospace, Consolas, monospace; user-select: text; -webkit-user-select: text;
}
.row { display: flex; align-items: center; gap: 12px; }
.row .btn { width: auto; min-width: 130px; }
.error { color: var(--danger); font-size: 14px; }
.custom { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.custom .level { flex: 1; min-height: 0; flex-direction: row; align-items: center; }
.custom .name { margin: 0; }
.empty { color: var(--muted); font-size: 14px; }

/* ------------------------------------------------------------ Partie */
#game[data-env="0"] { background: radial-gradient(120% 90% at 30% 15%, #5b4136 0%, #241813 55%, #0c0806 100%); }
#game[data-env="1"] { background: radial-gradient(120% 90% at 30% 15%, #3f7a46 0%, #17341d 55%, #07130a 100%); }
#game[data-env="2"] { background: radial-gradient(120% 90% at 30% 15%, #7a4a22 0%, #2e1a0b 55%, #0e0703 100%); }
#game[data-env="3"] { background: radial-gradient(120% 90% at 30% 15%, #5a6276 0%, #20242e 55%, #090a0e 100%); }
#game[data-env="4"] { background: radial-gradient(120% 90% at 30% 15%, #7fa6cc 0%, #27405c 55%, #0b1522 100%); }
#game[data-env="5"] {
  background:
    radial-gradient(1.5px 1.5px at 12% 22%, #fff 50%, transparent 51%), radial-gradient(1px 1px at 78% 12%, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 64% 68%, #cfd8ff 50%, transparent 51%), radial-gradient(1px 1px at 28% 82%, #fff 50%, transparent 51%),
    radial-gradient(1px 1px at 90% 48%, #fff 50%, transparent 51%), radial-gradient(1.5px 1.5px at 44% 38%, #e8d8ff 50%, transparent 51%),
    radial-gradient(1px 1px at 6% 60%, #fff 50%, transparent 51%), radial-gradient(1px 1px at 55% 8%, #fff 50%, transparent 51%),
    radial-gradient(120% 90% at 30% 15%, #4a2a78 0%, #170c2c 55%, #04020a 100%);
}
.hud-text { min-width: 0; flex: 1; }
#level-title { font-size: 19px; font-weight: 600; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-shadow: 0 1px 6px rgba(0,0,0,0.6); }
#moves-text { font-size: 14px; color: var(--text); opacity: 0.85; text-shadow: 0 1px 6px rgba(0,0,0,0.6); }
#stage { flex: 1; min-height: 0; position: relative; }
#board-wrap { position: absolute; inset: 0; overflow: hidden; }
#coach {
  position: absolute; left: 12px; right: 12px; top: 4px; z-index: 5; margin: 0 auto; max-width: 560px;
  display: flex; align-items: flex-start; gap: 6px;
  padding: 12px 8px 12px 16px; border-radius: 14px;
  background: rgba(16, 20, 30, 0.9); border: 1px solid rgba(74, 163, 255, 0.45); box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  animation: drop 0.35s ease-out;
}
#coach p { margin: 0; flex: 1; font-size: 15px; line-height: 1.4; }
.coach-close { flex: none; width: 32px; height: 32px; border-radius: 9px; font-size: 22px; line-height: 1; color: var(--muted); }
.pulse { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 4px rgba(255, 210, 74, 0.85), 0 0 22px rgba(255, 210, 74, 0.6); border-color: var(--gold); } }
@keyframes drop { from { opacity: 0; transform: translateY(-10px); } }
.btn.daily { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 14px 18px 12px;
  background: linear-gradient(135deg, #ffcf4a, #ff9a3c); color: #2a1600; border-color: transparent; }
.btn.daily .daily-title { font-size: 18px; }
.btn.daily .daily-sub { font-size: 13px; opacity: 0.8; font-weight: 500; }
.btn.daily:disabled { filter: saturate(0.6); }
.countdown { color: var(--muted) !important; font-size: 14px; margin-top: 12px !important; }
#board-wrap { touch-action: none; }
#board { position: absolute; left: 0; top: 0; touch-action: none; display: block; }
#hint-text {
  min-height: 22px; padding: 0 14px; text-align: center; flex: none;
  font-size: 14px; color: #b9ffd0; text-shadow: 0 1px 6px rgba(0,0,0,0.7);
}
.controls { flex: none; display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 6px 10px 10px; }
#hotbar { display: flex; gap: 6px; padding: 6px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line); }
.slot {
  position: relative; width: clamp(50px, 15.5vw, 68px); aspect-ratio: 1;
  border-radius: 11px; background: rgba(255, 255, 255, 0.05); border: 2px solid var(--line);
}
.slot img { position: absolute; left: 50%; top: 47%; width: 64%; height: 64%; transform: translate(-50%, -50%); object-fit: contain; pointer-events: none; }
.slot .key { position: absolute; left: 6px; top: 3px; font-size: 11px; color: var(--muted); }
.slot .count { position: absolute; right: 7px; bottom: 2px; font-size: 19px; font-weight: 600; }
.slot.empty { opacity: 0.32; }
.slot.armed { border-color: var(--gold); background: rgba(255, 210, 74, 0.2); box-shadow: 0 0 14px rgba(255, 210, 74, 0.45); }
.actions { display: flex; gap: 6px; width: 100%; max-width: 640px; }
.act {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px;
  padding: 11px 4px; border-radius: 12px; min-width: 0;
  background: var(--panel); border: 1px solid var(--line); font-weight: 600; font-size: 14px;
}
.act .glyph { font-size: 19px; line-height: 1; }
.act .lbl { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.act:disabled { opacity: 0.35; }
.act:not(:disabled):active { transform: scale(0.96); }
@media (max-width: 430px) { .act { flex-direction: column; gap: 2px; font-size: 11px; padding: 7px 2px; } }

/* Paysage sur téléphone : commandes à droite du plateau */
@media (orientation: landscape) and (max-height: 520px) {
  #game { display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto 1fr auto; }
  #game .bar { grid-column: 1; }
  #stage { grid-column: 1; grid-row: 2 / 4; }
  #hint-text { grid-column: 2; grid-row: 1; align-self: center; max-width: 250px; }
  .controls { grid-column: 2; grid-row: 2 / 4; justify-content: center; width: 250px; }
  #hotbar { flex-wrap: wrap; justify-content: center; }
  .slot { width: 62px; }
  .act .lbl { display: none; }
  .act { padding: 12px 4px; }
}

/* ------------------------------------------------------------ Fenêtres */
.overlay {
  position: absolute; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center; padding: 18px;
  background: rgba(4, 6, 10, 0.72); backdrop-filter: blur(3px);
  animation: fade 0.18s ease-out;
}
.overlay.light { background: rgba(4, 6, 10, 0.4); backdrop-filter: none; align-items: flex-end; padding-bottom: calc(24px + var(--safe-b)); }
#settings { position: fixed; z-index: 20; }
#copy-box { position: fixed; z-index: 25; }
#copy-text { width: 100%; resize: none; margin-top: 6px; padding: 10px; border-radius: 10px; background: var(--bg); color: var(--text);
  border: 1px solid var(--line); font: 14px/1.45 ui-monospace, Consolas, monospace; user-select: text; -webkit-user-select: text; }
.panel {
  width: min(380px, 100%); max-height: 100%; overflow-y: auto;
  padding: 22px 20px 20px; border-radius: 18px; text-align: center;
  background: var(--panel); border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
  animation: pop 0.2s ease-out;
}
.panel h2 { margin: 0 0 10px; font-size: 23px; font-weight: 600; color: var(--accent); }
.panel p { margin: 6px 0; color: var(--text); line-height: 1.45; white-space: pre-line; }
.record { color: var(--gold) !important; font-weight: 600; }
.stars-row { display: flex; justify-content: center; gap: 8px; margin: 8px 0; }
.stars-row img { width: 58px; height: 58px; }
.stars-row img.on { animation: star 0.4s ease-out backwards; }
.field { display: flex; flex-direction: column; gap: 8px; text-align: left; margin: 14px 0; color: var(--muted); font-size: 14px; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); height: 28px; }
.seg { display: flex; gap: 6px; }
.seg-btn { flex: 1; padding: 11px; border-radius: 10px; background: var(--panel-2); border: 1px solid var(--line); color: var(--text); font-weight: 600; }
.seg-btn.on { background: var(--accent); color: #06121f; }

#toast {
  position: fixed; left: 50%; bottom: calc(110px + var(--safe-b)); transform: translateX(-50%); z-index: 30;
  padding: 10px 18px; border-radius: 999px; background: #1f2a3d; border: 1px solid var(--line); font-weight: 600;
  animation: fade 0.2s ease-out;
}

@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: scale(0.92) translateY(8px); } }
@keyframes star { from { opacity: 0; transform: scale(0.2) rotate(-40deg); } }
@media (prefers-reduced-motion: reduce) { .overlay, .panel, .stars-row img.on { animation: none; } }
