:root {
  color-scheme: dark;
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg, radial-gradient(circle at top, #1f2937, #0f172a 70%));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  color: var(--text, #f8fafc);
  --operator-bg: linear-gradient(135deg, #38bdf8, #0ea5e9);
  --operator-text: #0f172a;
  --accent-bg: rgba(244, 114, 182, 0.2);
  --accent-border: rgba(244, 114, 182, 0.35);
  transition: background 0.6s ease-in-out;
}

.app {
  width: min(960px, 100%);
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.app__header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.app__title {
  margin: 0;
  font-size: clamp(2rem, 4vw, 2.6rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.theme-switcher {
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(15, 23, 42, 0.35);
  border: 1px solid rgba(148, 163, 184, 0.25);
  padding: 10px 14px;
  border-radius: 14px;
}

.theme-switcher label {
  font-weight: 600;
  color: var(--muted, #94a3b8);
}

.theme-switcher select {
  background: rgba(15, 23, 42, 0.3);
  border: 1px solid rgba(148, 163, 184, 0.3);
  color: var(--text, #f8fafc);
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 10px;
}

.workspace {
  display: grid;
  gap: 24px;
  grid-template-columns: minmax(0, 1fr);
}

.calculator {
  background: var(--panel, rgba(15, 23, 42, 0.75));
  border: 1px solid var(--border, rgba(148, 163, 184, 0.2));
  border-radius: 28px;
  padding: 28px 24px 24px;
  box-shadow: var(--shadow, 0 24px 48px rgba(15, 23, 42, 0.65));
  backdrop-filter: blur(16px);
}

.display {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 90px;
  padding: 12px 16px;
  border-radius: 18px;
  background: rgba(15, 23, 42, 0.4);
  border: 1px solid rgba(148, 163, 184, 0.15);
  text-align: right;
  overflow: hidden;
}

.history {
  font-size: 0.95rem;
  color: var(--muted);
  min-height: 1.2em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.output {
  font-size: clamp(2.5rem, 8vw, 3.5rem);
  font-weight: 600;
  letter-spacing: -0.03em;
}

.keypad {
  margin-top: 24px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.key {
  position: relative;
  display: grid;
  place-items: center;
  height: 64px;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text);
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 16px;
  backdrop-filter: blur(12px);
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.key--active {
  transform: translateY(1px);
  box-shadow: inset 0 0 0 2px rgba(248, 250, 252, 0.25);
}

.key:focus-visible {
  outline: 3px solid rgba(56, 189, 248, 0.65);
  outline-offset: 3px;
}

.key:active {
  transform: translateY(1px);
  background: rgba(15, 23, 42, 0.75);
}

.key--operator {
  background: var(--operator-bg);
  color: var(--operator-text);
  border: none;
}

.key--accent {
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
}

.key--wide {
  grid-column: span 2;
}

.history-panel {
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 24px;
  padding: 20px;
  box-shadow: var(--shadow, 0 24px 48px rgba(15, 23, 42, 0.5));
  max-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.history-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.history-panel__header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.history-panel__clear {
  border: none;
  background: var(--accent-bg);
  color: var(--text, #f8fafc);
  padding: 6px 12px;
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--accent-border);
}

.history-panel__list {
  list-style: decimal inside;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 340px;
  overflow-y: auto;
  scrollbar-width: thin;
}

.history-panel__empty {
  margin: 0;
  font-size: 0.95rem;
  color: var(--muted, #94a3b8);
}

.history-panel__list li {
  background: rgba(15, 23, 42, 0.45);
  border: 1px solid rgba(148, 163, 184, 0.15);
  border-radius: 12px;
  padding: 8px 12px;
  font-weight: 500;
  color: var(--text, #f8fafc);
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.history-panel__list li span {
  color: var(--muted, #94a3b8);
}

.history-panel__list li strong {
  margin-left: auto;
  font-weight: 700;
  color: var(--text, #f8fafc);
}

@media (min-width: 900px) {
  .workspace {
    grid-template-columns: minmax(0, 2fr) minmax(260px, 1fr);
  }
}

@media (max-width: 480px) {
  body {
    padding: 16px;
  }

  .workspace {
    gap: 16px;
  }

  .calculator {
    padding: 24px 18px 20px;
  }

  .key {
    height: 58px;
    border-radius: 14px;
  }
}

/* Themes */
body[data-theme="ocean"] {
  --bg: radial-gradient(circle at top, #1e3a8a, #0f172a 70%);
  --panel: rgba(15, 23, 42, 0.78);
  --border: rgba(125, 211, 252, 0.25);
  --text: #f8fafc;
  --muted: #a5b4fc;
  --operator-bg: linear-gradient(135deg, #38bdf8, #0ea5e9);
  --operator-text: #0f172a;
  --accent-bg: rgba(244, 114, 182, 0.2);
  --accent-border: rgba(244, 114, 182, 0.35);
  --shadow: 0 24px 48px rgba(15, 23, 42, 0.65);
}

body[data-theme="sunset"] {
  --bg: radial-gradient(circle at top, #be123c, #4c0519 70%);
  --panel: rgba(76, 5, 25, 0.8);
  --border: rgba(248, 113, 113, 0.35);
  --text: #fff7ed;
  --muted: #fecdd3;
  --operator-bg: linear-gradient(135deg, #fb7185, #f97316);
  --operator-text: #450a0a;
  --accent-bg: rgba(248, 172, 120, 0.25);
  --accent-border: rgba(248, 172, 120, 0.5);
  --shadow: 0 24px 48px rgba(76, 5, 25, 0.65);
}

body[data-theme="forest"] {
  --bg: radial-gradient(circle at top, #14532d, #052e16 70%);
  --panel: rgba(10, 38, 22, 0.82);
  --border: rgba(134, 239, 172, 0.3);
  --text: #ecfdf5;
  --muted: #bbf7d0;
  --operator-bg: linear-gradient(135deg, #34d399, #22c55e);
  --operator-text: #064e3b;
  --accent-bg: rgba(250, 204, 21, 0.2);
  --accent-border: rgba(250, 204, 21, 0.45);
  --shadow: 0 24px 48px rgba(5, 46, 22, 0.65);
}

body[data-theme="retro"] {
  --bg: radial-gradient(circle at top, #312e81, #111827 70%);
  --panel: rgba(17, 24, 39, 0.85);
  --border: rgba(129, 140, 248, 0.35);
  --text: #e0f2fe;
  --muted: #c7d2fe;
  --operator-bg: linear-gradient(135deg, #f59e0b, #ef4444);
  --operator-text: #2f0f36;
  --accent-bg: rgba(248, 113, 113, 0.25);
  --accent-border: rgba(248, 113, 113, 0.45);
  --shadow: 0 24px 48px rgba(17, 24, 39, 0.65);
}
