:root {
  --bg: var(--tg-theme-secondary-bg-color, #f1f4f8);
  --surface: var(--tg-theme-bg-color, #ffffff);
  --surface-soft: #f7f9fc;
  --text: var(--tg-theme-text-color, #15243a);
  --muted: var(--tg-theme-hint-color, #728097);
  --line: #dce4ee;
  --accent: var(--tg-theme-button-color, #2566a0);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --accent-soft: #e7f1fb;
  --green: #188a61;
  --green-soft: #e7f6ef;
  --orange: #a96519;
  --orange-soft: #fff1dd;
  --red: #b44646;
  --radius-xl: 22px;
  --radius-lg: 16px;
  --radius-md: 12px;
  --shadow: 0 8px 30px rgba(35, 64, 98, 0.08);
  font-family: Inter, "SF Pro Text", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-synthesis: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface-soft: rgba(255, 255, 255, 0.05);
    --line: rgba(255, 255, 255, 0.12);
    --accent-soft: rgba(63, 154, 230, 0.16);
    --green-soft: rgba(24, 138, 97, 0.16);
    --orange-soft: rgba(169, 101, 25, 0.18);
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body { min-height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button, input, select { font: inherit; }
button { color: inherit; }

.app-shell {
  width: min(100%, 620px);
  min-height: 100vh;
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 16px calc(96px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 2px 18px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--accent);
  font-size: 10px;
  line-height: 1.2;
  font-weight: 800;
  letter-spacing: 0.16em;
}

h1, h2, h3, p { margin-top: 0; }
.topbar h1 { margin: 0; font-size: 23px; line-height: 1.2; letter-spacing: -0.02em; }

.icon-button {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: pointer;
}

.icon-button:active { transform: scale(0.96); }
.material-icons-round { font-size: 21px; line-height: 1; }

.notice {
  margin-bottom: 12px;
  padding: 11px 14px;
  border-radius: var(--radius-md);
  color: var(--green);
  background: var(--green-soft);
  font-size: 13px;
  font-weight: 700;
}

.loading-panel, .error-panel {
  min-height: 58vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
}

.loading-panel p { margin: 18px 0 0; font-size: 14px; }
.spinner { width: 30px; height: 30px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.error-panel > .material-icons-round { font-size: 42px; color: var(--accent); margin-bottom: 14px; }
.error-panel h2 { margin-bottom: 8px; color: var(--text); font-size: 20px; }
.error-panel p { max-width: 340px; margin-bottom: 22px; font-size: 14px; line-height: 1.65; }

.group-card, .date-card, .filter-card, .metric, .action-card, .settings-card, .guide-list article {
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.group-card { padding: 18px; border-radius: var(--radius-xl); }
.group-identity { display: flex; align-items: center; gap: 12px; }
.group-avatar { width: 46px; height: 46px; display: grid; place-items: center; flex: none; color: var(--accent); background: var(--accent-soft); border-radius: 15px; }
.group-avatar .material-icons-round { font-size: 27px; }
.group-copy { min-width: 0; }
.group-copy span { display: block; margin-bottom: 4px; color: var(--muted); font-size: 11px; font-weight: 700; }
.group-copy h2 { overflow: hidden; margin: 0; font-size: 17px; line-height: 1.35; white-space: nowrap; text-overflow: ellipsis; }

.select-wrap { position: relative; display: block; margin-top: 15px; }
.select-wrap select {
  width: 100%;
  height: 44px;
  padding: 0 42px 0 13px;
  appearance: none;
  color: var(--text);
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  outline: none;
}
.select-wrap .material-icons-round { position: absolute; top: 12px; right: 12px; color: var(--muted); pointer-events: none; }
.select-wrap select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.context-row { display: flex; gap: 18px; margin-top: 14px; padding-top: 13px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }
.context-row > span { display: flex; align-items: center; gap: 5px; min-width: 0; }
.context-row b { overflow: hidden; color: var(--text); white-space: nowrap; text-overflow: ellipsis; }
.context-row .material-icons-round { font-size: 16px; color: var(--accent); }

.date-card { margin-top: 12px; padding: 14px; border-radius: var(--radius-lg); }
.date-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.date-row label { color: var(--muted); font-size: 12px; font-weight: 700; }
.date-row input { min-height: 38px; padding: 0 10px; color: var(--text); background: var(--surface-soft); border: 1px solid var(--line); border-radius: 10px; outline: none; }
.date-row input:focus { border-color: var(--accent); }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 12px; padding: 4px; background: var(--surface-soft); border-radius: var(--radius-md); }
.segmented button, .type-tabs button { min-height: 36px; border: 0; border-radius: 9px; color: var(--muted); background: transparent; font-size: 13px; font-weight: 700; cursor: pointer; }
.segmented button.active, .type-tabs button.active { color: var(--accent); background: var(--surface); box-shadow: 0 2px 9px rgba(35, 64, 98, 0.09); }

.view { display: none; padding-top: 22px; }
.view.active { display: block; }
.section-heading { display: flex; align-items: end; justify-content: space-between; margin: 0 2px 12px; }
.section-heading h2 { margin: 0; font-size: 19px; letter-spacing: -0.01em; }
.result-count { padding: 5px 9px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); border-radius: 999px; font-size: 11px; font-weight: 700; }

.filter-card { padding: 12px; border-radius: var(--radius-lg); }
.type-tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
.search-field { height: 42px; display: flex; align-items: center; gap: 8px; margin-top: 10px; padding: 0 12px; background: var(--surface-soft); border: 1px solid var(--line); border-radius: 11px; }
.search-field:focus-within { border-color: var(--accent); }
.search-field .material-icons-round { color: var(--muted); font-size: 19px; }
.search-field input { width: 100%; border: 0; outline: 0; color: var(--text); background: transparent; font-size: 13px; }
.search-field input::placeholder { color: var(--muted); }

.entries { display: grid; gap: 9px; margin-top: 12px; }
.entry-card { display: grid; grid-template-columns: 45px minmax(0, 1fr); gap: 11px; padding: 13px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow); }
.entry-time { padding-top: 3px; color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.entry-main { min-width: 0; }
.entry-topline { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.entry-kind { padding: 3px 7px; border-radius: 7px; font-size: 10px; font-weight: 800; }
.entry-kind.income { color: var(--green); background: var(--green-soft); }
.entry-kind.outgoing { color: var(--orange); background: var(--orange-soft); }
.entry-amount { text-align: right; font-size: 16px; font-weight: 800; font-variant-numeric: tabular-nums; }
.entry-formula { margin: 8px 0 0; color: var(--muted); font-size: 12px; line-height: 1.45; font-variant-numeric: tabular-nums; }
.entry-meta { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-top: 8px; color: var(--muted); font-size: 11px; }
.entry-meta a { color: var(--accent); font-weight: 700; text-decoration: none; }
.entry-note { width: 100%; overflow-wrap: anywhere; }
.compact .entry-card { padding-top: 9px; padding-bottom: 9px; }
.compact .entry-formula { margin-top: 5px; }
.compact .entry-meta { margin-top: 5px; }

.empty-state { padding: 36px 20px; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius-lg); }
.empty-state .material-icons-round { display: block; margin-bottom: 9px; color: var(--accent); font-size: 30px; }
.empty-state strong { display: block; margin-bottom: 6px; color: var(--text); }
.empty-state span { font-size: 13px; }

.pagination { align-items: center; justify-content: space-between; margin-top: 14px; }
.pagination:not([hidden]) { display: flex; }
.pagination button { min-height: 40px; display: flex; align-items: center; gap: 2px; padding: 0 11px; color: var(--accent); background: var(--surface); border: 1px solid var(--line); border-radius: 11px; font-size: 12px; font-weight: 700; }
.pagination button:disabled { color: var(--muted); opacity: .45; }
.pagination .material-icons-round { font-size: 17px; }
#pageLabel { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

.summary-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.metric { min-height: 118px; display: flex; flex-direction: column; justify-content: center; padding: 18px; border-radius: var(--radius-lg); }
.metric-wide { grid-column: 1 / -1; min-height: 128px; }
.metric span { margin-bottom: 12px; color: var(--muted); font-size: 12px; font-weight: 700; }
.metric strong { font-size: 21px; line-height: 1.25; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.metric small { margin-top: 6px; color: var(--muted); font-size: 12px; font-weight: 700; }
.metric-wide strong { font-size: 28px; }
.metric.due { border-color: rgba(37, 102, 160, 0.28); }
.metric.outstanding strong { color: var(--orange); }

.action-card { padding: 28px 20px 20px; border-radius: var(--radius-xl); text-align: center; }
.action-icon { width: 56px; height: 56px; display: grid; place-items: center; margin: 0 auto 16px; color: var(--accent); background: var(--accent-soft); border-radius: 17px; }
.action-icon .material-icons-round { font-size: 29px; }
.action-card h3 { margin-bottom: 9px; font-size: 18px; }
.action-card p { margin: 0 auto; max-width: 400px; color: var(--muted); font-size: 13px; line-height: 1.7; }
.export-scope { display: inline-block; margin: 18px 0; padding: 8px 12px; color: var(--accent); background: var(--accent-soft); border-radius: 999px; font-size: 12px; font-weight: 800; }
.primary-button { min-height: 46px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 0 22px; border: 0; border-radius: 12px; color: var(--accent-text); background: var(--accent); font-weight: 800; cursor: pointer; }
.primary-button:active { transform: scale(.98); }

.settings-card { padding: 0 17px; border-radius: var(--radius-xl); }
.setting-row { min-height: 78px; display: flex; align-items: center; justify-content: space-between; gap: 14px; border-bottom: 1px solid var(--line); }
.setting-row strong { display: block; margin-bottom: 5px; font-size: 14px; }
.setting-row span { color: var(--muted); font-size: 11px; line-height: 1.45; }
.switch { position: relative; width: 47px; height: 28px; flex: none; }
.switch input { position: absolute; opacity: 0; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: .2s ease; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 5px rgba(20, 40, 60, .24); transition: .2s ease; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(19px); }
.switch input:focus-visible + span { outline: 3px solid var(--accent-soft); }
.status-chip { padding: 6px 9px; color: var(--green) !important; background: var(--green-soft); border-radius: 999px; font-weight: 800; }
.setting-note { display: flex; align-items: flex-start; gap: 8px; padding: 15px 0 18px; color: var(--muted); font-size: 12px; line-height: 1.55; }
.setting-note .material-icons-round { color: var(--green); font-size: 18px; }

.guide-list { display: grid; gap: 10px; }
.guide-list article { display: grid; grid-template-columns: 34px minmax(0, 1fr); gap: 11px; padding: 17px; border-radius: var(--radius-lg); }
.guide-list article > span { padding-top: 2px; color: var(--accent); font-size: 12px; font-weight: 900; }
.guide-list h3 { margin-bottom: 6px; font-size: 14px; }
.guide-list p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.6; }
.guide-link { min-height: 48px; display: flex; align-items: center; justify-content: center; gap: 7px; margin-top: 12px; color: var(--accent); background: var(--accent-soft); border-radius: var(--radius-md); font-size: 13px; font-weight: 800; text-decoration: none; }
.guide-link .material-icons-round { font-size: 17px; }

.bottom-nav {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: 0;
  width: min(100%, 620px);
  min-height: calc(67px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  padding: 7px 12px calc(7px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(18px);
}
.bottom-nav:not([hidden]) { display: grid; grid-template-columns: repeat(5, 1fr); }
.bottom-nav button { min-height: 52px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; padding: 0; color: var(--muted); background: transparent; border: 0; border-radius: 12px; cursor: pointer; }
.bottom-nav button.active { color: var(--accent); background: var(--accent-soft); }
.bottom-nav .material-icons-round { font-size: 22px; }
.bottom-nav b { font-size: 10px; font-weight: 800; }

[hidden] { display: none !important; }
button:focus-visible, a:focus-visible { outline: 3px solid var(--accent-soft); outline-offset: 2px; }

@media (max-width: 360px) {
  .app-shell { padding-left: 11px; padding-right: 11px; }
  .topbar h1 { font-size: 21px; }
  .group-card { padding: 15px; }
  .context-row { gap: 10px; }
  .entry-card { grid-template-columns: 40px minmax(0, 1fr); padding-left: 11px; padding-right: 11px; }
  .metric { padding: 14px; }
  .metric strong { font-size: 18px; }
  .metric-wide strong { font-size: 24px; }
}

@media (min-width: 621px) {
  body { padding: 22px 0; }
  .app-shell { min-height: calc(100vh - 44px); border-radius: 28px; box-shadow: 0 18px 60px rgba(35, 64, 98, 0.14); }
  .bottom-nav { bottom: 22px; border-radius: 0 0 28px 28px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
