:root {
  --bg: #f3efe6; --paper: #fffdf8; --ink: #1f2328; --muted: #6b6f76; --line: #d9d3c5; --dot: #d6cfbf;
  --accent: #e4572e; --accent-ink: #fff; --good: #2f9e6a; --bad: #d63b3b; --shade: rgba(31, 35, 40, .45);
  --hand: 'Kalam', cursive; --mono: 'JetBrains Mono', monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171c; --paper: #1e2229; --ink: #ebe7de; --muted: #9aa0a8; --line: #343a43; --dot: #2b3038;
    --accent: #ff7a4d; --accent-ink: #15171c; --good: #4cc48d; --bad: #ff6b6b; --shade: rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171c; --paper: #1e2229; --ink: #ebe7de; --muted: #9aa0a8; --line: #343a43; --dot: #2b3038;
  --accent: #ff7a4d; --accent-ink: #15171c; --good: #4cc48d; --bad: #ff6b6b; --shade: rgba(0, 0, 0, .6);
  color-scheme: dark;
}

* { box-sizing: border-box; }
body { margin: 0; height: 100dvh; display: grid; grid-template-rows: auto 1fr; background: var(--bg); color: var(--ink); font: 15px/1.45 'Atkinson Hyperlegible', system-ui, sans-serif; }
h2, h3 { margin: 0 0 6px; line-height: 1.15; }
h2 { font-size: 22px; font-weight: 700; }
p { margin: 0 0 10px; }
button { font: inherit; color: inherit; cursor: pointer; border: 1.5px solid var(--ink); background: var(--paper); border-radius: 10px; padding: 8px 14px; }
button:disabled { opacity: .45; cursor: default; }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; box-shadow: 0 3px 0 var(--ink); }
.primary:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.ghost { border-color: var(--line); }
.danger { border-color: var(--bad); color: var(--bad); width: 100%; margin-top: 10px; }
.icon { width: 40px; height: 40px; padding: 0; border-radius: 50%; border-color: var(--line); }
.eyebrow { font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.muted { color: var(--muted); }

/* header */
.bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; border-bottom: 1.5px solid var(--line); background: var(--paper); }
.brand { display: flex; align-items: center; gap: 8px; font: 700 22px var(--hand); }
.brand svg { width: 30px; height: 30px; fill: var(--ink); }
.brand .base { fill: var(--accent); }
.stats { display: flex; gap: 22px; }
.stats div { display: grid; }
.stats small { font-size: 10px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.stats b { font: 700 17px var(--mono); }
.stats b small { font-size: 12px; letter-spacing: 0; text-transform: none; }
.meter { display: block; height: 3px; background: var(--line); border-radius: 2px; margin-top: 2px; }
.meter i { display: block; height: 100%; width: 0; background: var(--good); border-radius: 2px; transition: width .3s; }
.meter i.bad { background: var(--bad); }
.actions { display: flex; gap: 10px; }

/* board */
main { position: relative; overflow: hidden; background: radial-gradient(var(--dot) 1.2px, transparent 1.4px) 0 0 / 22px 22px; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; user-select: none; }
.node { cursor: grab; }
.node .shape { fill: var(--paper); stroke: var(--ink); stroke-width: 2.5; stroke-linejoin: round; }
.node .pad { fill: transparent; }
.node .stick { fill: none; stroke: var(--ink); stroke-width: 2.5; stroke-linecap: round; }
.node.sel .shape { stroke: var(--accent); stroke-width: 3.5; }
.node:focus { outline: none; }
.node:focus-visible .shape, .node.users:focus-visible .pad { stroke: var(--accent); stroke-width: 3.5; stroke-dasharray: 8 5; }
.node.over .shape { stroke: var(--bad); }
.node.over .mono { fill: var(--bad); font-weight: 700; }
.node text { fill: var(--ink); }
.node .title { font: 700 21px var(--hand); }
.node .sub { font-size: 12.5px; fill: var(--muted); }
.node .mono { font: 12.5px var(--mono); }
.node .mid { text-anchor: middle; }
.node .in { fill: var(--paper); stroke: var(--muted); stroke-width: 2; }
.port { fill: var(--accent); stroke: var(--paper); stroke-width: 3; cursor: crosshair; transition: r .15s; }
.port:hover { r: 11; }
.ring { fill: none; stroke: var(--bad); stroke-width: 3; stroke-dasharray: 10 7; animation: spin 1.2s linear infinite; }
.edge { cursor: pointer; }
.edge .hit { fill: none; stroke: transparent; stroke-width: 16; }
.wire { fill: none; stroke: var(--ink); stroke-width: 2.2; }
.edge:hover .wire { stroke: var(--bad); stroke-dasharray: 6 5; }
.wire.temp { stroke: var(--accent); stroke-dasharray: 6 5; pointer-events: none; }
.head { fill: none; stroke: var(--ink); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.running .wire { stroke: var(--accent); stroke-dasharray: 3 9; stroke-width: 3; animation: flow .5s linear infinite; }
.running #run { animation: pulse .6s ease-in-out infinite alternate; }
@keyframes flow { to { stroke-dashoffset: -12; } }
@keyframes spin { to { stroke-dashoffset: -34; } }
@keyframes pulse { to { transform: scale(.96); } }

/* cards */
.card { position: absolute; top: 16px; background: var(--paper); border: 1.5px solid var(--line); border-radius: 14px; padding: 16px; box-shadow: 0 6px 24px rgba(0, 0, 0, .08); max-height: calc(100% - 32px); overflow: auto; }
.left { left: 16px; width: 300px; }
.right { right: 16px; width: 240px; }
.rounds { display: flex; gap: 6px; margin-bottom: 14px; }
.rounds button { flex: 1; padding: 4px 0; border-color: var(--line); border-radius: 8px; font-weight: 700; display: grid; line-height: 1.1; }
.rounds button small { font-size: 9px; color: var(--accent); min-height: 10px; }
.rounds .on { border-color: var(--ink); background: var(--ink); color: var(--paper); }
.rounds .fold { flex: 0 0 30px; }
.left.min > :not(.rounds):not(.result) { display: none; }
.left.min .rounds { margin-bottom: 0; }
.left.min .result { margin-top: 12px; }
#brief ul { margin: 0 0 12px; padding-left: 18px; }
.tip { padding: 10px; border-radius: 10px; background: var(--bg); }
.tip b { color: var(--accent); margin-right: 4px; }
.result { border-top: 1.5px dashed var(--line); padding-top: 12px; }
.result > b { font-size: 16px; display: block; margin-bottom: 4px; }
.fail > b { color: var(--bad); }
.pass > b { color: var(--good); }
dl { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; margin: 8px 0; font-family: var(--mono); font-size: 13px; }
dt { color: var(--muted); }
dd { margin: 0; text-align: right; font-weight: 700; }
#palette { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.comp { position: relative; display: grid; padding: 10px 6px; text-align: center; }
.comp b { font: 700 16px var(--hand); }
.comp small { font: 11px var(--mono); color: var(--muted); }
.comp i { position: absolute; top: -8px; right: -6px; background: var(--accent); color: var(--accent-ink); font: 700 9px 'Atkinson Hyperlegible'; text-transform: uppercase; padding: 2px 6px; border-radius: 6px; }
#clear { width: 100%; }
#inspector { border-top: 1.5px dashed var(--line); margin-top: 14px; padding-top: 14px; }
#inspector p:not(.eyebrow) { color: var(--muted); }
.size { width: 100%; display: grid; grid-template-columns: 1fr auto; text-align: left; margin-bottom: 6px; border-color: var(--line); }
.size small { font: 11px var(--mono); color: var(--muted); }
.size.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--paper)); }
.link { display: flex; align-items: center; justify-content: space-between; padding: 4px 0 4px 10px; margin-bottom: 6px; border: 1.5px solid var(--line); border-radius: 10px; font-weight: 700; }
.link .icon { width: 30px; height: 30px; border: 0; }
#inspector select { width: 100%; font: inherit; color: var(--ink); background: var(--paper); border: 1.5px solid var(--ink); border-radius: 10px; padding: 8px 10px; }

/* modal & toast */
#modal { position: fixed; inset: 0; background: var(--shade); display: grid; place-items: center; padding: 16px; z-index: 10; }
#modal[hidden] { display: none; }
.sheet { background: var(--paper); border: 2px solid var(--ink); border-radius: 16px; padding: 24px; width: min(480px, 100%); box-shadow: 6px 6px 0 var(--ink); animation: pop .2s ease-out; }
.sheet .row { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0; }
.tiles div { border: 1.5px solid var(--line); border-radius: 10px; padding: 10px; display: grid; }
.tiles small { font-size: 10px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.tiles b { font: 700 20px var(--mono); }
.tiles span { font-size: 11px; color: var(--muted); }
.stars { font-size: 34px; color: var(--accent); letter-spacing: 4px; margin: 4px 0; }
@keyframes pop { from { transform: scale(.94); opacity: 0; } }
#toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: 10px; opacity: 0; transition: .2s; pointer-events: none; z-index: 20; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 760px) {
  .bar { flex-wrap: wrap; }
  .stats { order: 3; width: 100%; justify-content: space-between; gap: 8px; }
  .brand span { display: none; }
  .left { left: 16px; right: 16px; width: auto; max-height: 38%; }
  .right { top: auto; bottom: 16px; left: 16px; width: auto; max-height: 45%; }
  #palette { grid-template-columns: repeat(3, 1fr); }
}
