/* ════════════════════════════════════════════════════════════
   Calcolatrice Scientifica — Dark Theme
   ════════════════════════════════════════════════════════════ */

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

:root {
  --bg:         #1a1a2e;
  --surface:    #16213e;
  --surface2:   #0f3460;
  --accent:     #e94560;

  /* Categorie bottoni */
  --btn-num:    #1e2a45;
  --btn-num-h:  #2a3a5e;
  --btn-op:     #6c3483;
  --btn-op-h:   #8e44ad;
  --btn-sci:    #0e6655;
  --btn-sci-h:  #148f77;
  --btn-mem:    #1a5276;
  --btn-mem-h:  #2e86c1;
  --btn-eq:     #c0392b;
  --btn-eq-h:   #e74c3c;
  --btn-ac:     #7f1d1d;
  --btn-ac-h:   #b91c1c;
  --btn-util:   #374151;
  --btn-util-h: #4b5563;

  --text:       #e8eaf6;
  --text-dim:   #9fa8da;
  --radius:     10px;
  --shadow:     0 4px 20px rgba(0,0,0,.5);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 16px;
}

/* ── Layout principale ─────────────────────────────────────── */

.app {
  display: flex;
  gap: 14px;
  width: 100%;
  max-width: 860px;
  align-items: flex-start;
}

/* ── Calcolatrice ──────────────────────────────────────────── */

.calculator {
  flex: 0 0 auto;
  width: 100%;
  max-width: 460px;
  min-width: 340px;
  background: var(--surface);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* ── Display ───────────────────────────────────────────────── */

.display {
  background: #0d1b2a;
  padding: 18px 20px 14px;
  text-align: right;
  min-height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  position: relative;
}

#display-expr {
  font-size: 0.95rem;
  color: var(--text-dim);
  min-height: 1.4em;
  word-break: break-all;
  transition: opacity .15s;
}

#display-result {
  font-size: 2.4rem;
  font-weight: 300;
  letter-spacing: -0.5px;
  color: var(--text);
  min-height: 1.2em;
  word-break: break-all;
  transition: color .15s;
}

#display-result.error {
  color: var(--accent);
  animation: shake .3s;
}

@keyframes shake {
  0%,100% { transform: translateX(0); }
  25%      { transform: translateX(-6px); }
  75%      { transform: translateX(6px); }
}

#mem-indicator {
  position: absolute;
  top: 10px;
  left: 16px;
  font-size: 0.7rem;
  color: var(--btn-mem-h);
  font-weight: 700;
  opacity: 0;
  transition: opacity .2s;
}
#mem-indicator.active { opacity: 1; }

/* ── Tastierino ────────────────────────────────────────────── */

#keypad {
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

/* ── Pulsante base ─────────────────────────────────────────── */

.btn {
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .12s, transform .08s, box-shadow .12s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  color: var(--text);
}

.btn:hover  { filter: brightness(1.18); }
.btn:active,
.btn.pressed { transform: scale(.93); box-shadow: none; }
.btn.key-active { outline: 2px solid rgba(255,255,255,.4); }

/* Categorie */
.btn-num  { background: var(--btn-num);  }
.btn-op   { background: var(--btn-op);   }
.btn-sci  { background: var(--btn-sci);  font-size: .88rem; }
.btn-mem  { background: var(--btn-mem);  font-size: .82rem; }
.btn-eq   { background: var(--btn-eq);   font-size: 1.4rem; font-weight: 700; }
.btn-ac   { background: var(--btn-ac);   }
.btn-util { background: var(--btn-util); }

/* Estensione griglia */
.span2 { grid-column: span 2; }

/* ── Toggle DEG/RAD ────────────────────────────────────────── */

#btn-mode {
  font-size: .78rem;
  background: var(--surface2);
  letter-spacing: 1px;
}

/* ── Pannello cronologia ───────────────────────────────────── */

.history-panel {
  flex: 1 1 200px;
  min-width: 180px;
  max-width: 340px;
  background: var(--surface);
  border-radius: 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  max-height: 560px;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 10px;
  border-bottom: 1px solid rgba(255,255,255,.06);
}

.history-header h2 {
  font-size: .85rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.btn-clear-hist {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: .75rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: background .12s;
}
.btn-clear-hist:hover { background: rgba(255,255,255,.08); color: var(--text); }

#history-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 8px;
}

#history-list::-webkit-scrollbar { width: 4px; }
#history-list::-webkit-scrollbar-thumb { background: rgba(255,255,255,.1); border-radius: 2px; }

.history-item {
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: background .12s;
}
.history-item:hover { background: rgba(255,255,255,.07); }

.h-expr {
  display: block;
  font-size: .78rem;
  color: var(--text-dim);
  word-break: break-all;
}
.h-result {
  display: block;
  font-size: .92rem;
  font-weight: 600;
  color: var(--text);
}

.history-empty {
  text-align: center;
  color: var(--text-dim);
  font-size: .82rem;
  padding: 24px 0;
  list-style: none;
}

/* ── Responsive ─────────────────────────────────────────────── */

@media (max-width: 680px) {
  .app { flex-direction: column; align-items: center; }
  .history-panel {
    width: 100%;
    max-width: 460px;
    max-height: 240px;
    min-width: unset;
  }
  .calculator { max-width: 460px; }
}

@media (max-width: 380px) {
  .btn { height: 46px; font-size: .9rem; }
  #display-result { font-size: 2rem; }
}
