/* MathTools — Scientific calculator console */

.shell-calc {
  width: min(1080px, calc(100% - 2rem));
}

.no-js-message {
  display: none;
  margin: 0 0 1.25rem;
  padding: 1rem 1.1rem;
  background: var(--danger-soft);
  border: 1px solid color-mix(in srgb, var(--danger) 35%, var(--border));
  border-radius: var(--radius);
  color: var(--danger);
  font-weight: 500;
}

.calc-workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(220px, 0.7fr);
  gap: 1rem;
  align-items: start;
}

.calc-console {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(0.9rem, 2.4vw, 1.25rem);
}

.calc-readout {
  position: relative;
  background:
    linear-gradient(165deg, #1e2833 0%, #151c24 55%, #12181f 100%);
  color: #e8eef3;
  border: 1px solid #0d1218;
  border-radius: 8px;
  padding: 0.85rem 1rem 1rem;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    inset 0 -1px 0 rgba(0, 0, 0, 0.35);
  min-height: 7.25rem;
  overflow: hidden;
}

.calc-readout::after {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(255, 255, 255, 0.015) 2px,
    rgba(255, 255, 255, 0.015) 3px
  );
  pointer-events: none;
}

.readout-meta {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin-bottom: 0.35rem;
  position: relative;
  z-index: 1;
}

.angle-badge,
.memory-badge {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 0.2rem 0.45rem;
  border-radius: 3px;
}

.angle-badge {
  color: #7fe3d2;
  background: rgba(13, 159, 138, 0.18);
  border: 1px solid rgba(127, 227, 210, 0.35);
}

.memory-badge {
  color: #d7e2ea;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(215, 226, 234, 0.25);
}

.calc-expression {
  position: relative;
  z-index: 1;
  min-height: 1.35rem;
  font-family: var(--font-mono);
  font-size: 0.92rem;
  font-weight: 400;
  color: rgba(232, 238, 243, 0.62);
  text-align: right;
  word-break: break-all;
  line-height: 1.35;
}

.calc-result {
  position: relative;
  z-index: 1;
  margin-top: 0.35rem;
  font-family: var(--font-mono);
  font-size: clamp(1.85rem, 4.5vw, 2.45rem);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
  text-align: right;
  line-height: 1.15;
  color: #f4f8fb;
  word-break: break-all;
}

.calc-result.is-error {
  color: #ffb4b4;
  font-size: clamp(1.1rem, 3vw, 1.35rem);
  font-weight: 500;
}

.calc-result.is-ticking {
  animation: calc-tick 0.42s var(--ease);
}

@keyframes calc-tick {
  0% {
    opacity: 0.35;
    transform: translateY(5px);
    color: #7fe3d2;
  }
  55% {
    opacity: 1;
    transform: translateY(-1px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
    color: #f4f8fb;
  }
}

.calc-error {
  position: relative;
  z-index: 1;
  margin: 0.55rem 0 0;
  font-size: 0.8rem;
  font-weight: 500;
  color: #ffb4b4;
  text-align: right;
}

.calc-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem 0.65rem;
  margin: 0.85rem 0 0.75rem;
}

.toolbar-btn {
  appearance: none;
  min-height: 2.5rem;
  min-width: 3.4rem;
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--ink-muted);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  transition: color 0.15s var(--ease), border-color 0.15s var(--ease), background 0.15s var(--ease), box-shadow 0.15s var(--ease);
}

.toolbar-btn:hover {
  color: var(--ink);
  border-color: var(--line-strong);
}

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

.toolbar-btn[aria-pressed="true"],
.toolbar-btn.is-active {
  color: var(--accent-ink);
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: inset 0 -2px 0 var(--accent);
}

.kbd-hint {
  margin: 0;
  margin-left: auto;
  font-size: 0.72rem;
  color: var(--ink-muted);
  font-weight: 500;
}

.calc-keypad {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.key-row,
.keypad-numblock {
  display: grid;
  gap: 0.4rem;
}

.key-row-6 {
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

.key-row-4,
.keypad-numblock {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.key {
  appearance: none;
  position: relative;
  display: grid;
  place-items: center;
  min-height: 2.85rem;
  padding: 0.35rem 0.25rem;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-2);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  box-shadow: 0 1px 0 color-mix(in srgb, var(--ink) 8%, transparent);
  transition: transform 0.12s var(--ease), background 0.15s var(--ease), border-color 0.15s var(--ease), color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}

.key:hover {
  border-color: var(--line-strong);
  background: color-mix(in srgb, var(--accent) 10%, var(--surface-2));
}

.key:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  z-index: 1;
}

.key:active {
  transform: translateY(1px);
  box-shadow: none;
}

.key-num {
  font-family: var(--font-mono);
  font-size: 1.05rem;
  font-weight: 500;
}

.key-fn {
  font-size: 0.8rem;
  font-weight: 650;
  color: var(--ink-muted);
  background: var(--surface-2);
}

.key-op {
  font-family: var(--font-mono);
  font-size: 1.15rem;
  color: var(--accent-ink);
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--line));
}

.key-op:hover {
  background: color-mix(in srgb, var(--accent) 22%, var(--surface-2));
  border-color: var(--accent);
}

.key-eq {
  font-family: var(--font-mono);
  font-size: 1.2rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border-color: var(--accent-hover);
}

.key-eq:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: #fff;
}

.key-util {
  color: var(--danger);
  background: var(--danger-soft);
  border-color: color-mix(in srgb, var(--danger) 35%, var(--line));
  font-size: 0.82rem;
}

.key-mem {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  letter-spacing: 0.02em;
  color: var(--ink-muted);
}

.key-primary,
.key-secondary {
  line-height: 1.1;
}

.key-secondary {
  display: none;
  font-size: 0.72rem;
  opacity: 0.95;
}

body.calc-second .key[data-insert-2nd] .key-primary,
body.calc-second .key[data-action="sqrt"] .key-primary,
body.calc-second .key[data-action="square"] .key-primary {
  display: none;
}

body.calc-second .key[data-insert-2nd] .key-secondary,
body.calc-second .key[data-action="sqrt"] .key-secondary,
body.calc-second .key[data-action="square"] .key-secondary {
  display: block;
}

body.calc-second .key[data-insert-2nd],
body.calc-second .key[data-action="sqrt"],
body.calc-second .key[data-action="square"] {
  color: var(--accent-ink);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--line));
  background: var(--accent-soft);
}

.calc-history {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.95rem 1rem 1.05rem;
  min-height: 12rem;
  max-height: min(42rem, 78vh);
  display: flex;
  flex-direction: column;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.65rem;
}

.history-heading {
  margin: 0;
  font-family: var(--font-display);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-muted);
}

.history-clear {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--ink-muted);
  font-family: var(--font-body);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 0.35rem 0.4rem;
  min-height: 2.5rem;
  border-radius: 4px;
  cursor: pointer;
}

.history-clear:hover:not(:disabled) {
  color: var(--danger);
}

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

.history-clear:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.history-empty {
  margin: 0;
  font-size: 0.88rem;
  color: var(--ink-muted);
  line-height: 1.45;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  overflow-y: auto;
  margin-top: 0.35rem;
  padding-right: 0.15rem;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.history-item {
  appearance: none;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.15rem;
  width: 100%;
  padding: 0.65rem 0.75rem;
  text-align: right;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.15s var(--ease), background 0.15s var(--ease);
}

.history-item:hover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, var(--surface-2));
}

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

.history-expr {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--ink-muted);
  word-break: break-all;
}

.history-result {
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 550;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  word-break: break-all;
}

@media (prefers-reduced-motion: reduce) {
  .calc-result.is-ticking {
    animation: none !important;
  }

  .key {
    transition: none !important;
  }
}

@media (max-width: 860px) {
  .calc-workspace {
    grid-template-columns: 1fr;
  }

  .calc-history {
    max-height: 16rem;
    order: 2;
  }

  .kbd-hint {
    margin-left: 0;
    width: 100%;
  }
}

@media (max-width: 640px) {
  .shell-calc {
    width: min(100% - 1.25rem, 1080px);
  }

  .calc-keypad,
  .key-row,
  .keypad-numblock {
    gap: 0.35rem;
  }

  .key {
    min-height: 2.75rem;
  }

  .key-fn {
    font-size: 0.74rem;
  }
}
