/*
   Calculator theme.
   A monochrome design built around a black card on a white page. Colours,
   sizing, and spacing are centralised as custom properties so the whole look
   can be tuned from one place.
*/

:root {
  /* The page area around the calculator is white; the calculator card and
     its inner surfaces stay black. */
  --bg: #ffffff;
  --surface: #0a0a0a;
  --display-bg: #050505;

  /* Keys. Operators sit on a slightly lighter surface so they read as
     a distinct group without introducing any colour. */
  --key: #1c1c1c;
  --key-hover: #2a2a2a;
  --key-op: #2a2a2a;
  --key-op-hover: #383838;
  --key-eq: #f5f5f5;
  --key-eq-hover: #ffffff;

  /* Text. The running expression uses a muted grey so the primary
     result stays dominant. */
  --text: #ffffff;
  --muted: #9a9a9a;
  --text-invert: #000000;

  --border: rgba(255, 255, 255, 0.08);
  --border-bright: rgba(255, 255, 255, 0.16);
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.6);

  --radius-card: 24px;
  --radius-key: 16px;
  --gap: 12px;

  --font: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

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

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

body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
  color: var(--text-invert);
  font-family: var(--font);
  /* Crisp glyphs on the large display digits. */
  -webkit-font-smoothing: antialiased;
}

/* The main wrapper must span the full width so the card's width: 100% has a
   concrete width to resolve against; otherwise the card collapses to its
   content width and max-width never engages. */
main {
  width: 100%;
  display: flex;
  justify-content: center;
}

/* --- Calculator card --------------------------------------------- */

.calculator {
  width: 100%;
  max-width: 960px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow);
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  /* The card and everything inside it is dark, so dark-theme native controls
     (scrollbars in the history list and overflow areas) belong here, not on
     the now-white page. */
  color: var(--text);
  color-scheme: dark;
  /* Animate the card width so any change resizes smoothly rather than
     snapping. */
  transition: max-width 0.2s ease;
}

/* --- Mode toggle -------------------------------------------------- */

.mode-toggle {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
}

.mode-toggle__btn {
  font-family: inherit;
  color: var(--muted);
  background: transparent;
  border: none;
  border-radius: 10px;
  padding: 15px 10px;
  font-size: 1.05rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.mode-toggle__btn:hover {
  color: var(--text);
}

/* The active segment is marked by a subtle lighter surface, staying within
   the monochrome palette. */
.mode-toggle__btn[aria-selected="true"] {
  background: var(--key);
  color: var(--text);
}

.mode-toggle__btn:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 2px;
}

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

/* The hidden attribute must win over the keypad's grid display, so the
   inactive panel is fully removed from the layout. */
[data-mode-panel][hidden] {
  display: none;
}

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

.display {
  position: relative;
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-key);
  padding: 24px 26px;
  min-height: 150px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  gap: 8px;
  /* Long results scroll sideways rather than breaking the layout. */
  overflow: hidden;
}

/* Small badge in the corner shown only while memory holds a value. */
.display__memory {
  position: absolute;
  top: 10px;
  left: 14px;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
}

.display__memory[hidden] {
  display: none;
}

.display__expression {
  color: var(--muted);
  font-size: 1.1rem;
  min-height: 1.2em;
  letter-spacing: 0.01em;
  white-space: nowrap;
  max-width: 100%;
  overflow-x: auto;
}

.display__result {
  color: var(--text);
  font-size: clamp(2.8rem, 10vw, 4rem);
  font-weight: 300;
  line-height: 1.1;
  letter-spacing: -0.01em;
  white-space: nowrap;
  max-width: 100%;
  overflow-x: auto;
}

/* Slim, unobtrusive scrollbars on the values that can overflow sideways
   (long results and wide binary representations). */
.display__result::-webkit-scrollbar,
.display__expression::-webkit-scrollbar,
.prog-base__value::-webkit-scrollbar {
  height: 4px;
}

.display__result::-webkit-scrollbar-thumb,
.display__expression::-webkit-scrollbar-thumb,
.prog-base__value::-webkit-scrollbar-thumb {
  background: var(--key-hover);
  border-radius: 2px;
}

/* --- Memory bar --------------------------------------------------- */

.memory-bar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--gap);
}

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

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

/* --- Keys --------------------------------------------------------- */

.key {
  font-family: inherit;
  color: var(--text);
  background: var(--key);
  border: 1px solid transparent;
  border-radius: var(--radius-key);
  cursor: pointer;
  user-select: none;
  /* A fixed, comfortable key height keeps every button a proper rounded
     rectangle at any card width. A width-based ratio would make keys too
     short (pill-shaped) on narrow screens and oversized on wide ones. */
  height: 76px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.7rem;
  font-weight: 400;
  transition: background-color 0.12s ease, transform 0.06s ease,
    filter 0.12s ease;
}

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

/* A brief brightness lift and a soft scale-down give tactile feedback
   on press. The same look is mirrored on keyboard input via .is-active. */
.key:active,
.key.is-active {
  transform: scale(0.95);
  filter: brightness(1.25);
}

/* Visible keyboard focus. The outline sits in the gap between keys on the
   dark card, so a single white ring reads clearly on every key, including
   the inverted equals key. */
.key:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 3px;
}

/* Memory keys are compact, secondary controls. */
.key--memory {
  height: auto;
  padding: 12px 0;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--muted);
  background: transparent;
  border-color: var(--border);
}

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

/* Function keys (clear, sign, percent, root, square, reciprocal). */
.key--fn {
  font-size: 1.25rem;
  color: var(--text);
}

/* Operators read as a group via a lighter surface and a thin border. */
.key--op {
  background: var(--key-op);
  border-color: var(--border-bright);
  font-size: 1.5rem;
}

.key--op:hover {
  background: var(--key-op-hover);
}

/* Equals is the single emphasised key: an inverted, near-white surface
   that stays within the monochrome palette. */
.key--eq {
  background: var(--key-eq);
  color: var(--text-invert);
  font-weight: 500;
}

.key--eq:hover {
  background: var(--key-eq-hover);
}

/* The zero key is wider, spanning two grid columns. */
.key--zero {
  grid-column: span 2;
  aspect-ratio: auto;
}

/* --- Scientific mode ---------------------------------------------- */

/* Scientific mode shares the same wide card as the other modes; its distinct
   two-block keypad layout is defined below. */
.calculator--sci {
  max-width: 960px;
}

/* Scientific mode lays its keys out as two side-by-side blocks: a function
   grid on the left and a number pad on the right. This keeps the keypad wide
   and roughly six rows tall instead of one very long column. */
.keypad--sci {
  display: flex;
  gap: var(--gap);
  align-items: start;
}

.sci-funcs,
.sci-pad {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}

.keypad--sci .key {
  font-size: 1.2rem;
}

.keypad--sci .key--op {
  font-size: 1.55rem;
}

/* Equals fills the remainder of its row, next to the ANS key. */
.sci-pad .key--eq-sci {
  grid-column: span 3;
}

/* Angle and notation toggles: small, quiet, monochrome controls. */
.sci-controls {
  display: flex;
  gap: 8px;
}

.sci-controls[hidden] {
  display: none;
}

.sci-toggle {
  font-family: inherit;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 14px;
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: color 0.12s ease, background-color 0.12s ease;
}

.sci-toggle:hover {
  color: var(--text);
}

/* The notation toggle lights up when active. */
.sci-toggle[aria-pressed="true"] {
  color: var(--text);
  background: var(--key);
  border-color: var(--border-bright);
}

.sci-toggle:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 2px;
}

/* --- Programmer mode ---------------------------------------------- */

/* Programmer mode shares the same wide card; its keypad uses five columns. */
.calculator--prog {
  max-width: 960px;
}

.keypad--prog {
  grid-template-columns: repeat(5, 1fr);
}

.keypad--prog .key {
  font-size: 1.3rem;
}

/* Disabled keys (hex letters outside hex mode) are dimmed and inert. */
.key:disabled {
  opacity: 0.28;
  cursor: default;
}

.key:disabled:hover {
  background: var(--key);
}

/* Four-base readout. Each row shows one base and can be tapped to make that
   base active. */
.prog-bases {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--display-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-key);
  padding: 8px;
}

.prog-bases[hidden] {
  display: none;
}

.prog-base {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-family: inherit;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 7px 10px;
  cursor: pointer;
  transition: background-color 0.12s ease, border-color 0.12s ease;
}

.prog-base:hover {
  background: var(--key);
}

/* The active base row is marked with a subtle surface and brighter border. */
.prog-base[aria-pressed="true"] {
  background: var(--key);
  border-color: var(--border-bright);
}

.prog-base:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 2px;
}

.prog-base__label {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
}

.prog-base__value {
  color: var(--text);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow-x: auto;
  text-align: right;
}

/* --- History ------------------------------------------------------ */

.history {
  border-top: 1px solid var(--border);
  padding-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history[hidden] {
  display: none;
}

.history__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.history__title {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.history__clear {
  font-family: inherit;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 0.72rem;
  cursor: pointer;
  transition: color 0.12s ease, background-color 0.12s ease;
}

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

.history__list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 160px;
  overflow-y: auto;
}

/* Slim, monochrome scrollbar to match the rest of the interface. */
.history__list::-webkit-scrollbar {
  width: 6px;
}

.history__list::-webkit-scrollbar-thumb {
  background: var(--key-hover);
  border-radius: 3px;
}

.history__entry {
  width: 100%;
  font-family: inherit;
  background: transparent;
  border: none;
  border-radius: 10px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  cursor: pointer;
  transition: background-color 0.12s ease;
}

.history__entry:hover {
  background: var(--key);
}

.history__entry:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 2px;
}

.history__expr {
  color: var(--muted);
  font-size: 0.78rem;
}

.history__value {
  color: var(--text);
  font-size: 1.05rem;
}

/* --- Small screens ------------------------------------------------ */

/* The two scientific blocks sit side by side on large screens, but that is
   too cramped on a phone. Below this width they stack so each block spans the
   full width. */
@media (max-width: 600px) {
  .keypad--sci {
    flex-direction: column;
  }
}

@media (max-width: 480px) {
  body {
    padding: 14px;
  }

  .key {
    height: 54px;
    font-size: 1.4rem;
  }

  .key--fn {
    font-size: 1.05rem;
  }

  .keypad--prog .key {
    font-size: 1rem;
  }
}

@media (max-width: 380px) {
  .calculator {
    padding: 16px;
  }

  :root {
    --gap: 8px;
  }

  .key {
    height: 50px;
    font-size: 1.25rem;
  }

  .keypad--prog .key {
    font-size: 0.9rem;
  }

  .keypad--prog .key--op {
    font-size: 0.95rem;
  }
}
