:root {
  --case: #d6d3cc;
  --key: #bfbcb4;
  --ink: #23265a;
  --ink-soft: #55587a;
  --bezel: #5f6072;
  --lcd: #b3c28a;
  --lcd-grid: rgb(44 58 29 / 0.07);
  --lcd-ink: #2c3a1d;
  --accent: #a3245c;
  --on-accent: #ffffff;

  --serif: "STIX Two Text", "Times New Roman", serif;
  --pixel: "Galmuri11", ui-monospace, monospace;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --case: #1f2029;
    --key: #353644;
    --ink: #dcdaf0;
    --ink-soft: #a2a4bf;
    --bezel: #3b3c4b;
    --accent: #d9558f;
    --on-accent: #1f2029;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--case);
  color: var(--ink);
  font: 1.125rem/1.6 var(--serif);
}

main {
  max-width: 46rem;
  margin-inline: auto;
  padding: 3.5rem 1.25rem 2.5rem;
}

h1 {
  margin: 0;
  font-size: clamp(2.5rem, 8vw, 4.25rem);
  font-weight: 600;
  line-height: 1.05;
}

h2 {
  margin: 0 0 0.75rem;
  font-size: 1.625rem;
  font-weight: 600;
  line-height: 1.2;
}

.tagline {
  margin: 0.5rem 0 2.25rem;
  color: var(--ink-soft);
  font-size: 1.375rem;
  font-style: italic;
}

a {
  color: inherit;
  text-underline-offset: 0.15em;
}

var {
  font-style: italic;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* The device: a handheld-style screen bezel around a dot-matrix LCD. */

.bezel {
  padding: clamp(0.875rem, 4vw, 1.75rem);
  border-radius: 10px 10px 48px 10px;
  background: var(--bezel);
}

.bezel--small {
  padding: 0.75rem;
  border-radius: 8px 8px 28px 8px;
}

.lcd {
  padding: 1rem 0.75rem;
  border-radius: 3px;
  background-color: var(--lcd);
  background-image:
    linear-gradient(var(--lcd-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--lcd-grid) 1px, transparent 1px);
  background-size: 4px 4px;
  box-shadow: inset 0 3px 0 rgb(0 0 0 / 0.12);
  color: var(--lcd-ink);
  font-family: var(--pixel);
}

.readout {
  --sprite: 96px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: center;
  margin: 0;
  font-size: 48px;
  line-height: 1;
}

.readout--date {
  --sprite: 64px;
  min-height: calc(var(--sprite) * 0.8);
  font-size: 24px;
}

.readout--time {
  --sprite: clamp(64px, 20vw, 168px);
  min-height: calc(var(--sprite) * 0.8 + 0.5rem);
  font-size: clamp(40px, 13vw, 104px);
}

.readout--convert {
  min-height: calc(var(--sprite) * 0.8 + 0.5rem);
}

.readout:not([data-system="pokemon"]) {
  align-items: center;
  padding-block: 0.5rem;
}

.readout--time[data-system="2"] {
  font-size: clamp(18px, 4.6vw, 40px);
}

.readout--date[data-system="2"] {
  font-size: 18px;
}

@media (max-width: 30rem) {
  /* Four sprites and two separators overflow a phone-width row at 64px. */
  .readout--date {
    --sprite: 52px;
  }
}

.readout--convert:not([data-system="pokemon"]) {
  overflow-wrap: anywhere;
}

.numeral {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: center;
  column-gap: 0.25rem;
}

.separator {
  align-self: center;
  padding-inline: 0.15em;
}

.readout[data-system="pokemon"] .separator {
  /* Center on the artwork of a single row of sprites. */
  align-self: flex-start;
  margin-top: calc(var(--sprite) * 0.42 - 0.5em);
  font-size: calc(var(--sprite) * 0.35);
}

.digit {
  position: relative;
  margin: 0;
}

.digit img {
  display: block;
  width: var(--sprite);
  height: var(--sprite);
  /* PokéAPI sprites sit in a padded 96px canvas; trim it so rows hug the artwork. */
  margin-block: calc(var(--sprite) * -0.08) calc(var(--sprite) * -0.12);
  image-rendering: pixelated;
}

.negative img {
  transform: scaleY(-1);
}

/* Labels stay out of the way until a sprite is hovered, or tapped on touch screens. */
.label {
  position: absolute;
  z-index: 1;
  top: 100%;
  left: 50%;
  display: flex;
  gap: 0.4em;
  padding: 4px 6px 3px;
  border-radius: 2px;
  background: var(--lcd-ink);
  color: var(--lcd);
  font-size: 12px;
  line-height: 1.2;
  white-space: nowrap;
  transform: translateX(-50%);
  visibility: hidden;
}

@media (hover: hover) {
  .digit:hover .label {
    visibility: visible;
  }
}

.digit.show-label .label {
  visibility: visible;
}

.dex {
  opacity: 0.7;
}

/* Hardcore mode: silhouettes only, with no labels or formula to give it away. */
body:has(#hardcore:checked) .digit img {
  filter: brightness(0) opacity(0.8);
}

body:has(#hardcore:checked) :is(.label, .formula) {
  visibility: hidden;
}

/* Clock colons blink off for the second half of each second. */
@media (prefers-reduced-motion: no-preference) {
  .readout--time.colons-off .separator {
    visibility: hidden;
  }
}

/* Keys */

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1.5rem;
  margin-top: 1.25rem;
}

.keys {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0;
  padding: 0;
  border: 0;
}

.key {
  position: relative;
}

.key input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.key span,
.key select,
a.key {
  display: block;
  padding: 0.6rem 0.95rem 0.5rem;
  border-radius: 999px;
  background: var(--key);
  box-shadow: inset 0 -3px 0 rgb(0 0 0 / 0.16);
  font: 12px/1 var(--pixel);
  text-decoration: none;
  cursor: pointer;
  user-select: none;
}

.key select {
  appearance: none;
  padding-right: 2rem;
  border: 0;
  color: inherit;
}

/* appearance: none drops the native arrow, so the wrapper draws one. */
.select::after {
  content: "▾";
  position: absolute;
  top: 50%;
  right: 0.9rem;
  font: 12px/1 var(--pixel);
  transform: translateY(-50%);
  pointer-events: none;
}

.key input:checked + span {
  background: var(--accent);
  box-shadow: inset 0 2px 0 rgb(0 0 0 / 0.22);
  color: var(--on-accent);
}

@media (forced-colors: active) {
  .key input:checked + span {
    outline: 3px solid Highlight;
  }
}

.key input:focus-visible + span,
.key select:focus-visible,
a.key:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* Converter */

.convert,
.definition {
  margin-top: 4rem;
}

#number {
  display: block;
  width: 100%;
  margin-bottom: 0.875rem;
  padding: 0.65rem 0.85rem 0.55rem;
  border: 2px solid var(--bezel);
  border-radius: 6px;
  background: var(--key);
  color: var(--ink);
  font: 24px/1.2 var(--pixel);
}

#number:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.note {
  min-height: 1.6em;
  margin: 0.75rem 0 0;
  color: var(--ink-soft);
  overflow-wrap: anywhere;
}

.formula sup {
  font-size: 0.65em;
}

/* Definition */

.definition p {
  max-width: 36em;
  margin: 0 0 0.9rem;
}

footer {
  margin-top: 4rem;
  padding-top: 1.25rem;
  border-top: 1px solid color-mix(in srgb, var(--ink) 20%, transparent);
  color: var(--ink-soft);
  font-size: 0.9375rem;
}

footer p {
  margin: 0 0 0.5rem;
}

/* Clock only: #clock in the address shows just the device, scaled up like a desk clock. */

body:has(#clock:target) main {
  --device: min(92vw, 160dvh);
  display: grid;
  place-content: center;
  justify-items: center;
  gap: calc(var(--device) * 0.02);
  min-height: 100dvh;
  max-width: none;
  padding: 0;
}

body:has(#clock:target) main > :not(#clock, header),
body:has(#clock:target) :is(.tagline, .controls) {
  display: none;
}

/* The title moves under the screen, lined up with the LCD, where a handheld prints its name. */
body:has(#clock:target) header {
  order: 1;
  justify-self: start;
  padding-left: calc(var(--device) * 0.05);
}

body:has(#clock:target) h1 {
  font-size: max(14px, calc(var(--device) * 0.028));
}

body:has(#clock:target) .device {
  container-type: inline-size;
  width: var(--device);
}

body:has(#clock:target) .bezel {
  padding: 4cqi 5cqi;
  border-radius: 1.5cqi 1.5cqi 9cqi 1.5cqi;
}

body:has(#clock:target) .lcd {
  display: flex;
  flex-direction: column;
  gap: 1cqi;
  padding: 3.5cqi 3cqi;
  border-radius: 0.5cqi;
}

/* Text systems scale with the sprites; binary needs more room per value. */
body:has(#clock:target) .readout {
  padding-block: 0;
  font-size: calc(var(--sprite) * 0.6);
}

body:has(#clock:target) .readout[data-system="2"] {
  font-size: calc(var(--sprite) * 0.25);
}

body:has(#clock:target) .numeral {
  column-gap: 0.5cqi;
}

/* Sprites sit in padded canvases; trim the sides as well, so the separators set the rhythm. */
body:has(#clock:target) .digit img {
  margin-inline: calc(var(--sprite) * -0.14);
}

body:has(#clock:target) .readout[data-system="pokemon"] .separator {
  padding-inline: calc(var(--sprite) * 0.04);
  font-size: calc(var(--sprite) * 0.5);
}

body:has(#clock:target) .readout--date {
  --sprite: max(9cqi, 44px);
}

body:has(#clock:target) .readout--date[data-system="pokemon"] .separator {
  padding-inline: calc(var(--sprite) * 0.2);
}

body:has(#clock:target) .readout--time {
  --sprite: 30cqi;
  font-size: calc(var(--sprite) * 0.48);
}

body:has(#clock:target) .readout--time[data-system="2"] {
  font-size: calc(var(--sprite) * 0.2);
}
