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

:root {
  --bg: #1c1c1e;
  --display-bg: #2c2c2e;
  --display-color: #fff;
  --btn-num: #3a3a3c;
  --btn-fn: #636366;
  --btn-op: #ff9f0a;
  --btn-text: #fff;
  --shadow: rgba(0, 0, 0, 0.6);
  --toggle-bg: rgba(255, 255, 255, 0.1);
  --toggle-color: #fff;
}

:root.light {
  --bg: #e5e5ea;
  --display-bg: #f2f2f7;
  --display-color: #1c1c1e;
  --btn-num: #fff;
  --btn-fn: #d1d1d6;
  --btn-op: #ff9f0a;
  --btn-text: #1c1c1e;
  --shadow: rgba(0, 0, 0, 0.2);
  --toggle-bg: rgba(0, 0, 0, 0.08);
  --toggle-color: #1c1c1e;
}

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

.theme-toggle {
  position: fixed;
  top: 16px;
  right: 16px;
  background: var(--toggle-bg);
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.3s;
}

.calculator {
  width: 320px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 20px 60px var(--shadow);
  transition: box-shadow 0.3s;
}

.display {
  background: var(--display-bg);
  padding: 20px 24px 16px;
  text-align: right;
  transition: background 0.3s;
}

#display {
  color: var(--display-color);
  font-size: 56px;
  font-weight: 300;
  line-height: 1;
  word-break: break-all;
  min-height: 68px;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  transition: color 0.3s;
}

.buttons {
  background: var(--bg);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  transition: background 0.3s;
}

.btn {
  border: none;
  cursor: pointer;
  font-size: 28px;
  font-weight: 400;
  padding: 0;
  height: 78px;
  transition: filter 0.1s, background 0.3s, color 0.3s;
  color: var(--btn-text);
}

.btn:active {
  filter: brightness(1.15);
}

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

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

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

.btn.wide {
  grid-column: span 2;
  text-align: left;
  padding-left: 28px;
}

.sound-toggle {
  position: fixed;
  top: 112px;
  right: 16px;
  background: var(--toggle-bg);
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.3s;
  color: var(--toggle-color);
}

.history-toggle {
  position: fixed;
  top: 64px;
  right: 16px;
  background: var(--toggle-bg);
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.3s;
  color: var(--toggle-color);
}

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

.history-panel {
  position: fixed;
  top: 0;
  right: -300px;
  width: 280px;
  height: 100vh;
  background: var(--display-bg);
  box-shadow: -4px 0 20px var(--shadow);
  transition: right 0.3s ease;
  display: flex;
  flex-direction: column;
  z-index: 100;
}

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

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid rgba(128, 128, 128, 0.2);
  color: var(--display-color);
  font-size: 16px;
  font-weight: 600;
}

.history-clear-btn {
  background: none;
  border: 1px solid var(--btn-op);
  color: var(--btn-op);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 13px;
  cursor: pointer;
}

.history-clear-btn:hover {
  background: var(--btn-op);
  color: #fff;
}

#history-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

.history-item {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  padding: 10px 16px;
  cursor: pointer;
  border-bottom: 1px solid rgba(128, 128, 128, 0.1);
  transition: background 0.15s;
}

.history-item:hover {
  background: rgba(128, 128, 128, 0.1);
}

.history-expr {
  font-size: 13px;
  color: var(--btn-fn);
}

.history-result {
  font-size: 22px;
  font-weight: 300;
  color: var(--display-color);
}

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