/* RPN Programmer's Calculator */

/* Iosevka 34.9.0 (SIL OFL 1.1, see fonts/OFL.txt), subset to the characters the
   monospace text shows; CLAUDE.md has the command that builds it. Browsers that
   block fonts over file:// fall back to the system monospace. */
@font-face {
  font-family: "Iosevka";
  src: url("fonts/iosevka-subset.woff2") format("woff2");
  font-display: swap;
}

:root {
  color-scheme: light dark;

  --bg: #eef0f3;
  --surface: #ffffff;
  --border: #d5d9e0;
  --text: #1b1f24;
  --muted: #687385;
  --accent: #2563eb;      /* fills behind --accent-text */
  --accent-text: #ffffff;
  --accent-fg: #2563eb;   /* accent as text, outlines and borders on surfaces */
  --key-digit: #ffffff;
  --key-op: #e3ebfa;
  --key-bit: #ece7f7;
  --key-stack: #e9ecf0;
  --error: #c62828;
  --warning: #9a5b00;
  --info: #1d6fa5;

  --gap: 0.5rem;
  --key-gap: 0.3125rem;
  --radius: 12px;
  --key-radius: 8px;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "Iosevka", ui-monospace, "Cascadia Mono", Consolas, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101216;
    --surface: #1a1d23;
    --border: #2f343d;
    --text: #e7e9ed;
    --muted: #8b94a3;
    --accent: #3570d6;
    --accent-text: #ffffff;
    --accent-fg: #6ea0f5;
    --key-digit: #23272e;
    --key-op: #1f2b40;
    --key-bit: #2a2438;
    --key-stack: #262a31;
    --error: #ff6b6b;
    --warning: #f0b45a;
    --info: #6cb4e6;
  }
}

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

body {
  margin: 0;
  padding: 12px 16px;
  min-block-size: 100dvh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
}

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

.calc {
  inline-size: min(100%, 26rem);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

/* --- Settings ------------------------------------------------------------- */

.settings {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(9.5rem, 1fr));
  gap: var(--gap);
}

.setting {
  margin: 0;
  padding: 0;
  border: 0;
  min-inline-size: 0;
}

/* Entry base and Float precision share a slot: each shows only in its mode. */
.calc[data-mode="float"] .setting-int-only,
.calc[data-mode="int"] .setting-float-only {
  display: none;
}

.setting legend {
  padding: 0;
  margin-block-end: 0.125rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--key-radius);
  overflow: hidden;
  background: var(--surface);
}

.segmented label {
  position: relative;
  flex: 1;
}

.segmented label + label {
  border-inline-start: 1px solid var(--border);
}

.segmented input {
  position: absolute;
  inset: 0;
  opacity: 0;
  margin: 0;
  pointer-events: none;
}

.segmented span {
  display: block;
  padding: 0.375rem 0.25rem;
  text-align: center;
  font-size: 0.8125rem;
  cursor: pointer;
  user-select: none;
}

.segmented input:checked + span {
  background: var(--accent);
  color: var(--accent-text);
}

.segmented input:focus-visible + span {
  outline: 2px solid var(--accent-fg);
  outline-offset: -2px;
}

.segmented input:checked:focus-visible + span {
  outline-color: var(--accent-text);
}

.segmented input:disabled + span {
  opacity: 0.4;
  cursor: not-allowed;
}

/* --- Status --------------------------------------------------------------- */

.status-row {
  display: flex;
  align-items: center;
  gap: var(--gap);
}

.status {
  flex: 1;
  min-inline-size: 0;
  margin: 0;
  min-block-size: 1.25rem;
  font-size: 0.875rem;
}

/* Info is a single line (a 64-bit "Copied 0b…" would wrap); screen readers still get the whole text. */
.status[data-kind="info"] {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.icon-button {
  flex: none;
  inline-size: 1.5rem;
  block-size: 1.5rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--muted);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
}

.icon-button:hover:not(:disabled),
.icon-button.is-pressed {
  border-color: var(--accent-fg);
  color: var(--text);
}

.icon-button svg {
  display: block;
  margin: auto;
}

.icon-button:focus-visible {
  outline: 2px solid var(--accent-fg);
  outline-offset: 2px;
}

.icon-button:disabled {
  opacity: 0.35;
  cursor: default;
}

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

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

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

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

.display {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font-mono);
}

.stack-scroll {
  block-size: 7.5rem;
  overflow-y: auto;
  padding: 0.25rem 0.75rem;
}

.stack {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-block-size: 100%;
}

.stack li {
  display: grid;
  grid-template-columns: 2rem 1fr;
  align-items: baseline;
  padding-block: 0.125rem;
}

/* Rows far from X (see NEAR_ROWS in app.js) skip layout and painting while
   scrolled out of view, so a stack thousands deep stays fast. */
.stack li.is-deep {
  content-visibility: auto;
  contain-intrinsic-block-size: auto 1.375rem;
}

.stack-label {
  color: var(--muted);
  font-size: 0.8125rem;
}

.stack-value {
  text-align: end;
  font-size: 1.125rem;
  overflow-wrap: anywhere;
}

.entry-line {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  min-block-size: 2.75rem;
  padding: 0.5rem 0.75rem;
  border-block-start: 1px solid var(--border);
}

.entry-prompt {
  color: var(--muted);
}

.entry {
  flex: 1;
  text-align: end;
  font-size: 1.375rem;
  overflow-wrap: anywhere;
}

.entry.entry-long {
  font-size: 1.125rem;
}

.entry::after {
  content: "";
  display: inline-block;
  inline-size: 2px;
  block-size: 1.1em;
  margin-inline-start: 2px;
  vertical-align: text-bottom;
  background: var(--accent-fg);
  animation: blink 1s steps(1) infinite;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .entry::after {
    animation: none;
  }
}

/* --- Base conversions ----------------------------------------------------- */

.bases {
  margin: 0;
  padding: 0.375rem 0.75rem;
  display: grid;
  gap: 0.125rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.base {
  position: relative;
  display: grid;
  grid-template-columns: 2.75rem 1fr;
  align-items: baseline;
  margin-inline: -0.375rem;
  padding-inline: 0.375rem;
  border-radius: 4px;
}

/* Each row is a copy button: its ::after stretches over the whole row, label included. */
.base-copy {
  display: block;
  inline-size: 100%;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: inherit;
  overflow-wrap: anywhere;
  cursor: pointer;
}

.base-copy::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
}

.base-copy:disabled {
  cursor: default;
}

.base:has(.base-copy:hover:not(:disabled)) {
  background: var(--key-stack);
}

.base-copy:focus-visible {
  outline: none;
}

.base-copy:focus-visible::after {
  outline: 2px solid var(--accent-fg);
  border-radius: 4px;
}

.base dt {
  font-size: 0.75rem;
  color: var(--muted);
}

.base dd {
  margin: 0;
  min-block-size: 1.25em;
  text-align: end;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  overflow-wrap: anywhere;
}

/* A 64-bit Bin view holds a line break after 32 bits (see BIN_LINE in app.js). */
.base-bin output {
  white-space: pre-line;
}

/* Base conversions only apply to integers; Float mode shows functions in their place. */
.calc[data-mode="float"] .bases,
.calc[data-mode="int"] .functions,
.calc[data-mode="float"] [data-int-only] {
  display: none;
}

.calc[data-entry-base="16"] .base-hex dt,
.calc[data-entry-base="10"] .base-dec dt,
.calc[data-entry-base="8"] .base-oct dt,
.calc[data-entry-base="2"] .base-bin dt {
  color: var(--accent-fg);
  font-weight: 600;
}

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

.keypad,
.functions {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: var(--key-gap);
}

.key {
  min-block-size: 2.625rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--key-radius);
  background: var(--key-digit);
  color: var(--text);
  font: inherit;
  font-size: 1.0625rem;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}

/* Digits match the stack they type into. */
.key-digit {
  font-family: var(--font-mono);
}

.key-op {
  background: var(--key-op);
  font-size: 1.25rem;
}

.key-bit,
.key-stack,
.key-word {
  font-size: 0.8125rem;
  font-weight: 500;
}

.key-bit,
.key-fn {
  background: var(--key-bit);
}

/* Superscripts and subscripts shouldn't make a key taller. */
.key-fn sup,
.key-fn sub {
  font-size: 0.7em;
  line-height: 0;
}

.key-half {
  grid-column: span 3;
}

.key-stack {
  background: var(--key-stack);
}

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

.key-enter {
  grid-column: span 2;
  grid-row: span 3;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-size: 0.9375rem;
  font-weight: 600;
}

.key:hover:not(:disabled) {
  border-color: var(--accent-fg);
}

.key:active:not(:disabled) {
  transform: translateY(1px);
}

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

.key:disabled {
  opacity: 0.35;
  cursor: default;
}

/* Brief feedback when a key is triggered from the keyboard. */
.key.is-pressed {
  transform: translateY(1px);
  border-color: var(--accent-fg);
}

/* A disabled key pressed on the keyboard shows red, at full opacity so it reads. */
.key.is-rejected,
.icon-button.is-rejected {
  opacity: 1;
  border-color: var(--error);
  color: var(--error);
}

/*
 * Hover hints: a key's keyboard shortcut (data-shortcut, set by app.js) shows
 * above it after a short hover, only where there's a mouse. The empty alt text
 * keeps the hint out of the button's accessible name.
 */
@media (hover: hover) and (pointer: fine) {
  .key[data-shortcut],
  .icon-button[data-shortcut] {
    position: relative;
  }

  .key[data-shortcut]::after,
  .icon-button[data-shortcut]::after {
    content: attr(data-shortcut);
    content: attr(data-shortcut) / "";
    position: absolute;
    inset-block-end: calc(100% + 4px);
    inset-inline-start: 50%;
    translate: -50% 0;
    padding: 0.0625rem 0.375rem;
    border: 1px solid transparent;
    border-radius: 4px;
    /* Inverted colours, so the hint stands out from the keys in both themes. */
    background: var(--text);
    color: var(--surface);
    font-family: var(--font-mono);
    font-size: 0.8125rem;
    font-weight: normal;
    line-height: 1.4;
    white-space: nowrap;
    box-shadow: 0 2px 6px rgb(0 0 0 / 0.15);
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.1s, visibility 0.1s;
  }

  /* The status row's buttons sit at the right edge, so their hints align right. */
  .icon-button[data-shortcut]::after {
    inset-inline: auto 0;
    translate: none;
  }

  .key[data-shortcut]:hover:not(:disabled),
  .icon-button[data-shortcut]:hover:not(:disabled) {
    z-index: 1;
  }

  .key[data-shortcut]:hover:not(:disabled)::after,
  .icon-button[data-shortcut]:hover:not(:disabled)::after {
    opacity: 1;
    visibility: visible;
    transition-delay: 0.5s;
  }
}

/* A–F only appear when entering in Hex; elsewhere their row collapses. */
.calc:not([data-entry-base="16"]) .key-hex {
  display: none;
}

/* --- Forced colors (e.g. Windows high contrast) --------------------------- */

@media (forced-colors: active) {
  .segmented input:checked + span,
  .key-enter {
    forced-color-adjust: none;
    background: Highlight;
    color: HighlightText;
  }

  .entry::after {
    background: CanvasText;
  }

  /* Hover backgrounds are dropped in forced colors, so outline the row instead. */
  .base:has(.base-copy:hover:not(:disabled)) {
    outline: 1px solid Highlight;
  }

  .icon-button.is-pressed {
    border-color: Highlight;
  }

  .key:disabled,
  .icon-button:disabled,
  .segmented input:disabled + span {
    color: GrayText;
    opacity: 1;
  }

  .key.is-rejected,
  .icon-button.is-rejected {
    border-color: Mark;
    color: CanvasText;
  }
}

/* --- Help dialog ---------------------------------------------------------- */

.help {
  inline-size: min(26rem, calc(100% - 32px));
  max-block-size: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}

.help::backdrop {
  background: rgb(0 0 0 / 0.5);
}

.help-body {
  padding: 1rem;
}

.help h2 {
  margin: 0 0 0.5rem;
  font-size: 1.125rem;
}

/* The heading takes initial focus so the dialog opens at the top. */
.help h2:focus {
  outline: none;
}

.help h3 {
  margin: 1rem 0 0.375rem;
  font-size: 0.9375rem;
}

.help p {
  margin: 0 0 0.5rem;
  font-size: 0.875rem;
  line-height: 1.45;
}

.help-example {
  padding: 0.375rem 0.5rem;
  border-radius: var(--key-radius);
  background: var(--key-stack);
}

.help table {
  inline-size: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.help th,
.help td {
  padding: 0.3125rem 0;
  text-align: start;
  vertical-align: baseline;
  border-block-end: 1px solid var(--border);
}

.help th {
  padding-inline-end: 0.75rem;
  font-weight: normal;
  white-space: nowrap;
}

.help kbd {
  display: inline-block;
  min-inline-size: 1.5em;
  padding: 0 0.3125em;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--key-stack);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  text-align: center;
}

.help form {
  margin-block-start: 1rem;
  text-align: end;
}

.help-close {
  padding: 0.375rem 1rem;
  border: 1px solid var(--accent);
  border-radius: var(--key-radius);
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-size: 0.875rem;
  cursor: pointer;
}

.help-close:focus-visible {
  outline: 2px solid var(--accent-fg);
  outline-offset: 2px;
}
