:root {
  --bg: #12140f;
  --panel: rgba(22, 24, 18, 0.92);
  --line: #3a3b2c;
  --text: #e8e2cf;
  --muted: #a39d86;
  --gold: #c9a24d;
  --gold-dark: #8c6d2a;
  --danger: #e07a6a;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }

button {
  font: inherit; cursor: pointer; border: 1px solid var(--gold-dark);
  background: linear-gradient(#d4ae5a, #a8822f); color: #1c1608;
  padding: 8px 16px; border-radius: 6px; font-weight: 600;
}
button:hover { filter: brightness(1.08); }
button.ghost { background: transparent; color: var(--text); border-color: var(--line); font-weight: 500; }
button:disabled { opacity: .55; cursor: default; }

/* --- Carte --- */
#viewport { position: fixed; inset: 0; overflow: hidden; cursor: grab; touch-action: none; }
#viewport.dragging { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
#tiles, #grid, #castles, #hover, #selected { position: absolute; inset: 0; }
#tiles img { position: absolute; display: block; user-select: none; -webkit-user-drag: none; pointer-events: none; }
#grid {
  pointer-events: none; display: none;
  background-image:
    linear-gradient(to right, rgba(255,255,255,.22) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255,255,255,.22) 1px, transparent 1px);
}
#viewport.show-grid #grid { display: block; }
#hover, #selected { pointer-events: none; display: none; box-sizing: border-box; inset: auto; }
#hover { border: 2px solid rgba(255,255,255,.45); background: rgba(255,255,255,.06); }
#selected { border: 3px solid var(--gold); box-shadow: 0 0 18px rgba(201,162,77,.6) inset; }

.castle { position: absolute; pointer-events: none; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; }
.castle img { width: 100%; height: auto; filter: drop-shadow(0 6px 6px rgba(0,0,0,.45)); }
.castle .tag {
  margin-top: -6px; font-size: 18px; font-weight: 700; color: #fff; white-space: nowrap;
  background: rgba(0,0,0,.55); padding: 2px 8px; border-radius: 4px;
}
.castle.mine .tag { background: rgba(140,109,42,.9); }

/* --- Barre du haut --- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; height: 52px; display: flex; align-items: center; gap: 16px;
  padding: 0 16px; background: linear-gradient(rgba(10,11,8,.92), rgba(10,11,8,.75)); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; letter-spacing: .3em; color: var(--gold); }
#hint { flex: 1; color: var(--muted); font-size: 14px; }
#topbar .right { display: flex; align-items: center; gap: 12px; font-size: 14px; }
.toggle { display: flex; gap: 6px; align-items: center; color: var(--muted); cursor: pointer; }
#who { color: var(--gold); font-weight: 600; }
.ghost-link { color: var(--text); text-decoration: none; border: 1px solid var(--line); padding: 7px 14px; border-radius: 6px; }
.ghost-link:hover { border-color: var(--gold-dark); }

/* --- Panneau --- */
#panel {
  position: fixed; right: 16px; top: 68px; width: 300px; padding: 18px; border-radius: 10px;
  background: var(--panel); border: 1px solid var(--line); box-shadow: 0 10px 30px rgba(0,0,0,.5);
}
#panel h2 { margin: 0 0 6px; font-size: 18px; color: var(--gold); }
#panel p { margin: 0 0 12px; color: var(--muted); line-height: 1.4; }
#panel .close { position: absolute; top: 8px; right: 10px; background: none; border: none; color: var(--muted); font-size: 22px; padding: 0 4px; }

/* --- Connexion --- */
#authOverlay {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: radial-gradient(ellipse at center, rgba(0,0,0,.35), rgba(0,0,0,.8));
}
#authOverlay[hidden], #panel[hidden] { display: none; }
.card {
  width: min(360px, 92vw); padding: 28px; border-radius: 12px; background: var(--panel); border: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 14px; box-shadow: 0 20px 50px rgba(0,0,0,.6);
}
.card h1 { margin: 0; text-align: center; letter-spacing: .35em; color: var(--gold); }
.card .sub { margin: -6px 0 4px; text-align: center; color: var(--muted); font-size: 14px; }
.tabs { display: flex; gap: 6px; }
.tab { flex: 1; background: transparent; color: var(--muted); border-color: var(--line); font-weight: 500; }
.tab.active { color: var(--text); border-color: var(--gold-dark); background: rgba(201,162,77,.12); }
.card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.card input {
  font: inherit; padding: 10px 12px; border-radius: 6px; border: 1px solid var(--line);
  background: #0d0e0a; color: var(--text);
}
.card input:focus { outline: 2px solid var(--gold-dark); border-color: transparent; }
.error { color: var(--danger); font-size: 14px; min-height: 1em; margin: 0; }

/* ===== Jeu : barre de ressources, ville, fenêtres ===== */
#resbar { display: flex; gap: 14px; font-size: 14px; white-space: nowrap; }
#resbar .res { color: var(--text); font-variant-numeric: tabular-nums; }
#resbar .res.plein { color: #e6b450; }
button.alerte { border-color: #c0573f; color: #ffd2c4; }
#panelExtra { display: flex; flex-direction: column; gap: 8px; }
#panelExtra:empty { display: none; }
.small { font-size: 13px; } .muted { color: var(--muted); }

#ville { position: fixed; inset: 52px 0 0 0; display: grid; grid-template-columns: minmax(0, 1fr) 380px; background: var(--bg); z-index: 5; }
#ville[hidden] { display: none; }
#villeCarte { position: relative; overflow: hidden; display: grid; place-items: center; padding: 24px; }
#villeZone {
  position: relative; width: min(100%, 92vh); aspect-ratio: 1 / 1;
  border-radius: 38% 34% 36% 40% / 34% 40% 36% 38%;
  background:
    radial-gradient(circle at 30% 30%, rgba(120,140,60,.35), transparent 60%),
    radial-gradient(circle at 70% 70%, rgba(90,70,40,.35), transparent 55%),
    linear-gradient(135deg, #3d4a24, #2c3519 60%, #262d16);
  box-shadow: 0 0 0 10px #4a3720, 0 0 0 14px #2b1f12, 0 30px 60px rgba(0,0,0,.6);
}
#villeZone::after { content: "Carte de la ville provisoire"; position: absolute; left: 50%; top: 4%; transform: translateX(-50%); color: rgba(255,255,255,.35); font-size: 12px; letter-spacing: .1em; }
.emplacement {
  position: absolute; transform: translate(-50%, -50%); width: 118px; padding: 8px 6px; border-radius: 12px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: rgba(20, 22, 14, .78); color: var(--text); border: 1px solid rgba(201,162,77,.35); font-weight: 500;
}
.emplacement:hover { border-color: var(--gold); filter: none; background: rgba(30, 32, 20, .9); }
.emplacement.choisi { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(201,162,77,.45), 0 0 22px rgba(201,162,77,.35); }
.emplacement.vide { opacity: .6; border-style: dashed; }
.emplacement .ico { font-size: 30px; line-height: 1.1; }
.emplacement .nom { font-size: 13px; }
.emplacement .niv { font-size: 12px; color: var(--gold); }
.emplacement .chantier { position: absolute; top: -10px; right: -8px; font-size: 20px; animation: tape 1s infinite; }
@keyframes tape { 50% { transform: rotate(-25deg); } }
#villeFiche { border-left: 1px solid var(--line); background: var(--panel); padding: 20px; overflow: auto; }
#villeFiche h2 { margin: 0; color: var(--gold); }
#villeFiche .sous { color: var(--muted); margin: 2px 0 8px; }
#villeFiche p { color: var(--text); opacity: .85; line-height: 1.45; }
#villeRetour { position: absolute; left: 16px; top: 14px; background: rgba(10,11,8,.8); }
.effet { margin: 4px 0; }
.bloc { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.cout { display: flex; flex-wrap: wrap; gap: 10px; font-variant-numeric: tabular-nums; }
.cout .ok { color: var(--text); } .cout .manque { color: var(--danger); }
.raison { color: var(--danger); font-size: 13px; }
.encours { margin-top: 12px; padding: 10px; border-radius: 8px; background: rgba(201,162,77,.12); border: 1px solid var(--gold-dark); }
.troupes { display: flex; flex-wrap: wrap; gap: 10px; }
.unite { padding: 10px; border: 1px solid var(--line); border-radius: 8px; display: flex; flex-direction: column; gap: 6px; }
.unite.bloquee { opacity: .55; }
.uhaut { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.uform { display: flex; gap: 8px; align-items: center; }
.uform input, .rligne input { width: 80px; font: inherit; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--line); background: #0d0e0a; color: var(--text); }

#fenetre { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; background: rgba(0,0,0,.6); }
#fenetre[hidden] { display: none; }
.fen { position: relative; width: min(560px, 94vw); max-height: 86vh; overflow: auto; padding: 22px; border-radius: 12px; background: var(--panel); border: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; box-shadow: 0 20px 50px rgba(0,0,0,.6); }
.fen h2 { margin: 0 0 4px; color: var(--gold); } .fen h3 { margin: 8px 0 0; font-size: 15px; }
.fen .close { position: absolute; top: 8px; right: 10px; background: none; border: none; color: var(--muted); font-size: 22px; }
.rligne { display: grid; grid-template-columns: 1fr 90px auto; gap: 8px; align-items: center; }
button.small { padding: 4px 10px; font-size: 13px; }
.raidinfos { padding: 10px; border-radius: 8px; background: rgba(255,255,255,.04); display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.marche { padding: 10px; border: 1px solid var(--line); border-radius: 8px; display: flex; flex-direction: column; gap: 4px; }
.marche.alerte { border-color: #c0573f; background: rgba(192,87,63,.12); }
.rapport { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.rapport summary { cursor: pointer; }
.rapport.bon { border-left: 4px solid #6bbf6e; } .rapport.mauvais { border-left: 4px solid #c0573f; }
.rapport.nonlu summary b::after { content: " •"; color: var(--gold); }
.rapport > div { margin-top: 6px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none; transition: .2s; z-index: 30;
  background: #3a1d17; color: #ffd9d2; border: 1px solid #c0573f; padding: 10px 16px; border-radius: 8px; }
#toast.on { opacity: 1; transform: translateX(-50%); }
#toast.bon { background: #1f2e1a; color: #d9f2cf; border-color: #6bbf6e; }

@media (max-width: 900px) {
  #ville { grid-template-columns: 1fr; grid-template-rows: 55vh auto; overflow: auto; }
  #villeFiche { border-left: none; border-top: 1px solid var(--line); }
  #resbar { gap: 8px; font-size: 12px; }
  .emplacement { width: 84px; } .emplacement .ico { font-size: 22px; }
}
