:root {
  color-scheme: light;
  --bg: #eef0f8;
  --glow-1: rgba(108, 92, 255, 0.18);
  --glow-2: rgba(56, 189, 248, 0.14);
  --card: #ffffff;
  --card-border: rgba(20, 24, 50, 0.07);
  --display: #f4f5fb;
  --text: #171a2c;
  --muted: #6d7290;
  --key: #f3f4fa;
  --key-hover: #e7e9f5;
  --key-fn: #e3e5f2;
  --key-sci: #eef0fb;
  --sci-text: #3b4bc8;
  --op-bg: rgba(108, 92, 255, 0.12);
  --op-text: #5443e6;
  --accent: #6c5cff;
  --accent-2: #3fa9f5;
  --danger: #d93d42;
  --shadow: 0 30px 60px -24px rgba(40, 44, 90, 0.3);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0b0d18;
    --glow-1: rgba(124, 108, 255, 0.22);
    --glow-2: rgba(56, 189, 248, 0.12);
    --card: #141729;
    --card-border: rgba(255, 255, 255, 0.06);
    --display: #0e1121;
    --text: #eef0ff;
    --muted: #8a8fb0;
    --key: #1d2138;
    --key-hover: #272c49;
    --key-fn: #2a2f4e;
    --key-sci: #181c32;
    --sci-text: #93b6ff;
    --op-bg: rgba(124, 108, 255, 0.2);
    --op-text: #b0a6ff;
    --accent: #7c6cff;
    --accent-2: #38bdf8;
    --danger: #ff6b6b;
    --shadow: 0 30px 70px -24px rgba(0, 0, 0, 0.7);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0d18;
  --glow-1: rgba(124, 108, 255, 0.22);
  --glow-2: rgba(56, 189, 248, 0.12);
  --card: #141729;
  --card-border: rgba(255, 255, 255, 0.06);
  --display: #0e1121;
  --text: #eef0ff;
  --muted: #8a8fb0;
  --key: #1d2138;
  --key-hover: #272c49;
  --key-fn: #2a2f4e;
  --key-sci: #181c32;
  --sci-text: #93b6ff;
  --op-bg: rgba(124, 108, 255, 0.2);
  --op-text: #b0a6ff;
  --accent: #7c6cff;
  --accent-2: #38bdf8;
  --danger: #ff6b6b;
  --shadow: 0 30px 70px -24px rgba(0, 0, 0, 0.7);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  color: var(--text);
  background:
    radial-gradient(900px 520px at 10% -10%, var(--glow-1), transparent 70%),
    radial-gradient(700px 520px at 105% 110%, var(--glow-2), transparent 70%),
    var(--bg);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Card ---------- */

.calculator {
  position: relative;
  width: 100%;
  max-width: 400px;
  padding: 18px;
  border-radius: 28px;
  background: var(--card);
  border: 1px solid var(--card-border);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: max-width 0.25s ease;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.segmented {
  display: inline-flex;
  padding: 4px;
  border-radius: 12px;
  background: var(--key);
}

.segmented button {
  border: none;
  background: transparent;
  padding: 7px 14px;
  border-radius: 9px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}

.segmented button[aria-selected="true"] {
  background: var(--card);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.topbar-actions {
  display: flex;
  gap: 6px;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}

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

.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#themeBtn .sun {
  display: none;
}

:root[data-theme="dark"] #themeBtn .sun {
  display: block;
}

:root[data-theme="dark"] #themeBtn .moon {
  display: none;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) #themeBtn .sun {
    display: block;
  }
  :root:not([data-theme="light"]) #themeBtn .moon {
    display: none;
  }
}

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

.display {
  position: relative;
  min-height: 140px;
  padding: 14px 16px 16px;
  border-radius: 20px;
  background: var(--display);
  border: 1px solid var(--card-border);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: stretch;
  text-align: right;
  font-family: "Space Grotesk", "Inter", system-ui, sans-serif;
}

.status {
  position: absolute;
  top: 12px;
  left: 14px;
  display: flex;
  gap: 6px;
}

.tag {
  padding: 2px 7px;
  border-radius: 6px;
  font-family: "Inter", system-ui, sans-serif;
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--op-text);
  background: var(--op-bg);
}

.expression {
  margin-top: 26px;
  font-size: 2.5rem;
  line-height: 1.2;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  transition: font-size 0.18s ease, color 0.18s ease;
}

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

.expression.long {
  font-size: 1.75rem;
}

.expression .placeholder,
.expression .ghost {
  color: var(--muted);
  opacity: 0.55;
}

.result {
  min-height: 1.6rem;
  font-size: 1.25rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
  transition: font-size 0.18s ease, color 0.18s ease;
}

.result.error {
  color: var(--danger);
  font-size: 1rem;
  font-family: "Inter", system-ui, sans-serif;
}

/* After "=", the expression shrinks and the answer takes the spotlight. */
.display.evaluated .expression {
  font-size: 1.125rem;
  color: var(--muted);
}

.display.evaluated .result {
  font-size: 2.75rem;
  color: var(--text);
  line-height: 1.15;
}

.display.evaluated .result.long {
  font-size: 2rem;
}

/* ---------- Memory row ---------- */

.memory-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin: 12px 0;
}

.memory-row button {
  border: none;
  background: transparent;
  padding: 6px 0;
  border-radius: 8px;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}

.memory-row button:hover {
  background: var(--key);
  color: var(--text);
}

/* ---------- Keypad ---------- */

.pad {
  display: grid;
  gap: 10px;
}

.sci-keys {
  display: none;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.calculator.sci .sci-keys {
  display: grid;
}

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

.key {
  min-height: 60px;
  border: none;
  border-radius: 16px;
  background: var(--key);
  font-size: 1.375rem;
  font-weight: 500;
  transition: background 0.12s, transform 0.08s, box-shadow 0.12s;
  user-select: none;
}

.key:hover {
  background: var(--key-hover);
}

.key:active,
.key.pressed {
  transform: scale(0.95);
}

.key.fn {
  background: var(--key-fn);
  font-size: 1.125rem;
}

.key.op {
  background: var(--op-bg);
  color: var(--op-text);
  font-size: 1.625rem;
}

.key.op:hover {
  filter: brightness(1.1);
}

.key.equals {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 1.625rem;
  box-shadow: 0 10px 24px -10px var(--accent);
}

.key.equals:hover {
  filter: brightness(1.08);
}

.key.sci {
  min-height: 44px;
  border-radius: 12px;
  background: var(--key-sci);
  color: var(--sci-text);
  font-size: 0.9375rem;
}

.key.sci:hover {
  background: var(--key-hover);
}

.key.sci.toggle[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
}

.key .alt {
  display: none;
}

.calculator.second .key .main {
  display: none;
}

.calculator.second .key .alt {
  display: inline;
}

/* Side-by-side scientific layout on wide screens. */
@media (min-width: 860px) {
  .calculator.sci {
    max-width: 820px;
  }

  .calculator.sci .pad {
    grid-template-columns: 5fr 4fr;
    gap: 16px;
  }

  .calculator.sci .sci-keys {
    grid-auto-rows: 1fr;
    gap: 10px;
  }

  .key.sci {
    font-size: 1rem;
  }
}

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

  .key {
    min-height: 54px;
  }

  .key.sci {
    min-height: 40px;
    font-size: 0.875rem;
  }

  .expression {
    font-size: 2.125rem;
  }
}

/* ---------- History panel ---------- */

.history {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: 18px;
  background: var(--card);
  animation: slide-in 0.2s ease;
}

.history[hidden] {
  display: none;
}

@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
}

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.history-head > div {
  display: flex;
  align-items: center;
  gap: 4px;
}

.history h2 {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
}

.text-btn {
  border: none;
  background: transparent;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--danger);
}

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

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.history-list button {
  width: 100%;
  display: block;
  text-align: right;
  border: none;
  background: transparent;
  padding: 12px 10px;
  border-radius: 12px;
  font-family: "Space Grotesk", "Inter", system-ui, sans-serif;
}

.history-list button:hover {
  background: var(--key);
}

.history-list .h-expr {
  display: block;
  font-size: 0.9375rem;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.history-list .h-result {
  display: block;
  font-size: 1.5rem;
  overflow-wrap: anywhere;
}

.history-empty {
  margin: auto;
  color: var(--muted);
}

.history-empty[hidden] {
  display: none;
}
