/*
 * An engineer's computation pad: pale green squared paper in light mode,
 * a slate board in dark mode. The sheet is 16 squares wide; each key is
 * 4 x 4 squares, so the fine grid and the key lines always coincide.
 *
 * The rule under the number follows the bookkeeping convention: a single
 * rule under a subtotal (an operator is waiting), a double rule under a
 * total (after =), nothing while a number is being typed.
 */

:root {
  --paper: #e8f0e1;
  --ink: #1e2b26;
  --pencil: #56685e;
  --grid: #d2e0c9;
  --rule: #9db896;
  --accent: #2846a6;
  --accent-pressed: #1d3580;
  --accent-ink: #f3f7ef;
  --wash: rgb(40 70 166 / 0.13);
  --hover: rgb(40 70 166 / 0.06);
  --error: #b42318;

  --face: "Avenir Next Condensed", "Bahnschrift Condensed", "Bahnschrift", "Roboto Condensed",
    "sans-serif-condensed", "Arial Narrow", system-ui, sans-serif;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #16211c;
    --ink: #e6eee2;
    --pencil: #8fa497;
    --grid: #223029;
    --rule: #3b5045;
    --accent: #9eb4ff;
    --accent-pressed: #c3d0ff;
    --accent-ink: #16211c;
    --wash: rgb(158 180 255 / 0.16);
    --hover: rgb(158 180 255 / 0.07);
    --error: #ff8a7a;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--face);
  font-stretch: condensed;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* The sheet is 7 keys tall (2 for the display, 5 for the keypad) and 4 wide,
   so it fits whichever of the window's width or height runs out first. */
.sheet {
  container-type: inline-size;
  width: min(26rem, 100%, calc((100vh - 2rem) * 4 / 7));
  width: min(26rem, 100%, calc((100dvh - 2rem) * 4 / 7));
  /* Every square and key draws only its own right and bottom edges, so the
     sheet frames just its left and top, and no line is ever drawn twice. */
  border-left: 1px solid var(--rule);
  border-top: 1px solid var(--rule);
  --cell: 6.25cqw; /* one square: 1/16 of the sheet */
}

.display,
.keys {
  background-image: linear-gradient(to left, var(--grid) 1px, transparent 1px),
    linear-gradient(to top, var(--grid) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
}

/* Display: 16 x 8 squares. The number sits on the line one square up. */
.display {
  height: calc(var(--cell) * 8);
  padding: var(--cell);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
  box-shadow: inset -1px -1px 0 var(--rule);
}

.expression,
.figure {
  margin: 0;
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
}

.expression span,
.figure span {
  white-space: nowrap;
}

.expression {
  min-height: var(--cell);
  font-size: calc(var(--cell) * 0.9);
  line-height: var(--cell);
  color: var(--pencil);
}

.figure {
  font-size: calc(var(--cell) * 2.75);
  font-weight: 500;
  line-height: 1;
  font-variant-numeric: lining-nums tabular-nums;
  letter-spacing: -0.01em;
}

.figure span {
  position: relative;
  padding-bottom: calc(var(--cell) * 0.2);
}

.figure span::after {
  content: "";
  position: absolute;
  inset: auto 0 -1px 0;
  transform-origin: right;
}

.figure[data-status="pending"] span::after {
  border-bottom: 2px solid var(--ink);
}

.figure[data-status="result"] span::after {
  height: 7px;
  border-block: 2px solid var(--accent);
  animation: draw-rule 240ms cubic-bezier(0.2, 0.7, 0.2, 1);
}

.figure[data-status="error"] {
  color: var(--error);
}

@keyframes draw-rule {
  from {
    transform: scaleX(0);
  }
}

/* Keypad: 4 x 5 keys, each 4 x 4 squares, with a heavier line between keys. */
.keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: calc(var(--cell) * 4);
  user-select: none;
  -webkit-user-select: none;
}

.key {
  --fill: transparent;
  appearance: none;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--fill);
  box-shadow: inset -1px -1px 0 var(--rule);
  color: var(--ink);
  font: inherit;
  font-size: calc(var(--cell) * 1.6);
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.key-wide {
  grid-column: span 2;
}

.key-point {
  font-size: calc(var(--cell) * 2.4);
  font-weight: 700;
}

.key-fn {
  font-size: calc(var(--cell) * 1.15);
  letter-spacing: 0.04em;
}

.key-fn svg {
  width: calc(var(--cell) * 1.5);
  height: calc(var(--cell) * 1.5);
  vertical-align: middle;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.key-op {
  color: var(--accent);
  font-size: calc(var(--cell) * 1.9);
}

.key-equals {
  --fill: var(--accent);
  color: var(--accent-ink);
  font-size: calc(var(--cell) * 1.9);
}

@media (hover: hover) {
  .key:hover {
    --fill: var(--hover);
  }
  .key-equals:hover {
    --fill: var(--accent-pressed);
  }
}

.key:active,
.key.is-pressed {
  --fill: var(--wash);
}

.key-equals:active,
.key-equals.is-pressed {
  --fill: var(--accent-pressed);
}

.key:focus {
  outline: none;
}

.key:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: calc(var(--cell) * -0.5);
}

@media (prefers-reduced-motion: reduce) {
  .figure[data-status="result"] span::after {
    animation: none;
  }
}
