/*!
 * 算账稿纸 · 样式表
 * 设计语言：轻盈卡片 + 靛蓝主色 + 金额绿点缀，明暗双主题
 * 稿纸列表：左侧常驻侧栏（默认展开，可折叠，⌘/Ctrl+B）
 */

:root {
  --bg: #f4f5f7;
  --bg-soft: #eaecf1;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --border: #e4e6ec;
  --border-strong: #d3d7e0;
  --text: #191c22;
  --text-2: #5b6170;
  --text-3: #9aa0ad;
  --accent: #4b6bfb;
  --accent-2: #6f5cf1;
  --accent-soft: rgba(75, 107, 251, 0.10);
  --money: #0d9668;
  --money-soft: rgba(13, 150, 104, 0.10);
  --danger: #e5484d;
  --danger-soft: rgba(229, 72, 77, 0.10);
  --shadow-sm: 0 1px 2px rgba(17, 24, 39, 0.05);
  --shadow-md: 0 12px 32px -16px rgba(17, 24, 39, 0.28);
  --ring: 0 0 0 3px var(--accent-soft);
  --radius-lg: 14px;
  --radius-md: 10px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121419;
    --bg-soft: #1d2027;
    --panel: #1b1e25;
    --panel-2: #171a20;
    --border: #2a2e38;
    --border-strong: #3a3f4b;
    --text: #eceef2;
    --text-2: #a4aab6;
    --text-3: #6d7480;
    --accent: #6d8bff;
    --accent-2: #9a8cff;
    --accent-soft: rgba(109, 139, 255, 0.16);
    --money: #3ddc97;
    --money-soft: rgba(61, 220, 151, 0.13);
    --danger: #ff6b70;
    --danger-soft: rgba(255, 107, 112, 0.14);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 14px 34px -16px rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
textarea, input { -webkit-user-select: text; user-select: text; }

/* 布局：左侧常驻侧栏 + 右侧主区域 */
.app { height: 100vh; height: 100dvh; display: flex; }
.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; }

/* ---------------- 稿纸列表（左侧常驻，可折叠） ---------------- */
.sidebar {
  flex: none;
  width: 246px;
  height: 100vh;
  height: 100dvh;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: width 0.24s cubic-bezier(0.22, 0.61, 0.36, 1), border-color 0.24s ease;
}
.sidebar.collapsed { width: 0; border-right-color: transparent; }
.sidebar-inner {
  width: 246px;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 12px 10px;
}

/* 移动端抽屉遮罩（桌面端不显示） */
.scrim { display: none; }

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 8px 10px;
}
.sidebar-title { font-size: 12.5px; font-weight: 650; color: var(--text-2); letter-spacing: 0.3px; }
.sidebar-count { font-size: 11px; color: var(--text-3); }

.new-page-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  height: 34px;
  padding: 0 11px;
  margin-bottom: 8px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-2);
  font-size: 12.5px;
  font-weight: 550;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s ease;
  flex: none;
}
.new-page-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.new-page-btn svg { flex: none; }

.page-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 3px; padding-right: 2px; }
.page-list::-webkit-scrollbar { width: 8px; }
.page-list::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 8px;
  border: 2px solid var(--panel);
}

.page-item {
  position: relative;
  display: flex;
  align-items: center;
  border-radius: var(--radius-md);
  transition: background 0.13s ease;
}
.page-item:hover { background: var(--bg-soft); }
.page-item.active { background: var(--accent-soft); }
.page-item.active::before {
  content: "";
  position: absolute;
  left: 4px;
  top: 9px;
  bottom: 9px;
  width: 3px;
  border-radius: 2px;
  background: var(--accent);
}

.pi-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 2px 8px 13px;
  cursor: pointer;
}
.pi-main {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  min-width: 0;
}
.pi-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-item.active .pi-main { color: var(--accent); }
.pi-lock { display: inline-flex; align-items: center; flex: none; opacity: 0.8; }
.pi-sub {
  font-size: 11px;
  color: var(--text-3);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pi-actions {
  flex: none;
  display: flex;
  gap: 2px;
  padding-right: 6px;
  opacity: 0;
  transition: opacity 0.13s ease;
}
.page-item:hover .pi-actions,
.page-item.active .pi-actions { opacity: 1; }
.pi-btn {
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
  transition: all 0.13s ease;
}
.pi-btn:hover { background: rgba(0, 0, 0, 0.07); color: var(--text); }
.pi-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }
@media (prefers-color-scheme: dark) {
  .pi-btn:hover { background: rgba(255, 255, 255, 0.09); }
}

.rename-input {
  flex: 1;
  min-width: 0;
  margin: 8px 2px 8px 13px;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 0;
}

/* ---------------- 顶栏 ---------------- */
.topbar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-logo { width: 28px; height: 28px; border-radius: 8px; box-shadow: var(--shadow-sm); flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-name { font-size: 14px; font-weight: 650; letter-spacing: 0.2px; }
.brand-sub { font-size: 11px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.tools { display: flex; align-items: center; gap: 4px; flex: none; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--panel);
  color: var(--text-2);
  font-size: 12.5px;
  font-weight: 550;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.16s ease;
  white-space: nowrap;
}
.pill:hover { border-color: var(--accent); color: var(--accent); }
.pill-icon { font-weight: 700; font-size: 13px; }
.pill-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-3); transition: background 0.16s ease; }
.pill.on {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #fff;
  box-shadow: 0 5px 16px -8px var(--accent);
}
.pill.on .pill-dot { background: #fff; box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.22); }

.tool-sep { width: 1px; height: 18px; background: var(--border); margin: 0 6px; flex: none; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  transition: all 0.15s ease;
  flex: none;
}
.icon-btn svg { display: block; }
.icon-btn:hover { background: var(--bg-soft); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }
.icon-btn.accent { background: var(--accent-soft); color: var(--accent); }
.icon-btn.accent:hover { background: var(--accent); color: #fff; }
.icon-btn.on { background: var(--accent-soft); color: var(--accent); }
.hidden { display: none !important; }

/* ---------------- 计算区 ---------------- */
.pad {
  flex: 1;
  overflow-y: auto;
  padding: 10px 14px 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.row {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 11px 14px 9px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.row:focus-within { border-color: var(--accent); box-shadow: var(--ring), var(--shadow-sm); }
.row.just-added { animation: row-in 0.18s ease; }
@keyframes row-in {
  from { opacity: 0; transform: translateY(5px); }
  to { opacity: 1; transform: none; }
}

.row-main { display: flex; align-items: flex-end; gap: 12px; }
.row textarea {
  flex: 1;
  min-width: 60px;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  padding: 2px 0;
  max-height: 168px;
  overflow-y: auto;
}
.row textarea::placeholder { color: var(--text-3); }
.row textarea::-webkit-scrollbar { width: 8px; }
.row textarea::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 8px; }

.row-result {
  flex: none;
  max-width: 58%;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: 0.2px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  cursor: pointer;
  padding: 2px 7px;
  margin: -2px -7px -2px 0;
  border-radius: var(--radius-sm);
  transition: background 0.15s ease, color 0.15s ease;
  text-align: right;
  word-break: break-all;
}
.row-result:hover { background: var(--bg-soft); }
.row-result:active { background: var(--accent-soft); color: var(--accent); }
.row-result.empty { visibility: hidden; }
.row-result .eq { color: var(--text-3); font-weight: 500; margin-right: 6px; font-size: 0.78em; }

.row-fin {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
}
.fin-money {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
  font-size: 15px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.2px;
  color: var(--money);
  cursor: pointer;
  padding: 3px 9px;
  border-radius: var(--radius-sm);
  background: var(--money-soft);
  transition: filter 0.15s ease, transform 0.12s ease;
}
.fin-money:hover { filter: brightness(1.05); }
.fin-money:active { transform: scale(0.98); }
.fin-money .sym { font-size: 12px; font-weight: 700; opacity: 0.85; }
.fin-upper {
  font-size: 11px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  color: var(--text-2);
  background: var(--panel);
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.fin-upper:hover { border-color: var(--money); color: var(--money); background: var(--money-soft); }
.fin-upper:active { transform: scale(0.97); }

.pad.locked .row { background: var(--panel-2); }
.pad.locked .row textarea { opacity: 0.78; cursor: default; }

/* ---------------- 底部提示 ---------------- */
.hints {
  flex: none;
  padding: 7px 16px 9px;
  font-size: 11px;
  color: var(--text-3);
  display: flex;
  justify-content: center;
  gap: 10px;
}

/* ---------------- 计算器虚拟键盘（仅移动端显示） ---------------- */
.keypad {
  display: none;
  flex: none;
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  user-select: none;
  -webkit-user-select: none;
}

.keypad-head {
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding: 6px 8px 0;
}

/* 收起 / 展开：整条可点，高度加大以保证好点、看得见 */
.keypad-toggle {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--panel-2);
  color: var(--text-2);
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: background 0.12s ease, border-color 0.12s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.keypad-toggle:active { background: var(--border); }
.keypad-chevron {
  flex: none;
  color: var(--text-3);
  transition: transform 0.2s cubic-bezier(0.22, 0.61, 0.36, 1);
}
.keypad.collapsed .keypad-chevron { transform: rotate(-90deg); }
.keypad-title { flex: none; }
/* 当前作用行提示，如「第 2 行」 */
.keypad-target {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: right;
  color: var(--text-3);
  font-weight: 500;
}
/* 「收起 / 展开」动作提示，让按钮用途一目了然 */
.keypad-action {
  flex: none;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11.5px;
  font-weight: 600;
}

/* 按键震动开关 */
.keypad-haptic {
  flex: none;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--panel-2);
  color: var(--text-3);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.keypad-haptic:active { background: var(--border); }
.keypad-haptic.on {
  background: var(--accent-soft);
  border-color: transparent;
  color: var(--accent);
}

.keypad-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 46px;
  gap: 5px;
  padding: 0 8px 8px;
  overflow: hidden;
  /* 6 行 × 46px + 5 道间隙 × 5px + 底部内边距 8px = 309px，留些余量便于过渡 */
  max-height: 320px;
  transition: max-height 0.22s cubic-bezier(0.22, 0.61, 0.36, 1),
              padding-bottom 0.22s ease, opacity 0.16s ease;
}
.keypad.collapsed .keypad-grid {
  max-height: 0;
  padding-bottom: 0;
  opacity: 0;
  pointer-events: none;
}

.key {
  border: none;
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--text);
  font-family: inherit;
  font-size: 19px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  transition: background 0.1s ease, transform 0.06s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.key:active { transform: scale(0.96); background: var(--border); }
.key-op { color: var(--accent); font-weight: 600; background: var(--accent-soft); }
.key-op:active { background: var(--accent); color: #fff; }
.key-fn { color: var(--text-2); font-size: 16px; font-weight: 600; }
.key-enter {
  grid-column: span 4;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 14.5px;
  font-weight: 600;
}
.key-enter:active { filter: brightness(1.08); }

/* 移动端提示语与桌面端不同 */
.hint-mobile { display: none; }
@media (max-width: 720px) {
  .hint-desktop { display: none; }
  .hint-mobile { display: inline; }
}

/* ---------------- Toast ---------------- */
.toasts {
  position: fixed;
  left: 50%;
  bottom: 30px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
  z-index: 50;
}
.toast {
  background: rgba(22, 24, 30, 0.93);
  color: #fff;
  font-size: 12.5px;
  padding: 8px 15px;
  border-radius: 999px;
  box-shadow: var(--shadow-md);
  animation: toast-in 0.18s ease;
  max-width: 78vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
.toast.out { opacity: 0; transform: translateY(6px); transition: all 0.3s ease; }

/* ---------------- 滚动条 ---------------- */
.pad::-webkit-scrollbar { width: 10px; }
.pad::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 8px;
  border: 3px solid var(--bg);
}
.pad::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

/* ---------------- 窄窗口适配 ---------------- */
@media (max-width: 720px) {
  .topbar {
    flex-wrap: wrap;
    padding: 8px 10px;
    padding-top: calc(8px + env(safe-area-inset-top));
    row-gap: 8px;
  }
  .topbar-left { flex: 1 1 auto; min-width: 0; }
  .tools { flex: 1 1 auto; justify-content: flex-end; }
  .brand-sub { display: none; }

  .pad { padding: 8px 10px 16px; }

  .hints {
    padding: 6px 12px;
    padding-bottom: calc(6px + env(safe-area-inset-bottom));
    text-align: center;
  }

  .row { padding: 10px 12px 8px; border-radius: 12px; }
  .row-main { gap: 8px; }
  .row-result { font-size: 18px; max-width: 62%; }

  /* 移动端启用虚拟键盘 */
  .keypad { display: flex; flex-direction: column; }
  /* 键盘展开时压缩输入区最小高度，保证仍能看到若干行 */
  .pad { min-height: 0; }

  /* 侧栏改为抽屉：脱离文档流，从左侧滑入 */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 40;
    width: 264px;
    height: 100dvh;
    box-shadow: var(--shadow-md);
    transform: translateX(0);
    transition: transform 0.24s cubic-bezier(0.22, 0.61, 0.36, 1);
  }
  .sidebar.collapsed {
    width: 264px;
    border-right-color: var(--border);
    transform: translateX(-100%);
  }
  .sidebar-inner {
    width: 264px;
    padding-top: calc(12px + env(safe-area-inset-top));
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }
  /* 抽屉展开时显示遮罩 */
  .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 39;
    background: rgba(10, 12, 16, 0.42);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.24s ease;
  }
  .scrim.on { opacity: 1; pointer-events: auto; }
  /* 触屏设备上操作按钮常显，不依赖 hover */
  .pi-actions { opacity: 1; }
  .pi-btn { width: 28px; height: 28px; }
}

@media (max-width: 400px) {
  .pill { padding: 0 9px; }
  .icon-btn { width: 28px; height: 28px; }
  .tool-sep { margin: 0 3px; }
  .brand-name { font-size: 13px; }
}
