/* HabSociety city map. Dark chrome around a light map canvas. */

:root {
  --bg: #12161b;
  --surface: #1a2027;
  --surface-2: #222a33;
  --border: #2e3843;
  --text: #e8ecef;
  --text-dim: #96a2ad;
  --accent: #2dd9c6;
  --accent-deep: #17897c;
  --danger: #e06c6c;
  --map-ground: #dfe6d8;
  --map-road: #b9bfc7;
  --map-park: #a8d5a2;
  --map-divider: #4a5568;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
}

button { font: inherit; cursor: pointer; }
input, select { font: inherit; }

/* ---------- chrome ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-mark { color: var(--accent); font-size: 18px; }
.brand-name { font-weight: 700; letter-spacing: 0.02em; }
.brand-sub { color: var(--text-dim); font-size: 13px; }

.topbar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

.save-state { color: #e5b25d; font-size: 13px; }
.user-chip { color: var(--accent); font-size: 14px; }

.btn {
  border: 1px solid var(--accent-deep);
  background: var(--accent-deep);
  color: #fff;
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 14px;
}
.btn:hover { filter: brightness(1.1); }
.btn:active { transform: translateY(1px); }

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-dim);
}
.btn-ghost:hover { color: var(--text); border-color: var(--text-dim); }

.btn-danger { background: var(--danger); border-color: var(--danger); }

.icon-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  font-size: 16px;
}
.icon-btn:hover { color: var(--text); }
.icon-btn[aria-pressed="true"] { color: var(--accent); border-color: var(--accent-deep); }

/* ---------- layout ---------- */

.layout {
  display: flex;
  height: calc(100% - 56px);
}

.sidebar {
  width: 260px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.search {
  margin: 12px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.search:focus { border-color: var(--accent-deep); }

.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 12px 10px;
}
.chip {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 5px;
}
.chip:hover { color: var(--text); }
.chip[aria-pressed="true"] { background: var(--accent-deep); border-color: var(--accent-deep); color: #fff; }
.chip-dot { width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0; }

.saved-views {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 12px 10px;
}
.view-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 4px 6px 4px 10px;
  font-size: 12px;
}
.view-chip:hover { color: var(--text); }
.view-chip .view-name {
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  padding: 0;
  cursor: pointer;
}
.view-chip .view-name:hover { text-decoration: underline; }
.view-chip .view-remove {
  border: none;
  background: none;
  color: var(--text-dim);
  padding: 0 2px;
  font-size: 12px;
  line-height: 1;
}
.view-chip .view-remove:hover { color: var(--danger); }
.view-add {
  border: 1px dashed var(--border);
  background: none;
  color: var(--text-dim);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
}
.view-add:hover { color: var(--text); border-color: var(--text-dim); }

.loc-list { flex: 1; overflow-y: auto; padding: 0 8px 12px; }

.district {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  padding: 0;
  margin: 10px 6px 4px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.district:hover { color: var(--accent); }

.loc-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  padding: 7px 10px;
  border: none;
  background: none;
  color: var(--text);
  border-radius: 8px;
  font-size: 14px;
}
.loc-item:hover { background: var(--surface-2); }
.loc-item.active { background: var(--surface-2); outline: 1px solid var(--accent-deep); }

.loc-dot {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex-shrink: 0;
}

.sidebar-foot {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12px;
  min-height: 20px;
}

/* ---------- map ---------- */

.map-wrap { position: relative; flex: 1; overflow: hidden; }

#mapSvg {
  width: 100%;
  height: 100%;
  display: block;
  background: var(--map-ground);
  touch-action: none;
  cursor: grab;
  outline: none;
  transform-origin: 50% 50%;
  transition: transform 0.35s ease;
}
#mapSvg.panning { cursor: grabbing; }
#mapSvg.editing { cursor: crosshair; }

/* No `perspective` on purpose: an orthographic (perspective-free) 3D
   rotation keeps every edge at true scale, which is what makes it read
   as a flat isometric diamond instead of a distorted, foreshortened
   trapezoid. --iso-scale is computed in JS to fit the tilted, rotated
   shape back inside the (now much taller) bounding box it would
   otherwise overflow. */
.map-wrap.iso-view #mapSvg {
  transform: rotateX(55deg) rotateZ(45deg) scale(var(--iso-scale, 0.6));
  cursor: default;
  box-shadow: 0 40px 80px rgba(0, 0, 0, 0.45);
}
.map-wrap.iso-view .map-controls,
.map-wrap.iso-view .compass,
.map-wrap.iso-view .edit-toolbar,
.map-wrap.iso-view .detail-card,
.map-wrap.iso-view .legend-panel { transform: none; }

.map-controls {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.compass {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--accent);
  font-weight: 700;
  pointer-events: none;
}

.legend-panel {
  position: absolute;
  right: 14px;
  bottom: 168px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 12px;
  color: var(--text-dim);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}
.legend-row { display: flex; align-items: center; gap: 8px; padding: 3px 0; white-space: nowrap; }
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }
.legend-swatch.line-swatch { width: 16px; height: 3px; border-radius: 0; }

.edit-toolbar {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--text-dim);
  max-width: calc(100% - 28px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.tool-switch {
  display: flex;
  gap: 2px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
  flex-shrink: 0;
}

.tool-btn {
  border: none;
  background: none;
  color: var(--text-dim);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 13px;
  white-space: nowrap;
}
.tool-btn:hover { color: var(--text); }
.tool-btn[aria-checked="true"] { background: var(--accent-deep); color: #fff; }
.tool-btn:disabled { opacity: 0.35; cursor: default; }
.tool-btn:disabled:hover { color: var(--text-dim); }

.add-type { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.add-type select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
}

/* ---------- detail / edit card ---------- */

.detail-card {
  position: absolute;
  left: 14px;
  bottom: 14px;
  width: 300px;
  max-width: calc(100% - 28px);
  max-height: calc(100% - 28px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

.detail-card h3 { margin: 0 0 2px; font-size: 17px; }
.detail-card .loc-district { color: var(--text-dim); font-size: 13px; margin: 0 0 8px; }
.detail-card .loc-notes { font-size: 14px; margin: 0; white-space: pre-wrap; }

.detail-card label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 10px;
}

.detail-card input[type="text"],
.detail-card textarea,
.detail-card select {
  width: 100%;
  margin-top: 4px;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.detail-card input:focus, .detail-card textarea:focus { border-color: var(--accent-deep); }
.detail-card textarea { resize: vertical; min-height: 56px; }

.geom-row { display: flex; gap: 8px; }
.geom-row label { flex: 1; }
.geom-row input { width: 100%; }

.swatches { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; align-items: center; }
.swatch {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 2px solid transparent;
  padding: 0;
}
.swatch.active { border-color: var(--text); }
.swatch-custom {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 2px solid var(--border);
  padding: 0;
  background: none;
}

.icon-picker { display: flex; gap: 4px; margin-top: 6px; flex-wrap: wrap; }
.icon-opt {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  border: 2px solid transparent;
  background: var(--bg);
  font-size: 15px;
  padding: 0;
}
.icon-opt.active { border-color: var(--accent-deep); background: var(--surface-2); }
.icon-opt:hover { background: var(--surface-2); }

.image-field { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.image-preview {
  width: 100%;
  max-height: 120px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.image-field input[type="text"] { margin-top: 0; }
.image-field input[type="file"] {
  font-size: 12px;
  color: var(--text-dim);
}
.image-remove { align-self: flex-start; padding: 4px 10px; font-size: 12px; }

.card-image {
  display: block;
  width: 100%;
  max-height: 160px;
  object-fit: cover;
  border-radius: 8px;
  margin: 4px 0 8px;
  border: 1px solid var(--border);
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px;
  margin-top: 14px;
}

.card-close {
  position: absolute;
  top: 8px;
  right: 8px;
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 16px;
}
.card-close:hover { color: var(--text); }

/* ---------- modal ---------- */

.modal {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  padding: 22px;
  width: 320px;
  max-width: calc(100vw - 32px);
}
.modal::backdrop { background: rgba(0, 0, 0, 0.55); }
.modal h2 { margin: 0 0 12px; font-size: 18px; }
.modal label { display: block; font-size: 13px; color: var(--text-dim); margin-top: 10px; }
.modal input {
  width: 100%;
  margin-top: 4px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.modal input:focus { border-color: var(--accent-deep); }
.form-error { color: var(--danger); font-size: 13px; margin: 10px 0 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.modal-hint { font-size: 12px; color: var(--text-dim); margin: -4px 0 6px; }

.history-list { max-height: 320px; overflow-y: auto; margin-top: 8px; }
.history-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.history-row:last-child { border-bottom: none; }
.history-meta { color: var(--text); }
.history-meta span { display: block; color: var(--text-dim); font-size: 12px; }
.history-empty { color: var(--text-dim); font-size: 13px; padding: 8px 0; }

/* ---------- map svg elements ---------- */

.loc-label {
  font-family: system-ui, sans-serif;
  fill: #2c3438;
  pointer-events: none;
  user-select: none;
}
.loc-label.on-road { fill: #5b6470; }
.loc-icon { pointer-events: none; user-select: none; }
.loc-shape { cursor: pointer; transition: stroke 0.12s ease, stroke-width 0.12s ease; }
.loc-shape.selected { stroke: #17897c !important; stroke-width: 3 !important; }
.draft-rect { fill: rgba(45, 217, 198, 0.3); stroke: #17897c; stroke-dasharray: 4 3; }
.draft-line { stroke: #17897c; stroke-width: 3; stroke-dasharray: 4 3; stroke-linecap: round; }
.draft-arrow { fill: rgba(45, 217, 198, 0.35); stroke: #17897c; stroke-dasharray: 4 3; }
.marquee-rect { fill: rgba(45, 217, 198, 0.15); stroke: #17897c; stroke-dasharray: 4 3; }

.resize-handle { fill: #fff; stroke: #17897c; stroke-width: 1.5; }
.resize-handle[data-handle="nw"], .resize-handle[data-handle="se"] { cursor: nwse-resize; }
.resize-handle[data-handle="ne"], .resize-handle[data-handle="sw"] { cursor: nesw-resize; }

/* ---------- mobile ---------- */

.sidebar-toggle { display: none; }

@media (max-width: 760px) {
  .sidebar-toggle { display: grid; }
  .brand-sub { display: none; }
  .sidebar {
    position: absolute;
    z-index: 20;
    top: 56px;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 8px 0 30px rgba(0, 0, 0, 0.4);
  }
  .sidebar.open { transform: translateX(0); }
  .detail-card { left: 8px; right: 8px; width: auto; bottom: 8px; }
  .edit-toolbar { top: 8px; font-size: 12px; gap: 8px; }
  .edit-hint { display: none; }
}

/* ---------- toasts ---------- */

.toast-container {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.toast-error { border-color: var(--danger); }
.toast.toast-success { border-color: var(--accent-deep); }

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0.01ms !important; }
}
