/* ================================================================
   Apostila · Sistemas Embarcados · Dispositivos de E/S
   Direção B — Modern Tech Doc
   ================================================================ */

:root {
  --bg: #FAFAF9;
  --surface: #FFFFFF;
  --surface-2: #F5F5F2;
  --ink: #0A0F1F;
  --ink-soft: #475467;
  --ink-faint: #8A93A3;
  --primary: #2949E5;
  --primary-tint: #EEF2FF;
  --primary-tint-2: #DDE3FF;
  --accent: #E5484D;
  --accent-tint: #FFEDEB;
  --green: #0FA968;
  --green-tint: #E6F7EF;
  --amber: #C97F00;
  --amber-tint: #FFF6E4;
  --rule: rgba(10, 15, 31, 0.08);
  --rule-strong: rgba(10, 15, 31, 0.16);
  --code-bg: #0F1530;
  --code-ink: #E5EAF5;
  --code-muted: #8FA0C7;
  --code-blue: #7DD3FC;
  --code-pink: #F0ABFC;
  --code-amber: #FBBF24;
  --code-green: #86EFAC;

  --font-sans: "Geist", "Inter Tight", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "Geist Mono", "JetBrains Mono", ui-monospace, SFMono-Regular, monospace;

  --max-content: 720px;
  --sidebar-w: 280px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; scroll-padding-top: 24px; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 17px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

img { max-width: 100%; }

/* ---------- LAYOUT ---------- */

.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  gap: 0;
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  overflow-y: auto;
  padding: 32px 24px 32px 40px;
  border-right: 1px solid var(--rule);
  background: var(--surface);
  font-size: 14px;
}

.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--rule-strong); border-radius: 3px; }

.main {
  padding: 0;
  min-width: 0;
}

.content-wrap {
  max-width: 980px;
  margin: 0 auto;
  padding: 80px 64px 160px;
}

@media (max-width: 1100px) {
  .content-wrap { padding: 64px 40px 120px; }
}

@media (max-width: 880px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: relative; height: auto; border-right: none; border-bottom: 1px solid var(--rule); }
  .content-wrap { padding: 48px 24px 80px; }
}

/* ---------- SIDEBAR ---------- */

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 36px;
}
.sidebar-brand img { width: 40px; height: 40px; object-fit: contain; }
.sidebar-brand .brand-name { font-weight: 600; font-size: 14px; line-height: 1.2; }
.sidebar-brand .brand-sub { font-size: 11px; color: var(--ink-faint); letter-spacing: 0.06em; text-transform: uppercase; }

.sidebar-meta {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
  letter-spacing: 0.02em;
  margin-bottom: 12px;
  text-transform: uppercase;
}

.sidebar-title {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 20px;
  line-height: 1.3;
}

.toc { list-style: none; padding: 0; margin: 0; }
.toc > li { margin-bottom: 4px; }
.toc a {
  display: block;
  color: var(--ink-soft);
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 13.5px;
  border-left: 2px solid transparent;
  transition: background 120ms, color 120ms, border-color 120ms;
}
.toc a:hover { color: var(--ink); background: var(--surface-2); }
.toc a.is-active {
  color: var(--primary);
  background: var(--primary-tint);
  border-left-color: var(--primary);
  font-weight: 500;
}
.toc .toc-chapter > a {
  font-weight: 600;
  color: var(--ink);
  font-size: 14px;
  padding-top: 10px;
  padding-bottom: 10px;
}
.toc .toc-chapter > a .num {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--primary);
  margin-right: 8px;
  letter-spacing: 0.02em;
}
.toc .toc-sub { list-style: none; padding-left: 18px; margin: 2px 0 8px; }
.toc .toc-sub a { font-size: 13px; color: var(--ink-faint); padding: 4px 10px; }
.toc .toc-sub a.is-active { color: var(--primary); background: transparent; border-left-color: var(--primary); font-weight: 500; }

.sidebar-footer {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid var(--rule);
  font-size: 12px;
  color: var(--ink-faint);
  line-height: 1.5;
}
.sidebar-footer strong { color: var(--ink); font-weight: 600; }

/* ---------- COVER ---------- */

.cover {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 20px;
  padding: 72px 64px 60px;
  margin-bottom: 80px;
  position: relative;
  overflow: hidden;
}
.cover::before {
  content: "";
  position: absolute;
  top: -120px; right: -120px;
  width: 480px; height: 480px;
  background: radial-gradient(circle, var(--primary-tint) 0%, transparent 65%);
  pointer-events: none;
}
.cover::after {
  content: "";
  position: absolute;
  bottom: -180px; left: -120px;
  width: 380px; height: 380px;
  background: radial-gradient(circle, var(--accent-tint) 0%, transparent 65%);
  pointer-events: none;
}
.cover-inner { position: relative; z-index: 1; }
.cover-badge {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 12px;
  background: var(--primary-tint);
  color: var(--primary);
  border-radius: 999px;
  font-size: 12px; font-weight: 500;
  margin-bottom: 32px;
}
.cover-badge .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--primary); }
.cover h1 {
  font-size: clamp(40px, 5.5vw, 72px);
  line-height: 1.02;
  letter-spacing: -0.03em;
  font-weight: 600;
  margin: 0 0 24px;
}
.cover h1 em { font-style: normal; color: var(--primary); }
.cover-lead {
  font-size: 21px;
  line-height: 1.5;
  color: var(--ink-soft);
  max-width: 640px;
  margin: 0 0 48px;
  text-wrap: pretty;
}
.cover-meta {
  display: grid;
  grid-template-columns: repeat(4, minmax(0,1fr));
  gap: 32px;
  font-size: 14px;
}
.cover-meta dt { font-size: 11px; color: var(--ink-faint); letter-spacing: 0.06em; text-transform: uppercase; margin-bottom: 6px; }
.cover-meta dd { margin: 0; font-weight: 500; }

@media (max-width: 760px) {
  .cover { padding: 48px 32px 40px; }
  .cover-meta { grid-template-columns: repeat(2, 1fr); gap: 20px; }
}

/* ---------- CHAPTER ---------- */

.chapter { margin-bottom: 120px; }
.chapter:last-child { margin-bottom: 0; }

.chapter-header {
  padding-bottom: 36px;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 56px;
}
.chapter-kicker {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--primary);
  font-weight: 500;
  letter-spacing: 0.02em;
  margin-bottom: 14px;
}
.chapter h2 {
  font-size: clamp(34px, 4.2vw, 48px);
  font-weight: 600;
  letter-spacing: -0.025em;
  line-height: 1.08;
  margin: 0 0 20px;
  text-wrap: balance;
}
.chapter-summary {
  font-size: 19px;
  line-height: 1.55;
  color: var(--ink-soft);
  max-width: 760px;
  margin: 0;
  text-wrap: pretty;
}

/* ---------- CONTENT BLOCKS ---------- */

.block { margin-bottom: 24px; }
.block:last-child { margin-bottom: 0; }

h3.subsection {
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.015em;
  line-height: 1.2;
  margin: 64px 0 18px;
  text-wrap: balance;
}
h3.subsection:first-child { margin-top: 0; }

h4.sub2 {
  font-size: 19px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 36px 0 12px;
}

p.body {
  font-size: 17px;
  line-height: 1.7;
  margin: 0 0 18px;
  color: var(--ink);
  max-width: var(--max-content);
  text-wrap: pretty;
}
p.body:last-child { margin-bottom: 0; }
p.body.lead {
  font-size: 19px;
  color: var(--ink-soft);
  line-height: 1.6;
}

p.body code,
li code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: var(--surface-2);
  color: var(--accent);
  padding: 1px 6px;
  border-radius: 4px;
  border: 1px solid var(--rule);
}
p.body em { font-style: italic; color: var(--ink); }
p.body strong { font-weight: 600; color: var(--ink); }

ul.body, ol.body {
  max-width: var(--max-content);
  padding-left: 0;
  margin: 0 0 24px;
  list-style: none;
}
ul.body li, ol.body li {
  position: relative;
  padding-left: 28px;
  font-size: 17px;
  line-height: 1.65;
  margin-bottom: 8px;
  color: var(--ink);
}
ul.body li::before {
  content: "";
  position: absolute;
  left: 8px;
  top: 13px;
  width: 6px; height: 6px;
  background: var(--primary);
  border-radius: 50%;
}
ol.body { counter-reset: ol-counter; }
ol.body li { counter-increment: ol-counter; }
ol.body li::before {
  content: counter(ol-counter, decimal-leading-zero);
  position: absolute;
  left: 0;
  top: 0;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--primary);
  font-weight: 600;
}

.two-col-list {
  max-width: var(--max-content);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 28px;
  margin: 0 0 24px;
  padding: 0;
  list-style: none;
}
.two-col-list li {
  position: relative;
  padding-left: 18px;
  font-size: 15.5px;
  line-height: 1.55;
}
.two-col-list li::before {
  content: "→";
  position: absolute; left: 0;
  color: var(--primary);
  font-family: var(--font-mono);
}

/* ---------- CALLOUTS ---------- */

.callout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 16px;
  padding: 18px 22px;
  border-radius: 12px;
  margin: 28px 0;
  max-width: var(--max-content);
  font-size: 16px;
  line-height: 1.55;
  align-items: start;
  border: 1px solid var(--rule);
  background: var(--surface);
}
.callout .callout-icon {
  width: 28px; height: 28px;
  display: grid; place-items: center;
  border-radius: 8px;
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
}
.callout .callout-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  margin-bottom: 4px;
}
.callout p { margin: 0; font-size: 16px; line-height: 1.55; color: var(--ink); }
.callout p + p { margin-top: 8px; }
.callout code { font-family: var(--font-mono); background: rgba(0,0,0,0.05); padding: 1px 5px; border-radius: 4px; font-size: 13px; }

.callout--info { background: var(--primary-tint); border-color: rgba(41, 73, 229, 0.18); }
.callout--info .callout-icon { background: var(--primary); color: white; }
.callout--info .callout-label { color: var(--primary); }

.callout--warn { background: var(--accent-tint); border-color: rgba(229, 72, 77, 0.2); }
.callout--warn .callout-icon { background: var(--accent); color: white; }
.callout--warn .callout-label { color: var(--accent); }

.callout--tip { background: var(--green-tint); border-color: rgba(15, 169, 104, 0.2); }
.callout--tip .callout-icon { background: var(--green); color: white; }
.callout--tip .callout-label { color: var(--green); }

.callout--note { background: var(--amber-tint); border-color: rgba(201, 127, 0, 0.2); }
.callout--note .callout-icon { background: var(--amber); color: white; }
.callout--note .callout-label { color: var(--amber); }

/* ---------- CODE BLOCK ---------- */

.code-block {
  background: var(--code-bg);
  border-radius: 12px;
  overflow: hidden;
  margin: 28px 0;
  font-family: var(--font-mono);
  font-size: 13.5px;
  max-width: var(--max-content);
}
.code-block-header {
  padding: 10px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  color: var(--code-muted);
  font-size: 12px;
}
.code-block-header .dot-red { color: #FF5F57; }
.code-block-header .file { display: flex; align-items: center; gap: 8px; }
.code-block pre {
  margin: 0;
  padding: 18px 22px;
  color: var(--code-ink);
  line-height: 1.7;
  overflow-x: auto;
}
.code-block .c-comment { color: #748AB7; font-style: italic; }
.code-block .c-keyword { color: var(--code-pink); }
.code-block .c-type    { color: var(--code-pink); }
.code-block .c-fn      { color: var(--code-amber); }
.code-block .c-num     { color: var(--code-blue); }
.code-block .c-str     { color: var(--code-green); }

/* ---------- PULL QUOTE ---------- */

.pullquote {
  margin: 56px 0;
  padding: 0;
  max-width: 820px;
  font-size: 26px;
  line-height: 1.4;
  font-weight: 500;
  letter-spacing: -0.015em;
  color: var(--ink);
  text-wrap: balance;
  border-left: 3px solid var(--primary);
  padding-left: 28px;
}
.pullquote::before {
  content: "“";
  font-size: 64px;
  color: var(--primary);
  line-height: 0.5;
  margin-right: 4px;
  vertical-align: -0.4em;
}

/* ---------- FIGURE ---------- */

.figure {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 16px;
  overflow: hidden;
  margin: 36px 0;
}
.figure-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--rule);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-soft);
  background: var(--surface-2);
}
.figure-header .figure-dot { color: var(--primary); margin-right: 6px; }
.figure-body { padding: 24px; }
.figure-caption {
  padding: 12px 20px;
  border-top: 1px solid var(--rule);
  font-size: 13px;
  color: var(--ink-soft);
  background: var(--surface-2);
}

/* ---------- TABLE ---------- */

.data-table {
  width: 100%;
  max-width: var(--max-content);
  border-collapse: collapse;
  margin: 28px 0;
  font-size: 15px;
}
.data-table th, .data-table td {
  text-align: left;
  padding: 12px 16px;
  border-bottom: 1px solid var(--rule);
}
.data-table th {
  font-weight: 600;
  font-size: 12px;
  color: var(--ink-faint);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: var(--surface-2);
}
.data-table tbody tr:hover { background: var(--surface-2); }
.data-table .num {
  font-family: var(--font-mono);
  color: var(--primary);
  font-weight: 600;
}
.data-table .seg-cell {
  font-family: var(--font-mono);
  font-size: 13px;
  letter-spacing: 0.04em;
}
.data-table .seg-cell .seg-on {
  display: inline-block;
  background: var(--primary);
  color: white;
  width: 22px;
  text-align: center;
  border-radius: 4px;
  padding: 2px 0;
  margin-right: 3px;
  font-weight: 600;
}
.data-table .seg-cell .seg-off {
  display: inline-block;
  color: var(--ink-faint);
  width: 22px;
  text-align: center;
  padding: 2px 0;
  margin-right: 3px;
  border: 1px dashed var(--rule-strong);
  border-radius: 4px;
}

/* ---------- KEY/VALUE GRID (PIN TABLE) ---------- */

.kv-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px 18px;
  margin: 24px 0;
  max-width: var(--max-content);
  font-size: 15px;
}
.kv-grid dt {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  color: var(--primary);
  padding-top: 2px;
}
.kv-grid dd { margin: 0; color: var(--ink-soft); }

/* ---------- DIVIDER ---------- */

.section-break {
  text-align: center;
  margin: 80px 0;
  color: var(--ink-faint);
  font-family: var(--font-mono);
  font-size: 16px;
  letter-spacing: 0.4em;
}

/* ---------- WIDGET WRAPPER ---------- */

.widget {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 16px;
  overflow: hidden;
  margin: 36px 0;
}
.widget-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--rule);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-soft);
  background: var(--surface-2);
}
.widget-header > span:first-child { min-width: 0; flex: 1 1 auto; }
.widget-header > span:last-child { flex: 0 0 auto; }
.widget-header .live {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.widget-header .live .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 3px var(--green-tint);
}

/* ---------- KEYPAD ---------- */
.keypad-key {
  background: var(--surface);
  border: 1.5px solid var(--rule-strong);
  border-radius: 10px;
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 500;
  color: var(--ink);
  padding: 18px 0;
  text-align: center;
  cursor: pointer;
  position: relative;
  transition: all 120ms ease;
  user-select: none;
}
.keypad-key:hover { border-color: var(--primary); color: var(--primary); }
.keypad-key.pressed {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  transform: translateY(1px);
  box-shadow: 0 0 0 4px var(--primary-tint);
}
.keypad-key.scanned-row::before,
.keypad-key.scanned-col::after {
  content: "";
  position: absolute;
  background: var(--accent);
  opacity: 0.5;
}
.keypad-key.scanned-row::before {
  left: 0; right: 0; top: 50%;
  height: 2px;
  transform: translateY(-50%);
}
.keypad-key.scanned-col::after {
  top: 0; bottom: 0; left: 50%;
  width: 2px;
  transform: translateX(-50%);
}

/* ---------- LCD ---------- */
.lcd-screen {
  background: #6E9F47;
  padding: 20px;
  border-radius: 8px;
  font-family: var(--font-mono);
  letter-spacing: 0.04em;
  border: 6px solid #1F2A18;
  box-shadow: inset 0 0 12px rgba(0,0,0,0.2);
  display: inline-block;
}
.lcd-row {
  display: flex;
  gap: 3px;
}
.lcd-row + .lcd-row { margin-top: 4px; }
.lcd-cell {
  width: 22px;
  height: 32px;
  background: rgba(0,0,0,0.04);
  display: grid;
  place-items: center;
  font-size: 22px;
  font-weight: 600;
  color: #14260A;
  text-shadow: 0 0 1px rgba(0,0,0,0.4);
  border-radius: 2px;
}

/* ---------- BUTTONS ---------- */

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px;
  background: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: 8px;
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
  transition: all 120ms ease;
}
.btn:hover { background: var(--surface-2); border-color: var(--ink-faint); }
.btn.primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn.primary:hover { background: #2039CC; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--primary); }
.btn.ghost:hover { background: var(--primary-tint); }

/* ---------- LINKS ---------- */

a.inline-link { color: var(--primary); text-decoration: underline; text-decoration-color: rgba(41, 73, 229, 0.4); text-underline-offset: 3px; }
a.inline-link:hover { text-decoration-color: var(--primary); }
