:root {
  --bg: #0e1024;
  --panel: rgba(30, 33, 70, 0.72);
  --text: #f4f5ff;
  --muted: #a3a6d2;
  --accent: #ff8a2f;
  --accent2: #ff4d2f;
  --glass: rgba(255, 255, 255, 0.08);
  --glass-line: rgba(255, 255, 255, 0.14);

  /* размеры «локального» (после автоповорота) экрана выставляет js/main.js */
  --lw: 100vw;
  --lh: 100vh;

  /* физические отступы: вырезы экрана и интерфейс Telegram (в полноэкранном режиме) */
  --p-top: calc(env(safe-area-inset-top, 0px) + var(--tg-safe-area-inset-top, 0px));
  --p-right: calc(env(safe-area-inset-right, 0px) + var(--tg-safe-area-inset-right, 0px));
  --p-bottom: calc(env(safe-area-inset-bottom, 0px) + var(--tg-safe-area-inset-bottom, 0px));
  --p-left: calc(env(safe-area-inset-left, 0px) + var(--tg-safe-area-inset-left, 0px));
  --p-ctl: var(--tg-content-safe-area-inset-top, 0px);   /* кнопки «Закрыть» / «⋮» у Telegram */

  /* отступы раскладки (в обычной альбомной ориентации = физическим) */
  --in-top: var(--p-top);
  --in-right: var(--p-right);
  --in-bottom: var(--p-bottom);
  --in-left: var(--p-left);
  --side-top: var(--p-ctl);   /* боковые колонки не заходят под кнопки Telegram */
  --side-left: 0px;
}

/* Телефон держат вертикально — разворачиваем всю игру на 90°, пользователю ничего поворачивать не нужно.
   Физический верх экрана становится левой стороной, физический правый край — верхней. */
html.rot {
  --in-top: var(--p-right);
  --in-right: var(--p-bottom);
  --in-bottom: var(--p-left);
  --in-left: calc(var(--p-top) + var(--p-ctl));
  --side-top: 0px;
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(120% 90% at 50% 0%, #1d2050 0%, var(--bg) 60%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* ---------- раскладка: одно поле на весь экран, управление поверх ---------- */

#app {
  position: relative;
  width: var(--lw);
  height: var(--lh);
  padding: calc(2px + var(--in-top)) calc(4px + var(--in-right)) calc(2px + var(--in-bottom)) calc(4px + var(--in-left));
}
html.rot #app {
  position: fixed;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  transform: translateX(100vw) rotate(90deg);
}

#stage {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
#field { position: relative; }   /* размер задаёт js/main.js — ровно по холсту */
canvas { display: block; border-radius: 6px; box-shadow: 0 8px 36px rgba(0, 0, 0, 0.55); }

/* ---------- игроки и часы — на верхней стене карты ---------- */

#hud {
  position: absolute;
  top: 0;
  left: calc(var(--cell, 32px) * 2.2);
  right: calc(var(--cell, 32px) * 2.2);
  height: var(--cell, 32px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}
#hud > * { pointer-events: auto; }
#chips { display: flex; gap: 6px; min-width: 0; }
.chip {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 2px 9px 2px 5px;
  border-radius: 999px;
  background: rgba(14, 16, 40, 0.8);
  border: 1px solid var(--glass-line);
  font-size: calc(var(--cell, 32px) * 0.36);
  font-weight: 700;
  white-space: nowrap;
  transition: opacity 0.3s, transform 0.3s;
}
.chip::before {
  content: "";
  width: calc(var(--cell, 32px) * 0.34);
  height: calc(var(--cell, 32px) * 0.34);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff8, var(--c) 55%);
  box-shadow: 0 0 8px var(--c);
  flex: none;
}
.chip span { overflow: hidden; text-overflow: ellipsis; max-width: 9ch; }
.chip.me { border-color: var(--c); box-shadow: 0 0 0 1px var(--c) inset; }
.chip.dead { opacity: 0.35; text-decoration: line-through; transform: scale(0.95); }

#topbar { display: flex; align-items: center; gap: 6px; }
#clock {
  min-width: 3.4em;
  text-align: center;
  padding: 2px 9px;
  border-radius: 999px;
  background: rgba(14, 16, 40, 0.8);
  border: 1px solid var(--glass-line);
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: calc(var(--cell, 32px) * 0.38);
  color: var(--muted);
}
#clock:empty { display: none; }
#ping {
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(14, 16, 40, 0.8);
  border: 1px solid var(--glass-line);
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: calc(var(--cell, 32px) * 0.3);
  color: #6df0b0;
}
#ping.mid { color: #ffe27a; }
#ping.bad { color: #ff6a6a; }
#ping:empty { display: none; }
.round {
  font: inherit;
  font-size: calc(var(--cell, 32px) * 0.4);
  color: var(--text);
  background: rgba(14, 16, 40, 0.8);
  border: 1px solid var(--glass-line);
  border-radius: 50%;
  width: calc(var(--cell, 32px) * 0.78);
  height: calc(var(--cell, 32px) * 0.78);
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
}
.round[hidden] { display: none; }
.round svg { width: 55%; height: 55%; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* подсказка — на нижней стене карты */
#banner {
  position: absolute;
  bottom: calc(var(--cell, 32px) * 0.14);
  left: 50%;
  transform: translateX(-50%);
  max-width: 60%;
  padding: 2px 12px;
  border-radius: 999px;
  background: rgba(14, 16, 40, 0.82);
  border: 1px solid var(--glass-line);
  font-size: calc(var(--cell, 32px) * 0.38);
  font-weight: 700;
  white-space: nowrap;
  color: var(--text);
  pointer-events: none;
}
#banner:empty { display: none; }
#banner.alert { color: #fff; background: rgba(190, 20, 40, 0.88); animation: throb 0.5s infinite alternate; }
#banner.hint { color: #ffe27a; }
@keyframes throb { to { transform: translateX(-50%) scale(1.06); } }

/* ---------- оверлей: меню, лобби, итоги ---------- */

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  overflow-y: auto;
  z-index: 3;
  background: linear-gradient(180deg, rgba(8, 9, 28, 0.55), rgba(8, 9, 28, 0.82));
  backdrop-filter: blur(3px);
  border-radius: 10px;
}
#overlay[hidden] { display: none; }
#overlay.light { background: rgba(8, 9, 28, 0.35); backdrop-filter: none; }
.card { text-align: center; padding: 10px 18px; width: 100%; max-width: 460px; margin: auto; animation: pop 0.28s cubic-bezier(.2, 1.3, .4, 1); }
@keyframes pop { from { opacity: 0; transform: scale(0.92) translateY(8px); } }
.card h1 { margin: 0 0 6px; font-size: 24px; letter-spacing: 0.02em; text-shadow: 0 2px 12px rgba(0, 0, 0, 0.5); }
.card h1.logo {
  font-size: 34px;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: linear-gradient(180deg, #fff3b0 0%, #ffb23f 45%, #ff5a2f 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 3px 0 #7a1d0b) drop-shadow(0 6px 14px rgba(255, 90, 30, 0.45));
}
.card h1.huge { font-size: 96px; line-height: 1; animation: beat 1s infinite; }
@keyframes beat { 0% { transform: scale(1.25); opacity: 0.4; } 30% { transform: scale(1); opacity: 1; } }
#overlay-text { margin: 0 0 10px; color: var(--muted); line-height: 1.4; font-size: 14px; }
#overlay-text b { color: var(--text); }
#overlay-actions { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 8px; }
.code {
  font-size: 34px;
  font-weight: 900;
  letter-spacing: 0.2em;
  margin: 2px 0 6px;
  padding-left: 0.2em;
  color: var(--text);
  text-shadow: 0 0 18px rgba(255, 160, 60, 0.6);
}
.card input {
  font: inherit;
  font-size: 24px;
  font-weight: 700;
  text-align: center;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  width: 6.5em;
  padding: 6px;
  border-radius: 14px;
  border: 2px solid var(--glass-line);
  background: rgba(0, 0, 0, 0.35);
  color: var(--text);
  user-select: text;
  -webkit-user-select: text;
}
.plist { list-style: none; margin: 0 auto 8px; padding: 0; text-align: left; max-width: 260px; }
.plist li { display: flex; gap: 8px; align-items: center; padding: 3px 10px; color: var(--text); font-weight: 600; }
.plist li::before { content: ""; width: 11px; height: 11px; border-radius: 50%; background: var(--c); box-shadow: 0 0 8px var(--c); }
.plist .tag { margin-left: auto; font-size: 12px; color: var(--muted); font-weight: 500; }

.card button {
  font: inherit;
  font-weight: 800;
  font-size: 16px;
  color: #fff;
  background: linear-gradient(180deg, #ffb347 0%, var(--accent) 45%, var(--accent2) 100%);
  border: 0;
  border-radius: 16px;
  padding: 10px 26px;
  cursor: pointer;
  box-shadow: 0 4px 0 #9a2b12, 0 8px 18px rgba(255, 90, 30, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  text-shadow: 0 1px 2px rgba(120, 20, 0, 0.5);
  transition: transform 0.08s, box-shadow 0.08s;
}
.card button.secondary {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
  text-shadow: none;
  padding: 8px 18px;
}
.card button:active { transform: translateY(3px); box-shadow: 0 1px 0 #9a2b12; }
.card button.secondary:active { box-shadow: 0 0 0 rgba(0, 0, 0, 0.35); }

.board { list-style: none; margin: 0 auto 8px; padding: 0; text-align: left; font-size: 14px; color: var(--text); max-width: 300px; }
.board li { display: flex; gap: 8px; padding: 3px 8px; border-radius: 10px; }
.board li.me { background: var(--glass); color: #ffc36b; font-weight: 700; }
.board .pos { width: 1.6em; color: var(--muted); }
.board .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .sc { font-variant-numeric: tabular-nums; font-weight: 700; }
.board .gm { color: var(--muted); font-size: 12px; align-self: center; }

/* ---------- управление (поверх карты) ---------- */

:root { --joy-scale: 1; --act-scale: 1; --ctl-opacity: 0.8; }

#app.controls-off #dpad,
#app.controls-off #actions { opacity: 0; pointer-events: none; }
/* пока открыты настройки — показываем управление, чтобы видеть эффект ползунков */
#app.preview #dpad,
#app.preview #actions { opacity: var(--ctl-opacity) !important; }
#app.controls-off #zone, #app.preview #zone { pointer-events: none; }
#dpad, #actions { transition: opacity 0.2s; }

/* прозрачная область касаний джойстика: сторона экрана ниже верхней стены карты */
#zone {
  position: absolute;
  top: calc(var(--cell, 32px) * 1.1);
  bottom: 0;
  left: 0;
  width: 50%;
  z-index: 4;
  touch-action: none;
}
#app.lefty #zone { left: auto; right: 0; }

#dpad {
  position: absolute;
  left: calc(10px + var(--in-left));
  bottom: calc(6px + var(--in-bottom));
  z-index: 5;
  width: calc(clamp(104px, calc(var(--lh) * 0.4), 170px) * var(--joy-scale));
  aspect-ratio: 1;
  border-radius: 50%;
  background:
    radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.05) 0 38%, transparent 39%),
    radial-gradient(circle at 50% 40%, rgba(58, 63, 122, 0.5), rgba(27, 30, 68, 0.5) 75%);
  border: 2px solid rgba(255, 255, 255, 0.18);
  box-shadow: inset 0 6px 18px rgba(0, 0, 0, 0.45), 0 6px 18px rgba(0, 0, 0, 0.35);
  opacity: var(--ctl-opacity);
  pointer-events: none;   /* касания принимает #zone */
}
#app.lefty #dpad { left: auto; right: calc(10px + var(--in-right)); }
/* «плавающий» режим: в покое — бледная подсказка на привычном месте, при касании — под пальцем */
#dpad.idle { opacity: calc(var(--ctl-opacity) * 0.45); }
#dpad[data-float] {
  bottom: auto;
  right: auto;
  transform: translate(-50%, -50%);
  transition: none;
}
#dpad.active { box-shadow: inset 0 6px 18px rgba(0, 0, 0, 0.45), 0 0 0 4px rgba(255, 179, 71, 0.22), 0 6px 18px rgba(0, 0, 0, 0.35); }
.arrow {
  position: absolute;
  font-size: 14px;
  color: #fff;
  opacity: 0.3;
  transition: opacity 0.05s, color 0.05s;
}
.arrow.up { top: 7%; left: 50%; transform: translateX(-50%); }
.arrow.down { bottom: 7%; left: 50%; transform: translateX(-50%); }
.arrow.left { left: 9%; top: 50%; transform: translateY(-50%); }
.arrow.right { right: 9%; top: 50%; transform: translateY(-50%); }
#dpad[data-dir="up"] .up, #dpad[data-dir2="up"] .up,
#dpad[data-dir="down"] .down, #dpad[data-dir2="down"] .down,
#dpad[data-dir="left"] .left, #dpad[data-dir2="left"] .left,
#dpad[data-dir="right"] .right, #dpad[data-dir2="right"] .right { color: #ffb347; opacity: 1; }

#knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 42%;
  height: 42%;
  margin: -21% 0 0 -21%;
  border-radius: 50%;
  background: radial-gradient(circle at 36% 30%, #8d93da, #4a4f9c 55%, #2c3070);
  border: 2px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.5), inset 0 2px 4px rgba(255, 255, 255, 0.35);
  transform: translate(var(--kx, 0px), var(--ky, 0px));
  pointer-events: none;
}

#actions {
  position: absolute;
  right: calc(12px + var(--in-right));
  bottom: calc(8px + var(--in-bottom));
  z-index: 5;
  opacity: var(--ctl-opacity);
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: 12px;
}

.action {
  width: calc(clamp(80px, calc(var(--lh) * 0.32), 130px) * var(--act-scale));
  aspect-ratio: 1;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.35);
  padding: 14%;
  background: radial-gradient(circle at 38% 28%, rgba(255, 197, 107, 0.78) 0%, rgba(255, 138, 47, 0.74) 45%, rgba(255, 77, 47, 0.74) 100%);
  box-shadow: 0 7px 0 rgba(154, 43, 18, 0.9), 0 12px 24px rgba(255, 90, 30, 0.35), inset 0 3px 8px rgba(255, 255, 255, 0.45);
  touch-action: none;
  cursor: pointer;
  transition: transform 0.05s, box-shadow 0.05s;
}
.action svg { width: 100%; height: 100%; filter: drop-shadow(0 3px 3px rgba(0, 0, 0, 0.4)); }
.action.pressed { transform: translateY(5px); box-shadow: 0 2px 0 rgba(154, 43, 18, 0.9), 0 4px 10px rgba(255, 90, 30, 0.4), inset 0 3px 8px rgba(255, 255, 255, 0.45); }


#app.lefty #actions { right: auto; left: calc(12px + var(--in-left)); }

/* ---------- экран настроек ---------- */

.settings { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 22px; text-align: left; margin-bottom: 8px; }
.settings .row { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.settings .row > label, .settings .row > .lbl { font-size: 12.5px; font-weight: 700; color: var(--muted); display: flex; justify-content: space-between; gap: 6px; }
.settings .row > label b, .settings .row > .lbl b { color: var(--text); font-variant-numeric: tabular-nums; }
.seg { display: flex; gap: 4px; }
.card .seg button {
  flex: 1;
  font-size: 12.5px;
  font-weight: 700;
  padding: 6px 4px;
  border-radius: 10px;
  color: var(--text);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  box-shadow: none;
  text-shadow: none;
  white-space: nowrap;
}
.card .seg button.on { background: linear-gradient(180deg, #ffb347, var(--accent2)); border-color: transparent; color: #fff; }
.card .seg button:active { transform: none; box-shadow: none; }
.settings input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 26px;
  background: transparent;
  touch-action: pan-x;
  accent-color: var(--accent);
}
.settings input[type="range"]::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: var(--glass-line); }
.settings input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 22px; height: 22px; margin-top: -8px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #ffd9a0, var(--accent)); border: 2px solid #fff6; }
.settings input[type="range"]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--glass-line); }
.settings input[type="range"]::-moz-range-thumb { width: 20px; height: 20px; border-radius: 50%; background: var(--accent); border: 2px solid #fff6; }
@media (max-width: 520px) { .settings { grid-template-columns: 1fr; } }
.card.wide { max-width: 680px; }
