:root {
  color-scheme: dark;
  --bg: #1e222b;
  --panel: #252b36;
  --panel-2: #2b323f;
  --border: #414b5d;
  --border-soft: #343d4b;
  --text: #e3e9f2;
  --muted: #9aa7b8;
  --accent: #71b7ff;
  --accent-2: #77e0c0;
  --warn: #ffcc70;
  --danger: #ff8d9c;
  --wire: #607085;
  --code: #d7e6f7;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-width: 0; min-height: 100%; background: var(--bg); }
body { color: var(--text); font-size: 14px; overflow: auto; }
button, select, textarea { font: inherit; }
button, select { color: var(--text); }
button {
  min-height: 32px;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 10px;
  background: var(--panel-2);
  cursor: pointer;
  touch-action: manipulation;
}
button:hover { border-color: var(--accent); background: #344154; }
button:active { background: #3d4b60; }
button:disabled { opacity: .5; cursor: default; }
select {
  height: 32px;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 3px 8px;
  background: var(--panel-2);
}

#app { min-height: 100vh; }
.app-shell { width: 100%; max-width: 1680px; margin: 0 auto; padding: 8px; }
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 6px;
  margin-bottom: 8px;
  border: 1px solid var(--border-soft);
  border-radius: 7px;
  background: var(--panel);
}
.toolbar .spacer { flex: 1 1 auto; min-width: 12px; }
.toolbar-label { color: var(--muted); white-space: nowrap; }
.toolbar select.example { flex: 1 1 180px; }
.toolbar .speed-range { flex: 1 1 100px; width: clamp(90px, 11vw, 155px); min-width: 78px; accent-color: var(--accent); }
.toolbar .speed-number { width: 68px; height: 32px; padding: 3px 5px; border: 1px solid var(--border); border-radius: 5px; outline: 0; color: var(--code); background: var(--panel-2); font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; text-align: right; }
.toolbar .speed-number:focus { border-color: var(--accent); box-shadow: 0 0 0 1px #3d638b; }
.toolbar .speed-unit, .toolbar .speed-hint { color: var(--muted); white-space: nowrap; font-size: 11px; }
.cycle { color: var(--muted); white-space: nowrap; padding: 0 4px; }

.board {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(350px, .75fr);
  gap: 8px;
  align-items: start;
}
.left-stack, .right-stack { display: grid; gap: 8px; min-width: 0; }
.left-stack { grid-template-rows: auto auto; }
.right-stack { grid-template-rows: auto auto auto minmax(360px, 1fr); }
.left-bottom { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(260px, .9fr); gap: 8px; min-width: 0; }

.panel {
  min-width: 0;
  border: 1px solid var(--border-soft);
  border-radius: 7px;
  background: var(--panel);
  overflow: hidden;
}
.panel-title {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 6px 9px;
  border-bottom: 1px solid var(--border-soft);
  background: #29313d;
  font-weight: 600;
}
.panel-title small { margin-left: auto; color: var(--muted); font-weight: 400; }
.panel-body { padding: 8px; }

.datapath-body { padding: 0; background: #202631; }
#datapath {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 820 / 420;
  min-height: 180px;
  background: #202631;
  touch-action: pinch-zoom;
}
.hint { color: var(--muted); font-size: 12px; }

.signal-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 8px; }
.signal-row { display: flex; align-items: center; min-height: 24px; gap: 6px; color: var(--muted); }
.signal-row code { color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.led { width: 9px; height: 9px; flex: 0 0 9px; border-radius: 50%; background: #596579; box-shadow: 0 0 0 1px #202631; }
.led.on { background: var(--accent-2); box-shadow: 0 0 7px var(--accent-2); }
.signal-extra { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 8px; margin-top: 7px; padding-top: 7px; border-top: 1px solid var(--border-soft); color: var(--muted); }
.signal-extra b { color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-weight: 500; }

.io-body { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; padding: 8px; }
.io-device { min-width: 0; padding: 8px; border: 1px solid var(--border-soft); border-radius: 6px; background: #202631; }
.io-device-title { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; color: var(--text); font-weight: 600; }
.io-device-title > span:last-child { margin-left: auto; color: var(--muted); font-size: 11px; font-weight: 400; }
.io-device-title button { margin-left: auto; min-height: 24px; padding: 2px 6px; color: var(--muted); border-color: var(--border-soft); font-size: 11px; }
.keyboard-device .io-device-title > span:last-child { margin-left: 0; }
.led-label { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 11px; font-weight: 400; }
.io-controls { display: flex; align-items: center; gap: 6px; min-width: 0; }
.io-controls input { width: 100%; min-width: 0; height: 30px; padding: 4px 7px; border: 1px solid var(--border); border-radius: 5px; outline: 0; color: var(--code); background: var(--panel-2); font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.io-controls input:focus { border-color: var(--accent); box-shadow: 0 0 0 1px #3d638b; }
.io-controls button { flex: 0 0 auto; min-height: 30px; padding-inline: 7px; font-size: 12px; }
.keyboard-chassis { padding: 7px; border: 1px solid #151a22; border-radius: 8px; background: linear-gradient(145deg, #3a4350, #252c36 58%, #1b2028); box-shadow: inset 0 1px 0 rgba(255,255,255,.08), inset 0 -2px 0 rgba(0,0,0,.45), 0 3px 7px rgba(0,0,0,.22); }
.keyboard-topbar { display: flex; align-items: center; justify-content: space-between; margin: 0 2px 6px; color: #7f8d9e; font: 9px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; letter-spacing: .04em; }
.keyboard-brand { color: #c5d1df; font-weight: 700; }
.keyboard-topbar span:last-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.virtual-keyboard { display: block; max-width: 100%; overflow-x: auto; padding: 6px; border: 1px solid #11161d; border-radius: 6px; background: #171c23; box-shadow: inset 0 2px 5px rgba(0,0,0,.5); -webkit-overflow-scrolling: touch; }
.keyboard-layout { display: grid; grid-template-columns: minmax(680px, 1fr) 340px; grid-template-rows: auto auto; gap: 6px 8px; width: 100%; min-width: 1028px; }
.keyboard-function-main { grid-column: 1; grid-row: 1; min-width: 0; }
.keyboard-function-side { grid-column: 2; grid-row: 1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: 8px; width: 340px; min-width: 340px; }
.keyboard-function-nav, .keyboard-function-numpad { min-width: 0; }
.keyboard-main { grid-column: 1; grid-row: 2; display: grid; gap: 4px; min-width: 0; }
.keyboard-side { grid-column: 2; grid-row: 2; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: 8px; width: 340px; min-width: 340px; }
.keyboard-cluster { display: grid; align-content: start; gap: 4px; min-width: 0; }
.keyboard-row { display: flex; gap: 4px; min-width: 0; }
.numpad-cluster { grid-template-columns: repeat(4, minmax(0, 1fr)); grid-template-rows: repeat(5, 28px); gap: 4px; align-content: start; }
.numpad-cluster .keyboard-row { display: contents; }
.numpad-cluster .keycap { flex: none; width: auto; min-width: 0; }
.numpad-cluster .keycap.key-tall { min-height: 0; grid-row: span 2; }
.numpad-cluster .keycap.key-wide { grid-column: span 2; }
.numpad-cluster [data-physical-code="NumpadEnter"] { font-size: 8px; }
.keycap { display: flex; align-items: center; justify-content: center; flex: 1 1 0; min-width: 0; min-height: 28px; padding: 3px 2px; border: 1px solid #596678; border-radius: 4px; color: #dbe5f1; background: linear-gradient(180deg, #414c5c, #303947); box-shadow: 0 2px 0 #1a2029, inset 0 1px 0 rgba(255,255,255,.1); font-size: 11px; line-height: 1; font-weight: 600; text-align: center; user-select: none; }
.key-label { display: inline-flex; flex: 0 0 auto; min-width: 0; flex-direction: column; align-items: center; justify-content: center; gap: 1px; white-space: nowrap; }
.key-primary { display: block; line-height: 1; }
.key-secondary { display: block; color: #a6b4c5; font-size: .68em; line-height: 1; font-weight: 500; }
.key-shift-label { color: #dbe5f1; }
.keycap.key-wide { flex-grow: 1.55; }
.keycap.key-long { flex-grow: 1.35; min-width: 42px; }
.keycap.key-space { flex-grow: 4.2; }
.keycap.key-tall { min-height: 60px; }
.keycap.key-iso { flex-grow: .85; }
.keycap.key-function { color: #aebdce; font-size: 9px; letter-spacing: -.015em; }
.keycap.key-arrow { flex-grow: .85; font-size: 14px; }
.key-spacer { flex: 1 1 0; min-width: 0; min-height: 28px; visibility: hidden; pointer-events: none; }
.keycap:hover { border-color: var(--accent); background: linear-gradient(180deg, #52647a, #39485a); }
.keycap:active, .keycap.pressed { transform: translateY(2px); border-color: var(--accent-2); background: linear-gradient(180deg, #304b4c, #263c40); box-shadow: 0 0 0 #1a2029, inset 0 1px 0 rgba(255,255,255,.08); }
.keycap.latched { border-color: var(--accent-2); color: #d9ffe8; background: linear-gradient(180deg, #385b59, #294341); box-shadow: 0 0 7px rgba(93, 218, 159, .3), inset 0 1px 0 rgba(255,255,255,.1); }
.keyboard-hint { margin-top: 6px; color: #8493a5; font-size: 10px; text-align: center; }
.keyboard-fallback { display: flex; align-items: center; gap: 6px; margin-top: 7px; }
.keyboard-fallback input { flex: 1 1 auto; width: 100%; min-width: 0; height: 29px; padding: 4px 7px; border: 1px solid var(--border); border-radius: 5px; outline: 0; color: var(--code); background: var(--panel-2); font: 11px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.keyboard-fallback input:focus { border-color: var(--accent); box-shadow: 0 0 0 1px #3d638b; }
.keyboard-fallback button { flex: 0 0 auto; min-height: 29px; padding-inline: 7px; font-size: 11px; }
.io-meta { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 8px; margin-top: 7px; color: var(--muted); font-size: 11px; }
.io-meta b, .io-meta code, .io-latch code { color: var(--code); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-weight: 500; }
.monitor-chassis { padding: 8px 8px 7px; border: 1px solid #151a22; border-radius: 10px; background: linear-gradient(145deg, #3c4653, #252c37 58%, #1b2028); box-shadow: inset 0 1px 0 rgba(255,255,255,.08), inset 0 -2px 0 rgba(0,0,0,.45), 0 3px 7px rgba(0,0,0,.22); }
.monitor-topbar { display: flex; align-items: center; justify-content: space-between; margin: 0 3px 6px; color: #7f8d9e; font: 9px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; letter-spacing: .05em; }
.monitor-brand { color: #c5d1df; font-weight: 700; }
.monitor-status { display: inline-flex; align-items: center; gap: 5px; }
.monitor-status b { color: #8ca79d; font-weight: 500; }
.monitor-led { width: 7px; height: 7px; border-radius: 50%; background: #596579; box-shadow: 0 0 0 1px #202631; }
.monitor-led.on { background: var(--accent-2); box-shadow: 0 0 7px var(--accent-2); }
.monitor-bezel { padding: 8px; border: 1px solid #11161d; border-radius: 7px; background: #11171c; box-shadow: inset 0 2px 6px rgba(0,0,0,.65), 0 1px 0 rgba(255,255,255,.08); }
.monitor-screen { position: relative; min-height: 140px; overflow: hidden; border: 1px solid #182c27; border-radius: 4px; background: radial-gradient(circle at 50% 35%, #143d32, #071713 72%); box-shadow: inset 0 0 18px rgba(39, 220, 150, .18), 0 0 4px rgba(54, 196, 146, .16); }
.monitor-screen::after { position: absolute; inset: 0; z-index: 1; pointer-events: none; content: ""; background: repeating-linear-gradient(180deg, rgba(173,255,218,.045) 0, rgba(173,255,218,.045) 1px, transparent 1px, transparent 4px); }
.pixel-screen { display: none; width: min(100%, 140px); height: 140px; margin: 0 auto; image-rendering: pixelated; image-rendering: crisp-edges; background: #000; }
.monitor-screen.graphics-mode .pixel-screen { display: block; }
.monitor-screen.graphics-mode .output-screen, .monitor-screen.graphics-mode .screen-cursor { display: none; }
.output-screen { position: relative; z-index: 0; height: 140px; margin: 0; padding: 9px 10px; overflow: auto; color: #a9ffcf; text-shadow: 0 0 5px rgba(109, 255, 184, .45); font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
.screen-cursor { display: inline-block; width: 7px; height: 14px; margin-left: 1px; vertical-align: -2px; background: #a9ffcf; box-shadow: 0 0 7px rgba(109, 255, 184, .65); animation: screen-cursor-blink 1s steps(2, start) infinite; }
.screen-cursor.visible { display: inline-block; }
@keyframes screen-cursor-blink { 50% { opacity: 0; } }
.monitor-controls { display: flex; align-items: center; gap: 7px; margin-top: 7px; color: #9aa7b8; font-size: 11px; }
.monitor-controls span { flex: 1 1 auto; min-width: 0; }
.monitor-controls code { color: var(--code); font: 11px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.monitor-controls button { flex: 0 0 auto; min-height: 27px; padding: 2px 7px; font-size: 11px; }
.io-latch { flex: 1 1 auto; min-width: 0; color: var(--muted); font-size: 11px; }

.data-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.data-table th, .data-table td { height: 25px; padding: 3px 6px; border-bottom: 1px solid var(--border-soft); text-align: left; white-space: nowrap; }
.data-table th { color: var(--muted); font-weight: 500; }
.data-table td.code, .data-table code { color: var(--code); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.data-table tr.current td { color: var(--accent); background: #263b50; }
.data-table tr.changed td { color: var(--accent-2); background: #233b3d; }
.data-table tr.breakpoint td:first-child { color: var(--danger); }
.data-table input[type="checkbox"] { accent-color: var(--danger); }
.scrollbox { max-height: 270px; overflow: auto; overscroll-behavior: contain; }
.memory-scroll { height: 270px; overflow: auto; overscroll-behavior: contain; }
.memory-track { position: relative; height: 98304px; }
.memory-table { position: absolute; top: 0; left: 0; width: 100%; table-layout: fixed; }
.memory-table th:first-child, .memory-table td:first-child { width: 72px; }
.memory-table th:last-child, .memory-table td:last-child { width: 100%; }
.memory-table td { height: 24px; padding-top: 2px; padding-bottom: 2px; }

.editor-panel { min-height: 360px; }
.editor-tabs { display: flex; align-items: center; gap: 5px; padding: 6px; border-bottom: 1px solid var(--border-soft); background: #222935; }
.editor-tabs button { min-height: 28px; padding: 3px 9px; color: var(--muted); background: transparent; border-color: transparent; }
.editor-tabs button.active { color: var(--text); border-color: var(--accent); background: #304158; }
.editor-tabs .tab-hint { margin-left: auto; color: var(--muted); font-size: 12px; white-space: nowrap; }
.editor-wrap { display: grid; grid-template-columns: 42px minmax(0, 1fr); height: 100%; min-height: 360px; background: #1c222b; }
.editor-wrap[hidden] { display: none; }
.line-numbers {
  margin: 0;
  padding: 9px 7px 9px 3px;
  overflow: hidden;
  color: #6e7c91;
  border-right: 1px solid var(--border-soft);
  text-align: right;
  user-select: none;
  font: 12px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#source, #machine-code {
  width: 100%; height: 100%; min-height: 360px; resize: vertical;
  padding: 9px 10px; border: 0; outline: 0; color: var(--code); background: transparent;
  tab-size: 4; white-space: pre; overflow: auto;
  font: 12px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#source:focus, #machine-code:focus { box-shadow: inset 0 0 0 1px #3d638b; }

.statusbar { display: flex; align-items: flex-start; min-height: 34px; margin-top: 8px; padding: 8px 10px; border: 1px solid var(--border-soft); border-radius: 7px; color: var(--muted); background: var(--panel); }
.statusbar.error { color: var(--danger); }

.modal-backdrop { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 16px; background: rgba(8, 12, 18, .72); }
.modal-backdrop[hidden] { display: none; }
.modal { display: flex; flex-direction: column; width: min(820px, 100%); max-height: min(720px, 90vh); border: 1px solid var(--border); border-radius: 8px; background: var(--panel); box-shadow: 0 18px 70px rgba(0, 0, 0, .45); }
.modal-header { display: flex; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--border-soft); font-weight: 600; }
.modal-header button { margin-left: auto; min-height: 28px; }
.modal-body { overflow: auto; padding: 12px 16px; }
.modal-body h3 { margin: 10px 0 5px; color: var(--accent); font-size: 14px; }
.modal-body p, .modal-body pre { margin: 5px 0 12px; color: var(--muted); line-height: 1.55; white-space: pre-wrap; }
.modal-body pre { font: 12px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

@media (max-width: 1100px) {
  .board { grid-template-columns: minmax(0, 1fr); }
  .right-stack { grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr); grid-template-rows: auto auto auto; }
  .right-stack .control-panel { grid-column: 1; grid-row: 1; }
  .right-stack .register-panel { grid-column: 1; grid-row: 2; }
  .right-stack .io-panel { grid-column: 1; grid-row: 3; }
  .right-stack .editor-panel { grid-column: 2; grid-row: 1 / span 3; }
}

@media (max-width: 700px) {
  body { font-size: 13px; }
  .app-shell { padding: 4px; }
  .toolbar { padding: 5px; gap: 5px; }
  .toolbar button { flex: 1 1 auto; padding-inline: 7px; }
  .toolbar .spacer, .toolbar .cycle { display: none; }
  .toolbar-label { margin-left: 2px; }
  .toolbar select.example { flex-basis: 150px; }
  .toolbar .speed-range { flex-basis: 82px; width: 82px; }
  .toolbar .speed-number { width: 62px; }
  .left-bottom { grid-template-columns: minmax(0, 1fr); }
  .right-stack { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto auto; }
  .right-stack .control-panel, .right-stack .register-panel, .right-stack .io-panel, .right-stack .editor-panel { grid-column: 1; grid-row: auto; }
  .panel-title { min-height: 32px; }
  #datapath { min-height: 155px; }
  .scrollbox, .memory-scroll { max-height: 230px; height: 230px; }
  .editor-panel, .editor-wrap, #source, #machine-code { min-height: 360px; }
  .signal-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .io-body { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 420px) {
  .toolbar button { font-size: 12px; padding-inline: 5px; }
  .toolbar-label { font-size: 12px; }
  .toolbar select.example { min-width: 0; flex-basis: 130px; }
  .toolbar .speed-range { flex-basis: 65px; width: 65px; min-width: 54px; }
  .toolbar .speed-number { width: 58px; }
  .toolbar .speed-hint { display: none; }
  .editor-tabs { flex-wrap: wrap; }
  .editor-tabs .tab-hint { width: 100%; margin-left: 0; }
  .signal-grid, .signal-extra { grid-template-columns: minmax(0, 1fr); }
  .io-meta { grid-template-columns: minmax(0, 1fr); }
  .virtual-keyboard { gap: 2px; padding: 4px; }
  .keyboard-row { gap: 2px; }
  .keycap { min-height: 25px; padding-inline: 1px; font-size: 9px; }
  .keycap.key-function { font-size: 8px; }
  .keycap.key-arrow { font-size: 12px; }
  .monitor-screen, .output-screen { min-height: 116px; height: 116px; }
  .pixel-screen { height: 116px; width: min(100%, 116px); }
  .keyboard-fallback { flex-wrap: wrap; }
  .keyboard-fallback input { flex-basis: 150px; }
}

body.host-fullscreen { background: var(--bg); }
