/* ===== 基础重置与变量 ===== */
:root {
  --bg: #f5f0eb;
  --card-bg: #ffffff;
  --text-primary: #4a4a4a;
  --text-secondary: #8a8a8a;
  --text-muted: #b0a8a0;
  --border: #e8e2dc;
  --accent: #b8a9c0;
  --accent-light: #d4c8db;
  --success: #a3b5a0;
  --danger: #c9a0a0;
  --shadow: 0 2px 12px rgba(0,0,0,0.06);
  --radius: 12px;
  --radius-sm: 8px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
    'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100vh;
  padding-bottom: calc(80px + env(safe-area-inset-bottom, 16px));
  -webkit-font-smoothing: antialiased;
}

/* ===== 顶部导航 ===== */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}

.app-title-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.app-title-row .icon-btn {
  padding: 2px 4px;
}

.app-title-row .icon-btn svg {
  width: 14px;
  height: 14px;
  vertical-align: middle;
}

.top-icons {
  display: flex;
  gap: 8px;
  position: relative;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  transition: background 0.2s;
}

.icon-btn:hover, .icon-btn:active {
  background: var(--border);
}

/* ===== 存档子菜单 ===== */
.sub-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 8px 0;
  min-width: 160px;
  z-index: 1000;
  animation: fadeSlideIn 0.15s ease;
}

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

.sub-menu-group {
  padding: 4px 0;
}

.sub-menu-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 4px 14px 6px;
}

.sub-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-primary);
  padding: 8px 14px;
  transition: background 0.15s;
}

.sub-menu-item:hover, .sub-menu-item:active {
  background: var(--border);
}

.sub-menu-divider {
  height: 1px;
  background: var(--border);
  margin: 6px 0;
}

/* ===== 月度总览 ===== */
.section-overview {
  background: var(--card-bg);
  margin: 16px;
  padding: 24px 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.overview-main {
  text-align: center;
  margin-bottom: 20px;
}

.remaining-amount {
  font-size: 32px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.5px;
}

.remaining-hint {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
  margin-bottom: 4px;
}

.overview-sub {
  margin-top: 6px;
  font-size: 13px;
}

.text-muted {
  color: var(--text-muted);
}

.divider {
  color: var(--border);
  margin: 0 8px;
}

.overview-row {
  display: flex;
  justify-content: space-between;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.overview-item {
  text-align: center;
  cursor: pointer;
  flex: 1;
  min-width: 0;
}

.overview-item .label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.overview-item .value {
  font-size: 15px;
  font-weight: 600;
}

.fixed-detail {
  margin-top: 12px;
  padding: 12px;
  background: var(--bg);
  border-radius: var(--radius-sm);
}

.fixed-detail ul {
  list-style: none;
  font-size: 13px;
}

.fixed-detail li {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  color: var(--text-secondary);
}

/* ===== 今日建议 ===== */
.section-today {
  background: var(--card-bg);
  margin: 0 16px 16px;
  padding: 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: center;
}

.today-label {
  font-size: 13px;
  color: var(--text-muted);
}

.today-amount {
  display: block;
  font-size: 22px;
  font-weight: 700;
  margin-top: 4px;
  color: var(--accent);
  word-break: break-all;
}

.yesterday-info {
  margin-top: 10px;
  font-size: 13px;
}

.yesterday-spent {
  color: var(--text-secondary);
  font-weight: 500;
}

/* ===== 项目方块 ===== */
.section-projects {
  padding: 0 16px 16px;
}

.projects-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 10px;
  min-width: 0;
}

.project-card-wrapper {
  display: flex;
  flex-direction: column;
}

.project-card {
  border-radius: var(--radius);
  padding: 12px;
  position: relative;
  box-shadow: var(--shadow);
  transition: transform 0.15s;
  overflow: hidden;
  height: fit-content;
}

.project-card:active {
  transform: scale(0.97);
}

.project-card .proj-name {
  font-size: 11px;
  font-weight: 500;
  opacity: 0.8;
  margin-bottom: 4px;
}

.project-card .proj-amount {
  font-size: 14px;
  font-weight: 700;
  word-break: break-all;
}

.project-card .proj-amount-input {
  font-size: 14px;
  font-weight: 700;
  width: 100%;
  border: none;
  border-bottom: 2px solid rgba(0,0,0,0.15);
  background: transparent;
  outline: none;
  color: inherit;
}

.project-card .proj-monthly {
  font-size: 10px;
  opacity: 0.6;
  margin-top: 4px;
}

.proj-monthly.hidden {
  display: none;
}

/* ===== 底部操作区 ===== */
.section-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 16px));
  z-index: 100;
}

.action-row {
  display: flex;
  gap: 8px;
  align-items: center;
  width: 100%;
  box-sizing: border-box;
}

.action-row .btn-group {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
  width: auto;
  max-width: 50%;
  box-sizing: border-box;
}

.action-row .btn-group .btn {
  flex: 1 1 auto;
  width: auto;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.action-row .btn-group .btn:first-child {
  min-width: 56px;
}

.action-row .btn-group .btn:last-child {
  min-width: 44px;
  flex: 0 0 auto;
}

.balance-input {
  flex: 1 1 0;
  min-width: 0;
  height: 44px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: var(--bg);
  outline: none;
  transition: border-color 0.2s;
}

.balance-input:focus {
  border-color: var(--accent);
}

.remaining-days-input {
  width: 88px;
  height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: var(--bg);
  outline: none;
  transition: border-color 0.2s;
  flex-shrink: 0;
}

.remaining-days-input:focus {
  border-color: var(--accent);
}

/* ===== 按钮 ===== */
.btn {
  box-sizing: border-box;
  height: 44px;
  padding: 0 12px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
  white-space: nowrap;
  flex: 1 1 0;
  min-width: 0;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-secondary {
  background: var(--border);
  color: var(--text-primary);
}

.btn-block {
  display: block;
  width: 100%;
  margin-top: 10px;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
  background: var(--border);
  color: var(--text-secondary);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  margin-top: 8px;
}

/* ===== 弹窗 ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  padding-top: env(safe-area-inset-top, 0px);
  background: rgba(0,0,0,0.35);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.2s;
}

.modal {
  background: var(--card-bg);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 500px;
  max-height: 85vh;
  max-height: calc(85vh - env(safe-area-inset-top, 0px));
  overflow-y: auto;
  animation: slideUp 0.25s;
}

.modal-sm {
  max-height: 55vh;
}

.modal-lg {
  max-height: 90vh;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 20px 10px;
  position: sticky;
  top: 0;
  background: var(--card-bg);
  z-index: 1;
}

.modal-header h2 {
  font-size: 18px;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 8px;
}

.modal-body {
  padding: 10px 20px 30px;
}

.date-range-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.date-range-row .input-full {
  flex: 1;
  margin-bottom: 0;
}

.date-sep {
  color: var(--text-muted);
  font-size: 14px;
  flex-shrink: 0;
}

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

.reset-text {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary);
  margin-bottom: 12px;
}

.reset-warning {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 20px;
}

.modal-footer {
  display: flex;
  gap: 10px;
}

.modal-footer .btn {
  flex: 1;
}

/* ===== 存钱罐 ===== */
.piggy-total {
  text-align: center;
  padding: 20px 0;
}

.piggy-total .label {
  font-size: 13px;
  color: var(--text-muted);
  display: block;
}

.piggy-amount {
  font-size: 28px;
  font-weight: 700;
  color: var(--accent);
  word-break: break-all;
}

.piggy-records h3 {
  font-size: 14px;
  margin: 20px 0 10px;
  color: var(--text-secondary);
}

.piggy-records ul {
  list-style: none;
}

.piggy-records li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.piggy-records .record-info {
  display: flex;
  flex-direction: column;
}

.piggy-records .record-date {
  font-size: 11px;
  color: var(--text-muted);
}

.piggy-records .record-amount-in {
  color: var(--success);
  font-weight: 600;
}

.piggy-records .record-amount-out {
  color: var(--danger);
  font-weight: 600;
}

.piggy-title-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.piggy-reset-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, color 0.2s;
}

.piggy-reset-btn:hover {
  background: var(--bg);
  color: var(--danger);
}

/* ===== 设置页 ===== */
.settings-body {
  padding-bottom: 40px;
}

.setting-group {
  margin-bottom: 20px;
}

.setting-group > label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
}

.input-full {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: var(--bg);
  outline: none;
  margin-bottom: 8px;
}

.input-full:focus {
  border-color: var(--accent);
}

.fixed-item, .project-item {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

.fixed-item input, .project-item input {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  outline: none;
  background: var(--bg);
}

.fixed-item input:focus, .project-item input:focus {
  border-color: var(--accent);
}

.fixed-item .input-name, .project-item .input-name {
  flex: 1;
}

.fixed-item .input-amount {
  width: 90px;
}

.project-item .input-percent {
  width: 60px;
}

.project-item .color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid var(--border);
  cursor: pointer;
  flex-shrink: 0;
}

.btn-remove {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.percent-hint {
  font-size: 12px;
  margin-top: 6px;
  color: var(--text-muted);
}

.percent-hint.error {
  color: var(--danger);
}

/* ===== 颜色选择 ===== */
.color-palette {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  padding: 8px;
}

.color-option {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
  justify-self: center;
}

.color-option:hover, .color-option.selected {
  transform: scale(1.15);
  border-color: var(--text-primary);
}

/* ===== 报表 ===== */
.report-text {
  margin-top: 16px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 16px;
}

.report-text pre {
  white-space: pre-wrap;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-primary);
  font-family: inherit;
}

/* ===== 工具类 ===== */
.hidden {
  display: none !important;
}

/* ===== 动画 ===== */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

/* ===== 小屏适配 (360px - 500px) ===== */
@media (max-width: 500px) and (min-width: 361px) {
  .projects-grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 8px;
  }
  .project-card { padding: 8px; }
  .project-card .proj-amount { font-size: 12px; }
  .project-card .proj-amount-input { font-size: 12px; }
  .project-card .proj-name { font-size: 10px; }
}

/* ===== 超小屏适配 (320px - 360px) ===== */
@media (max-width: 360px) {
  .remaining-amount { font-size: 28px; }
  .today-amount { font-size: 20px; }
  .piggy-amount { font-size: 24px; }
  .section-overview { margin: 12px; padding: 18px 14px; }
  .section-today { margin: 0 12px 12px; padding: 16px; }
  .section-projects { padding: 0 12px 12px; }
  .projects-grid { gap: 6px; }
  .project-card { padding: 8px; }
  .project-card .proj-amount { font-size: 12px; }
  .project-card .proj-amount-input { font-size: 12px; }
  .overview-item .value { font-size: 13px; }
  .modal { max-height: 80vh; }
  .color-palette { grid-template-columns: repeat(4, 1fr); }
  .action-row { gap: 6px; }
  .action-row .btn-group { gap: 6px; max-width: 50%; width: auto; }
  .action-row .btn-group .btn { flex: 1 1 auto; max-width: 100%; }
  .action-row .btn-group .btn:first-child { min-width: 52px; }
  .action-row .btn-group .btn:last-child { min-width: 44px; flex: 0 0 auto; }
  .balance-input {
    padding: 8px 10px;
    height: 42px;
  }
  .btn {
    padding: 0 8px;
    font-size: 13px;
    height: 42px;
  }
}

/* ===== 极窄屏适配 (< 320px) ===== */
@media (max-width: 320px) {
  .action-row {
    flex-wrap: wrap;
    gap: 6px;
  }
  .balance-input {
    width: 100%;
    flex: none;
    margin-bottom: 4px;
    height: 42px;
  }
  .action-row .btn-group {
    width: 100%;
    max-width: 100%;
    flex-shrink: 0;
  }
  .action-row .btn-group .btn {
    flex: 1 1 calc(50% - 3px);
    min-width: calc(50% - 3px);
    height: 42px;
  }
  .action-row .btn-group .btn:last-child {
    flex: 0 0 auto;
    min-width: 44px;
  }
}

/* ===== 响应式 ===== */
@media (min-width: 500px) {
  .projects-grid {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  }
  .modal {
    border-radius: 20px;
    margin: auto;
    max-width: 460px;
  }
  .modal-overlay {
    align-items: center;
  }
}
