:root {
  color-scheme: light dark;
  --bg: #0f1115;
  --panel: #171a21;
  --screen: #10131a;
  --key: #232833;
  --key-hover: #2d3342;
  --op: #2b3446;
  --accent: #3d7dff;
  --text: #f2f4f8;
  --muted: #8b94a7;
  --danger: #ff6b6b;
  --radius: 14px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #eef1f6;
    --panel: #ffffff;
    --screen: #f6f8fc;
    --key: #e7ebf3;
    --key-hover: #dbe1ec;
    --op: #dde5f7;
    --text: #12151c;
    --muted: #5c6577;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.calculator {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border-radius: 22px;
  padding: 18px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
}

.screen {
  background: var(--screen);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 14px;
  text-align: right;
  overflow: hidden;
}

.expression {
  min-height: 22px;
  font-size: 15px;
  color: var(--muted);
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}

.expression::-webkit-scrollbar { display: none; }

.result {
  display: block;
  font-size: clamp(28px, 9vw, 40px);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}

.result.error { color: var(--danger); font-size: 20px; }

.keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.key {
  appearance: none;
  border: 0;
  border-radius: var(--radius);
  padding: 16px 0;
  font-size: 19px;
  font-family: inherit;
  color: var(--text);
  background: var(--key);
  cursor: pointer;
  transition: background 0.12s ease, transform 0.06s ease;
}

.key:hover { background: var(--key-hover); }
.key:active, .key.active { transform: translateY(1px); background: var(--key-hover); }
.key:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.key.op, .key.fn { background: var(--op); }
.key.fn { font-size: 16px; color: var(--muted); }

.key.equals {
  grid-column: span 2;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.key.equals:hover { background: #5590ff; }

.history { margin-top: 16px; }

.history h2 {
  margin: 0 0 6px;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.history ul {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 132px;
  overflow-y: auto;
}

.history li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 9px;
  font-size: 14px;
  cursor: pointer;
}

.history li:hover { background: var(--key); }
.history li span:first-child { color: var(--muted); }
.history li span:last-child { font-variant-numeric: tabular-nums; }

.history:not(:has(li)) { display: none; }

@media (prefers-reduced-motion: reduce) {
  .key { transition: none; }
}
