:root {
  --bg: #12151c;
  --panel: #1b2029;
  --panel2: #232a35;
  --line: #313a48;
  --text: #e6e9ef;
  --muted: #8c96a8;
  --accent: #ffb703;
  --good: #52b788;
  --bad: #ef476f;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13px/1.35 system-ui, "Segoe UI", Roboto, sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
code { background: #0003; padding: 1px 4px; border-radius: 3px; }
kbd { font: 10px monospace; background: #0005; border: 1px solid var(--line); border-radius: 3px; padding: 0 3px; color: var(--muted); }
.ic-gold { color: #ffd60a; }
.ic-ess { color: #9bf6ff; }
.ic-life { color: #ff4d6d; }

.btn { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 7px 12px; cursor: pointer; font: inherit; }
.btn:hover { border-color: #56657c; background: #2a3341; }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn.tiny { padding: 2px 7px; font-size: 11px; }
.btn.on { border-color: var(--accent); color: var(--accent); }
.btn.dis, .btn:disabled { opacity: .45; cursor: default; }
.btn.ghost { background: transparent; }
.btn.up { border-color: #3f8f5f; }
.btn.sell { border-color: #8f3f4f; }
input { background: #0d1015; color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 7px 10px; font: inherit; }

/* ---------- меню ---------- */
#menu { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.menu-box { width: min(720px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 24px; display: flex; flex-direction: column; gap: 12px; }
h1 { margin: 0 0 4px; font-size: 34px; letter-spacing: 1px; }
h1 span { display: block; font-size: 13px; font-weight: 400; color: var(--muted); letter-spacing: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row label { color: var(--muted); }
.sec-title { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; margin-top: 4px; }
.sec-title small { text-transform: none; letter-spacing: 0; opacity: .8; margin-left: 6px; }
.maps { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.map-card { background: var(--panel2); border: 2px solid var(--line); border-radius: 8px; padding: 8px; cursor: pointer; color: var(--text); text-align: left; font: inherit; }
.map-card.on { border-color: var(--accent); }
.map-card small { color: var(--muted); margin-left: 6px; }
.map-card canvas { display: block; width: 100%; margin-top: 6px; border-radius: 4px; image-rendering: pixelated; }
.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.mode-btn { background: linear-gradient(#2b3443, #222934); border: 1px solid var(--line); border-radius: 8px; padding: 14px; color: var(--text); cursor: pointer; text-align: left; font: inherit; }
.mode-btn:hover { border-color: var(--accent); }
.mode-btn b { display: block; font-size: 15px; }
.mode-btn small { color: var(--muted); }
#roomCode { width: 80px; text-transform: uppercase; text-align: center; letter-spacing: 3px; }
#onlineStatus { min-height: 18px; }
.code { color: var(--accent); font-size: 18px; letter-spacing: 3px; }
@media (max-width: 600px) { .maps, .modes { grid-template-columns: 1fr; } }

/* ---------- игра ---------- */
#game { display: flex; flex-direction: column; height: 100%; }
#topbar { display: flex; align-items: center; gap: 8px; padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--line); }
#topbar .spacer { flex: 1; }
#modeTitle { color: var(--muted); }
#speedBox { display: flex; gap: 2px; }
#panels { flex: 1; display: flex; gap: 10px; padding: 8px; min-height: 0; overflow: auto; }
#panels.one { justify-content: center; }
#panels.one > .panel { flex: 1 1 auto; max-width: 1500px; }
#panels.two > .panel { flex: 1 1 0; min-width: 0; }
#panels.compact-right > .panel:first-child { flex: 3 1 0; }
#panels.compact-right > .panel.compact { flex: 2 1 0; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.hud { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.hud .pname { font-weight: 700; font-size: 14px; }
.hud .stat b { font-size: 14px; }
.hud .next-btn { margin-left: auto; }
.preview { min-height: 20px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; }
.wchip { background: var(--panel2); border-radius: 4px; padding: 1px 6px; }
.wchip i, .merc-btn i { display: inline-block; width: 10px; height: 10px; outline: 2px solid; margin-right: 5px; vertical-align: -1px; }

.body { display: flex; gap: 10px; min-height: 0; }
.layout-stack .body { flex-direction: column; }
.field-col { flex: 1; min-width: 0; display: flex; justify-content: center; }
.canvas-wrap { position: relative; width: 100%; }
.canvas-wrap canvas { display: block; width: 100%; aspect-ratio: 640 / 416; border-radius: 6px; cursor: crosshair; background: #000; }
.layout-side .canvas-wrap { max-width: calc((100vh - 130px) * 1.538); margin: 0 auto; }
.layout-stack .canvas-wrap { max-width: calc((100vh - 480px) * 1.538); margin: 0 auto; min-width: 360px; }
.compact .canvas-wrap { max-width: calc((100vh - 150px) * 1.538); margin: 0 auto; }
.compact .canvas-wrap canvas { cursor: default; }
.compact .side { display: none; }
.alerts { position: absolute; left: 8px; top: 6px; pointer-events: none; font-weight: 700; text-shadow: 0 1px 2px #000, 0 0 4px #000; font-size: 13px; }
.veil { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; flex-direction: column; background: #000a; border-radius: 6px; }
.veil.on { display: flex; }
.veil .big { font-size: 32px; font-weight: 800; }

.side { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.layout-side .side { width: 380px; flex: none; }
.layout-stack .side { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 8px; }
.layout-stack .info { min-height: 150px; max-height: 175px; }
.layout-stack .mercs-wrap { grid-column: 1 / -1; }

.shop { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; align-content: start; }
.layout-stack .shop { grid-template-columns: repeat(5, 1fr); }
.shop-btn { position: relative; display: grid; grid-template-columns: 26px 1fr; grid-template-rows: auto auto; column-gap: 6px; align-items: center; background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 4px 5px; cursor: pointer; color: var(--text); text-align: left; font: inherit; font-size: 12px; min-width: 0; }
.shop-btn:hover { border-color: #56657c; }
.shop-btn.on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.shop-btn.dis { opacity: .5; }
.shop-btn i { grid-row: 1 / 3; width: 26px; height: 26px; display: flex; align-items: center; justify-content: center; font-style: normal; font-weight: 700; font-size: 10px; border-radius: 3px; color: #fff; text-shadow: 0 1px 1px #000; }
.shop-btn .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.shop-btn .cost { font-size: 11px; color: var(--muted); }
.shop-btn kbd { position: absolute; right: 3px; top: 3px; }

.info { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 8px; min-height: 190px; max-height: 260px; overflow: auto; font-size: 12px; }
.info .ttl { font-weight: 700; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.info .ttl i { width: 20px; height: 20px; display: inline-flex; align-items: center; justify-content: center; font-style: normal; font-size: 9px; border-radius: 3px; color: #fff; }
.info .lvl { margin-left: auto; color: var(--accent); font-size: 12px; }
.info .sub { margin: 4px 0; }
.info .desc { margin: 6px 0; }
.info .hint { color: var(--muted); margin-top: 6px; }
.badge { border: 1px solid var(--line); border-radius: 4px; padding: 0 5px; font-size: 11px; }
.eff { display: flex; gap: 3px; flex-wrap: wrap; margin: 4px 0; }
.chip { font-size: 11px; padding: 0 4px; border-radius: 3px; background: #0003; }
.chip i { display: inline-block; width: 8px; height: 8px; margin-right: 3px; }
.chip.good { color: var(--good); }
.chip.bad { color: var(--bad); }
table.stats { border-collapse: collapse; margin: 4px 0; }
table.stats td { padding: 1px 8px 1px 0; }
table.stats td:first-child { color: var(--muted); }
table.stats td.nx { color: var(--good); }
.modes .btn { margin: 4px 3px 0 0; }
.info .modes { display: flex; flex-wrap: wrap; }
.acts { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }

.mercs { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; }
.merc-btn { position: relative; overflow: hidden; display: grid; grid-template-columns: 1fr auto; gap: 0 4px; background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px 6px; cursor: pointer; color: var(--text); text-align: left; font: inherit; font-size: 11px; min-width: 0; }
.merc-btn.elite { border-color: #2f6f7a; background: #1d2c33; }
.merc-btn:hover { border-color: var(--accent); }
.merc-btn.dis { opacity: .45; }
.merc-btn .nm { grid-column: 1 / 3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.merc-btn .inc { color: var(--good); text-align: right; }
.merc-btn .stk { color: var(--muted); }
.merc-btn .cdbar { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--accent); }

/* ---------- оверлей ---------- */
#overlay { position: fixed; inset: 0; background: #000b; display: flex; align-items: center; justify-content: center; z-index: 10; padding: 16px; }
.modal { position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px 24px; max-width: 900px; max-height: 90vh; overflow: auto; }
.modal .close { position: absolute; right: 10px; top: 8px; background: none; border: 0; color: var(--muted); font-size: 18px; cursor: pointer; }
.modal h2 { margin-top: 0; }
.modal h3 { margin: 16px 0 6px; }
table.matrix { border-collapse: collapse; }
table.matrix th, table.matrix td { padding: 4px 10px; border: 1px solid var(--line); text-align: center; }
table.matrix td.good { color: var(--good); font-weight: 700; }
table.matrix td.bad { color: var(--bad); }
table.list td { padding: 3px 8px 3px 0; vertical-align: top; }
.sq { display: inline-flex; width: 22px; height: 22px; align-items: center; justify-content: center; font-style: normal; font-size: 9px; color: #fff; border-radius: 3px; }
.endbox { text-align: center; min-width: 320px; }
.endbox h2 { font-size: 32px; }
.endbox.win h2 { color: var(--good); }
.endbox.lose h2 { color: var(--bad); }
.endbox .acts { justify-content: center; }

/* ---------- картинки-иконки ---------- */
.pic { display: inline-flex; align-items: center; justify-content: center; background: rgba(0,0,0,.25); border-radius: 4px; overflow: hidden; }
.pic img { width: 100%; height: 100%; object-fit: contain; display: block; }
.shop-btn i.pic { background: rgba(0,0,0,.3); }
.wchip i.pic { width: 20px; height: 20px; outline: none; border: 2px solid; vertical-align: middle; margin-right: 4px; }
.merc-btn i.pic { width: 26px; height: 26px; outline: none; border: 2px solid; }
.info .ttl i.pic { width: 30px; height: 30px; }
#btnOpp { display: none; }
html { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.canvas-wrap canvas { touch-action: none; user-select: none; -webkit-user-select: none; }

/* ---------- телефон / узкий экран ---------- */
@media (max-width: 900px) {
  body { font-size: 12px; }
  #topbar { padding: 4px 6px; gap: 4px; padding-left: max(6px, env(safe-area-inset-left)); padding-right: max(6px, env(safe-area-inset-right)); }
  #modeTitle { display: none; }
  .btn.small { padding: 6px 9px; }
  body.has-opp #btnOpp { display: inline-block; }
  #panels { padding: 4px; gap: 4px; flex-direction: column; }
  #panels.two > .panel, #panels.compact-right > .panel:first-child, #panels.compact-right > .panel.compact { flex: none; }
  /* поле соперника — по кнопке «Соперник» */
  #panels.compact-right > .panel.compact { display: none; }
  body.show-opp #panels.compact-right > .panel.compact { display: flex; }
  body.show-opp #panels.compact-right > .panel:not(.compact) { display: none; }
  .panel { padding: 4px; gap: 4px; border-radius: 6px; }
  .hud { gap: 9px; }
  .hud .pname { display: none; }
  .hud .next-btn { padding: 6px 10px; }
  .preview { flex-wrap: nowrap; overflow-x: auto; white-space: nowrap; font-size: 11px; }
  .body, .layout-side .body, .layout-stack .body { flex-direction: column; gap: 4px; }
  .layout-side .canvas-wrap, .layout-stack .canvas-wrap, .compact .canvas-wrap { max-width: none; min-width: 0; }
  .layout-side .side { width: auto; }
  .side, .layout-stack .side { display: flex; flex-direction: column; gap: 4px; }
  .shop, .layout-stack .shop { display: flex; overflow-x: auto; gap: 4px; padding-bottom: 3px; scroll-snap-type: x proximity; }
  .shop-btn { flex: 0 0 64px; display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 4px 2px; scroll-snap-align: start; }
  .shop-btn i { width: 38px; height: 38px; }
  .shop-btn .nm { font-size: 10px; max-width: 60px; text-align: center; }
  .shop-btn .cost { font-size: 10px; }
  kbd { display: none; }
  .info, .layout-stack .info { min-height: 0; max-height: 150px; padding: 6px; }
  .info .acts .btn, .info .modes .btn { padding: 8px 10px; }
  .mercs { display: flex; overflow-x: auto; padding-bottom: 3px; }
  .merc-btn { flex: 0 0 92px; min-height: 56px; }
  .sec-title small { display: none; }
}
/* телефон боком: поле слева во всю высоту, панель управления справа */
@media (max-width: 900px) and (orientation: landscape) {
  #panels { height: calc(100vh - 38px); overflow: hidden; }
  .panel { height: 100%; min-height: 0; }
  .preview { display: none; }
  .body, .layout-side .body, .layout-stack .body { flex-direction: row; flex: 1; min-height: 0; }
  .field-col { flex: none; }
  .layout-side .canvas-wrap, .layout-stack .canvas-wrap, .compact .canvas-wrap { width: min(62vw, calc((100vh - 76px) * 1.538)); }
  .side, .layout-stack .side { flex: 1; min-width: 0; overflow-y: auto; }
  .shop, .layout-stack .shop { display: grid; grid-template-columns: repeat(auto-fill, minmax(56px, 1fr)); overflow: visible; }
  .shop-btn { flex: none; }
  .shop-btn i { width: 32px; height: 32px; }
  .shop-btn .nm { display: none; }
}
@media (max-width: 900px) {
  #topbar .btn { white-space: nowrap; min-width: 36px; }
}
@media (max-width: 600px) {
  #topbar .lbl { display: none; }
}
@media (max-width: 900px) and (orientation: landscape) {
  .side > * { flex-shrink: 0; }
  .info, .layout-stack .info { max-height: none; }
}
/* ---------- вертикальная карта (телефон в портрете) ---------- */
@media (max-width: 900px) {
  .info.empty { display: none; }
}
@media (max-width: 900px) and (orientation: portrait) {
  #panels { padding: 2px; }
  .panel { padding: 3px; }
  .hud { gap: 7px; font-size: 13px; }
  .preview { min-height: 0; }
}
/* если телефон повернули боком посреди игры с вертикальной картой */
@media (max-width: 900px) and (orientation: landscape) {
  .panel.vertical .canvas-wrap { width: calc((100vh - 76px) * 0.65); }
}
@media (max-width: 900px) and (orientation: landscape) {
  .layout-side .side, .layout-stack .side { flex: 1; width: auto; }
  .layout-side .canvas-wrap, .layout-stack .canvas-wrap, .compact .canvas-wrap { width: min(60vw, calc((100vh - 100px) * 1.538)); }
  .hud .next-btn { padding: 4px 10px; }
}

/* перетаскивание башни из магазина */
.shop-btn { touch-action: pan-x; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.drag-ghost { position: fixed; left: 0; top: 0; z-index: 1000; pointer-events: none; opacity: .85; filter: drop-shadow(0 4px 6px rgba(0,0,0,.6)); }
.drag-ghost i { display: flex; width: 44px; height: 44px; border-radius: 6px; }
.drag-ghost i img { width: 100%; height: 100%; object-fit: contain; }
@media (max-width: 900px) and (orientation: landscape) { .shop-btn { touch-action: none; } }

/* телефон в портрете: поле подстраивается под высоту экрана, золото и жизни всегда видны */
@media (max-width: 900px) and (orientation: portrait) {
  .hud { position: sticky; top: -2px; z-index: 5; background: var(--panel); padding: 3px 0; }
  .panel.vertical .canvas-wrap { width: min(100%, calc((100vh - 215px) * 0.65)); margin: 0 auto; }
  @supports (height: 100dvh) {
    .panel.vertical .canvas-wrap { width: min(100%, calc((100dvh - 215px) * 0.65)); }
  }
}

/* кнопки зума поверх поля */
.zoombar { position: absolute; right: 6px; bottom: 6px; display: flex; flex-direction: column; gap: 4px; opacity: .85; }
.zoombar .btn { width: 34px; height: 34px; padding: 0; font-size: 20px; line-height: 1; background: rgba(20,26,36,.75); }
.compact .zoombar { display: none; }
