* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

:root {
  --bg-deep: #0a0a0c;
  --bg-calc: #141418;
  --bg-display: #0d0d10;
  --btn-num: #2a2a32;
  --btn-num-hover: #35353f;
  --btn-func: #3d3d47;
  --btn-func-hover: #4a4a55;
  --btn-op: #e8752a;
  --btn-op-hover: #f08535;
  --btn-op-active: #c46020;
  --text-primary: #f0f0f2;
  --text-secondary: #8a8a96;
  --text-muted: #5a5a66;
  --accent: #e8752a;
  --accent-glow: rgba(232, 117, 42, 0.15);
}

[data-theme="light"] {
  --bg-deep: #e8e8ee;
  --bg-calc: #f2f2f6;
  --bg-display: #e0e0e8;
  --btn-num: #ffffff;
  --btn-num-hover: #f0f0f4;
  --btn-func: #d8d8e0;
  --btn-func-hover: #ccccd4;
  --btn-op: #e8752a;
  --btn-op-hover: #f08535;
  --btn-op-active: #c46020;
  --text-primary: #1a1a20;
  --text-secondary: #6a6a76;
  --text-muted: #9a9aa6;
  --accent: #e8752a;
  --accent-glow: rgba(232, 117, 42, 0.1);
}

body {
  background: var(--bg-deep);
  color: var(--text-primary);
  font-family: 'Outfit', sans-serif;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: background 0.35s ease, color 0.35s ease;
  overflow-x: hidden;
}

.calc-wrapper {
  width: 100%;
  max-width: 420px;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
}

.calc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 8px;
}

.calc-brand {
  display: flex;
  flex-direction: column;
}

.calc-brand-name {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text-primary);
  transition: color 0.35s;
}

.calc-brand-tagline {
  font-size: 11px;
  font-weight: 400;
  color: var(--text-muted);
  letter-spacing: 0.3px;
  transition: color 0.35s;
}

.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: none;
  background: var(--btn-num);
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

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

.icon-btn:active {
  transform: scale(0.92);
}

/* Display */
.display-container {
  background: var(--bg-display);
  margin: 8px 16px 12px;
  border-radius: 20px;
  padding: 24px 24px 28px;
  min-height: 140px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.3);
  transition: background 0.35s, box-shadow 0.35s;
  overflow: hidden;
}

.display-expression {
  font-family: 'DM Mono', monospace;
  font-size: 16px;
  font-weight: 400;
  color: var(--text-muted);
  text-align: right;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  min-height: 24px;
  transition: color 0.35s;
  direction: rtl;
}

.display-expression::-webkit-scrollbar { display: none; }

.display-value {
  font-family: 'DM Mono', monospace;
  font-weight: 500;
  color: var(--text-primary);
  text-align: right;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  margin-top: 8px;
  line-height: 1.1;
  transition: color 0.35s, font-size 0.15s;
}

.display-value::-webkit-scrollbar { display: none; }

/* Scientific row */
.sci-row {
  display: flex;
  gap: 6px;
  padding: 0 16px;
  margin-bottom: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.sci-row::-webkit-scrollbar { display: none; }

.sci-btn {
  flex-shrink: 0;
  height: 38px;
  min-width: 48px;
  padding: 0 10px;
  border: none;
  border-radius: 10px;
  background: var(--btn-func);
  color: var(--text-secondary);
  font-family: 'DM Mono', monospace;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s ease;
}

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

.sci-btn:active {
  transform: scale(0.93);
}

/* Button grid */
.btn-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding: 0 16px 12px;
  flex: 1;
}

.calc-btn {
  border: none;
  border-radius: 16px;
  font-family: 'Outfit', sans-serif;
  font-size: 24px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  transition: all 0.12s ease;
  position: relative;
  overflow: hidden;
}

.calc-btn::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 16px;
  opacity: 0;
  transition: opacity 0.15s;
  background: radial-gradient(circle at center, rgba(255,255,255,0.12), transparent 70%);
}

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

.calc-btn:active {
  transform: scale(0.92);
}

.btn-number {
  background: var(--btn-num);
  color: var(--text-primary);
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
}

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

.btn-func {
  background: var(--btn-func);
  color: var(--text-primary);
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
}

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

.btn-operator {
  background: var(--btn-op);
  color: #fff;
  box-shadow: 0 2px 8px rgba(232, 117, 42, 0.3);
}

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

.btn-operator:active {
  background: var(--btn-op-active);
}

.btn-operator.active-op {
  box-shadow: 0 0 20px rgba(232, 117, 42, 0.5), 0 2px 8px rgba(232, 117, 42, 0.3);
  background: var(--btn-op-hover);
}

.btn-zero {
  grid-column: span 2;
  aspect-ratio: auto;
  height: 100%;
  border-radius: 16px;
  justify-content: flex-start;
  padding-left: 32px;
}

.btn-equals {
  background: var(--btn-op);
  color: #fff;
  box-shadow: 0 2px 12px rgba(232, 117, 42, 0.35);
  font-size: 28px;
  font-weight: 600;
}

.btn-equals:hover {
  background: var(--btn-op-hover);
  box-shadow: 0 4px 16px rgba(232, 117, 42, 0.45);
}

.btn-equals:active {
  background: var(--btn-op-active);
}

/* History panel */
.history-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

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

.history-panel {
  position: fixed;
  top: 0;
  left: -320px;
  width: 300px;
  max-width: 85vw;
  height: 100dvh;
  background: var(--bg-calc);
  z-index: 101;
  transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.35s;
  display: flex;
  flex-direction: column;
  box-shadow: 4px 0 24px rgba(0,0,0,0.3);
}

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

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
  border-bottom: 1px solid rgba(128,128,128,0.15);
}

.history-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  scrollbar-width: thin;
}

.history-item {
  padding: 12px 14px;
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 4px;
}

.history-item:hover {
  background: var(--btn-num);
}

.history-item:active {
  background: var(--btn-num-hover);
}

.history-expr {
  font-family: 'DM Mono', monospace;
  font-size: 13px;
  color: var(--text-muted);
}

.history-result {
  font-family: 'DM Mono', monospace;
  font-size: 20px;
  font-weight: 500;
  color: var(--accent);
  margin-top: 2px;
}

.history-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  margin-top: 40px;
}

.clear-history-btn {
  margin: 12px;
  padding: 12px;
  border: none;
  border-radius: 12px;
  background: rgba(220, 60, 60, 0.12);
  color: #e05050;
  font-family: 'Outfit', sans-serif;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.clear-history-btn:hover {
  background: rgba(220, 60, 60, 0.2);
}

/* Footer */
.calc-footer {
  text-align: center;
  padding: 10px 0 18px;
  font-size: 12px;
}

.calc-footer a {
  color: var(--text-muted);
  text-decoration: none;
  transition: color 0.2s;
}

.calc-footer a:hover {
  color: var(--accent);
}

/* Animations */
@keyframes resultPop {
  0% { transform: scale(1); }
  50% { transform: scale(1.04); }
  100% { transform: scale(1); }
}

.result-pop {
  animation: resultPop 0.2s ease-out;
}

@keyframes errorShake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.error-shake {
  animation: errorShake 0.4s ease-out;
}

/* Scrollbar styling for history */
.history-list::-webkit-scrollbar {
  width: 4px;
}

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

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

@media (max-width: 420px) {
  .btn-grid {
    gap: 8px;
    padding: 0 12px 10px;
  }
  .display-container {
    margin: 6px 12px 10px;
    padding: 20px 18px 24px;
    min-height: 120px;
  }
  .sci-row {
    padding: 0 12px;
    gap: 5px;
  }
  .calc-btn {
    font-size: 22px;
    border-radius: 14px;
  }
}

@media (min-height: 800px) {
  .display-container {
    min-height: 160px;
  }
}