:root {
  --panel: #fff5cf;
  --paper: #fffdf0;
  --ink: #171717;
  --line: #111;
  --blue: #2388ff;
  --cyan: #5ad8ff;
  --green: #20c865;
  --red: #ff4a59;
  --yellow: #ffd447;
  --muted: #6d6657;
  --shadow: #030712;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-width: 1180px;
  min-height: 100vh;
  background: linear-gradient(90deg, rgba(255,255,255,.05) 1px, transparent 1px), linear-gradient(rgba(255,255,255,.05) 1px, transparent 1px), linear-gradient(135deg,#151c2c,#334362 60%,#111827);
  background-size: 24px 24px, 24px 24px, auto;
  color: var(--ink);
}
button, input { font: inherit; }
input { font-size: 16px; }
button { cursor: pointer; color: inherit; touch-action: manipulation; }

.mobile-appbar,
.mobile-bottom-nav { display: none; }

.app-shell { height: 100vh; display: grid; grid-template-columns: 220px minmax(720px,1fr); gap: 20px; padding: 16px; }
.card, .pixel-btn, .searchbar, .modal { border: 4px solid var(--line); box-shadow: 8px 8px 0 var(--shadow); }
.sidebar { color: #fff; display: flex; flex-direction: column; gap: 18px; }
.brand { display: flex; gap: 12px; align-items: center; padding: 18px; border: 4px solid #fff; background: #101827; box-shadow: 8px 8px 0 #000; }
.brand b { width: 54px; height: 54px; display: grid; place-items: center; border: 4px solid #111; background: var(--yellow); color: #111; font-size: 32px; }
.brand strong { display: block; font-size: 22px; }
.brand span { color: var(--cyan); font-size: 11px; }
.side-nav { display: grid; gap: 12px; }
.side-nav button { height: 66px; border: 4px solid #fff; background: #24314a; color: #fff; box-shadow: 6px 6px 0 #000; font-size: 20px; text-align: left; padding: 0 18px; }
.side-nav button.active { background: var(--cyan); color: #111; transform: translate(4px,4px); box-shadow: 2px 2px 0 #000; }
.side-nav span { font-size: 28px; margin-right: 14px; }
.side-total { margin-top: auto; padding: 16px; border: 4px solid #fff; background: #111827; box-shadow: 8px 8px 0 #000; }
.side-total span { color: #aac3d7; }
.side-total strong { display: block; margin-top: 10px; color: var(--yellow); font-size: 30px; }

.workspace { min-width: 0; display: flex; flex-direction: column; gap: 18px; }
.header { height: 88px; display: flex; justify-content: space-between; align-items: center; color: #fff; }
.header p { margin: 0 0 4px; color: var(--cyan); }
.header h1 { margin: 0; font-size: 40px; text-shadow: 4px 4px 0 #000; }
.searchbar { width: 420px; height: 52px; display: grid; grid-template-columns: 1fr 92px; background: var(--panel); }
.searchbar input { border: 0; outline: 0; background: transparent; padding: 0 16px; }
.searchbar button { border: 0; border-left: 4px solid var(--line); background: var(--yellow); font-weight: 900; }
.view { display: none; min-height: 0; flex: 1; }
.view.active { display: flex; flex-direction: column; gap: 18px; }

.summary-grid { display: grid; grid-template-columns: 1.2fr repeat(3,1fr); gap: 16px; }
.summary { min-height: 132px; padding: 18px; background: var(--panel); position: relative; overflow: hidden; }
.summary::after { content: ""; position: absolute; right: -18px; bottom: -18px; width: 70px; height: 70px; border: 4px solid rgba(0,0,0,.15); background: rgba(0,0,0,.07); }
.summary span, .summary small { color: var(--muted); }
.summary strong { display: block; margin: 12px 0 8px; font-size: 34px; }
.summary b { font-size: 48px; }
.income strong, .income-text { color: var(--green); }
.expense strong, .expense-text { color: var(--red); }
.balance strong { color: var(--blue); }

.panel { background: var(--panel); padding: 16px; }
.ledger-panel { min-height: 0; flex: 1; display: flex; flex-direction: column; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.panel-head h2 { margin: 0; font-size: 26px; }
.panel-head div { display: flex; gap: 10px; }
.pixel-btn { min-height: 42px; padding: 0 16px; background: var(--paper); border-color: var(--line); font-weight: 900; }
.pixel-btn.primary { background: var(--blue); color: #fff; }
.pixel-btn.light { background: #fffbe7; }
.pixel-btn.danger { background: var(--red); color: #fff; }
.pixel-btn.ok { background: var(--green); color: #fff; }

.ledger-list { min-height: 0; overflow: auto; border: 4px solid var(--line); background: var(--paper); }
.empty { height: 100%; min-height: 260px; display: grid; place-items: center; color: var(--muted); font-size: 20px; text-align: center; }
.day-head { min-height: 42px; padding: 0 14px; display: grid; grid-template-columns: 1fr auto; align-items: center; background: #e4d9ae; border-bottom: 4px solid var(--line); font-weight: 900; }
.daily-total { display: flex; gap: 16px; color: var(--muted); font-size: 13px; }
.bill-row { min-height: 72px; display: grid; grid-template-columns: 52px minmax(160px,1fr) auto 46px; gap: 14px; align-items: center; padding: 10px 14px; border-bottom: 3px solid #e1d7b8; }
.bill-icon, .cat-icon { width: 48px; height: 48px; display: grid; place-items: center; border: 4px solid var(--line); background: #f0e8c8; font-weight: 900; font-size: 18px; }
.bill-title { font-weight: 900; font-size: 18px; }
.bill-note { margin-top: 4px; color: var(--muted); font-size: 12px; }
.bill-date { color: var(--muted); }
.bill-amount { font-weight: 900; font-size: 20px; white-space: nowrap; }
.bill-amount.income { color: var(--green); }
.bill-amount.expense { color: var(--red); }
.delete-bill { width: 36px; height: 36px; border: 4px solid var(--line); background: var(--red); color: #fff; font-weight: 900; }

.chart-controls { padding: 14px; background: var(--panel); display: flex; justify-content: space-between; }
.toggle { display: grid; grid-template-columns: repeat(2,1fr); width: 240px; border: 4px solid var(--line); background: #fff; }
.toggle button { height: 42px; border: 0; border-right: 4px solid var(--line); background: #fff; font-weight: 900; }
.toggle button:last-child { border-right: 0; }
.toggle button.active { background: var(--blue); color: #fff; }
.chart-grid { min-height: 0; flex: 1; display: grid; grid-template-columns: minmax(0,1.1fr) minmax(330px,.9fr); grid-template-rows: 1fr 1fr; gap: 18px; }
.chart-card { background: var(--panel); padding: 16px; overflow: hidden; }
.chart-card.wide { grid-row: span 2; }
.chart-card h2 { margin: 0 0 12px; }
.bar-chart { height: calc(100% - 44px); min-height: 320px; display: grid; grid-template-columns: repeat(30,minmax(8px,1fr)); align-items: end; gap: 7px; padding: 36px 12px 18px; border: 4px solid var(--line); background: linear-gradient(#eadfb8 2px, transparent 2px), var(--paper); background-size: 100% 42px; }
.bar { min-height: 4px; border: 3px solid var(--line); background: var(--cyan); position: relative; }
.bar.last { background: #b7cdd8; }
.bar-tip { position: absolute; top: -30px; left: 50%; transform: translateX(-50%); white-space: nowrap; font-size: 12px; }
.pie-wrap { height: calc(100% - 44px); display: grid; grid-template-columns: 210px 1fr; align-items: center; gap: 14px; }
.pie { width: 200px; height: 200px; border-radius: 50%; border: 4px solid var(--line); position: relative; }
.pie::after { content: attr(data-center); white-space: pre; position: absolute; inset: 52px; border: 4px solid var(--line); border-radius: 50%; background: var(--paper); display: grid; place-items: center; text-align: center; color: var(--blue); font-size: 18px; }
.legend { display: flex; flex-direction: column; gap: 8px; font-size: 13px; }
.dot { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--line); margin-right: 6px; }
.rank-list { height: calc(100% - 44px); overflow: auto; border: 4px solid var(--line); background: var(--paper); }
.rank-row { min-height: 68px; padding: 10px; display: grid; grid-template-columns: 48px 1fr auto; gap: 12px; align-items: center; border-bottom: 3px solid #e1d7b8; }
.track { height: 9px; border: 3px solid var(--line); background: #e6dfc6; margin-top: 8px; }
.fill { height: 100%; background: var(--blue); }

.profile-grid { display: grid; grid-template-columns: 1.1fr .9fr; gap: 18px; }
.profile-card { background: var(--panel); padding: 20px; }
.profile-card.full { grid-column: 1 / -1; }
.account-card { display: grid; grid-template-columns: 76px 1fr 108px; align-items: center; gap: 16px; }
.avatar { width: 76px; height: 76px; display: grid; place-items: center; border: 4px solid var(--line); background: var(--yellow); font-size: 30px; font-weight: 900; }
.account-info p { margin: 0 0 8px; color: var(--muted); }
.account-info h2 { margin: 0 0 8px; }
.account-info span { color: var(--muted); }
.profile-stats { display: grid; grid-template-columns: repeat(3,1fr); gap: 12px; }
.profile-stats div { padding: 12px; border: 4px solid var(--line); background: var(--paper); font-weight: 900; }
.profile-stats span { display: block; color: var(--muted); font-size: 13px; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; }
.chips button { padding: 12px; border: 4px solid var(--line); background: var(--paper); font-weight: 900; box-shadow: 4px 4px 0 rgba(0,0,0,.28); }
.chips button.active { background: var(--yellow); transform: translate(3px,3px); box-shadow: 1px 1px 0 rgba(0,0,0,.5); }
.category-detail { margin-top: 16px; display: grid; grid-template-columns: 1fr repeat(3,150px); gap: 12px; align-items: stretch; }
.category-detail .detail-title, .category-detail .detail-metric { border: 4px solid var(--line); background: var(--paper); padding: 14px; font-weight: 900; }
.category-detail span { display: block; color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.category-detail b { font-size: 22px; }

.modal { padding: 0; background: var(--panel); max-height: 88vh; border: 4px solid var(--line); }
.modal::backdrop { background: rgba(4,8,15,.72); }
.add-modal { width: 440px; }
.search-modal { width: min(860px, calc(100vw - 48px)); }
.add-panel, .search-panel { background: var(--panel); box-shadow: none; padding: 18px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-head h2 { margin: 0; font-size: 28px; }
.modal-head button { width: 42px; height: 42px; border: 4px solid var(--line); background: var(--red); color: #fff; font-size: 24px; font-weight: 900; }
.add-tabs { width: 100%; margin-bottom: 14px; }
.category-grid { display: grid; grid-template-columns: repeat(4,1fr); gap: 10px; max-height: 258px; overflow: auto; }
.category-item { min-height: 84px; display: grid; place-items: center; gap: 6px; border: 0; background: transparent; font-weight: 900; }
.category-item.active .cat-icon { background: var(--yellow); }
.custom-box { margin-top: 10px; display: grid; grid-template-columns: 1fr 70px; gap: 8px; }
.custom-box.hidden { display: none; }
.custom-box input, .note input, .search-fields input { height: 42px; border: 4px solid var(--line); background: var(--paper); outline: 0; padding: 0 10px; }
.custom-box button { border: 4px solid var(--line); background: var(--green); color: #fff; font-weight: 900; }
.note { display: grid; grid-template-columns: 48px 1fr; gap: 8px; align-items: center; margin-top: 14px; font-weight: 900; }
.amount { height: 70px; margin: 14px 0; padding: 0 14px; display: flex; justify-content: flex-end; align-items: center; border: 4px solid var(--line); background: #111827; color: var(--yellow); font-size: 42px; font-weight: 900; }
.keyboard { display: grid; grid-template-columns: repeat(3,1fr); gap: 8px; }
.keyboard button { height: 52px; border: 4px solid var(--line); background: var(--paper); font-size: 22px; font-weight: 900; box-shadow: 4px 4px 0 rgba(0,0,0,.3); }
.entry-actions { margin-top: 14px; display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; }
.search-fields { display: grid; grid-template-columns: repeat(4,1fr); gap: 12px; }
.search-fields label { display: grid; gap: 6px; font-weight: 900; }
.search-results { height: 330px; }

.backup-tip { margin: 0 16px; padding: 12px 16px; border: 4px solid var(--yellow); background: #fffbe7; color: #6d5a14; font-weight: 900; box-shadow: 6px 6px 0 var(--shadow); }
.backup-tip.hidden { display: none; }
.header-actions { display: flex; gap: 12px; align-items: center; }
.backup-btns { display: flex; gap: 8px; }
.backup-btns .pixel-btn { color: var(--ink) !important; }
.backup-btns .pixel-btn { min-height: 52px; }
#dateInput { font-size: 16px; }

.install-banner { position: fixed; left: 0; right: 0; bottom: 0; z-index: 100; display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: var(--panel); border-top: 4px solid var(--line); box-shadow: 0 -8px 0 var(--shadow); transform: translateY(0); transition: transform .25s ease, opacity .25s ease; }
.install-banner.hidden { transform: translateY(calc(100% + 120px)); opacity: 0; pointer-events: none; }
.install-banner span { flex: 1; font-weight: 900; font-size: 14px; }
.install-banner button { min-height: 40px; padding: 0 18px; border: 4px solid var(--line); background: var(--blue); color: #fff; font-weight: 900; }
.install-banner button:last-child { background: var(--paper); color: var(--ink); }

/* 账期选择器 */
.period-trigger { cursor: pointer; user-select: none; }
.period-trigger:hover { opacity: 0.7; }
.period-panel { min-width: 300px; max-width: 92vw; }
.period-year { display: flex; align-items: center; justify-content: center; gap: 24px; margin: 4px 0 12px; }
.period-year strong { font-size: 20px; font-weight: 900; min-width: 64px; text-align: center; }
.period-year button { width: 44px; height: 44px; border: 4px solid var(--line); background: var(--paper); color: var(--ink); font-size: 22px; font-weight: 900; line-height: 1; }
.period-mode { margin-bottom: 12px; }
.period-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.period-cell { min-height: 44px; padding: 8px 4px; border: 4px solid var(--line); background: var(--paper); color: var(--ink); font-weight: 900; font-size: 14px; }
.period-cell.active { background: var(--yellow); color: var(--ink); }
.period-day-box { margin-top: 12px; }
.period-sub { font-size: 14px; font-weight: 900; margin: 0 0 8px; }
.period-day-grid { grid-template-columns: repeat(7, 1fr); }
.period-day-grid .period-cell { font-size: 13px; padding: 6px 2px; }

@media (max-width: 1280px) {
  .app-shell { grid-template-columns: 210px minmax(680px,1fr); gap: 14px; padding: 14px; }
  .summary-grid { grid-template-columns: repeat(2,1fr); }
}

@media (max-width: 900px) {
  body { min-width: 0; padding-top: 62px; padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px)); }
  .mobile-appbar {
    display: flex;
    position: fixed;
    top: 0; left: 0; right: 0;
    z-index: 50;
    height: 62px;
    align-items: center;
    justify-content: space-between;
    padding: 0 12px 0 16px;
    background: #111827;
    border-bottom: 4px solid #000;
    box-shadow: 0 4px 0 rgba(0,0,0,.4);
  }
  .mobile-brand { display: flex; align-items: center; gap: 10px; color: #fff; }
  .mobile-logo { width: 38px; height: 38px; display: grid; place-items: center; border: 3px solid var(--line); background: var(--yellow); color: var(--ink); font-size: 22px; font-weight: 900; }
  .mobile-brand strong { display: block; font-size: 16px; line-height: 1; }
  .mobile-brand span { display: block; font-size: 11px; color: var(--cyan); margin-top: 3px; }
  .mobile-actions { display: flex; gap: 2px; }
  .icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border: 0; background: transparent; color: #fff; }
  .icon-btn:active { color: var(--yellow); }

  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 50;
    height: calc(64px + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: #111827;
    border-top: 4px solid #000;
    box-shadow: 0 -4px 0 rgba(0,0,0,.4);
  }
  .mobile-bottom-nav button {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    border: 0;
    background: transparent;
    color: #8fa4b8;
    font-size: 11px;
    font-weight: 700;
    padding: 0;
    min-height: 60px;
  }
  .mobile-bottom-nav button.active { color: var(--yellow); }
  .mobile-bottom-nav .mobile-add {
    position: relative;
    top: -18px;
    background: var(--yellow);
    color: var(--ink);
    border: 4px solid #000;
    border-radius: 18px;
    width: 78px;
    height: 78px;
    flex: 0 0 auto;
    box-shadow: 0 6px 0 rgba(0,0,0,.4);
  }
  .mobile-bottom-nav .mobile-add span { color: var(--ink); font-weight: 900; }

  .app-shell { grid-template-columns: 1fr; height: auto; min-height: calc(100vh - 62px - 64px); padding: 10px 10px 0; gap: 12px; }
  .sidebar { display: none; }
  .workspace { gap: 12px; }
  .header { flex-direction: column; align-items: stretch; height: auto; gap: 10px; }
  .header h1 { font-size: 28px; text-shadow: 3px 3px 0 #000; }
  .header-actions { flex-direction: column; align-items: stretch; gap: 10px; }
  .searchbar { width: 100%; height: 48px; }
  .backup-btns { gap: 10px; }
  .backup-btns .pixel-btn { flex: 1; min-height: 46px; }

  .summary-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .summary { min-height: 88px; padding: 12px; }
  .summary strong { font-size: 22px; margin: 8px 0 4px; }
  .summary b { font-size: 28px; }
  .summary small { display: none; }

  .panel-head { flex-wrap: wrap; gap: 10px; }
  .panel-head h2 { font-size: 20px; }
  .panel-head div { width: 100%; }
  .panel-head div .pixel-btn { flex: 1; min-height: 46px; }

  .ledger-list { max-height: none; }
  .bill-row { min-height: 66px; grid-template-columns: 44px minmax(0,1fr) auto 40px; gap: 10px; padding: 8px 12px; }
  .bill-icon { width: 40px; height: 40px; font-size: 16px; }
  .bill-title { font-size: 16px; }
  .bill-amount { font-size: 18px; }
  .delete-bill { width: 34px; height: 34px; }
  .day-head { padding: 0 12px; min-height: 38px; font-size: 14px; }
  .daily-total { gap: 10px; }

  .chart-grid { grid-template-columns: 1fr; grid-template-rows: auto; gap: 12px; }
  .chart-card.wide { grid-row: auto; }
  .chart-card { padding: 12px; }
  .chart-controls { flex-wrap: wrap; gap: 10px; }
  .toggle { width: 100%; }
  .bar-chart { min-height: 220px; gap: 3px; padding: 28px 8px 12px; }
  .pie-wrap { grid-template-columns: 1fr 1fr; gap: 10px; }
  .pie { width: 130px; height: 130px; }
  .pie::after { inset: 34px; font-size: 14px; }

  .profile-grid { grid-template-columns: 1fr; gap: 12px; }
  .account-card { grid-template-columns: 60px 1fr 86px; gap: 12px; }
  .avatar { width: 60px; height: 60px; font-size: 24px; }
  .account-info h2 { font-size: 18px; }
  .category-detail { grid-template-columns: 1fr 1fr; }

  .add-modal, .search-modal {
    width: 100vw !important;
    max-width: 100vw;
    max-height: calc(100vh - env(safe-area-inset-top, 0px));
    margin: 0;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    top: auto;
    border-radius: 16px 16px 0 0;
    animation: sheetUp .2s ease-out;
  }
  .add-panel, .search-panel { padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px)); }
  .modal::backdrop { background: rgba(0,0,0,.65); }
  @keyframes sheetUp { from { transform: translateY(40%); opacity: .6; } to { transform: translateY(0); opacity: 1; } }

  .category-grid { grid-template-columns: repeat(4,1fr); max-height: 180px; }
  .category-item { min-height: 70px; font-size: 13px; }
  .cat-icon { width: 40px; height: 40px; font-size: 15px; }
  .amount { height: 56px; font-size: 34px; }
  .keyboard button { height: 60px; font-size: 26px; }
  .entry-actions { grid-template-columns: 1fr 1.5fr; }
  .entry-actions .pixel-btn { min-height: 52px; }
  .search-fields { grid-template-columns: 1fr 1fr; gap: 10px; }
  .search-results { height: 220px; }

  .install-banner { bottom: calc(64px + env(safe-area-inset-bottom, 0px)); }
}

@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .mobile-bottom-nav { padding-bottom: env(safe-area-inset-bottom); }
}
