/* ===== CSS Custom Properties ===== */
:root {
  --bg-primary: #0a0a1a;
  --bg-secondary: #12122a;
  --glass-bg: rgba(255, 255, 255, 0.04);
  --glass-border: rgba(255, 255, 255, 0.08);
  --glass-shadow: rgba(0, 0, 0, 0.5);
  --text-primary: #ffffff;
  --text-secondary: rgba(255, 255, 255, 0.6);
  --text-tertiary: rgba(255, 255, 255, 0.3);
  --accent: #ff6b35;
  --accent-glow: rgba(255, 107, 53, 0.35);
  --accent-secondary: #7c3aed;
  --number-bg: rgba(255, 255, 255, 0.06);
  --number-hover: rgba(255, 255, 255, 0.10);
  --operator-bg: rgba(255, 107, 53, 0.15);
  --operator-hover: rgba(255, 107, 53, 0.25);
  --special-bg: rgba(255, 255, 255, 0.08);
  --special-hover: rgba(255, 255, 255, 0.14);
  --sci-bg: rgba(124, 58, 237, 0.12);
  --sci-hover: rgba(124, 58, 237, 0.22);
  --equals-bg: linear-gradient(135deg, #ff6b35, #ff8c42);
  --equals-shadow: rgba(255, 107, 53, 0.4);
  --radius-sm: 12px;
  --radius-md: 18px;
  --radius-lg: 24px;
  --radius-xl: 28px;
  --transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

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

html, body {
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at 20% 50%, rgba(124, 58, 237, 0.12) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 20%, rgba(255, 107, 53, 0.08) 0%, transparent 50%),
    radial-gradient(ellipse at 50% 80%, rgba(59, 130, 246, 0.06) 0%, transparent 50%),
    linear-gradient(135deg, #0a0a1a 0%, #0f0f2a 30%, #0d1b2a 70%, #0a0a1a 100%);
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.08); border-radius: 4px; }

/* ===== App Container ===== */
.app-container {
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

/* ===== Calculator Card ===== */
.calculator {
  width: 100%;
  max-width: 420px;
  background: var(--glass-bg);
  backdrop-filter: blur(32px) saturate(1.2);
  -webkit-backdrop-filter: blur(32px) saturate(1.2);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-xl);
  padding: 24px;
  box-shadow:
    0 8px 32px var(--glass-shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.06);
  position: relative;
  overflow: hidden;
  transition: var(--transition);
}

.calculator::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle at 30% 20%, rgba(255, 255, 255, 0.02) 0%, transparent 50%);
  pointer-events: none;
}

/* ===== Header ===== */
.calc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  position: relative;
  z-index: 1;
}

.calc-brand {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, var(--accent), #ffb347);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ===== Mode Toggle ===== */
.mode-toggle {
  display: flex;
  background: rgba(255, 255, 255, 0.05);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 2px;
}

.mode-btn {
  background: transparent;
  border: none;
  color: var(--text-tertiary);
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 6px 14px;
  border-radius: 10px;
  cursor: pointer;
  transition: var(--transition);
  white-space: nowrap;
}

.mode-btn.active {
  background: rgba(255, 255, 255, 0.10);
  color: var(--text-primary);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.history-toggle {
  background: transparent;
  border: none;
  color: var(--text-tertiary);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
}

.history-toggle:hover {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-secondary);
}

/* ===== Display ===== */
.calc-display {
  text-align: right;
  padding: 8px 4px 16px;
  margin-bottom: 8px;
  position: relative;
  z-index: 1;
  min-height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.display-expression {
  font-size: 16px;
  font-weight: 400;
  color: var(--text-secondary);
  min-height: 24px;
  word-break: break-all;
  line-height: 1.5;
  transition: var(--transition);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.display-result {
  font-size: 44px;
  font-weight: 700;
  letter-spacing: -1px;
  color: var(--text-primary);
  min-height: 52px;
  line-height: 1.1;
  word-break: break-all;
  transition: var(--transition);
}

.display-result.error {
  color: #ef4444;
  font-size: 32px;
}

/* Copy button */
.copy-btn {
  position: absolute;
  top: 12px;
  right: 4px;
  background: rgba(255, 255, 255, 0.05);
  border: none;
  color: var(--text-tertiary);
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 11px;
  font-family: inherit;
  transition: var(--transition);
  opacity: 0;
}

.calc-display:hover .copy-btn {
  opacity: 1;
}

.copy-btn:hover {
  background: rgba(255, 255, 255, 0.10);
  color: var(--text-secondary);
}

/* ===== Scientific Buttons ===== */
.sci-buttons {
  display: none;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  position: relative;
  z-index: 1;
}

.sci-buttons.active {
  display: grid;
  animation: fadeSlideIn 0.3s ease;
}

.btn-sci {
  background: var(--sci-bg);
  border: 1px solid rgba(124, 58, 237, 0.10);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  padding: 12px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: var(--transition);
  font-family: inherit;
}

.btn-sci:hover {
  background: var(--sci-hover);
  color: var(--text-primary);
}

/* ===== Button Grid ===== */
.button-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  position: relative;
  z-index: 1;
}

/* ===== Button Base ===== */
.btn {
  position: relative;
  border: none;
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 22px;
  font-weight: 500;
  cursor: pointer;
  padding: 18px 8px;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.2s ease;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.btn::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at center, rgba(255,255,255,0.10) 0%, transparent 60%);
  opacity: 0;
  transition: opacity 0.2s ease;
}

.btn:active::after {
  opacity: 1;
}

/* Press animation */
.btn:active {
  transform: scale(0.93);
}

/* ===== Number Buttons ===== */
.btn-number {
  background: var(--number-bg);
  border: 1px solid rgba(255, 255, 255, 0.04);
  color: var(--text-primary);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.btn-number:hover {
  background: var(--number-hover);
}

.zero {
  grid-column: span 2;
}

/* ===== Operator Buttons ===== */
.btn-operator {
  background: var(--operator-bg);
  border: 1px solid rgba(255, 107, 53, 0.10);
  color: var(--accent);
  box-shadow: 0 2px 8px rgba(255, 107, 53, 0.08);
}

.btn-operator:hover {
  background: var(--operator-hover);
  box-shadow: 0 2px 12px rgba(255, 107, 53, 0.15);
}

.btn-operator:active {
  box-shadow: 0 0 20px rgba(255, 107, 53, 0.25);
}

/* ===== Special Buttons ===== */
.btn-special {
  background: var(--special-bg);
  border: 1px solid rgba(255, 255, 255, 0.04);
  color: var(--text-secondary);
  font-size: 18px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.btn-special:hover {
  background: var(--special-hover);
  color: var(--text-primary);
}

/* ===== Equals Button ===== */
.btn-equals {
  background: var(--equals-bg);
  border: none;
  color: #ffffff;
  font-size: 28px;
  font-weight: 700;
  box-shadow:
    0 4px 16px var(--equals-shadow),
    0 2px 8px rgba(0, 0, 0, 0.2);
}

.btn-equals:hover {
  box-shadow:
    0 6px 24px var(--equals-shadow),
    0 2px 8px rgba(0, 0, 0, 0.2);
}

.btn-equals:active {
  box-shadow:
    0 0 32px var(--equals-shadow),
    0 2px 8px rgba(0, 0, 0, 0.2);
  background: linear-gradient(135deg, #ff7a45, #ff9c55);
}

/* ===== History Panel ===== */
.history-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 280px;
  height: 100%;
  background: rgba(10, 10, 30, 0.95);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-left: 1px solid var(--glass-border);
  padding: 20px;
  transform: translateX(100%);
  opacity: 0;
  transition: var(--transition);
  z-index: 10;
  border-radius: 0 var(--radius-xl) var(--radius-xl) 0;
  overflow-y: auto;
}

.history-panel.open {
  transform: translateX(0);
  opacity: 1;
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.3);
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-secondary);
}

.history-clear {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: var(--text-tertiary);
  font-family: inherit;
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: var(--transition);
}

.history-clear:hover {
  background: rgba(239, 68, 68, 0.15);
  border-color: rgba(239, 68, 68, 0.2);
  color: #ef4444;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.03);
  cursor: pointer;
  transition: var(--transition);
  border: 1px solid transparent;
}

.history-item:hover {
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.06);
}

.history-item-expr {
  font-size: 13px;
  color: var(--text-tertiary);
  margin-bottom: 2px;
}

.history-item-result {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-primary);
}

.history-empty {
  text-align: center;
  color: var(--text-tertiary);
  font-size: 14px;
  padding: 32px 0;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: rgba(255, 255, 255, 0.10);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.10);
  padding: 10px 24px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  opacity: 0;
  transition: all 0.35s ease;
  z-index: 100;
  pointer-events: none;
}

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

/* ===== Animations ===== */
@keyframes fadeSlideIn {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

.result-animate {
  animation: resultSlide 0.25s ease;
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .calculator {
    max-width: 100%;
    border-radius: var(--radius-lg);
    padding: 16px;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    border-radius: 0;
    border: none;
  }

  .app-container {
    padding: 0;
    align-items: stretch;
  }

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

  .btn {
    padding: 16px 8px;
    border-radius: 14px;
    font-size: 20px;
  }

  .btn-sci {
    padding: 10px 6px;
    font-size: 12px;
  }

  .button-grid {
    gap: 8px;
  }

  .sci-buttons {
    gap: 6px;
  }

  .calc-display {
    min-height: 80px;
    padding: 4px 4px 12px;
  }

  .history-panel {
    width: 100%;
    border-radius: 0;
    border-left: none;
  }

  .calc-header {
    margin-bottom: 12px;
  }
}

@media (min-width: 481px) and (max-width: 768px) {
  .calculator {
    max-width: 400px;
    padding: 20px;
  }
}

@media (min-width: 769px) {
  .btn:hover {
    transform: translateY(-1px);
  }
}
