:root {
  --bg-top: #1a2332;
  --bg-bottom: #0d121a;
  --shell: #243044;
  --shell-edge: #1a2433;
  --screen: #0a0e14;
  --screen-glow: rgba(120, 200, 180, 0.08);
  --text: #e8eef6;
  --muted: #7a8aa0;
  --key: #334155;
  --key-hover: #3e4d63;
  --key-press: #2a3648;
  --fn: #3d4a5c;
  --op: #c45c26;
  --op-hover: #d46a32;
  --eq: #2a9d8f;
  --eq-hover: #34b3a3;
  --shadow: 0 28px 60px rgba(0, 0, 0, 0.45);
  --radius: 28px;
}

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

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

body {
  font-family: "Outfit", sans-serif;
  color: var(--text);
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, #2a3b55 0%, transparent 55%),
    linear-gradient(165deg, var(--bg-top), var(--bg-bottom));
  display: grid;
  place-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  overflow-x: hidden;
}

.stage {
  width: min(100%, 360px);
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
  align-items: stretch;
}

.stage--history {
  width: min(100%, 360px);
}

.calc {
  background: linear-gradient(180deg, #2c3d55 0%, var(--shell) 40%, var(--shell-edge) 100%);
  border-radius: var(--radius);
  padding: 22px 18px 18px;
  box-shadow:
    var(--shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.06);
  animation: viewIn 0.2s ease;
}

.calc__brand {
  margin: 0 0 14px;
  padding: 0 4px;
  font-family: "Space Grotesk", sans-serif;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--text);
  text-align: center;
}

.calc[hidden] {
  display: none !important;
}

.calc__screen {
  background: var(--screen);
  border-radius: 18px;
  padding: 18px 20px 14px;
  margin-bottom: 18px;
  min-height: 108px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  box-shadow:
    inset 0 2px 8px rgba(0, 0, 0, 0.55),
    0 0 0 1px rgba(255, 255, 255, 0.04);
  background-image: linear-gradient(180deg, var(--screen-glow), transparent 60%);
}

.calc__expr {
  font-family: "Space Grotesk", sans-serif;
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--muted);
  min-height: 1.25em;
  letter-spacing: 0.02em;
  word-break: break-all;
  text-align: right;
  width: 100%;
}

.calc__display {
  font-family: "Space Grotesk", sans-serif;
  font-size: clamp(2rem, 8vw, 2.75rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.15;
  text-align: right;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.key {
  appearance: none;
  border: none;
  cursor: pointer;
  font-family: "Outfit", sans-serif;
  font-size: 1.35rem;
  font-weight: 500;
  color: var(--text);
  background: var(--key);
  border-radius: 14px;
  height: 64px;
  transition: background 0.12s ease, transform 0.08s ease, box-shadow 0.12s ease;
  box-shadow:
    0 2px 0 rgba(0, 0, 0, 0.25),
    inset 0 1px 0 rgba(255, 255, 255, 0.06);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

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

.key:active,
.key.is-pressed {
  background: var(--key-press);
  transform: translateY(1px);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.35);
}

.key--fn {
  background: var(--fn);
  color: #c5d0de;
  font-size: 1.1rem;
  font-weight: 600;
}

.key--fn:hover {
  background: #4a586c;
}

@keyframes viewIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.key--op {
  background: var(--op);
  color: #fff7f2;
  font-size: 1.5rem;
}

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

.key--op.is-active {
  outline: 2px solid rgba(255, 210, 170, 0.85);
  outline-offset: -2px;
}

.key--eq {
  background: var(--eq);
  color: #f2fffc;
  font-size: 1.55rem;
  font-weight: 600;
}

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

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

.history {
  background: linear-gradient(180deg, #2c3d55 0%, var(--shell) 40%, var(--shell-edge) 100%);
  border-radius: var(--radius);
  padding: 22px 18px 18px;
  box-shadow:
    var(--shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.06);
  display: flex;
  flex-direction: column;
  min-height: 0;
  width: 100%;
  height: var(--calc-h, auto);
  animation: viewIn 0.22s ease;
}

.history[hidden] {
  display: none !important;
}

.history__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  padding: 0 4px;
  flex-shrink: 0;
}

.history__title {
  margin: 0;
  font-family: "Space Grotesk", sans-serif;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.history__total {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 12px;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(10, 14, 20, 0.55);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  flex-shrink: 0;
}

.history__total[hidden] {
  display: none !important;
}

.history__total-label {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 500;
}

.history__total-value {
  font-family: "Space Grotesk", sans-serif;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--eq-hover);
  word-break: break-all;
  text-align: right;
}

.history__actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.history__clear,
.history__close {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--muted);
  font-family: "Outfit", sans-serif;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  padding: 8px 10px;
  border-radius: 8px;
  transition: color 0.12s ease, background 0.12s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.history__close {
  font-size: 1.45rem;
  line-height: 1;
  min-width: 40px;
  min-height: 40px;
}

.history__clear:hover,
.history__clear:active {
  color: #f0a090;
  background: rgba(255, 255, 255, 0.05);
}

.history__close:hover,
.history__close:active {
  color: var(--text);
  background: rgba(255, 255, 255, 0.05);
}

.history__list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-height: 0;
  scrollbar-width: thin;
  scrollbar-color: #3d4a5c transparent;
}

.history__item {
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.history__item.is-new {
  opacity: 0;
  transform: translateY(-6px);
}

.history__btn {
  appearance: none;
  width: 100%;
  border: none;
  cursor: pointer;
  text-align: left;
  background: rgba(10, 14, 20, 0.45);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: background 0.12s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.history__btn:hover,
.history__btn:active {
  background: rgba(10, 14, 20, 0.7);
}

.history__expr {
  font-family: "Space Grotesk", sans-serif;
  font-size: 0.82rem;
  color: var(--muted);
  word-break: break-all;
}

.history__result {
  font-family: "Space Grotesk", sans-serif;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.history__empty {
  margin: auto 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  padding: 24px 8px;
}

@media (max-width: 380px) {
  .stage,
  .stage--history {
    width: 100%;
  }
}
