/* ============================================
   CalcVerse — Premium Scientific Calculator
   Glassmorphism + Animated Dark Theme
   ============================================ */

/* === CSS Variables === */
:root {
  /* Dark Theme (default) */
  --bg-primary: #0a0a1a;
  --bg-glass: rgba(255, 255, 255, 0.04);
  --bg-glass-hover: rgba(255, 255, 255, 0.08);
  --bg-display: rgba(0, 0, 0, 0.4);
  --text-primary: #f0f0f8;
  --text-secondary: rgba(240, 240, 248, 0.6);
  --text-muted: rgba(240, 240, 248, 0.35);
  --border-glass: rgba(255, 255, 255, 0.08);
  --border-glass-active: rgba(255, 255, 255, 0.15);

  /* Accent Colors */
  --accent-primary: #6c5ce7;
  --accent-secondary: #a29bfe;
  --accent-gradient: linear-gradient(135deg, #6c5ce7 0%, #a855f7 50%, #ec4899 100%);
  --accent-glow: rgba(108, 92, 231, 0.4);
  --equals-gradient: linear-gradient(135deg, #6c5ce7, #a855f7);
  --operator-color: #a78bfa;
  --operator-bg: rgba(167, 139, 250, 0.1);
  --operator-bg-hover: rgba(167, 139, 250, 0.2);
  --clear-color: #f87171;
  --clear-bg: rgba(248, 113, 113, 0.1);
  --number-bg: rgba(255, 255, 255, 0.06);
  --number-bg-hover: rgba(255, 255, 255, 0.12);
  --func-color: #22d3ee;
  --func-bg: rgba(34, 211, 238, 0.08);
  --func-bg-hover: rgba(34, 211, 238, 0.15);
  --mem-color: #fbbf24;
  --mem-bg: rgba(251, 191, 36, 0.08);

  /* Shadows */
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 8px 32px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 16px 64px rgba(0, 0, 0, 0.5);
  --shadow-glow: 0 0 40px rgba(108, 92, 231, 0.15);

  /* Layout */
  --radius-sm: 12px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --transition-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-smooth: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-spring: 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* === Light Theme === */
[data-theme="light"] {
  --bg-primary: #f0f0f8;
  --bg-glass: rgba(255, 255, 255, 0.65);
  --bg-glass-hover: rgba(255, 255, 255, 0.8);
  --bg-display: rgba(255, 255, 255, 0.6);
  --text-primary: #1a1a2e;
  --text-secondary: rgba(26, 26, 46, 0.6);
  --text-muted: rgba(26, 26, 46, 0.35);
  --border-glass: rgba(255, 255, 255, 0.4);
  --border-glass-active: rgba(255, 255, 255, 0.6);
  --number-bg: rgba(255, 255, 255, 0.5);
  --number-bg-hover: rgba(255, 255, 255, 0.7);
  --operator-bg: rgba(108, 92, 231, 0.1);
  --operator-bg-hover: rgba(108, 92, 231, 0.2);
  --clear-bg: rgba(248, 113, 113, 0.12);
  --func-bg: rgba(34, 211, 238, 0.1);
  --func-bg-hover: rgba(34, 211, 238, 0.18);
  --mem-bg: rgba(251, 191, 36, 0.12);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 8px 32px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 16px 64px rgba(0, 0, 0, 0.12);
  --shadow-glow: 0 0 40px rgba(108, 92, 231, 0.08);
}

/* === Reset & Base === */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  transition: background var(--transition-smooth);
}

/* === Particle Canvas === */
#particleCanvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

/* === Background Orbs === */
.bg-orbs {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}

.orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.4;
  animation: orbFloat 20s ease-in-out infinite;
}

[data-theme="light"] .orb {
  opacity: 0.25;
}

.orb-1 {
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, #6c5ce7 0%, transparent 70%);
  top: -10%;
  left: -5%;
  animation-delay: 0s;
}

.orb-2 {
  width: 350px;
  height: 350px;
  background: radial-gradient(circle, #ec4899 0%, transparent 70%);
  bottom: -10%;
  right: -5%;
  animation-delay: -5s;
}

.orb-3 {
  width: 300px;
  height: 300px;
  background: radial-gradient(circle, #06b6d4 0%, transparent 70%);
  top: 50%;
  left: 60%;
  animation-delay: -10s;
}

.orb-4 {
  width: 250px;
  height: 250px;
  background: radial-gradient(circle, #f59e0b 0%, transparent 70%);
  top: 30%;
  left: -10%;
  animation-delay: -15s;
}

@keyframes orbFloat {
  0%, 100% { transform: translate(0, 0) scale(1); }
  25% { transform: translate(60px, -40px) scale(1.1); }
  50% { transform: translate(-30px, 60px) scale(0.95); }
  75% { transform: translate(40px, 30px) scale(1.05); }
}

/* === App Container === */
.app-container {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 20px;
  max-height: 100vh;
}

/* === Header === */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 420px;
  padding: 0 4px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  font-size: 28px;
  background: var(--accent-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-weight: 700;
  animation: logoSpin 8s linear infinite;
}

@keyframes logoSpin {
  0%, 100% { transform: rotate(0deg); }
  50% { transform: rotate(360deg); }
}

.logo-text {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.5px;
  background: var(--accent-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-controls {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: var(--bg-glass);
  border: 1px solid var(--border-glass);
  color: var(--text-secondary);
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
  backdrop-filter: blur(10px);
}

.icon-btn:hover {
  background: var(--bg-glass-hover);
  color: var(--text-primary);
  border-color: var(--border-glass-active);
  transform: translateY(-1px);
}

.icon-btn:active {
  transform: translateY(0) scale(0.95);
}

/* Theme toggle icon visibility */
.moon-icon { display: none; }
[data-theme="light"] .sun-icon { display: none; }
[data-theme="light"] .moon-icon { display: block; }

/* === Calculator Body === */
.calculator {
  width: 100%;
  max-width: 420px;
  max-height: calc(100vh - 80px);
  overflow-y: auto;
  background: var(--bg-glass);
  backdrop-filter: blur(40px);
  -webkit-backdrop-filter: blur(40px);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-glass);
  padding: 20px;
  box-shadow: var(--shadow-lg), var(--shadow-glow);
  transition: all var(--transition-smooth);
  animation: calcEntry 0.8s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.calculator::-webkit-scrollbar {
  width: 4px;
}

.calculator::-webkit-scrollbar-track {
  background: transparent;
}

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

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

/* === Display === */
.display-container {
  background: var(--bg-display);
  border-radius: var(--radius-md);
  padding: 20px 24px;
  margin-bottom: 16px;
  min-height: 110px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border-glass);
}

.display-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--accent-secondary), transparent);
  opacity: 0.3;
}

.display-expression {
  font-family: 'JetBrains Mono', monospace;
  font-size: 14px;
  color: var(--text-secondary);
  min-height: 20px;
  text-align: right;
  width: 100%;
  word-break: break-all;
  line-height: 1.4;
  transition: all var(--transition-fast);
}

.display-result {
  font-family: 'JetBrains Mono', monospace;
  font-size: 40px;
  font-weight: 600;
  text-align: right;
  width: 100%;
  word-break: break-all;
  line-height: 1.2;
  transition: all var(--transition-fast);
  background: linear-gradient(135deg, var(--text-primary) 0%, var(--accent-secondary) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.display-result.shrink {
  font-size: 28px;
}

.display-result.shrink-more {
  font-size: 22px;
}

.display-memory {
  position: absolute;
  top: 8px;
  left: 12px;
  font-size: 11px;
  font-weight: 600;
  color: var(--mem-color);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

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

/* Result animation */
.display-result.pop {
  animation: resultPop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes resultPop {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

/* === Mode Toggle === */
.mode-toggle-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  gap: 12px;
}

.mode-toggle {
  display: flex;
  position: relative;
  background: var(--bg-display);
  border-radius: 10px;
  padding: 3px;
  border: 1px solid var(--border-glass);
}

.mode-btn {
  padding: 6px 16px;
  font-size: 12px;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 8px;
  z-index: 1;
  transition: color var(--transition-fast);
}

.mode-btn.active {
  color: var(--text-primary);
}

.mode-slider {
  position: absolute;
  top: 3px;
  left: 3px;
  width: calc(50% - 3px);
  height: calc(100% - 6px);
  background: var(--accent-gradient);
  border-radius: 8px;
  transition: transform var(--transition-spring);
  opacity: 0.2;
}

.mode-toggle[data-active="scientific"] .mode-slider {
  transform: translateX(100%);
}

.angle-toggle {
  display: flex;
  gap: 4px;
  opacity: 0;
  pointer-events: none;
  transition: all var(--transition-smooth);
}

.angle-toggle.visible {
  opacity: 1;
  pointer-events: all;
}

.angle-btn {
  padding: 6px 10px;
  font-size: 11px;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  border: 1px solid var(--border-glass);
  background: var(--bg-display);
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 8px;
  transition: all var(--transition-fast);
}

.angle-btn.active {
  background: var(--func-bg);
  color: var(--func-color);
  border-color: var(--func-color);
}

/* === Scientific Panel === */
.scientific-panel {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.5s cubic-bezier(0.4, 0, 0.2, 1),
              opacity 0.3s ease,
              margin 0.3s ease;
  opacity: 0;
  margin-bottom: 0;
}

.scientific-panel.visible {
  max-height: 250px;
  opacity: 1;
  margin-bottom: 12px;
}

.sci-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

/* === Memory Bar === */
.memory-bar {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}

/* === Button Grid === */
.btn-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

/* === Buttons === */
.btn {
  font-family: 'Inter', sans-serif;
  font-size: 18px;
  font-weight: 500;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  padding: 16px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
  position: relative;
  overflow: hidden;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* Ripple effect */
.btn .ripple {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  transform: scale(0);
  animation: ripple 0.6s ease-out;
  pointer-events: none;
}

@keyframes ripple {
  to {
    transform: scale(4);
    opacity: 0;
  }
}

/* Number Buttons */
.btn-number {
  background: var(--number-bg);
  color: var(--text-primary);
  border-color: var(--border-glass);
}

.btn-number:hover {
  background: var(--number-bg-hover);
  border-color: var(--border-glass-active);
  transform: translateY(-2px);
  box-shadow: var(--shadow-sm);
}

.btn-number:active {
  transform: translateY(0) scale(0.96);
}

/* Operator Buttons */
.btn-operator {
  background: var(--operator-bg);
  color: var(--operator-color);
  border-color: rgba(167, 139, 250, 0.15);
  font-size: 22px;
}

.btn-operator:hover {
  background: var(--operator-bg-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(167, 139, 250, 0.2);
}

.btn-operator:active {
  transform: translateY(0) scale(0.96);
}

.btn-operator.active-op {
  background: var(--operator-bg-hover);
  border-color: var(--operator-color);
  box-shadow: 0 0 16px rgba(167, 139, 250, 0.25);
}

/* Equals Button */
.btn-equals {
  background: var(--equals-gradient);
  color: white;
  font-size: 24px;
  font-weight: 700;
  border: none;
  box-shadow: 0 4px 20px var(--accent-glow);
}

.btn-equals:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 28px var(--accent-glow);
  filter: brightness(1.1);
}

.btn-equals:active {
  transform: translateY(0) scale(0.96);
}

/* Clear Button */
.btn-clear {
  background: var(--clear-bg);
  color: var(--clear-color);
  font-weight: 600;
  border-color: rgba(248, 113, 113, 0.15);
}

.btn-clear:hover {
  background: rgba(248, 113, 113, 0.2);
  transform: translateY(-2px);
}

.btn-clear:active {
  transform: translateY(0) scale(0.96);
}

/* Utility Buttons */
.btn-util {
  background: var(--number-bg);
  color: var(--text-secondary);
  border-color: var(--border-glass);
  font-size: 16px;
}

.btn-util:hover {
  background: var(--number-bg-hover);
  color: var(--text-primary);
  transform: translateY(-2px);
}

.btn-util:active {
  transform: translateY(0) scale(0.96);
}

/* Function Buttons (Scientific) */
.btn-func {
  background: var(--func-bg);
  color: var(--func-color);
  font-size: 13px;
  font-weight: 600;
  padding: 10px 4px;
  border-color: rgba(34, 211, 238, 0.1);
}

.btn-func:hover {
  background: var(--func-bg-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(34, 211, 238, 0.15);
}

.btn-func:active {
  transform: translateY(0) scale(0.96);
}

/* Memory Buttons */
.btn-mem {
  flex: 1;
  background: var(--mem-bg);
  color: var(--mem-color);
  font-size: 12px;
  font-weight: 600;
  padding: 8px 4px;
  border-color: rgba(251, 191, 36, 0.1);
}

.btn-mem:hover {
  background: rgba(251, 191, 36, 0.18);
  transform: translateY(-1px);
}

.btn-mem:active {
  transform: translateY(0) scale(0.96);
}

/* === History Panel === */
.history-panel {
  position: fixed;
  top: 0;
  right: -380px;
  width: 360px;
  height: 100vh;
  background: var(--bg-glass);
  backdrop-filter: blur(40px);
  -webkit-backdrop-filter: blur(40px);
  border-left: 1px solid var(--border-glass);
  padding: 24px;
  z-index: 100;
  transition: right var(--transition-spring);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
}

.history-panel.open {
  right: 0;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.history-header h2 {
  font-size: 18px;
  font-weight: 600;
  background: var(--accent-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.history-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-list::-webkit-scrollbar {
  width: 4px;
}

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

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

.history-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  height: 200px;
  color: var(--text-muted);
  font-size: 14px;
}

.history-item {
  background: var(--bg-display);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  cursor: pointer;
  border: 1px solid var(--border-glass);
  transition: all var(--transition-fast);
  animation: historySlide 0.3s ease-out;
}

@keyframes historySlide {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.history-item:hover {
  background: var(--bg-glass-hover);
  border-color: var(--border-glass-active);
  transform: translateX(-4px);
}

.history-item .expr {
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.history-item .ans {
  font-family: 'JetBrains Mono', monospace;
  font-size: 18px;
  font-weight: 600;
  color: var(--accent-secondary);
}

/* === Toast === */
.toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  color: var(--text-primary);
  padding: 12px 24px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-glass);
  font-size: 14px;
  font-weight: 500;
  z-index: 200;
  opacity: 0;
  transition: all var(--transition-spring);
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* === Overlay for History (mobile) === */
.history-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-smooth);
}

.history-overlay.active {
  opacity: 1;
  pointer-events: all;
}

/* === Responsive === */
@media (max-width: 460px) {
  .app-container {
    padding: 12px;
    gap: 10px;
  }

  .calculator {
    padding: 14px;
    border-radius: var(--radius-md);
  }

  .display-container {
    padding: 16px 18px;
    min-height: 90px;
  }

  .display-result {
    font-size: 32px;
  }

  .btn {
    padding: 14px 6px;
    font-size: 16px;
  }

  .btn-grid {
    gap: 6px;
  }

  .btn-operator {
    font-size: 20px;
  }

  .btn-equals {
    font-size: 22px;
  }

  .history-panel {
    width: 100%;
    right: -100%;
  }
}

@media (max-height: 750px) {
  .btn {
    padding: 12px 6px;
  }

  .display-container {
    min-height: 85px;
    padding: 14px 18px;
  }

  .display-result {
    font-size: 32px;
  }

  .btn-func {
    padding: 8px 4px;
  }
}

/* === Accessibility === */
.btn:focus-visible,
.icon-btn:focus-visible,
.mode-btn:focus-visible,
.angle-btn:focus-visible {
  outline: 2px solid var(--accent-secondary);
  outline-offset: 2px;
}

/* === Selection === */
::selection {
  background: var(--accent-primary);
  color: white;
}

/* === Error state === */
.display-result.error {
  color: var(--clear-color);
  -webkit-text-fill-color: var(--clear-color);
  font-size: 20px;
}
