:root {
  --bg: #0b0f14;
  --surface: #151d27;
  --surface-raised: #1b2531;
  --line: #26323f;
  --text: #eaf0f6;
  --muted: #93a3b5;
  --accent: #ffc266;
  --accent-strong: #ffb03a;
  --mint: #6ee7c8;
  --danger: #ff9b86;
  --focus: #7fd8ff;

  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "SFMono-Regular", "Cascadia Code", "Segoe UI Mono", Consolas, "Liberation Mono", monospace;

  --radius-lg: 20px;
  --radius-md: 16px;
  --radius-sm: 10px;
  --shadow-card: 0 24px 60px rgba(0, 0, 0, 0.45), 0 2px 0 rgba(255, 255, 255, 0.03) inset;
  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);
}

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

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

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--sans);
  color: var(--text);
  background:
    radial-gradient(1100px 560px at 12% -10%, rgba(255, 178, 58, 0.11), transparent 62%),
    radial-gradient(900px 520px at 100% 0%, rgba(110, 231, 200, 0.07), transparent 58%),
    radial-gradient(700px 500px at 50% 120%, rgba(127, 216, 255, 0.05), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  line-height: 1.5;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-image:
    linear-gradient(to right, rgba(255, 255, 255, 0.035) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255, 255, 255, 0.035) 1px, transparent 1px);
  background-size: 44px 44px;
  -webkit-mask-image: radial-gradient(ellipse at 50% 20%, #000 20%, transparent 78%);
  mask-image: radial-gradient(ellipse at 50% 20%, #000 20%, transparent 78%);
}

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

.skip-link {
  position: absolute;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 10;
  padding: 0.6rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--accent-strong);
  color: #23180a;
  font-weight: 700;
  text-decoration: none;
  transform: translateY(-160%);
  transition: transform 0.2s var(--ease);
}

.skip-link:focus-visible {
  transform: translateY(0);
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

:where(button, a, [tabindex]):focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

kbd {
  display: inline-block;
  padding: 0.05em 0.4em;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: #101821;
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.82em;
  line-height: 1.5;
}

code {
  font-family: var(--mono);
  font-size: 0.92em;
}

.page {
  width: min(100% - 2rem, 1060px);
  margin: 0 auto;
  padding: clamp(2rem, 5vw, 3.25rem) 0 clamp(1.75rem, 4vw, 2.75rem);
}

.masthead {
  max-width: 58ch;
  margin-bottom: clamp(1.5rem, 3vw, 2.25rem);
}

.masthead__eyebrow {
  margin: 0 0 0.55rem;
  color: var(--mint);
  font-size: 0.72rem;
  font-weight: 650;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

.masthead__title {
  margin: 0;
  font-size: clamp(2.1rem, 5.2vw, 2.9rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.05;
}

.masthead__lede {
  margin: 0.7rem 0 0;
  max-width: 52ch;
  color: var(--muted);
  font-size: clamp(0.98rem, 1.6vw, 1.06rem);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
  align-items: start;
}

.calculator,
.panel {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.035), rgba(255, 255, 255, 0)) , var(--surface);
  box-shadow: var(--shadow-card);
}

.calculator {
  padding: clamp(0.9rem, 2.4vw, 1.25rem);
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

.display {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 0.35rem;
  min-height: 128px;
  margin-bottom: 0.9rem;
  padding: 1rem 1.15rem 0.9rem;
  overflow: hidden;
  border: 1px solid #223041;
  border-radius: var(--radius-md);
  background:
    radial-gradient(120% 140% at 100% 0%, rgba(110, 231, 200, 0.06), transparent 55%),
    #090f16;
  box-shadow: 0 2px 14px rgba(0, 0, 0, 0.55) inset;
}

.display__expression {
  margin: 0;
  min-height: 1.45em;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.92rem;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.display__value {
  margin: 0;
  color: var(--text);
  font-family: var(--mono);
  font-size: clamp(2.6rem, 10vw, 3.35rem);
  font-variant-numeric: tabular-nums;
  font-weight: 400;
  letter-spacing: -0.02em;
  line-height: 1.08;
  text-align: right;
  overflow-wrap: anywhere;
}

.display__value[data-size="md"] { font-size: clamp(2.1rem, 8vw, 2.65rem); }
.display__value[data-size="sm"] { font-size: clamp(1.6rem, 6.4vw, 2rem); }
.display__value[data-size="xs"] { font-size: clamp(1.2rem, 5vw, 1.5rem); }

.display__value.is-error {
  color: var(--danger);
  font-family: var(--sans);
  font-size: clamp(1.15rem, 4.6vw, 1.45rem);
  font-weight: 600;
  letter-spacing: 0;
  line-height: 1.3;
}

.display.has-error {
  border-color: rgba(255, 155, 134, 0.45);
  box-shadow: 0 2px 14px rgba(0, 0, 0, 0.55) inset, 0 0 0 1px rgba(255, 155, 134, 0.12);
}

.display__value.is-fresh {
  animation: settle 0.22s var(--ease);
}

@keyframes settle {
  from { transform: translateY(5px); opacity: 0.55; }
  to   { transform: translateY(0); opacity: 1; }
}

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

.key {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60px;
  padding: 0.4rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--key-bg, var(--surface-raised));
  color: var(--key-fg, var(--text));
  font-family: inherit;
  font-size: 1.32rem;
  font-variant-numeric: tabular-nums;
  font-weight: 550;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition:
    background-color 0.15s var(--ease),
    border-color 0.15s var(--ease),
    transform 0.09s var(--ease),
    box-shadow 0.15s var(--ease),
    color 0.15s var(--ease);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.05) inset, 0 3px 8px rgba(0, 0, 0, 0.28);
}

.key:hover {
  background-color: var(--key-hover, #243243);
  border-color: #35485c;
}

.key:active,
.key.is-pressed {
  transform: translateY(1px) scale(0.985);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.03) inset, 0 1px 3px rgba(0, 0, 0, 0.35);
}

.key--number {
  --key-bg: #1b2531;
  --key-hover: #253445;
}

.key--decimal {
  --key-bg: #1b2531;
  --key-hover: #253445;
  font-size: 1.5rem;
  line-height: 1;
}

.key--function {
  --key-bg: #121a23;
  --key-fg: #a9bccd;
  --key-hover: #1b2632;
  font-size: 1.05rem;
  font-weight: 600;
}

.key--operator {
  --key-bg: #1c2a38;
  --key-fg: var(--accent);
  --key-hover: #263a4d;
  font-size: 1.5rem;
  font-weight: 500;
}

.key--operator[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #241704;
  box-shadow: 0 0 0 1px rgba(255, 178, 58, 0.35), 0 6px 18px rgba(255, 178, 58, 0.18);
}

.key--equals {
  --key-bg: var(--accent-strong);
  --key-fg: #2b1c02;
  --key-hover: var(--accent-strong);
  border-color: transparent;
  background-image: linear-gradient(135deg, #ffd88a, #ffab33);
  font-size: 1.5rem;
  font-weight: 750;
  box-shadow: 0 2px 0 rgba(255, 255, 255, 0.35) inset, 0 8px 20px rgba(255, 171, 51, 0.22);
}

.key--equals:hover {
  filter: brightness(1.06);
}

.panel {
  padding: clamp(0.9rem, 2.4vw, 1.1rem);
}

.history {
  display: flex;
  flex-direction: column;
  min-height: 230px;
}

.history__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.55rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--line);
}

.history__title {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 650;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.history__clear {
  padding: 0.28rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.74rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s var(--ease), color 0.15s var(--ease), border-color 0.15s var(--ease);
}

.history__clear:hover:not(:disabled) {
  background: #1b2632;
  border-color: #35485c;
  color: var(--text);
}

.history__clear:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.history__list {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 264px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: #33465a transparent;
}

.history__list::-webkit-scrollbar {
  width: 6px;
}

.history__list::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: #33465a;
}

.history__entry {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  width: 100%;
  padding: 0.5rem 0.55rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: right;
  cursor: pointer;
  transition: background-color 0.15s var(--ease), border-color 0.15s var(--ease);
}

.history__entry:hover {
  background: #1a2430;
  border-color: var(--line);
}

.history__expression {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.76rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history__result {
  font-family: var(--mono);
  font-size: 1.02rem;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.history__empty {
  margin: auto 0;
  padding-top: 0.75rem;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.6;
}

.history__empty[hidden] {
  display: none;
}

.shortcuts__title {
  margin: 0 0 0.55rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 650;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.shortcuts__list {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin: 0;
}

.shortcuts__row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
}

.shortcuts__row dt {
  flex: 0 0 auto;
  white-space: nowrap;
}

.shortcuts__row dt kbd {
  font-size: 0.72rem;
}

.shortcuts__row dd {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  text-align: right;
}

.shortcuts__note {
  margin: 0.85rem 0 0;
  padding-top: 0.7rem;
  border-top: 1px dashed rgba(38, 50, 63, 0.9);
  color: #7f8fa1;
  font-size: 0.76rem;
  line-height: 1.5;
}

.footer {
  margin-top: 1.6rem;
  padding-top: 1rem;
  border-top: 1px solid rgba(38, 50, 63, 0.7);
  color: #7f8fa1;
  font-size: 0.8rem;
  text-align: center;
}

.footer p {
  margin: 0;
}

@media (min-width: 640px) {
  .keypad {
    gap: 0.7rem;
  }

  .key {
    min-height: 64px;
    font-size: 1.45rem;
  }

  .key--function {
    font-size: 1.1rem;
  }

  .key--operator,
  .key--equals {
    font-size: 1.65rem;
  }
}

@media (min-width: 980px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr) 320px;
    gap: 1.15rem;
  }

  .display {
    min-height: 138px;
  }

  .history__list {
    max-height: 264px;
  }
}

@media (max-width: 380px) {
  .page {
    width: calc(100% - 1.5rem);
  }

  .keypad {
    gap: 0.45rem;
  }

  .key {
    min-height: 54px;
    border-radius: 13px;
    font-size: 1.2rem;
  }

  .key--decimal,
  .key--operator,
  .key--equals {
    font-size: 1.35rem;
  }

  .key--function {
    font-size: 0.98rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
