:root {
  color-scheme: dark;
  --bg: #000000;
  --key-digit: #333333;
  --key-extra: #262626;
  --key-function: #5C5C5E;
  --key-operator: #FF9F0A;
  --text: #FFFFFF;
  --text-muted: #8E8E93;
  --segment-bg: #1C1C1E;
  --font: "Archivo Narrow", "Arial Narrow", "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  display: flex;
  justify-content: center;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

/* --w is the keypad width. Everything else is sized from it. */
.calc {
  --w: min(448px, calc(100vw - 32px), max(260px, calc(100vh - 290px)));
  width: var(--w);
  min-height: 100%;
  display: flex;
  flex-direction: column;
  gap: calc(var(--w) * 0.04);
  padding-block: max(16px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-bottom));
}

@supports (height: 100dvh) {
  .calc { --w: min(448px, calc(100vw - 32px), max(260px, calc(100dvh - 290px))); }
}

/* ---------- Mode selector ---------- */

.modes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  padding: 3px;
  background: var(--segment-bg);
  border-radius: 999px;
}

.modes button {
  font: 500 16px/1 var(--font);
  color: var(--text);
  background: transparent;
  border: 0;
  border-radius: 999px;
  padding: 9px 0;
  cursor: pointer;
  transition: background-color 150ms ease-out;
}

.modes button[aria-checked="true"] { background: var(--key-operator); }

.modes button:focus-visible {
  outline: 2px solid var(--key-operator);
  outline-offset: 2px;
}

/* ---------- Display ---------- */

.display {
  flex: 1;
  min-height: calc(var(--w) * 0.45);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  text-align: right;
  padding-inline: calc(var(--w) * 0.03);
}

.display-input,
.display-main {
  white-space: nowrap;
  overflow: hidden;
  font-variant-numeric: tabular-nums;
}

.display-input {
  color: var(--text-muted);
  font-size: calc(var(--w) * 0.075);
  line-height: 1.3;
  min-height: 1.3em;
}

.display-main {
  font-size: calc(var(--w) * 0.19);
  line-height: 1.15;
  font-weight: 400;
  padding-top: 0.2em; /* room for raised exponents */
}

.display-main.wrap,
.display-input.wrap {
  white-space: normal;
  overflow-wrap: anywhere;
}

.display-notice {
  margin: 0;
  color: var(--text-muted);
  font-size: max(13px, calc(var(--w) * 0.036));
  line-height: 1.4;
  min-height: 1.4em;
}

sup {
  font-size: 0.55em;
  line-height: 0;
  position: relative;
  top: -0.75em;
  vertical-align: baseline;
  margin-left: 0.04em;
}

sup.pending {
  display: inline-block;
  width: 0.5em;
  height: 0.7em;
  border: 0.07em solid currentColor;
  border-radius: 0.08em;
  opacity: 0.5;
  top: -1.1em;
}

.display-main.fade-in { animation: fade-in 200ms ease-out; }

.display.shake { animation: shake 320ms ease-in-out; }

@keyframes fade-in {
  from { opacity: 0; }
}

@keyframes shake {
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

/* ---------- Keypad ---------- */

.keys {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: calc(var(--w) * 0.03);
}

.key {
  --key-bg: var(--key-digit);
  aspect-ratio: 1;
  max-width: 100%;
  border: 0;
  border-radius: 50%;
  padding: 0;
  background: var(--key-bg);
  color: var(--text);
  font: 400 calc(var(--w) * 0.075) / 1 var(--font);
  display: grid;
  place-items: center;
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
  transition: background-color 150ms ease-out;
}

.key:active,
.key.is-pressed {
  background: color-mix(in srgb, var(--key-bg) 80%, #fff);
  transition: none;
}

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

.key-extra {
  --key-bg: var(--key-extra);
  font-size: calc(var(--w) * 0.062);
}

.key-function { --key-bg: var(--key-function); }

.key-operator {
  --key-bg: var(--key-operator);
  font-size: calc(var(--w) * 0.09);
}

.key svg {
  width: 1.05em;
  height: 1.05em;
}

.key sup { top: -0.55em; }

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

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
