/* ==========================================================================
   CalcVerse — colorful scientific calculator
   Theme tokens, layout, calculator, graph, solver & matrix panels
   ========================================================================== */

/* ------------------------------------------------------------------ *
 *  Tokens
 * ------------------------------------------------------------------ */

:root {
  --bg: #0b0a1f;
  --bg-soft: #120f2b;
  --card: rgba(255, 255, 255, 0.06);
  --card-solid: #14122e;
  --inner: rgba(255, 255, 255, 0.045);
  --stroke: rgba(255, 255, 255, 0.11);
  --stroke-strong: rgba(255, 255, 255, 0.2);
  --text: #f3f2ff;
  --muted: #a29ecb;
  --grid: rgba(140, 140, 205, 0.16);
  --axis: rgba(170, 170, 235, 0.45);
  --pink: #ff2d95;
  --purple: #7c4dff;
  --cyan: #00d4ff;
  --green: #22e5a3;
  --amber: #ffb020;
  --red: #ff5c7a;
  --grad: linear-gradient(135deg, #ff2d95 0%, #7c4dff 55%, #00d4ff 100%);
  --grad-soft: linear-gradient(135deg, rgba(255, 45, 149, 0.18), rgba(124, 77, 255, 0.18) 55%, rgba(0, 212, 255, 0.16));
  --shadow: 0 24px 60px rgba(5, 2, 30, 0.55);
  --ring: 0 0 0 3px rgba(124, 77, 255, 0.35);
  --radius-lg: 28px;
  --radius-md: 18px;
  --radius-sm: 12px;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #f3f1ff;
  --bg-soft: #e9e6ff;
  --card: rgba(255, 255, 255, 0.72);
  --card-solid: #ffffff;
  --inner: rgba(84, 66, 160, 0.06);
  --stroke: rgba(60, 44, 130, 0.14);
  --stroke-strong: rgba(60, 44, 130, 0.26);
  --text: #1c1838;
  --muted: #6b6594;
  --grid: rgba(80, 70, 150, 0.16);
  --axis: rgba(70, 58, 140, 0.5);
  --shadow: 0 22px 50px rgba(70, 50, 150, 0.16);
  --ring: 0 0 0 3px rgba(124, 77, 255, 0.25);
  color-scheme: light;
}

/* ------------------------------------------------------------------ *
 *  Base
 * ------------------------------------------------------------------ */

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

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

body {
  margin: 0;
  min-height: 100dvh;
  font-family: "Segoe UI", "Cairo", ui-sans-serif, system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1100px 620px at 8% -12%, rgba(124, 77, 255, 0.3), transparent 62%),
    radial-gradient(900px 520px at 108% 4%, rgba(0, 212, 255, 0.18), transparent 60%),
    radial-gradient(800px 500px at 50% 120%, rgba(255, 45, 149, 0.12), transparent 65%),
    var(--bg);
  overflow-x: hidden;
}

[data-theme="light"] body {
  background:
    radial-gradient(1100px 620px at 8% -12%, rgba(124, 77, 255, 0.18), transparent 62%),
    radial-gradient(900px 520px at 108% 4%, rgba(0, 212, 255, 0.16), transparent 60%),
    radial-gradient(800px 500px at 50% 120%, rgba(255, 45, 149, 0.1), transparent 65%),
    var(--bg);
}

button,
input {
  font-family: inherit;
}

button {
  cursor: pointer;
}

code {
  font-family: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.92em;
  background: var(--inner);
  border: 1px solid var(--stroke);
  border-radius: 6px;
  padding: 1px 5px;
  direction: ltr;
  display: inline-block;
}

[hidden] {
  display: none !important;
}

:focus-visible {
  outline: none;
  box-shadow: var(--ring);
}

/* ------------------------------------------------------------------ *
 *  Background blobs
 * ------------------------------------------------------------------ */

.bg-blobs {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}

.blob {
  position: absolute;
  border-radius: 50%;
  filter: blur(70px);
  opacity: 0.55;
  animation: float 16s ease-in-out infinite alternate;
}

.blob.b1 {
  width: 46vmax;
  height: 46vmax;
  top: -18vmax;
  right: -12vmax;
  background: radial-gradient(circle, rgba(255, 45, 149, 0.55), transparent 65%);
}

.blob.b2 {
  width: 40vmax;
  height: 40vmax;
  bottom: -16vmax;
  left: -12vmax;
  background: radial-gradient(circle, rgba(0, 212, 255, 0.5), transparent 65%);
  animation-delay: -5s;
}

.blob.b3 {
  width: 34vmax;
  height: 34vmax;
  top: 32%;
  left: 42%;
  background: radial-gradient(circle, rgba(124, 77, 255, 0.55), transparent 65%);
  animation-delay: -9s;
}

.blob.b4 {
  width: 22vmax;
  height: 22vmax;
  bottom: 18%;
  right: 8%;
  background: radial-gradient(circle, rgba(34, 229, 163, 0.35), transparent 65%);
  animation-delay: -3s;
}

@keyframes float {
  from {
    transform: translate3d(0, 0, 0) scale(1);
  }
  to {
    transform: translate3d(-4vmax, 5vmax, 0) scale(1.12);
  }
}

/* ------------------------------------------------------------------ *
 *  Layout
 * ------------------------------------------------------------------ */

.stage {
  position: relative;
  z-index: 1;
  width: min(100% - 28px, 780px);
  margin: 0 auto;
  padding: 26px 0 56px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.logo {
  width: 54px;
  height: 54px;
  flex: 0 0 54px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-size: 27px;
  font-weight: 800;
  color: #fff;
  background: var(--grad);
  box-shadow: 0 12px 30px rgba(124, 77, 255, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

.brand-text h1 {
  margin: 0;
  font-size: 22px;
  letter-spacing: 0.4px;
}

.brand-text p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12.5px;
}

.top-actions {
  margin-inline-start: auto;
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--stroke);
  background: var(--card);
  color: var(--text);
  font-size: 17px;
  display: grid;
  place-items: center;
  backdrop-filter: blur(12px);
  transition: transform 0.15s ease, border-color 0.2s ease, background 0.2s ease;
}

.icon-btn:hover {
  border-color: var(--stroke-strong);
  background: var(--inner);
}

.icon-btn:active {
  transform: scale(0.92);
}

.icon-btn.small {
  width: 32px;
  height: 32px;
  font-size: 14px;
}

.icon-btn.off {
  opacity: 0.5;
}

/* ------------------------------------------------------------------ *
 *  Tabs
 * ------------------------------------------------------------------ */

.tabs {
  display: flex;
  gap: 6px;
  padding: 6px;
  border-radius: 999px;
  border: 1px solid var(--stroke);
  background: var(--card);
  backdrop-filter: blur(14px);
  overflow-x: auto;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab-btn {
  flex: 1 0 auto;
  border: none;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  font-size: 13.5px;
  padding: 11px 14px;
  border-radius: 999px;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  transition: color 0.2s ease, background 0.2s ease, transform 0.15s ease;
}

.tab-btn:hover {
  color: var(--text);
}

.tab-btn.active {
  color: #fff;
  background: var(--grad);
  box-shadow: 0 10px 24px rgba(124, 77, 255, 0.4);
}

.tab-btn:active {
  transform: scale(0.97);
}

/* ------------------------------------------------------------------ *
 *  Panels & cards
 * ------------------------------------------------------------------ */

.panel {
  display: none;
}

.panel.active {
  display: block;
  animation: panelIn 0.35s ease both;
}

@keyframes panelIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.card {
  border: 1px solid var(--stroke);
  border-radius: var(--radius-lg);
  background: var(--card);
  backdrop-filter: blur(18px);
  box-shadow: var(--shadow);
  padding: 18px;
}

.card-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.card-head h2 {
  margin: 0;
  font-size: 17px;
}

.card-sub {
  color: var(--muted);
  font-size: 12px;
}

.card-sub code {
  font-size: 11px;
}

/* ------------------------------------------------------------------ *
 *  Calculator
 * ------------------------------------------------------------------ */

.calculator {
  max-width: 540px;
  margin: 0 auto;
  padding: 16px;
  border: 1px solid var(--stroke);
  border-radius: var(--radius-lg);
  background: var(--card);
  backdrop-filter: blur(18px);
  box-shadow: var(--shadow);
}

.display {
  border-radius: 22px;
  border: 1px solid var(--stroke);
  background:
    linear-gradient(160deg, rgba(124, 77, 255, 0.12), transparent 55%),
    var(--inner);
  padding: 12px 16px 14px;
  margin-bottom: 14px;
}

.display-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 22px;
}

.chip {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.5px;
  padding: 2px 9px;
  border-radius: 999px;
  border: 1px solid var(--stroke);
  color: var(--muted);
  background: var(--inner);
}

.chip.accent {
  color: #fff;
  border: none;
  background: linear-gradient(135deg, var(--purple), var(--cyan));
}

.chip.mem {
  color: #0b0a1f;
  background: linear-gradient(135deg, var(--green), var(--cyan));
  border: none;
}

.expression-wrap {
  overflow: hidden;
}

.expression {
  direction: ltr;
  text-align: right;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  font-size: clamp(28px, 7.4vw, 40px);
  font-weight: 800;
  line-height: 1.25;
  min-height: 50px;
  padding-bottom: 2px;
  letter-spacing: 0.2px;
}

.expression::-webkit-scrollbar {
  display: none;
}

.preview {
  direction: ltr;
  text-align: right;
  min-height: 22px;
  margin-top: 4px;
  font-size: 15px;
  font-weight: 600;
  color: var(--cyan);
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}

.preview.error {
  color: var(--red);
}

[data-theme="light"] .preview {
  color: #0b8fb8;
}

.memory-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 12px;
}

.memory-row button {
  height: 36px;
  border-radius: 11px;
  border: 1px solid var(--stroke);
  background: var(--inner);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.4px;
  position: relative;
  overflow: hidden;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.memory-row button:hover {
  color: var(--text);
  border-color: var(--stroke-strong);
}

.keypad {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sci-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.sci-grid .key {
  height: 48px;
}

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

.key {
  position: relative;
  overflow: hidden;
  border-radius: 16px;
  border: 1px solid var(--stroke);
  background: var(--inner);
  color: var(--text);
  font-weight: 700;
  font-size: 18px;
  height: 56px;
  display: grid;
  place-items: center;
  transition: transform 0.08s ease, background 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.key:hover {
  border-color: var(--stroke-strong);
  background: rgba(124, 77, 255, 0.12);
}

.key:active {
  transform: scale(0.95);
}

.key.num {
  font-size: 21px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.07), rgba(255, 255, 255, 0.02));
}

.key.fn {
  font-size: 13.5px;
  font-weight: 800;
  color: var(--muted);
  letter-spacing: 0.2px;
}

.key.fn:hover {
  color: var(--text);
}

.key.mode {
  border-style: dashed;
  color: var(--cyan);
}

.key.op {
  color: var(--purple);
  font-size: 22px;
  background: linear-gradient(180deg, rgba(124, 77, 255, 0.16), rgba(124, 77, 255, 0.05));
}

[data-theme="light"] .key.op {
  color: #5b32d6;
}

.key.eq {
  color: #fff;
  border: none;
  background: var(--grad);
  font-size: 24px;
  box-shadow: 0 10px 24px rgba(124, 77, 255, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.35);
}

.key.danger {
  color: var(--red);
  background: linear-gradient(180deg, rgba(255, 92, 122, 0.16), rgba(255, 92, 122, 0.05));
}

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

.hint {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  margin: 14px 0 0;
  line-height: 1.9;
}

.hint.small {
  margin-top: 12px;
  font-size: 11.5px;
}

.hint kbd {
  font-family: inherit;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 6px;
  border: 1px solid var(--stroke);
  background: var(--inner);
  color: var(--text);
}

/* ------------------------------------------------------------------ *
 *  Graph
 * ------------------------------------------------------------------ */

.func-list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.func-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 14px;
  border: 1px solid var(--stroke);
  background: var(--inner);
}

.swatch {
  width: 20px;
  height: 20px;
  flex: 0 0 20px;
  border-radius: 50%;
  border: none;
  box-shadow: 0 0 0 3px var(--inner);
  transition: transform 0.15s ease;
}

.swatch:hover {
  transform: scale(1.12);
}

.func-input {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  font-family: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
  font-size: 14px;
  padding: 8px 10px;
  border-radius: 10px;
  direction: ltr;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.func-input:focus {
  outline: none;
  border-color: rgba(124, 77, 255, 0.6);
  background: var(--card);
  box-shadow: none;
}

.func-input.invalid {
  border-color: var(--red);
  background: rgba(255, 92, 122, 0.08);
}

.func-actions {
  display: flex;
  gap: 4px;
}

.func-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 10px;
  text-align: center;
  border: 1px dashed var(--stroke);
  border-radius: 12px;
}

.func-add-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.ghost-btn {
  border: 1px solid var(--stroke);
  background: var(--inner);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  padding: 9px 14px;
  border-radius: 999px;
  position: relative;
  overflow: hidden;
  transition: border-color 0.18s ease, background 0.18s ease, transform 0.12s ease;
}

.ghost-btn:hover {
  border-color: var(--stroke-strong);
  background: rgba(124, 77, 255, 0.14);
}

.ghost-btn:active {
  transform: scale(0.96);
}

.quick-funcs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.quick-btn {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--stroke);
  background: transparent;
  color: var(--muted);
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 11.5px;
  padding: 7px 10px;
  border-radius: 999px;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.quick-btn:hover {
  color: var(--text);
  border-color: rgba(0, 212, 255, 0.55);
  background: rgba(0, 212, 255, 0.1);
}

.canvas-wrap {
  position: relative;
  margin-top: 12px;
  height: clamp(300px, 46vh, 470px);
  border-radius: var(--radius-md);
  border: 1px solid var(--stroke);
  background:
    linear-gradient(160deg, rgba(124, 77, 255, 0.1), transparent 55%),
    var(--inner);
  overflow: hidden;
  touch-action: none;
}

.canvas-wrap canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.coord-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(12, 9, 34, 0.78);
  color: #fff;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px;
  direction: ltr;
  backdrop-filter: blur(8px);
  pointer-events: none;
  white-space: nowrap;
}

.graph-controls {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.gbtn {
  position: relative;
  overflow: hidden;
  height: 44px;
  min-width: 46px;
  padding: 0 14px;
  border-radius: 13px;
  border: 1px solid var(--stroke);
  background: var(--inner);
  color: var(--text);
  font-size: 20px;
  font-weight: 700;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.gbtn:hover {
  background: rgba(124, 77, 255, 0.14);
  border-color: var(--stroke-strong);
}

.gbtn:active {
  transform: scale(0.95);
}

.gbtn.wide {
  flex: 1;
  font-size: 14px;
}

/* ------------------------------------------------------------------ *
 *  Solver
 * ------------------------------------------------------------------ */

.solver-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}

.st-btn {
  position: relative;
  overflow: hidden;
  flex: 1 1 auto;
  padding: 10px 12px;
  border-radius: 999px;
  border: 1px solid var(--stroke);
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  transition: all 0.18s ease;
}

.st-btn:hover {
  color: var(--text);
  border-color: var(--stroke-strong);
}

.st-btn.active {
  color: #fff;
  border-color: transparent;
  background: var(--grad);
  box-shadow: 0 8px 20px rgba(124, 77, 255, 0.35);
}

.solver-pane {
  display: none;
}

.solver-pane.active {
  display: block;
  animation: panelIn 0.25s ease both;
}

.pane-hint {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 12.5px;
}

.field-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}

.field {
  flex: 1 1 80px;
  min-width: 78px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field span {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  padding-inline-start: 4px;
  direction: ltr;
  text-align: right;
}

.field input {
  width: 100%;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--stroke);
  background: var(--inner);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  direction: ltr;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.field input:focus {
  outline: none;
  border-color: rgba(124, 77, 255, 0.65);
  background: var(--card);
  box-shadow: none;
}

.field input.invalid {
  border-color: var(--red);
  background: rgba(255, 92, 122, 0.08);
}

.solve-btn {
  position: relative;
  overflow: hidden;
  width: 100%;
  margin-top: 4px;
  padding: 13px 16px;
  border: none;
  border-radius: 14px;
  background: var(--grad);
  color: #fff;
  font-size: 15px;
  font-weight: 800;
  box-shadow: 0 10px 26px rgba(124, 77, 255, 0.38), inset 0 1px 0 rgba(255, 255, 255, 0.3);
  transition: transform 0.12s ease, filter 0.15s ease;
}

.solve-btn:hover {
  filter: brightness(1.06);
}

.solve-btn:active {
  transform: scale(0.985);
}

.result-card {
  display: none;
  margin-top: 14px;
  padding: 14px 16px;
  border-radius: 16px;
  border: 1px solid var(--stroke);
  background: var(--inner);
}

.result-card.show {
  display: block;
  animation: panelIn 0.3s ease both;
}

.result-card.ok {
  border-color: rgba(34, 229, 163, 0.35);
}

.result-title {
  font-size: 14px;
  font-weight: 800;
  margin-bottom: 8px;
}

.result-body {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.res-line {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.8;
  color: var(--muted);
}

.res-big {
  margin: 0;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: clamp(17px, 4.2vw, 23px);
  font-weight: 800;
  color: var(--cyan);
  direction: ltr;
  text-align: left;
  word-break: break-word;
}

[data-theme="light"] .res-big {
  color: #0b8fb8;
}

/* ------------------------------------------------------------------ *
 *  Matrix
 * ------------------------------------------------------------------ */

.matrix-size {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}

.size-btn {
  position: relative;
  overflow: hidden;
  padding: 9px 16px;
  border-radius: 999px;
  border: 1px solid var(--stroke);
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  transition: all 0.18s ease;
}

.size-btn.active {
  color: #fff;
  border-color: transparent;
  background: var(--grad);
  box-shadow: 0 8px 20px rgba(124, 77, 255, 0.32);
}

.matrices-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.matrix-box {
  padding: 12px;
  border-radius: 16px;
  border: 1px solid var(--stroke);
  background: var(--inner);
}

.matrix-box h3 {
  margin: 0 0 10px;
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 700;
  letter-spacing: 0.3px;
}

.mgrid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr));
  gap: 6px;
}

.mcell {
  width: 100%;
  min-width: 0;
  padding: 9px 4px;
  border-radius: 9px;
  border: 1px solid var(--stroke);
  background: var(--card);
  color: var(--text);
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 13.5px;
  text-align: center;
  direction: ltr;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.mcell:focus {
  outline: none;
  border-color: rgba(124, 77, 255, 0.65);
  background: var(--card-solid);
  box-shadow: none;
}

.mcell.invalid {
  border-color: var(--red);
  background: rgba(255, 92, 122, 0.08);
}

.matrix-ops {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.mop-btn {
  position: relative;
  overflow: hidden;
  padding: 9px 13px;
  border-radius: 999px;
  border: 1px solid var(--stroke);
  background: var(--inner);
  color: var(--text);
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12.5px;
  font-weight: 700;
  transition: all 0.16s ease;
}

.mop-btn:hover {
  border-color: rgba(124, 77, 255, 0.6);
  background: rgba(124, 77, 255, 0.14);
  transform: translateY(-1px);
}

.mop-btn:active {
  transform: scale(0.96);
}

.mres-grid {
  display: inline-grid;
  grid-template-columns: repeat(var(--cols, 2), auto);
  gap: 6px 16px;
  padding: 8px 14px;
  border-inline-start: 2px solid var(--cyan);
  border-inline-end: 2px solid var(--cyan);
  border-radius: 6px;
  background: rgba(0, 212, 255, 0.06);
}

.mres-cell {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 14px;
  font-weight: 700;
  text-align: center;
  direction: ltr;
  color: var(--text);
  white-space: nowrap;
}

/* ------------------------------------------------------------------ *
 *  History drawer
 * ------------------------------------------------------------------ */

.history-drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(370px, 88vw);
  z-index: 60;
  display: flex;
  flex-direction: column;
  padding: 18px;
  border-inline-end: 1px solid var(--stroke);
  background: var(--card-solid);
  box-shadow: 20px 0 60px rgba(0, 0, 0, 0.45);
  transform: translateX(-105%);
  transition: transform 0.32s cubic-bezier(0.4, 0.0, 0.2, 1);
}

.history-drawer.open {
  transform: none;
}

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

.history-head h2 {
  margin: 0;
  font-size: 18px;
}

.history-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.history-list {
  list-style: none;
  margin: 14px 0 0;
  padding: 0;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overscroll-behavior: contain;
}

.history-item {
  padding: 11px 13px;
  border-radius: 13px;
  border: 1px solid var(--stroke);
  background: var(--inner);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 5px;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.12s ease;
}

.history-item:hover {
  border-color: rgba(124, 77, 255, 0.55);
  background: rgba(124, 77, 255, 0.1);
  transform: translateX(-2px);
}

.h-expr {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12.5px;
  color: var(--muted);
  direction: ltr;
  text-align: left;
  word-break: break-all;
}

.h-res {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 15px;
  font-weight: 800;
  direction: ltr;
  text-align: left;
}

.history-empty {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  line-height: 1.8;
  margin-top: 30px;
}

.scrim {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(4, 2, 20, 0.55);
  opacity: 0;
  transition: opacity 0.28s ease;
  backdrop-filter: blur(2px);
}

.scrim[hidden] {
  display: none !important;
}

.scrim.show {
  opacity: 1;
}

/* ------------------------------------------------------------------ *
 *  Toast & ripple
 * ------------------------------------------------------------------ */

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  z-index: 80;
  max-width: min(88vw, 420px);
  padding: 11px 20px;
  border-radius: 999px;
  border: 1px solid var(--stroke-strong);
  background: var(--card-solid);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 14px);
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.ripple {
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: rgba(150, 120, 255, 0.4);
  transform: translate(-50%, -50%) scale(0);
  pointer-events: none;
  animation: ripple 0.6s ease-out forwards;
}

@keyframes ripple {
  to {
    transform: translate(-50%, -50%) scale(14);
    opacity: 0;
  }
}

/* ------------------------------------------------------------------ *
 *  Error shake
 * ------------------------------------------------------------------ */

.shake {
  animation: shake 0.42s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

@keyframes shake {
  10%,
  90% {
    transform: translateX(-1px);
  }
  20%,
  80% {
    transform: translateX(2px);
  }
  30%,
  50%,
  70% {
    transform: translateX(-4px);
  }
  40%,
  60% {
    transform: translateX(4px);
  }
}

/* ------------------------------------------------------------------ *
 *  Responsive
 * ------------------------------------------------------------------ */

@media (max-width: 560px) {
  .stage {
    width: min(100% - 20px, 780px);
    padding: 16px 0 40px;
    gap: 12px;
  }

  .logo {
    width: 46px;
    height: 46px;
    flex-basis: 46px;
    border-radius: 15px;
    font-size: 23px;
  }

  .brand-text h1 {
    font-size: 19px;
  }

  .brand-text p {
    font-size: 11.5px;
  }

  .icon-btn {
    width: 38px;
    height: 38px;
    font-size: 15px;
  }

  .tab-btn {
    font-size: 12px;
    padding: 9px 11px;
  }

  .calculator {
    padding: 12px;
    border-radius: 24px;
  }

  .display {
    padding: 10px 12px 12px;
  }

  .expression {
    min-height: 42px;
  }

  .sci-grid,
  .main-grid {
    gap: 6px;
  }

  .key {
    height: 47px;
    border-radius: 14px;
    font-size: 16px;
  }

  .sci-grid .key {
    height: 43px;
  }

  .key.num {
    font-size: 18px;
  }

  .key.fn {
    font-size: 11.5px;
  }

  .key.eq {
    font-size: 20px;
  }

  .card {
    padding: 14px;
    border-radius: 22px;
  }

  .memory-row button {
    height: 33px;
    font-size: 11px;
  }

  .func-input,
  .mcell,
  .field input {
    font-size: 16px;
  }

  .history-drawer {
    padding: 14px;
  }
}

@media (max-width: 380px) {
  .key {
    height: 42px;
  }

  .key.fn {
    font-size: 10px;
    letter-spacing: 0;
  }

  .field {
    flex-basis: 64px;
  }
}

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