* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, sans-serif;
  display: flex;
  height: 100vh;
  color: #1a1a1a;
}
#sidebar {
  width: 280px;
  border-right: 1px solid #ddd;
  padding: 12px;
  overflow-y: auto;
  background: #fafafa;
  flex-shrink: 0;
}
#sidebar h1 { font-size: 18px; margin: 0 0 12px; }
#view-nav { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 10px; }
#view-nav button {
  flex: 1;
  padding: 6px 4px;
  border: 1px solid #ccc;
  background: #fff;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
#view-nav button:hover { background: #eee; }
#view-nav button.active { background: #2563eb; color: #fff; border-color: #2563eb; }
#view-nav .nav-parent { flex-basis: 100%; }
.nav-children {
  display: flex;
  gap: 4px;
  flex-basis: 100%;
  padding-left: 12px;
}
.nav-children.hidden { display: none; }

/* ---- Buildings grid ---- */
#buildings {
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
  align-content: start;
}
.bcard {
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 10px 12px;
  background: #fff;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.bcard.owned { border-color: #4a9a3f; background: #f5faf3; }
.bcard.locked { opacity: 0.75; }
.bcard-name { font-weight: 600; font-size: 13px; cursor: pointer; }
.bcard-name:hover { color: #2563eb; text-decoration: underline; }
.bcard-ctl { display: flex; align-items: center; gap: 6px; }
.bcard-ctl button {
  width: 24px;
  height: 24px;
  border: 1px solid #ccc;
  background: #fff;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}
.bcard-ctl button:hover:not(:disabled) { background: #eee; }
.bcard-ctl button:disabled { opacity: 0.4; cursor: default; }
.bcard-ctl .blevel {
  width: 52px;
  padding: 3px 6px;
  border: 1px solid #ccc;
  border-radius: 4px;
  text-align: center;
}
.bcard-ctl .bmax { color: #777; font-size: 12px; }
#main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
#toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid #ddd;
}
#toolbar h2 { margin: 0; font-size: 16px; flex-shrink: 0; }
#controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
  flex-wrap: wrap;
}
#controls label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #444;
}
#controls input {
  width: 56px;
  padding: 4px 6px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
#controls select {
  padding: 4px 6px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
.speed-totals {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 4px 10px;
  font-size: 12px;
  color: #444;
  background: #f3ecd7;
  border: 1px solid #d8c68a;
  border-radius: 4px;
}
.speed-totals strong { color: #7a5c12; }
.bonus-summary {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 10px;
  font-size: 13px;
  color: #444;
}
#content-wrap { overflow: auto; flex: 1; position: relative; }
.hidden { display: none !important; }

/* ---- Research graph ---- */
#graph {
  min-width: 100%;
  min-height: 100%;
  background: #fff;
}
.graph-canvas { position: relative; }
.graph-canvas svg { position: absolute; top: 0; left: 0; pointer-events: none; }
.edge {
  fill: none;
  stroke: #c9a24a;
  stroke-width: 2;
}
.node {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 4px 6px;
  border: 1px solid #b9922f;
  border-radius: 6px;
  background: #f7f3e8;
  color: #1a1a1a;
  font-size: 12px;
  line-height: 1.15;
  cursor: pointer;
}
.node:hover { border-color: #2563eb; }
.node.selected {
  border-color: #2563eb;
  box-shadow: 0 0 0 2px #2563eb;
}
.node.owned { background: #e5f0e0; border-color: #4a9a3f; }
.node.locked { opacity: 0.85; border-style: dashed; }
.node-name { font-weight: 600; }
.node-ctl {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 3px;
}
.node-ctl button {
  width: 18px;
  height: 18px;
  line-height: 1;
  padding: 0;
  border: 1px solid #aaa;
  border-radius: 4px;
  background: #fff;
  cursor: pointer;
  font-size: 13px;
}
.node-ctl button:hover:not(:disabled) { background: #ddd; }
.node-ctl button:disabled { opacity: 0.35; cursor: default; }
.node-badge {
  font-size: 11px;
  color: #555;
  min-width: 26px;
}
.node-ctl .nlevel {
  width: 40px;
  padding: 1px 3px;
  border: 1px solid #aaa;
  border-radius: 4px;
  text-align: center;
  font-size: 11px;
}
.node-ctl .nmax { font-size: 11px; color: #666; }
tr.owned-row { background: #e5f0e0 !important; }
tr.next-row { background: #fff6d6 !important; }

/* ---- Research detail panel ---- */
#detail {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 640px;
  max-width: 85%;
  background: #fff;
  border-left: 1px solid #ddd;
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  z-index: 10;
}
#detail-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid #ddd;
}
#detail-header h3 { margin: 0; font-size: 15px; flex: 1; }
#detail-close {
  border: none;
  background: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: #666;
}
#detail-close:hover { color: #000; }
#detail-body { overflow: auto; flex: 1; }
table { border-collapse: collapse; font-size: 12px; width: 100%; }
th, td {
  border: 1px solid #e5e5e5;
  padding: 4px 8px;
  text-align: left;
  white-space: nowrap;
}
th {
  position: sticky;
  top: 0;
  background: #f3f4f6;
  cursor: pointer;
  user-select: none;
}
th:hover { background: #e5e7eb; }
th .type { color: #999; font-weight: normal; font-size: 10px; }
tbody tr:nth-child(even) { background: #fafafa; }
td.desc { white-space: normal; min-width: 320px; max-width: 480px; }

/* ---- Cost modal ---- */
#cost-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}
.cost-box {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  width: 720px;
  max-width: 92%;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
}
.cost-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid #ddd;
}
.cost-header h3 { margin: 0; font-size: 15px; flex: 1; }
#cost-close {
  border: none;
  background: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: #666;
}
#cost-close:hover { color: #000; }
#cost-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
}
.cost-scroll { flex: 1 1 auto; min-height: 0; overflow: auto; }
.cost-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid #ddd;
  flex: 0 0 auto;
}
.cost-footer button {
  padding: 7px 14px;
  border-radius: 6px;
  border: 1px solid #bbb;
  background: #f3f4f6;
  cursor: pointer;
  font-size: 13px;
}
.cost-footer button:hover { background: #e7e9ee; }
.cost-footer button.primary {
  background: #2f6f3e;
  border-color: #2f6f3e;
  color: #fff;
}
.cost-footer button.primary:hover { background: #285f35; }
.cost-table td, .cost-table th { text-align: right; }
.cost-table td:first-child, .cost-table th:first-child { text-align: left; }
.cost-table { table-layout: fixed; }
.cost-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #fff;
  box-shadow: inset 0 -1px 0 #ddd;
}
.cost-totals { margin-top: 0; }
tr.cost-total td { border-top: 2px solid #999; background: #f3f4f6; }
tr.cost-reduced td { background: #e5f0e0; }
.gem-breakdown {
  margin-top: 10px;
  font-size: 13px;
  color: #444;
}
.ico {
  width: 1.1em;
  height: 1.1em;
  vertical-align: -0.2em;
  flex: none;
}
.res-head {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.gem-breakdown .gem-item { padding: 1px 0; }
.gem-breakdown .gem-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 24px;
  margin-top: 4px;
}
.gem-breakdown .gem-total {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid #ddd;
  font-size: 14px;
}
.kind-tag {
  display: inline-block;
  font-size: 10px;
  color: #7a5c1a;
  background: #f3e6c4;
  border-radius: 3px;
  padding: 0 4px;
  vertical-align: middle;
}
.kind-tag.research { color: #1e4d7a; background: #cfe6f7; }
tr.stage-row td {
  background: #eef2ff;
  color: #3730a3;
  font-weight: 600;
  font-size: 12px;
  border-top: 2px solid #c7d2fe;
  padding: 5px 8px;
}
