body {
  background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  overflow: hidden;
}

.main-container {
  display: flex;
  gap: 0;
  align-items: flex-start;
  position: relative;
}

.calculator {
  width: 320px;
  padding: 20px;
  border-radius: 24px;
  background: rgba(15, 32, 39, 0.85);
  backdrop-filter: blur(20px);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  border: 3px solid rgb(45, 94, 115);
  z-index: 2;
  animation: slideInUp 0.6s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.calc-header {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}

.history-toggle-btn {
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.6);
  font-size: 20px;
  cursor: pointer;
  transition: color 0.2s;
  padding: 5px;
}

.display-wrapper {
  background: rgba(0, 0, 0, 0.3);
  border-radius: 16px;
  margin-bottom: 20px;
  padding: 15px 20px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-end;
  min-height: 100px;
}

#expression {
  color: rgba(255, 255, 255, 0.6);
  font-size: 24px;
  min-height: 30px;
  margin-bottom: 5px;
  width: 100%;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transform-origin: right bottom;
}

#expression.animate {
  animation: slideUpSmall 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
}

#display {
  background: transparent;
  color: white;
  font-size: 56px;
  text-align: right;
  padding: 0;
  border-radius: 0;
  margin-bottom: 0;
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
}

#display.fade-in {
  animation: fadeInResult 0.4s ease-out;
}

@keyframes slideUpSmall {
  from {
    opacity: 0;
    transform: translateY(30px) scale(1.5);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

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

.digit-animate {
  opacity: 0;
  display: inline-block;
  animation: fadeInDigit 0.5s cubic-bezier(0.23, 1, 0.32, 1) forwards;
  will-change: transform, opacity, filter;
  transform: translateZ(0); /* Force hardware acceleration */
  backface-visibility: hidden;
}

@keyframes fadeInDigit {
  0% {
    opacity: 0;
    transform: translateY(20px) scale(0.5);
    filter: blur(5px);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
    filter: blur(0);
  }
}

#display::-webkit-scrollbar {
  height: 6px;
}

#display::-webkit-scrollbar-track {
  background: transparent;
}

#display::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 3px;
}

#display::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.5);
}

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

.btn {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border: none;
  padding: 20px;
  font-size: 24px;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.1s ease;
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.3);
  -webkit-tap-highlight-color: transparent;
  margin-bottom: 5px; /* Space for shadow */
}

@media (hover: hover) {
  .btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: translateY(-2px);
    box-shadow: 0 7px 0 rgba(0, 0, 0, 0.3);
  }
  
  .op:hover {
    background: #48cae4;
    box-shadow: 0 7px 0 #0077b6;
  }
  
  .equals:hover {
    background: #023e8a;
    box-shadow: 0 7px 0 #005f87;
  }
  
  .clear-history-btn:hover {
    background: rgba(255, 255, 255, 0.2);
  }
  
  .history-toggle-btn:hover {
    color: white;
  }
}

.btn:active {
  transform: translateY(5px) !important;
  box-shadow: 0 0 0 rgba(0, 0, 0, 0.3) !important;
  background: rgba(255, 255, 255, 0.2);
}

.op {
  background: #00b4d8;
  color: white;
  box-shadow: 0 5px 0 #0077b6;
}

.op:active {
  background: #0096c7;
  box-shadow: 0 0 0 #0077b6 !important;
}

.equals {
  background: #0077b6;
  grid-row: span 2;
  box-shadow: 0 5px 0 #005f87;
}

.equals:active {
  background: #005f87;
  box-shadow: 0 0 0 #005f87 !important;
}

.zero {
  grid-column: span 2;
}

.history {
  width: 0;
  height: 540px;
  background: rgba(15, 32, 39, 0.9);
  border-radius: 0 24px 24px 0;
  padding: 20px 0;
  color: white;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1), padding 0.4s, opacity 0.4s, margin-left 0.4s;
  opacity: 0;
  margin-left: -20px;
  z-index: 1;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.history.open {
  width: 280px;
  padding: 20px;
  opacity: 1;
  margin-left: 10px;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  min-width: 240px;
}

.history-header h3 {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
}

.clear-history-btn {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
  transition: background 0.2s;
}

#history-list {
  list-style: none;
  padding: 0;
  margin: 0;
  overflow-y: auto;
  flex-grow: 1;
  min-width: 240px;
}

#history-list li {
  padding: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  font-size: 16px;
  color: #aaa;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  animation: fadeIn 0.3s ease;
}

#history-list li span.result {
  color: white;
  font-size: 22px;
  font-weight: 600;
  margin-top: 4px;
}

#history-list::-webkit-scrollbar {
  width: 6px;
}

#history-list::-webkit-scrollbar-track {
  background: transparent;
}

#history-list::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

#history-list::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

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

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

@media (max-width: 480px) {
  body {
    align-items: flex-start; /* Align to top */
  }

  .main-container {
    width: 100%;
    height: 100vh;
    overflow: hidden;
  }

  .calculator {
    width: 100%;
    height: 100%;
    border-radius: 0;
    border: none;
    padding: 20px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
  }

  #display {
    margin-top: auto; /* Push to bottom of top section */
    margin-bottom: 30px;
    font-size: 60px;
  }

  .buttons {
    height: 65%;
    gap: 15px;
  }

  .history {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    z-index: 100;
    border-radius: 0;
    margin-left: 0;
    background: rgba(15, 32, 39, 0.98); /* More opaque on mobile */
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    width: 85% !important; /* Override width animation */
    opacity: 1 !important;
    padding: 20px !important;
    box-sizing: border-box;
    display: flex; /* Keep flex layout */
  }

  .history.open {
    transform: translateX(0);
    margin-left: 0;
  }
  
  /* Add a backdrop when history is open */
  .history.open::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0,0,0,0.5);
    z-index: -1;
    pointer-events: none;
  }
}

.enter-animation {
  opacity: 0;
  animation: enterKey 0.7s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
  will-change: transform, opacity, filter;
  transform: translateZ(0); /* Force hardware acceleration */
  backface-visibility: hidden;
}

@keyframes enterKey {
  0% {
    opacity: 0;
    transform: scale(0.5);
    filter: blur(10px);
  }
  100% {
    opacity: 1;
    transform: scale(1);
    filter: blur(0);
  }
}
