:root {
  --bg: #0f1623;
  --panel: #151c29;
  --panel2: #1c2536;
  --panel3: #202b3f;
  --line: rgba(255,255,255,.07);
  --text: #eef4ff;
  --muted: #8ea0c2;
  --accent: #71e4ff;
  --accent2: #a78bfa;
  --success: #8bf3b2;
  --danger: #ff9f9f;
  --warn: #ffc966;
  --floor: #1a2435;
  --wall: #33425c;
  --grid: #233047;
  --player: #ffd86c;
  --exit-c: #adff6b;
  --plate-c: #68d8ff;
  --gate-closed: #ff9f74;
  --gate-open: #7ef1b0;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 500 14px/1.35 Inter, ui-sans-serif, system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body {
  height: 100dvh;
  touch-action: manipulation;
}

/* ─── layout ─── */
.app {
  display: grid;
  grid-template-columns: 296px 1fr;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: hidden;
}

.side-top,
.side-bottom {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.side-bottom {
  flex: 1;
}

/* ─── panels ─── */
.panel {
  background: linear-gradient(180deg, rgba(255,255,255,.03), rgba(255,255,255,.01));
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.section-label {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}

/* ─── logo ─── */
.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 2px;
}

.logo-mark {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  position: relative;
  flex: 0 0 28px;
}

.logo-mark::after {
  content: "";
  position: absolute;
  inset: 6px;
  border-radius: 50%;
  background: var(--player);
}

.logo-name {
  font-size: 18px;
  font-weight: 800;
  letter-spacing: -.03em;
  line-height: 1;
}

.logo-sub {
  font-size: 11px;
  color: var(--muted);
  line-height: 1;
  margin-top: 2px;
}

/* ─── compact fields ─── */
.info-strip {
  display: grid;
  grid-template-columns: 1.8fr repeat(3, 1fr);
  gap: 6px;
}

.compact-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.compact-field span {
  font-size: 10px;
  line-height: 1;
  color: var(--muted);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .06em;
}

input[type="number"],
input[type="text"],
textarea {
  width: 100%;
  min-width: 0;
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 9px;
  font-size: 13px;
  font-weight: 700;
  outline: none;
  transition: border-color .12s, background .12s;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  background: #212d43;
}

.field-name input {
  padding-left: 10px;
}

/* ─── buttons ─── */
.mode-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.group-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.mode-btn,
.group-btn,
.act-btn,
.test-btn,
.small-btn {
  appearance: none;
  border: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  transition: background .12s, border-color .12s, transform .06s, color .12s;
}

.mode-btn,
.group-btn {
  min-height: 38px;
  border-radius: 10px;
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--line);
  font-size: 12px;
  font-weight: 700;
  padding: 8px 6px;
  text-align: center;
}

.mode-btn:hover,
.group-btn:hover,
.act-btn:hover,
.test-btn:hover {
  background: #253048;
}

.mode-btn:active,
.group-btn:active,
.act-btn:active,
.test-btn:active,
.small-btn:active {
  transform: scale(.97);
}

.mode-btn.active {
  border-color: var(--accent);
  background: rgba(113,228,255,.12);
  color: var(--accent);
}

.group-btn {
  font-size: 14px;
  font-weight: 900;
  color: var(--gc);
  border-color: transparent;
}

.group-btn.active {
  border-color: var(--gc);
  background: rgba(255,255,255,.05);
}

.size-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 6px;
  align-items: end;
}

.small-btn {
  min-height: 38px;
  border-radius: 10px;
  padding: 8px 10px;
  background: var(--accent);
  color: #091121;
  font-size: 11px;
  font-weight: 900;
  white-space: nowrap;
}

.action-grid {
  display: grid;
  gap: 6px;
}

.action-grid-4 { grid-template-columns: repeat(4, 1fr); }
.action-grid-3 { grid-template-columns: repeat(3, 1fr); }

.act-btn {
  min-height: 38px;
  border-radius: 10px;
  padding: 8px 8px;
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--line);
  font-size: 12px;
  font-weight: 800;
  text-align: center;
}

.act-btn.primary {
  background: var(--accent);
  color: #0a1020;
  border-color: transparent;
}

.act-btn.danger {
  color: var(--danger);
}

.status-msg {
  min-height: 18px;
  font-size: 12px;
  line-height: 18px;
  color: var(--success);
  padding: 2px 2px 0;
}

/* ─── main canvas ─── */
.main-area {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.canvas-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.coord-info {
  font-size: 12px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.validation-info {
  font-size: 12px;
  font-weight: 800;
  text-align: right;
}

.validation-info.ok  { color: var(--success); }
.validation-info.err { color: var(--danger); }

.canvas-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(circle at center, rgba(255,255,255,.02), transparent 52%),
    linear-gradient(180deg, rgba(255,255,255,.01), rgba(255,255,255,0));
}

.canvas-wrap canvas {
  display: block;
  cursor: crosshair;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

/* ─── test overlay ─── */
.test-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg);
  display: none;
  flex-direction: column;
  overflow: hidden;
}

.test-overlay.show {
  display: flex;
}

.test-bar {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: calc(10px + var(--safe-top)) 16px 10px;
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.test-title {
  font-size: 16px;
  font-weight: 900;
  color: var(--accent);
}

.test-stats {
  display: flex;
  gap: 14px;
  color: var(--muted);
  font-size: 13px;
  flex: 1;
  flex-wrap: wrap;
}

.test-stats strong {
  color: var(--text);
}

.test-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.test-btn {
  min-height: 36px;
  border-radius: 10px;
  padding: 7px 13px;
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--line);
  font-size: 12px;
  font-weight: 800;
}

.test-btn.primary {
  background: var(--accent);
  color: #0a1020;
  border-color: transparent;
}

.test-btn.danger {
  color: var(--danger);
}

.test-btn:disabled,
.act-btn:disabled,
.small-btn:disabled {
  opacity: .45;
  cursor: default;
}

.test-canvas-wrap {
  flex: 1;
  min-height: 0;
}

.test-canvas-wrap canvas {
  cursor: default;
}

.test-msg {
  min-height: 40px;
  line-height: 1.3;
  padding: 8px 14px calc(10px + var(--safe-bottom));
  text-align: center;
  font-size: 14px;
  font-weight: 800;
}

.test-msg.win  { color: var(--success); }
.test-msg.info { color: var(--muted); }

/* ─── modal ─── */
.modal-bg {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,.62);
  display: none;
  align-items: center;
  justify-content: center;
}

.modal-bg.show {
  display: flex;
}

.modal {
  width: min(520px, 92vw);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 18px 60px rgba(0,0,0,.35);
}

.modal-title {
  font-size: 16px;
  font-weight: 900;
}

.modal textarea {
  resize: vertical;
  min-height: 180px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* ─── mobile app layout ─── */
@media (max-width: 820px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr) auto;
    height: 100dvh;
  }

  .sidebar {
    display: contents;
  }

  .side-top {
    grid-row: 1;
    background: linear-gradient(180deg, rgba(21,28,41,.98), rgba(21,28,41,.94));
    border-bottom: 1px solid var(--line);
    padding: calc(8px + var(--safe-top)) 10px 8px;
    gap: 8px;
    z-index: 5;
  }

  .side-bottom {
    grid-row: 3;
    background: linear-gradient(180deg, rgba(21,28,41,.95), rgba(15,22,35,.98));
    border-top: 1px solid var(--line);
    padding: 8px 10px calc(10px + var(--safe-bottom));
    gap: 8px;
    z-index: 5;
  }

  .main-area {
    grid-row: 2;
    min-height: 0;
  }

  .logo {
    display: none;
  }

  .panel {
    background: none;
    border: none;
    border-radius: 0;
    padding: 0;
    gap: 6px;
  }

  .section-label {
    display: none;
  }

  .info-strip {
    grid-template-columns: 1.8fr .8fr .95fr .8fr;
    gap: 6px;
  }

  .compact-field {
    gap: 3px;
  }

  .compact-field span {
    font-size: 9px;
  }

  input[type="number"],
  input[type="text"],
  textarea {
    height: 36px;
    padding: 7px 8px;
    border-radius: 10px;
    font-size: 13px;
  }

  .mode-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }

  .mode-btn {
    min-height: 36px;
    font-size: 11px;
    padding: 6px 4px;
  }

  .group-grid {
    gap: 6px;
  }

  .group-btn {
    min-height: 34px;
    font-size: 13px;
  }

  .size-row {
    grid-template-columns: .75fr .75fr 1.1fr;
    gap: 6px;
  }

  .small-btn,
  .act-btn {
    min-height: 36px;
    font-size: 11px;
    padding: 7px 5px;
  }

  .status-msg {
    min-height: 14px;
    line-height: 14px;
    font-size: 11px;
    text-align: center;
  }

  .canvas-header {
    min-height: 32px;
    padding: 6px 10px;
  }

  .coord-info {
    display: none;
  }

  .validation-info {
    width: 100%;
    text-align: center;
    font-size: 11px;
  }

  .canvas-wrap {
    padding: 0;
  }

  .test-bar {
    padding: calc(8px + var(--safe-top)) 10px 8px;
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .test-title {
    font-size: 15px;
  }

  .test-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
    font-size: 11px;
  }

  .test-stats span {
    background: rgba(255,255,255,.03);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 7px 8px;
    text-align: center;
  }

  .test-actions {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
  }

  .test-btn {
    min-height: 36px;
    padding: 7px 4px;
    font-size: 10px;
  }

  .test-msg {
    min-height: 34px;
    padding: 7px 10px calc(8px + var(--safe-bottom));
    font-size: 13px;
  }

  .modal-bg {
    align-items: flex-end;
  }

  .modal {
    width: 100vw;
    max-width: none;
    border-radius: 18px 18px 0 0;
    padding: 16px 14px calc(16px + var(--safe-bottom));
    border-left: none;
    border-right: none;
    border-bottom: none;
  }
}

@media (max-width: 420px) {
  .info-strip {
    grid-template-columns: 1.55fr .8fr .9fr .8fr;
  }

  .mode-btn,
  .act-btn {
    font-size: 10px;
  }

  .test-actions {
    grid-template-columns: repeat(5, 1fr);
  }
}

@media (max-height: 760px) and (max-width: 820px) {
  .side-top {
    padding-top: calc(6px + var(--safe-top));
    padding-bottom: 6px;
  }

  .side-bottom {
    padding-top: 6px;
    gap: 6px;
  }

  .compact-field span {
    font-size: 8px;
  }

  input[type="number"],
  input[type="text"] {
    height: 34px;
    font-size: 12px;
  }

  .mode-btn,
  .group-btn,
  .act-btn,
  .small-btn {
    min-height: 32px;
  }

  .mode-btn,
  .act-btn,
  .test-btn {
    font-size: 10px;
  }
}