:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --ink: #1f2724;
  --muted: #66716c;
  --line: #d7d2c9;
  --teal: #177e70;
  --teal-dark: #0d5b51;
  --amber: #cd6b1f;
  --blue: #2b6ea3;
  --red: #b64337;
  --shell: #e8e3da;
  --code: #212a28;
  --shadow: 0 10px 30px rgba(39, 44, 42, .08);
  font-family: "Segoe UI", "Microsoft YaHei", -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 16px;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background:
    linear-gradient(90deg, rgba(23,126,112,.05) 1px, transparent 1px),
    linear-gradient(rgba(23,126,112,.05) 1px, transparent 1px),
    var(--bg);
  background-size: 28px 28px, 28px 28px, auto;
}

button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4, p { margin: 0; }
button { cursor: pointer; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 74px;
  padding: 12px clamp(20px, 5vw, 64px);
  background: rgba(255, 255, 255, .94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 2px solid var(--ink);
  border-radius: 6px;
  background: var(--shell);
  font-family: Consolas, monospace;
  font-size: .76rem;
  font-weight: 800;
  letter-spacing: .06em;
}
.brand h1 { font-size: 1.22rem; letter-spacing: 0; }
.brand p { color: var(--muted); font-size: .84rem; }

.module-nav {
  display: flex;
  gap: 5px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--shell);
}
.nav-btn {
  min-width: 82px;
  padding: 9px 16px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  white-space: nowrap;
}
.nav-btn:hover { color: var(--ink); }
.nav-btn.active {
  color: #fff;
  background: var(--teal-dark);
  box-shadow: inset 0 -3px 0 rgba(255,255,255,.22);
}

main {
  width: min(1360px, calc(100% - 32px));
  margin: 32px auto 40px;
}
.view.hidden, .hidden { display: none !important; }

.section-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 22px;
}
.section-heading h2 {
  font-size: clamp(1.4rem, 2.4vw, 2rem);
  line-height: 1.1;
}
.section-heading p {
  max-width: 680px;
  margin-top: 8px;
  color: var(--muted);
}
.chip {
  flex: 0 0 auto;
  padding: 7px 11px;
  border: 1px solid rgba(43,110,163,.35);
  border-radius: 4px;
  color: var(--blue);
  background: rgba(43,110,163,.08);
  font-family: Consolas, monospace;
  font-size: .78rem;
  white-space: nowrap;
}

.panel {
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.panel + .panel { margin-top: 18px; }
.panel-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 16px;
}
.panel-title h3 { font-size: 1.05rem; }
.small-label {
  color: var(--muted);
  font-family: Consolas, monospace;
  font-size: .72rem;
  text-transform: uppercase;
}

.primary-btn, .ghost-btn {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 9px 16px;
  transition: background .15s, border-color .15s, color .15s;
}
.primary-btn { background: var(--teal-dark); color: #fff; }
.primary-btn:hover { background: #08463e; }
.primary-btn:disabled, .ghost-btn:disabled { opacity: .45; cursor: not-allowed; }
.ghost-btn {
  border-color: #adb8b2;
  background: #fff;
  color: var(--ink);
}
.ghost-btn:hover { border-color: var(--teal); color: var(--teal-dark); }
.wide { width: 100%; }

/* Startup */
.startup-layout {
  display: grid;
  grid-template-columns: minmax(360px, .78fr) minmax(420px, 1fr);
  gap: 18px;
}
.step-panel { position: relative; overflow: hidden; }
.step-progress {
  position: absolute;
  top: 0;
  left: 0;
  height: 4px;
  background: var(--amber);
  transition: width .35s ease;
}
.step-counter {
  margin-bottom: 10px;
  color: var(--muted);
  font-family: Consolas, monospace;
  font-size: .78rem;
}
#startup-step-title { font-size: 1.5rem; }
#startup-step-description { min-height: 78px; margin: 12px 0 16px; color: #4a544f; line-height: 1.65; }
.code-block {
  min-height: 170px;
  padding: 15px;
  overflow: auto;
  border-radius: 6px;
  background: var(--code);
}
.code-block code {
  display: block;
  color: #d9e7e2;
  font-family: Consolas, "Cascadia Mono", monospace;
  font-size: .82rem;
  line-height: 1.6;
  white-space: pre;
}
.transport {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

.register-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 18px;
}
.register-cell {
  padding: 10px 8px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #faf9f6;
}
.register-cell span, .register-cell strong { display: block; font-family: Consolas, monospace; }
.register-cell span { color: var(--muted); font-size: .68rem; }
.register-cell strong { margin-top: 3px; font-size: .84rem; }
.register-cell.changed { border-color: var(--amber); background: #fff4e7; }

.memory-map {
  display: grid;
  grid-template-columns: 106px 1fr;
  gap: 9px;
  align-items: center;
}
.memory-row { display: contents; }
.memory-label {
  padding: 8px 7px;
  border-radius: 4px;
  background: var(--shell);
  font-family: Consolas, monospace;
  font-size: .72rem;
  text-align: right;
}
.memory-block {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 38px;
  padding: 4px;
  border: 1px solid #b4bdb9;
  border-radius: 4px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.word {
  flex: 0 0 auto;
  min-width: 52px;
  padding: 4px 5px;
  border-radius: 3px;
  background: #f0efeb;
  color: #535d58;
  font-family: Consolas, monospace;
  font-size: .68rem;
  text-align: center;
}
.word.flash { background: #d7e7e3; color: var(--teal-dark); }
.word.ram { background: #dbe9f4; color: #225f8e; }
.word.changed { outline: 2px solid var(--amber); outline-offset: 1px; }
.word.empty { opacity: .35; }
.word.label { min-width: 96px; background: #eceff4; }
.legend { display: flex; gap: 16px; margin-top: 15px; color: var(--muted); font-size: .78rem; }
.legend span { display: flex; align-items: center; gap: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; }
.dot.flash { background: #9fc6bb; }
.dot.ram { background: #a9c9e0; }
.dot.changed { background: var(--amber); }

/* Forms */
.controls-panel { display: flex; flex-direction: column; gap: 15px; }
.field-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
label {
  display: block;
  color: var(--muted);
  font-size: .8rem;
}
input, select {
  width: 100%;
  height: 42px;
  margin-top: 6px;
  padding: 0 12px;
  border: 1px solid #b8c0bc;
  border-radius: 5px;
  background: #fff;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(23,126,112,.3);
  border-color: var(--teal);
}
.input-wrap { position: relative; display: block; }
.input-wrap em {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-38%);
  color: var(--muted);
  font-family: Consolas, monospace;
  font-style: normal;
  font-size: .74rem;
}
fieldset {
  min-width: 0;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
}
legend { padding: 0 6px; color: var(--muted); font-size: .78rem; }
.preset-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.wide-label label { margin-top: 0; }

.insight-panel {
  padding: 14px;
  border-left: 3px solid var(--amber);
  background: #fdf6ee;
}
.insight-panel h4 { margin-bottom: 7px; }
.insight-panel p { margin-top: 6px; color: #584b3e; font-size: .85rem; line-height: 1.55; }
code { font-family: Consolas, "Cascadia Mono", monospace; }
.insight-panel code { color: var(--teal-dark); font-weight: 700; }

/* Clock */
.clock-layout {
  display: grid;
  grid-template-columns: 350px minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}
.clock-results { min-width: 0; }
.clock-tree { display: grid; grid-template-columns: 1fr; gap: 0; padding: 20px 20px 16px; }
.tree-lane {
  position: relative;
  min-height: 62px;
  padding: 8px 8px 0;
  border-bottom: 1px dashed #dedad2;
}
.tree-lane:last-child { border-bottom: 0; }
.tree-label {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-family: Consolas, monospace;
  font-size: .72rem;
}
.tree-label b { color: var(--ink); font-size: .82rem; }
.tree-bar {
  position: relative;
  height: 21px;
  margin-top: 7px;
  border-radius: 3px;
  background: rgba(23,126,112,.12);
}
.tree-bar::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: repeating-linear-gradient(90deg, var(--teal) 0 2px, transparent 2px 10px);
  opacity: .35;
}
.tree-bar.usb { background: rgba(205,107,31,.16); }
.tree-bar.usb::after { background: repeating-linear-gradient(90deg, var(--amber) 0 2px, transparent 2px 8px); }
.tree-badge {
  position: absolute;
  right: 0;
  top: -25px;
  padding: 2px 6px;
  border-radius: 3px;
  color: #fff;
  font-size: .68rem;
  white-space: nowrap;
}
.tree-badge.good { background: var(--teal-dark); }
.tree-badge.warn { background: var(--red); }

.solution-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 18px;
}
.solution-card {
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
}
.solution-card.best { border-color: var(--teal); outline: 1px solid var(--teal); }
.solution-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.solution-head h3 { font-size: 1.15rem; font-family: Consolas, monospace; }
.best-tag, .usb-tag {
  padding: 3px 7px;
  border-radius: 3px;
  color: #fff;
  font-size: .7rem;
}
.best-tag { background: var(--teal-dark); }
.usb-tag.good { background: var(--teal); }
.usb-tag.warn { background: var(--red); }
.solution-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin: 14px 0;
}
.param {
  padding: 7px;
  border-radius: 4px;
  background: var(--shell);
  font-family: Consolas, monospace;
  text-align: center;
  font-size: .76rem;
}
.param b { display: block; font-size: .9rem; }
.solution-notes { color: var(--muted); font-size: .8rem; line-height: 1.5; }
.solution-notes li { margin-top: 4px; }
.solution-code {
  margin-top: 12px;
  padding: 10px;
  overflow-x: auto;
  border-radius: 5px;
  background: #eef1ef;
  font-size: .7rem;
  white-space: pre;
}
.empty-state {
  grid-column: 1 / -1;
  padding: 28px;
  border: 1px dashed #b9b2a7;
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

/* NVIC */
.nvic-layout {
  display: grid;
  grid-template-columns: 390px minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}
.nvic-output { min-width: 0; }
.irq-config {
  display: grid;
  grid-template-columns: 74px 1fr 1fr 1fr 28px;
  gap: 6px;
  align-items: center;
}
.irq-config input, .irq-config select { height: 38px; margin: 0; padding: 0 7px; }
.irq-name { text-transform: uppercase; font-family: Consolas, monospace; font-size: .78rem; }
.remove-irq {
  height: 34px;
  border: 0;
  border-radius: 4px;
  background: #eee7dd;
  color: #7d5747;
  font-weight: 700;
}
.irq-divider {
  grid-column: 1 / -1;
  height: 1px;
  margin: 2px 0;
  background: var(--line);
}
.irq-preview {
  grid-column: 1 / -1;
  margin-top: -2px;
  color: var(--muted);
  font-family: Consolas, monospace;
  font-size: .68rem;
}
.timeline-panel { padding-bottom: 10px; }
.nvic-track { position: relative; margin-bottom: 24px; }
.nvic-track-label {
  position: absolute;
  left: 0;
  top: -18px;
  display: flex;
  justify-content: space-between;
  width: 100%;
  color: var(--muted);
  font-family: Consolas, monospace;
  font-size: .7rem;
}
.nvic-canvas { position: relative; height: 36px; border-bottom: 1px solid #d8d2c8; background: rgba(255,255,255,.4); }
.irq-block {
  position: absolute;
  top: 6px;
  height: 24px;
  min-width: 3px;
  border-radius: 3px;
  padding: 0 6px;
  color: #fff;
  font-family: Consolas, monospace;
  font-size: .66rem;
  line-height: 24px;
  white-space: nowrap;
  overflow: hidden;
}
.irq-block.preempted { box-shadow: inset 0 -4px 0 rgba(0,0,0,.2); }
.irq-marker {
  position: absolute;
  top: 31px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  transform: translateX(-3px);
}
.time-axis {
  position: relative;
  height: 22px;
  margin-top: 8px;
  border-bottom: 1px solid var(--ink);
}
.time-tick {
  position: absolute;
  bottom: 0;
  transform: translateX(-50%);
  color: var(--muted);
  font-family: Consolas, monospace;
  font-size: .66rem;
}
.time-tick::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -5px;
  width: 1px;
  height: 5px;
  background: var(--ink);
}
.event-log {
  display: grid;
  gap: 8px;
  max-height: 260px;
  overflow: auto;
}
.event-item {
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 10px;
  padding: 9px;
  border-radius: 5px;
  background: #faf9f6;
  font-size: .82rem;
}
.event-time { color: var(--muted); font-family: Consolas, monospace; }

/* Register translator */
.register-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}
#gpio-builder .field-grid { grid-template-columns: repeat(3, 1fr); }
textarea {
  width: 100%;
  min-height: 210px;
  resize: vertical;
  padding: 12px;
  border: 1px solid #b8c0bc;
  border-radius: 6px;
  background: #fff;
  font-family: Consolas, monospace;
  font-size: .8rem;
  line-height: 1.55;
}
.translation-result {
  display: grid;
  gap: 9px;
  margin-top: 15px;
}
.translation-item {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 5px;
}
.translation-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 10px;
  background: var(--shell);
  font-family: Consolas, monospace;
  font-size: .72rem;
}
.translation-body { padding: 10px; font-size: .83rem; line-height: 1.55; }
.translation-body ul { margin: 6px 0 0; padding-left: 18px; color: #545f59; }
.translation-body li { margin-top: 3px; }
.plain { color: var(--teal-dark); font-weight: 650; }
.generated-code {
  margin-top: 10px;
  padding: 12px;
  overflow-x: auto;
  border-radius: 5px;
  background: var(--code);
  color: #e0ebe7;
  font-size: .73rem;
  white-space: pre;
}
.notice {
  padding: 10px;
  border-left: 3px solid var(--blue);
  background: #eef4f9;
  color: #455a6b;
  font-size: .82rem;
}

footer {
  width: min(1360px, calc(100% - 32px));
  margin: 0 auto 40px;
  color: var(--muted);
  font-size: .78rem;
}

@media (max-width: 1020px) {
  .startup-layout, .clock-layout, .nvic-layout, .register-layout {
    grid-template-columns: 1fr;
  }
  .solution-list { grid-template-columns: 1fr; }
  .app-header { align-items: stretch; flex-direction: column; }
  .module-nav { overflow-x: auto; }
  .nav-btn { flex: 1 0 auto; }
  .section-heading { align-items: flex-start; flex-direction: column; }
}

@media (max-width: 640px) {
  :root { font-size: 15px; }
  main { width: min(100% - 20px, 1360px); margin-top: 20px; }
  .panel { padding: 15px; }
  .field-grid, #gpio-builder .field-grid { grid-template-columns: 1fr; }
  .register-grid { grid-template-columns: repeat(2, 1fr); }
  .memory-map { grid-template-columns: 88px 1fr; }
  .word { min-width: 45px; font-size: .62rem; }
  .word.label { min-width: 84px; }
  .transport { flex-wrap: wrap; }
  .transport .primary-btn { flex: 1; }
  .preset-row { grid-template-columns: 1fr; }
  .solution-grid { grid-template-columns: repeat(2, 1fr); }
  .irq-config { grid-template-columns: 1fr 1fr; }
  .irq-divider, .irq-preview { grid-column: 1 / -1; }
  .remove-irq { grid-column: 1; }
  .irq-config > *:nth-child(-n+4) { grid-column: span 1; }
}
