:root {
  --bg: #000;
  --digit: #333;
  --fn: #a5a5a5;
  --op: #ff9f0a;
  --text-dim: #8e8e93;
  --gap: clamp(10px, 1.9vmin, 20px);
  --pad: clamp(16px, 3.4vmin, 34px);
  --calc-w: min(92vw, 62vmin, 600px);
}

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

html, body { height: 100%; }

body {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--bg);
  color: #fff;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Helvetica Neue", "PingFang SC", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

button {
  font-family: inherit;
  border: none;
  cursor: pointer;
  background: none;
  color: inherit;
}

.app { position: relative; display: flex; height: 100%; }

/* ---------- 历史记录 ---------- */

.history {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 30;
  width: min(80vw, 360px);
  display: flex;
  flex-direction: column;
  padding:
    calc(env(safe-area-inset-top, 0px) + 28px)
    20px
    calc(env(safe-area-inset-bottom, 0px) + 20px)
    calc(env(safe-area-inset-left, 0px) + 24px);
  background: rgba(20, 20, 22, 0.98);
  -webkit-backdrop-filter: blur(24px);
  backdrop-filter: blur(24px);
  border-right: 1px solid #242426;
  transform: translateX(-103%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0.28, 1);
}

.app.show-history .history { transform: translateX(0); }

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.history-title { font-size: 15px; font-weight: 600; color: #fff; }

.history-clear { font-size: 13px; color: var(--op); padding: 6px 2px; }

.history-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-bottom: 8px;
}

.history-list:empty { display: none; }

.hrow {
  display: block;
  width: 100%;
  text-align: right;
  padding: 10px 6px;
  border-radius: 12px;
}

.hrow:active { background: #1c1c1e; }

.hexpr {
  color: var(--text-dim);
  font-size: 14px;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.hres {
  color: #fff;
  font-size: 24px;
  font-weight: 300;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.history-empty {
  margin: auto;
  color: #48484a;
  font-size: 14px;
}

.scrim {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: transparent;
  display: none;
}

.app.show-history .scrim { display: block; }

/* ---------- 计算器主体 ---------- */

.calc {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  position: relative;
  padding:
    calc(env(safe-area-inset-top, 0px) + 14px)
    calc(env(safe-area-inset-right, 0px) + var(--pad))
    calc(env(safe-area-inset-bottom, 0px) + var(--pad))
    calc(env(safe-area-inset-left, 0px) + var(--pad));
}

.history-toggle {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 14px);
  left: calc(env(safe-area-inset-left, 0px) + 14px);
  width: 46px;
  height: 46px;
  border-radius: 14px;
  background: #1c1c1e;
  color: #a5a5a5;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.history-toggle:active { background: #2c2c2e; color: #fff; }

.history-toggle svg { width: 22px; height: 22px; }

.display {
  width: var(--calc-w);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: clamp(4px, 0.8vmin, 10px);
  padding: 0 calc(var(--gap) * 0.9) clamp(14px, 2.6vmin, 28px);
}

.expression {
  min-height: 1.35em;
  text-align: right;
  color: var(--text-dim);
  font-size: clamp(17px, 2.9vmin, 27px);
  font-weight: 400;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}

.result {
  text-align: right;
  font-size: clamp(50px, 10.5vmin, 96px);
  font-weight: 200;
  line-height: 1.08;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  font-variant-numeric: tabular-nums;
}

.keypad {
  width: var(--calc-w);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}

.key {
  aspect-ratio: 1 / 1;
  border-radius: 50%;
  background: var(--digit);
  color: #fff;
  font-size: clamp(24px, 4.5vmin, 40px);
  font-weight: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.15s ease, filter 0.12s ease, transform 0.08s ease;
  will-change: transform;
  outline: none;
}

@media (hover: hover) and (pointer: fine) {
  .key:hover, .history-toggle:hover { filter: brightness(1.18); }
}

.key:active, .key.pressed {
  filter: brightness(1.45);
  transform: scale(0.97);
}

.key.fn {
  background: var(--fn);
  color: #000;
  font-size: clamp(22px, 4vmin, 36px);
}

.key.op {
  background: var(--op);
  font-size: clamp(30px, 5.6vmin, 52px);
  font-weight: 300;
}

.key.op.active {
  background: #fff;
  color: var(--op);
}

.key.zero {
  grid-column: span 2;
  aspect-ratio: auto;
  border-radius: 999px;
  justify-content: flex-start;
  padding-left: calc((100% - var(--gap)) / 4 - 0.32em);
}

/* ---------- 横屏：历史侧栏常驻 ---------- */

@media (orientation: landscape) {
  .history {
    position: relative;
    transform: none;
    width: min(38vw, 400px);
    height: 100%;
    flex-shrink: 0;
    background: transparent;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .scrim { display: none !important; }
  .history-toggle { display: none; }
}
