:root {
  color-scheme: dark;
  --bg: #14101f;
  --panel: #1d1730;
  --panel-strong: #251d3c;
  --ink: #f7f5ff;
  --muted: #b6a7df;
  --line: #3a2f5c;
  --number: #271f40;
  --number-hover: #342a55;
  --utility: #2d2348;
  --utility-hover: #3b2e5e;
  --operator: #ff7a18;
  --operator-hover: #ff9233;
  --equals: #ff2d95;
  --equals-hover: #ff52ab;
  --accent: #ff4fd8;
  --rainbow: linear-gradient(
    90deg,
    #ff3b3b,
    #ff8a00,
    #ffe600,
    #36e26a,
    #2bd4ff,
    #6a5bff,
    #d24bff,
    #ff3b3b
  );
  --shadow: 0 24px 80px rgba(120, 40, 200, 0.35);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

@keyframes rainbow-shift {
  to {
    background-position: 200% 50%;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background:
    radial-gradient(circle at 12% 8%, rgba(255, 79, 216, 0.18), transparent 38%),
    radial-gradient(circle at 88% 90%, rgba(43, 212, 255, 0.18), transparent 40%),
    linear-gradient(
      135deg,
      #2a0f3d 0%,
      var(--bg) 38%,
      #0f1a3a 72%,
      #1a0f33 100%
    );
  background-attachment: fixed;
  color: var(--ink);
}

button {
  font: inherit;
}

/* Twinkling starfield behind everything */
.starfield {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
}

.star {
  position: absolute;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 0 6px 1px rgba(255, 255, 255, 0.8);
  animation: star-twinkle 3s ease-in-out infinite;
}

@keyframes star-twinkle {
  0%, 100% { opacity: 0.25; transform: scale(0.7); }
  50% { opacity: 1; transform: scale(1.25); }
}

/* Scatter + vary the stars (color, size, position, timing) */
.star:nth-child(5n + 1) { background: #ff6ec7; box-shadow: 0 0 8px 1px rgba(255, 110, 199, 0.9); }
.star:nth-child(5n + 2) { background: #5bc8ff; box-shadow: 0 0 8px 1px rgba(91, 200, 255, 0.9); }
.star:nth-child(5n + 3) { background: #ffe45b; box-shadow: 0 0 8px 1px rgba(255, 228, 91, 0.9); }
.star:nth-child(5n + 4) { background: #8aff9b; box-shadow: 0 0 8px 1px rgba(138, 255, 155, 0.9); }
.star:nth-child(3n) { width: 6px; height: 6px; }
.star:nth-child(7n) { width: 2px; height: 2px; }

.star:nth-child(1)  { top: 6%;  left: 8%;  animation-delay: 0.0s; }
.star:nth-child(2)  { top: 12%; left: 82%; animation-delay: 0.4s; }
.star:nth-child(3)  { top: 18%; left: 24%; animation-delay: 0.9s; }
.star:nth-child(4)  { top: 9%;  left: 56%; animation-delay: 1.3s; }
.star:nth-child(5)  { top: 27%; left: 92%; animation-delay: 0.2s; }
.star:nth-child(6)  { top: 33%; left: 14%; animation-delay: 1.7s; }
.star:nth-child(7)  { top: 41%; left: 70%; animation-delay: 0.6s; }
.star:nth-child(8)  { top: 47%; left: 4%;  animation-delay: 2.1s; }
.star:nth-child(9)  { top: 52%; left: 88%; animation-delay: 1.0s; }
.star:nth-child(10) { top: 58%; left: 38%; animation-delay: 0.3s; }
.star:nth-child(11) { top: 63%; left: 60%; animation-delay: 1.9s; }
.star:nth-child(12) { top: 69%; left: 18%; animation-delay: 0.8s; }
.star:nth-child(13) { top: 74%; left: 84%; animation-delay: 1.5s; }
.star:nth-child(14) { top: 79%; left: 48%; animation-delay: 0.5s; }
.star:nth-child(15) { top: 84%; left: 10%; animation-delay: 2.3s; }
.star:nth-child(16) { top: 88%; left: 74%; animation-delay: 1.1s; }
.star:nth-child(17) { top: 15%; left: 40%; animation-delay: 1.6s; }
.star:nth-child(18) { top: 22%; left: 64%; animation-delay: 0.7s; }
.star:nth-child(19) { top: 36%; left: 50%; animation-delay: 2.0s; }
.star:nth-child(20) { top: 44%; left: 30%; animation-delay: 0.1s; }
.star:nth-child(21) { top: 55%; left: 76%; animation-delay: 1.4s; }
.star:nth-child(22) { top: 66%; left: 6%;  animation-delay: 0.9s; }
.star:nth-child(23) { top: 72%; left: 94%; animation-delay: 2.2s; }
.star:nth-child(24) { top: 81%; left: 62%; animation-delay: 0.4s; }
.star:nth-child(25) { top: 90%; left: 34%; animation-delay: 1.8s; }
.star:nth-child(26) { top: 4%;  left: 70%; animation-delay: 1.2s; }
.star:nth-child(27) { top: 30%; left: 2%;  animation-delay: 0.6s; }
.star:nth-child(28) { top: 50%; left: 54%; animation-delay: 2.4s; }
.star:nth-child(29) { top: 60%; left: 22%; animation-delay: 0.3s; }
.star:nth-child(30) { top: 86%; left: 90%; animation-delay: 1.7s; }

/* Floating unicorn behind everything */
.bg-unicorn {
  position: fixed;
  right: 4vw;
  bottom: -2vh;
  z-index: 0;
  font-size: clamp(200px, 40vw, 540px);
  line-height: 1;
  opacity: 0.18;
  pointer-events: none;
  filter: drop-shadow(0 0 60px rgba(255, 79, 216, 0.55)) saturate(1.3);
  transform: rotate(-6deg);
  animation: unicorn-float 9s ease-in-out infinite;
}

@keyframes unicorn-float {
  50% {
    transform: rotate(-6deg) translateY(-22px);
  }
}

/* Big bold rainbow page title */
.playhouse-title {
  position: relative;
  z-index: 1;
  margin: 0;
  padding: 34px 16px 8px;
  text-align: center;
  font-size: clamp(2.6rem, 8vw, 6rem);
  font-weight: 900;
  letter-spacing: 0.02em;
  line-height: 1.02;
  background: var(--rainbow);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 18px rgba(255, 79, 216, 0.45));
  animation: rainbow-shift 5s linear infinite;
}

/* Playful subtitle under the title */
.playhouse-subtitle {
  position: relative;
  z-index: 1;
  margin: 0;
  padding: 0 16px 6px;
  text-align: center;
  font-size: clamp(1.05rem, 2.6vw, 1.6rem);
  font-weight: 700;
  font-style: italic;
  letter-spacing: 0.01em;
  color: var(--muted);
  text-shadow: 0 0 16px rgba(255, 79, 216, 0.35);
}

/* Keep the calculator above the starfield */
.app-shell {
  position: relative;
  z-index: 1;
  min-height: auto;
}

@media (prefers-reduced-motion: reduce) {
  .star,
  .playhouse-title,
  .bg-unicorn {
    animation: none;
  }
}

.app-shell {
  width: min(1120px, calc(100vw - 32px));
  min-height: calc(100vh - 180px);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(320px, 440px) minmax(260px, 1fr);
  gap: 24px;
  align-items: center;
  padding: 12px 0 32px;
}

.calculator,
.history-panel {
  position: relative;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(29, 23, 48, 0.82);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.calculator {
  border-radius: 14px;
  overflow: hidden;
}

/* Rainbow top edge on the calculator */
.calculator::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 4px;
  background: var(--rainbow);
  background-size: 200% 100%;
  animation: rainbow-shift 6s linear infinite;
  z-index: 2;
}

.topbar,
.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar {
  padding: 20px 22px 16px;
}

.eyebrow,
.topbar h1,
.panel-heading h2 {
  margin: 0;
}

.eyebrow {
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.topbar h1 {
  margin-top: 2px;
  font-size: 1.45rem;
  line-height: 1.15;
  background: var(--rainbow);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: rainbow-shift 6s linear infinite;
}

.icon-button {
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--ink);
  cursor: pointer;
  display: inline-grid;
  place-items: center;
  font-size: 1.15rem;
  transition:
    background 140ms ease,
    transform 140ms ease,
    border-color 140ms ease;
}

.icon-button:hover {
  background: var(--number-hover);
  border-color: var(--accent);
}

.icon-button:active {
  transform: translateY(1px);
}

.display {
  min-height: 146px;
  padding: 14px 22px 24px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background:
    linear-gradient(180deg, rgba(40, 30, 66, 0.55), rgba(20, 16, 31, 0.85)),
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.04) 0 1px, transparent 1px 24px);
}

.expression {
  min-height: 28px;
  color: var(--muted);
  font-size: 0.98rem;
  overflow: hidden;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result {
  display: block;
  min-height: 58px;
  color: var(--ink);
  text-shadow: 0 0 18px rgba(255, 79, 216, 0.35);
  font-variant-numeric: tabular-nums;
  font-size: clamp(2.3rem, 9vw, 3.8rem);
  font-weight: 800;
  letter-spacing: 0;
  line-height: 1.02;
  overflow: hidden;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  padding: 18px;
  background: rgba(15, 11, 26, 0.6);
}

.key {
  aspect-ratio: 1.08 / 1;
  min-width: 0;
  border: 0;
  border-radius: 8px;
  background: var(--number);
  color: var(--ink);
  cursor: pointer;
  font-size: 1.35rem;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  display: inline-grid;
  place-items: center;
  transition:
    background 140ms ease,
    color 140ms ease,
    transform 140ms ease,
    box-shadow 140ms ease;
}

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

.key:active,
.key.is-active {
  transform: translateY(1px);
  box-shadow: inset 0 2px 8px rgba(23, 33, 31, 0.14);
}

/* Rainbow-tinted number keys */
[data-digit="1"] { color: #ff5b5b; }
[data-digit="2"] { color: #ff8a3d; }
[data-digit="3"] { color: #ffd23d; }
[data-digit="4"] { color: #9be23d; }
[data-digit="5"] { color: #36e26a; }
[data-digit="6"] { color: #2bd4ff; }
[data-digit="7"] { color: #4f8bff; }
[data-digit="8"] { color: #6a5bff; }
[data-digit="9"] { color: #b95bff; }
[data-digit="0"] { color: #ff5bd0; }

.key[data-digit]:hover {
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12) inset, 0 6px 18px rgba(0, 0, 0, 0.3);
}

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

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

.key.operator {
  background: var(--operator);
  color: #ffffff;
}

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

.key.equals {
  background: var(--rainbow);
  background-size: 200% 100%;
  color: #ffffff;
  font-weight: 850;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  box-shadow: 0 8px 26px rgba(255, 45, 149, 0.4);
  animation: rainbow-shift 5s linear infinite;
}

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

.history-panel {
  min-height: 480px;
  border-radius: 14px;
  padding: 22px;
}

.panel-heading {
  border-bottom: 1px solid var(--line);
  padding-bottom: 16px;
}

.panel-heading h2 {
  font-size: 1rem;
}

#history-count {
  min-width: 28px;
  height: 28px;
  border-radius: 999px;
  background: var(--rainbow);
  background-size: 200% 100%;
  animation: rainbow-shift 5s linear infinite;
  color: #ffffff;
  display: inline-grid;
  place-items: center;
  font-size: 0.8rem;
  font-weight: 800;
}

.history-list {
  min-height: 348px;
  margin: 0;
  padding: 14px 0 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-list:empty::before {
  content: "No entries";
  color: var(--muted);
  display: grid;
  min-height: 320px;
  place-items: center;
  font-size: 0.95rem;
}

.history-item {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  padding: 12px;
  cursor: pointer;
  display: grid;
  gap: 4px;
  text-align: right;
  transition:
    background 140ms ease,
    border-color 140ms ease;
}

.history-item:hover {
  background: #2f2550;
  border-color: var(--accent);
}

.history-expression,
.history-result {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-expression {
  color: var(--muted);
  font-size: 0.85rem;
}

.history-result {
  color: var(--ink);
  font-size: 1.15rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 840px) {
  .app-shell {
    grid-template-columns: 1fr;
    align-items: start;
    padding: 20px 0;
  }

  .calculator {
    width: min(100%, 460px);
    margin: 0 auto;
  }

  .history-panel {
    min-height: 0;
    width: min(100%, 460px);
    margin: 0 auto;
  }

  .history-list {
    min-height: 160px;
  }

  .history-list:empty::before {
    min-height: 150px;
  }
}

@media (max-width: 420px) {
  .app-shell {
    width: min(100vw - 20px, 460px);
  }

  .topbar,
  .display {
    padding-left: 16px;
    padding-right: 16px;
  }

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

  .key {
    font-size: 1.18rem;
  }
}
