/* place_arena — одна раскладка на ПК и телефон.
   Разница только в позиционировании панели: колонка справа против шторки снизу.
   Отдельной мобильной версии нет, поэтому логика интерфейса тоже одна. */

:root {
  /* Отступы под рекламные полосы. Поле сжимается на них, поэтому реклама
     оказывается за его рамкой, а не поверх полотна. */
  --bg: #1b1d23;
  --surface: #22252d;
  --surface-2: #2c303a;
  --line: #383d49;
  --fg: #dce2ea;
  --muted: #8b95a6;
  --accent: #3f76d0;
  --ok: #66b04c;
  --warn: #e89533;
  --danger: #d0423f;
  --radius: 10px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--fg);
  font: 14px/1.5 ui-sans-serif, system-ui, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

/* Контейнер поля отделён от самого холста намеренно.
   У <canvas> ширина auto берётся не от бокса, а от собственного буфера, и при
   попытке задать размер только отступами он начинает раздуваться сам от себя:
   буфер растёт -> растёт CSS-размер -> снова растёт буфер. */
#stage {
  position: fixed; inset: 0;
  /* Видимая граница поля: за ней начинается всё, что полем не является. */
  box-shadow: 0 0 0 1px var(--line);
}

#field {
  position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1;
  display: block; cursor: crosshair;
  touch-action: none; /* жесты обрабатываем сами, иначе браузер съест pinch */
  image-rendering: pixelated;
}

.overlay {
  position: absolute; inset: 0; width: 100%; height: 100%;
  display: none; pointer-events: none; z-index: 5;
}
.overlay.on { display: block; }

/* Подсказка появляется только когда файл действительно тащат над полем. */
.dropzone {
  position: fixed; inset: 12px; z-index: 30; display: none;
  align-items: center; justify-content: center;
  border: 3px dashed var(--accent); border-radius: 16px;
  background: rgba(63, 118, 208, .12); color: var(--fg); font-size: 18px;
  pointer-events: none;
}
body.dragging .dropzone { display: flex; }

.edit-bar {
  position: fixed; z-index: 12; left: 50%; transform: translateX(-50%);
  bottom: calc(126px + var(--safe-b));
  /* Один ряд с прокруткой, а не перенос: перенос вырастал до трёх рядов и
     закрывал нижнюю треть поля — ровно ту, по которой и рисуют. */
  display: none; gap: 6px; flex-wrap: nowrap; align-items: center;
  max-width: calc(100vw - 16px); overflow-x: auto; scrollbar-width: thin;
  padding: 6px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--line);
}
.edit-bar > * { flex: 0 0 auto; }
body.editing .edit-bar { display: flex; }

.tool {
  font-size: 13px; padding: 7px 11px; border-radius: 8px;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--fg);
  cursor: pointer; white-space: nowrap;
}
.tool.on { border-color: var(--accent); color: var(--accent); }
.tool.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.tool.danger { color: var(--danger); }
.tool.file { display: inline-flex; align-items: center; }
.tool label, label.tool { display: inline-flex; align-items: center; gap: 6px; }
.text-input { width: 190px; padding: 6px 9px; display: none; }
body.tool-text .text-input { display: inline-block; }
#edit-thickness { width: 90px; }

@media (pointer: coarse) {
  .tool { padding: 10px 12px; }
}

/* --- верхняя панель ------------------------------------------------------- */

.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: linear-gradient(180deg, rgba(27,29,35,.95), rgba(27,29,35,0));
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }

.brand { font-weight: 700; letter-spacing: .02em; }
.brand span { color: var(--muted); font-weight: 500; }

.status { font-size: 12px; padding: 3px 9px; border-radius: 99px; background: var(--surface); }
.status.online { color: var(--ok); }
.status.connecting { color: var(--warn); }
.status.offline { color: var(--danger); }

.coords { margin-left: auto; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }

/* --- индикатор кулдауна --------------------------------------------------- */

.cooldown {
  position: fixed; z-index: 10; left: 50%; transform: translateX(-50%);
  /* Все плавающие панели отсчитываются от верха рекламной полосы, а не от низа
     экрана: иначе они наезжают на палитру, как только реклама появляется. */
  bottom: calc(84px + var(--safe-b));
  padding: 6px 16px; border-radius: 99px;
  font-size: 13px; font-variant-numeric: tabular-nums;
  background: var(--surface); border: 1px solid var(--line);
  transition: color .2s;
}
.cooldown.ready { color: var(--ok); }
.cooldown.waiting { color: var(--warn); }
.cooldown.free { color: var(--accent); border-color: var(--accent); }

/* --- палитра -------------------------------------------------------------- */

.palette-wrap {
  position: fixed; z-index: 10;
  left: 0; right: 0;
  bottom: 0; padding: 8px 8px calc(8px + var(--safe-b));
  background: linear-gradient(0deg, rgba(27,29,35,.96), rgba(27,29,35,.6) 70%, transparent);
}
.palette {
  display: flex; gap: 6px; overflow-x: auto; padding: 2px;
  scrollbar-width: none; justify-content: center;
}
.palette::-webkit-scrollbar { display: none; }

.swatch {
  flex: 0 0 auto; width: 34px; height: 34px;
  border: 2px solid transparent; border-radius: 8px; cursor: pointer;
  padding: 0; transition: transform .12s, border-color .12s;
}
.swatch:hover { transform: translateY(-2px); }
.swatch.active { border-color: var(--fg); transform: translateY(-4px); }

/* Палец крупнее курсора: на тач-экране цели меньше 44px промахиваются. */
@media (pointer: coarse) {
  .swatch { width: 40px; height: 40px; }
  .palette { justify-content: flex-start; }
}

/* --- боковая панель ------------------------------------------------------- */

.panel {
  position: fixed; z-index: 20; top: 0; right: 0; bottom: 0; width: 340px;
  display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--line);
  transform: translateX(100%); transition: transform .22s ease;
}
body.panel-open .panel { transform: none; }

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px calc(14px + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--line);
}
.panel-body { flex: 1; overflow-y: auto; padding: 14px; padding-bottom: 90px; }

@media (max-width: 720px) {
  /* На узком экране панель — шторка снизу: тянуться до верхнего угла одной
     рукой неудобно, а нижние 60% экрана достижимы большим пальцем. */
  .panel {
    top: auto; left: 0; width: auto; height: 78vh;
    border-left: 0; border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
    transform: translateY(100%);
  }
  body.panel-open .panel { transform: none; }
  .panel-head { padding-top: 14px; }
}

/* --- элементы ------------------------------------------------------------- */

section { margin-bottom: 22px; }
h3 { margin: 0 0 8px; font-size: 15px; }
h4 { margin: 14px 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.muted { color: var(--muted); }

button {
  font: inherit; color: var(--fg); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 12px; cursor: pointer; transition: background .12s, border-color .12s;
}
button:hover:not([disabled]) { background: #343945; }
button[disabled] { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #4f86e2; }
button.danger { color: var(--danger); }
button.on { border-color: var(--accent); color: var(--accent); }
button.wide { width: 100%; margin-top: 8px; }
button.small { padding: 5px 9px; font-size: 12px; }
button.link { background: none; border: 0; color: var(--muted); text-decoration: underline; padding: 0; }
button.icon { background: none; border: 0; font-size: 18px; line-height: 1; padding: 4px 8px; }

input {
  font: inherit; color: var(--fg); background: var(--bg);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 11px; width: 100%; min-width: 0;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type=file] { padding: 7px; }
input[type=checkbox] { width: auto; }

.form { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 1 1 auto; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; }

.account { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.stat {
  background: var(--bg); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 8px 10px;
}
.stat b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 11px; color: var(--muted); }
.stats h4, .stats .top { grid-column: 1 / -1; }

.top { margin: 0; padding-left: 18px; }
.top li { display: flex; justify-content: space-between; gap: 10px; }

.clan-list { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.clan-row, .stencil-row {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--bg); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 8px 10px;
}
.stencil-row { flex-direction: column; align-items: stretch; gap: 8px; margin-bottom: 8px; }

.layer {
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px; display: flex; flex-direction: column; gap: 8px;
}
.layer-head { display: flex; justify-content: space-between; gap: 8px; }

.price { font-size: 22px; font-weight: 700; }

.refresh-box { margin-top: 14px; }
select.refresh {
  font: inherit; color: var(--fg); background: var(--bg);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 10px; width: 100%;
}
.small-text { font-size: 12px; margin: 8px 0 0; }

/* --- реклама вокруг поля ---------------------------------------------------
   Блоки живут в координатах мира и ездят вместе с полотном: они лежат за его
   границей, как фон, и не занимают места на экране. Слой лежит под холстом,
   который вне поля прозрачен. */

#world {
  position: absolute; inset: 0; overflow: hidden;
  background: #0e1014;
  z-index: 0;
}
#world-inner { position: absolute; left: 0; top: 0; transform-origin: 0 0; }

.ad {
  position: absolute;
  border: 1px solid #262b35; border-radius: 4px; overflow: hidden;
  background: #14161b;
}
.ad img { display: block; width: 100%; height: 100%; object-fit: contain; }

/* Подпись обязательна: блок за границей поля всё равно должен читаться как
   реклама, а не как часть игры. */
.ad-label {
  position: absolute; left: 0; top: -1.4em;
  font-size: 10px; letter-spacing: .14em; text-transform: uppercase;
  color: #5a6373; white-space: nowrap;
}

/* --- уведомления и модальные окна ----------------------------------------- */

.toasts {
  position: fixed; z-index: 40;
  left: 50%; transform: translateX(-50%);
  top: calc(56px + env(safe-area-inset-top, 0px));
  display: flex; flex-direction: column; gap: 6px; align-items: center;
  pointer-events: none;
}
.toast {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 99px; padding: 7px 14px; font-size: 13px;
  animation: pop .18s ease; max-width: 90vw;
}
.toast.error { border-color: var(--danger); color: #f2a6a4; }
.toast.hint { border-color: var(--accent); color: #9dc0f5; }
.toast.out { opacity: 0; transition: opacity .5s; }
@keyframes pop { from { transform: translateY(-6px); opacity: 0 } }

.modal {
  position: fixed; inset: 0; z-index: 50; display: none;
  align-items: center; justify-content: center;
  background: rgba(12,13,16,.7); padding: 16px;
}
.modal.open { display: flex; }
.sheet {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; width: min(440px, 100%);
  max-height: 86vh; overflow-y: auto; padding: 16px;
}
.sheet header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet h2 { margin: 0; font-size: 17px; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
