/* ============================================
   Scientific Calculator - Glassmorphism Theme
   ============================================ */

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

:root {
  --bg-primary: #0a0a1a;
  --bg-secondary: #12122a;
  --glass-bg: rgba(255, 255, 255, 0.05);
  --glass-border: rgba(255, 255, 255, 0.1);
  --glass-highlight: rgba(255, 255, 255, 0.15);
  --text-primary: #e8e8f0;
  --text-secondary: #9090b0;
  --accent-blue: #4a9eff;
  --accent-purple: #8b5cf6;
  --accent-orange: #f59e0b;
  --accent-red: #ef4444;
  --accent-green: #10b981;
  --btn-number: rgba(255, 255, 255, 0.08);
  --btn-number-hover: rgba(255, 255, 255, 0.15);
  --btn-operator: rgba(74, 158, 255, 0.15);
  --btn-operator-hover: rgba(74, 158, 255, 0.3);
  --btn-scientific: rgba(139, 92, 246, 0.15);
  --btn-scientific-hover: rgba(139, 92, 246, 0.3);
  --btn-memory: rgba(16, 185, 129, 0.15);
  --btn-memory-hover: rgba(16, 185, 129, 0.3);
  --btn-equals: linear-gradient(135deg, #4a9eff, #8b5cf6);
  --btn-clear: rgba(239, 68, 68, 0.2);
  --btn-clear-hover: rgba(239, 68, 68, 0.35);
  --shadow-soft: 0 8px 32px rgba(0, 0, 0, 0.3);
  --shadow-glow: 0 0 40px rgba(74, 158, 255, 0.1);
  --radius: 16px;
  --radius-sm: 12px;
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

html {
  font-size: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

/* Animated background orbs */
body::before,
body::after {
  content: '';
  position: fixed;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.3;
  z-index: 0;
  animation: float 8s ease-in-out infinite alternate;
}

body::before {
  width: 400px;
  height: 400px;
  background: var(--accent-blue);
  top: -100px;
  right: -100px;
}

body::after {
  width: 350px;
  height: 350px;
  background: var(--accent-purple);
  bottom: -100px;
  left: -100px;
  animation-delay: -4s;
}

@keyframes float {
  0%   { transform: translate(0, 0) scale(1); }
  100% { transform: translate(30px, 20px) scale(1.1); }
}

/* ---- Layout ---- */
.calculator-container {
  display: flex;
  gap: 16px;
  z-index: 1;
  padding: 16px;
  max-width: 100vw;
}

.calculator {
  width: 380px;
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-soft), var(--shadow-glow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ---- Display ---- */
.display {
  padding: 24px 20px 16px;
  position: relative;
  min-height: 120px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.display-expression {
  font-size: 0.95rem;
  color: var(--text-secondary);
  text-align: right;
  min-height: 1.4em;
  word-break: break-all;
  line-height: 1.4;
  padding-right: 2px;
}

.display-result {
  font-size: 2.8rem;
  font-weight: 300;
  text-align: right;
  letter-spacing: -0.5px;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: font-size var(--transition);
}

.display-result.long {
  font-size: 1.8rem;
}

.display-memory {
  position: absolute;
  top: 10px;
  left: 16px;
  font-size: 0.7rem;
  color: var(--accent-green);
  font-weight: 600;
  letter-spacing: 1px;
  opacity: 0;
  transition: opacity var(--transition);
}

.display-memory.visible {
  opacity: 1;
}

/* ---- Toolbar ---- */
.toolbar {
  display: flex;
  gap: 8px;
  padding: 0 12px 8px;
}

.toolbar-btn {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  color: var(--text-secondary);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 1px;
  padding: 4px 12px;
  border-radius: 20px;
  cursor: pointer;
  transition: all var(--transition);
}

.toolbar-btn:hover {
  background: var(--glass-highlight);
  color: var(--text-primary);
}

.toolbar-btn.active {
  background: rgba(74, 158, 255, 0.2);
  color: var(--accent-blue);
  border-color: rgba(74, 158, 255, 0.3);
}

/* ---- Buttons Grid ---- */
.buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 8px 12px 12px;
}

.btn {
  position: relative;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 1.05rem;
  font-weight: 500;
  cursor: pointer;
  padding: 14px 8px;
  color: var(--text-primary);
  transition: all var(--transition);
  overflow: hidden;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* Ripple effect */
.btn::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at var(--ripple-x, 50%) var(--ripple-y, 50%),
    rgba(255, 255, 255, 0.3) 0%, transparent 60%);
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
}

.btn.ripple::after {
  opacity: 1;
  transition: opacity 0s;
}

/* Button types */
.btn-number {
  background: var(--btn-number);
  border-color: var(--glass-border);
}
.btn-number:hover { background: var(--btn-number-hover); }

.btn-operator {
  background: var(--btn-operator);
  color: var(--accent-blue);
  border-color: rgba(74, 158, 255, 0.1);
}
.btn-operator:hover { background: var(--btn-operator-hover); }

.btn-scientific {
  background: var(--btn-scientific);
  color: var(--accent-purple);
  font-size: 0.9rem;
  border-color: rgba(139, 92, 246, 0.1);
}
.btn-scientific:hover { background: var(--btn-scientific-hover); }

.btn-memory {
  background: var(--btn-memory);
  color: var(--accent-green);
  font-size: 0.85rem;
  font-weight: 600;
  border-color: rgba(16, 185, 129, 0.1);
}
.btn-memory:hover { background: var(--btn-memory-hover); }

.btn-clear {
  background: var(--btn-clear);
  color: var(--accent-red);
  font-weight: 600;
  border-color: rgba(239, 68, 68, 0.15);
}
.btn-clear:hover { background: var(--btn-clear-hover); }

.btn-backspace {
  background: var(--btn-number);
  color: var(--accent-orange);
  font-size: 1.3rem;
  border-color: var(--glass-border);
}
.btn-backspace:hover { background: var(--btn-number-hover); }

.btn-equals {
  grid-column: 1 / -1;
  background: var(--btn-equals);
  color: #fff;
  font-size: 1.4rem;
  font-weight: 600;
  padding: 16px;
  border: none;
  box-shadow: 0 4px 15px rgba(74, 158, 255, 0.3);
}
.btn-equals:hover {
  filter: brightness(1.15);
  box-shadow: 0 4px 20px rgba(74, 158, 255, 0.5);
}

.btn:active {
  transform: scale(0.95);
}

/* ---- History Panel ---- */
.history-panel {
  width: 280px;
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-soft);
  display: none;
  flex-direction: column;
  max-height: 600px;
}

.history-panel.open {
  display: flex;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--glass-border);
  font-weight: 600;
  font-size: 0.95rem;
}

.history-clear {
  background: none;
  border: none;
  color: var(--accent-red);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  transition: background var(--transition);
}
.history-clear:hover {
  background: rgba(239, 68, 68, 0.15);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.history-list::-webkit-scrollbar {
  width: 4px;
}
.history-list::-webkit-scrollbar-thumb {
  background: var(--glass-border);
  border-radius: 4px;
}

.history-empty {
  text-align: center;
  color: var(--text-secondary);
  padding: 40px 16px;
  font-size: 0.85rem;
}

.history-item {
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  transition: background var(--transition);
  margin-bottom: 4px;
}
.history-item:hover {
  background: var(--glass-highlight);
}

.history-item-expr {
  font-size: 0.8rem;
  color: var(--text-secondary);
  word-break: break-all;
}

.history-item-result {
  font-size: 1.1rem;
  font-weight: 500;
  color: var(--text-primary);
}

/* ---- Error State ---- */
.display-result.error {
  color: var(--accent-red);
  font-size: 1.4rem;
}

/* ---- Responsive ---- */
@media (max-width: 720px) {
  .calculator-container {
    flex-direction: column;
    align-items: center;
  }

  .calculator {
    width: 100%;
    max-width: 400px;
  }

  .history-panel {
    width: 100%;
    max-width: 400px;
    max-height: 300px;
  }
}

@media (max-width: 400px) {
  .calculator {
    border-radius: 0;
    border: none;
    width: 100vw;
    min-height: 100vh;
  }

  .calculator-container {
    padding: 0;
  }

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

  .display-result {
    font-size: 2.4rem;
  }

  .buttons {
    gap: 6px;
    padding: 6px 8px 8px;
  }

  .btn {
    padding: 16px 6px;
    font-size: 1rem;
    border-radius: 10px;
  }

  .btn-equals {
    padding: 18px;
  }

  .history-panel {
    border-radius: 0;
    border: none;
    max-height: 250px;
  }
}
