:root {
  --bg: #101319;
  --panel: #181d27;
  --panel-alt: #0f141d;
  --text: #e8efff;
  --muted: #9ba9c2;
  --accent: #56d7ff;
  --danger: #ff4a4a;
  --warn: #ffbe38;
  --ok: #3fcf9e;
  --border: #2a3242;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 10%, #223049 0%, transparent 40%),
    radial-gradient(circle at 85% 80%, #1e2a42 0%, transparent 35%),
    var(--bg);
}

.app {
  display: grid;
  grid-template-columns: 340px 1fr 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "controls map viewer"
    "controls chunk viewer";
  gap: 14px;
  padding: 14px;
  min-height: 100vh;
}

section {
  background: linear-gradient(170deg, rgba(24, 29, 39, 0.96), rgba(15, 20, 29, 0.96));
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}

.controls { grid-area: controls; }
.map-panel { grid-area: map; }
.viewer-panel { grid-area: viewer; }
.chunk-panel { grid-area: chunk; overflow: auto; }

h1, h2 {
  margin: 0 0 8px 0;
  font-weight: 700;
}

h1 {
  font-size: 1.2rem;
}

h2 {
  font-size: 1rem;
}

.subtitle {
  margin: 0 0 10px 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.35;
}

.file-picker {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}

.access-key {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}

input[type="file"],
input[type="number"] {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #0d1118;
  color: var(--text);
  padding: 8px;
}

.coord-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin: 10px 0;
}

.coord-grid label {
  font-size: 0.82rem;
  color: var(--muted);
}

button {
  width: 100%;
  border: none;
  border-radius: 8px;
  padding: 10px;
  color: #061018;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), #8cedff);
  cursor: pointer;
}

button[disabled] {
  opacity: 0.6;
  cursor: not-allowed;
}

.status {
  margin-top: 10px;
  min-height: 20px;
  color: var(--muted);
  font-size: 0.86rem;
  white-space: pre-wrap;
}

#mapCanvas {
  display: block;
  width: 100%;
  max-height: 62vh;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #0b0f15;
  image-rendering: pixelated;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--muted);
}

.swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 5px;
}

.decode-error { background: #572429; }
.invalid { background: var(--danger); }
.entity { background: var(--ok); }
.empty { background: #4b586e; }
.suspect { background: var(--warn); }
.selected { background: #ff7a00; }

#viewer3d {
  width: 100%;
  min-height: 640px;
  height: calc(100vh - 80px);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #0c1117;
}

.chunk-meta {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.45;
  margin-bottom: 10px;
}

.entity-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.fix-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.fix-btn {
  width: auto;
  background: #24344f;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 10px;
}

.fix-results {
  border: 1px solid var(--border);
  background: #0b111a;
  border-radius: 8px;
  padding: 8px;
  margin-bottom: 8px;
  font-size: 0.8rem;
  color: var(--muted);
}

.map-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  flex-wrap: wrap;
}

.map-toggle-btn {
  width: auto;
  background: #1e2b42;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 12px;
  font-size: 0.82rem;
}

.map-toggle-btn.active {
  background: #2a4a2a;
  border-color: var(--ok);
  color: var(--ok);
}

.selection-count {
  font-size: 0.82rem;
  color: #ff7a00;
  font-weight: 600;
}

.delete-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  margin-bottom: 6px;
}

.delete-btn {
  width: auto;
  background: #4a1f1f;
  color: var(--text);
  border: 1px solid #7a3030;
  padding: 8px 12px;
}

.delete-btn:not([disabled]):hover {
  background: #5e2a2a;
}

.delete-results {
  font-size: 0.8rem;
  color: var(--muted);
  min-height: 1em;
}

.download-link {
  color: #8cedff;
  text-decoration: none;
}

.download-link:hover {
  text-decoration: underline;
}

.debug-btn {
  width: auto;
  margin-bottom: 8px;
  background: #1e2b42;
  color: var(--text);
  border: 1px solid var(--border);
}

.debug-output {
  margin: 0 0 10px 0;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #0b111a;
  color: #d9e4ff;
  font-size: 0.76rem;
  line-height: 1.35;
  max-height: 260px;
  overflow: auto;
  white-space: pre-wrap;
}

.entity-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  background: #101722;
}

.entity-item.invalid {
  border-color: #7a3030;
  background: #231317;
}

.entity-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--muted);
}

.entity-title {
  color: var(--text);
  font-weight: 600;
  margin-bottom: 4px;
  font-size: 0.84rem;
}

.jump-btn {
  border: 1px solid var(--border);
  background: #152033;
  color: var(--text);
  border-radius: 6px;
  padding: 4px 8px;
  width: auto;
  font-size: 0.76rem;
}

@media (max-width: 1280px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto;
    grid-template-areas:
      "controls"
      "map"
      "viewer"
      "chunk";
  }

  #viewer3d {
    height: 460px;
    min-height: 460px;
  }
}
