* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --ink: #1c2230;
  --panel: rgba(247, 243, 233, 0.97);
  --brass: #e0b64a;
  --brass-dark: #a5812a;
  --steel: #3aa0ff;
  --green: #2ed573;
  --red: #ff4757;
  --red-dark: #c8323f;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: #0b0f1a;
  font-family: 'Nunito', system-ui, sans-serif;
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#stage { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
#frame {
  position: relative;
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
}
#cv { width: 100%; height: 100%; display: block; touch-action: none; background: #10131f; }

/* ---------- rotate-to-landscape ---------- */
#rotate { display: none; }
@media (orientation: portrait) {
  body.touch #rotate {
    position: fixed; inset: 0; z-index: 100;
    background: #0a0e1a; color: #fff;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 18px; text-align: center; padding: 30px;
  }
  body.touch #stage { display: none; }
}
#rotate .rotico { font-size: 64px; animation: rot 2s ease-in-out infinite; }
#rotate .rottxt { font-weight: 800; font-size: 19px; color: #aab6cf; line-height: 1.5; }
#rotate .rottxt b { color: var(--brass); }
@keyframes rot { 0%,55% { transform: rotate(0deg); } 75%,100% { transform: rotate(-90deg); } }

.hidden { display: none !important; }

/* ---------- overlays / panels ---------- */
#overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
#overlay.gone { display: none; }
.panel {
  pointer-events: auto;
  background: var(--panel);
  border-radius: 24px;
  padding: 34px 42px;
  width: min(440px, calc(100vw - 32px));
  max-height: 94vh; overflow-y: auto;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(224,182,74,0.3);
  display: flex; flex-direction: column; gap: 14px; align-items: center;
}
.title {
  font-family: 'Cinzel', serif; font-weight: 900;
  font-size: 46px; line-height: 0.95; color: var(--ink); letter-spacing: 1px;
}
.title span { color: var(--brass-dark); }
.title::after { content: " ⚔️"; font-size: 34px; }
.tagline { font-weight: 700; color: #6e7688; font-size: 13.5px; line-height: 1.5; }

input {
  font-family: inherit; font-weight: 800; font-size: 18px; text-align: center;
  padding: 13px 16px; border-radius: 14px; border: 3px solid #ddd7c6;
  background: #fbf8f0; color: var(--ink); width: 100%; outline: none;
}
input:focus { border-color: var(--brass); }
input::placeholder { color: #b3ac98; font-weight: 700; }
#codeInput { text-transform: uppercase; letter-spacing: 6px; width: 130px; }

.btn {
  font-family: inherit; font-weight: 900; font-size: 17px; letter-spacing: 0.5px;
  padding: 13px 22px; border: none; border-radius: 14px;
  background: var(--brass); color: #3a2c05; cursor: pointer;
  box-shadow: 0 4px 0 var(--brass-dark), 0 10px 20px rgba(165,129,42,0.3);
  transition: transform 0.08s, box-shadow 0.08s, filter 0.15s;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--brass-dark); }
.btn:disabled { filter: grayscale(0.7) brightness(0.97); cursor: default; box-shadow: 0 4px 0 #9aa1b1; }
.btn.big { width: 100%; font-size: 20px; padding: 16px 22px; }
.btn.ghost { background: #ece7da; color: #6a6250; box-shadow: 0 3px 0 #d3ccba; font-size: 14px; padding: 10px 18px; }
.btn.ghost:active { box-shadow: 0 1px 0 #d3ccba; }

.divider { width: 100%; display: flex; align-items: center; gap: 10px; color: #b3ac98; font-weight: 700; font-size: 13px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 2px; background: #e5dfce; border-radius: 2px; }
.joinrow { display: flex; gap: 10px; width: 100%; justify-content: center; }
.error { color: var(--red-dark); font-weight: 800; font-size: 14px; }
.hint { color: #98917f; font-weight: 700; font-size: 13px; }

.lobbylabel { font-weight: 900; font-size: 13px; letter-spacing: 3px; color: #98917f; }
#codeBig { font-family: 'Cinzel', serif; font-weight: 900; font-size: 60px; letter-spacing: 12px; padding-left: 12px; line-height: 1; color: var(--brass-dark); }
#playerList { width: 100%; display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.prow { display: flex; align-items: center; gap: 12px; background: #f0ece0; border-radius: 12px; padding: 10px 16px; font-weight: 800; font-size: 16px; }
.pdot { width: 16px; height: 16px; border-radius: 50%; flex-shrink: 0; box-shadow: 0 0 0 3px rgba(0,0,0,0.08) inset; }
.pcrown { margin-left: auto; }

/* ---------- game over ---------- */
#overEmoji { font-size: 64px; }
#gameover h2 { font-family: 'Cinzel', serif; font-weight: 900; font-size: 30px; }
#overStats { width: 100%; display: flex; flex-direction: column; gap: 6px; }
.srow { display: flex; gap: 10px; align-items: center; background: #f0ece0; border-radius: 10px; padding: 8px 14px; font-weight: 800; font-size: 14px; }
.srow .skills { margin-left: auto; color: #6e7688; font-size: 12.5px; }

/* ============================ HUD ============================ */
#hud { position: absolute; inset: 0; pointer-events: none; font-weight: 800; }

/* top: base HP bars + clock */
#topbar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: flex-start; gap: 12px;
  padding: 12px 16px;
}
.baseblock { flex: 1; min-width: 0; }
.baseblock.right { text-align: right; }
.baselabel {
  font-size: 12px; letter-spacing: 1px; color: #cdd6e6;
  text-shadow: 0 1px 3px rgba(0,0,0,0.7); margin-bottom: 3px;
}
.baselabel span:first-child { color: #fff; font-weight: 900; }
.basebar {
  height: 18px; border-radius: 999px; overflow: hidden;
  background: rgba(0,0,0,0.55); border: 2px solid rgba(255,255,255,0.18);
}
.basebar .fill { height: 100%; width: 100%; background: linear-gradient(90deg, #17a34a, #2ed573); transition: width 0.25s; }
.baseblock.right .basebar { transform: scaleX(-1); }
.basebar .fill.enemy { background: linear-gradient(90deg, #c8323f, #ff6b81); }
#mates { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.matechip {
  font-size: 10.5px; font-weight: 900; color: #0b0f1a;
  border-radius: 7px; padding: 2px 8px; box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}
#aiAge {
  margin-top: 6px; font-size: 11px; font-weight: 900; letter-spacing: 0.5px;
  color: #ffb3ba; text-shadow: 0 1px 3px rgba(0,0,0,0.7);
}

#clockwrap { text-align: center; flex-shrink: 0; padding: 0 6px; }
#clock {
  font-family: 'Cinzel', serif; font-weight: 900; font-size: 30px; color: #fff;
  text-shadow: 0 2px 0 rgba(0,0,0,0.5), 0 6px 16px rgba(0,0,0,0.5); line-height: 1;
}
#clockwrap .vs { font-size: 10px; letter-spacing: 3px; color: var(--brass); font-weight: 900; }

/* banner + toasts */
#banner {
  position: absolute; top: 26%; left: 50%; transform: translateX(-50%);
  font-family: 'Cinzel', serif; font-weight: 900; font-size: 42px; color: #fff;
  text-shadow: 0 4px 0 rgba(0,0,0,0.4), 0 12px 30px rgba(0,0,0,0.5);
  animation: bannerIn 0.35s cubic-bezier(0.2, 1.6, 0.4, 1); white-space: nowrap; text-align: center;
}
@keyframes bannerIn { from { transform: translateX(-50%) scale(0.4); opacity: 0; } }
#toasts { position: absolute; top: 88px; right: 16px; display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
.toast {
  background: rgba(11, 15, 26, 0.86); color: #fff; border: 1px solid rgba(224,182,74,0.3);
  border-radius: 10px; padding: 7px 13px; font-weight: 800; font-size: 12.5px;
  animation: toastIn 0.25s ease-out;
}
@keyframes toastIn { from { transform: translateX(30px); opacity: 0; } }

/* bottom control bar */
#controls {
  position: absolute; bottom: 0; left: 0; right: 0;
  display: flex; align-items: stretch; justify-content: space-between; gap: 10px;
  padding: 8px 12px 10px;
  background: linear-gradient(0deg, rgba(9,12,20,0.92), rgba(9,12,20,0.55) 70%, transparent);
  pointer-events: auto;
}
.cluster { display: flex; align-items: center; gap: 8px; }
.cluster.unitcol { flex: 1; flex-direction: column; justify-content: flex-end; gap: 5px; }
#unitbar { display: flex; justify-content: center; gap: 10px; }
#ageTabs { display: flex; gap: 5px; justify-content: center; }
.agetab {
  font-family: inherit; font-weight: 900; font-size: 10.5px; letter-spacing: 0.3px;
  padding: 3px 10px; border-radius: 8px; cursor: pointer;
  border: 1.5px solid rgba(255,255,255,0.14); background: rgba(28,34,48,0.9); color: #9fb0cf;
}
.agetab.sel { background: var(--brass); color: #3a2c05; border-color: var(--brass); }
.agetab.locked { opacity: 0.3; cursor: default; }

/* turret slot panel */
#turretPanel { display: flex; flex-direction: column; gap: 3px; align-items: center; }
#turretPanel .tlabel { font-weight: 900; font-size: 10px; letter-spacing: 1px; color: #9fb0cf; }
#turretPanel .tlabel small { color: var(--brass); }
#turretSlots { display: flex; gap: 4px; }
.tslot {
  position: relative; width: 34px; height: 40px; border-radius: 9px; cursor: pointer;
  border: 2px solid rgba(255,255,255,0.14); background: rgba(28,34,48,0.92);
  display: flex; align-items: center; justify-content: center; font-size: 18px;
  color: #66707f; transition: filter 0.1s, transform 0.07s;
}
.tslot:hover { filter: brightness(1.15); }
.tslot:active { transform: translateY(1px); }
.tslot.filled { color: #eef2fb; }
.tslot.upg { border-color: #5ee7ff; box-shadow: 0 0 0 2px rgba(94,231,255,0.25); }
.tslot .sell {
  position: absolute; top: -6px; right: -6px; width: 15px; height: 15px; border-radius: 50%;
  background: var(--red); color: #fff; font-size: 10px; font-weight: 900; line-height: 15px; text-align: center;
  border: none; cursor: pointer; padding: 0;
}
.tslot .tier { position: absolute; bottom: -2px; right: 1px; font-size: 8px; font-weight: 900; color: var(--brass); }

/* lobby difficulty picker */
#diffpicker { display: flex; gap: 8px; width: 100%; justify-content: center; }
.diffbtn {
  flex: 1; font-family: inherit; font-weight: 900; font-size: 14px;
  padding: 10px 8px; border-radius: 12px; cursor: pointer;
  border: 2px solid #ddd7c6; background: #fbf8f0; color: #6a6250;
  transition: border-color 0.1s, transform 0.07s;
}
.diffbtn small { display: block; font-size: 10px; font-weight: 700; color: #98917f; margin-top: 1px; }
.diffbtn.sel { border-color: var(--brass); background: #fff6dd; color: var(--ink); }
.diffbtn:disabled { cursor: default; opacity: 0.75; }

#goldPill {
  background: rgba(0,0,0,0.5); border: 2px solid rgba(224,182,74,0.5); border-radius: 12px;
  padding: 8px 12px; color: var(--brass); font-weight: 900; font-size: 18px; white-space: nowrap;
  min-width: 92px; text-align: center;
}
.agebox { display: flex; flex-direction: column; gap: 4px; min-width: 130px; }
#ageName { font-family: 'Cinzel', serif; font-weight: 900; font-size: 14px; color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,0.7); }
#xpwrap {
  position: relative; height: 16px; border-radius: 999px; overflow: hidden;
  background: rgba(0,0,0,0.5); border: 2px solid rgba(94,231,255,0.35);
}
#xpFill { height: 100%; width: 0%; background: linear-gradient(90deg, #2f7bd6, #5ee7ff); transition: width 0.2s; }
#xpText { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-weight: 900; font-size: 10px; color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,0.7); }

/* action + unit buttons */
.actbtn {
  position: relative; font-family: inherit; font-weight: 900; font-size: 13px; letter-spacing: 0.3px;
  padding: 8px 12px; border-radius: 12px; border: 2px solid rgba(255,255,255,0.14);
  background: rgba(30,36,50,0.92); color: #eef2fb; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 1px; line-height: 1.05;
  transition: transform 0.07s, border-color 0.1s, filter 0.12s;
}
.actbtn .ai { font-size: 18px; }
.actbtn small { font-weight: 800; font-size: 10px; color: #9fb0cf; }
.actbtn:hover { filter: brightness(1.12); }
.actbtn:active { transform: translateY(2px); }
.actbtn:disabled { filter: grayscale(0.6) brightness(0.7); cursor: default; }
.actbtn.evolve { border-color: rgba(94,231,255,0.4); }
.actbtn.evolve.ready { background: linear-gradient(180deg, #2f7bd6, #1e5bb0); border-color: #5ee7ff; color: #fff; animation: readyPulse 1s infinite alternate; }
.actbtn.evolve.ready small { color: #cdeeff; }
@keyframes readyPulse { to { box-shadow: 0 0 0 3px rgba(94,231,255,0.35); } }
.actbtn.special { border-color: rgba(224,182,74,0.4); overflow: hidden; }
.actbtn.special.ready { border-color: var(--brass); }
#spCdRing { position: absolute; inset: 0; background: rgba(10,14,26,0.72); pointer-events: none; }
.actbtn.ghost { background: rgba(30,36,50,0.7); padding: 8px 11px; }

.ubtn {
  position: relative; font-family: inherit; cursor: pointer;
  width: 108px; padding: 6px 8px 7px; border-radius: 13px;
  border: 2px solid rgba(255,255,255,0.14); background: rgba(28,34,48,0.94);
  color: #eef2fb; display: flex; flex-direction: column; align-items: center; gap: 1px; line-height: 1.1;
  transition: transform 0.07s, border-color 0.1s, filter 0.12s;
}
.ubtn:hover { filter: brightness(1.12); transform: translateY(-2px); }
.ubtn:active { transform: translateY(1px); }
.ubtn.broke { filter: grayscale(0.7) brightness(0.62); }
.ubtn .uico { font-size: 24px; }
.ubtn .uname { font-weight: 900; font-size: 12px; }
.ubtn .ucost { font-weight: 900; font-size: 12px; color: var(--brass); }
.ubtn .ukind {
  position: absolute; top: 5px; left: 6px; font-size: 9px; font-weight: 900;
  color: #0b0f1a; background: #9fb0cf; border-radius: 5px; padding: 0 4px;
}
.ubtn .ukind.rng { background: #5ee7ff; }
.ubtn .ukind.hvy { background: #ffb14a; }
.ubtn .uslot { position: absolute; top: 4px; right: 6px; font-size: 9px; font-weight: 900; color: #66707f; }
.ubtn .uq {
  position: absolute; bottom: -6px; right: -6px; min-width: 18px; height: 18px;
  background: var(--green); color: #05310f; font-weight: 900; font-size: 11px;
  border-radius: 9px; display: flex; align-items: center; justify-content: center; padding: 0 4px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.5);
}

#hint {
  position: absolute; bottom: 92px; left: 50%; transform: translateX(-50%);
  color: rgba(255,255,255,0.55); font-weight: 700; font-size: 11.5px; white-space: nowrap;
  text-shadow: 0 1px 3px rgba(0,0,0,0.6); pointer-events: none;
}
#hint b { color: var(--brass); }
body.touch #hint { display: none; }

/* ---------- escape menu ---------- */
#gameMenu {
  position: absolute; inset: 0; z-index: 50;
  background: rgba(9, 12, 20, 0.82);
  display: flex; align-items: center; justify-content: center; pointer-events: auto;
}
.menuCard {
  background: var(--panel); border-radius: 22px;
  padding: 26px 32px; width: min(560px, calc(100vw - 40px));
  max-height: 88vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 24px 60px rgba(0,0,0,0.5);
}
.menuTitle { font-family: 'Cinzel', serif; font-weight: 900; font-size: 26px; color: var(--brass-dark); text-align: center; }
.menuHelp { display: flex; flex-direction: column; gap: 8px; font-weight: 700; font-size: 13.5px; color: #55607a; line-height: 1.45; }
.menuHelp b { color: var(--ink); }
.menuHubBtn { text-align: center; text-decoration: none; display: block; }
.legendLabel { font-weight: 900; font-size: 11px; letter-spacing: 2.5px; color: #98917f; margin-top: 6px; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 7px 14px; }
.lrow { display: flex; align-items: center; gap: 11px; background: #f0ece0; border-radius: 11px; padding: 7px 11px; min-height: 44px; }
.lrow .lico { width: 30px; flex-shrink: 0; font-size: 20px; text-align: center; }
.lrow .ltxt { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.lrow .ltxt b { font-size: 12px; color: var(--ink); }
.lrow .ltxt small { font-size: 10.5px; color: #6e7688; font-weight: 700; }
@media (max-width: 700px) { .legend { grid-template-columns: 1fr; } }

/* keep buttons legible on small landscape phones */
@media (max-height: 460px) {
  .ubtn { width: 92px; }
  .ubtn .uico { font-size: 20px; }
  #hint { display: none; }
}
