* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: Arial, Helvetica, sans-serif;
  background: radial-gradient(circle at top, #2b3347 0%, #161b26 45%, #0e1118 100%);
  color: #f3f5f8;
}

body {
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

button {
  font: inherit;
}

input,
button {
  outline: none;
}

.app {
  display: flex;
  width: 100%;
  height: 100%;
}

.sidebar {
  width: 320px;
  min-width: 320px;
  padding: 18px 16px;
  background: rgba(10, 12, 18, 0.86);
  border-right: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  z-index: 5;
  overflow-y: auto;
}

.sidebar h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.05;
  font-weight: 800;
}

.muted {
  margin: -4px 0 0;
  color: rgba(255, 255, 255, 0.68);
  font-size: 14px;
  line-height: 1.4;
}

.block {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.block-title {
  font-size: 14px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.92);
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toolbar.compact {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.toolbar button,
.topbar-actions button {
  width: 100%;
  min-height: 46px;
  border: 0;
  border-radius: 14px;
  padding: 10px 14px;
  background: linear-gradient(180deg, #f2f5fb 0%, #d8dfec 100%);
  color: #121826;
  font-weight: 800;
  cursor: pointer;
  box-shadow:
    0 10px 18px rgba(0, 0, 0, 0.18),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
}

.toolbar button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.active-color-dot {
  width: 18px;
  height: 18px;
  min-width: 18px;
  border-radius: 50%;
  background: #e74c3c;
  border: 2px solid #ffffff;
  box-shadow:
    0 0 0 2px rgba(17, 24, 38, 0.32),
    0 3px 8px rgba(0, 0, 0, 0.28);
}

.palette {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.color-btn {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 14px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  cursor: pointer;
  box-shadow:
    0 8px 16px rgba(0, 0, 0, 0.2),
    inset 0 2px 6px rgba(255, 255, 255, 0.2);
}

.color-btn.active {
  border-color: #ffffff;
  box-shadow:
    0 0 0 2px rgba(255, 255, 255, 0.18),
    0 12px 20px rgba(0, 0, 0, 0.28),
    inset 0 2px 6px rgba(255, 255, 255, 0.25);
}

.status-card {
  padding: 12px 14px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.status-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
  margin: 6px 0;
}

.hint {
  display: none;
}

.stage-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at center, rgba(119, 151, 214, 0.13) 0%, rgba(0, 0, 0, 0) 55%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.03), rgba(255, 255, 255, 0));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  min-height: 52px;
  background: rgba(0, 0, 0, 0.18);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.topbar-title {
  font-size: 16px;
  font-weight: 800;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.topbar-actions button {
  min-height: 38px;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 14px;
  width: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.viewport {
  position: relative;
  width: 100%;
  height: calc(100% - 52px);
  overflow: hidden;
  cursor: grab;
  touch-action: none;
}

.viewport.dragging-pan {
  cursor: grabbing;
}

.iso-scene {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 1100px;
  height: 780px;
  transform: translate(-50%, -50%);
  transform-origin: center center;
  will-change: transform;
}

.floor {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 760px;
  height: 760px;
  transform:
    translate(-50%, -50%)
    rotate(45deg)
    skew(-10deg, -10deg);
  border-radius: 44px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0.04)),
    linear-gradient(180deg, #3a4864 0%, #243147 52%, #1a2332 100%);
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.05),
    inset 0 -40px 60px rgba(0, 0, 0, 0.22),
    0 40px 100px rgba(0, 0, 0, 0.35);
}

.floor::before {
  content: "";
  position: absolute;
  inset: 24px;
  border-radius: 28px;
  background:
    repeating-linear-gradient(
      0deg,
      rgba(255, 255, 255, 0.04) 0,
      rgba(255, 255, 255, 0.04) 2px,
      transparent 2px,
      transparent 42px
    ),
    repeating-linear-gradient(
      90deg,
      rgba(255, 255, 255, 0.03) 0,
      rgba(255, 255, 255, 0.03) 2px,
      transparent 2px,
      transparent 42px
    );
  opacity: 0.55;
}

.figures-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.figure {
  position: absolute;
  width: 72px;
  height: 152px;
  transform: translate(-50%, -88%) translateZ(0);
  user-select: none;
  touch-action: none;
  cursor: grab;
  filter: drop-shadow(0 12px 16px rgba(0, 0, 0, 0.25));
  will-change: left, top, transform;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transition: transform 0.08s linear;
}

.figure.dragging {
  cursor: grabbing;
}

.figure.selected {
  transform: translate(-50%, -88%) scale(1.04) translateZ(0);
  filter: drop-shadow(0 14px 18px rgba(0, 0, 0, 0.28));
}

.figure-shadow {
  position: absolute;
  left: 50%;
  bottom: 40px;
  width: 52px;
  height: 18px;
  transform: translateX(-50%);
  border-radius: 50%;
  background: radial-gradient(circle, rgba(0, 0, 0, 0.42) 0%, rgba(0, 0, 0, 0.1) 60%, rgba(0, 0, 0, 0) 100%);
  pointer-events: none;
}

.figure-body {
  position: absolute;
  left: 50%;
  bottom: 50px;
  width: 54px;
  height: 88px;
  transform: translateX(-50%);
  border-radius: 22px 22px 18px 18px;
  background: var(--figure-color, #e74c3c);
  box-shadow:
    inset 0 -10px 16px rgba(0, 0, 0, 0.16),
    inset 0 10px 10px rgba(255, 255, 255, 0.15);
}

.figure.selected .figure-body {
  box-shadow:
    0 0 0 3px #ffffff,
    inset 0 -10px 16px rgba(0, 0, 0, 0.16),
    inset 0 10px 10px rgba(255, 255, 255, 0.15);
}

/* ГОЛОВА ФІГУРИ */
.figure-body::before {
  content: "";
  position: absolute;
  left: 50%;
  top: -22px;
  width: 34px;
  height: 34px;
  transform: translateX(-50%);
  border-radius: 50%;
  background: inherit;
  box-shadow:
    inset 0 -5px 8px rgba(0, 0, 0, 0.12),
    inset 0 5px 6px rgba(255, 255, 255, 0.12);
}

.figure-body::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -8px;
  width: 18px;
  height: 16px;
  transform: translateX(-50%);
  border-radius: 0 0 10px 10px;
  background: rgba(255, 255, 255, 0.14);
  opacity: 0.35;
}

.figure-face-arrow {
  position: absolute;
  left: 50%;
  top: -34px;
  width: 24px;
  height: 24px;
  transform: translateX(-50%) rotate(var(--face-angle, 0deg));
  transform-origin: 50% 50%;
  pointer-events: none;
}

.figure-face-arrow::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 1px;
  width: 4px;
  height: 14px;
  transform: translateX(-50%);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.96);
}

.figure-face-arrow::after {
  content: "";
  position: absolute;
  left: 50%;
  top: -3px;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 10px solid rgba(255, 255, 255, 0.96);
}

.figure-label {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  min-width: 56px;
  max-width: 148px;
  padding: 6px 10px;
  border-radius: 12px;
  background: rgba(10, 16, 26, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.14);
  color: #ffffff;
  font-size: 12px;
  line-height: 1.15;
  font-weight: 700;
  text-align: center;
  word-break: break-word;
  pointer-events: none;
}

.figure.selected .figure-label {
  border-color: rgba(255, 255, 255, 0.65);
}

/* М НАПРЯМОК */

.field-direction {
  position: absolute;
  z-index: 80;
  pointer-events: none;
  width: 44px;
  height: 44px;
}

.field-direction .fd-label {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 24px;
  height: 24px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(10, 16, 26, 0.9);
  border: 2px solid #fff;
  color: #fff;
  font-size: 13px;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
}

.field-direction .fd-arrow {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 28px;
  height: 28px;
  border-top: 4px solid #fff;
  border-right: 4px solid #fff;
}

/* середини сторін ізометричного поля */
.field-direction.top {
  left: 50%;
  top: 24%;
  transform: translate(-50%, -50%);
}

.field-direction.top .fd-arrow {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.field-direction.right {
  left: 73%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.field-direction.right .fd-arrow {
  transform: translate(-50%, -50%) rotate(45deg);
}

.field-direction.bottom {
  left: 50%;
  top: 76%;
  transform: translate(-50%, -50%);
}

.field-direction.bottom .fd-arrow {
  transform: translate(-50%, -50%) rotate(135deg);
}

.field-direction.left {
  left: 27%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.field-direction.left .fd-arrow {
  transform: translate(-50%, -50%) rotate(225deg);
}

/* MOBILE */

@media (max-width: 900px) {
  .app {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    min-width: 0;
    height: 26vh;
    max-height: 26vh;
    overflow-y: auto;
    padding: 8px 10px;
    gap: 8px;
    border-right: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(10, 12, 18, 0.92);
  }

  .sidebar h1 {
    font-size: 18px;
    margin: 0;
  }

  .muted {
    font-size: 11px;
    margin: 0;
  }

  .block-title {
    font-size: 11px;
    margin-bottom: 2px;
  }

  .toolbar,
  .toolbar.compact {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .toolbar button {
    min-height: 28px;
    padding: 5px 8px;
    border-radius: 10px;
    font-size: 11px;
  }

  .palette {
    grid-template-columns: repeat(8, 1fr);
    gap: 6px;
  }

  .color-btn {
    border-radius: 10px;
    border-width: 2px;
  }

  .status-card {
    display: none;
  }

  .stage-wrap {
    height: 74vh;
    min-height: 74vh;
  }

  .topbar {
    min-height: 38px;
    padding: 5px 8px;
    gap: 6px;
  }

  .topbar-title {
    font-size: 13px;
    flex: 0 0 auto;
  }

  .topbar-actions {
    gap: 6px;
    flex-wrap: nowrap;
    overflow-x: auto;
  }

  .topbar-actions button {
    min-height: 28px;
    padding: 4px 8px;
    border-radius: 10px;
    font-size: 11px;
    white-space: nowrap;
    flex: 0 0 auto;
  }

  .viewport {
    height: calc(100% - 38px);
  }

  .iso-scene {
    width: 860px;
    height: 620px;
  }

  .floor {
    width: 540px;
    height: 540px;
  }

  .figure {
    width: 58px;
    height: 126px;
  }

  .figure-shadow {
    bottom: 34px;
    width: 42px;
    height: 14px;
  }

  .figure-body {
    bottom: 42px;
    width: 44px;
    height: 72px;
  }

  .figure-body::before {
    width: 28px;
    height: 28px;
    top: -18px;
  }

  .figure-face-arrow {
    top: -28px;
    width: 20px;
    height: 20px;
  }

  .figure-face-arrow::before {
    height: 12px;
  }

  .figure-face-arrow::after {
    border-left-width: 6px;
    border-right-width: 6px;
    border-bottom-width: 8px;
  }

  .figure-label {
    min-width: 48px;
    max-width: 118px;
    padding: 4px 8px;
    font-size: 10px;
    border-radius: 10px;
  }

  .field-direction.top {
    left: 50%;
    top: 24%;
  }

  .field-direction.right {
    left: 73%;
    top: 50%;
  }

  .field-direction.bottom {
    left: 50%;
    top: 76%;
  }

  .field-direction.left {
    left: 27%;
    top: 50%;
  }
}

/* ===== M ON FIELD SIDE — NO ARROW ===== */

.field-direction {
  position: absolute;
  z-index: 80;
  pointer-events: none;
  width: 28px;
  height: 28px;
}

.field-direction .fd-arrow {
  display: none !important;
}

.field-direction .fd-label {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: rgba(10, 16, 26, 0.92);
  border: 2px solid #fff;
  color: #fff;
  font-size: 13px;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* саме середини сторін ромба */
.field-direction.top {
  left: 50%;
  top: 7%;
  transform: translate(-50%, -50%);
}

.field-direction.right {
  left: 81%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.field-direction.bottom {
  left: 50%;
  top: 93%;
  transform: translate(-50%, -50%);
}

.field-direction.left {
  left: 19%;
  top: 50%;
  transform: translate(-50%, -50%);
}

@media (max-width: 900px) {
  .field-direction.top {
    left: 50%;
    top: 6.5%;
  }

  .field-direction.right {
    left: 81.5%;
    top: 50%;
  }

  .field-direction.bottom {
    left: 50%;
    top: 93.5%;
  }

  .field-direction.left {
    left: 18.5%;
    top: 50%;
  }
}

/* ===== ТОЧНО ПО РЕБРАМ РОМБА ===== */

.field-direction {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 80;
  pointer-events: none;
}

.field-direction .fd-arrow {
  display: none !important;
}

.field-direction .fd-label {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(10,16,26,0.95);
  border: 2px solid #fff;
  color: #fff;
  font-size: 13px;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ВАЖЛИВО: рухаємо по діагоналях ромба */

.field-direction.bottom {
  transform: translate(-50%, -50%) translate(0px, 300px);
}

.field-direction.top {
  transform: translate(-50%, -50%) translate(0px, -300px);
}

.field-direction.right {
  transform: translate(-50%, -50%) translate(300px, 0px);
}

.field-direction.left {
  transform: translate(-50%, -50%) translate(-300px, 0px);
}

/* мобілка */
@media (max-width: 900px) {
  .field-direction.bottom {
    transform: translate(-50%, -50%) translate(0px, 210px);
  }

  .field-direction.top {
    transform: translate(-50%, -50%) translate(0px, -210px);
  }

  .field-direction.right {
    transform: translate(-50%, -50%) translate(210px, 0px);
  }

  .field-direction.left {
    transform: translate(-50%, -50%) translate(-210px, 0px);
  }
}

/* ===== FIX LABELS ===== */
.figure-label {
  min-width: 80px !important;
  max-width: 180px !important;

  padding: 6px 10px !important;
  font-size: 12px !important;

  white-space: normal !important;
  word-break: normal !important;

  display: -webkit-box !important;
  -webkit-line-clamp: 2 !important;
  -webkit-box-orient: vertical !important;

  overflow: hidden !important;
  text-overflow: ellipsis !important;

  text-align: center !important;
}