:root {
  --bg: #1b1d23;
  --card-bg: #24262f;
  --display-bg: #14151a;
  --text-primary: #f5f6fa;
  --text-secondary: #9297a8;
  --number-bg: #343745;
  --number-bg-hover: #3d4152;
  --number-bg-active: #2c2f3b;
  --operator-bg: #ff9f43;
  --operator-bg-hover: #ffb26b;
  --operator-bg-active: #e78a30;
  --function-bg: #4b4f60;
  --function-bg-hover: #565b6e;
  --function-bg-active: #40434f;
  --equals-bg: #3d7cf4;
  --equals-bg-hover: #5c91f6;
  --equals-bg-active: #2e63cc;
  --error-color: #ff5c5c;
  --radius-lg: 24px;
  --radius-md: 14px;
  --focus-ring: #7fb1ff;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at top, #2a2d38, var(--bg));
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif;
  color: var(--text-primary);
  padding: 16px;
}

.app {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  width: 100%;
  max-width: 760px;
  justify-content: center;
  align-items: flex-start;
}

.calculator {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  padding: 20px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

.display {
  background: var(--display-bg);
  border-radius: var(--radius-md);
  padding: 20px 18px;
  margin-bottom: 18px;
  min-height: 96px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  overflow: hidden;
}

.display__expression {
  min-height: 22px;
  font-size: 15px;
  color: var(--text-secondary);
  text-align: right;
  word-break: break-all;
}

.display__current {
  font-size: clamp(32px, 8vw, 44px);
  font-weight: 600;
  text-align: right;
  overflow-x: auto;
  white-space: nowrap;
}

.display__current--error {
  color: var(--error-color);
  font-size: clamp(22px, 6vw, 30px);
}

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

.btn {
  border: none;
  border-radius: var(--radius-md);
  padding: 18px 0;
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  background: var(--number-bg);
  cursor: pointer;
  transition: background-color 0.12s ease, transform 0.05s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover {
  background: var(--number-bg-hover);
}

.btn:active {
  background: var(--number-bg-active);
  transform: scale(0.96);
}

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

.btn--operator {
  background: var(--operator-bg);
  color: #1b1d23;
}

.btn--operator:hover {
  background: var(--operator-bg-hover);
}

.btn--operator:active {
  background: var(--operator-bg-active);
}

.btn--function {
  background: var(--function-bg);
}

.btn--function:hover {
  background: var(--function-bg-hover);
}

.btn--function:active {
  background: var(--function-bg-active);
}

.btn--equals {
  background: var(--equals-bg);
  grid-column: span 1;
}

.btn--equals:hover {
  background: var(--equals-bg-hover);
}

.btn--equals:active {
  background: var(--equals-bg-active);
}

.history {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  padding: 20px;
  width: 100%;
  max-width: 320px;
  max-height: 468px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

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

.history__header h2 {
  margin: 0;
  font-size: 16px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.history__clear {
  background: transparent;
  border: 1px solid var(--function-bg);
  color: var(--text-secondary);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
}

.history__clear:hover {
  color: var(--text-primary);
  border-color: var(--text-secondary);
}

.history__list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history__item {
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
  background: var(--display-bg);
  border-radius: var(--radius-md);
}

.history__expr {
  font-size: 13px;
  color: var(--text-secondary);
}

.history__result {
  font-size: 18px;
  font-weight: 600;
  text-align: right;
}

.history__empty {
  color: var(--text-secondary);
  font-size: 14px;
  text-align: center;
  padding: 20px 0;
}

/* Tablet */
@media (min-width: 768px) {
  .app {
    align-items: stretch;
  }

  .calculator,
  .history {
    max-width: 340px;
  }
}

/* Desktop */
@media (min-width: 1024px) {
  .app {
    max-width: 800px;
  }

  .calculator {
    max-width: 380px;
  }

  .history {
    max-width: 340px;
  }
}

/* Small mobile */
@media (max-width: 380px) {
  .calculator,
  .history {
    padding: 14px;
  }

  .btn {
    padding: 16px 0;
    font-size: 18px;
  }
}
