:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --border: #d9dee7;
  --text: #0f172a;
  --text-2: #4b5567;
  --muted: #7a8496;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --accent-soft: #e3ecff;
  --good: #15803d;
  --warn: #b45309;
  --bad: #b91c1c;
  --grid: #e5e9f0;
  --bit-on: #2563eb;
  --bit-off: #ffffff;
  --shadow: 0 1px 2px rgb(15 23 42 / .06), 0 4px 16px rgb(15 23 42 / .05);
  --radius: 14px;
  --mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120;
    --surface: #131b2e;
    --surface-2: #1b253b;
    --border: #2a3550;
    --text: #e6eaf2;
    --text-2: #a9b3c6;
    --muted: #7f8aa1;
    --accent: #6c9bff;
    --accent-ink: #0b1120;
    --accent-soft: #1d2b4d;
    --good: #4ade80;
    --warn: #fbbf24;
    --bad: #f87171;
    --grid: #232e46;
    --bit-on: #6c9bff;
    --bit-off: #131b2e;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #131b2e;
  --surface-2: #1b253b;
  --border: #2a3550;
  --text: #e6eaf2;
  --text-2: #a9b3c6;
  --muted: #7f8aa1;
  --accent: #6c9bff;
  --accent-ink: #0b1120;
  --accent-soft: #1d2b4d;
  --good: #4ade80;
  --warn: #fbbf24;
  --bad: #f87171;
  --grid: #232e46;
  --bit-on: #6c9bff;
  --bit-off: #131b2e;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 112px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
code, .mono { font-family: var(--mono); }
.muted { color: var(--muted); }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.top-inner {
  max-width: 980px; margin: 0 auto; padding: 10px 16px 4px;
  display: flex; align-items: center; justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 18px; }
.brand svg { width: 28px; height: 28px; color: var(--accent); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text-2); cursor: pointer; display: grid; place-items: center;
}
.icon-btn svg { width: 18px; height: 18px; }
.tabs {
  max-width: 980px; margin: 0 auto; padding: 6px 16px 10px;
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  flex: none; padding: 6px 12px; border-radius: 999px; font-size: 14px; font-weight: 500;
  color: var(--text-2); text-decoration: none; border: 1px solid transparent;
}
.tabs a:hover { background: var(--surface-2); }
.tabs a.on { background: var(--accent-soft); color: var(--accent); border-color: color-mix(in srgb, var(--accent) 30%, transparent); }

/* ---------- layout ---------- */
main { max-width: 980px; margin: 0 auto; padding: 16px; }
.intro { color: var(--text-2); margin: 4px 0 18px; }
.intro code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px; margin-bottom: 18px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
h2 { font-size: 19px; margin: 0; letter-spacing: -.01em; }
.share {
  flex: none; font: inherit; font-size: 13px; padding: 5px 10px; border-radius: 8px; cursor: pointer;
  background: transparent; color: var(--accent); border: 1px solid var(--border);
}
.share:hover { background: var(--accent-soft); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px 14px; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-2); font-weight: 500; min-width: 0; }
input, select {
  font: inherit; font-size: 15px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px; padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 0; border-color: var(--accent); }
input.bad { border-color: var(--bad); }
.field { display: flex; align-items: stretch; position: relative; }
.field input { padding-right: 46px; }
.field em {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  font-style: normal; color: var(--muted); font-size: 13px; pointer-events: none;
}
.field em.pre { left: 10px; right: auto; }
.field em.pre + input { padding-left: 30px; padding-right: 10px; }
.hint { color: var(--muted); font-size: 13px; margin: 12px 0 0; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; margin: 0 8px 14px 0; flex-wrap: wrap; }
.seg button {
  font: inherit; font-size: 14px; border: 0; background: transparent; color: var(--text-2);
  padding: 6px 12px; border-radius: 8px; cursor: pointer;
}
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .12); font-weight: 600; }
.seg.small button { font-size: 13px; padding: 4px 10px; }

/* ---------- results ---------- */
.results { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; margin-top: 16px; }
.res { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; min-width: 0; }
.res .k { font-size: 12px; color: var(--muted); }
.res .v { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.res .s { font-size: 12px; color: var(--text-2); margin-top: 2px; }
.res.main { background: var(--accent-soft); }
.res.main .v { color: var(--accent); font-size: 24px; }
.msg { grid-column: 1 / -1; padding: 10px 12px; border-radius: 10px; font-size: 14px; border: 1px solid; display: flex; gap: 8px; align-items: flex-start; }
.msg::before { font-weight: 700; }
.msg.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); background: color-mix(in srgb, var(--warn) 8%, transparent); }
.msg.warn::before { content: "⚠"; }
.msg.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); background: color-mix(in srgb, var(--bad) 8%, transparent); }
.msg.bad::before { content: "✕"; }
.msg.ok { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, transparent); background: color-mix(in srgb, var(--good) 8%, transparent); }
.msg.ok::before { content: "✓"; }
table.pairs { grid-column: 1 / -1; border-collapse: collapse; width: 100%; font-size: 14px; font-variant-numeric: tabular-nums; }
table.pairs th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.pairs td { padding: 7px 8px; border-bottom: 1px solid var(--grid); }
table.pairs tr:first-child td { font-weight: 650; }

.schema { width: 100%; max-width: 360px; display: block; margin: 16px auto 0; color: var(--muted); }
.schema .lbl, .chart text { fill: var(--muted); font-size: 12px; font-family: inherit; }

/* ---------- color code ---------- */
.bands { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px 12px; margin-top: 14px; }
.band-sel { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-2); font-weight: 500; }
.band-sel .wrap { position: relative; }
.band-sel .sw { position: absolute; left: 9px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; border-radius: 5px; border: 1px solid rgb(0 0 0 / .25); pointer-events: none; }
.band-sel select { padding-left: 34px; }
.resistor { margin: 18px auto 0; max-width: 520px; }
.resistor svg { width: 100%; height: auto; display: block; }

/* ---------- Bode ---------- */
.bode { position: relative; margin-top: 18px; display: grid; gap: 10px; }
.chart-wrap { min-width: 0; }
.chart-title { font-size: 12px; color: var(--muted); font-weight: 500; margin-bottom: 2px; }
.chart { width: 100%; height: 220px; display: block; overflow: visible; touch-action: pan-y; }
#rc_ph { height: 160px; }
.chart .gridl { stroke: var(--grid); stroke-width: 1; vector-effect: non-scaling-stroke; }
.chart .axis { stroke: var(--border); stroke-width: 1; vector-effect: non-scaling-stroke; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
.chart .fcl { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; vector-effect: non-scaling-stroke; }
.chart .cross { stroke: var(--text-2); stroke-width: 1; vector-effect: non-scaling-stroke; }
.chart .dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; vector-effect: non-scaling-stroke; }
.tip {
  position: absolute; pointer-events: none; background: var(--text); color: var(--surface);
  font-size: 12px; padding: 6px 9px; border-radius: 8px; white-space: nowrap; font-variant-numeric: tabular-nums;
  transform: translate(-50%, -110%); box-shadow: 0 4px 14px rgb(0 0 0 / .2);
}

/* ---------- STM32 ---------- */
.bits-scroll { overflow-x: auto; margin-top: 16px; padding-bottom: 4px; }
.bits { display: grid; grid-template-columns: repeat(32, minmax(22px, 1fr)); gap: 2px; min-width: 760px; }
.bit {
  font: inherit; font-family: var(--mono); font-size: 14px; font-weight: 600; cursor: pointer;
  height: 34px; border: 1px solid var(--border); background: var(--bit-off); color: var(--text-2);
  border-radius: 5px; padding: 0;
}
.bit.on { background: var(--bit-on); color: var(--accent-ink); border-color: var(--bit-on); }
.bit.res-bit { opacity: .35; cursor: not-allowed; }
.bit.nib { margin-left: 6px; }
.bitn { font-family: var(--mono); font-size: 10px; color: var(--muted); text-align: center; }
.bitf { font-size: 10px; color: var(--text-2); text-align: center; border-top: 2px solid var(--accent); padding-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.bitf.alt { border-top-color: var(--muted); }
.bitf.empty { border-top-color: transparent; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 8px 12px; margin-top: 14px; }
.fld { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.fld .n { font-size: 13px; font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.fld .n span { color: var(--muted); font-weight: 400; font-family: var(--mono); font-size: 11px; }
.fld .d { font-size: 12px; color: var(--muted); }
.fld select, .fld input { padding: 5px 8px; font-size: 14px; }
.fld label.chk { flex-direction: row; align-items: center; gap: 8px; font-size: 14px; color: var(--text); cursor: pointer; }
.fld label.chk input { width: 18px; height: 18px; accent-color: var(--accent); }
.code { margin-top: 14px; background: var(--surface-2); border-radius: 10px; padding: 10px 12px; display: flex; align-items: center; gap: 10px; }
.code code { flex: 1; font-size: 14px; overflow-x: auto; white-space: pre; min-width: 0; }
.copy, .ghost {
  font: inherit; font-size: 13px; padding: 5px 10px; border-radius: 8px; cursor: pointer;
  background: var(--surface); color: var(--text-2); border: 1px solid var(--border);
}
.copy:hover, .ghost:hover { color: var(--accent); border-color: var(--accent); }
.row-btns { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

[hidden] { display: none !important; }

footer { max-width: 980px; margin: 0 auto; padding: 8px 16px 40px; color: var(--muted); font-size: 13px; text-align: center; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0;
  background: var(--text); color: var(--surface); padding: 8px 14px; border-radius: 10px; font-size: 14px;
  transition: .2s; pointer-events: none; z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 560px) {
  .card { padding: 14px; }
  .grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .results { grid-template-columns: 1fr 1fr; }
  .res .v { font-size: 17px; }
  .res.main .v { font-size: 20px; }
  .card-head { align-items: flex-start; }
  .chart { height: 180px; }
}
@media (max-width: 380px) {
  .grid, .results { grid-template-columns: 1fr; }
}

/* ==================== v2 : accueil, icônes, schémas, simulations ==================== */
.hero {
  margin: 8px 0 18px; padding: 22px 20px; border-radius: var(--radius);
  background:
    radial-gradient(120% 140% at 100% 0%, color-mix(in srgb, var(--accent) 18%, transparent), transparent 60%),
    linear-gradient(180deg, var(--surface), var(--surface));
  border: 1px solid var(--border);
}
.hero h1 { margin: 0 0 6px; font-size: clamp(22px, 4.5vw, 30px); letter-spacing: -.02em; }
.hero p { margin: 0; color: var(--text-2); max-width: 70ch; }
.hero code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 13px; }

.hub { display: grid; gap: 14px; margin-bottom: 22px; }
.hub-group h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 600; }
.hub-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 8px; }
.tile {
  display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--border); color: var(--text); text-decoration: none;
  transition: border-color .15s, transform .15s, box-shadow .15s; min-width: 0;
}
.tile:hover { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); transform: translateY(-1px); box-shadow: var(--shadow); }
.tile .tt { display: flex; flex-direction: column; min-width: 0; }
.tile b { font-size: 14px; font-weight: 600; }
.tile small { color: var(--muted); font-size: 12px; line-height: 1.35; }
.ic {
  flex: none; width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
}
.ic svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
h2 { display: flex; align-items: center; gap: 10px; }
h2 .ic { width: 30px; height: 30px; }
h2 .ic svg { width: 18px; height: 18px; }
h3.sub { font-size: 15px; margin: 22px 0 10px; padding-top: 16px; border-top: 1px solid var(--border); }

/* Schémas */
.sch { margin: 16px auto 0; max-width: 440px; color: var(--text-2); }
.sch:empty { display: none; }
.sch svg { width: 100%; height: auto; display: block; overflow: visible; }
.sch svg path, .sch svg line, .sch svg rect, .sch svg circle { stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sch svg .nofill, .sch svg path { fill: none; }
.sch svg .body { fill: var(--surface); }
.sch svg .thick { stroke-width: 3; }
.sch svg .thin { stroke-width: 1.3; }
.sch svg .dot { fill: currentColor; stroke: none; }
.sch svg .term { fill: var(--surface); }
.sch svg .arrow { stroke: var(--accent); }
.sch svg .dash path:first-child, .sch svg .dash rect { stroke-dasharray: 4 3; }
.sch svg text { font-size: 12px; fill: var(--muted); stroke: none; font-family: inherit; }
.sch svg text .nm, .sch svg text.nm { fill: var(--text); font-weight: 600; }
.sch svg text .val { fill: var(--accent); font-weight: 600; font-variant-numeric: tabular-nums; }
.sch svg text.pin { font-size: 10px; font-family: var(--mono); }
.formula {
  margin: 14px 0 0; font-size: 13px; color: var(--text-2); font-family: var(--mono);
  background: var(--surface-2); border-radius: 8px; padding: 8px 10px; overflow-wrap: anywhere;
}
.formula:empty { display: none; }

/* Simulations */
.chart-wrap { position: relative; }
.chart-wrap.sim { margin-top: 12px; }
.chart.small { height: 140px; }
#rc_time { height: 200px; }
.chart .line.in { stroke: var(--muted); stroke-dasharray: 5 4; }
.chart .dot.in { fill: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-2); margin-bottom: 4px; }
.lg { display: inline-flex; align-items: center; gap: 6px; }
.lg::before { content: ""; width: 18px; height: 0; border-top: 2px solid var(--accent); }
.lg.in::before { border-top: 2px dashed var(--muted); }
.sim-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: 16px; align-items: center; }
.sim-grid .sch { margin-top: 12px; }
@media (max-width: 760px) { .sim-grid { grid-template-columns: 1fr; } }

/* Jauge de chute de tension */
.meter-wrap { margin-top: 16px; }
.meter-wrap:empty { display: none; }
.meter { position: relative; height: 12px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.meter span { position: absolute; inset: 0 auto 0 0; background: var(--good); border-radius: 99px; transition: width .2s; }
.meter.over span { background: var(--bad); }
.meter i { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--text-2); }
.meter-lbl { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 4px; }

@media (max-width: 560px) {
  .hero { padding: 16px; }
  .hub-grid { grid-template-columns: 1fr 1fr; }
  .tile { padding: 8px 10px; gap: 8px; }
  .tile small { display: none; }
  .ic { width: 30px; height: 30px; }
}
