:root {
  --bg: #0b0c1a;
  --card: rgba(24, 26, 48, 0.62);
  --card-border: rgba(255, 255, 255, 0.08);
  --text: #f3f4ff;
  --muted: #9aa0c3;
  --key: rgba(255, 255, 255, 0.06);
  --key-hover: rgba(255, 255, 255, 0.11);
  --key-util: rgba(255, 255, 255, 0.12);
  --key-fn: rgba(255, 255, 255, 0.04);
  --accent: #7c5cff;
  --accent-2: #3dd6f5;
  --op: #a48bff;
  --op-bg: rgba(124, 92, 255, 0.16);
  --error: #ff6b81;
  --shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.6);
  --blob1: #5b3cff;
  --blob2: #00b3d6;
  --blob3: #ff3c8e;
  --radius: 28px;
  --key-radius: 20px;
  --gap: 12px;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --spring: cubic-bezier(.34, 1.56, .64, 1);
}

:root[data-theme="light"] {
  --bg: #eef0f8;
  --card: rgba(255, 255, 255, 0.68);
  --card-border: rgba(20, 20, 60, 0.08);
  --text: #15172e;
  --muted: #6b6f8e;
  --key: rgba(20, 22, 60, 0.05);
  --key-hover: rgba(20, 22, 60, 0.09);
  --key-util: rgba(20, 22, 60, 0.1);
  --key-fn: rgba(20, 22, 60, 0.035);
  --op: #6a46ff;
  --op-bg: rgba(124, 92, 255, 0.12);
  --shadow: 0 30px 70px -25px rgba(40, 40, 110, 0.35);
  --blob1: #a996ff;
  --blob2: #7fe3f7;
  --blob3: #ffa3c9;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #eef0f8;
    --card: rgba(255, 255, 255, 0.68);
    --card-border: rgba(20, 20, 60, 0.08);
    --text: #15172e;
    --muted: #6b6f8e;
    --key: rgba(20, 22, 60, 0.05);
    --key-hover: rgba(20, 22, 60, 0.09);
    --key-util: rgba(20, 22, 60, 0.1);
    --key-fn: rgba(20, 22, 60, 0.035);
    --op: #6a46ff;
    --op-bg: rgba(124, 92, 255, 0.12);
    --shadow: 0 30px 70px -25px rgba(40, 40, 110, 0.35);
    --blob1: #a996ff;
    --blob2: #7fe3f7;
    --blob3: #ffa3c9;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: grid;
  place-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  transition: background-color .4s var(--ease), color .4s var(--ease);
}

button { font: inherit; color: inherit; }

/* ---------- animated background ---------- */
.bg { position: fixed; inset: 0; overflow: hidden; z-index: 0; pointer-events: none; }
.blob {
  position: absolute; width: 46vmax; height: 46vmax; border-radius: 50%;
  filter: blur(80px); opacity: .45;
  animation: float 22s ease-in-out infinite alternate;
}
.b1 { background: var(--blob1); top: -12vmax; left: -10vmax; }
.b2 { background: var(--blob2); bottom: -16vmax; right: -12vmax; animation-delay: -7s; }
.b3 { background: var(--blob3); top: 40%; left: 55%; width: 30vmax; height: 30vmax; opacity: .25; animation-delay: -14s; }
@keyframes float {
  0% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(6vmax, 4vmax) scale(1.08); }
  100% { transform: translate(-4vmax, 7vmax) scale(.95); }
}

/* ---------- card ---------- */
.calc {
  position: relative;
  z-index: 1;
  width: min(100%, 400px);
  max-height: calc(100dvh - 32px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(24px) saturate(140%);
  -webkit-backdrop-filter: blur(24px) saturate(140%);
  overflow: hidden;
  animation: enter .7s var(--spring) both;
  transition: width .45s var(--ease);
}
@keyframes enter {
  from { opacity: 0; transform: translateY(24px) scale(.96); }
  to { opacity: 1; transform: none; }
}

/* ---------- top bar ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.icon-btn {
  width: 42px; height: 42px; border: 0; border-radius: 14px;
  background: var(--key); cursor: pointer;
  display: grid; place-items: center;
  transition: background .2s, transform .2s var(--spring);
}
.icon-btn:hover { background: var(--key-hover); }
.icon-btn:active { transform: scale(.9); }
.icon-btn svg, .copy-btn svg, .k svg {
  width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
.icon-btn .moon { display: none; }
:root[data-theme="light"] .icon-btn .sun { display: none; }
:root[data-theme="light"] .icon-btn .moon { display: block; }
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .icon-btn .sun { display: none; }
  :root:not([data-theme="dark"]) .icon-btn .moon { display: block; }
}

.seg {
  position: relative; display: flex; padding: 4px; border-radius: 14px; background: var(--key);
}
.seg::before {
  content: ""; position: absolute; top: 4px; bottom: 4px; left: 4px; width: calc(50% - 4px);
  border-radius: 10px; background: linear-gradient(135deg, var(--accent), var(--accent-2));
  transition: transform .4s var(--spring);
  box-shadow: 0 6px 18px -6px var(--accent);
}
.seg.sci-on::before { transform: translateX(100%); }
.seg-btn {
  position: relative; z-index: 1; border: 0; background: none; cursor: pointer;
  padding: 8px 14px; font-size: 13px; font-weight: 600; border-radius: 10px; color: var(--muted);
  transition: color .3s; min-width: 92px;
}
.seg-btn.active { color: #fff; }

/* ---------- display ---------- */
.display {
  position: relative;
  min-height: 132px;
  padding: 14px 6px 6px;
  display: flex; flex-direction: column; justify-content: flex-end; align-items: stretch;
  text-align: right;
}
.angle-badge {
  position: absolute; top: 6px; left: 6px;
  font-size: 11px; font-weight: 700; letter-spacing: .08em; padding: 3px 8px;
  border-radius: 8px; background: var(--op-bg); color: var(--op);
}
.expr {
  min-height: 28px; font-size: 22px; color: var(--muted);
  overflow-x: auto; white-space: nowrap; scrollbar-width: none;
  letter-spacing: .02em;
}
.expr::-webkit-scrollbar { display: none; }
.expr .caret {
  display: inline-block; width: 2px; height: 1.05em; margin-left: 2px; vertical-align: -0.15em;
  background: var(--accent-2); animation: blink 1.1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.result-row { display: flex; align-items: center; justify-content: flex-end; gap: 8px; min-width: 0; }
.result {
  display: block; flex: 1; min-width: 0;
  font-size: clamp(40px, 12vw, 56px); font-weight: 300; line-height: 1.15; letter-spacing: -.02em;
  overflow-x: auto; white-space: nowrap; scrollbar-width: none;
  transition: color .3s, font-size .25s var(--ease);
  font-variant-numeric: tabular-nums;
}
.result::-webkit-scrollbar { display: none; }
.result.preview { color: var(--muted); font-size: clamp(30px, 9vw, 40px); }
.result.error { color: var(--error); font-size: clamp(26px, 7vw, 32px); }
.result.pop { animation: pop .45s var(--spring); }
@keyframes pop {
  0% { transform: translateY(18px); opacity: 0; }
  100% { transform: none; opacity: 1; }
}
.display.shake { animation: shake .4s var(--ease); }
@keyframes shake {
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}
.copy-btn {
  flex: none; width: 34px; height: 34px; border: 0; border-radius: 10px; background: transparent;
  color: var(--muted); cursor: pointer; display: grid; place-items: center; opacity: .7;
  transition: opacity .2s, background .2s, transform .2s var(--spring);
}
.copy-btn:hover { opacity: 1; background: var(--key); }
.copy-btn:active { transform: scale(.85); }
.copy-btn svg { width: 18px; height: 18px; }

/* ---------- keys ---------- */
.keys-wrap { display: flex; flex-direction: column; gap: var(--gap); }

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

.sci {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px;
  max-height: 0; opacity: 0; transform: translateY(-8px) scale(.98);
  overflow: hidden; pointer-events: none;
  transition: max-height .45s var(--ease), opacity .3s var(--ease), transform .45s var(--spring), margin .45s var(--ease);
  margin-bottom: calc(var(--gap) * -1);
}
.calc.sci-mode .sci {
  max-height: 200px; opacity: 1; transform: none; pointer-events: auto; margin-bottom: 0;
}
.sci .k { height: 42px; font-size: 15px; border-radius: 12px; }
/* INV toggles which trig row is visible */
.sci [data-insert="asin("], .sci [data-insert="acos("], .sci [data-insert="atan("] { display: none; }
.sci.inv [data-insert="asin("], .sci.inv [data-insert="acos("], .sci.inv [data-insert="atan("] { display: grid; }
.sci.inv [data-insert="sin("], .sci.inv [data-insert="cos("], .sci.inv [data-insert="tan("] { display: none; }

.k {
  position: relative; overflow: hidden;
  height: clamp(58px, 9.5dvh, 72px);
  border: 0; border-radius: var(--key-radius); cursor: pointer;
  background: var(--key);
  font-size: 26px; font-weight: 500;
  display: grid; place-items: center;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
  transition: background .2s, transform .18s var(--spring), box-shadow .2s;
}
.k:hover { background: var(--key-hover); }
.k:active, .k.pressed { transform: scale(.92); }
.k:focus-visible, .icon-btn:focus-visible, .seg-btn:focus-visible, .copy-btn:focus-visible, .text-btn:focus-visible {
  outline: 2px solid var(--accent-2); outline-offset: 2px;
}
.k.util { background: var(--key-util); font-size: 22px; }
.k.fn { background: var(--key-fn); color: var(--muted); font-weight: 600; }
.k.fn:hover { color: var(--text); }
.k.fn.on { background: var(--op-bg); color: var(--op); }
.k.op { background: var(--op-bg); color: var(--op); font-size: 30px; }
.k.op.active { background: var(--op); color: #fff; }
.k.eq {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; font-size: 30px;
  box-shadow: 0 12px 28px -10px var(--accent);
}
.k.eq:hover { filter: brightness(1.08); }

.ripple {
  position: absolute; border-radius: 50%; pointer-events: none;
  background: currentColor; opacity: .25; transform: scale(0);
  animation: ripple .55s var(--ease) forwards;
}
@keyframes ripple { to { transform: scale(2.6); opacity: 0; } }

/* ---------- history drawer ---------- */
.history {
  position: absolute; inset: 0 0 0 auto; width: min(86%, 340px); z-index: 5;
  background: var(--card); backdrop-filter: blur(30px); -webkit-backdrop-filter: blur(30px);
  border-left: 1px solid var(--card-border);
  display: flex; flex-direction: column;
  padding: 18px;
  transform: translateX(105%);
  transition: transform .45s var(--ease);
  visibility: hidden;
}
.calc.history-open .history { transform: none; visibility: visible; }
.history-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.history-head h2 { margin: 0; font-size: 18px; font-weight: 600; }
.text-btn {
  border: 0; background: none; color: var(--op); cursor: pointer; font-weight: 600; font-size: 14px;
  padding: 6px 10px; border-radius: 10px;
}
.text-btn:hover { background: var(--op-bg); }
.history-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.history-list li { animation: slidein .35s var(--ease) both; }
.history-list button {
  width: 100%; text-align: right; border: 0; background: none; cursor: pointer;
  padding: 12px 10px; border-radius: 14px; display: block;
  transition: background .2s;
}
.history-list button:hover { background: var(--key); }
.history-list .h-expr { display: block; color: var(--muted); font-size: 14px; word-break: break-all; }
.history-list .h-res { display: block; font-size: 22px; margin-top: 2px; word-break: break-all; }
@keyframes slidein { from { opacity: 0; transform: translateX(16px); } to { opacity: 1; transform: none; } }
.history-empty { color: var(--muted); text-align: center; margin-top: 40px; }
.history-empty[hidden] { display: none; }

.scrim {
  position: absolute; inset: 0; z-index: 4; background: rgba(0, 0, 0, .25);
  animation: fade .3s var(--ease);
}
.scrim[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); z-index: 10;
  transform: translate(-50%, 30px); opacity: 0;
  padding: 10px 18px; border-radius: 14px;
  background: var(--text); color: var(--bg); font-weight: 600; font-size: 14px;
  transition: transform .35s var(--spring), opacity .25s;
  pointer-events: none;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

/* ---------- responsive ---------- */
@media (min-width: 720px) {
  .calc.sci-mode { width: 720px; }
  .calc.sci-mode .keys-wrap { flex-direction: row; align-items: stretch; }
  .calc.sci-mode .sci {
    grid-template-columns: repeat(3, 1fr); flex: 0 0 42%; max-height: none; margin-bottom: 0;
    align-content: stretch; grid-auto-rows: 1fr;
  }
  .calc.sci-mode .sci .k { height: auto; min-height: 48px; }
  .calc.sci-mode .keys { flex: 1; }
}

@media (max-width: 420px) {
  body { padding: 0; place-items: stretch; }
  .calc {
    width: 100%; max-height: none; min-height: 100dvh; border-radius: 0; border: 0;
    padding: max(14px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
    justify-content: flex-end;
  }
  .display { flex: 1; }
  :root { --gap: 10px; }
  .k { height: auto; aspect-ratio: 1 / 0.88; }
  .sci .k { aspect-ratio: auto; height: 40px; font-size: 14px; }
}

@media (max-height: 640px) and (max-width: 420px) {
  .k { aspect-ratio: auto; height: 52px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
