:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-raised: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22d3ee;
  --accent-ink: #052e36;
  --danger: #fb7185;
  --border: rgba(148, 163, 184, 0.16);
  --radius: 12px;
  --shadow: 0 12px 32px rgba(2, 6, 23, 0.45), 0 2px 6px rgba(2, 6, 23, 0.3);
  --ease: 180ms ease;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

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

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  background:
    radial-gradient(900px 520px at 85% -10%, rgba(34, 211, 238, 0.11), transparent 60%),
    radial-gradient(700px 420px at -10% 110%, rgba(34, 211, 238, 0.06), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.4;
}

button {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Header and footer ---------- */

.topbar {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 32px;
  height: 32px;
  flex: none;
  filter: drop-shadow(0 4px 12px rgba(34, 211, 238, 0.35));
}

h1 {
  margin: 0;
  font-size: clamp(1.05rem, 4.4vw, 1.4rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  flex: none;
}

.btn {
  min-height: 40px;
  min-width: 40px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition:
    background var(--ease),
    border-color var(--ease),
    color var(--ease),
    transform var(--ease);
}

.btn:hover {
  background: var(--surface-raised);
  border-color: rgba(34, 211, 238, 0.45);
}

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

.btn-lang {
  padding: 0 10px;
  letter-spacing: 0.06em;
}

.footer {
  padding: 18px 16px 22px;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}

.footer a {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 0 6px;
  color: var(--accent);
  text-decoration: none;
}

.footer a:hover {
  text-decoration: underline;
}

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

.layout {
  flex: 1;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 4px 16px 8px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-content: start;
  gap: 16px;
}

.card {
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 6px);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.03), transparent 40%), var(--surface);
  box-shadow: var(--shadow);
}

.calc,
.history {
  width: 100%;
  max-width: 520px;
  justify-self: center;
}

.calc {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ---------- Display ---------- */

.display {
  min-height: 136px;
  padding: 8px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
  border: 1px solid rgba(34, 211, 238, 0.18);
  border-radius: var(--radius);
  background: linear-gradient(180deg, #0a1120 0%, #0d1a30 100%);
  box-shadow:
    inset 0 2px 14px rgba(0, 0, 0, 0.5),
    0 0 0 1px rgba(255, 255, 255, 0.02);
}

.display-top {
  margin-bottom: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
}

.badge {
  flex: none;
  padding: 2px 8px;
  border: 1px solid rgba(34, 211, 238, 0.35);
  border-radius: 999px;
  background: rgba(34, 211, 238, 0.08);
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.ans-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.copy-status {
  margin-left: auto;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
  opacity: 0;
  transition: opacity var(--ease);
}

.copy-status.is-visible {
  opacity: 1;
}

.icon-btn {
  flex: none;
  width: 40px;
  height: 40px;
  display: inline-grid;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition:
    background var(--ease),
    color var(--ease);
}

.icon-btn:hover:not(:disabled) {
  background: rgba(34, 211, 238, 0.1);
  color: var(--accent);
}

.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.expr,
.preview,
.result {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.expr {
  min-height: 1.4em;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  font-size: 1.65rem;
  letter-spacing: 0.01em;
  transition:
    font-size var(--ease),
    color var(--ease);
}

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

.expr.is-empty {
  color: var(--muted);
  font-size: 1.05rem;
  opacity: 0.75;
}

.display:not(.is-evaluated) .expr:not(.is-empty)::after {
  content: "";
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin-left: 3px;
  vertical-align: -0.15em;
  background: var(--accent);
  animation: blink 1.1s steps(1) infinite;
}

.expr mark {
  padding: 0 1px;
  border-radius: 4px;
  background: rgba(251, 113, 133, 0.22);
  color: var(--danger);
}

.is-evaluated .expr {
  color: var(--muted);
  font-size: 1.1rem;
}

.preview {
  color: var(--muted);
  font-size: 1.2rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.preview:empty,
.result:empty {
  display: none;
}

.result {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 2.6rem;
  font-weight: 650;
  line-height: 1.15;
}

.result[data-size="m"] {
  font-size: 2.05rem;
}

.result[data-size="s"] {
  font-size: 1.55rem;
}

.is-evaluated .result {
  color: var(--accent);
  text-shadow: 0 0 26px rgba(34, 211, 238, 0.35);
  animation: pop 220ms ease;
}

.has-error .result {
  color: var(--danger);
  font-size: 1.1rem;
  font-weight: 600;
  white-space: normal;
}

/* ---------- Toolbar ---------- */

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

.angle-toggle {
  min-height: 40px;
  padding: 3px;
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  cursor: pointer;
}

.angle-toggle span {
  padding: 0 14px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  transition:
    background var(--ease),
    color var(--ease);
}

.angle-toggle span.is-active {
  background: var(--accent);
  color: var(--accent-ink);
}

.fn-toggle {
  margin-left: auto;
  font-style: italic;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.05rem;
}

.fn-toggle svg {
  transition: transform var(--ease);
}

.fn-toggle[aria-expanded="true"] {
  border-color: var(--accent);
  color: var(--accent);
}

.fn-toggle[aria-expanded="true"] svg {
  transform: rotate(180deg);
}

.hint {
  display: none;
  margin: 0 0 0 auto;
  color: var(--muted);
  font-size: 0.75rem;
  text-align: right;
}

/* ---------- Keypads ---------- */

.pads {
  display: grid;
  gap: 10px;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  touch-action: manipulation;
}

.sci-pad {
  display: none;
}

.calc.fn-open .sci-pad {
  display: grid;
}

.key {
  min-width: 0;
  min-height: 52px;
  padding: 0 2px;
  border: 1px solid rgba(255, 255, 255, 0.04);
  border-radius: var(--radius);
  background: #2a3a52;
  color: var(--text);
  font-size: 1.25rem;
  font-weight: 550;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 2px 4px rgba(2, 6, 23, 0.35);
  transition:
    background var(--ease),
    color var(--ease),
    box-shadow var(--ease),
    transform 120ms ease;
}

.key:hover {
  background: #3a4c66;
}

.key:active,
.key.is-pressed {
  transform: scale(0.95);
  box-shadow:
    0 0 0 2px rgba(34, 211, 238, 0.55),
    0 0 18px rgba(34, 211, 238, 0.25);
}

.key--digit {
  background: var(--surface-raised);
  color: #f8fafc;
  font-size: 1.35rem;
}

.key--digit:hover {
  background: #44546d;
}

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

.key--fn {
  min-height: 42px;
  background: #18243a;
  color: #cbd5e1;
  font-size: 0.95rem;
}

.key--fn:hover {
  background: #22314b;
  color: var(--accent);
}

.key--command {
  background: #2e2a40;
  color: #fda4af;
  font-size: 1.1rem;
}

.key--command:hover {
  background: #3d3552;
}

.key--equals {
  border-color: transparent;
  background: linear-gradient(180deg, #67e8f9, var(--accent));
  color: var(--accent-ink);
  font-size: 1.55rem;
  font-weight: 700;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    0 6px 18px rgba(34, 211, 238, 0.3);
}

.key--equals:hover {
  background: linear-gradient(180deg, #a5f3fc, var(--accent));
}

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

.history {
  padding: 12px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

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

.history h2 {
  margin: 0;
  font-size: 1rem;
  letter-spacing: 0.02em;
}

.text-btn {
  min-height: 40px;
  padding: 0 10px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
  transition:
    background var(--ease),
    color var(--ease);
}

.text-btn:hover:not(:disabled) {
  background: rgba(251, 113, 133, 0.1);
  color: var(--danger);
}

.text-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.history-empty {
  margin: 0;
  padding: 24px 0;
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
}

.history-list {
  min-height: 0;
  max-height: 340px;
  margin: 0;
  padding: 0 2px 0 0;
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  list-style: none;
  scrollbar-width: thin;
  scrollbar-color: #334155 transparent;
}

.history-item {
  width: 100%;
  min-height: 44px;
  padding: 9px 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: rgba(15, 23, 42, 0.6);
  text-align: right;
  cursor: pointer;
  transition:
    background var(--ease),
    border-color var(--ease);
}

.history-item:hover {
  border-color: rgba(34, 211, 238, 0.4);
  background: rgba(34, 211, 238, 0.07);
}

.h-expr {
  color: var(--muted);
  font-size: 0.85rem;
  overflow-wrap: anywhere;
}

.h-result {
  font-size: 1.15rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.history-list li:first-child .h-result {
  color: var(--accent);
}

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

@media (max-width: 419px) {
  .btn-label {
    display: none;
  }

  #reset-btn {
    padding: 0;
  }
}

@media (min-width: 640px) {
  .sci-pad {
    display: grid;
  }

  .fn-toggle {
    display: none;
  }

  .calc {
    padding: 16px;
    gap: 12px;
  }
}

@media (min-width: 960px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) 320px;
    align-content: center;
    align-items: stretch;
    gap: 24px;
    padding-top: 12px;
  }

  .calc,
  .history {
    max-width: none;
  }

  .calc {
    padding: 18px;
    gap: 14px;
  }

  /* The history card takes the calculator's height and scrolls inside. */
  .history {
    contain: size;
    padding: 16px 16px 18px;
  }

  .history-list {
    max-height: none;
  }

  .display {
    min-height: 150px;
    padding: 10px 18px 14px;
  }

  .result {
    font-size: 3rem;
  }

  .result[data-size="m"] {
    font-size: 2.4rem;
  }

  .hint {
    display: block;
  }

  .pads {
    grid-template-columns: 4fr 5fr;
    gap: 16px;
  }

  .sci-pad {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(5, auto);
    grid-auto-flow: column;
  }

  .key,
  .key--fn {
    min-height: 54px;
  }
}

/* ---------- Motion ---------- */

@keyframes blink {
  50% {
    opacity: 0;
  }
}

@keyframes pop {
  from {
    opacity: 0.4;
    transform: translateY(6px);
  }
}

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