/* ====================  Territory Planner  ==================== */

#planner {
  --tp-grid-line: rgba(100, 116, 139, 0.28);
  --tp-map-bg: #eef2f7;
}

body[data-theme="dark"] #planner {
  --tp-grid-line: rgba(148, 163, 184, 0.22);
  --tp-map-bg: #1e293b;
}

/* ---------- Toolbar ---------- */

.tp-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.tp-tools,
.tp-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.tp-btn,
.tp-tool {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card-bg);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  transition: var(--transition);
}

.tp-btn:hover,
.tp-tool:hover {
  border-color: var(--primary-light);
  color: var(--primary);
}

.tp-tool.active {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow);
}

.tp-tool.active .tp-size {
  color: rgba(255, 255, 255, 0.8);
}

.tp-size {
  font-size: 0.75rem;
  color: var(--text-light);
}

.tp-btn.danger:hover {
  border-color: #ef4444;
  color: #ef4444;
}

.tp-num {
  width: 3.6rem;
  padding: 0.45rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.9rem;
}

.tp-controls label {
  font-size: 0.85rem;
  color: var(--text-light);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

/* ---------- Layout ---------- */

.tp-body {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
}

.tp-viewport {
  flex: 1 1 auto;
  overflow: auto;
  max-height: 70vh;
  padding: 12px;
  background: var(--tp-map-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.12);
}

.tp-viewport.erasing {
  cursor: crosshair;
}

.tp-grid {
  position: relative;
  background-color: transparent;
  background-image:
    linear-gradient(to right, var(--tp-grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--tp-grid-line) 1px, transparent 1px);
  background-size: var(--cell, 26px) var(--cell, 26px);
  border: 1px solid var(--tp-grid-line);
  touch-action: none;
}

/* ---------- Buildings ---------- */

.tp-item {
  position: absolute;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border-radius: 4px;
  border: 2px solid rgba(15, 23, 42, 0.55);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  cursor: grab;
  overflow: hidden;
  user-select: none;
}

.tp-item:active { cursor: grabbing; }

.tp-item.selected {
  outline: 3px solid #fbbf24;
  outline-offset: -3px;
  z-index: 5;
}

.tp-bearTrap { background: rgba(239, 68, 68, 0.85); }
.tp-hq       { background: rgba(139, 92, 246, 0.85); }
.tp-banner   { background: rgba(245, 158, 11, 0.9); }
.tp-city     { background: rgba(59, 130, 246, 0.85); }
.tp-city.globe {
  background: rgba(16, 185, 129, 0.95);
  border-color: #064e3b;
  box-shadow: 0 0 0 1px rgba(209, 250, 229, 0.55) inset, 0 1px 4px rgba(0, 0, 0, 0.4);
}

.tp-item-icon {
  font-size: clamp(9px, calc(var(--cell, 26px) * 0.62), 22px);
  line-height: 1;
  pointer-events: none;
}

.tp-banner .tp-item-icon { font-size: clamp(8px, calc(var(--cell, 26px) * 0.6), 16px); }

.tp-item-label {
  font-size: clamp(7px, calc(var(--cell, 26px) * 0.28), 12px);
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
  max-width: 100%;
  padding: 0 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

/* Blocked tiles: red with diagonal hatching, sitting under everything. */
.tp-void {
  position: absolute;
  z-index: 0;
  background-color: rgba(239, 68, 68, 0.28);
  background-image: repeating-linear-gradient(
      45deg,
      rgba(248, 113, 113, 0.9) 0,
      rgba(248, 113, 113, 0.9) 2px,
      transparent 2px,
      transparent 8px);
  box-shadow: inset 0 0 0 1px rgba(248, 113, 113, 0.45);
  pointer-events: none;
}

.tp-swatch-void {
  background-color: rgba(239, 68, 68, 0.35);
  background-image: repeating-linear-gradient(
      45deg,
      rgba(248, 113, 113, 0.95) 0,
      rgba(248, 113, 113, 0.95) 2px,
      transparent 2px,
      transparent 6px);
}

.tp-tool[data-tool="void"].active {
  background: #ef4444;
  border-color: #ef4444;
}

.tp-aura {
  position: absolute;
  z-index: 0;
  overflow: hidden;
  border-radius: 6px;
  border: 3px dashed rgba(52, 211, 153, 0.95);
  background: rgba(16, 185, 129, 0.28);
  box-shadow: 0 0 14px rgba(16, 185, 129, 0.45), inset 0 0 18px rgba(16, 185, 129, 0.3);
  pointer-events: none;
}

body[data-theme="dark"] .tp-aura {
  background: rgba(16, 185, 129, 0.34);
}

.tp-aura.selected {
  border-color: #fbbf24;
  background: rgba(16, 185, 129, 0.42);
  box-shadow: 0 0 18px rgba(251, 191, 36, 0.5), inset 0 0 18px rgba(16, 185, 129, 0.35);
}

.tp-aura-tag {
  position: absolute;
  top: 2px;
  left: 2px;
  padding: 1px 5px;
  border-radius: 999px;
  background: rgba(6, 78, 59, 0.9);
  color: #d1fae5;
  font-size: clamp(8px, calc(var(--cell, 26px) * 0.32), 12px);
  font-weight: 700;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
}

.tp-globe-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: clamp(13px, calc(var(--cell, 26px) * 0.58), 24px);
  height: clamp(13px, calc(var(--cell, 26px) * 0.58), 24px);
  border-radius: 50%;
  background: #0f172a;
  border: 2px solid #ffffff;
  color: #ffffff;
  font-size: clamp(8px, calc(var(--cell, 26px) * 0.36), 15px);
  font-weight: 800;
  line-height: 1;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}

.tp-ghost {
  position: absolute;
  display: none;
  align-items: center;
  justify-content: center;
  border: 2px dashed var(--ghost-color, #4f46e5);
  background: rgba(79, 70, 229, 0.22); /* fallback for browsers without color-mix */
  background: color-mix(in srgb, var(--ghost-color, #4f46e5) 25%, transparent);
  border-radius: 4px;
  font-size: 1rem;
  opacity: 0.85;
  pointer-events: none;
  z-index: 4;
}

.tp-ghost.invalid {
  border-color: #ef4444;
  background: rgba(239, 68, 68, 0.25);
}

/* ---------- Side panel ---------- */

.tp-side {
  flex: 0 0 230px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.tp-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem;
  box-shadow: var(--shadow-sm);
}

.tp-card h3 {
  margin: 0 0 0.6rem 0;
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-light);
}

.tp-legend-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
  font-size: 0.85rem;
}

.tp-legend-row .tp-count {
  margin-left: auto;
  font-weight: 700;
  color: var(--primary);
}

.tp-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(15, 23, 42, 0.4);
  flex: 0 0 auto;
}

.tp-selected-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}

.tp-field {
  display: block;
  font-size: 0.8rem;
  color: var(--text-light);
  margin-bottom: 0.6rem;
}

.tp-check {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.85rem;
  margin-bottom: 0.6rem;
  cursor: pointer;
}

.tp-check input { cursor: pointer; }

.tp-field select {
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.45rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--select-bg);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}

.tp-field input {
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.45rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.9rem;
}

.tp-dim {
  font-size: 0.78rem;
  color: var(--text-light);
}

.tp-hint {
  margin: 0;
  font-size: 0.82rem;
  color: var(--text-light);
  line-height: 1.5;
}

.tp-hint + .tp-hint { margin-top: 0.5rem; }

.tp-card .tp-btn { width: 100%; justify-content: center; margin-top: 0.5rem; }

/* ---------- Toast ---------- */

.tp-message {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%) translateY(1rem);
  background: #ef4444;
  color: #fff;
  padding: 0.6rem 1rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  font-size: 0.88rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 60;
}

.tp-message.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Responsive ---------- */

@media (max-width: 820px) {
  .tp-body { flex-direction: column; }
  .tp-side { flex: 1 1 auto; width: 100%; }
  .tp-viewport { max-height: 60vh; width: 100%; }
}
