/* Calc — premium scientific calculator
 * Palette: mauve + warm-cream + butter-yellow operators
 * © אופיר ברנס (Ofir Baranes) 2026
 */

* { margin:0; padding:0; box-sizing:border-box; -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; }

:root {
  --bg-1: #1a0d18;
  --bg-2: #241224;
  --bg-3: #2d1530;
  --mauve-300: #d4b3d9;
  --mauve-400: #c084d3;
  --mauve-500: #a06ca6;
  --mauve-600: #855590;
  --mauve-700: #6b4276;
  --mauve-800: #4d2e58;
  --cream: #fef9c3;
  --cream-soft: #fef3c7;
  --cream-light: #fffbeb;
  --gold: #fcd34d;
  --gold-soft: #fde047;
  --bright: #c084fc;
  --text-primary: #fef9c3;
  --text-secondary: rgba(254,249,195,0.72);
  --text-muted: rgba(254,249,195,0.42);
  --surface: rgba(212,179,217,0.05);
  --surface-strong: rgba(212,179,217,0.10);
  --border: rgba(192,132,211,0.16);
  --border-strong: rgba(192,132,211,0.28);
  --shadow-glass: 0 30px 60px -25px rgba(26,13,24,0.7);
  --bezier: cubic-bezier(.34,1.56,.64,1);
  --bezier-soft: cubic-bezier(.4,0,.2,1);
}

html, body {
  background: var(--bg-1);
  color: var(--text-primary);
  font-family: 'Heebo', system-ui, sans-serif;
  font-weight: 400;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

body {
  background:
    radial-gradient(ellipse 700px 800px at 80% -10%, rgba(192,132,211,0.18), transparent 60%),
    radial-gradient(ellipse 500px 600px at -10% 60%, rgba(160,108,166,0.16), transparent 65%),
    radial-gradient(ellipse 800px 1000px at 50% 110%, rgba(77,46,88,0.30), transparent 70%),
    linear-gradient(180deg, #1a0d18 0%, #241224 60%, #2d1530 110%);
  background-attachment: fixed;
  position: relative;
  padding-bottom: 30px;
}

.ambient-bg {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}
.orb-blur {
  position: absolute;
  border-radius: 50%;
  filter: blur(85px);
  opacity: 0.45;
}
.orb-blur.o1 { width: 420px; height: 420px; top: -120px; right: -120px; background: radial-gradient(circle, #c084d3 0%, transparent 70%); animation: drift1 26s ease-in-out infinite; }
.orb-blur.o2 { width: 340px; height: 340px; bottom: 5%; left: -100px; background: radial-gradient(circle, #fcd34d 0%, transparent 70%); opacity: 0.18; animation: drift2 32s ease-in-out infinite; }
.orb-blur.o3 { width: 260px; height: 260px; top: 40%; right: 30%; background: radial-gradient(circle, #855590 0%, transparent 70%); opacity: 0.32; animation: drift3 38s ease-in-out infinite; }
@keyframes drift1 { 0%,100% { transform: translate(0,0) scale(1); } 50% { transform: translate(-30px,40px) scale(1.1); } }
@keyframes drift2 { 0%,100% { transform: translate(0,0) scale(1); } 50% { transform: translate(40px,-30px) scale(1.08); } }
@keyframes drift3 { 0%,100% { transform: translate(0,0) scale(1); } 50% { transform: translate(-25px,-35px) scale(1.12); } }

/* ===== Topbar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: linear-gradient(180deg, rgba(26,13,24,0.85) 0%, rgba(26,13,24,0.55) 100%);
  backdrop-filter: blur(20px) saturate(150%);
  -webkit-backdrop-filter: blur(20px) saturate(150%);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 700;
  color: var(--cream-light);
  letter-spacing: -0.01em;
}
.mark {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--mauve-400), var(--mauve-700));
  color: var(--cream-light);
  box-shadow: 0 0 18px rgba(192,132,211,0.4);
}
.mark svg { width: 18px; height: 18px; }

.header-actions { display: flex; gap: 8px; }
.icon-btn, .install-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  border-radius: 12px;
  padding: 7px 12px;
  cursor: pointer;
  transition: all 0.2s var(--bezier-soft);
  backdrop-filter: blur(10px);
}
.icon-btn { padding: 7px 9px; font-size: 16px; }
.icon-btn:hover, .install-btn:hover {
  background: var(--surface-strong);
  border-color: var(--border-strong);
  transform: translateY(-1px);
}
.install-btn {
  background: linear-gradient(135deg, var(--mauve-400), var(--mauve-700));
  border: none;
  color: var(--cream-light);
  font-weight: 600;
}

main {
  position: relative;
  z-index: 1;
  padding: 16px 16px 20px;
  max-width: 480px;
  margin: 0 auto;
}
.hidden { display: none !important; }

/* ===== Display panel ===== */
.display-panel {
  background: linear-gradient(160deg, rgba(212,179,217,0.10) 0%, rgba(212,179,217,0.04) 100%);
  backdrop-filter: blur(22px) saturate(180%);
  border: 1px solid var(--border-strong);
  border-radius: 24px;
  padding: 22px 22px 16px;
  margin-bottom: 16px;
  position: relative;
  overflow: hidden;
  min-height: 170px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
.display-panel::before {
  content: '';
  position: absolute;
  inset: -1px;
  border-radius: 24px;
  padding: 1.5px;
  background: linear-gradient(135deg, rgba(192,132,211,0.4) 0%, rgba(252,211,77,0.18) 50%, transparent 80%);
  -webkit-mask: linear-gradient(#000,#000) content-box, linear-gradient(#000,#000);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  pointer-events: none;
}

.memory-bar {
  position: absolute;
  top: 14px;
  right: 18px;
  display: flex;
  gap: 6px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  color: var(--mauve-300);
  letter-spacing: 0.05em;
}
.memory-bar .mb-pill {
  padding: 3px 8px;
  background: rgba(192,132,211,0.12);
  border: 1px solid rgba(192,132,211,0.25);
  border-radius: 100px;
}

.d-expression {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 14px;
  color: var(--text-muted);
  text-align: left;
  direction: ltr;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  margin-bottom: 8px;
  min-height: 18px;
}
.d-expression::-webkit-scrollbar { display: none; }

.d-result {
  font-family: 'IBM Plex Mono', monospace;
  font-size: clamp(38px, 11vw, 58px);
  font-weight: 300;
  color: var(--cream-light);
  text-align: left;
  direction: ltr;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.025em;
  line-height: 1;
  text-shadow: 0 0 30px rgba(254,243,199,0.15);
  word-break: break-all;
  overflow-wrap: break-word;
}
.d-result.error {
  color: #fb7185;
  text-shadow: 0 0 30px rgba(251,113,133,0.4);
}

.d-aux {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed rgba(192,132,211,0.18);
}
.d-mode {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--mauve-300);
}
.d-copy {
  background: rgba(212,179,217,0.06);
  border: 1px solid var(--border);
  color: var(--mauve-300);
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 100px;
  cursor: pointer;
  transition: all 0.2s;
}
.d-copy:hover { background: rgba(212,179,217,0.12); color: var(--cream-light); }
.d-copy.copied { background: rgba(134,239,172,0.18); color: #86efac; border-color: rgba(134,239,172,0.35); }

/* ===== Mode tabs ===== */
.mode-tabs {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 4px;
  margin-bottom: 14px;
}
.mode-tab {
  flex: 1;
  padding: 9px 6px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 10px;
  transition: all 0.25s var(--bezier);
}
.mode-tab:hover { color: var(--cream-light); }
.mode-tab.active {
  background: linear-gradient(135deg, var(--mauve-500), var(--mauve-700));
  color: var(--cream-light);
  font-weight: 700;
  box-shadow: 0 6px 14px rgba(133,85,144,0.35);
}

/* ===== Keypads ===== */
.keypad {
  display: grid;
  gap: 8px;
}
.keypad.basic {
  grid-template-columns: repeat(4, 1fr);
}
.keypad.sci {
  grid-template-columns: repeat(5, 1fr);
}
.keypad.mem {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.key {
  position: relative;
  padding: 0;
  height: 60px;
  border: 1px solid var(--border);
  border-radius: 16px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 22px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.12s var(--bezier);
  display: grid;
  place-items: center;
  font-variant-numeric: tabular-nums;
  background: linear-gradient(180deg, rgba(212,179,217,0.06), rgba(212,179,217,0.02));
  color: var(--cream-light);
  box-shadow: 0 4px 12px rgba(0,0,0,0.18), inset 0 1px 0 rgba(254,249,195,0.08);
}
.key:hover {
  background: linear-gradient(180deg, rgba(212,179,217,0.12), rgba(212,179,217,0.04));
  border-color: var(--border-strong);
  transform: translateY(-1px);
}
.key:active, .key.pressed {
  transform: scale(0.94);
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.key.pressed::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 16px;
  background: rgba(254,249,195,0.15);
  animation: key-flash 0.3s ease-out forwards;
}
@keyframes key-flash {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

.key.num {
  background: linear-gradient(180deg, rgba(212,179,217,0.10), rgba(212,179,217,0.03));
  color: var(--cream-light);
  font-weight: 400;
}

.key.fn {
  background: linear-gradient(180deg, rgba(212,179,217,0.04), rgba(212,179,217,0));
  color: var(--mauve-300);
  font-size: 18px;
  font-weight: 600;
}
.key.fn svg { width: 18px; height: 18px; }

.key.op {
  background: linear-gradient(180deg, rgba(252,211,77,0.18), rgba(252,211,77,0.06));
  color: var(--gold);
  font-weight: 700;
  font-size: 26px;
  border-color: rgba(252,211,77,0.25);
  box-shadow: 0 4px 12px rgba(252,211,77,0.10), inset 0 1px 0 rgba(254,243,199,0.18);
}
.key.op:hover {
  background: linear-gradient(180deg, rgba(252,211,77,0.28), rgba(252,211,77,0.10));
  border-color: rgba(252,211,77,0.5);
}
.key.op.active {
  background: linear-gradient(180deg, var(--gold), #ca8a04);
  color: var(--bg-1);
  border-color: var(--gold);
  box-shadow: 0 0 20px rgba(252,211,77,0.4);
}

.key.eq {
  background: linear-gradient(180deg, var(--mauve-500), var(--mauve-800));
  color: var(--cream-light);
  font-weight: 800;
  font-size: 28px;
  border: none;
  box-shadow: 0 6px 16px rgba(133,85,144,0.45), inset 0 1px 0 rgba(254,249,195,0.18);
}
.key.eq:hover {
  background: linear-gradient(180deg, var(--mauve-400), var(--mauve-700));
  box-shadow: 0 10px 22px rgba(133,85,144,0.6), inset 0 1px 0 rgba(254,249,195,0.22);
}

.key.sci {
  background: linear-gradient(180deg, rgba(254,249,195,0.04), rgba(254,249,195,0.01));
  color: var(--cream-soft);
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 0.02em;
}
.key.sci:hover {
  background: linear-gradient(180deg, rgba(254,249,195,0.08), rgba(254,249,195,0.02));
  color: var(--cream-light);
}

.keypad.sci .key { height: 52px; font-size: 18px; }
.keypad.sci .key.sci { font-size: 13px; }
.keypad.sci .key.fn { font-size: 16px; }

/* ===== Memory mode ===== */
.mem-header h3 {
  font-family: 'Frank Ruhl Libre', 'Heebo', serif;
  font-size: 22px;
  font-weight: 700;
  color: var(--cream-light);
  margin-bottom: 6px;
}
.mem-header p {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 4px;
}
.mem-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.mem-slot {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 14px 16px;
  cursor: pointer;
  transition: all 0.25s var(--bezier);
  text-align: right;
  font-family: inherit;
}
.mem-slot:hover { background: var(--surface-strong); border-color: var(--border-strong); }
.mem-slot.active {
  background: linear-gradient(140deg, rgba(192,132,211,0.18), rgba(192,132,211,0.04));
  border-color: var(--mauve-400);
  box-shadow: 0 0 18px rgba(192,132,211,0.25);
}
.mem-slot .ms-label {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--mauve-300);
  margin-bottom: 4px;
}
.mem-slot .ms-val {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 18px;
  font-weight: 500;
  color: var(--cream-light);
  font-variant-numeric: tabular-nums;
  direction: ltr;
  text-align: left;
}
.mem-slot.empty .ms-val { color: var(--text-muted); font-weight: 300; }

.mem-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.mem-actions .key { height: 52px; font-size: 14px; }

.mem-hint {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  font-family: 'IBM Plex Mono', monospace;
}

/* ===== Modal ===== */
.modal {
  position: fixed; inset: 0;
  background: rgba(15,8,15,0.7);
  backdrop-filter: blur(8px);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: modal-fade 0.25s var(--bezier-soft);
}
@keyframes modal-fade { from { opacity: 0; } to { opacity: 1; } }
.modal-card {
  width: 100%;
  max-width: 540px;
  background: linear-gradient(180deg, #2d1530 0%, #1a0d18 100%);
  border: 1px solid var(--border-strong);
  border-bottom: none;
  border-radius: 28px 28px 0 0;
  padding: 20px 22px 32px;
  padding-bottom: max(32px, env(safe-area-inset-bottom));
  animation: modal-slide 0.4s var(--bezier);
  max-height: 88vh;
  overflow-y: auto;
}
@keyframes modal-slide { from { transform: translateY(100%); } to { transform: translateY(0); } }
.handle {
  width: 38px; height: 4px;
  background: var(--border-strong);
  border-radius: 4px;
  margin: 0 auto 16px;
}
.modal-card h3 {
  font-family: 'Frank Ruhl Libre', 'Heebo', serif;
  color: var(--cream-light);
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 14px;
}
.modal-card p {
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.65;
  margin-bottom: 14px;
}
.mark-big {
  width: 64px; height: 64px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--mauve-400), var(--mauve-700));
  border-radius: 18px;
  color: var(--cream-light);
  margin: 4px auto 14px;
  box-shadow: 0 0 30px rgba(192,132,211,0.4);
}
.mark-big svg { width: 32px; height: 32px; }

/* Tape */
.tape-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.tape-header h3 { margin-bottom: 0; }
.tape-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 60vh;
  overflow-y: auto;
}
.tape-list::-webkit-scrollbar { width: 4px; }
.tape-list::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }

.tape-row {
  background: rgba(212,179,217,0.04);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
  transition: all 0.2s;
}
.tape-row:hover { background: rgba(192,132,211,0.10); border-color: var(--border-strong); }
.tape-expr {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  color: var(--text-muted);
  direction: ltr;
  text-align: left;
  margin-bottom: 4px;
  word-break: break-all;
}
.tape-result {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 18px;
  font-weight: 500;
  color: var(--cream-light);
  font-variant-numeric: tabular-nums;
  direction: ltr;
  text-align: left;
  word-break: break-all;
}
.tape-empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 13px;
}

.btn-primary, .btn-secondary, .btn-mini {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 13px 18px;
  border-radius: 14px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.25s var(--bezier);
  border: none;
}
.btn-primary {
  background: linear-gradient(135deg, var(--mauve-500), var(--mauve-800));
  color: var(--cream-light);
  font-weight: 700;
  box-shadow: 0 8px 24px rgba(133,85,144,0.4);
}
.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 12px 30px rgba(133,85,144,0.55); }
.btn-secondary {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--cream-light);
}
.btn-mini {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  flex: 0 1 auto;
  padding: 9px 14px;
  font-size: 12px;
}
.btn-mini:hover { background: var(--surface-strong); color: var(--cream-light); }
