/* ===== 设计令牌 ===== */
:root {
  --bg: #F6F6F4;
  --surface: #FFFFFF;
  --ink: #1B1B1F;
  --ink-soft: #6E6E73;
  --ink-faint: #A8A8AD;
  --line: rgba(27, 27, 31, 0.06);
  --line-strong: rgba(27, 27, 31, 0.12);
  --key-bg: #FFFFFF;
  --key-bg-hi: #F0EFEC;
  --accent: #4F46E5;
  --accent-soft: rgba(79, 70, 229, 0.10);
  --shadow: 0 24px 60px -28px rgba(20, 20, 40, 0.28);
}

[data-theme="dark"] {
  --bg: #0E0F12;
  --surface: #15161A;
  --ink: #F3F3F5;
  --ink-soft: #9A9BA3;
  --ink-faint: #56575E;
  --line: rgba(255, 255, 255, 0.07);
  --line-strong: rgba(255, 255, 255, 0.16);
  --key-bg: #1B1C21;
  --key-bg-hi: #26272E;
  --accent: #7C7CFA;
  --accent-soft: rgba(124, 124, 250, 0.16);
  --shadow: 0 24px 60px -28px rgba(0, 0, 0, 0.6);
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto,
    "PingFang SC", "Microsoft YaHei", sans-serif;
  transition: background 0.35s ease, color 0.35s ease;
}

.stage {
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 0;
}

.calc {
  width: 100%;
  max-width: 480px;
  min-height: 100dvh;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 14px) 18px calc(env(safe-area-inset-bottom) + 18px);
  transition: background 0.35s ease;
}

@media (min-width: 560px) {
  .calc {
    border-radius: 34px;
    min-height: 0;
    margin: 5vh auto;
    box-shadow: var(--shadow);
    height: 90vh;
    max-height: 92vh;
  }
  .stage { align-items: center; }
}

/* ===== 顶栏 ===== */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 6px var(--accent-soft);
}
.brand-name {
  font-size: 18px; font-weight: 600; letter-spacing: 0.3px;
  color: var(--ink);
}
.top-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.chip {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-soft);
  font-size: 14px;
  height: 32px;
  padding: 0 14px;
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
}
.chip:active { transform: scale(0.94); background: var(--key-bg-hi); }
/* 筛选按钮：按下后切换强调色 */
.chip.filter {
  padding: 0;
  width: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
}
.chip.filter[aria-pressed="true"]#favFilter {
  background: rgba(255, 185, 0, 0.16);
  border-color: rgba(255, 185, 0, 0.38);
  color: #e09812;
}
.chip.filter[aria-pressed="true"]#lockFilter {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* 筛选状态：只显示收藏/锁定 */
.sheet[data-filter-fav="1"]:not([data-filter-lock="1"]) .line:not([data-fav="1"]) { display: none; }
.sheet[data-filter-lock="1"]:not([data-filter-fav="1"]) .line:not([data-lock="1"]) { display: none; }
/* 都按了：两个都满足（收藏 或 锁定）显示，其他不显示 */
.sheet[data-filter-fav="1"][data-filter-lock="1"] .line:not([data-fav="1"]):not([data-lock="1"]) { display: none; }

/* ===== 多行计算区域 ===== */
.sheet {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px 2px 18px;
  scrollbar-width: thin;
}
.sheet::-webkit-scrollbar { width: 5px; }
.sheet::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 4px; }

/* 计算区 → 键盘 之间的分界：一条 1px 软边 + 上方 6px 留白，
   让两区分界有明显的安全距，内容不会看起来"贴在键盘上/被键盘盖住" */
.keypad-sep {
  height: 1px;
  background: var(--line);
  margin: 2px 0 10px;
}

.line {
  position: relative;
  overflow: hidden;
  padding: 10px 4px;
  border: none;
  transition: background 0.2s ease;
  /* 底色只给公式区域（下面的 .line-expr 默认着色），这里不再给整行胶囊 */
  background: transparent;
  margin-bottom: 2px;
}
.line:last-child { margin-bottom: 0; }

/* 内容行：可整体左滑，删除按钮是行内最后一个元素，平时在可视区外 */
.line-inner {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  will-change: transform;
}

/* 行尾红色删除按钮：绝对定位于行内最右侧之外，平时被 `.line{overflow:hidden}` 裁掉，
   拖动整行内容时随行内元素一起向左滑出（苹果 Mail 效果） */
.line-del {
  position: absolute;
  top: 50%;
  right: -88px;
  transform: translateY(-50%);
  width: 78px;
  height: 46px;
  border-radius: 12px;
  background: #ff3b30;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
  pointer-events: none;
  user-select: none;
}

/* 一行内依次：收藏按钮、锁定按钮、输入框、结果区 */
.row-tools {
  flex: 0 0 auto;
  display: flex;
  flex-direction: row;
  gap: 8px;
}
.rowtool {
  border: none;
  background: transparent;
  color: var(--ink-soft);
  width: 32px;
  height: 32px;
  border-radius: 11px;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease, transform 0.12s ease;
}
.rowtool:active { transform: scale(0.86); }
.rowtool .icn { display: block; }

/* 收藏：默认灰 → 激活金色（背景+图标同时切换） */
.rowtool.fav.active { background: rgba(255, 185, 0, 0.16); color: #f6a623; }
/* 锁定：默认灰 → 激活紫色（背景+图标同时切换） */
.rowtool.lock.active { background: var(--accent-soft); color: var(--accent); }
.line.locked .line-expr { opacity: 0.72; }
.line.locked .line-expr:focus { background: transparent; }

.line-expr {
  flex: 1 1 auto;
  outline: none;
  font-size: 22px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-all;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  min-height: 30px;
  cursor: text;
  border-radius: 12px;
  padding: 6px 10px;
  margin: -2px 0;
  margin-right: -6px;
  /* 非激活行：公式区域淡淡的浅灰底（不是整行）；当前激活行聚焦恢复强调底 */
  background: rgba(120, 120, 128, 0.055);
  transition: background 0.18s ease;
  /* 阻止双击浏览器默认选字高亮效果、禁止 iOS 长按放大镜与拼写检查菜单 */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
[data-theme="dark"] .line-expr { background: rgba(255, 255, 255, 0.035); }
/* 聚焦（真实焦点，PC 鼠标模式）或当前激活行：去掉浅灰底，回到强调底色。
   激活态（.is-active）默认仍禁用 user-select：那是自绘光标场景（touch/iOS），
   不出现原生选择以免触发放大镜/系统键盘。 */
.line-expr:focus,
.line:has(.line-expr:focus) .line-expr,
.line.is-active .line-expr {
  background: var(--accent-soft);
}
/* PC（真实 focus）模式：允许原生光标与鼠标选字，原生光标可见 */
.line-expr:focus {
  caret-color: auto;
  user-select: text;
  -webkit-user-select: text;
}
.line-expr:empty:before { content: "\8bf7\8f93\5165"; color: var(--ink-faint); font-size: 20px; }
/* 只有自绘光标（没有文本）时仍然显示 placeholder（:empty 会因有子节点失效，这里兜底） */
.line-expr.has-caret-only:empty:before,
.line-expr.has-caret-only:before {
  content: "\8bf7\8f93\5165";
  color: var(--ink-faint);
  font-size: 20px;
}
/* 自绘逻辑光标：插入到 line-expr 文本中指定位置的闪烁光标 */
.logical-caret {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--accent);
  vertical-align: -0.15em;
  margin: 0 -1px;
  border-radius: 1px;
  animation: caretBlink 1s step-end infinite;
  position: relative;
  z-index: 1;
}
@keyframes caretBlink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0; }
}
/* 自绘光标模式（touch/iOS，无真实 focus）下浏览器原生光标隐藏；
   获得真实焦点（PC）时由上面 .line-expr:focus 恢复到 auto。 */
.line-expr {
  caret-color: transparent;
}

.line-result {
  flex: 0 0 auto;
  min-width: 74px;
  font-size: 22px;
  font-weight: 600;
  color: var(--accent);
  text-align: right;
  font-variant-numeric: tabular-nums;
  line-height: 1.4;
  white-space: nowrap;
}
.line-result.err { color: #E5484D; }
.line-result.stats {
  min-width: 92px;
  font-size: 17px;
  font-weight: 600;
  line-height: 1.35;
  cursor: pointer;
  color: var(--accent);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}
.line-result.stats:hover { text-decoration: underline; text-underline-offset: 3px; }
.stat-more { color: var(--ink-faint); letter-spacing: 1px; }

/* ===== 键盘 ===== */
.keypad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  padding-top: 6px;
  /* 键盘区域内的按下/点击一律不抢焦点，避免输入框失焦 */
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.kpad-spacer { pointer-events: none; visibility: hidden; }
.keypad button {
  -webkit-appearance: none;
  appearance: none;
  height: 60px;
  border: none;
  border-radius: 18px;
  background: var(--key-bg);
  color: var(--ink);
  font-size: 22px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: none !important;
  transform: none !important;
  box-shadow: none !important;
  outline: none !important;
  filter: none !important;
  /* 按钮本身也不抢焦点：保持 contenteditable 光标原位 */
  user-select: none;
  -webkit-user-select: none;
}
@media (max-height: 720px) { .keypad button { height: 54px; } }
.keypad button:active { background: var(--key-bg-hi) !important; }
.op {
  color: var(--accent) !important;
  font-size: 24px !important;
  font-weight: 500 !important;
  line-height: 1;
}
.fn { color: var(--ink-soft) !important; font-size: 18px !important; }
.newline {
  background: var(--accent) !important;
  color: #fff !important;
  font-size: 24px !important;
}
[data-theme="dark"] .newline { color: #0E0F12 !important; font-weight: 600; }

/* ===== 统计详情弹层 ===== */
.pop-mask {
  position: fixed; inset: 0;
  background: rgba(10, 10, 16, 0.4);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 40;
  opacity: 0;
  animation: fadeIn 0.2s ease forwards;
}
@keyframes fadeIn { to { opacity: 1; } }

.stat-pop {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%) scale(0.95);
  width: min(92%, 420px);
  max-height: 78vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 24px;
  padding: 22px;
  box-shadow: 0 30px 80px -24px rgba(0, 0, 0, 0.5);
  z-index: 50;
  transition: background 0.35s ease;
  animation: popIn 0.24s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
}
@keyframes popIn { to { transform: translate(-50%, -50%) scale(1); } }

.stat-pop-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  font-size: 17px;
  font-weight: 600;
  color: var(--ink);
}
.stat-pop-close {
  border: none;
  background: var(--key-bg-hi);
  color: var(--ink-soft);
  width: 30px; height: 30px;
  border-radius: 50%;
  font-size: 15px;
  cursor: pointer;
  font-family: inherit;
}
.stat-pop-close:active { transform: scale(0.92); }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.stat-item {
  background: var(--key-bg-hi);
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stat-item-k { font-size: 13px; color: var(--ink-soft); }
.stat-item-v { font-size: 20px; font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }

/* ===== 函数/常量 下拉提示 ===== */
.suggest {
  position: fixed;
  z-index: 80;
  min-width: 120px;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 16px 40px -16px rgba(0, 0, 0, 0.4);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: background 0.35s ease;
}
.suggest-item {
  border: none;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font: inherit;
  font-variant-numeric: tabular-nums;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.suggest-item.fn { color: var(--accent); }
.suggest-item:active { background: var(--key-bg-hi); }

/* π 键 */
.fn-pi { font-size: 20px !important; font-weight: 600 !important; color: var(--accent) !important; }
/* ƒ(x) 键：调出函数/常量菜单 */
.fn-func { font-size: 17px !important; font-weight: 600 !important; color: var(--accent) !important; }

/* 函数/常量 弹窗（ƒ 按钮） */
.fn-mask {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.28);
  z-index: 1000;
  backdrop-filter: blur(2px);
}
.fn-pop {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: min(420px, calc(100vw - 36px));
  max-height: 80vh;
  background: var(--surface);
  color: var(--ink);
  border-radius: 22px;
  box-shadow: 0 28px 80px -20px rgba(0,0,0,0.35);
  z-index: 1001;
  padding: 18px 18px 20px;
  overflow-y: auto;
  animation: fnPopIn .18s ease-out;
}
@keyframes fnPopIn {
  from { opacity: 0; transform: translate(-50%, -46%) scale(.98); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
.fn-pop-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px;
  font-size: 16px; font-weight: 600;
}
.fn-sect + .fn-sect { margin-top: 16px; }
.fn-sect-title {
  font-size: 12px; color: var(--ink-soft);
  letter-spacing: 0.5px; margin-bottom: 8px;
}
.fn-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.fn-chip {
  height: 44px;
  border: 1px solid var(--line);
  background: var(--key-bg);
  color: var(--ink);
  border-radius: 12px;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  transition: transform .1s ease, background .15s ease, border-color .15s ease;
}
.fn-chip.fn { color: var(--accent); font-weight: 600; }
.fn-chip.const { color: var(--ink); font-weight: 500; }
.fn-chip:active { transform: scale(0.95); background: var(--key-bg-hi); border-color: var(--line-strong); }

/* 长按锁定行：操作弹层 */
.act-pop .act-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
.act-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--key-bg);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  transition: transform .1s ease, background .15s ease, border-color .15s ease;
}
.act-btn:active { transform: scale(0.985); background: var(--key-bg-hi); border-color: var(--line-strong); }
.act-btn .act-k {
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
}
.act-btn .act-d {
  font-size: 13px;
  color: var(--ink-soft);
}
.act-btn[data-act="copy"] .act-k { color: var(--accent); }

/* 拖动删除：页面中心提示 */
.drag-hint {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 999;
  background: rgba(27, 27, 31, 0.82);
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  letter-spacing: 0.5px;
  padding: 12px 20px;
  border-radius: 999px;
  box-shadow: 0 12px 32px -12px rgba(0, 0, 0, 0.5);
  pointer-events: none;
  user-select: none;
  white-space: nowrap;
  animation: dragHintPulse 0.9s ease-in-out infinite;
}
@keyframes dragHintPulse {
  0%, 100% { opacity: 0.9; }
  50% { opacity: 0.6; }
}
.drag-hint.hint-warn { background: rgba(255, 59, 48, 0.92); }
.drag-hint.hint-lock { background: rgba(79, 70, 229, 0.94); animation: none; }

/* 收藏/锁定行被拖时：轻微抖动提示 */
@keyframes rowShake {
  0%,100% { transform: translateX(0); }
  25%     { transform: translateX(2px); }
  75%     { transform: translateX(-2px); }
}
.line.shake { animation: rowShake 0.22s ease-in-out 0s 2; }

/* ===== 使用说明弹窗（居中） ===== */
.dialog-mask {
  position: fixed; inset: 0;
  background: rgba(10, 10, 16, 0.45);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 60;
  opacity: 0;
  animation: fadeIn 0.2s ease forwards;
}
.dialog {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%) scale(0.95);
  width: min(92%, 400px);
  max-height: 78vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 24px;
  padding: 22px;
  box-shadow: 0 30px 80px -24px rgba(0, 0, 0, 0.5);
  z-index: 70;
  transition: background 0.35s ease;
  animation: dialogIn 0.24s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
}
@keyframes dialogIn { to { transform: translate(-50%, -50%) scale(1); } }
.dialog-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  font-size: 18px;
  font-weight: 600;
  color: var(--ink);
}
.dialog-body { }
.help-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  margin: 16px 0 6px;
}
.help-title:first-child { margin-top: 0; }
.hi {
  font-size: 14.5px;
  line-height: 1.7;
  color: var(--ink-soft);
}
.hi b { color: var(--ink); font-weight: 600; }
ul.hi { list-style: none; padding: 0; margin: 0; }
ul.hi li {
  position: relative;
  padding-left: 16px;
  margin-bottom: 4px;
}
ul.hi li::before {
  content: "";
  position: absolute;
  left: 4px;
  top: 11px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.75;
}
.code {
  font-family: ui-monospace, "SF Mono", SFMono-Regular, Consolas, monospace;
  font-size: 13.5px;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 1px 6px;
  border-radius: 6px;
}