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

:root {
  --cyan: #00d4ff;
  --cyan-dim: #007a94;
  --cyan-glow: rgba(0, 212, 255, 0.4);
  --bg: #000000;
  --bg-panel: #050f14;
  --bg-btn: #071820;
  --bg-btn-hover: #0d2d3d;
  --bg-operator: #071e2a;
  --bg-operator-hover: #0f3347;
  --text: #e0f7ff;
  --text-dim: #5a9ab0;
  --border: rgba(0, 212, 255, 0.25);
}

body {
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  font-family: 'Courier New', Courier, monospace;
  color: var(--text);
}

.app-wrapper {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.calculator {
  width: 320px;
  background: var(--bg-panel);
  border: 1px solid var(--cyan);
  border-radius: 4px;
  box-shadow:
    0 0 20px var(--cyan-glow),
    0 0 60px rgba(0, 212, 255, 0.1),
    inset 0 0 20px rgba(0, 212, 255, 0.03);
  overflow: hidden;
}

/* Display */
.display {
  padding: 20px 20px 12px;
  background: #000;
  border-bottom: 1px solid var(--border);
  min-height: 100px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-end;
  gap: 4px;
}

.expression {
  font-size: 13px;
  color: var(--text-dim);
  min-height: 18px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
  letter-spacing: 0.05em;
}

.result {
  font-size: 36px;
  color: var(--cyan);
  text-shadow: 0 0 10px var(--cyan-glow), 0 0 20px rgba(0, 212, 255, 0.2);
  letter-spacing: 0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
  line-height: 1.1;
}

/* Button grid */
.buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: var(--border);
  border-top: none;
}

.btn {
  background: var(--bg-btn);
  border: none;
  color: var(--text);
  font-family: 'Courier New', Courier, monospace;
  font-size: 18px;
  padding: 20px 0;
  cursor: pointer;
  transition: background 0.1s ease, color 0.1s ease, box-shadow 0.1s ease;
  position: relative;
  letter-spacing: 0.05em;
  outline: none;
}

.btn:hover {
  background: var(--bg-btn-hover);
  color: var(--cyan);
  box-shadow: inset 0 0 12px rgba(0, 212, 255, 0.15);
}

.btn:active {
  background: rgba(0, 212, 255, 0.15);
  box-shadow: inset 0 0 20px rgba(0, 212, 255, 0.3);
}

/* Operators */
.btn-operator {
  background: var(--bg-operator);
  color: var(--cyan);
  text-shadow: 0 0 6px rgba(0, 212, 255, 0.5);
}

.btn-operator:hover {
  background: var(--bg-operator-hover);
  box-shadow: inset 0 0 16px rgba(0, 212, 255, 0.2);
}

/* Clear */
.btn-memory {
  background: var(--bg-btn);
  color: var(--cyan-dim);
  font-size: 0.75rem;
  text-shadow: 0 0 4px rgba(0, 212, 255, 0.3);
}

.btn-memory:hover {
  background: var(--bg-btn-hover);
  color: var(--cyan);
}

.btn-memory:active {
  background: rgba(0, 212, 255, 0.1);
}

.btn-clear {
  background: var(--bg-operator);
  color: #ff4466;
  text-shadow: 0 0 6px rgba(255, 68, 102, 0.5);
}

.btn-clear:hover {
  background: #1a0a0d;
  color: #ff6680;
  box-shadow: inset 0 0 16px rgba(255, 68, 102, 0.2);
}

.btn-clear:active {
  background: rgba(255, 68, 102, 0.15);
}

/* Equals */
.btn-equals {
  background: var(--cyan-dim);
  color: #000;
  font-weight: bold;
  text-shadow: none;
}

.btn-equals:hover {
  background: #009bb8;
  color: #000;
  box-shadow: inset 0 0 16px rgba(0, 212, 255, 0.4);
}

.btn-equals:active {
  background: var(--cyan);
  box-shadow: 0 0 20px var(--cyan-glow);
}

/* Span utilities */
.span-2 {
  grid-column: span 2;
}

/* Scanline overlay effect */
.calculator::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0, 0, 0, 0.05) 2px,
    rgba(0, 0, 0, 0.05) 4px
  );
  pointer-events: none;
  border-radius: 4px;
}

.calculator {
  position: relative;
}

/* Scientific panel */
.scientific-panel {
  display: none;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: var(--border);
  border-bottom: 1px solid var(--border);
}

.scientific-panel.visible {
  display: grid;
}

.btn-sci {
  background: #040e18;
  color: var(--cyan-dim);
  font-size: 13px;
  padding: 12px 0;
  letter-spacing: 0.03em;
  text-shadow: 0 0 4px rgba(0, 212, 255, 0.3);
}

.btn-sci:hover {
  background: #08192a;
  color: var(--cyan);
  box-shadow: inset 0 0 10px rgba(0, 212, 255, 0.15);
}

.btn-sci-toggle {
  display: block;
  width: 100%;
  background: var(--bg-btn);
  color: var(--cyan-dim);
  font-size: 0.75rem;
  text-shadow: 0 0 4px rgba(0, 212, 255, 0.3);
  border: none;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 7px 0;
  cursor: pointer;
  font-family: 'Courier New', Courier, monospace;
  letter-spacing: 0.1em;
  transition: background 0.1s ease, color 0.1s ease;
  outline: none;
}

.btn-sci-toggle:hover {
  background: var(--bg-btn-hover);
  color: var(--cyan);
}

.btn-sci-toggle.active {
  color: var(--cyan);
  background: #071e2a;
  box-shadow: inset 0 0 12px rgba(0, 212, 255, 0.2);
}

/* History panel */
.history-panel {
  width: 180px;
  background: var(--bg-panel);
  border: 1px solid var(--cyan);
  border-radius: 4px;
  box-shadow: 0 0 20px var(--cyan-glow), 0 0 60px rgba(0, 212, 255, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: 500px;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  background: #000;
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  letter-spacing: 0.1em;
  color: var(--cyan-dim);
}

.btn-clear-history {
  background: none;
  border: 1px solid rgba(255, 68, 102, 0.4);
  color: #ff4466;
  font-family: 'Courier New', Courier, monospace;
  font-size: 10px;
  padding: 2px 6px;
  cursor: pointer;
  border-radius: 2px;
  letter-spacing: 0.05em;
  transition: background 0.1s ease, color 0.1s ease;
}

.btn-clear-history:hover {
  background: rgba(255, 68, 102, 0.15);
  color: #ff6680;
}

.history-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
}

.history-entry {
  display: flex;
  flex-direction: column;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s ease;
}

.history-entry:hover {
  background: var(--bg-btn-hover);
}

.history-expr {
  font-size: 10px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-result {
  font-size: 14px;
  color: var(--cyan);
  text-shadow: 0 0 6px var(--cyan-glow);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Mobile: 400px breakpoint ── */
@media (max-width: 560px) {
  .app-wrapper {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    width: 100vw;
  }

  .history-panel {
    width: 100%;
    max-height: 160px;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }

  .history-list {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
  }

  .history-entry {
    min-width: 100px;
    border-bottom: none;
    border-right: 1px solid var(--border);
  }
}

@media (max-width: 400px) {
  .calculator {
    width: 100vw;
    border-left: none;
    border-right: none;
    border-radius: 0;
  }

  .btn {
    font-size: 16px;
    padding: 13px 0; /* keeps min-height ≥ 44px */
    min-height: 44px;
  }

  .result {
    font-size: 30px;
  }
}

/* ── Mobile: 320px breakpoint ── */
@media (max-width: 320px) {
  .calculator {
    width: 100vw;
  }

  .btn {
    font-size: 14px;
    padding: 11px 0;
    min-height: 44px;
  }

  .display {
    padding: 14px 12px 10px;
    min-height: 80px;
  }

  .result {
    font-size: 26px;
  }

  .expression {
    font-size: 11px;
  }
}
