:root {
  --bg: #111418;
  --surface: #1a1f26;
  --surface-raised: #232a33;
  --key: #2a323c;
  --key-hover: #37414d;
  --text: #f5f7fa;
  --text-strong: #ffffff;
  --accent: #ff8a1f;
  --accent-press: #ff9d40;
  --accent-text: #111418;
  --error: #ff5a6a;
  --border: #3a444f;
  --radius: 16px;
  --font: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}

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

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.3;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  scrollbar-width: none;
}

body::-webkit-scrollbar,
.history__list::-webkit-scrollbar {
  display: none;
}

button {
  font-family: inherit;
  color: inherit;
}

.app {
  display: grid;
  grid-template-columns: minmax(320px, 520px) minmax(280px, 380px);
  gap: 32px;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  padding: 32px;
}

.calculator {
  display: flex;
  flex-direction: column;
  gap: 20px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 24px;
  padding: 24px;
}

.display {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 132px;
  padding: 16px 20px;
  background: var(--bg);
  border-radius: var(--radius);
  border: 2px solid var(--border);
  text-align: right;
  overflow: hidden;
}

.display__expression {
  min-height: 28px;
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.02em;
  overflow-wrap: anywhere;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  direction: rtl;
  text-align: right;
}

.display__value {
  font-size: 56px;
  font-weight: 800;
  color: var(--text-strong);
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  word-break: break-all;
}

.display__value--error {
  font-size: 30px;
  font-weight: 700;
  color: var(--error);
  line-height: 1.2;
}

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

.key {
  min-height: 68px;
  border: 2px solid transparent;
  border-radius: var(--radius);
  background: var(--key);
  color: var(--text-strong);
  font-size: 30px;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  transition: background 120ms ease, transform 60ms ease;
  -webkit-tap-highlight-color: transparent;
}

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

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

.key:active {
  transform: scale(0.97);
}

.key--action {
  background: var(--surface-raised);
  color: var(--text);
  font-size: 24px;
}

.key--op {
  background: #333d49;
  color: var(--text-strong);
}

.key--equals {
  background: var(--accent);
  color: var(--accent-text);
  font-size: 34px;
}

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

.key--zero {
  grid-column: span 1;
}

.hint {
  margin: 0;
  font-size: 15px;
  color: var(--text);
  text-align: center;
  opacity: 0.9;
}

.history {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-self: stretch;
  max-height: 640px;
  padding: 24px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 24px;
}

.history__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.history__title {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  color: var(--text-strong);
}

.history__clear {
  background: transparent;
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
}

.history__clear:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--text-strong);
}

.history__clear:disabled {
  opacity: 0.55;
  cursor: default;
}

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

.history__item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  background: var(--surface-raised);
  border-radius: 12px;
}

.history__expr {
  font-size: 16px;
  color: var(--text);
  overflow-wrap: anywhere;
}

.history__result {
  font-size: 22px;
  font-weight: 800;
  color: var(--text-strong);
  font-variant-numeric: tabular-nums;
}

.history__empty {
  margin: 0;
  font-size: 17px;
  color: var(--text);
}

.history__empty[hidden] {
  display: none;
}

@media (max-width: 860px) {
  body {
    overflow: auto;
  }

  .app {
    grid-template-columns: 1fr;
    gap: 20px;
    align-content: start;
    padding: 12px;
    min-height: 100dvh;
  }

  .calculator {
    padding: 16px;
    gap: 14px;
    border-radius: 20px;
  }

  .display {
    min-height: 108px;
    padding: 12px 16px;
  }

  .display__expression {
    font-size: 19px;
  }

  .display__value {
    font-size: 40px;
  }

  .display__value--error {
    font-size: 24px;
  }

  .keypad {
    gap: 10px;
  }

  .key {
    min-height: 60px;
    font-size: 28px;
  }

  .key--action {
    min-height: 52px;
    font-size: 22px;
  }

  .hint {
    font-size: 13px;
  }

  .history {
    max-height: none;
    padding: 16px;
    border-radius: 20px;
  }

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

@media (max-width: 400px) {
  .key {
    min-height: 56px;
    font-size: 26px;
  }

  .key--action {
    font-size: 20px;
  }
}

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