:root {
  --panel-width: 340px;
  --bg: #16181d;
  --panel: #1e2128;
  --panel-2: #262a33;
  --text: #e8eaed;
  --muted: #9aa0a6;
  --accent: #4c8dff;
  --accent-2: #3ddc84;
  --danger: #ff6b6b;
  --border: #333742;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

#map {
  position: absolute;
  inset: 0 var(--panel-width) 0 0;
  background: #0d0f13;
}

/* --- side panel --- */
#panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--panel-width);
  background: var(--panel);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px;
}

#panel h1 { margin: 0; font-size: 20px; }
#panel h2 { margin: 4px 0 10px; font-size: 15px; }
#panel-sub { margin: 6px 0 0; color: var(--muted); font-size: 13px; }

.view { margin-top: 16px; }

fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 0 0 12px;
  padding: 10px 12px 12px;
  background: var(--panel-2);
}

legend { padding: 0 6px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }

label {
  display: block;
  margin: 8px 0 0;
  font-size: 13px;
  color: var(--text);
}

label.check { display: flex; align-items: center; gap: 8px; }

input[type="number"], input[type="text"], select, input[type="password"] {
  width: 100%;
  margin-top: 4px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

input:focus, select:focus { outline: 1px solid var(--accent); }

.color-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

label.color {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

input[type="color"] {
  width: 42px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: none;
  cursor: pointer;
}

button, .button {
  display: inline-block;
  width: 100%;
  margin-top: 10px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}

button.primary, .button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

button.primary:hover { filter: brightness(1.08); }
button:disabled { opacity: .55; cursor: default; }
button.ghost, .button.ghost { background: transparent; }

.error { color: var(--danger); font-size: 13px; margin: 8px 0 0; }
.stage { margin: 8px 0; color: var(--accent); font-weight: 600; }
.meta { color: var(--muted); font-size: 12px; margin: 6px 0; }

.bar {
  height: 10px;
  border-radius: 6px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width .5s ease;
}

.bar-fill.indeterminate {
  width: 35% !important;
  animation: slide 1.4s ease-in-out infinite;
}

@keyframes slide {
  0% { margin-left: -35%; }
  100% { margin-left: 100%; }
}

.actions { display: flex; gap: 8px; }
.actions .button { margin-top: 12px; }

.warnings {
  margin: 8px 0 0;
  padding-left: 18px;
  color: #ffcf87;
  font-size: 12px;
}

#history { margin-top: 22px; border-top: 1px solid var(--border); padding-top: 10px; }

.history { list-style: none; margin: 0; padding: 0; }

.history li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-top: 6px;
  background: var(--panel-2);
  cursor: pointer;
  font-size: 13px;
}

.history li:hover { border-color: var(--accent); }

.history .status {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: .03em;
  background: var(--border);
  color: var(--muted);
}

.status.succeeded { background: rgba(61, 220, 132, .18); color: var(--accent-2); }
.status.failed, .status.interrupted { background: rgba(255, 107, 107, .16); color: var(--danger); }
.status.running, .status.queued { background: rgba(76, 141, 255, .16); color: var(--accent); }

/* --- grid size bar (always visible, above the views) --- */
#grid-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
}

#grid-bar label {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

#grid-bar select {
  flex: 1;
  width: auto;
  margin-top: 0;
}

#grid-bar button {
  width: auto;
  margin-top: 0;
  padding: 7px 12px;
  white-space: nowrap;
}

#grid-bar button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

#grid-origin {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 6px;
}

#grid-origin button {
  width: auto;
  margin-top: 0;
  padding: 2px 10px;
  font-size: 12px;
}

/* --- batch view: one row per selected cell --- */
.batch-item {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
}

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

.batch-label { font-weight: 600; font-size: 13px; }

.batch-status { font-size: 12px; font-weight: 600; color: var(--accent); }
.batch-status.done { color: var(--accent-2); }
.batch-status.bad { color: var(--danger); }

.bar.small { height: 6px; margin-top: 8px; }

.batch-item .error { margin-top: 6px; }
.batch-item .actions { margin-top: 4px; }
.batch-item .actions .button {
  margin-top: 10px;
  padding: 7px 6px;
  font-size: 13px;
}
.batch-item > .primary { margin-top: 8px; padding: 7px 12px; font-size: 13px; }

#groups { margin-top: 22px; border-top: 1px solid var(--border); padding-top: 10px; }

/* --- map affordances --- */
#zoom-hint {
  position: absolute;
  left: 50%;
  top: 18px;
  transform: translateX(-50%);
  background: rgba(22, 24, 29, .92);
  border: 1px solid var(--border);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 1000;
  pointer-events: none;
}

.tile-cell { cursor: pointer; }

/* Move mode: dragging shifts the lattice instead of panning. */
#move-hint {
  position: absolute;
  left: 50%;
  top: 18px;
  transform: translateX(-50%);
  background: rgba(22, 24, 29, .92);
  border: 1px solid var(--accent);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 1001;
  pointer-events: none;
}

.leaflet-container.moving { cursor: grab; }
.leaflet-container.moving .tile-cell { cursor: grab; }

.leaflet-container { font: inherit; }

/* --- token overlay --- */
#token-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 14, .82);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.token-box {
  width: 300px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
}

.token-box h2 { margin: 0 0 10px; font-size: 16px; }

@media (max-width: 760px) {
  :root { --panel-width: 100%; }
  #map { inset: 45vh 0 0 0; }
  #panel { top: auto; bottom: 0; height: 45vh; width: 100%; border-left: none; border-top: 1px solid var(--border); }
}

/* --- 3D viewer overlay --- */
#viewer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 14, .88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2500;
}

#viewer-box {
  width: min(960px, calc(100vw - 32px));
  height: min(78vh, 720px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#viewer-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

#viewer-title { font-weight: 600; }
#viewer-bar .stage { margin: 0; flex: 1; }
#viewer-bar .button { width: auto; margin: 0; padding: 6px 16px; }

#viewer-canvas {
  flex: 1;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}

#viewer-canvas:active { cursor: grabbing; }

#viewer-foot {
  padding: 6px 14px 10px;
  border-top: 1px solid var(--border);
}

#viewer-hint { margin: 4px 0; color: var(--muted); font-size: 12px; }
#viewer-foot .error { margin: 4px 0 0; }
