:root {
  color-scheme: dark;
  --chrome-0: #14171a;
  --chrome-1: #1d2226;
  --chrome-2: #292f34;
  --line: #3b444b;
  --text: #edf2f4;
  --muted: #95a2aa;
  --cyan: #54c7d8;
  --yellow: #f0b84a;
  --green: #56c58a;
  --red: #e46d67;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--chrome-0);
  color: var(--text);
  font: 13px/1.35 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--chrome-2);
  color: var(--text);
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: #68747c;
  background: #343b41;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 1px;
}

button:disabled {
  cursor: default;
  opacity: 0.35;
}

.topbar {
  height: 54px;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
  background: var(--chrome-1);
}

.brand {
  min-width: 190px;
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--text);
  text-decoration: none;
}

.brand-mark {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 2px solid var(--yellow);
  color: var(--yellow);
  font: 800 11px/1 ui-monospace, monospace;
}

.brand strong,
.brand small {
  display: block;
  letter-spacing: 0;
}

.brand strong {
  font-size: 14px;
}

.brand small {
  margin-top: 1px;
  color: var(--muted);
  font: 700 9px/1 ui-monospace, monospace;
}

.map-name {
  min-width: 180px;
  max-width: 280px;
  flex: 1 1 220px;
}

.map-name span {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

input {
  width: 100%;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #111416;
  color: var(--text);
  padding: 0 9px;
}

select {
  width: 100%;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #111416;
  color: var(--text);
  padding: 0 8px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.top-actions button {
  padding: 0 11px;
}

.top-actions .icon-btn {
  width: 34px;
  padding: 0;
  font-size: 20px;
}

.top-actions .primary {
  border-color: #448f9c;
  background: #276c77;
}

.top-actions .play {
  border-color: #9d762e;
  background: #79571c;
  color: #fff4da;
}

.editor-shell {
  height: calc(100% - 82px);
  display: grid;
  grid-template-columns: 238px minmax(0, 1fr) 292px;
  grid-template-rows: minmax(0, 1fr);
}

.sidebar {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  /* 도구 막대와 탭은 붙박이고, 팔레트만 안에서 구른다 — 긴 목록을 훑어도 탭이 안 사라진다. */
  grid-template-rows: auto auto minmax(0, 1fr);
  border-right: 1px solid var(--line);
  background: var(--chrome-1);
}

.sidebar-panels {
  min-height: 0;
  overflow: auto;
}

/* 팔레트가 없는 도구만 담는 막대. 무엇을 놓을지는 아래 탭이 맡는다. */
.tool-tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  padding: 10px 12px;
  gap: 0;
}

.tool-tabs .tool-button {
  min-height: 30px;
  padding: 0 2px;
  /* 다섯 칸이 좁아 기본 크기로는 '지우개'가 두 줄로 접힌다. */
  font-size: 11px;
  white-space: nowrap;
}

/* 탭 = 무엇을 놓을지. 지금 무엇을 찍는 중인지 한 줄로 보인다. */
.sidebar-tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--chrome-2);
}

.sidebar-tab {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 34px;
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.sidebar-tab:last-child { border-right: 0; }

.sidebar-tab em {
  font: 9px/1 ui-monospace, monospace;
  font-style: normal;
  opacity: 0.55;
}

.sidebar-tab:hover:not([aria-selected="true"]) {
  background: #343b41;
  color: var(--text);
}

.sidebar-tab[aria-selected="true"] {
  background: var(--chrome-1);
  color: #ffe5a8;
  /* 선택된 탭만 아래로 열려 있다 — 밑줄이 곧 지금 보고 있는 패널이다. */
  box-shadow: inset 0 -2px 0 var(--yellow);
}

/* 한 패널 안에서 묶음을 나눌 때 쓰는 얇은 구분선 (맵 탭의 크기 ↔ 배경). */
.panel-divider {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.panel-section {
  padding: 15px 12px;
  border-bottom: 1px solid var(--line);
}

.panel-section h2 {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  color: #dce3e7;
}

.section-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.section-title span {
  color: var(--muted);
  font: 10px/1 ui-monospace, monospace;
}

.size-controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: end;
  gap: 6px;
}

.size-controls label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 10px;
}

.size-controls input {
  padding: 0 6px;
}

.size-controls > span {
  padding-bottom: 8px;
  color: var(--muted);
}

.size-controls button {
  grid-column: 1 / -1;
  margin-top: 2px;
}

.tool-button {
  border-radius: 0;
  margin-left: -1px;
}

.tool-button:first-child {
  margin-left: 0;
  border-radius: 4px 0 0 4px;
}

.tool-button:last-child {
  border-radius: 0 4px 4px 0;
}

.tool-button.active {
  z-index: 1;
  border-color: var(--yellow);
  background: #544323;
  color: #ffe5a8;
}

.palette-section {
  padding-bottom: 24px;
}

.block-palette {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.palette-group {
  grid-column: 1 / -1;
  min-width: 0;
}

.palette-group + .palette-group {
  margin-top: 4px;
}

.palette-group > summary {
  min-height: 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  cursor: pointer;
  color: #cbd4d9;
  font-size: 11px;
  user-select: none;
}

.palette-group > summary small {
  color: var(--muted);
  font: 9px/1 ui-monospace, monospace;
}

.palette-group > .block-palette {
  padding: 3px 0 9px;
}

.palette-group-preview {
  width: 100%;
  aspect-ratio: 2 / 1;
  display: block;
  margin: 2px 0 8px;
  border: 1px solid #34434a;
  object-fit: cover;
  image-rendering: pixelated;
}

/* 미리보기 그림 자체가 "세트 통째로 놓기" 버튼이다. */
.palette-group-stamp {
  position: relative;
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  color: inherit;
}

.palette-group-stamp > .palette-group-preview {
  margin: 2px 0 0;
  transition: border-color 0.12s ease;
}

.palette-group-stamp > span {
  display: block;
  padding: 3px 0 8px;
  color: var(--muted);
  font-size: 10px;
  text-align: center;
  letter-spacing: 0.02em;
}

.palette-group-stamp:hover > .palette-group-preview,
.palette-group-stamp:focus-visible > .palette-group-preview {
  border-color: #70e4ee;
}

.palette-group-stamp:hover > span,
.palette-group-stamp:focus-visible > span {
  color: #cbd4d9;
}

.palette-group-stamp.selected > .palette-group-preview {
  border-color: #70e4ee;
  box-shadow: 0 0 0 1px #70e4ee inset;
}

.palette-group-stamp.selected > span {
  color: #70e4ee;
}

.palette-item {
  min-width: 0;
  /* 이름이 두 줄로 접히는 항목(보스처럼 긴 이름)이 있어 높이를 고정하지 않는다 —
     고정하면 아래 줄(HP 표기)이 잘려 나간다. */
  min-height: 64px;
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  align-items: center;
  gap: 7px;
  padding: 7px;
  text-align: left;
  overflow: hidden;
}

.palette-item img {
  width: 26px;
  height: 26px;
  object-fit: contain;
  image-rendering: pixelated;
}

.palette-item span {
  min-width: 0;
  overflow-wrap: anywhere;
  color: #cbd4d9;
  font-size: 10px;
}

.palette-item.selected {
  border-color: var(--yellow);
  background: #4c3b1f;
  box-shadow: inset 0 0 0 1px #a67d2d;
}

.decoration-palette .palette-item img {
  max-width: 26px;
  max-height: 38px;
}

.item-palette .palette-item {
  min-height: 58px;
}

.item-swatch {
  display: grid;
  width: 26px;
  height: 26px;
  place-items: center;
  color: var(--item-color, #8fd3ff);
  font: 700 13px/1 ui-monospace, monospace;
  transform: rotate(45deg);
  border: 1px solid currentColor;
  background: #08131c;
  box-shadow: 0 0 8px color-mix(in srgb, currentColor 42%, transparent);
}

.item-swatch > span {
  color: inherit;
  font: inherit;
  transform: rotate(-45deg);
}

.item-palette .palette-item strong {
  display: block;
  color: #e7eef2;
  font: 700 10px/1.3 ui-monospace, monospace;
}

.item-palette .palette-item em {
  display: block;
  color: var(--muted);
  font: 9px/1.4 system-ui, sans-serif;
  font-style: normal;
}

.unit-palette .enemy-swatch {
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 색 네모로 그리는 종류 */
.unit-palette .enemy-swatch i {
  display: block;
  border: 1px solid rgba(6, 14, 22, 0.85);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.18);
}

/* 스프라이트가 붙은 종류 — 시트 첫 프레임을 캔버스에 그려 둔다. */
.unit-palette .enemy-swatch canvas,
.unit-palette .enemy-swatch img {
  display: block;
  image-rendering: pixelated;
  object-fit: contain;
}

.unit-palette .palette-item {
  /* 보스 이름은 세 줄까지 접힌다 — 높이를 고정하면 아래 HP 줄이 잘려 나간다. */
  min-height: 56px;
}

.unit-palette .palette-item strong {
  display: block;
  color: #e7eef2;
  font: 700 11px/1.3 ui-monospace, monospace;
}

.unit-palette .palette-item em {
  display: block;
  color: var(--muted);
  font: 9px/1.4 system-ui, sans-serif;
  font-style: normal;
}

.palette-note {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.5;
}

.palette-note strong {
  color: #cbd4d9;
  font-weight: 600;
}

.environment-palette {
  display: grid;
  gap: 7px;
}

.environment-item {
  position: relative;
  min-width: 0;
  height: 72px;
  padding: 0;
  overflow: hidden;
  text-align: left;
}

.environment-item img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  image-rendering: pixelated;
  opacity: 0.72;
}

.environment-item span {
  position: absolute;
  left: 7px;
  bottom: 6px;
  padding: 3px 5px;
  border: 1px solid rgba(148, 190, 191, 0.35);
  border-radius: 3px;
  background: rgba(5, 16, 20, 0.78);
  color: #e2f0ec;
  font-size: 10px;
}

.environment-item.selected {
  border-color: #72c6a8;
  box-shadow: inset 0 0 0 1px #72c6a8;
}

.environment-item.selected img {
  opacity: 1;
}

.viewport {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #0d1114;
}

#editor-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

#editor-canvas[data-tool="pan"] {
  cursor: grab;
}

#editor-canvas[data-tool="zone"] {
  cursor: cell;
}

#editor-canvas[data-tool="select"] {
  cursor: crosshair;
}

#editor-canvas.pasting {
  cursor: copy;
}

#editor-canvas.dragging {
  cursor: grabbing;
}

#editor-canvas[data-tool="select"].dragging {
  cursor: crosshair;
}

.zoom-readout {
  position: absolute;
  right: 10px;
  bottom: 10px;
  min-width: 52px;
  padding: 5px 7px;
  border: 1px solid rgba(255,255,255,0.16);
  border-radius: 4px;
  background: rgba(16,20,23,0.82);
  color: #c8d1d6;
  text-align: center;
  font: 10px/1 ui-monospace, monospace;
  pointer-events: none;
}

.empty-state {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 8px 10px;
  border: 1px solid var(--line);
  background: rgba(20,23,26,0.9);
  color: var(--muted);
  pointer-events: none;
}

.statusbar {
  height: 28px;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 12px;
  border-top: 1px solid var(--line);
  background: var(--chrome-1);
  color: var(--muted);
  font: 10px/1 ui-monospace, monospace;
}

#save-status[data-state="dirty"] {
  color: var(--yellow);
}

#save-status[data-state="saved"] {
  color: var(--green);
}

#save-status[data-state="error"] {
  color: var(--red);
}

#cursor-status {
  margin-left: auto;
}

.logic-panel {
  min-width: 0;
  min-height: 0;
  overflow: auto;
  border-left: 1px solid var(--line);
  background: var(--chrome-1);
}

.logic-section {
  padding: 14px 12px;
  border-bottom: 1px solid var(--line);
}

.logic-section h2 {
  margin: 0;
  font-size: 12px;
}

.compact-button {
  min-height: 26px;
  padding: 0 8px;
  border-color: #448f9c;
  color: #aeeaf2;
}

.entity-list {
  display: grid;
  gap: 5px;
  margin-top: 10px;
}

.trigger-phase-tabs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
  margin-top: 10px;
}

.trigger-phase-tab {
  min-height: 26px;
  padding: 0 5px;
  border-color: #334f58;
  background: #17262b;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
}

.trigger-phase-tab[aria-selected="true"] {
  border-color: var(--cyan);
  background: #213f45;
  color: #aeeaf2;
}

.entity-row {
  min-height: 38px;
  display: grid;
  grid-template-columns: 10px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  text-align: left;
}

.entity-row .entity-color {
  width: 8px;
  height: 24px;
}

.entity-row strong,
.entity-row small {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entity-row strong {
  font-size: 11px;
}

.entity-row small {
  margin-top: 2px;
  color: var(--muted);
  font: 9px/1 ui-monospace, monospace;
}

.entity-row > span:last-child {
  color: var(--muted);
  font-size: 10px;
}

.entity-row.selected {
  border-color: var(--cyan);
  background: #213f45;
}

.panel-empty {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 11px;
}

.inspector {
  display: grid;
  gap: 10px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.inspector[hidden] {
  display: none;
}

.field-label,
.coord-grid label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 10px;
}

.field-label[hidden] {
  display: none;
}

.coord-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}

.inline-field {
  min-height: 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--muted);
  font-size: 10px;
}

.inline-field input[type="color"] {
  width: 44px;
  padding: 3px;
}

.danger-button {
  border-color: #824b48;
  background: #4e2928;
  color: #ffc1bd;
}

.trigger-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.enabled-toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
  font-size: 11px;
}

.section-toggle {
  margin-top: 10px;
}

.enabled-toggle input {
  width: 16px;
  height: 16px;
  accent-color: var(--cyan);
}

.zone-functions {
  grid-template-columns: 1fr 1fr;
}

fieldset {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 4px;
}

legend {
  padding: 0 5px;
  color: var(--yellow);
  font-size: 10px;
  font-weight: 700;
}

.conditions-fieldset {
  gap: 10px;
}

.conditions-list {
  display: grid;
  gap: 0;
}

.condition-editor {
  display: grid;
  gap: 8px;
  padding-left: 9px;
  border-left: 2px solid #448f9c;
}

.condition-heading {
  min-height: 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.condition-heading strong {
  color: var(--text);
  font-size: 10px;
}

.condition-remove {
  width: 24px;
  height: 24px;
  padding: 0;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  font-size: 17px;
  line-height: 1;
}

.condition-remove:hover:not(:disabled) {
  border-color: #824b48;
  color: #ffc1bd;
}

.condition-join {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 42px;
}

.condition-join::before {
  position: absolute;
  inset: 0 50%;
  width: 1px;
  background: var(--line);
  content: "";
}

.condition-join select {
  position: relative;
  width: 72px;
  min-height: 26px;
  padding: 3px 22px 3px 8px;
  border-color: #448f9c;
  background-color: var(--chrome-1);
  color: #aeeaf2;
  font-weight: 700;
  text-align: center;
}

.add-condition-button {
  width: 100%;
}

.unit-input {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 24px;
  align-items: center;
}

.action-delay-input {
  grid-template-columns: minmax(0, 1fr) 36px;
}

.unit-input span {
  color: var(--muted);
  text-align: right;
}

@media (max-width: 980px) {
  .brand {
    min-width: auto;
  }

  .brand > span:last-child,
  .top-actions #fit-btn,
  .top-actions #export-btn,
  .top-actions #import-btn {
    display: none;
  }

  .editor-shell {
    grid-template-columns: 190px minmax(280px, 1fr) 260px;
  }

  .block-palette {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 700px) {
  .topbar {
    gap: 6px;
    padding: 0 6px;
  }

  .map-name {
    display: none;
  }

  .top-actions {
    margin-left: auto;
    gap: 4px;
  }

  .top-actions button {
    padding: 0 7px;
  }

  .top-actions .icon-btn {
    width: 32px;
  }

  .editor-shell {
    min-width: 720px;
    grid-template-columns: 174px minmax(280px, 1fr) 260px;
  }

  .sidebar .panel-section {
    padding-left: 10px;
    padding-right: 10px;
  }
}
