:root {
  --bg: #f2efe8;
  --screen: #fbfaf7;
  --text: #1f1d1a;
  --muted: #625d54;
  --key: #ffffff;
  --key-text: #1f1d1a;
  --fn: #e3ded3;
  --fn-text: #1f1d1a;
  --op: #1f6f6b;
  --op-text: #ffffff;
  --equals: #c2491d;
  --equals-text: #ffffff;
  --focus: #1f6f6b;
  --gap: 0.5rem;
  --vh: 100vh;
  color-scheme: light dark;
}

/* On a phone vh counts the space under the address bar too, and the bottom row would fall below the screen. */
@supports (height: 100dvh) {
  :root { --vh: 100dvh; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514;
    --screen: #201f1d;
    --text: #f2efe8;
    --muted: #aaa499;
    --key: #2d2b28;
    --key-text: #f2efe8;
    --fn: #46423c;
    --fn-text: #f2efe8;
    --op: #3aa39c;
    --op-text: #0f1716;
    --equals: #f08a4b;
    --equals-text: #1a0f08;
    --focus: #3aa39c;
  }
}

* { box-sizing: border-box; }

html, body { overscroll-behavior: none; }

body {
  display: flex;
  min-height: var(--vh);
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Auto margins centre it without pushing its top off a screen too short for it. */
.calc {
  display: grid;
  grid-template-rows: minmax(5rem, 1fr) auto;
  gap: 0.75rem;
  width: min(100%, 24rem);
  height: min(var(--vh), 38rem);
  margin: auto;
  padding: 0.75rem;
  touch-action: manipulation;
}

/* A phone gives it the whole screen, with the keys at the bottom, under the thumb. */
@media (max-width: 30rem) {
  .calc {
    width: 100%;
    height: var(--vh);
  }
}

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

/* A column the other way up starts scrolled to its end, so the end of a long expression stays in
   view as it grows, and a person can scroll back to its start. The two lines share one box inside
   it, which keeps them in order. */
.screen {
  display: flex;
  flex-direction: column-reverse;
  overflow-y: auto;
  padding: 0.75rem;
  border-radius: 1rem;
  background: var(--screen);
  text-align: right;
  overflow-wrap: anywhere;
}

.above {
  margin: 0;
  color: var(--muted);
  font-size: 1.125rem;
}

/* Small enough that the longest result, such as −9.99999999999e+307, fits on one line of a phone. */
.line {
  display: block;
  font-size: 1.875rem;
  line-height: 1.2;
}

.keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: clamp(2.75rem, calc(var(--vh) * 0.09), 4.5rem);
  gap: var(--gap);
  -webkit-user-select: none;
  user-select: none;
}

.keys button {
  border: 0;
  border-radius: 0.875rem;
  background: var(--key);
  color: var(--key-text);
  font: inherit;
  font-size: 1.5rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.keys .fn {
  background: var(--fn);
  color: var(--fn-text);
  font-size: 1.25rem;
}

.keys .op,
.keys .equals {
  font-size: 1.75rem;
}

.keys .op {
  background: var(--op);
  color: var(--op-text);
}

.keys .equals {
  background: var(--equals);
  color: var(--equals-text);
}

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

.keys button:active { opacity: 0.75; }

.keys button:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
