:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2230;
  --disp: #05070c;
  --text: #e6edf3;
  --muted: #6b7684;
  --num: #262d3a;
  --num-hover: #313a4a;
  --fn: #1f2833;
  --fn-hover: #2a3542;
  --op: #f0883e;
  --op-hover: #ff9b52;
  --eq: #2ea043;
  --eq-hover: #3fbf55;
  --clear: #e5484d;
  --accent: #f0883e;
}

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

body {
  font-family: 'Space Grotesk', sans-serif;
  background:
    radial-gradient(circle at 20% 10%, #1a2333 0%, transparent 55%),
    radial-gradient(circle at 85% 90%, #221a2e 0%, transparent 55%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px 14px 40px;
}

.app-wrap {
  width: 100%;
  max-width: 400px;
  animation: rise 0.5s ease both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(18px); }
  to { opacity: 1; transform: translateY(0); }
}

.calc-header { text-align: center; margin-bottom: 18px; }
.calc-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}
.calc-header .emoji { font-size: 1.4rem; }
.subtitle {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 300;
  margin-top: 4px;
}

.calc-body {
  background: linear-gradient(160deg, var(--panel), var(--panel-2));
  border: 1px solid #ffffff10;
  border-radius: 24px;
  padding: 16px;
  box-shadow: 0 24px 60px -20px #000000cc, inset 0 1px 0 #ffffff0d;
}

/* Display */
.display {
  position: relative;
  background: var(--disp);
  border-radius: 16px;
  padding: 22px 18px 16px;
  margin-bottom: 14px;
  border: 1px solid #ffffff08;
  overflow: hidden;
}
.mode-badge {
  position: absolute;
  top: 10px;
  left: 12px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.65rem;
  font-weight: 800;
  letter-spacing: 1px;
  color: var(--accent);
  background: #f0883e1a;
  border: 1px solid #f0883e44;
  border-radius: 8px;
  padding: 3px 9px;
  cursor: pointer;
  transition: all 0.15s;
}
.mode-badge:hover { background: #f0883e33; }
.disp-expr {
  font-family: 'JetBrains Mono', monospace;
  color: var(--muted);
  font-size: 0.95rem;
  min-height: 1.2em;
  text-align: right;
  overflow-x: auto;
  white-space: nowrap;
  margin-bottom: 6px;
  padding-bottom: 2px;
}
.disp-result {
  font-family: 'JetBrains Mono', monospace;
  color: var(--text);
  font-size: 2.3rem;
  font-weight: 500;
  text-align: right;
  overflow-x: auto;
  white-space: nowrap;
  line-height: 1.1;
}
.disp-expr::-webkit-scrollbar, .disp-result::-webkit-scrollbar { height: 3px; }
.disp-expr::-webkit-scrollbar-thumb, .disp-result::-webkit-scrollbar-thumb { background: #ffffff22; border-radius: 2px; }

.flash {
  background: #f0883e1a;
  border: 1px solid #f0883e44;
  color: var(--accent);
  border-radius: 12px;
  padding: 8px 12px;
  text-align: center;
  font-size: 0.85rem;
  margin-bottom: 12px;
  animation: pop 0.3s ease both;
}
@keyframes pop { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* Grid */
.grid { display: flex; flex-direction: column; gap: 9px; }
.row { display: grid; gap: 9px; grid-template-columns: repeat(5, 1fr); }
.row:nth-child(3) { grid-template-columns: repeat(5, 1fr); }
.row:nth-child(n+4):not(.last-row) { grid-template-columns: repeat(4, 1fr); }
.last-row { grid-template-columns: repeat(4, 1fr); }

.calc-btn {
  font-family: 'JetBrains Mono', monospace;
  font-size: 1.15rem;
  font-weight: 500;
  border: none;
  border-radius: 14px;
  padding: 15px 0;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.calc-btn:active { transform: scale(0.92); }

.calc-num { background: var(--num); }
.calc-num:hover { background: var(--num-hover); }

.calc-fn { background: var(--fn); color: var(--accent); font-size: 0.95rem; }
.calc-fn:hover { background: var(--fn-hover); }

.calc-op { background: var(--op); color: #1a1000; font-weight: 700; }
.calc-op:hover { background: var(--op-hover); }

.calc-eq { background: var(--eq); color: #062b0f; font-weight: 800; grid-column: span 2; }
.calc-eq:hover { background: var(--eq-hover); }

.calc-clear { background: var(--clear); color: #fff; font-weight: 700; }
.calc-clear:hover { filter: brightness(1.12); }

.calc-back { background: var(--fn); color: var(--muted); }
.calc-back:hover { background: var(--fn-hover); color: var(--text); }

.wide { grid-column: span 1; }

/* History */
.history {
  margin-top: 16px;
  background: var(--panel);
  border: 1px solid #ffffff0d;
  border-radius: 16px;
  padding: 12px 14px;
}
.history-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.history-head span { font-size: 0.85rem; color: var(--muted); font-weight: 500; }
.clear-hist {
  background: none;
  border: none;
  color: var(--clear);
  font-size: 0.75rem;
  cursor: pointer;
  font-family: inherit;
}
.clear-hist:hover { text-decoration: underline; }
.history-list { display: flex; flex-direction: column; gap: 4px; max-height: 180px; overflow-y: auto; }
.history-item {
  display: flex;
  align-items: baseline;
  gap: 6px;
  background: none;
  border: none;
  border-radius: 8px;
  padding: 6px 8px;
  cursor: pointer;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text);
  text-align: left;
  transition: background 0.12s;
  justify-content: flex-end;
}
.history-item:hover { background: #ffffff0a; }
.hi-expr { color: var(--muted); font-size: 0.8rem; }
.hi-eq { color: var(--accent); }
.hi-out { font-weight: 700; font-size: 0.95rem; }

.calc-footer { text-align: center; margin-top: 22px; }
.calc-footer a {
  color: var(--muted);
  font-size: 0.8rem;
  text-decoration: none;
  border-bottom: 1px dotted var(--muted);
  transition: color 0.15s;
}
.calc-footer a:hover { color: var(--accent); border-color: var(--accent); }