/* ============================================
   CALCULATOR PRO - PREMIUM EDITION
   Modern, Glassmorphism Design System
   ============================================ */

/* --- CSS Custom Properties (Design Tokens) --- */
:root {
  /* Typography */
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", "Fira Code", monospace;

  /* Spacing Scale */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;
  --space-2xl: 3rem;

  /* Border Radius */
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-full: 9999px;

  /* Transitions */
  --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-base: 250ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 350ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-bounce: 500ms cubic-bezier(0.34, 1.56, 0.64, 1);

  /* Z-Index Scale */
  --z-base: 1;
  --z-elevated: 10;
  --z-overlay: 100;
  --z-modal: 1000;
}

/* --- Dark Theme (Default) --- */
[data-theme="dark"] {
  --color-bg: #0a0a0f;
  --color-bg-gradient-1: #1a1a2e;
  --color-bg-gradient-2: #16213e;
  --color-bg-gradient-3: #0f3460;

  --color-surface: rgba(255, 255, 255, 0.03);
  --color-surface-hover: rgba(255, 255, 255, 0.06);
  --color-surface-active: rgba(255, 255, 255, 0.08);

  --color-glass: rgba(255, 255, 255, 0.05);
  --color-glass-border: rgba(255, 255, 255, 0.1);
  --color-glass-highlight: rgba(255, 255, 255, 0.15);

  --color-text: #ffffff;
  --color-text-secondary: rgba(255, 255, 255, 0.6);
  --color-text-muted: rgba(255, 255, 255, 0.4);

  --color-primary: #6366f1;
  --color-primary-glow: rgba(99, 102, 241, 0.4);
  --color-primary-light: #818cf8;

  --color-accent: #22d3ee;
  --color-accent-glow: rgba(34, 211, 238, 0.3);

  --color-success: #10b981;
  --color-warning: #f59e0b;
  --color-danger: #ef4444;
  --color-danger-glow: rgba(239, 68, 68, 0.3);

  --color-num-bg: rgba(255, 255, 255, 0.06);
  --color-num-hover: rgba(255, 255, 255, 0.1);
  --color-num-active: rgba(255, 255, 255, 0.15);

  --color-op-bg: rgba(99, 102, 241, 0.15);
  --color-op-hover: rgba(99, 102, 241, 0.25);
  --color-op-active: rgba(99, 102, 241, 0.35);

  --color-func-bg: rgba(34, 211, 238, 0.1);
  --color-func-hover: rgba(34, 211, 238, 0.2);
  --color-func-active: rgba(34, 211, 238, 0.3);

  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
  --shadow-glow: 0 0 40px var(--color-primary-glow);

  --blur-bg: blur(100px);
  --blur-glass: blur(20px);
}

/* --- Light Theme --- */
[data-theme="light"] {
  --color-bg: #f8fafc;
  --color-bg-gradient-1: #e0e7ff;
  --color-bg-gradient-2: #c7d2fe;
  --color-bg-gradient-3: #a5b4fc;

  --color-surface: rgba(0, 0, 0, 0.02);
  --color-surface-hover: rgba(0, 0, 0, 0.04);
  --color-surface-active: rgba(0, 0, 0, 0.06);

  --color-glass: rgba(255, 255, 255, 0.7);
  --color-glass-border: rgba(0, 0, 0, 0.08);
  --color-glass-highlight: rgba(255, 255, 255, 0.9);

  --color-text: #1e293b;
  --color-text-secondary: rgba(30, 41, 59, 0.7);
  --color-text-muted: rgba(30, 41, 59, 0.5);

  --color-primary: #4f46e5;
  --color-primary-glow: rgba(79, 70, 229, 0.3);
  --color-primary-light: #6366f1;

  --color-accent: #0891b2;
  --color-accent-glow: rgba(8, 145, 178, 0.2);

  --color-success: #059669;
  --color-warning: #d97706;
  --color-danger: #dc2626;
  --color-danger-glow: rgba(220, 38, 38, 0.2);

  --color-num-bg: rgba(0, 0, 0, 0.04);
  --color-num-hover: rgba(0, 0, 0, 0.08);
  --color-num-active: rgba(0, 0, 0, 0.12);

  --color-op-bg: rgba(79, 70, 229, 0.1);
  --color-op-hover: rgba(79, 70, 229, 0.18);
  --color-op-active: rgba(79, 70, 229, 0.25);

  --color-func-bg: rgba(8, 145, 178, 0.08);
  --color-func-hover: rgba(8, 145, 178, 0.15);
  --color-func-active: rgba(8, 145, 178, 0.22);

  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.12);
  --shadow-glow: 0 0 40px var(--color-primary-glow);

  --blur-bg: blur(80px);
  --blur-glass: blur(16px);
}

/* --- CSS Reset & Base --- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-sans);
  background-color: var(--color-bg);
  color: var(--color-text);
  min-height: 100vh;
  overflow-x: hidden;
  position: relative;
}

/* --- Animated Background --- */
.bg-gradient {
  position: fixed;
  inset: 0;
  background: linear-gradient(
    135deg,
    var(--color-bg) 0%,
    var(--color-bg-gradient-1) 100%
  );
  z-index: -3;
}

.bg-blur {
  position: fixed;
  border-radius: 50%;
  filter: var(--blur-bg);
  opacity: 0.6;
  z-index: -2;
  animation: float 20s ease-in-out infinite;
}

.bg-blur-1 {
  width: 600px;
  height: 600px;
  background: var(--color-bg-gradient-2);
  top: -200px;
  right: -100px;
  animation-delay: 0s;
}

.bg-blur-2 {
  width: 500px;
  height: 500px;
  background: var(--color-bg-gradient-3);
  bottom: -150px;
  left: -100px;
  animation-delay: -7s;
}

.bg-blur-3 {
  width: 400px;
  height: 400px;
  background: var(--color-primary);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0.3;
  animation-delay: -14s;
}

@keyframes float {
  0%,
  100% {
    transform: translate(0, 0) scale(1);
  }
  25% {
    transform: translate(30px, -30px) scale(1.05);
  }
  50% {
    transform: translate(-20px, 20px) scale(0.95);
  }
  75% {
    transform: translate(-30px, -20px) scale(1.02);
  }
}

/* --- Main App Container --- */
.app {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-lg);
}

/* --- Glass Card --- */
.card {
  width: 100%;
  max-width: 680px;
  border-radius: var(--radius-xl);
  overflow: hidden;
  animation: cardEntrance 0.6s ease-out;
}

.card.glass {
  background: var(--color-glass);
  backdrop-filter: var(--blur-glass);
  -webkit-backdrop-filter: var(--blur-glass);
  border: 1px solid var(--color-glass-border);
  box-shadow: var(--shadow-lg), inset 0 1px 0 var(--color-glass-highlight);
}

@keyframes cardEntrance {
  from {
    opacity: 0;
    transform: translateY(30px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* --- Card Header --- */
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--color-glass-border);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.brand-icon {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  background: linear-gradient(
    135deg,
    var(--color-primary),
    var(--color-accent)
  );
  border-radius: var(--radius-md);
  color: white;
}

.brand-text {
  display: flex;
  align-items: baseline;
  gap: var(--space-xs);
}

.brand-text h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.badge {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 0.2em 0.5em;
  background: linear-gradient(
    135deg,
    var(--color-primary),
    var(--color-accent)
  );
  color: white;
  border-radius: var(--radius-full);
}

.controls {
  display: flex;
  gap: var(--space-xs);
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  background: var(--color-surface);
  border: 1px solid var(--color-glass-border);
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: all var(--transition-base);
}

.icon-btn:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
  transform: translateY(-2px);
}

.icon-btn:active {
  transform: translateY(0) scale(0.95);
}

/* Theme Toggle Icons */
[data-theme="dark"] .icon-sun {
  display: block;
}
[data-theme="dark"] .icon-moon {
  display: none;
}
[data-theme="light"] .icon-sun {
  display: none;
}
[data-theme="light"] .icon-moon {
  display: block;
}

/* --- Display Area --- */
.display {
  padding: var(--space-lg);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-glass-border);
  position: relative;
}

.display-inner {
  min-height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: var(--space-xs);
}

.expr {
  font-family: var(--font-mono);
  font-size: 1.1rem;
  font-weight: 400;
  color: var(--color-text-secondary);
  text-align: right;
  overflow-x: auto;
  white-space: nowrap;
  padding: var(--space-xs) 0;
  scrollbar-width: thin;
  scrollbar-color: var(--color-glass-border) transparent;
}

.expr::-webkit-scrollbar {
  height: 4px;
}

.expr::-webkit-scrollbar-thumb {
  background: var(--color-glass-border);
  border-radius: var(--radius-full);
}

.result {
  display: flex;
  align-items: baseline;
  justify-content: flex-end;
  gap: var(--space-sm);
}

.result-prefix {
  font-size: 1.5rem;
  color: var(--color-text-muted);
  font-weight: 300;
}

.result-value {
  font-family: var(--font-mono);
  font-size: clamp(2rem, 8vw, 3rem);
  font-weight: 600;
  color: var(--color-text);
  letter-spacing: -1px;
  transition: all var(--transition-base);
}

.display-indicators {
  position: absolute;
  top: var(--space-md);
  right: var(--space-lg);
  display: flex;
  gap: var(--space-xs);
}

.indicator {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 0.3em 0.6em;
  background: var(--color-surface);
  border: 1px solid var(--color-glass-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  opacity: 0;
  transition: all var(--transition-base);
}

.indicator.active {
  opacity: 1;
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: white;
}

/* --- Button Pad --- */
.pad {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: var(--space-sm);
  padding: var(--space-md);
}

.group {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.group-right {
  transition: all var(--transition-slow);
  overflow: hidden;
}

.group-right.hidden {
  width: 0;
  opacity: 0;
  padding: 0;
  margin: 0;
}

.row {
  display: flex;
  gap: var(--space-xs);
}

/* --- Buttons Base --- */
.btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  height: 48px;
  padding: var(--space-sm) var(--space-md);
  border: none;
  border-radius: var(--radius-md);
  font-family: var(--font-sans);
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition-fast);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.btn::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(
    135deg,
    transparent 0%,
    rgba(255, 255, 255, 0.1) 100%
  );
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.btn:hover::before {
  opacity: 1;
}

.btn:active {
  transform: scale(0.95);
}

.btn span,
.btn svg {
  position: relative;
  z-index: 1;
}

/* Ripple Effect */
.btn::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  background: radial-gradient(
    circle,
    rgba(255, 255, 255, 0.3) 0%,
    transparent 70%
  );
  transform: scale(0);
  opacity: 0;
  transition: transform 0.5s, opacity 0.3s;
}

.btn:active::after {
  transform: scale(2);
  opacity: 1;
  transition: 0s;
}

/* --- Button Variants --- */

/* Number Buttons */
.btn-num {
  flex: 1;
  background: var(--color-num-bg);
  color: var(--color-text);
  font-weight: 600;
  font-size: 1.2rem;
}

.btn-num:hover {
  background: var(--color-num-hover);
}

.btn-num:active {
  background: var(--color-num-active);
}

/* Zero Button */
.btn-zero {
  grid-column: span 2;
}

/* Operator Buttons */
.btn-op {
  background: var(--color-op-bg);
  color: var(--color-primary-light);
  font-weight: 600;
  font-size: 1.3rem;
}

.btn-op:hover {
  background: var(--color-op-hover);
  box-shadow: 0 0 20px var(--color-primary-glow);
}

.btn-op:active {
  background: var(--color-op-active);
}

/* Function Buttons */
.btn-func {
  flex: 1;
  background: var(--color-func-bg);
  color: var(--color-accent);
  font-size: 0.85rem;
  font-weight: 500;
}

.btn-func:hover {
  background: var(--color-func-hover);
}

.btn-func:active {
  background: var(--color-func-active);
}

.btn-func sup {
  font-size: 0.7em;
}

/* Constants (π, e) */
.btn-const {
  font-family: var(--font-mono);
  font-size: 1.1rem;
}

/* Memory Buttons */
.btn-memory {
  flex: 1;
  height: 36px;
  background: var(--color-surface);
  color: var(--color-text-secondary);
  font-size: 0.75rem;
  font-weight: 600;
}

.btn-memory:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

/* Utility Buttons */
.btn-util {
  flex: 1;
  height: 40px;
  background: var(--color-surface);
  color: var(--color-text-secondary);
  font-size: 0.85rem;
}

.btn-util:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

/* Warning Buttons (Backspace) */
.btn-warn {
  flex: 1;
  background: rgba(245, 158, 11, 0.15);
  color: var(--color-warning);
}

.btn-warn:hover {
  background: rgba(245, 158, 11, 0.25);
}

/* Danger Buttons (AC) */
.btn-danger {
  flex: 1;
  background: rgba(239, 68, 68, 0.15);
  color: var(--color-danger);
  font-weight: 700;
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.25);
  box-shadow: 0 0 20px var(--color-danger-glow);
}

/* --- Equals Button (Hero Button) --- */
.btn-equals {
  width: 100%;
  height: 56px;
  background: linear-gradient(
    135deg,
    var(--color-primary),
    var(--color-primary-light)
  );
  color: white;
  font-size: 1.8rem;
  font-weight: 700;
  border-radius: var(--radius-lg);
  box-shadow: 0 4px 20px var(--color-primary-glow);
  position: relative;
  overflow: hidden;
}

.btn-equals:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px var(--color-primary-glow);
}

.btn-equals:active {
  transform: translateY(0) scale(0.98);
}

.btn-glow {
  position: absolute;
  inset: -50%;
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.3),
    transparent
  );
  transform: translateX(-100%);
  animation: shimmer 3s infinite;
}

@keyframes shimmer {
  100% {
    transform: translateX(100%);
  }
}

/* --- Number Grid Layout --- */
.number-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-xs);
}

.number-grid .btn-zero {
  grid-column: span 2;
}

/* --- History Section --- */
.card-footer {
  border-top: 1px solid var(--color-glass-border);
}

.history {
  padding: var(--space-md) var(--space-lg);
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-md);
}

.history-header h3 {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--color-text-secondary);
}

.history-actions {
  display: flex;
  gap: var(--space-xs);
}

.btn-ghost {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  background: transparent;
  border: 1px solid var(--color-glass-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-ghost:hover {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-text-muted);
}

.history-list {
  list-style: none;
  max-height: 150px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--color-glass-border) transparent;
}

.history-list::-webkit-scrollbar {
  width: 4px;
}

.history-list::-webkit-scrollbar-thumb {
  background: var(--color-glass-border);
  border-radius: var(--radius-full);
}

.history-list li {
  padding: var(--space-sm) var(--space-md);
  margin-bottom: var(--space-xs);
  background: var(--color-surface);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.history-list li:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
  transform: translateX(4px);
}

.history-empty {
  text-align: center;
  color: var(--color-text-muted);
  font-family: var(--font-sans);
  font-style: italic;
  cursor: default !important;
}

.history-empty:hover {
  transform: none !important;
}

/* --- Responsive Design --- */
@media (max-width: 700px) {
  .pad {
    grid-template-columns: 1fr;
  }

  .group-right {
    order: -1;
  }

  .row-sci {
    flex-wrap: wrap;
  }

  .row-sci .btn {
    flex: 1 1 calc(20% - var(--space-xs));
    min-width: 50px;
  }

  .bg-blur-1,
  .bg-blur-2 {
    width: 300px;
    height: 300px;
  }

  .bg-blur-3 {
    width: 200px;
    height: 200px;
  }
}

@media (max-width: 480px) {
  .app {
    padding: var(--space-sm);
  }

  .card {
    border-radius: var(--radius-lg);
  }

  .card-header {
    padding: var(--space-sm) var(--space-md);
  }

  .brand-text h1 {
    font-size: 1.2rem;
  }

  .display {
    padding: var(--space-md);
  }

  .result-value {
    font-size: 2rem;
  }

  .btn {
    min-width: 40px;
    height: 44px;
  }

  .btn-num {
    font-size: 1.1rem;
  }

  .btn-equals {
    height: 50px;
    font-size: 1.5rem;
  }

  .history {
    padding: var(--space-sm) var(--space-md);
  }
}

/* --- Accessibility --- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .bg-blur {
    animation: none;
  }

  .btn-glow {
    animation: none;
  }
}

/* Focus Visible */
.btn:focus-visible,
.icon-btn:focus-visible,
.btn-ghost:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* High Contrast Mode */
@media (prefers-contrast: high) {
  .btn {
    border: 2px solid currentColor;
  }

  .card.glass {
    border-width: 2px;
  }
}

/* --- Print Styles --- */
@media print {
  .bg-gradient,
  .bg-blur {
    display: none;
  }

  .card.glass {
    box-shadow: none;
    border: 1px solid #000;
  }
}
