:root {
  --bg-top: #f7e9d7;
  --bg-bottom: #e2efff;
  --body-glow-1: rgba(255, 255, 255, 0.6);
  --body-glow-2: rgba(255, 140, 66, 0.25);
  --panel: rgba(255, 248, 238, 0.88);
  --panel-edge: rgba(24, 33, 51, 0.1);
  --screen: linear-gradient(180deg, #fffaf1 0%, #ebe2d5 100%);
  --display-text: #1f2937;
  --display-dim: rgba(31, 41, 55, 0.55);
  --display-subtle: rgba(31, 41, 55, 0.66);
  --text-main: #182133;
  --text-dim: rgba(24, 33, 51, 0.62);
  --key: rgba(24, 33, 51, 0.04);
  --key-border: rgba(24, 33, 51, 0.1);
  --key-strong: rgba(255, 255, 255, 0.95);
  --soft-key-text: #b86f1e;
  --accent: #ff8c42;
  --accent-strong: #ff6b1a;
  --history-panel: rgba(255, 248, 238, 0.9);
  --history-card-bg: rgba(24, 33, 51, 0.03);
  --history-card-border: rgba(24, 33, 51, 0.08);
  --history-result: #1b2436;
  --chip-bg: rgba(24, 33, 51, 0.06);
  --chip-border: rgba(24, 33, 51, 0.1);
  --chip-hover: rgba(24, 33, 51, 0.12);
  --toggle-track: rgba(255, 255, 255, 0.84);
  --toggle-thumb: linear-gradient(180deg, #ffb371 0%, #ff7a2f 100%);
  --focus-ring: rgba(255, 140, 66, 0.72);
  --shadow: 0 24px 60px rgba(41, 52, 74, 0.18);
}

body[data-theme="dark"] {
  --bg-top: #0f172a;
  --bg-bottom: #020617;
  --body-glow-1: rgba(109, 156, 255, 0.14);
  --body-glow-2: rgba(255, 140, 66, 0.12);
  --panel: rgba(18, 24, 38, 0.9);
  --panel-edge: rgba(255, 255, 255, 0.14);
  --screen: linear-gradient(180deg, #faf7f0 0%, #e4ddd2 100%);
  --display-text: #1f2937;
  --display-dim: rgba(31, 41, 55, 0.55);
  --display-subtle: rgba(31, 41, 55, 0.66);
  --text-main: #f7f2ea;
  --text-dim: rgba(247, 242, 234, 0.72);
  --key: rgba(255, 255, 255, 0.08);
  --key-border: rgba(255, 255, 255, 0.12);
  --key-strong: rgba(255, 255, 255, 0.14);
  --soft-key-text: #ffd7a4;
  --history-panel: rgba(20, 27, 43, 0.88);
  --history-card-bg: rgba(255, 255, 255, 0.05);
  --history-card-border: rgba(255, 255, 255, 0.1);
  --history-result: #fff4e9;
  --chip-bg: rgba(255, 255, 255, 0.08);
  --chip-border: rgba(255, 255, 255, 0.12);
  --chip-hover: rgba(255, 255, 255, 0.14);
  --toggle-track: rgba(255, 255, 255, 0.12);
  --toggle-thumb: linear-gradient(180deg, #fff1de 0%, #ffd0a2 100%);
  --focus-ring: rgba(255, 215, 164, 0.95);
  --shadow: 0 30px 80px rgba(0, 0, 0, 0.42);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  font-family: "Avenir Next", "Century Gothic", "Trebuchet MS", sans-serif;
  color: var(--text-main);
  background:
    radial-gradient(circle at top left, var(--body-glow-1), transparent 34%),
    radial-gradient(circle at bottom right, var(--body-glow-2), transparent 25%),
    linear-gradient(160deg, var(--bg-top), var(--bg-bottom));
  transition: background 220ms ease, color 220ms ease;
}

.page-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 32px 20px;
}

.calculator-layout {
  width: min(100%, 860px);
  display: grid;
  grid-template-columns: minmax(0, 380px) minmax(260px, 320px);
  gap: 24px;
  align-items: start;
}

.calculator {
  position: relative;
  width: min(100%, 380px);
  padding: 24px;
  border: 1px solid var(--panel-edge);
  border-radius: 32px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), transparent 18%),
    var(--panel);
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: card-rise 700ms ease-out both;
}

.history-panel {
  min-height: 100%;
  padding: 24px 22px;
  border: 1px solid var(--panel-edge);
  border-radius: 28px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), transparent 18%),
    var(--history-panel);
  box-shadow: var(--shadow);
  animation: card-rise 820ms ease-out both;
}

.history-panel__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.history-panel__eyebrow {
  margin: 0 0 6px;
  font-size: 0.78rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.history-panel__title {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.1;
}

.history-panel__clear {
  padding: 10px 14px;
  border: 1px solid var(--chip-border);
  border-radius: 999px;
  background: var(--chip-bg);
  color: var(--text-main);
  font: inherit;
  font-size: 0.92rem;
  cursor: pointer;
  transition: background 160ms ease, transform 160ms ease, opacity 160ms ease;
}

.history-panel__clear:hover:not(:disabled) {
  background: var(--chip-hover);
  transform: translateY(-1px);
}

.history-panel__clear:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

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

.history-panel__empty {
  margin: 0;
  padding: 16px 0 4px;
  color: var(--text-dim);
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 12px;
  max-height: 460px;
  overflow-y: auto;
}

.history-item {
  padding: 14px 16px;
  border: 1px solid var(--history-card-border);
  border-radius: 18px;
  background: var(--history-card-bg);
}

.history-item__expression,
.history-item__result {
  margin: 0;
}

.history-item__expression {
  color: var(--text-dim);
  font-size: 0.98rem;
  overflow-wrap: anywhere;
}

.history-item__result {
  margin-top: 8px;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--history-result);
  overflow-wrap: anywhere;
}

.calculator__glow {
  position: absolute;
  inset: -120px auto auto -80px;
  width: 240px;
  height: 240px;
  background: radial-gradient(circle, rgba(255, 140, 66, 0.34), transparent 68%);
  pointer-events: none;
}

.calculator__topbar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.calculator__label {
  margin: 0;
  font-size: 0.85rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.calculator__actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px 6px 6px;
  border: 1px solid var(--chip-border);
  border-radius: 999px;
  background: var(--chip-bg);
  color: var(--text-main);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 160ms ease, transform 160ms ease, border-color 160ms ease;
}

.theme-toggle:hover {
  background: var(--chip-hover);
  transform: translateY(-1px);
}

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

.theme-toggle__track {
  position: relative;
  width: 42px;
  height: 24px;
  border-radius: 999px;
  background: var(--toggle-track);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12);
}

.theme-toggle__thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--toggle-thumb);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.18);
  transition: transform 180ms ease;
}

body[data-theme="dark"] .theme-toggle__thumb {
  transform: translateX(18px);
}

.theme-toggle__text {
  min-width: 4.4ch;
}

.calculator__status {
  display: inline-flex;
  gap: 6px;
}

.calculator__status span {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
}

.calculator__status span:first-child {
  background: #ff5f57;
}

.calculator__status span:nth-child(2) {
  background: #febb2f;
}

.calculator__status span:last-child {
  background: #28c840;
}

.display {
  position: relative;
  padding: 24px 22px;
  margin-bottom: 20px;
  border-radius: 24px;
  background: var(--screen);
  color: var(--display-text);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.8),
    inset 0 -12px 20px rgba(58, 46, 33, 0.08);
}

.display__memory,
.display__expression,
.display__value {
  margin: 0;
}

.display__memory {
  font-size: 0.8rem;
  letter-spacing: 0.18em;
  color: var(--display-dim);
}

.display__expression {
  margin-top: 24px;
  font-size: 1rem;
  color: var(--display-subtle);
  min-height: 1.25em;
  overflow-wrap: anywhere;
}

.display__value {
  margin-top: 10px;
  font-size: clamp(2.8rem, 10vw, 4rem);
  line-height: 1;
  text-align: right;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

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

.key {
  min-height: 68px;
  border: 1px solid var(--key-border);
  border-radius: 20px;
  background: linear-gradient(180deg, var(--key-strong), var(--key));
  color: var(--text-main);
  font: inherit;
  font-size: 1.35rem;
  font-weight: 500;
  cursor: pointer;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 10px 20px rgba(0, 0, 0, 0.16);
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.key:hover {
  transform: translateY(-2px);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    0 14px 24px rgba(0, 0, 0, 0.18);
}

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

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

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

.key--accent {
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
  border-color: rgba(255, 255, 255, 0.16);
  color: #fff7ef;
}

.key.is-active {
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.16),
    0 0 0 2px rgba(255, 247, 239, 0.2),
    0 14px 24px rgba(0, 0, 0, 0.2);
  transform: translateY(-1px);
}

.key.is-pressed {
  transform: translateY(1px);
  box-shadow:
    inset 0 2px 6px rgba(0, 0, 0, 0.18),
    0 6px 14px rgba(0, 0, 0, 0.16);
}

.key--equals {
  font-size: 1.6rem;
}

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

@keyframes card-rise {
  from {
    opacity: 0;
    transform: translateY(26px) scale(0.98);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@media (max-width: 420px) {
  .calculator {
    padding: 20px;
    border-radius: 28px;
  }

  .history-panel {
    padding: 20px 18px;
    border-radius: 24px;
  }

  .display {
    padding: 20px 18px;
  }

  .key {
    min-height: 62px;
    border-radius: 18px;
  }
}

@media (max-width: 780px) {
  .calculator-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .calculator {
    width: 100%;
  }

  .history-list {
    max-height: 280px;
  }
}
