@layer tokens {
  :root {
    --panel: oklch(27% 0.025 255);
    --panel-low: oklch(20% 0.025 255);
    --panel-rim: oklch(40% 0.02 255);
    --panel-shadow: 0 2rem 4rem -1.5rem oklch(0% 0 0 / .8);
    --slot: oklch(12% 0.01 255);
    --paddle-hi: oklch(97% 0.01 90);
    --paddle-lo: oklch(78% 0.02 90);
    --lamp-off: oklch(32% 0.07 25);
    --lamp-on: oklch(70% 0.23 28);
    --phosphor: oklch(84% 0.16 75);
    --phosphor-dim: oklch(55% 0.1 75);
    --screen-hi: oklch(21% 0.035 70);
    --screen-lo: oklch(9% 0.02 70);
    --silk: oklch(86% 0.01 250);

    --slot-size: 3.25rem;
    --glyph-size: clamp(5.5rem, 26cqi, 10rem);
  }

  @counter-style cp437 {
    system: fixed 0;
    symbols:
      "␀" "☺\FE0E" "☻\FE0E" "♥\FE0E" "♦\FE0E" "♣\FE0E" "♠\FE0E" "•" "◘" "○" "◙" "♂\FE0E" "♀\FE0E" "♪\FE0E" "♫\FE0E" "☼\FE0E"
      "►\FE0E" "◄\FE0E" "↕\FE0E" "‼\FE0E" "¶\FE0E" "§\FE0E" "▬" "↨\FE0E" "↑\FE0E" "↓\FE0E" "→\FE0E" "←\FE0E" "∟" "↔\FE0E" "▲\FE0E" "▼\FE0E"
      "␠" "!" '"' "#" "$" "%" "&" "'" "(" ")" "*" "+" "," "-" "." "/"
      "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" ":" ";" "<" "=" ">" "?"
      "@" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O"
      "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "[" "\\" "]" "^" "_"
      "`" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o"
      "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "{" "|" "}" "~" "⌂"
      "Ç" "ü" "é" "â" "ä" "à" "å" "ç" "ê" "ë" "è" "ï" "î" "ì" "Ä" "Å"
      "É" "æ" "Æ" "ô" "ö" "ò" "û" "ù" "ÿ" "Ö" "Ü" "¢" "£" "¥" "₧" "ƒ"
      "á" "í" "ó" "ú" "ñ" "Ñ" "ª" "º" "¿" "⌐" "¬" "½" "¼" "¡" "«" "»"
      "░" "▒" "▓" "│" "┤" "╡" "╢" "╖" "╕" "╣" "║" "╗" "╝" "╜" "╛" "┐"
      "└" "┴" "┬" "├" "─" "┼" "╞" "╟" "╚" "╔" "╩" "╦" "╠" "═" "╬" "╧"
      "╨" "╤" "╥" "╙" "╘" "╒" "╓" "╫" "╪" "┘" "┌" "█" "▄" "▌" "▐" "▀"
      "α" "ß" "Γ" "π" "Σ" "σ" "µ" "τ" "Φ" "Θ" "Ω" "δ" "∞" "φ" "ε" "∩"
      "≡" "±" "≥" "≤" "⌠" "⌡" "÷" "≈" "°" "∙" "·" "√" "ⁿ" "²" "■" "⍽";
  }

  @counter-style dec3 {
    system: extends decimal;
    pad: 3 "0";
  }

  @counter-style hex2 {
    system: numeric;
    symbols: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F";
    pad: 2 "0";
  }

  @counter-style bin8 {
    system: numeric;
    symbols: "0" "1";
    pad: 8 "0";
  }
}

@layer components {
  .stage {
    container-type: inline-size;
  }

  .panel {
    counter-reset: byte;
    display: grid;
    grid-template-areas:
      "screen"
      "switches"
      "plate";
    gap: var(--space-l);
    inline-size: min(100%, 40rem);
    padding: clamp(var(--space-s), 4vw, var(--space-xl));
    border: 1px solid var(--panel-rim);
    border-radius: var(--radius-l);
    background: linear-gradient(var(--panel), var(--panel-low));
    box-shadow: var(--panel-shadow);
  }

  .switches {
    grid-area: switches;
    display: grid;
    grid-template-columns: repeat(4, 1fr) var(--space-xs) repeat(4, 1fr);
    column-gap: clamp(var(--space-3xs), 1.2cqi, var(--space-xs));
  }

  .switch {
    display: grid;
    justify-items: center;
    gap: var(--space-xs);
    padding-block: var(--space-xs);
    border-radius: var(--radius-s);
    cursor: pointer;

    &:nth-child(5) {
      grid-column-start: 6;
    }

    &:has(:focus-visible) {
      outline: 2px solid var(--color-accent);
      outline-offset: 2px;
    }

    &:has(:checked) {
      counter-increment: byte pow(2, sibling-count() - sibling-index());
    }
  }

  @supports not (counter-increment: byte pow(2, sibling-index())) {
    :where(.switch:nth-child(1)):has(:checked) { counter-increment: byte 128; }
    :where(.switch:nth-child(2)):has(:checked) { counter-increment: byte 64; }
    :where(.switch:nth-child(3)):has(:checked) { counter-increment: byte 32; }
    :where(.switch:nth-child(4)):has(:checked) { counter-increment: byte 16; }
    :where(.switch:nth-child(5)):has(:checked) { counter-increment: byte 8; }
    :where(.switch:nth-child(6)):has(:checked) { counter-increment: byte 4; }
    :where(.switch:nth-child(7)):has(:checked) { counter-increment: byte 2; }
    :where(.switch:nth-child(8)):has(:checked) { counter-increment: byte 1; }
  }

  .lamp {
    inline-size: .8rem;
    aspect-ratio: 1;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 30%, color-mix(in oklch, var(--lamp-off), white 15%), var(--lamp-off) 60%);
    box-shadow: inset 0 0 0 1px oklch(0% 0 0 / .5);

    .switch:has(:checked) & {
      background: radial-gradient(circle at 35% 30%, color-mix(in oklch, var(--lamp-on), white 55%), var(--lamp-on) 60%);
      box-shadow: 0 0 .75rem var(--lamp-on);
    }
  }

  .paddle {
    position: relative;
    inline-size: clamp(1.25rem, 5cqi, 1.75rem);
    block-size: var(--slot-size);
    border-radius: var(--radius-s);
    background: var(--slot);
    box-shadow: inset 0 2px 4px oklch(0% 0 0 / .8), 0 1px 0 var(--panel-rim);

    &::before {
      content: "";
      position: absolute;
      inset-inline: 12%;
      inset-block-end: 6%;
      block-size: 52%;
      border-radius: var(--radius-s);
      background: linear-gradient(var(--paddle-hi), var(--paddle-lo));
      box-shadow: 0 3px 4px oklch(0% 0 0 / .6);
    }

    .switch:has(:checked) &::before {
      translate: 0 calc(var(--slot-size) * -.36);
      background: linear-gradient(var(--paddle-lo), var(--paddle-hi));
      box-shadow: 0 -3px 4px oklch(0% 0 0 / .6);
    }

    @media (prefers-reduced-motion: no-preference) {
      &::before {
        transition: translate var(--duration-quick) var(--ease-out);
      }
    }
  }

  .weight {
    color: var(--silk);
    font-family: var(--font-mono);
    font-size: var(--step--1);
    font-variant-numeric: tabular-nums;
  }

  .screen {
    grid-area: screen;
    position: relative;
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "glyph dec"
      "glyph hex"
      "glyph bin";
    align-items: center;
    column-gap: var(--space-l);
    padding: var(--space-m) var(--space-l);
    overflow: clip;
    border-radius: var(--radius-m);
    background: radial-gradient(ellipse at 40% 45%, var(--screen-hi), var(--screen-lo) 75%);
    box-shadow: inset 0 0 0 3px var(--panel-low), inset 0 0 2rem oklch(0% 0 0 / .9);
    color: var(--phosphor);
    font-family: var(--font-mono);
    text-shadow: 0 0 .3em color-mix(in oklch, var(--phosphor) 70%, transparent);

    &::after {
      content: "";
      position: absolute;
      inset: 0;
      background: repeating-linear-gradient(oklch(0% 0 0 / .28) 0 1px, transparent 1px 3px);
      pointer-events: none;
    }
  }

  .glyph {
    grid-area: glyph;
    display: grid;
    place-items: center;
    min-block-size: calc(var(--glyph-size) * 1.15);
    font-size: var(--glyph-size);
    line-height: 1;

    &::before {
      content: counter(byte, cp437);
    }
  }

  .reading {
    display: flex;
    justify-content: space-between;
    gap: var(--space-s);
    font-size: var(--step-0);
    font-variant-numeric: tabular-nums;
  }

  .key {
    color: var(--phosphor-dim);
  }

  .reading:nth-child(2) {
    grid-area: dec;
  }

  .reading:nth-child(3) {
    grid-area: hex;
  }

  .reading:nth-child(4) {
    grid-area: bin;
  }

  .dec::before {
    content: counter(byte, dec3);
  }

  .hex::before {
    content: counter(byte, hex2);
  }

  .bin::before {
    content: counter(byte, bin8);
  }

  .plate {
    grid-area: plate;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-s);
  }

  .maker {
    color: var(--silk);
    font-size: var(--step--1);
    font-weight: 700;
    letter-spacing: .3em;
  }

  .clear {
    min-block-size: var(--size-target-min);
    padding: var(--space-2xs) var(--space-s);
    border: 1px solid var(--panel-rim);
    border-radius: var(--radius-full);
    background: var(--panel-low);
    color: var(--silk);
    font-size: var(--step--1);
    cursor: pointer;

    @media (hover: hover) {
      &:hover {
        border-color: var(--silk);
      }
    }
  }

  @container (inline-size < 30rem) {
    .screen {
      grid-template-columns: repeat(3, 1fr);
      grid-template-areas:
        "glyph glyph glyph"
        "dec hex bin";
      row-gap: var(--space-xs);
    }

    .reading {
      flex-direction: column;
      align-items: center;
      gap: 0;
      font-size: var(--step--1);
    }
  }

  @media (forced-colors: active) {
    .switch:has(:checked) .lamp {
      background: Highlight;
    }
  }
}
