:root {
  --bg: #000;
  --key: #333333;
  --fn: #a5a5a5;
  --fn-text: #000;
  --sci: #2b2b2b;
  --op: #ff9f0a;
  --op-text: #fff;
  --text: #fff;
  --muted: #a1a1a1;
  --chip: #1c1c1e;
  --ring: #3a3a3c;
  --sheet: #1c1c1e;
  --divider: #3a3a3c;
  --accent: #0a84ff;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f2f2f7;
    --key: #ffffff;
    --fn: #d1d1d6;
    --fn-text: #000;
    --sci: #e4e4ea;
    --op: #ff9f0a;
    --op-text: #fff;
    --text: #000;
    --muted: #8e8e93;
    --chip: #ffffff;
    --ring: #d1d1d6;
    --sheet: #ffffff;
    --divider: #c6c6c8;
    --accent: #007aff;
    color-scheme: light;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* dvh tracks the visible area above Safari's toolbar; vh is the fallback. */
html, body {
  height: 100%;
  height: 100dvh;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'SF Pro Display', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  justify-content: center;
}
/* Column that fills the visible screen. Children share the height with flex. */
.calc {
  width: min(100%, 430px);
  height: 100%;
  padding: 8px 12px max(12px, env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex: 0 0 auto;
  height: 44px;
}
.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--ring);
  background: var(--chip);
  color: var(--text);
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}
.icon-btn:active { opacity: .7; }
.icon-btn.active { background: var(--op); color: #fff; border-color: var(--op); }
/* Display takes the spare height; keypads take fixed shares of the rest. */
.display {
  flex: 2 1 0;
  min-height: 90px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  text-align: right;
  padding: 0 12px 4px;
  overflow: hidden;
}
.calc.sci-open .display { flex: 1.4 1 0; }
.expression {
  color: var(--muted);
  font-size: clamp(15px, 3.6vh, 19px);
  min-height: 1.4em;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}
.result {
  font-size: clamp(40px, 12vh, 84px);
  font-weight: 300;
  letter-spacing: -0.02em;
  line-height: 1.05;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}
/* Keypads: 5 rows each, sized by flex so nothing runs off screen. */
.keys {
  flex: 5 1 0;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(5, minmax(0, 1fr));
  gap: clamp(6px, 1.8vh, 12px) clamp(8px, 2.2vw, 12px);
}
.keys.sci {
  flex: 3 1 0;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: clamp(5px, 1.2vh, 10px) clamp(5px, 1.5vw, 10px);
}
.keys[hidden] { display: none; }
.key {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: none;
  border-radius: 999px;
  background: var(--key);
  color: var(--text);
  font-family: inherit;
  font-size: clamp(16px, min(5.6vw, 4.2vh), 30px);
  font-weight: 400;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  white-space: nowrap;
  touch-action: manipulation;
  transition: filter .12s, transform .08s;
}
.key:active { filter: brightness(1.6); transform: scale(.96); }
.key.func { background: var(--fn); color: var(--fn-text); }
.key.op { background: var(--op); color: var(--op-text); }
.key.sci-key {
  background: var(--sci);
  font-size: clamp(11px, min(3.4vw, 2.6vh), 17px);
}
.key.sci-key.active { background: var(--op); color: #fff; }
.history {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(100%, 360px);
  height: 100dvh;
  background: var(--sheet);
  box-shadow: -12px 0 36px rgba(0,0,0,.35);
  padding: calc(16px + env(safe-area-inset-top, 0px)) 16px calc(16px + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  z-index: 10;
}
.history[hidden] { display: none; }
.history-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 12px;
}
.text-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
}
#history-list { list-style: none; padding: 0; margin: 0; }
#history-list li {
  padding: 12px 4px;
  border-bottom: 1px solid var(--divider);
  cursor: pointer;
  text-align: right;
}
#history-list .h-expr { color: var(--muted); font-size: 14px; }
#history-list .h-res { font-size: 22px; font-weight: 300; }
@media (max-width: 360px) {
  .calc { padding: 6px 10px max(10px, env(safe-area-inset-bottom, 0px)); }
}
