:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2129;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --blue: #58a6ff;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --purple: #bc8cff;
  --mono: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); font-size: 12px; }

/* 顶栏 */
.topbar {
  display: flex;
  align-items: center;
  gap: 32px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 12px; flex: none; }
.logo { font-size: 30px; }
.brand h1 { margin: 0; font-size: 18px; font-weight: 600; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.stats { display: flex; gap: 28px; flex: 1; align-items: flex-end; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; gap: 2px; min-width: 90px; }
.stat.wide { min-width: 230px; }
.stat label { color: var(--muted); font-size: 12px; }
.stat b { font-family: var(--mono); font-size: 18px; font-weight: 600; white-space: nowrap; }
.stat.eta b { color: var(--yellow); }
.bar { height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; width: 0; background: var(--green); transition: width .2s; }

/* 对决赛道 */
.race {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 20px;
  padding: 10px 20px 12px;
  background: linear-gradient(90deg, rgba(63, 185, 80, .08), transparent 40%, transparent 60%, rgba(188, 140, 255, .1));
  border-bottom: 1px solid var(--border);
  position: relative;
}
.racer-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 6px; }
.rival .racer-head { justify-content: flex-end; }
.face { font-size: 26px; line-height: 1; align-self: center; }
.face.robot { display: inline-block; }
.face.robot.shake { animation: shake .5s; }
@keyframes shake { 25% { transform: rotate(-12deg); } 75% { transform: rotate(12deg); } }
.who { font-weight: 700; font-size: 16px; }
.you .who, .you .pct { color: var(--green); }
.rival .who, .rival .pct { color: var(--purple); }
.pct { font-family: var(--mono); font-size: 22px; }
.spd { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.track { height: 12px; background: var(--panel-2); border-radius: 6px; overflow: hidden; border: 1px solid var(--border); }
.track i { display: block; height: 100%; width: 0; transition: width .3s; }
.you .track i { background: var(--green); box-shadow: 0 0 10px rgba(63, 185, 80, .7); }
.rival .track { direction: rtl; }
.rival .track i { background: var(--purple); box-shadow: 0 0 10px rgba(188, 140, 255, .7); }
.vs { display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 180px; }
.vs b { font-size: 26px; font-style: italic; font-weight: 900; color: #ffa657; letter-spacing: .05em; }
.vs span { font-size: 12px; color: var(--muted); text-align: center; }
.vs span.behind { color: #ffa198; }
.vs span.ahead { color: #7ee787; }
.bubble {
  position: absolute;
  right: 20px;
  top: calc(100% + 8px);
  max-width: 360px;
  padding: 10px 14px;
  background: #2a2140;
  border: 1px solid var(--purple);
  border-radius: 12px;
  color: #e9ddff;
  font-size: 14px;
  line-height: 1.5;
  z-index: 15;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
  animation: bubblein .25s ease-out;
}
.bubble::before {
  content: "";
  position: absolute;
  top: -7px;
  right: 18px;
  width: 12px;
  height: 12px;
  background: #2a2140;
  border-left: 1px solid var(--purple);
  border-top: 1px solid var(--purple);
  transform: rotate(45deg);
}
@keyframes bubblein { from { transform: translateY(-6px); opacity: 0; } }

.intro-text { color: #c9d1d9; line-height: 1.7; margin: 6px 0; }
.purple { color: var(--purple); }
.intro-vs { display: flex; align-items: center; justify-content: space-around; margin: 20px 0; padding: 16px; background: var(--panel-2); border-radius: 10px; }
.intro-vs div { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.intro-vs span { font-size: 40px; }
.intro-vs small { color: var(--muted); }
.intro-vs strong { font-size: 30px; font-style: italic; color: #ffa657; }
.intro-rule { font-size: 16px; margin: 0 0 20px; }

.incident {
  padding: 10px 20px;
  background: rgba(248, 81, 73, .15);
  border-bottom: 1px solid rgba(248, 81, 73, .5);
  color: #ffa198;
  font-weight: 500;
  animation: blink 2s ease-in-out infinite;
}
@keyframes blink { 50% { background: rgba(248, 81, 73, .28); } }

/* 三栏布局 */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(420px, 1fr) minmax(320px, min(46vw, calc(100vh - 250px))) 360px;
  grid-template-areas: "editor map side";
  gap: 12px;
  padding: 12px;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  min-height: 0;
}
.panel-title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 10px 14px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

/* 编辑器 */
.editor-panel { grid-area: editor; display: flex; flex-direction: column; overflow: hidden; }
.tabbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--panel-2);
  border-bottom: 1px solid var(--border);
  padding: 0 12px 0 0;
}
.tab {
  font-family: var(--mono);
  font-size: 13px;
  padding: 9px 16px;
  background: var(--bg);
  border-right: 1px solid var(--border);
  border-top: 2px solid var(--blue);
}
.file-meta { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.editor {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 22px;
  padding: 8px 0;
  cursor: pointer;
  user-select: none;
  outline: none;
}
.editor:active { background: #0f141b; }
.row { display: flex; height: 22px; white-space: pre; padding-right: 12px; }
.row .ln { width: 64px; flex: none; text-align: right; padding-right: 16px; color: #484f58; }
.row .code { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.row.done { opacity: .32; }
.row.done .ln { color: var(--green); }
.row.head { background: rgba(56, 139, 253, .14); box-shadow: inset 3px 0 0 var(--blue); }
.row.head .ln { color: var(--text); }
.row.sep { color: var(--muted); font-size: 12px; justify-content: center; opacity: .8; }
.row.bug, .row.found { height: auto; min-height: 22px; }
.row.bug .code, .row.found .code { white-space: pre-wrap; }
.row.bug { background: rgba(248, 81, 73, .16); box-shadow: inset 3px 0 0 var(--red); animation: bugpulse 1.2s ease-in-out infinite; }
.row.found { background: rgba(248, 81, 73, .3); box-shadow: inset 3px 0 0 var(--red); }
@keyframes bugpulse { 50% { background: rgba(248, 81, 73, .32); } }
.row .tag {
  flex: none;
  font-family: var(--sans);
  font-size: 11px;
  color: var(--blue);
  margin-left: 8px;
}
.row.bug .tag { color: #ffa198; font-weight: 600; }
.kw { color: #ff7b72; }
.str { color: #a5d6ff; }
.num { color: #79c0ff; }
.cm { color: #8b949e; font-style: italic; }
.hint {
  padding: 8px 14px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  background: var(--panel-2);
}

/* 仓库地图 */
.map-panel { grid-area: map; display: flex; flex-direction: column; overflow: hidden; }
.map-wrap { position: relative; margin: 12px; aspect-ratio: 1; max-height: calc(100% - 24px); align-self: center; width: calc(100% - 24px); }
#map { width: 100%; height: 100%; display: block; image-rendering: pixelated; border-radius: 4px; }
.map-grid {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(to right, rgba(13, 17, 23, .55) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(13, 17, 23, .55) 1px, transparent 1px);
  background-size: 6.25% 6.25%;
}
.map-file {
  position: absolute;
  width: 6.25%;
  height: 6.25%;
  border: 2px solid var(--blue);
  box-shadow: 0 0 12px rgba(88, 166, 255, .6);
  pointer-events: none;
  transition: left .15s, top .15s;
}
.map-cursor {
  position: absolute;
  width: 8px;
  height: 8px;
  margin: -4px 0 0 -4px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 8px 2px var(--blue);
  pointer-events: none;
}
.map-cursor.bug { background: var(--red); box-shadow: 0 0 14px 6px var(--red); width: 14px; height: 14px; margin: -7px 0 0 -7px; }
.legend { display: flex; gap: 16px; padding: 0 14px 12px; color: var(--muted); font-size: 12px; margin-top: auto; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.sw-code { background: linear-gradient(90deg, #4a6fa0, #7a5c96, #3f8a78); }
.sw-done { background: #18221f; border: 1px solid var(--border); }
.sw-file { border: 2px solid var(--blue); }

/* 右栏 */
.side { grid-area: side; display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.rival { padding: 12px 14px; flex: none; }
.rival-head { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  display: grid; place-items: center; font-size: 22px;
  background: rgba(188, 140, 255, .15);
  border: 1px solid rgba(188, 140, 255, .5);
}
.rival-name { font-weight: 600; }
.rival-pct { margin-left: auto; font-family: var(--mono); font-size: 18px; color: var(--purple); }
.rival-bar i { background: var(--purple); }
.rival-status { margin: 8px 0 0; font-size: 12px; color: var(--muted); min-height: 2.8em; line-height: 1.4; }

.shop { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.owned-summary { margin: 0 14px 6px; font-size: 12px; color: var(--green); }
.shop h3 { margin: 10px 14px 6px; font-size: 12px; color: var(--muted); font-weight: 500; }
.shop-list { display: flex; flex-direction: column; gap: 6px; padding: 0 10px; }
.shop-list:last-child { padding-bottom: 10px; }
.item {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "name cost" "desc owned";
  gap: 2px 8px;
  text-align: left;
  width: 100%;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.item:hover { border-color: var(--blue); }
.item .name { grid-area: name; font-weight: 600; }
.item .desc { grid-area: desc; font-size: 12px; color: var(--muted); }
.item .cost { grid-area: cost; font-family: var(--mono); font-size: 13px; color: var(--yellow); text-align: right; }
.item .owned { grid-area: owned; font-family: var(--mono); font-size: 12px; color: var(--muted); text-align: right; }
.item.poor { cursor: not-allowed; opacity: .55; }
.item.poor:hover { border-color: var(--border); }
.item.poor .cost { color: var(--muted); }
.item.bought { opacity: .45; cursor: default; }
.item.bought .cost { color: var(--green); }
.item.mystery { cursor: default; opacity: .5; }
.item.mystery:hover { border-color: var(--border); }
.item.pop { animation: pop .25s ease-out; }
@keyframes pop { 40% { transform: scale(1.03); border-color: var(--green); } }

.log-panel { flex: 0 0 210px; display: flex; flex-direction: column; overflow: hidden; }
.log { list-style: none; margin: 0; padding: 8px 14px; overflow-y: auto; flex: 1; font-size: 12px; line-height: 1.5; }
.log li { padding: 4px 0; border-bottom: 1px dashed #21262d; color: #c9d1d9; }
.log li time { font-family: var(--mono); color: var(--muted); margin-right: 8px; }
.log li.ai { color: #d2a8ff; }
.log li.alert { color: #ffa198; }
.log li.win { color: #7ee787; }
.link { background: none; border: none; color: var(--muted); cursor: pointer; font: inherit; font-size: 12px; }
.link:hover { color: var(--red); }

/* 点击飘字 */
.float {
  position: fixed;
  pointer-events: none;
  font-family: var(--mono);
  font-weight: 700;
  font-size: 16px;
  color: var(--green);
  text-shadow: 0 0 6px rgba(63, 185, 80, .6);
  animation: floatup .8s ease-out forwards;
  z-index: 10;
}
@keyframes floatup { to { transform: translateY(-50px); opacity: 0; } }

/* 结局 */
.ending {
  position: fixed;
  inset: 0;
  background: rgba(1, 4, 9, .78);
  display: grid;
  place-items: center;
  z-index: 20;
}
.ending-card {
  width: min(640px, calc(100vw - 32px));
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px 32px;
  animation: rise .4s ease-out;
}
@keyframes rise { from { transform: translateY(20px); opacity: 0; } }
.ending-kicker { color: var(--red); font-weight: 700; letter-spacing: .1em; }
.ending h2 { margin: 6px 0 16px; font-size: 24px; }
.bug-code {
  background: var(--bg);
  border: 1px solid rgba(248, 81, 73, .5);
  border-radius: 8px;
  padding: 12px 14px;
  font-family: var(--mono);
  font-size: 13px;
  white-space: pre-wrap;
  margin: 0;
}
.bug-explain { color: #c9d1d9; line-height: 1.6; }
.bug-explain code { font-family: var(--mono); color: #ffa198; }
.ending-stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 16px 0; }
.ending-stats div { background: var(--panel-2); border-radius: 8px; padding: 10px 12px; }
.ending-stats label { display: block; color: var(--muted); font-size: 12px; }
.ending-stats b { font-family: var(--mono); font-size: 16px; }
.ending-line { font-size: 17px; line-height: 1.6; margin: 16px 0 20px; }
.primary {
  background: var(--green);
  color: #04130a;
  border: none;
  border-radius: 8px;
  padding: 10px 22px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

/* 窄屏：地图挪到编辑器下面 */
@media (max-width: 1280px) {
  body { overflow: auto; }
  .layout {
    grid-template-columns: 1fr 340px;
    grid-template-areas: "editor side" "map side";
    grid-template-rows: 60vh auto;
  }
  .map-wrap { max-width: 560px; }
}
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; grid-template-areas: "editor" "map" "side"; grid-template-rows: 60vh auto auto; }
  .topbar { flex-direction: column; align-items: flex-start; gap: 12px; }
}
