:root {
  color-scheme: dark;
  --page-1: #080b10;
  --page-2: #111827;
  --panel: rgba(20, 24, 31, 0.86);
  --panel-border: rgba(255,255,255,.07);
  --display: rgba(7,10,15,.78);
  --key: #2a2f38;
  --key-hover: #373d48;
  --muted-key: #747c87;
  --accent: #ff9f0a;
  --accent-hover: #ffb13b;
  --text: #f6f8fb;
  --muted: #aeb6c2;
  --shadow: 0 35px 100px rgba(0,0,0,.52);
}

html[data-theme="light"] {
  color-scheme: light;
  --page-1: #edf2f7;
  --page-2: #dce6f2;
  --panel: rgba(255,255,255,.82);
  --panel-border: rgba(15,23,42,.08);
  --display: rgba(246,248,252,.92);
  --key: #e5eaf1;
  --key-hover: #d7dee8;
  --muted-key: #b8c0cb;
  --text: #101318;
  --muted: #667085;
  --shadow: 0 30px 80px rgba(51,65,85,.18);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

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

body {
  margin: 0;
  background:
    radial-gradient(circle at 20% 0%, rgba(255,255,255,.11), transparent 32%),
    linear-gradient(145deg, var(--page-1), var(--page-2));
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  transition: background .25s ease, color .25s ease;
}

button { font: inherit; }

.shell {
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding:
    max(24px, env(safe-area-inset-top))
    max(14px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
}

.calculator {
  position: relative;
  width: min(100%, 404px);
  padding: 16px;
  border: 1px solid var(--panel-border);
  border-radius: 34px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(28px) saturate(140%);
  overflow: hidden;
}

.topbar {
  display: flex;
  justify-content: space-between;
  padding: 2px 2px 10px;
}

.icon-button {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: rgba(127,127,127,.12);
  color: var(--muted);
  cursor: pointer;
  font-size: 20px;
}

.display {
  min-height: 150px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  gap: 8px;
  margin-bottom: 13px;
  padding: 22px 14px 18px;
  border-radius: 25px;
  background: var(--display);
  overflow: hidden;
}

.expression {
  width: 100%;
  min-height: 24px;
  overflow: hidden;
  color: var(--muted);
  font-size: 17px;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result {
  display: block;
  width: 100%;
  overflow: hidden;
  color: var(--text);
  font-size: clamp(43px, 13vw, 64px);
  font-weight: 300;
  line-height: 1;
  letter-spacing: -.055em;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.key {
  aspect-ratio: 1;
  border: 0;
  border-radius: 50%;
  background: var(--key);
  color: var(--text);
  cursor: pointer;
  font-size: 25px;
  font-weight: 500;
  transition: transform .09s ease, filter .12s ease, background-color .2s ease;
  user-select: none;
}

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

.key:active,
.key.is-pressed {
  transform: scale(.94);
  filter: brightness(1.15);
}

.key-muted {
  background: var(--muted-key);
  color: #11151b;
}

.key-accent {
  background: var(--accent);
  color: white;
}

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

.key-zero {
  grid-column: span 2;
  aspect-ratio: auto;
  border-radius: 999px;
  padding-left: 32px;
  text-align: left;
}

.history-panel {
  position: absolute;
  inset: 60px 16px 16px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--panel-border);
  border-radius: 25px;
  background: var(--panel);
  backdrop-filter: blur(30px);
  opacity: 0;
  pointer-events: none;
  transform: translateY(12px);
  transition: .2s ease;
}

.history-panel.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.history-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px;
  border-bottom: 1px solid var(--panel-border);
  font-weight: 650;
}

.history-head button {
  border: 0;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
}

.history-list {
  flex: 1;
  overflow: auto;
  padding: 10px;
}

.history-item {
  padding: 12px;
  border-radius: 14px;
  cursor: pointer;
}

.history-item:hover {
  background: rgba(127,127,127,.1);
}

.history-item .small {
  color: var(--muted);
  font-size: 13px;
}

.history-empty {
  padding: 28px 14px;
  color: var(--muted);
  text-align: center;
}

.access-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(3,6,10,.68);
  backdrop-filter: blur(18px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease;
}

.access-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.access-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px 22px;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 18px;
  background: rgba(18,22,29,.9);
  color: #fff;
  box-shadow: 0 25px 60px rgba(0,0,0,.35);
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255,255,255,.22);
  border-top-color: white;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 430px) {
  .shell { align-items: end; }
  .calculator { width: 100%; border-radius: 30px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation-duration: .001ms !important; }
}
