:root {
  --bg: #0b0e12;
  --panel: #10141a;
  --panel-2: #141920;
  --card: #171d25;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --text: #eceff4;
  --muted: #8b95a5;
  --muted-2: #636d7c;
  --accent: #fb923c;
  --accent-bg: rgba(251, 146, 60, 0.16);
  --green: #34d399;
  --blue: #38bdf8;
  --pink: #f472b6;
  --purple: #c084fc;
  --yellow: #fbbf24;
  --red: #f87171;
  --radius: 8px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", sans-serif;
  font-size: 12px;
  line-height: 1.4;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
}

::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.14);
  border-radius: 99px;
}
::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.24);
}

#app {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  background: var(--bg);
}

/* Top Header */
.topbar {
  height: 54px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand {
  min-width: 170px;
}
.tribe-name {
  font-size: 15px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.01em;
}
.tribe-sub {
  font-size: 11px;
  color: var(--muted);
}

.hdr-stats {
  display: flex;
  align-items: center;
  gap: 22px;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
}

.stat-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}
.stat-ico {
  font-size: 16px;
  line-height: 1;
}
.stat-val {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text);
}
.stat-sub {
  font-size: 10.5px;
  color: var(--muted);
}

.hdr-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.ai-meta {
  text-align: right;
}
.ai-status {
  font-size: 11px;
  color: var(--text);
}
.ai-tokens {
  font-size: 10.5px;
  color: var(--blue);
  font-variant-numeric: tabular-nums;
}

.icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  transition: background 0.15s;
}
.icon-btn:hover {
  background: rgba(255, 255, 255, 0.11);
}

/* Main 3-Column Workspace */
.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
}

/* Left Residents Rail */
.rail {
  width: 218px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

.rail-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 10px 6px;
}
.rail-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.rail-count {
  font-size: 10.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.rail-sort {
  margin-left: auto;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 2px 6px;
  font-size: 10.5px;
  color: var(--text);
  outline: none;
}

.rail-search-wrap {
  padding: 0 10px 8px;
}
.rail-search-wrap input {
  width: 100%;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 11px;
  color: var(--text);
  outline: none;
}
.rail-search-wrap input:focus {
  border-color: rgba(251, 146, 60, 0.5);
}

.rail-list {
  flex: 1;
  overflow-y: auto;
  padding: 2px 6px 10px;
}

.rail-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.12s;
}
.rail-row:hover {
  background: rgba(255, 255, 255, 0.05);
}
.rail-row.selected {
  background: var(--accent-bg);
  outline: 1px solid rgba(251, 146, 60, 0.45);
}
.rail-sex {
  font-size: 12px;
  font-weight: 700;
  line-height: 1.3;
  flex-shrink: 0;
}
.rail-sex.m { color: var(--blue); }
.rail-sex.f { color: var(--pink); }
.rail-info {
  min-width: 0;
  flex: 1;
}
.rail-name {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rail-meta {
  font-size: 10.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Center Viewport */
.viewport {
  position: relative;
  flex: 1;
  min-width: 0;
  background: #080b0f;
  overflow: hidden;
}

#main-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}
#main-canvas:active {
  cursor: grabbing;
}

.glass-pill {
  position: absolute;
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(16, 20, 26, 0.78);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.map-topbar {
  top: 10px;
  left: 10px;
  right: 10px;
  padding: 6px 10px;
  z-index: 4;
}

.map-title {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text);
}
.badge-green {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--green);
  background: rgba(52, 211, 153, 0.14);
  padding: 2px 7px;
  border-radius: 99px;
}
.map-hint {
  font-size: 10.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.zoom-group {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 4px;
}
.pill-btn {
  height: 24px;
  min-width: 24px;
  padding: 0 8px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text);
}
.pill-btn:hover {
  background: rgba(255, 255, 255, 0.14);
}
.pill-btn.active {
  background: var(--accent);
  color: #111;
  font-weight: 700;
}

.view-switcher {
  top: 52px;
  left: 50%;
  transform: translateX(-50%);
  padding: 4px;
  gap: 4px;
  border-radius: 99px;
  z-index: 4;
}
.mode-btn {
  padding: 4px 12px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}
.mode-btn:hover {
  color: var(--text);
}
.mode-btn.active {
  background: var(--accent);
  color: #111;
}

.hover-card {
  position: absolute;
  bottom: 14px;
  left: 14px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  max-width: 320px;
  background: rgba(16, 20, 26, 0.9);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}
.hover-card img {
  width: 40px;
  height: 40px;
  object-fit: contain;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.04);
}
.hover-card .hc-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
}
.hover-card .hc-sub {
  font-size: 10.5px;
  color: var(--muted);
}

.solar-caption, .planet-caption {
  position: absolute;
  bottom: 14px;
  left: 14px;
  z-index: 4;
  padding: 8px 12px;
  background: rgba(16, 20, 26, 0.8);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 11px;
  color: var(--text);
  pointer-events: none;
}

.solar-stars {
  position: absolute;
  top: 52px;
  left: 12px;
  z-index: 4;
  padding: 8px 10px;
  background: rgba(16, 20, 26, 0.8);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 10.5px;
  color: var(--muted);
  max-width: 220px;
}

/* Right Inspector */
.inspector {
  width: 320px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--border);
}

.insp-tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 8px 8px 0;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tab-btn {
  padding: 7px 8px;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tab-btn:hover {
  color: var(--text);
}
.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.insp-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.era-banner {
  position: relative;
  height: 78px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #18202b;
}
.era-banner img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.era-banner-grad {
  position: absolute;
  inset: 0;
  background: linear-gradient(to top, rgba(11, 14, 18, 0.92), rgba(11, 14, 18, 0.1));
}
.era-banner-label {
  position: absolute;
  left: 10px;
  bottom: 8px;
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
}

.sec-hdr {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 5px;
}

.card-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
}

.leader-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.portrait {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  object-fit: cover;
  background: #1c2430;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.leader-name {
  font-size: 12.5px;
  font-weight: 700;
}
.policy-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 6px;
  font-size: 11px;
}
.policy-focus {
  color: var(--accent);
  font-weight: 600;
}
.policy-src {
  color: var(--muted);
  font-size: 10px;
}
.quote {
  margin-top: 5px;
  font-size: 11px;
  font-style: italic;
  color: #cfd6e2;
}

.spark-wrap {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px;
}

.kv-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 2.5px 0;
  font-size: 11.5px;
}
.kv-k {
  color: var(--muted);
}
.kv-v {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
}
.kv-detail {
  font-size: 10px;
  color: var(--muted);
  margin-right: 6px;
  font-weight: 400;
}

.job-bar-row {
  display: grid;
  grid-template-columns: 78px 1fr 28px;
  align-items: center;
  gap: 8px;
  padding: 2.5px 0;
  font-size: 11px;
}
.bar-track {
  height: 6px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.07);
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  border-radius: 99px;
  background: var(--blue);
}
.bar-fill.orange { background: var(--accent); }
.bar-fill.green { background: var(--green); }
.bar-fill.purple { background: var(--purple); }
.bar-fill.red { background: var(--red); }

.pill-tag {
  display: inline-block;
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text);
}
.pill-tag.accent {
  background: var(--accent-bg);
  border-color: rgba(251, 146, 60, 0.4);
  color: var(--accent);
}
.pill-tag.green {
  background: rgba(52, 211, 153, 0.14);
  border-color: rgba(52, 211, 153, 0.35);
  color: var(--green);
}

.btn-sm {
  padding: 5px 10px;
  border-radius: 6px;
  background: rgba(251, 146, 60, 0.16);
  border: 1px solid rgba(251, 146, 60, 0.4);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}
.btn-sm:hover {
  background: rgba(251, 146, 60, 0.28);
}

.chron-item {
  display: flex;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 7px;
  background: var(--card);
  border: 1px solid var(--border);
}
.chron-img {
  width: 42px;
  height: 42px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
}
.chron-when {
  font-size: 10px;
  color: var(--muted);
}
.chron-txt {
  font-size: 11.5px;
  color: var(--text);
}

/* Bottom Bar */
.bottombar {
  height: 46px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  gap: 12px;
}

.bottom-left, .bottom-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.play-btn {
  padding: 6px 14px;
  border-radius: 7px;
  background: var(--accent);
  color: #111;
  font-weight: 700;
  font-size: 12px;
}
.play-btn:hover {
  filter: brightness(1.08);
}

.speed-lbl {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-left: 6px;
}

.speed-pills {
  display: flex;
  align-items: center;
  gap: 4px;
}
.spd-pill {
  padding: 4px 8px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
}
.spd-pill:hover {
  color: var(--text);
}
.spd-pill.active {
  background: var(--accent);
  color: #111;
  font-weight: 700;
  border-color: var(--accent);
}

.bottom-note {
  font-size: 11px;
  color: var(--muted);
  margin-left: 6px;
}

.foot-btn {
  padding: 5px 11px;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  font-size: 11.5px;
  color: var(--text);
}
.foot-btn:hover {
  background: rgba(255, 255, 255, 0.12);
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(4, 6, 10, 0.72);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-card {
  width: 100%;
  max-width: 440px;
  background: var(--panel-2);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.65);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.modal-head h2 {
  font-size: 15px;
  font-weight: 700;
}

.modal-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.modal-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  color: var(--muted);
}
.modal-form input[type="text"],
.modal-form input[type="number"],
.modal-form input[type="password"],
.modal-form select {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 7px 9px;
  color: var(--text);
  font-size: 12px;
  outline: none;
}
.modal-form label.checkbox-lbl {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  color: var(--text);
  padding-top: 16px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.muted-txt {
  font-size: 11.5px;
  color: var(--muted);
}
.usage-box {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 11px;
  color: var(--muted);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
}
.primary-btn {
  padding: 7px 16px;
  border-radius: 7px;
  background: var(--accent);
  color: #111;
  font-weight: 700;
  font-size: 12px;
}
.primary-btn:hover {
  filter: brightness(1.08);
}

#main-canvas {
  touch-action: none;
}

.mobile-only {
  display: none;
}

@media (max-width: 900px) {
  .mobile-only {
    display: inline-block;
  }

  #app {
    height: 100dvh;
  }

  .topbar {
    height: auto;
    flex-wrap: wrap;
    padding: 8px 10px 6px;
    gap: 6px 10px;
  }
  .brand {
    min-width: 0;
    flex: 1;
  }
  .tribe-name {
    font-size: 13.5px;
  }
  .hdr-right {
    order: 2;
  }
  .hdr-stats {
    order: 3;
    flex-basis: 100%;
    gap: 14px;
    padding-top: 2px;
    scrollbar-width: none;
  }
  .hdr-stats::-webkit-scrollbar {
    display: none;
  }

  .workspace {
    flex-direction: column;
  }
  .rail {
    display: none;
  }
  .viewport {
    flex: none;
    height: 46dvh;
    min-height: 240px;
    border-bottom: 1px solid var(--border);
  }
  .map-topbar {
    top: 6px;
    left: 6px;
    right: 6px;
    padding: 5px 8px;
    gap: 6px;
  }
  .map-hint {
    display: none;
  }
  .view-switcher {
    top: 44px;
  }

  .inspector {
    width: 100%;
    flex: 1;
    min-height: 0;
    border-left: none;
  }
  .insp-tabs {
    padding: 4px 8px 0;
    scrollbar-width: none;
  }
  .insp-tabs::-webkit-scrollbar {
    display: none;
  }
  .tab-btn {
    padding: 8px 10px;
    font-size: 11.5px;
  }

  .bottombar {
    height: auto;
    flex-wrap: wrap;
    padding: 6px 10px calc(8px + env(safe-area-inset-bottom));
    gap: 6px;
  }
  .bottom-left {
    width: 100%;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .bottom-left::-webkit-scrollbar {
    display: none;
  }
  .bottom-note {
    display: none;
  }
  .bottom-right {
    width: 100%;
    justify-content: space-between;
  }
  .bottom-right .foot-btn {
    flex: 1;
    text-align: center;
    padding: 6px 8px;
  }

  .form-row {
    grid-template-columns: 1fr;
  }
}
