/* ============================
   CSS VARIABLES — DARK THEME
   ============================ */
:root,
[data-theme="dark"] {
  --bg: #0d0d0f;
  --bg-grid: rgba(255, 255, 255, 0.03);
  --card-bg: #18181c;
  --card-border: rgba(255, 255, 255, 0.07);
  --display-bg: #111115;
  --display-border: rgba(255, 255, 255, 0.05);

  --text-primary: #f0f0f2;
  --text-secondary: #6b6b7a;
  --text-expression: #888899;

  --btn-num-bg: #222228;
  --btn-num-hover: #2e2e36;
  --btn-num-active: #3a3a44;
  --btn-num-text: #e8e8ee;

  --btn-fn-bg: #2a2a32;
  --btn-fn-hover: #36363f;
  --btn-fn-text: #aaaacc;

  --btn-op-bg: #2a1f3d;
  --btn-op-hover: #3a2a52;
  --btn-op-active: #7c5cbf;
  --btn-op-text: #b89de0;
  --btn-op-active-text: #fff;

  --btn-eq-bg: linear-gradient(135deg, #7c5cbf, #a070e0);
  --btn-eq-hover: linear-gradient(135deg, #8f6ed4, #b482f5);
  --btn-eq-text: #fff;
  --btn-eq-shadow: 0 8px 30px rgba(140, 90, 220, 0.4);

  --accent: #9b72e8;
  --accent-glow: rgba(155, 114, 232, 0.25);

  --shadow-card: 0 40px 80px rgba(0, 0, 0, 0.6);
  --shadow-btn: 0 4px 12px rgba(0, 0, 0, 0.4);

  --toggle-bg: #2a2a36;
  --toggle-thumb: #7c5cbf;
  --brand-color: #9b72e8;
}

/* ============================
   CSS VARIABLES — LIGHT THEME
   ============================ */
[data-theme="light"] {
  --bg: #f0eff5;
  --bg-grid: rgba(0, 0, 0, 0.04);
  --card-bg: #ffffff;
  --card-border: rgba(0, 0, 0, 0.06);
  --display-bg: #f8f7fc;
  --display-border: rgba(0, 0, 0, 0.05);

  --text-primary: #1a1a2e;
  --text-secondary: #9090aa;
  --text-expression: #aaaabc;

  --btn-num-bg: #f2f1f8;
  --btn-num-hover: #e8e6f2;
  --btn-num-active: #dcdaec;
  --btn-num-text: #2a2a3e;

  --btn-fn-bg: #e8e6f5;
  --btn-fn-hover: #dcdaee;
  --btn-fn-text: #6656a8;

  --btn-op-bg: #ede8f8;
  --btn-op-hover: #e2daf4;
  --btn-op-active: #7c5cbf;
  --btn-op-text: #7c5cbf;
  --btn-op-active-text: #fff;

  --btn-eq-bg: linear-gradient(135deg, #7c5cbf, #a070e0);
  --btn-eq-hover: linear-gradient(135deg, #8f6ed4, #b482f5);
  --btn-eq-text: #fff;
  --btn-eq-shadow: 0 8px 30px rgba(124, 92, 191, 0.35);

  --accent: #7c5cbf;
  --accent-glow: rgba(124, 92, 191, 0.15);

  --shadow-card: 0 30px 70px rgba(100, 80, 160, 0.15);
  --shadow-btn: 0 2px 8px rgba(0, 0, 0, 0.08);

  --toggle-bg: #e0ddf0;
  --toggle-thumb: #7c5cbf;
  --brand-color: #7c5cbf;
}

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

html {
  font-size: 16px;
}

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.35s ease, color 0.35s ease;
  overflow-x: hidden;
}

/* ============================
   BACKGROUND GRID
   ============================ */
.bg-grid {
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(var(--bg-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--bg-grid) 1px, transparent 1px);
  background-size: 40px 40px;
  pointer-events: none;
  z-index: 0;
  transition: background-image 0.35s ease;
}

/* ============================
   APP WRAPPER
   ============================ */
.app-wrapper {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 20px;
  width: 100%;
  max-width: 420px;
}

/* ============================
   TOP BAR
   ============================ */
.top-bar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px;
}

.brand {
  font-family: 'Space Mono', monospace;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.22em;
  color: var(--brand-color);
  opacity: 0.8;
}

/* ============================
   THEME TOGGLE
   ============================ */
.theme-toggle {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
}

.toggle-track {
  position: relative;
  width: 56px;
  height: 28px;
  background: var(--toggle-bg);
  border-radius: 14px;
  display: flex;
  align-items: center;
  padding: 0 5px;
  transition: background 0.3s ease;
  border: 1px solid var(--card-border);
}

.toggle-thumb {
  position: absolute;
  left: 4px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--toggle-thumb);
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

[data-theme="light"] .toggle-thumb {
  transform: translateX(28px);
}

.icon-sun, .icon-moon {
  position: absolute;
  width: 13px;
  height: 13px;
  color: var(--text-secondary);
  transition: opacity 0.3s ease;
}

.icon-sun { right: 7px; }
.icon-moon { left: 7px; }

[data-theme="dark"] .icon-sun { opacity: 0.5; }
[data-theme="dark"] .icon-moon { opacity: 0; }
[data-theme="light"] .icon-sun { opacity: 0; }
[data-theme="light"] .icon-moon { opacity: 0.5; }

/* ============================
   CALCULATOR CARD
   ============================ */
.calculator {
  width: 100%;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 28px;
  box-shadow: var(--shadow-card);
  overflow: hidden;
  transition: background 0.35s ease, box-shadow 0.35s ease;
  animation: slideUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(24px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ============================
   DISPLAY
   ============================ */
.display {
  background: var(--display-bg);
  border-bottom: 1px solid var(--display-border);
  padding: 28px 28px 20px;
  min-height: 140px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  gap: 4px;
  transition: background 0.35s ease;
  position: relative;
}

.expression {
  font-family: 'Space Mono', monospace;
  font-size: 0.95rem;
  color: var(--text-expression);
  min-height: 1.4em;
  text-align: right;
  word-break: break-all;
  transition: color 0.3s ease;
  line-height: 1.5;
}

.result {
  font-family: 'Space Mono', monospace;
  font-weight: 700;
  font-size: 3rem;
  color: var(--text-primary);
  text-align: right;
  line-height: 1.1;
  transition: color 0.3s ease, transform 0.15s ease;
  word-break: break-all;
  cursor: default;
}

.result.animate-pop {
  animation: pop 0.18s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.04); }
  100% { transform: scale(1); }
}

.result.error {
  color: #e05555;
  font-size: 1.6rem;
}

.keyboard-hint {
  font-size: 0.65rem;
  color: var(--text-secondary);
  font-family: 'DM Sans', sans-serif;
  letter-spacing: 0.05em;
  opacity: 0.5;
  margin-top: 6px;
  align-self: flex-start;
}

/* ============================
   KEYPAD
   ============================ */
.keypad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  padding: 20px;
}

/* ============================
   BUTTONS
   ============================ */
.btn {
  font-family: 'DM Sans', sans-serif;
  font-size: 1.2rem;
  font-weight: 500;
  border: none;
  border-radius: 16px;
  cursor: pointer;
  height: 68px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  transition:
    background 0.2s ease,
    transform 0.12s ease,
    box-shadow 0.2s ease,
    color 0.2s ease;
  box-shadow: var(--shadow-btn);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* Ripple effect */
.btn::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at center, rgba(255,255,255,0.15) 0%, transparent 70%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

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

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

/* Number buttons */
.btn-num {
  background: var(--btn-num-bg);
  color: var(--btn-num-text);
}

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

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

/* Zero button spans 2 columns */
.btn-zero {
  grid-column: span 2;
  justify-content: flex-start;
  padding-left: 28px;
  border-radius: 34px;
}

/* Function buttons (AC, +/-, %) */
.btn-fn {
  background: var(--btn-fn-bg);
  color: var(--btn-fn-text);
  font-weight: 600;
}

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

/* Operator buttons */
.btn-op {
  background: var(--btn-op-bg);
  color: var(--btn-op-text);
  font-size: 1.35rem;
  font-weight: 700;
}

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

.btn-op.active {
  background: var(--btn-op-active);
  color: var(--btn-op-active-text);
  box-shadow: 0 4px 20px var(--accent-glow);
}

/* Equals button */
.btn-eq {
  background: var(--btn-eq-bg);
  color: var(--btn-eq-text);
  font-size: 1.4rem;
  font-weight: 700;
  box-shadow: var(--btn-eq-shadow);
}

.btn-eq:hover {
  background: var(--btn-eq-hover);
  box-shadow: 0 12px 36px rgba(140, 90, 220, 0.5);
  transform: translateY(-1px);
}

.btn-eq:active {
  transform: scale(0.93) translateY(0);
}

/* Key press highlight */
.btn.key-press {
  transform: scale(0.93);
  background: var(--btn-num-active);
}

.btn-op.key-press {
  background: var(--btn-op-active);
  color: #fff;
}

.btn-eq.key-press {
  background: var(--btn-eq-hover);
  transform: scale(0.93);
}

/* ============================
   RESPONSIVE
   ============================ */
@media (max-width: 400px) {
  .app-wrapper {
    padding: 12px;
    gap: 14px;
  }

  .result {
    font-size: 2.4rem;
  }

  .btn {
    height: 60px;
    font-size: 1.1rem;
    border-radius: 14px;
  }

  .btn-zero {
    border-radius: 30px;
  }

  .keypad {
    gap: 10px;
    padding: 16px;
  }

  .display {
    padding: 20px 20px 16px;
    min-height: 120px;
  }
}

@media (max-width: 320px) {
  .btn {
    height: 54px;
    font-size: 1rem;
    border-radius: 12px;
  }

  .result {
    font-size: 2rem;
  }

  .keypad {
    gap: 8px;
    padding: 12px;
  }
}

@media (min-width: 600px) {
  .app-wrapper {
    max-width: 400px;
  }

  .btn {
    height: 72px;
    font-size: 1.25rem;
  }
}

/* ============================
   FOCUS STYLES (accessibility)
   ============================ */
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.theme-toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: 16px;
}
