/* ================================================================ 色・共通 */

:root {
  color-scheme: light dark;
  --bg: light-dark(#f4f3ef, #16171a);
  --panel: light-dark(#ffffff, #1f2024);
  --text: light-dark(#1c1d20, #ececec);
  --muted: light-dark(#7a7c82, #8d9097);
  --line: light-dark(#e2e0da, #2e3036);
  --key: light-dark(#ffffff, #2a2c31);
  --key-hover: light-dark(#f0efea, #33363c);
  --fn: light-dark(#e9e7e1, #3a3d44);
  --fn-hover: light-dark(#dedcd5, #454850);
  --accent: light-dark(#d9622b, #e0703a);
  --accent-hover: light-dark(#c4551f, #ea8150);
  --accent-text: #ffffff;
  --error: light-dark(#c0392b, #ff7b6b);
  --good: light-dark(#1d7f55, #5fd09a);
  --good-bg: light-dark(#e6f3ec, #1b2e25);
  --bad-bg: light-dark(#f8e9e6, #33211f);
  --shadow: 0 1px 2px light-dark(rgb(0 0 0 / 0.06), rgb(0 0 0 / 0.4)),
            0 8px 24px light-dark(rgb(0 0 0 / 0.06), rgb(0 0 0 / 0.3));
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  min-height: 100dvh;
}

/* ボタンの見た目はすべてこのサイトで決める */
button { all: unset; box-sizing: border-box; cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.noscript { margin: 0; padding: 12px 16px; text-align: center; background: var(--bad-bg); color: var(--error); }

.bad { color: var(--error); }
.good { color: var(--good); }

/* ================================================================ レイアウト
   スマホ: 電卓 → 説明 の縦積み。広い画面: 左に説明パネル、右に電卓 */

.layout {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}

@media (min-width: 1000px) {
  .layout {
    flex-direction: row;
    justify-content: center;
    align-items: flex-start;
    gap: 48px;
    padding: 24px;
  }
  .about { order: -1; width: 420px; }
  .about-closed .layout { align-items: center; }
  .layout .about-open { position: fixed; top: 20px; left: 20px; }
}

/* 電卓全体（関数キーを開いた状態でも）が画面に収まる高さのときだけ、スクロールしても上に留める */
@media (min-width: 1000px) and (min-height: 880px) {
  .calc { position: sticky; top: 24px; }
  .about-closed .calc { position: static; }
}

/* ================================================================ 電卓 */

.calc {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ---- 表示部 ---- */

.screen {
  background: var(--panel);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 16px 18px 14px;
  min-height: 168px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 4px;
}

.screen-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: auto;
  padding-bottom: 8px;
}

.chip {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--accent);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
}
.chip:hover { background: var(--fn); }

.format select {
  appearance: none;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--fn) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%237a7c82' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") no-repeat right 10px center;
  border: none;
  border-radius: 999px;
  padding: 4px 28px 4px 12px;
  cursor: pointer;
}

/* 横にはみ出す内容はスクロールできるが、バーは見せない */
.expr, .result {
  overflow-x: auto;
  scrollbar-width: none;
}
.expr::-webkit-scrollbar, .result::-webkit-scrollbar { display: none; }

.expr {
  color: var(--muted);
  font-size: 18px;
  text-align: right;
  white-space: nowrap;
  min-height: 1.4em;
  font-variant-numeric: tabular-nums;
}

.caret {
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: -0.15em;
  background: var(--accent);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.result {
  display: block;
  text-align: right;
  font-size: 40px;
  line-height: 1.2;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  word-break: break-all;
  overflow-y: hidden;
  min-height: 1.2em;
  padding: 0.12em 0;
  border-radius: 6px;
  transition: color 0.15s;
}
.result:focus-visible { outline-offset: 4px; }
.result.preview { color: var(--muted); }
.result.size-m { font-size: 30px; }
.result.size-s { font-size: 22px; }
.result.size-xs { font-size: 16px; }

.message {
  text-align: right;
  font-size: 14px;
  min-height: 1.2em;
  color: var(--muted);
}
.message.error { color: var(--error); }

/* 数値・数式の部品 */
/* 循環節の上線（text-decoration の overline はフォントによって数字から離れすぎるので罫線で描く） */
.rep { position: relative; }
.rep::before {
  content: "";
  position: absolute;
  inset: 0.16em 0 auto;
  border-top: 0.06em solid currentColor;
}
.ellipsis, .approx { color: var(--muted); }
.approx { margin-right: 0.1em; }
.times { margin: 0 0.15em; }
.exp { font-size: 0.6em; vertical-align: 0.8em; line-height: 0; margin-left: 0.04em; }
/* KaTeX は組版のために要素をわずかにはみ出させるので、ここでは overflow を指定しない */
.math { display: inline-block; max-width: 100%; vertical-align: middle; }
.math .katex { font-size: 1.05em; }
.result .math .katex { font-size: 1.1em; }

/* ---- 関数キー ---- */

.fn-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.fn-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--fn);
}
.fn-toggle:hover { background: var(--fn-hover); }
.fn-icon { font-style: italic; font-family: Georgia, "Times New Roman", serif; font-size: 16px; color: var(--accent); }
.chev { margin-left: 2px; transition: transform 0.15s; }
.fn-toggle[aria-expanded="true"] .chev { transform: rotate(180deg); }
.kbd-hint { font-size: 12px; color: var(--muted); }

.fnpad {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  padding: 4px;
  border-radius: 14px;
  background: var(--fn);
}
.fnpad[hidden] { display: none; }
.fnpad button {
  text-align: center;
  height: 38px;
  line-height: 38px;
  border-radius: 10px;
  font-size: 15px;
  white-space: nowrap;
  user-select: none;
  transition: background 0.12s, transform 0.05s;
}
.fnpad button:hover { background: var(--panel); }
.fnpad button:active, .fnpad button.pressed { transform: scale(0.96); background: var(--panel); }
.fnpad sup { font-size: 0.65em; line-height: 0; }
.fnpad .toggle {
  background: var(--panel);
  color: var(--accent);
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.04em;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.12);
}

/* ---- キー ---- */

.keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.k {
  text-align: center;
  height: 58px;
  line-height: 58px;
  border-radius: 14px;
  background: var(--key);
  font-size: 22px;
  user-select: none;
  box-shadow: 0 1px 0 var(--line);
  transition: background 0.1s, transform 0.05s;
}
.k:hover { background: var(--key-hover); }
.k:active, .k.pressed { transform: scale(0.97); }
.k.fn { background: var(--fn); font-size: 19px; }
.k.fn:hover { background: var(--fn-hover); }
.k.op { color: var(--accent); font-size: 26px; }
.k.eq { grid-column: 1 / -1; background: var(--accent); color: var(--accent-text); font-size: 26px; }
.k.eq:hover { background: var(--accent-hover); }

/* ================================================================ 説明パネル */

.about {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 22px 22px 18px;
  line-height: 1.6;
}
.about-closed .about { display: none; }

.about-head { display: flex; justify-content: space-between; align-items: center; }
.brand { font-size: 13px; font-weight: 700; letter-spacing: 0.08em; color: var(--accent); }
.about-close {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  color: var(--muted);
}
.about-close:hover { background: var(--fn); color: var(--text); }

.about h1 { font-size: 28px; line-height: 1.25; margin: 6px 0 8px; letter-spacing: 0.01em; }
.about h2 { font-size: 14px; color: var(--muted); font-weight: 600; margin: 22px 0 8px; }
.lead { margin: 0 0 18px; font-size: 15px; }
.lead strong { color: var(--accent); }

.about-open {
  display: none;
  font-size: 13px;
  color: var(--muted);
  background: var(--panel);
  box-shadow: var(--shadow);
  border-radius: 999px;
  padding: 8px 14px;
}
.about-open:hover { color: var(--text); }
.about-closed .about-open { display: inline-block; }

/* 例のカード共通 */
.hero, .example, .sym-card { transition: border-color 0.15s, outline-color 0.15s, transform 0.1s; }
.hero:active, .example:active, .sym-card:active { transform: scale(0.99); }
.example:hover, .sym-card:hover { outline: 1px solid var(--accent); }

/* 一番の例 */
.hero {
  display: block;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
}
.hero:hover { border-color: var(--accent); }
.hero-expr {
  display: block;
  font-size: 19px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  margin-bottom: 12px;
  overflow-wrap: anywhere;
}
.vs { display: grid; gap: 6px; }
.vs-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  border-radius: 10px;
  padding: 8px 12px;
}
.vs-row.bad { background: var(--bad-bg); }
.vs-row.good { background: var(--good-bg); }
.vs-label { font-size: 12px; font-weight: 600; white-space: nowrap; }
.vs-val { font-size: 20px; font-weight: 600; text-align: right; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.vs-row.bad .vs-val { font-size: 30px; line-height: 1.1; }
.hero-exact, .try { display: block; text-align: right; }
.hero-exact { margin-top: 10px; font-size: 13px; color: var(--muted); }
.try { margin-top: 8px; font-size: 12px; color: var(--accent); }
.try::after { content: " →"; }

/* 浮動小数点との比較 */
.examples { display: grid; gap: 6px; }
.legend { display: flex; justify-content: flex-end; gap: 16px; font-size: 12px; }
.example {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "expr float" "expr exact";
  column-gap: 12px;
  align-items: center;
  border-radius: 12px;
  padding: 8px 12px;
  background: var(--bg);
  font-variant-numeric: tabular-nums;
}
.ex-expr { grid-area: expr; font-weight: 600; font-size: 16px; }
.ex-float, .ex-exact { text-align: right; font-size: 14px; }
.ex-float { grid-area: float; }
.ex-exact { grid-area: exact; font-weight: 600; }
.mark { margin-right: 5px; font-size: 0.85em; }

/* 記号のまま答える例 */
.symbolic { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.sym-card {
  display: flex;
  flex-direction: column;
  gap: 2px;
  border-radius: 12px;
  padding: 10px 12px;
  background: var(--bg);
}
.sym-in { font-size: 13px; color: var(--muted); }
.sym-out { font-size: 22px; font-weight: 600; color: var(--good); }

.note { font-size: 12px; color: var(--muted); margin: 16px 0 0; }

.fine { margin-top: 14px; font-size: 12px; color: var(--muted); }
.fine summary { cursor: pointer; }
.fine ul { margin: 6px 0 0; padding-left: 1.2em; }
.fine li { margin-bottom: 4px; }
.fine code { font-size: 11px; background: var(--fn); padding: 0 4px; border-radius: 4px; color: var(--text); }
