/* ============================================================================
 * 哈皮点金 精修预览版 - 部件
 * ----------------------------------------------------------------------------
 * 面板、按钮、输入框、下拉框、标签、表格骨架、提示条、弹窗、空状态。
 * 颜色、字号只引用 tokens.css 的变量。
 * ========================================================================= */

/* ------------------------------------------------------------ 面板 */

.panel,
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-panel);
  padding: var(--pad-panel);
}

.panel-head {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  flex-wrap: wrap;
  margin-bottom: var(--space-6);
  min-height: var(--space-6);
}

/* ------------------------------------------------------------ 按钮 */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: var(--h-btn);
  padding: 0 var(--btn-pad);
  border: 1px solid transparent;
  border-radius: var(--r-control);
  font-size: var(--fs-ui);
  font-weight: var(--fw-title);
  line-height: var(--lh-text);
  white-space: nowrap;
  transition: background-color var(--dur-fast) var(--ease-out),
    border-color var(--dur-fast) var(--ease-out),
    color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:active {
  transform: scale(0.98);
  transition-duration: var(--dur-press);
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
  transform: none;
}

.btn-primary {
  background: var(--cyan);
  color: var(--cyan-ink);
}

.btn-primary:hover {
  background: var(--cyan-bright);
}

.btn-ghost {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--text);
}

.btn-ghost:hover {
  border-color: var(--line-hover);
  background: var(--hover-fill);
}

.btn-sm {
  height: var(--h-btn-sm);
  padding: 0 var(--btn-pad-sm);
}

.btn .icon {
  width: var(--space-4);
  height: var(--space-4);
}

/* 图标按钮：行尾删除、编辑、关闭 */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--icon-btn);
  height: var(--icon-btn);
  border: 1px solid transparent;
  border-radius: var(--r-control);
  color: var(--text-3);
  transition: background-color var(--dur-fast) var(--ease-out),
    border-color var(--dur-fast) var(--ease-out),
    color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

.icon-btn:hover {
  background: var(--hover-fill);
  color: var(--text);
}

.icon-btn:active {
  transform: scale(0.98);
  transition-duration: var(--dur-press);
}

.icon-btn.is-danger:hover {
  background: var(--expense-soft);
  color: var(--expense);
}

.icon-btn .icon {
  width: var(--space-4);
  height: var(--space-4);
}

/* ------------------------------------------------------------ 输入框 */

.input,
.select-wrap select {
  display: block;
  width: 100%;
  height: var(--h-input);
  padding: 0 var(--input-pad);
  background: var(--input-bg);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-control);
  color: var(--text);
  font-size: var(--fs-ui);
  font-weight: var(--fw-text);
  line-height: var(--lh-text);
  transition: border-color var(--dur-base) var(--ease-out),
    background-color var(--dur-base) var(--ease-out);
}

.input::placeholder {
  color: var(--text-3);
}

.input:focus,
.select-wrap select:focus {
  border-color: var(--cyan);
  box-shadow: 0 0 0 var(--focus-w) var(--ring);
  outline: none;
}

.input.is-error,
.select-wrap select.is-error {
  border-color: var(--expense);
}

.select-wrap {
  position: relative;
  display: block;
}

.select-wrap select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: calc(var(--input-pad) + var(--space-8));
  cursor: pointer;
}

.select-wrap::after {
  content: "";
  position: absolute;
  right: var(--input-pad);
  top: 50%;
  width: var(--space-2);
  height: var(--space-2);
  margin-top: -6px;
  border-right: 1.75px solid var(--text-3);
  border-bottom: 1.75px solid var(--text-3);
  transform: rotate(45deg);
  pointer-events: none;
}

.select-wrap.is-error select {
  border-color: var(--expense);
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-width: 0;
}

.field-label {
  color: var(--text-2);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
}

/* ------------------------------------------------------------ 标签 */

.tag {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  max-width: 100%;
  padding: var(--space-4);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-tag);
  color: var(--text-2);
  font-size: var(--fs-ui);
  line-height: 1;
}

.tag-name {
  min-width: 0;
  color: var(--text-2);
  font-size: var(--fs-ui);
  line-height: 1;
  text-align: left;
  overflow-wrap: anywhere;
  transition: color var(--dur-fast) var(--ease-out);
}

.tag-name:hover {
  color: var(--cyan);
}

.tag-del {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  /* 视觉占位仍是 20x20，热区 28x28；负外边距把多出来的 8px 从布局里吃掉，
     标签高度不会因此变高 */
  width: calc(var(--icon-size) + var(--space-2));
  height: calc(var(--icon-size) + var(--space-2));
  margin: calc(-1 * var(--space-1));
  border-radius: var(--r-tag);
  color: var(--text-3);
  transition: color var(--dur-fast) var(--ease-out), background-color var(--dur-fast) var(--ease-out);
}

.tag-del:hover {
  color: var(--expense);
  background: var(--expense-soft);
}

.tag-del .icon {
  width: var(--space-4);
  height: var(--space-4);
}

.status-tag {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  padding: var(--space-4);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-tag);
  color: var(--text-3);
  font-size: var(--fs-ui);
  line-height: 1;
  white-space: nowrap;
}

.status-tag.is-active {
  color: var(--income);
}

/* ------------------------------------------------------------ 范围切换、图例、小开关 */

.range {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-left: auto;
  white-space: nowrap;
}

.range-btn {
  position: relative;
  padding: var(--space-1) var(--space-2);
  color: var(--text-3);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
  transition: color var(--dur-base) var(--ease-out);
}

.range-btn:hover {
  color: var(--text);
}

.range-btn.is-active {
  color: var(--cyan);
}

.legend {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-4);
  color: var(--text-2);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
}

.legend li {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.swatch {
  display: inline-block;
  flex: 0 0 auto;
  width: var(--swatch-size);
  height: var(--swatch-size);
  border-radius: 2px;
  background: var(--c, var(--text-3));
}

.sw-quark { --c: var(--p-quark); }
.sw-task { --c: var(--p-task); }
.sw-know { --c: var(--p-know); }
.sw-none { visibility: hidden; }

.sw-line {
  width: var(--space-4);
  height: var(--nav-ink);
  border-radius: 0;
  background: var(--income);
}

.sw-bar {
  width: var(--space-2);
  height: var(--space-3);
  border-radius: 0;
  background: var(--income);
  opacity: 0.35;
}

.switch {
  display: flex;
  align-items: center;
  gap: var(--space-6);
  white-space: nowrap;
}

.switch button {
  position: relative;
  padding: var(--space-1) 0;
  color: var(--text-3);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
  transition: color var(--dur-base) var(--ease-out);
}

.switch button:hover {
  color: var(--text);
}

/* 选中态只用颜色区分：和范围切换一样用青色，不画下划线。 */
.switch button.is-on {
  color: var(--cyan);
}

/* ------------------------------------------------------------ 表格骨架 */

/* 表体在卡片里滚动；电脑和平板上页面本身不滚动（见 pages.css） */
.table-scroll {
  overflow: auto;
}

.tbl-inner {
  min-width: var(--tbl-min, 0);
}

.tbl-head,
.tbl-row {
  display: grid;
  grid-template-columns: var(--cols);
  align-items: center;
}

/* 表头固定在滚动区的顶部，底色和面板一样，盖住从下面滚过去的行 */
.tbl-head {
  position: sticky;
  top: 0;
  z-index: 3;
  padding-bottom: var(--th-gap);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  color: var(--th-color);
  font-size: var(--fs-ui);
  font-weight: var(--fw-text);
  line-height: var(--lh-text);
  white-space: nowrap;
}

/* 所有表头一个样子：同一个颜色、常规字重、16px（“日期”也是） */
.tbl-head .c,
.tbl-head .c.num,
.tbl-head .c.date {
  color: var(--th-color);
  font-family: var(--font-cn);
  font-size: var(--fs-ui);
  font-weight: var(--fw-text);
  line-height: var(--lh-text);
  font-variant-numeric: normal;
}

.tbl-row {
  height: var(--h-row);
  transition: background-color var(--dur-base) var(--ease-out);
}

.tbl-row + .tbl-row {
  border-top: 1px solid var(--line);
}

.tbl-row:hover {
  background: var(--panel-hover);
}

.c {
  min-width: 0;
  padding: 0 var(--cell-pad);
  text-align: center;
}

.c.num {
  font-family: var(--font-num);
  font-size: var(--fs-table);
  font-weight: var(--fw-num);
  line-height: var(--lh-num);
  font-variant-numeric: lining-nums tabular-nums;
  color: var(--text);
}

.c.date {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--panel);
  font-family: var(--font-num);
  font-size: var(--fs-table);
  font-weight: var(--fw-num);
  line-height: var(--lh-num);
  font-variant-numeric: lining-nums tabular-nums;
  color: var(--text-2);
}

/* 表头里的日期格：既跟着表头固定在顶上，又跟着日期列固定在左边 */
.tbl-head .c.date {
  z-index: 4;
  background: var(--panel);
}

.tbl-row:hover .c.date {
  background: var(--panel-hover);
}

.c.note {
  text-align: left;
  overflow: hidden;
}

.c.note .cell-text {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-2);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
}

.c .cell-text {
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: middle;
}

/* 可编辑的格子：平时是数字（空的格子留空），悬停出现虚线下划线。
   空格子没有字，靠 44px 的最小高度保证点得到。 */
.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: var(--h-btn);
  font: inherit;
  color: inherit;
  cursor: text;
}

.cell .cell-text {
  border-bottom: 1px dashed transparent;
  padding-bottom: 2px;
  transition: border-color var(--dur-fast) var(--ease-out);
}

.cell:hover .cell-text,
.cell:focus-visible .cell-text {
  border-bottom-color: var(--line-hover);
}

.cell.is-note {
  justify-content: flex-start;
}

.cell-input {
  width: 100%;
  height: var(--h-input);
  padding: 0 var(--space-3);
  background: var(--input-bg);
  border: 1px solid var(--cyan);
  border-radius: var(--r-control);
  color: var(--text);
  font-family: var(--font-num);
  font-size: var(--fs-table);
  font-weight: var(--fw-num);
  line-height: var(--lh-num);
  font-variant-numeric: lining-nums tabular-nums;
  text-align: center;
}

.cell-input:focus {
  box-shadow: 0 0 0 var(--focus-w) var(--ring);
  outline: none;
}

.cell-input.is-error {
  border-color: var(--expense);
  box-shadow: 0 0 0 var(--focus-w) var(--expense-soft);
}

.cell-input.is-note {
  font-family: var(--font-cn);
  font-size: var(--fs-ui);
  font-weight: var(--fw-text);
  text-align: left;
}

/* 保存成功后，格子里的数字闪一下浅青（只闪数字那一小块，600ms 后消失） */
.cell.is-saved .cell-text {
  animation: cellFlash 600ms var(--ease-out);
}

@keyframes cellFlash {
  0% { background: var(--cyan-soft); }
  100% { background: transparent; }
}

/* ------------------------------------------------------------ 空状态 */

.empty {
  padding: var(--space-12) var(--space-4);
  color: var(--text-2);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
  text-align: center;
}

/* ------------------------------------------------------------ 提示条 */

.toast {
  position: fixed;
  left: 50%;
  bottom: var(--toast-bottom);
  z-index: 400;
  max-width: min(var(--toast-max), calc(100vw - var(--space-8)));
  padding: var(--space-3) var(--space-4);
  background: var(--popover);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-panel);
  color: var(--text);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transform: translate(-50%, var(--space-4));
  transition: opacity var(--dur-base) var(--ease-out), transform var(--dur-base) var(--ease-out),
    visibility var(--dur-base) linear;
}

.toast.is-in {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
}

.toast.is-out {
  opacity: 0;
  transform: translate(-50%, var(--space-2));
  transition-duration: var(--dur-out);
}

/* ------------------------------------------------------------ 确认弹窗 */

.modal-mask {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: var(--mask);
  opacity: 0;
  transition: opacity var(--dur-base) var(--ease-out);
}

.modal-mask.is-on {
  opacity: 1;
}

.modal {
  width: 100%;
  max-width: var(--modal-w);
  padding: var(--space-6);
  background: var(--popover);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-panel);
  opacity: 0;
  transform: scale(0.96);
  transition: opacity var(--dur-base) var(--ease-out), transform var(--dur-base) var(--ease-out);
}

.modal-mask.is-on .modal {
  opacity: 1;
  transform: scale(1);
}

.modal-text {
  color: var(--text);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  margin-top: var(--space-6);
}

/* ------------------------------------------------------------ 加载小圆环、对勾 */

.spinner {
  display: inline-block;
  width: var(--space-4);
  height: var(--space-4);
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.check-icon {
  display: inline-block;
  width: var(--space-4);
  height: var(--space-4);
}

/* ------------------------------------------------------------ 页面标题行 */

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.title-wrap {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.head-actions {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex: 0 0 auto;
}

.date-pop {
  position: absolute;
  right: 0;
  top: calc(100% + var(--space-2));
  z-index: 30;
  width: max-content;
  max-width: calc(100vw - var(--space-8));
  padding: var(--space-3);
  background: var(--popover);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-panel);
  animation: popIn var(--dur-fast) var(--ease-out);
}

@keyframes popIn {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.date-pop[hidden] {
  display: none;
}

/* 手机：系统自带的日期选择 */
.date-native {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.date-native[hidden] {
  display: none;
}

.date-pop .input {
  width: 180px;
  min-width: 0;
}

/* 电脑和平板：自己做的深色小日历。
   上面是上个月、年月、下个月；中间是日期格子（周一在最左）；下面一行“今天”。 */
.calendar {
  width: 328px;
}

.calendar[hidden] {
  display: none;
}

.cal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
}

/* 日历的翻月按钮：热区仍是 32×32，图标 18px、第二级文字色，
   悬停和键盘聚焦变青色，让人看出能点。 */
.cal-head .icon-btn {
  color: var(--text-2);
}

.cal-head .icon-btn .icon {
  width: 18px;
  height: 18px;
}

.cal-head .icon-btn:hover {
  color: var(--cyan);
}

.cal-head .icon-btn:focus-visible {
  color: var(--cyan);
}

.cal-title {
  color: var(--text);
  font-size: var(--fs-ui);
  font-weight: var(--fw-title);
  line-height: var(--lh-text);
  text-align: center;
}

.cal-week {
  display: grid;
  grid-template-columns: repeat(7, 40px);
  gap: var(--space-1);
  margin-bottom: var(--space-1);
  color: var(--th-color);
  font-size: var(--fs-ui);
  line-height: var(--lh-text);
  text-align: center;
}

.cal-days {
  display: grid;
  grid-template-columns: repeat(7, 40px);
  gap: var(--space-1);
}

.cal-day {
  height: 40px;
  border-radius: var(--r-control);
  color: var(--text);
  font-family: var(--font-num);
  font-size: var(--fs-ui);
  font-weight: var(--fw-num);
  line-height: 1;
  font-variant-numeric: lining-nums tabular-nums;
  transition: background-color var(--dur-fast) var(--ease-out),
    color var(--dur-fast) var(--ease-out),
    box-shadow var(--dur-fast) var(--ease-out);
}

.cal-day.is-other {
  color: var(--text-3);
}

.cal-day:hover {
  background: var(--hover-fill);
}

.cal-day.is-today:not(.is-selected) {
  color: var(--cyan);
  box-shadow: inset 0 0 0 1px var(--cyan);
}

.cal-day.is-selected,
.cal-day.is-selected:hover {
  background: var(--cyan);
  color: var(--cyan-ink);
}

.cal-foot {
  display: flex;
  justify-content: center;
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--line);
}

.cal-foot .btn {
  width: 100%;
}

/* ------------------------------------------------------------ 平板、手机调整 */

@media (max-width: 1279px) {
  .tbl-row {
    height: var(--h-row-tight);
  }
}

/* ------------------------------------------------------------ 窄平板（768–1023）：表格压进首屏
   格子内边距缩到 4px、表头和日期降到 15px，五张表在 768 和 810 宽度下
   都不出横向滚动。万一还是超宽（以后分类加多了）：操作列固定在可视右缘
   （position: sticky），卡片右缘出一道很淡的渐隐提示——卡片上有
   has-overflow 类时才显示（tables.js/quark.js/toys.js 的 syncOverflow 挂）。 */
@media (min-width: 768px) and (max-width: 1023px) {
  .tbl-head .c,
  .tbl-row .c {
    padding: 0 4px;
  }

  .tbl-head .c {
    font-size: 15px;
  }

  .c.date {
    font-size: 15px;
  }

  /* 录入行里的原生日期控件（情趣用品窄档）：字号 15px 让年月日完整显示 */
  .tbl-row.is-form input[type="date"] {
    font-size: 15px;
  }

  /* 录入行和表头、数据行共用同一套列宽（toys.js 的窄档模板），这里不再单独给列宽。
     窄列里的下拉：左右内边距收一点，箭头跟着靠右一点，“支出/收入”和分类名才露得出来。
     pages.css 里同名的规则后加载，这里多带一层 body.is-list 才盖得住 */
  body.is-list .tbl-row.is-form .select-wrap select {
    padding-left: 8px;
    padding-right: 24px;
  }

  body.is-list .tbl-row.is-form .select-wrap::after {
    right: 8px;
  }

  /* 操作列固定在可视右缘，不跟着横向滑动跑掉 */
  .tbl-head .c:last-child,
  .tbl-row .c:last-child {
    position: sticky;
    right: 0;
    z-index: 1;
    background: var(--panel);
  }

  .tbl-row:hover .c:last-child {
    background: var(--panel-hover);
  }

  /* 表头的操作格是空的，按内容算高度是 0：固定列在表头里根本盖不住东西，
     滑过来的表头文字会从它底下露出来、被滚动区边缘硬切掉。让它撑满表头一行的高度。 */
  .tbl-head .c:last-child {
    align-self: stretch;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .tbl-row.is-hit .c:last-child,
  .tbl-row.is-hit:hover .c:last-child {
    background: transparent;
  }

  body.is-list #tblCard {
    position: relative;
  }

  body.is-list #tblCard.has-overflow::after {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 28px;
    z-index: 2; /* 渐隐要盖在固定于右缘的操作列（z-index 1）之上才看得见 */
    background: linear-gradient(to left, var(--panel) 6px, var(--panel-alpha0));
    pointer-events: none;
  }

  /* 渐隐的主体：紧贴操作列左侧、跟着操作列一起固定在可视右缘。
     表格区域以外（卡片右边的留白）没有内容可隐，只靠上面那道看不出效果；
     这一道盖在会横向滑动的最后一列内容上，文字滑到这里逐渐隐进面板色，
     一眼看得出右边还有内容。底色用不透明的面板色，靠遮罩渐变到透明，
     悬停行换底色时也跟着对得上。不挡点击（pointer-events: none）。 */
  body.is-list #tblCard.has-overflow .tbl-head .c:last-child::before,
  body.is-list #tblCard.has-overflow .tbl-row .c:last-child::before {
    content: "";
    position: absolute;
    right: 100%;
    top: 0;
    bottom: 0;
    width: 56px;
    background: var(--panel);
    /* 离操作列近的地方几乎全遮，往外逐渐透出来：正常大小下也看得出边缘在变暗 */
    -webkit-mask-image: linear-gradient(to left, #000, rgba(0, 0, 0, 0.6) 40%, transparent);
    mask-image: linear-gradient(to left, #000, rgba(0, 0, 0, 0.6) 40%, transparent);
    pointer-events: none;
  }

  body.is-list #tblCard.has-overflow .tbl-row:hover .c:last-child::before {
    background: var(--panel-hover);
  }

  /* 刚添加的那一行高亮时操作格是透明的，这一道也收起来，不遮高亮 */
  body.is-list #tblCard.has-overflow .tbl-row.is-hit .c:last-child::before,
  body.is-list #tblCard.has-overflow .tbl-row.is-hit:hover .c:last-child::before {
    content: none;
  }
}

@media (max-width: 767px) {
  .panel-head {
    margin-bottom: var(--space-4);
  }

  .page-head {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-3);
  }

  .head-actions {
    width: 100%;
  }

  .head-actions .btn-primary {
    width: 100%;
  }

  .date-pop {
    left: 0;
    right: auto;
    width: 100%;
  }

  .date-pop .input {
    width: auto;
    flex: 1 1 auto;
  }

  .toast {
    bottom: var(--space-6);
  }
}
