:root {
  --bg: #1c1c1e;
  --display-bg: #1c1c1e;
  --history-bg: #2c2c2e;
  --btn-number: #3a3a3c;
  --btn-function: #636366;
  --btn-operator: #ff9f0a;
  --btn-equal: #ff9f0a;
  --btn-active: #ffffff;
  --text-primary: #ffffff;
  --text-secondary: #ebebf5cc;
  --text-result: #ffffff;
  --text-history: #ebebf5cc;
  --history-hover: #3a3a3c;
  --radius-btn: 50%;
  --gap: 12px;
  /*
    ボタンサイズ = 幅制限と高さ制限の小さい方
    ・幅: (画面幅 - 左右padding×2 - 列間gap×3) ÷ 4列
    ・高: (画面高 - ディスプレイ～パディング固定分) ÷ 5行
  */
  /*
    310px = ディスプレイ(128px) + ボタン上下padding+gap(80px) + 履歴最小確保(100px)
  */
  --btn-size: min(
    calc((100vw - 5 * var(--gap)) / 4),
    calc((100dvh - 310px) / 5)
  );
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

body {
  background: #000;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', sans-serif;
}

#app {
  background: var(--bg);
  width: 100%;
  max-width: 420px;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ── 履歴エリア ─────────────────────── */
#history-area {
  flex: 1;
  background: var(--history-bg);
  overflow-y: auto;
  padding: 12px 16px 8px;
  display: flex;
  flex-direction: column;
  min-height: 100px;
}

#history-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.history-item {
  color: var(--text-history);
  font-size: 16px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
  text-align: right;
  line-height: 1.4;
}

.history-item:hover,
.history-item:active {
  background: var(--history-hover);
  color: var(--text-primary);
}

.history-item .hist-expr {
  font-size: 13px;
  opacity: 0.7;
}

.history-item .hist-result {
  font-size: 18px;
  font-weight: 500;
}

.history-empty {
  color: #636366;
  font-size: 14px;
  text-align: center;
  margin: auto;
  padding: 16px 0;
}

#history-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
  min-height: 32px;
}

#clear-history {
  background: none;
  border: 1px solid #636366;
  color: #636366;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 20px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

#clear-history:hover {
  border-color: #ff453a;
  color: #ff453a;
}

#footer-right {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
}

#share-btn {
  background: none;
  border: 1px solid #ff9f0a;
  color: #ff9f0a;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 20px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

#share-btn:hover {
  background: #ff9f0a;
  color: #000;
}

#contact-link {
  color: #636366;
  font-size: 13px;
  text-decoration: none;
  padding: 4px 2px;
}

#contact-link:hover {
  color: #ebebf5cc;
}

/* ── ディスプレイ ─────────────────────── */
#display {
  background: var(--display-bg);
  padding: 12px 24px 8px;
  text-align: right;
}

#expression {
  color: var(--text-secondary);
  font-size: 28px;
  min-height: 36px;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
  text-align: right;
}

#expression::-webkit-scrollbar {
  display: none;
}

#result {
  color: var(--text-result);
  font-size: 64px;
  font-weight: 300;
  line-height: 1.1;
  min-height: 72px;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
  transition: font-size 0.15s;
}

#result::-webkit-scrollbar {
  display: none;
}

#result.shrink {
  font-size: 44px;
}

#result.shrink-more {
  font-size: 30px;
}

/* ── ボタンエリア ─────────────────────── */
#buttons {
  display: grid;
  grid-template-columns: repeat(4, var(--btn-size));
  gap: var(--gap);
  padding: 8px var(--gap) 24px;
  background: var(--bg);
  justify-content: center;
}

.btn {
  width: var(--btn-size);
  height: var(--btn-size);
  border: none;
  border-radius: var(--radius-btn);
  font-size: 24px;
  font-weight: 400;
  cursor: pointer;
  transition: filter 0.1s, transform 0.08s;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn:active {
  filter: brightness(1.4);
  transform: scale(0.94);
}

.btn-number   { background: var(--btn-number); }
.btn-function { background: var(--btn-function); color: #000; }
.btn-operator { background: var(--btn-operator); color: #fff; font-size: 28px; }
.btn-equal    { background: var(--btn-equal); color: #fff; font-size: 28px; }

.btn-operator.active {
  background: #fff;
  color: var(--btn-operator);
}

/* 0ボタンは横2マス */
.btn-zero {
  grid-column: span 2;
  width: calc(2 * var(--btn-size) + var(--gap));
  height: var(--btn-size);
  border-radius: 9999px;
  justify-content: flex-start;
  padding-left: calc(var(--btn-size) * 0.35);
}

/* ── レスポンシブ ────────────────────────── */

/* PC：420px超では幅方向を420px固定、高さ制限は継続 */
@media (min-width: 420px) {
  :root {
    --btn-size: min(
      calc((420px - 5 * var(--gap)) / 4),
      calc((100dvh - 310px) / 5)
    );
  }
}

/* 縦が小さい画面 */
@media (max-height: 700px) {
  :root { --gap: 10px; }
  #result { font-size: 52px; }
  #history-area { max-height: 180px; }
}

@media (max-height: 580px) {
  :root { --gap: 8px; }
  #result { font-size: 44px; }
  #history-area { max-height: 120px; }
}
