/* ============================================================
   CSS VARIABLES — Tema escuro (padrão) e claro
   ============================================================ */

:root {
  --bg-body: linear-gradient(135deg, #0f0f1a 0%, #1a1a2e 40%, #16213e 70%, #0f3460 100%);
  --bg-calculator: #1a1a2e;
  --bg-display: #12121e;
  --bg-button: #22223a;
  --bg-button-hover: #2e2e4e;
  --bg-button-active: #3a3a56;
  --text-primary: #ffffff;
  --text-secondary: #6b6b8a;
  --text-muted: #555577;
  --text-memory: #a78bfa;
  --text-copy: #fbbf24;
  --text-trig: #6ee7b7;
  --text-paren: #93c5fd;
  --text-operator: #b794f4;
  --text-clear: #fca5a5;
  --text-equals: #ffffff;
  --bg-operator: #2a1e4e;
  --bg-operator-hover: #3a2e6e;
  --bg-equals: linear-gradient(135deg, #7c3aed, #9333ea);
  --bg-equals-hover: linear-gradient(135deg, #8b5cf6, #a855f7);
  --bg-clear: #3a2020;
  --bg-clear-hover: #4e2a2a;
  --bg-paren: #1e2a3a;
  --bg-paren-hover: #2a3a4e;
  --bg-trig: #1a2a2a;
  --bg-trig-hover: #243a3a;
  --bg-memory: #1a1a2e;
  --bg-memory-hover: #22223e;
  --bg-memory-border: rgba(167, 139, 250, 0.15);
  --bg-history: #1a1a2e;
  --bg-history-item: #12121e;
  --bg-history-item-hover: #1e1e32;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
  --shadow-border: 0 0 0 1px rgba(255, 255, 255, 0.06);
  --toast-bg: #7c3aed;
  --toast-shadow: rgba(124, 58, 237, 0.3);
  --scrollbar-thumb: #333355;
}

/* Tema claro */
[data-theme="light"] {
  --bg-body: linear-gradient(135deg, #e8e4f0 0%, #ddd8ee 40%, #c8c8e0 70%, #b8c0d8 100%);
  --bg-calculator: #f0eef5;
  --bg-display: #ffffff;
  --bg-button: #e4e2ec;
  --bg-button-hover: #d8d6e4;
  --bg-button-active: #cccade;
  --text-primary: #1a1a2e;
  --text-secondary: #666688;
  --text-muted: #9999bb;
  --text-memory: #7c3aed;
  --text-copy: #b45309;
  --text-trig: #047857;
  --text-paren: #1d4ed8;
  --text-operator: #7c3aed;
  --text-clear: #dc2626;
  --text-equals: #ffffff;
  --bg-operator: #ede4f8;
  --bg-operator-hover: #ddd4f0;
  --bg-equals: linear-gradient(135deg, #7c3aed, #9333ea);
  --bg-equals-hover: linear-gradient(135deg, #8b5cf6, #a855f7);
  --bg-clear: #fce4e4;
  --bg-clear-hover: #f5d0d0;
  --bg-paren: #e0e8f4;
  --bg-paren-hover: #d0dcee;
  --bg-trig: #e0f0ec;
  --bg-trig-hover: #d0e4de;
  --bg-memory: #f0eef5;
  --bg-memory-hover: #e8e4f0;
  --bg-memory-border: rgba(124, 58, 237, 0.15);
  --bg-history: #f0eef5;
  --bg-history-item: #ffffff;
  --bg-history-item-hover: #f4f2f8;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
  --shadow-border: 0 0 0 1px rgba(0, 0, 0, 0.06);
  --toast-bg: #7c3aed;
  --toast-shadow: rgba(124, 58, 237, 0.3);
  --scrollbar-thumb: #c8c0d8;
}

/* ============================================================
   GERAIS
   ============================================================ */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100vh;
  padding: 40px 20px;
  background: var(--bg-body);
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
  transition: background 0.3s ease;
}

.wrapper {
  display: flex;
  gap: 20px;
  align-items: flex-start;
  flex-wrap: wrap;
  justify-content: center;
}

/* ============================================================
   CALCULATOR
   ============================================================ */

.calculator {
  background: var(--bg-calculator);
  border-radius: 20px;
  padding: 20px;
  width: 100%;
  max-width: 340px;
  box-shadow: var(--shadow), var(--shadow-border);
  transition: background 0.3s ease, box-shadow 0.3s ease;
}

/* ----- Display ----- */

.display {
  background: var(--bg-display);
  border-radius: 14px;
  padding: 12px 20px 16px;
  margin-bottom: 16px;
  min-height: 95px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  position: relative;
  overflow: hidden;
  transition: background 0.3s ease;
}

.display-top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  margin-bottom: 4px;
}

.theme-toggle {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 2px 6px;
  border-radius: 6px;
  transition: all 0.2s;
  line-height: 1;
  order: 0;
}

.theme-toggle:hover {
  background: var(--bg-button);
  transform: scale(1.1);
}

.memory-indicator {
  font-size: 0.7rem;
  color: var(--text-memory);
  font-weight: 700;
  opacity: 0;
  transition: opacity 0.2s;
  order: 1;
}

.memory-indicator.active {
  opacity: 1;
}

.expression {
  font-size: 0.9rem;
  color: var(--text-secondary);
  min-height: 22px;
  word-break: break-all;
  text-align: right;
  width: 100%;
  line-height: 1.4;
  transition: color 0.3s ease;
}

.current-operand {
  font-size: 2.3rem;
  color: var(--text-primary);
  font-weight: 600;
  word-break: break-all;
  text-align: right;
  line-height: 1.2;
  max-width: 100%;
  transition: font-size 0.1s ease, color 0.3s ease;
}

/* ----- Buttons Grid ----- */

.buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

button {
  border: none;
  outline: none;
  cursor: pointer;
  font-size: 1.2rem;
  font-weight: 500;
  padding: 14px 0;
  border-radius: 12px;
  background: var(--bg-button);
  color: var(--text-primary);
  transition: all 0.12s ease, background 0.3s ease, color 0.3s ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  font-family: inherit;
}

button:hover {
  background: var(--bg-button-hover);
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0px);
  filter: brightness(0.85);
}

/* Numeric buttons */
button[data-action="number"] {
  background: var(--bg-button);
  color: var(--text-primary);
}
button[data-action="number"]:hover {
  background: var(--bg-button-hover);
}

button[data-action="decimal"] {
  background: var(--bg-button);
  color: var(--text-primary);
}
button[data-action="decimal"]:hover {
  background: var(--bg-button-hover);
}

/* Operators */
button[data-action="add"],
button[data-action="subtract"],
button[data-action="multiply"],
button[data-action="divide"],
button[data-action="power"],
button[data-action="percent"] {
  background: var(--bg-operator);
  color: var(--text-operator);
}
button[data-action="add"]:hover,
button[data-action="subtract"]:hover,
button[data-action="multiply"]:hover,
button[data-action="divide"]:hover,
button[data-action="power"]:hover,
button[data-action="percent"]:hover {
  background: var(--bg-operator-hover);
}

/* Trig functions */
button[data-action="sin"],
button[data-action="cos"],
button[data-action="tan"] {
  background: var(--bg-trig);
  color: var(--text-trig);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 10px 0;
}
button[data-action="sin"]:hover,
button[data-action="cos"]:hover,
button[data-action="tan"]:hover {
  background: var(--bg-trig-hover);
}

/* Toggle sign */
button[data-action="toggle-sign"] {
  background: var(--bg-button);
  color: var(--text-primary);
  font-size: 1.2rem;
  font-weight: 600;
}
button[data-action="toggle-sign"]:hover {
  background: var(--bg-button-hover);
}

/* Equals */
.btn-equals {
  background: var(--bg-equals);
  color: var(--text-equals);
  font-size: 1.4rem;
}
.btn-equals:hover {
  background: var(--bg-equals-hover);
}

/* Clear / Delete */
button[data-action="clear"] {
  background: var(--bg-clear);
  color: var(--text-clear);
}
button[data-action="clear"]:hover {
  background: var(--bg-clear-hover);
}

button[data-action="delete"] {
  background: var(--bg-button);
  color: var(--text-clear);
}
button[data-action="delete"]:hover {
  background: var(--bg-button-hover);
}

/* Parentheses */
button[data-action="paren"] {
  background: var(--bg-paren);
  color: var(--text-paren);
}
button[data-action="paren"]:hover {
  background: var(--bg-paren-hover);
}

/* Square root */
button[data-action="sqrt"] {
  background: var(--bg-trig);
  color: var(--text-trig);
}
button[data-action="sqrt"]:hover {
  background: var(--bg-trig-hover);
}

/* Copy button */
.btn-copy {
  background: var(--bg-button);
  color: var(--text-copy);
  font-size: 1.1rem;
}
.btn-copy:hover {
  background: var(--bg-button-hover);
}

/* Memory buttons */
button[data-action="memory-clear"],
button[data-action="memory-recall"],
button[data-action="memory-add"],
button[data-action="memory-subtract"] {
  background: var(--bg-memory);
  color: var(--text-memory);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 10px 0;
  border: 1px solid var(--bg-memory-border);
}
button[data-action="memory-clear"]:hover,
button[data-action="memory-recall"]:hover,
button[data-action="memory-add"]:hover,
button[data-action="memory-subtract"]:hover {
  background: var(--bg-memory-hover);
  border-color: rgba(167, 139, 250, 0.3);
}

/* ============================================================
   HISTORY PANEL
   ============================================================ */

.history-panel {
  background: var(--bg-history);
  border-radius: 20px;
  padding: 20px;
  width: 100%;
  max-width: 340px;
  min-height: 200px;
  max-height: 500px;
  box-shadow: var(--shadow), var(--shadow-border);
  display: flex;
  flex-direction: column;
  transition: background 0.3s ease, box-shadow 0.3s ease;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  color: var(--text-secondary);
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: color 0.3s ease;
}

.history-clear-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 2px 8px;
  border-radius: 6px;
  transition: all 0.15s;
}

.history-clear-btn:hover {
  background: var(--bg-button);
  color: var(--text-clear);
}

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

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

.history-empty {
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
  padding: 30px 0;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--bg-history-item);
  transition: background 0.15s;
  cursor: default;
}

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

.history-item-expression {
  color: var(--text-secondary);
  font-size: 0.8rem;
}

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

/* ============================================================
   TOAST
   ============================================================ */

.toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--toast-bg);
  color: #fff;
  padding: 10px 24px;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: 0 8px 30px var(--toast-shadow);
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
  z-index: 100;
}

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

/* ============================================================
   RESPONSIVE
   ============================================================ */

@media (max-width: 760px) {
  body {
    padding: 20px 12px;
    align-items: center;
  }

  .wrapper {
    flex-direction: column;
    align-items: center;
  }

  .calculator,
  .history-panel {
    max-width: 340px;
  }

  .history-panel {
    max-height: 250px;
    min-height: 150px;
  }
}

@media (max-width: 380px) {
  .calculator {
    padding: 14px;
    border-radius: 16px;
  }

  .display {
    padding: 10px 16px 14px;
    min-height: 75px;
  }

  .current-operand {
    font-size: 1.8rem;
  }

  .buttons {
    gap: 8px;
  }

  button {
    font-size: 1rem;
    padding: 12px 0;
    border-radius: 10px;
  }

  button[data-action="sin"],
  button[data-action="cos"],
  button[data-action="tan"] {
    font-size: 0.8rem;
    padding: 8px 0;
  }

  button[data-action="memory-clear"],
  button[data-action="memory-recall"],
  button[data-action="memory-add"],
  button[data-action="memory-subtract"] {
    font-size: 0.75rem;
    padding: 8px 0;
  }
}
