/* Calculator — dark UI, no external fonts or assets. */

:root {
  --bg: #0b0d10;
  --bg-glow: #171b22;
  --surface: #14171c;
  --surface-edge: #23272f;
  --key: #1e222a;
  --key-hover: #272c36;
  --key-fn: #2b313c;
  --key-fn-hover: #353c49;
  --accent: #ff9f0a;
  --accent-hover: #ffb340;
  --accent-ink: #1a1205;
  --text: #f4f6fa;
  --muted: #868e9d;
  --focus: #6aa8ff;
  --radius: 16px;
  --gap: clamp(0.5rem, 2vw, 0.75rem);
  /* Display type scale, stepped down by JS as the number gets longer. */
  --fs-lg: clamp(2.5rem, 13vw, 3.25rem);
  --fs-md: clamp(1.9rem, 9.5vw, 2.4rem);
  --fs-sm: clamp(1.4rem, 7vw, 1.75rem);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: clamp(0.75rem, 3vw, 2rem);
  padding-bottom: max(clamp(0.75rem, 3vw, 2rem), env(safe-area-inset-bottom));
  background-color: var(--bg);
  background-image: radial-gradient(120% 90% at 50% 0%, var(--bg-glow) 0%, var(--bg) 62%);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial,
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

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

.calc {
  width: 100%;
  max-width: 24rem;
  padding: clamp(0.75rem, 3vw, 1.15rem);
  border: 1px solid var(--surface-edge);
  border-radius: calc(var(--radius) + 8px);
  background: var(--surface);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.04) inset, 0 24px 60px -20px rgba(0, 0, 0, 0.75);
}

/* ------------------------------------------------------------- display -- */

.display {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-end;
  gap: 0.25rem;
  /* Tall enough for the largest type step, so the keypad never shifts when
     the result length changes. */
  min-height: 7.25rem;
  padding: 0.9rem 0.85rem 1.1rem;
  overflow: hidden;
}

.display__expression {
  min-height: 1.25em;
  font-size: clamp(0.85rem, 3.2vw, 0.95rem);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.display__value {
  display: block;
  max-width: 100%;
  overflow: hidden;
  font-weight: 300;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  letter-spacing: -0.02em;
  white-space: nowrap;
  /* Deliberately not transitioned: the size must change in the same frame as
     the text, or a long result paints one frame at the old larger size and
     gets clipped. */
}

/* Type scale steps down as the number gets longer, so it never spills. */
.display__value[data-size="lg"] {
  font-size: var(--fs-lg);
}
.display__value[data-size="md"] {
  font-size: var(--fs-md);
}
.display__value[data-size="sm"] {
  font-size: var(--fs-sm);
}

.display__value.is-error {
  color: #ff6b6b;
}

/* ---------------------------------------------------------------- keys -- */

.keys {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--gap);
}

.key {
  -webkit-appearance: none;
  appearance: none;
  aspect-ratio: 1 / 1;
  min-height: 3rem;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: var(--key);
  color: var(--text);
  font-family: inherit;
  font-size: clamp(1.25rem, 5.5vw, 1.5rem);
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background-color 120ms ease, transform 90ms ease, border-color 120ms ease;
}

.key--fn {
  background: var(--key-fn);
  color: var(--text);
}

.key--op,
.key--eq {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.key--small {
  font-size: clamp(0.95rem, 4vw, 1.15rem);
  letter-spacing: -0.03em;
}

@media (hover: hover) {
  .key:hover {
    background: var(--key-hover);
  }
  .key--fn:hover {
    background: var(--key-fn-hover);
  }
  .key--op:hover,
  .key--eq:hover {
    background: var(--accent-hover);
  }
}

.key:active,
.key.is-active {
  transform: scale(0.95);
  border-color: rgba(255, 255, 255, 0.14);
  background: var(--key-hover);
}

.key--fn:active,
.key--fn.is-active {
  background: var(--key-fn-hover);
}

.key--op:active,
.key--op.is-active,
.key--eq:active,
.key--eq.is-active {
  background: var(--accent-hover);
}

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

/* ---------------------------------------------------------------- hint -- */

.hint {
  margin: 1rem 0 0.15rem;
  text-align: center;
  font-size: 0.75rem;
  line-height: 1.5;
  color: var(--muted);
}

.hint kbd {
  padding: 0.1em 0.4em;
  border: 1px solid var(--surface-edge);
  border-radius: 5px;
  background: var(--key);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
  color: var(--text);
}

/* Short viewports (landscape phones): drop the square keys and the hint. */
@media (max-height: 34rem) {
  :root {
    --fs-lg: 1.9rem;
    --fs-md: 1.6rem;
    --fs-sm: 1.25rem;
  }
  .key {
    aspect-ratio: auto;
    min-height: 2.5rem;
    padding: 0.6rem 0;
  }
  .display {
    min-height: 5.25rem;
    padding-bottom: 0.6rem;
  }
  .hint {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
  .key:active,
  .key.is-active {
    transform: none;
  }
}
