:root {
  --bg: #14171d;
  --bg2: #1b1f27;
  --bg3: #222732;
  --line: #2c323d;
  --fg: #dfe3ea;
  --fg-dim: #9aa3b2;
  --accent: #61afef;
  --green: #7bc96f;
  --red: #e06c75;
  --yellow: #e5c07b;
  --purple: #c678dd;
  --cyan: #56b6c2;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.6;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- 加载遮罩 ---------- */
#loading {
  position: fixed; inset: 0; z-index: 999;
  background: #10131a;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
#loading[hidden] { display: none; }
.load-box { max-width: 520px; text-align: center; }
.load-title { font-size: 22px; font-weight: 800; margin-bottom: 14px; color: #fff; }
#load-msg { color: var(--fg-dim); margin-bottom: 18px; font-weight: 600; }
.load-bar { height: 8px; background: var(--bg3); border-radius: 5px; overflow: hidden; }
#load-bar-in { height: 100%; width: 15%; background: linear-gradient(90deg, var(--accent), var(--cyan)); transition: width .35s ease; }
.load-sub { margin-top: 16px; font-size: 12px; color: #6f7889; font-weight: 600; }

/* ---------- 顶栏 ---------- */
#topbar {
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--bg2);
  border-bottom: 2px solid var(--line);
}
.brand { display: flex; flex-direction: column; line-height: 1.3; }
.brand-main { font-size: 17px; font-weight: 800; color: #fff; letter-spacing: .5px; }
.brand-sub { font-size: 11px; color: var(--fg-dim); font-weight: 600; }

.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sep { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }

.btn {
  background: var(--bg3); color: var(--fg);
  border: 1px solid #39404e; border-radius: 6px;
  padding: 7px 13px; font-size: 13px; font-weight: 700;
  cursor: pointer; font-family: inherit;
  transition: background .12s, border-color .12s;
}
.btn:hover:not(:disabled) { background: #2c3340; border-color: #4a5364; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .38; cursor: not-allowed; }
.btn.primary { background: #235239; border-color: #357a52; color: #d6f5df; }
.btn.primary:hover:not(:disabled) { background: #2b6446; border-color: #43966a; }

select, input[type="text"], #mem-addr, #stdin {
  background: #171a21; color: var(--fg);
  border: 1px solid #39404e; border-radius: 6px;
  padding: 7px 10px; font-size: 13px; font-weight: 600; font-family: inherit;
}
select:focus, input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#example-select { max-width: 290px; }

.speed { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--fg-dim); }
.speed input[type="range"] { width: 96px; accent-color: var(--accent); }
.speed b { color: var(--yellow); min-width: 34px; font-weight: 800; }

.status {
  margin-left: auto; padding: 7px 14px; border-radius: 6px;
  background: #171a21; border: 1px solid var(--line);
  font-size: 13px; font-weight: 700; color: var(--fg-dim);
  max-width: 46ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.status.running { color: var(--green); border-color: #357a52; }
.status.halted { color: var(--cyan); border-color: #2c6570; }
.status.error { color: var(--red); border-color: #7a3540; }
.status.break { color: var(--yellow); border-color: #7a6535; }
.status.input { color: var(--purple); border-color: #6a3f7a; }

/* ---------- 三栏布局 ---------- */
#layout {
  flex: 1;
  display: grid;
  grid-template-columns: 280px minmax(360px, 1fr) 380px;
  gap: 14px;
  padding: 14px 20px 20px;
  min-height: 0;
}
#left, #right { overflow-y: auto; min-height: 0; padding-right: 4px; }
#center { display: flex; flex-direction: column; gap: 10px; min-height: 0; min-width: 0; }

.panel {
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px 14px;
  margin-bottom: 12px;
}
.panel-title {
  font-size: 13px; font-weight: 800; color: #fff;
  margin-bottom: 10px; letter-spacing: .6px;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.panel-title.clickable { cursor: pointer; margin-bottom: 0; }
details[open] .panel-title.clickable { margin-bottom: 10px; }
summary.panel-title { list-style: none; }
summary.panel-title::-webkit-details-marker { display: none; }
summary.panel-title::before { content: "▸"; color: var(--accent); margin-right: 2px; }
details[open] summary.panel-title::before { content: "▾"; }

.note { font-size: 13px; color: var(--fg); font-weight: 600; line-height: 1.75; }
.hint { font-size: 11px; color: #767f8f; font-weight: 600; margin-top: 8px; line-height: 1.6; }
.empty-hint { font-size: 12px; color: #767f8f; padding: 10px 2px; font-weight: 600; }

/* ---------- 教程链接 ---------- */
.link-group { margin-bottom: 12px; }
.link-group-title { font-size: 11px; color: var(--cyan); font-weight: 800; margin-bottom: 6px; letter-spacing: .5px; }
.link-item { display: block; margin-bottom: 8px; }
.link-item a { font-size: 13px; font-weight: 700; }
.link-desc { font-size: 11px; color: #7d8697; font-weight: 600; line-height: 1.55; margin-top: 2px; }
.diff-item { margin-bottom: 10px; }
.diff-item b { color: var(--yellow); font-size: 12px; display: block; margin-bottom: 2px; }
.diff-item span { font-size: 12px; color: var(--fg-dim); font-weight: 600; line-height: 1.65; }

/* ---------- 编辑器 ---------- */
.edit-tabs { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
#blocks-actions { display: flex; gap: 6px; flex-wrap: wrap; }
#blockly-wrap {
  flex: 1 1 auto;
  min-height: 300px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #1b1f27;
}
#blockly-div { width: 100%; height: 100%; min-height: 300px; }
#editor-wrap {
  flex: 1 1 auto; min-height: 240px;
  border: 1px solid var(--line); border-radius: 8px;
  overflow: hidden; background: var(--bg2);
}
#editor { height: 100%; }
#editor .cm-editor { height: 100%; }

#error-banner, #warn-banner {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 10px 14px; border-radius: 7px; font-size: 13px; font-weight: 700;
}
#error-banner { background: #3a1f24; border: 1px solid #7a3540; color: #ffc9ce; }
#warn-banner { background: #3a3320; border: 1px solid #7a6535; color: #ffe6ab; }
#error-banner[hidden], #warn-banner[hidden] { display: none; }
.err-icon { color: var(--red); font-weight: 800; }
.warn-icon { color: var(--yellow); font-weight: 800; }
#error-text, #warn-text { flex: 1; line-height: 1.6; }
.mini-btn {
  background: #2c3340; color: var(--fg); border: 1px solid #414a5a;
  border-radius: 4px; padding: 2px 8px; font-size: 11px; font-weight: 700;
  cursor: pointer; font-family: inherit;
}
.mini-btn:hover { background: #39414f; }
.mini-btn.danger { color: #ffc9ce; border-color: #7a3540; background: #33222a; }
.mini-btn.danger:hover { background: #452b34; }

/* ---------- 下方标签页 ---------- */
#bottom {
  flex: 0 0 auto; height: 290px;
  display: flex; flex-direction: column;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px;
  overflow: hidden;
}
.tabs { display: flex; align-items: center; gap: 4px; padding: 6px 10px; background: #171a21; border-bottom: 1px solid var(--line); }
.tab {
  background: transparent; border: 1px solid transparent; color: var(--fg-dim);
  padding: 5px 12px; border-radius: 5px; font-size: 12px; font-weight: 700;
  cursor: pointer; font-family: inherit;
}
.tab:hover { color: var(--fg); background: #222732; }
.tab.active { color: #fff; background: var(--bg3); border-color: #39404e; }
.tab-spacer { flex: 1; }
.counters { font-size: 11px; color: #7d8697; font-weight: 700; font-family: Consolas, monospace; }

.tab-page { flex: 1; overflow: auto; padding: 12px 14px; min-height: 0; }
.tab-page[hidden] { display: none; }

#console-out {
  margin: 0; font-family: "Cascadia Code", Consolas, "Courier New", monospace;
  font-size: 13px; font-weight: 600; color: #c8e6c9; white-space: pre-wrap;
  word-break: break-all; line-height: 1.7;
}
#tab-console { display: flex; flex-direction: column; }
#console-out { flex: 1; overflow: auto; }
#input-row { display: flex; align-items: center; gap: 8px; padding-top: 10px; border-top: 1px solid var(--line); margin-top: 8px; }
#input-row[hidden] { display: none; }
.input-label { font-size: 12px; color: var(--purple); font-weight: 800; white-space: nowrap; }
#stdin { flex: 1; }

/* ---------- VGA ---------- */
#vga { background: #000; padding: 8px; border-radius: 5px; display: inline-block; }
.vga-grid { display: grid; gap: 0; font-family: Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.25; }
.vga-cell { width: 9.6px; height: 17px; text-align: center; overflow: hidden; white-space: pre; }
.vga-cursor { outline: 1px solid #fff; outline-offset: -1px; }

/* ---------- 反汇编 ---------- */
#disasm { font-family: Consolas, "Courier New", monospace; font-size: 12px; }
.disasm-head, .disasm-row { display: grid; grid-template-columns: 20px 82px 168px 1fr; gap: 8px; align-items: baseline; padding: 2px 4px; }
.disasm-head { color: #7d8697; font-weight: 800; border-bottom: 1px solid var(--line); position: sticky; top: -12px; background: var(--bg2); padding-bottom: 5px; }
.disasm-row { border-radius: 3px; }
.disasm-row:hover { background: #222732; }
.disasm-row.current { background: #2b3a24; box-shadow: inset 3px 0 0 var(--green); }
.da-bp { color: #3a4150; cursor: pointer; font-weight: 800; text-align: center; }
.da-bp.on { color: var(--red); }
.da-addr { color: var(--cyan); font-weight: 700; }
.da-bytes { color: #8b93a3; word-break: break-all; }
.da-text { color: var(--fg); font-weight: 600; }

/* ---------- 寄存器 ---------- */
.reg-table { width: 100%; border-collapse: collapse; font-family: Consolas, monospace; font-size: 13px; }
.reg-table td { padding: 3px 6px; border-bottom: 1px solid #232833; }
.reg-name { color: var(--yellow); font-weight: 800; width: 46px; }
.reg-hex { color: var(--fg); font-weight: 700; }
.reg-dec { color: #7d8697; text-align: right; font-size: 11px; }
.reg-table tr.changed .reg-hex { color: var(--green); }
.reg-table tr.changed { background: #1f2a20; }
.reg-table tr.is-eip .reg-name { color: var(--green); }

/* ---------- 标志 ---------- */
.flags { display: flex; flex-wrap: wrap; gap: 6px; }
.flag {
  display: inline-flex; align-items: center; gap: 4px;
  background: #171a21; border: 1px solid #333a47; border-radius: 5px;
  padding: 3px 8px; font-size: 11px; font-family: Consolas, monospace;
}
.flag b { color: var(--fg-dim); font-weight: 800; }
.flag i { font-style: normal; color: #5a6272; font-weight: 800; }
.flag.on { border-color: #357a52; background: #1f2a20; }
.flag.on b { color: var(--green); }
.flag.on i { color: var(--green); }
.flag-raw { width: 100%; font-size: 11px; color: #7d8697; font-family: Consolas, monospace; margin-top: 4px; }

/* ---------- 内存 ---------- */
.mem-nav { display: flex; gap: 4px; align-items: center; margin-left: auto; }
#mem-addr { width: 82px; padding: 2px 6px; font-size: 11px; font-family: Consolas, monospace; }
.memory { font-family: Consolas, "Courier New", monospace; font-size: 11.5px; overflow-x: auto; }
.mem-head, .mem-row { display: grid; grid-template-columns: 70px repeat(16, 1fr) 130px; gap: 1px; align-items: center; }
.mem-head { color: #7d8697; font-weight: 800; margin-bottom: 4px; font-size: 10px; }
.mem-col-h { text-align: center; }
.mem-ascii-h { text-align: left; padding-left: 6px; }
.mem-addr-col { color: var(--cyan); font-weight: 700; }
.mem-cell { text-align: center; color: var(--fg); cursor: pointer; border-radius: 2px; padding: 1px 0; font-weight: 600; }
.mem-cell:hover { background: #39414f; outline: 1px solid var(--accent); }
.mem-cell.zero { color: #4d5563; }
.mem-cell.changed { color: var(--red); font-weight: 800; background: #2a1f24; }
.mem-ascii { color: #8b93a3; padding-left: 6px; white-space: pre; letter-spacing: .5px; }

/* ---------- 栈 ---------- */
.stack-head { font-size: 11px; color: var(--fg-dim); font-weight: 700; margin-bottom: 6px; font-family: Consolas, monospace; }
.stack-warn { font-size: 12px; color: var(--yellow); font-weight: 700; }
.stack-table { width: 100%; border-collapse: collapse; font-family: Consolas, monospace; font-size: 12px; }
.stack-table td { padding: 2px 5px; border-bottom: 1px solid #232833; }
.stack-addr { color: var(--cyan); font-weight: 700; }
.stack-val { color: var(--fg); font-weight: 700; }
.stack-note { color: var(--green); font-size: 11px; }
.stack-top { background: #1f2a20; }
.stack-top .stack-addr::after { content: " ← ESP"; color: var(--green); font-size: 10px; }

/* ---------- 磁盘 ---------- */
.disk-bar { margin-bottom: 8px; }
.disk-usage { font-size: 11px; color: var(--fg-dim); font-weight: 700; }
.disk-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.disk-table td { padding: 4px 5px; border-bottom: 1px solid #232833; }
.disk-name { color: var(--accent); font-weight: 700; cursor: pointer; word-break: break-all; }
.disk-name:hover { text-decoration: underline; }
.disk-size { color: #7d8697; text-align: right; white-space: nowrap; font-family: Consolas, monospace; }
.disk-ops { text-align: right; width: 52px; }

/* ---------- 页脚 ---------- */
#foot {
  padding: 14px 24px 22px;
  background: var(--bg2); border-top: 2px solid var(--line);
  font-size: 11.5px; color: #828b9c; font-weight: 600; line-height: 1.85;
}
#foot b { color: var(--fg-dim); font-weight: 800; }
#foot .sep { display: inline-block; width: 1px; height: 11px; background: var(--line); margin: 0 10px; vertical-align: middle; }

/* ---------- 滚动条 ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: #171a21; }
::-webkit-scrollbar-thumb { background: #333a47; border-radius: 5px; border: 2px solid #171a21; }
::-webkit-scrollbar-thumb:hover { background: #454e5e; }

/* ---------- 提示条 ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 34px;
  transform: translateX(-50%);
  z-index: 600;
  max-width: min(620px, calc(100vw - 48px));
  background: #23304a;
  border: 1px solid #3d5378;
  color: #dbe7ff;
  border-radius: 8px;
  padding: 11px 18px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.65;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .45);
}
#toast[hidden] { display: none; }

/* ---------- 窄屏 ---------- */
@media (max-width: 1280px) {
  #layout { grid-template-columns: 240px minmax(300px, 1fr) 330px; gap: 10px; padding: 10px 12px 16px; }
}
@media (max-width: 1024px) {
  /* 窄屏改为正常文档流：三栏自然堆叠、页面滚动，避免 flex/grid 高度挤压导致重叠 */
  #layout { display: block; flex: none; height: auto; }
  #left, #right { overflow: visible; min-height: 0; }
  #center { min-height: 0; }
  #editor-wrap { height: 360px; flex: none; }
  #blockly-wrap { height: 420px; flex: none; }
  .status { margin-left: 0; max-width: none; }
}

/* ---------- 模式切换 ---------- */
.modes { display: flex; gap: 6px; }
.btn.mode { border-color: #39404e; }
.btn.mode.active { background: #23405c; border-color: #3d6a94; color: #d6e9ff; }

/* ---------- 侧栏教程入口 ---------- */
.reader-launch { display: flex; flex-direction: column; gap: 8px; }
.btn.wide { width: 100%; text-align: center; }
.ext-links { margin-top: 10px; }
.ext-links summary { cursor: pointer; font-size: 12px; color: var(--fg-dim); font-weight: 700; }
.ext-links #tutorial-links { margin-top: 8px; }

/* ---------- 整机模式 ---------- */
#whole {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px 20px 20px;
}
.whole-bar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 14px;
}
#v86-image { max-width: 340px; }
.v86-status { margin-left: auto; font-size: 12px; font-weight: 700; color: var(--fg-dim); }
.v86-status.err { color: var(--red); }

.whole-body {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) 360px;
  gap: 14px;
  align-items: start;
}
.whole-screen-wrap { display: flex; flex-direction: column; gap: 8px; }
.v86-screen {
  height: 440px;
  background: #000;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
  padding: 8px;
  outline: none;
  cursor: pointer;
}
.v86-screen:focus { border-color: var(--accent); }
.v86-text { color: #aaa; font: 14px/1.15 monospace; white-space: pre; }
.v86-canvas { image-rendering: pixelated; display: block; }

.whole-serial {
  display: flex; flex-direction: column; gap: 8px;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px;
  padding: 12px 14px; min-height: 0;
}
#v86-serial {
  flex: 1; min-height: 200px; resize: vertical;
  background: #10131a; color: #c8e6c9;
  border: 1px solid #39404e; border-radius: 6px;
  font: 12px/1.6 Consolas, monospace; padding: 10px;
}
.serial-send-row { display: flex; gap: 8px; }
.serial-send-row input { flex: 1; }

/* ---------- 下载进度遮罩 ---------- */
#v86-progress {
  position: fixed; inset: 0; z-index: 700;
  background: rgba(8, 10, 14, .82);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.prog-box {
  width: min(560px, 100%);
  background: var(--bg2); border: 1px solid #39404e; border-radius: 10px;
  padding: 22px 26px; text-align: center;
}
.prog-title { font-size: 16px; font-weight: 800; color: #fff; margin-bottom: 14px; }
.prog-file { font-size: 13px; font-weight: 700; color: var(--cyan); margin-bottom: 10px; min-height: 20px; }
.prog-bar { height: 12px; background: #171a21; border: 1px solid #39404e; border-radius: 7px; overflow: hidden; }
#v86-prog-bar-in { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), var(--green)); transition: width .12s linear; }
.prog-text { font-size: 13px; font-weight: 700; color: var(--fg); margin-top: 10px; font-family: Consolas, monospace; }
.prog-sub { font-size: 11px; color: #767f8f; font-weight: 600; margin: 12px 0 14px; line-height: 1.6; }

/* ---------- 教程阅读器 ---------- */
#reader {
  position: fixed; inset: 0; z-index: 650;
  background: rgba(8, 10, 14, .9);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.reader {
  width: min(1080px, 100%); height: min(860px, 100%);
  background: var(--bg2); border: 1px solid #39404e; border-radius: 12px;
  display: flex; flex-direction: column; overflow: hidden;
}
.reader-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px; background: #171a21; border-bottom: 1px solid var(--line);
}
.reader-title { font-size: 15px; font-weight: 800; color: #fff; }
.reader-spacer { flex: 1; }
.reader-body { flex: 1; display: grid; grid-template-columns: 260px 1fr; min-height: 0; }
.reader-list { overflow-y: auto; border-right: 1px solid var(--line); padding: 10px; background: #171a21; }
.reader-item {
  display: block; width: 100%; text-align: left;
  background: transparent; border: 1px solid transparent; border-radius: 6px;
  color: var(--fg-dim); font: 700 12.5px/1.5 inherit; font-family: inherit;
  padding: 8px 10px; cursor: pointer; margin-bottom: 4px;
}
.reader-item:hover { background: #222732; color: var(--fg); }
.reader-item.active { background: #23405c; border-color: #3d6a94; color: #d6e9ff; }
.reader-content { overflow-y: auto; padding: 20px 26px 30px; }
.reader-content h2 { font-size: 19px; font-weight: 800; color: #fff; margin: 0 0 14px; }
.reader-content h3 { font-size: 14px; font-weight: 800; color: var(--cyan); margin: 18px 0 8px; }
.reader-content p { font-size: 13.5px; font-weight: 600; line-height: 1.85; color: var(--fg); margin: 0 0 10px; }
.reader-content ul { margin: 0 0 12px; padding-left: 22px; }
.reader-content li { font-size: 13px; font-weight: 600; line-height: 1.8; color: var(--fg); margin-bottom: 4px; }
.reader-code {
  background: #10131a; border: 1px solid #2c323d; border-radius: 6px;
  padding: 12px 14px; overflow-x: auto;
  font: 600 12.5px/1.7 Consolas, monospace; color: #c8e6c9;
  margin: 0 0 12px; white-space: pre;
}
.reader-tip, .reader-warn {
  border-radius: 6px; padding: 10px 14px; margin: 0 0 12px;
  font-size: 12.5px; font-weight: 600; line-height: 1.75;
}
.reader-tip { background: #1f2a20; border: 1px solid #357a52; color: #cdeed6; }
.reader-tip b { color: var(--green); margin-right: 8px; }
.reader-warn { background: #3a3320; border: 1px solid #7a6535; color: #ffe6ab; }
.reader-warn b { color: var(--yellow); margin-right: 8px; }
.reader-example { border: 1px solid #3d6a94; border-radius: 8px; padding: 14px 16px; margin-top: 16px; background: #1a2230; }
.reader-example-head { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.reader-example-head b { color: #d6e9ff; font-size: 13px; }
.reader-example-head span { color: var(--fg-dim); font-size: 12px; font-weight: 600; }

@media (max-width: 900px) {
  .whole-body { grid-template-columns: 1fr; }
  .reader-body { grid-template-columns: 1fr; }
  .reader-list { max-height: 180px; border-right: none; border-bottom: 1px solid var(--line); }
}