:root {
  --bg: #f9f5ec;
  --card: #fefbf2;
  --border: #e6d9b8;
  --text: #1a1208;
  --body: #4a3c20;
  --muted: #8a7040;
  --accent: #b8922a;
  --accent-light: #e8d080;
  --accent-soft: #f3e8bd;
  --income: #507853;
  --income-soft: #e4eddf;
  --expense: #a5523f;
  --expense-soft: #f4e3dd;
  --danger: #943f32;
  --shadow: 0 8px 24px rgba(74, 60, 32, 0.07);
  --radius: 18px;
  --app-height: 100dvh;
  --header-height: 75px;
  --nav-height: 70px;
  --composer-height: 86px;
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "PingFang SC", "Hiragino Sans GB", "Segoe UI", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

html {
  background: var(--bg);
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

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

button,
a,
input,
textarea,
select {
  -webkit-tap-highlight-color: transparent;
}

button {
  color: inherit;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 3px solid rgba(184, 146, 42, 0.28);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.14em;
}

.login-view {
  min-height: var(--app-height);
  display: grid;
  place-items: center;
  padding: calc(32px + env(safe-area-inset-top)) 22px calc(32px + env(safe-area-inset-bottom));
  background:
    linear-gradient(90deg, var(--accent), var(--accent-light), var(--accent)) top / 100% 5px no-repeat,
    var(--bg);
}

.login-card {
  width: min(100%, 380px);
  padding: 30px 24px 26px;
  border: 1px solid var(--border);
  border-radius: 24px;
  background: var(--card);
  box-shadow: var(--shadow);
}

.brand-mark {
  display: grid;
  width: 48px;
  height: 48px;
  margin-bottom: 22px;
  place-items: center;
  border-radius: 15px;
  background: var(--accent);
  color: #fffdf7;
  font-family: Georgia, serif;
  font-size: 25px;
  font-weight: 700;
}

.login-card h1,
.app-header h1,
.page-intro h2,
.dialog-header h2 {
  margin: 0;
  line-height: 1.1;
}

.login-card h1 {
  font-size: 27px;
}

.login-copy {
  margin: 10px 0 26px;
  color: var(--body);
  font-size: 15px;
  line-height: 1.65;
}

.login-form label,
.field > span {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.login-form input,
.field input,
.field select,
.compact-month input {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fffdf8;
  color: var(--text);
  appearance: none;
}

.login-form input:focus,
.field input:focus,
.field select:focus,
.compact-month input:focus,
.composer textarea:focus {
  border-color: var(--accent);
}

.primary-button {
  width: 100%;
  min-height: 47px;
  margin-top: 16px;
  border: 0;
  border-radius: 13px;
  background: var(--accent);
  color: #fffdf7;
  font-weight: 750;
  cursor: pointer;
}

.primary-button:disabled,
.send-button:disabled,
.entry-action:disabled,
.retry-button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.form-message,
.setup-message {
  margin: 10px 0 0;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
}

.error,
.setup-message {
  background: var(--expense-soft);
  color: var(--danger);
}

.app-shell {
  position: relative;
  width: min(100%, 760px);
  height: var(--app-height);
  margin: 0 auto;
  overflow: hidden;
  background: var(--bg);
}

.app-shell::before {
  position: absolute;
  z-index: 20;
  top: 0;
  right: 0;
  left: 0;
  height: 4px;
  background: linear-gradient(90deg, var(--accent), var(--accent-light), var(--accent));
  content: "";
}

.app-header {
  position: absolute;
  z-index: 12;
  top: 0;
  right: 0;
  left: 0;
  height: calc(var(--header-height) + env(safe-area-inset-top));
  padding: calc(18px + env(safe-area-inset-top)) 18px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(230, 217, 184, 0.85);
  background: rgba(249, 245, 236, 0.96);
  backdrop-filter: blur(16px);
}

.app-header h1 {
  font-size: 21px;
  letter-spacing: -0.02em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-month {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.icon-button {
  display: grid;
  width: 40px;
  height: 40px;
  padding: 9px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  cursor: pointer;
}

.icon-button svg,
.tab-button svg,
.send-button svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.global-notice {
  position: absolute;
  z-index: 18;
  top: calc(var(--header-height) + env(safe-area-inset-top) + 8px);
  right: 14px;
  left: 14px;
  padding: 10px 13px;
  border: 1px solid #dfbea8;
  border-radius: 12px;
  background: #fff4e9;
  color: #7f4733;
  font-size: 12px;
  line-height: 1.45;
  box-shadow: var(--shadow);
}

.page {
  position: absolute;
  inset: calc(var(--header-height) + env(safe-area-inset-top)) 0 calc(var(--nav-height) + env(safe-area-inset-bottom));
  overflow-x: hidden;
  overflow-y: auto;
  padding: 18px 16px 28px;
  overscroll-behavior-y: contain;
  scrollbar-width: none;
}

.page::-webkit-scrollbar,
.message-list::-webkit-scrollbar {
  display: none;
}

.chat-page {
  bottom: calc(var(--nav-height) + var(--composer-height) + env(safe-area-inset-bottom));
  padding: 14px 14px 24px;
  scroll-behavior: smooth;
}

.page-intro {
  margin: 2px 2px 20px;
}

.page-intro h2 {
  font-size: 25px;
}

.stats-heading,
.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.compact-month {
  width: 146px;
}

.compact-month input {
  min-height: 40px;
  padding: 8px 10px;
  background: var(--card);
  font-size: 13px;
  font-weight: 650;
}

.inline-status,
.empty-state {
  padding: 18px;
  border: 1px dashed var(--border);
  border-radius: 15px;
  color: var(--muted);
  background: rgba(254, 251, 242, 0.7);
  font-size: 13px;
  line-height: 1.55;
  text-align: center;
}

.inline-status.warning {
  border-style: solid;
  border-color: #dfbea8;
  background: #fff4e9;
  color: #7f4733;
}

.message-list {
  display: flex;
  min-height: 100%;
  flex-direction: column;
  justify-content: flex-end;
  gap: 16px;
}

.chat-empty {
  margin: auto 8px;
  padding: 34px 22px;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--card);
  text-align: center;
}

.chat-empty-mark {
  display: grid;
  width: 48px;
  height: 48px;
  margin: 0 auto 15px;
  place-items: center;
  border-radius: 50%;
  background: var(--accent-soft);
  color: #7a5c0e;
  font-size: 23px;
}

.chat-empty h2 {
  margin: 0;
  font-size: 20px;
}

.chat-empty p {
  margin: 9px 0 0;
  color: var(--body);
  font-size: 14px;
  line-height: 1.6;
}

.message {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.message.user {
  align-items: flex-end;
}

.message-bubble {
  max-width: 88%;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: 7px 17px 17px 17px;
  background: var(--card);
  color: var(--body);
  font-size: 14px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message.user .message-bubble {
  border-color: #d7bf7d;
  border-radius: 17px 7px 17px 17px;
  background: var(--accent-soft);
  color: #3f310d;
}

.message-time {
  margin: 5px 4px 0;
  color: var(--muted);
  font-size: 10px;
}

.result-stack {
  width: min(94%, 520px);
  margin-top: 8px;
  display: grid;
  gap: 8px;
}

.entry-card,
.ledger-entry {
  padding: 12px 13px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: var(--card);
}

.entry-card-head,
.ledger-entry-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.entry-description {
  color: var(--text);
  font-size: 14px;
  font-weight: 750;
  line-height: 1.35;
}

.entry-category {
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
}

.entry-amount {
  flex-shrink: 0;
  color: var(--expense);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 15px;
  font-weight: 750;
}

.entry-amount.income,
.entry-amount.refund,
.entry-amount.reimbursement {
  color: var(--income);
}

.entry-amount.transfer {
  color: var(--muted);
}

.entry-amount.pending_income {
  color: var(--muted);
}

.entry-meta {
  display: flex;
  margin-top: 9px;
  flex-wrap: wrap;
  gap: 5px 9px;
  color: var(--muted);
  font-size: 10.5px;
  line-height: 1.4;
}

.entry-note {
  width: 100%;
  color: var(--body);
}

.entry-actions,
.message-actions {
  display: flex;
  margin-top: 9px;
  gap: 8px;
}

.entry-action,
.message-action,
.retry-button {
  min-height: 31px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: #fffdf8;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.message-action.undo {
  color: var(--danger);
}

.unsent-card {
  align-self: flex-end;
  width: min(88%, 430px);
  padding: 11px 13px;
  border: 1px solid #dfbea8;
  border-radius: 15px;
  background: #fff4e9;
}

.unsent-label {
  color: var(--danger);
  font-size: 11px;
  font-weight: 750;
}

.unsent-content {
  margin-top: 5px;
  color: var(--body);
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.composer {
  position: absolute;
  z-index: 11;
  right: 0;
  bottom: calc(var(--nav-height) + env(safe-area-inset-bottom));
  left: 0;
  min-height: 68px;
  padding: 10px 13px;
  border-top: 1px solid var(--border);
  background: rgba(249, 245, 236, 0.97);
  backdrop-filter: blur(16px);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.composer textarea {
  width: 100%;
  max-height: 112px;
  min-height: 48px;
  resize: none;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: var(--card);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  outline: 0;
}

.composer textarea::placeholder {
  color: #a38d61;
}

.send-button {
  display: grid;
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  padding: 13px;
  place-items: center;
  border: 0;
  border-radius: 15px;
  background: var(--accent);
  color: #fffdf7;
  cursor: pointer;
}

.send-button.loading svg {
  animation: pulse 0.85s ease-in-out infinite alternate;
}

@keyframes pulse {
  to { transform: translateY(-2px); opacity: 0.5; }
}

.context-chip {
  width: fit-content;
  max-width: 100%;
  margin: -2px 0 8px;
  padding: 5px 7px 5px 10px;
  border: 1px solid #d7bf7d;
  border-radius: 999px;
  background: var(--accent-soft);
  color: #634b11;
  font-size: 11px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.context-chip button {
  margin-left: 5px;
  padding: 0 3px;
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.composer-error {
  display: inline;
  margin: 6px 8px 0 2px;
  color: var(--danger);
  font-size: 11px;
  line-height: 1.4;
}

.retry-button {
  margin-top: 6px;
  color: var(--danger);
}

.bottom-nav {
  position: absolute;
  z-index: 13;
  right: 0;
  bottom: 0;
  left: 0;
  height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  padding: 5px 18px env(safe-area-inset-bottom);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-top: 1px solid var(--border);
  background: rgba(254, 251, 242, 0.98);
  backdrop-filter: blur(18px);
}

.tab-button {
  position: relative;
  display: flex;
  min-width: 0;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 3px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  cursor: pointer;
}

.tab-button svg {
  width: 22px;
  height: 22px;
}

.tab-button.active {
  color: #7b5c0d;
}

.tab-button.active::before {
  position: absolute;
  top: -5px;
  left: 50%;
  width: 24px;
  height: 3px;
  transform: translateX(-50%);
  border-radius: 2px;
  background: var(--accent);
  content: "";
}

.filter-row {
  display: grid;
  grid-template-columns: 138px 1fr;
  gap: 10px;
}

.section-meta {
  min-height: 18px;
  margin: 14px 2px 8px;
  color: var(--muted);
  font-size: 11px;
}

.ledger-list {
  display: grid;
  gap: 9px;
}

.ledger-entry {
  padding: 13px 14px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.summary-card {
  min-width: 0;
  padding: 13px 11px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: var(--card);
}

.summary-label {
  color: var(--muted);
  font-size: 10px;
}

.summary-value {
  margin-top: 6px;
  color: var(--text);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: clamp(14px, 4vw, 18px);
  font-weight: 750;
  letter-spacing: -0.04em;
  overflow-wrap: anywhere;
}

.summary-card.income .summary-value { color: var(--income); }
.summary-card.expense .summary-value { color: var(--expense); }

.paper-card {
  margin-top: 10px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
}

.budget-card h3,
.section-heading h3 {
  margin: 0;
  font-size: 15px;
}

.budget-card p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}

.budget-amounts {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.budget-percent {
  color: var(--muted);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 13px;
  font-weight: 700;
}

.progress-track {
  height: 8px;
  margin-top: 13px;
  overflow: hidden;
  border-radius: 999px;
  background: #eee6d3;
}

.progress-fill {
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
}

.progress-fill.over {
  background: var(--expense);
}

.chart-card {
  padding-bottom: 18px;
}

.section-heading {
  margin-bottom: 16px;
}

.chart-legend {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 9px;
}

.income-dot,
.expense-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-left: 3px;
  border-radius: 50%;
  background: var(--income);
}

.expense-dot {
  background: var(--expense);
}

.category-chart {
  display: grid;
  gap: 12px;
}

.category-row {
  display: grid;
  grid-template-columns: minmax(58px, auto) 1fr auto;
  align-items: center;
  gap: 9px;
}

.category-name,
.category-value {
  color: var(--body);
  font-size: 11px;
}

.category-value {
  font-family: "SFMono-Regular", Consolas, monospace;
  text-align: right;
}

.category-track {
  height: 7px;
  overflow: hidden;
  border-radius: 99px;
  background: #eee6d3;
}

.category-fill {
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
}

.trend-chart {
  display: grid;
  min-height: 154px;
  grid-template-columns: repeat(var(--trend-count, 1), minmax(34px, 1fr));
  align-items: end;
  gap: 8px;
  padding-top: 8px;
  border-bottom: 1px solid var(--border);
}

.trend-month {
  display: grid;
  height: 146px;
  grid-template-rows: 1fr 20px;
  gap: 6px;
}

.trend-bars {
  display: flex;
  height: 120px;
  align-items: flex-end;
  justify-content: center;
  gap: 3px;
}

.trend-bar {
  width: min(13px, 42%);
  min-height: 2px;
  border-radius: 4px 4px 1px 1px;
  background: var(--income);
}

.trend-bar.expense {
  background: var(--expense);
}

.trend-label {
  color: var(--muted);
  font-size: 9px;
  text-align: center;
  white-space: nowrap;
}

.freshness-note {
  margin: 14px 3px 0;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.5;
}

.settings-dialog {
  width: min(calc(100% - 26px), 440px);
  max-height: calc(100dvh - 28px);
  padding: 0;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--card);
  color: var(--text);
  box-shadow: 0 20px 70px rgba(48, 37, 15, 0.25);
}

.settings-dialog::backdrop {
  background: rgba(42, 32, 13, 0.42);
  backdrop-filter: blur(3px);
}

.settings-dialog form {
  padding: 22px;
}

.dialog-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 24px;
}

.dialog-header h2 {
  font-size: 23px;
}

.close-button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 25px;
  line-height: 1;
}

.dialog-field + .dialog-field {
  display: block;
  margin-top: 15px;
}

.secondary-link,
.text-button {
  display: block;
  width: 100%;
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: transparent;
  color: var(--body);
  font-size: 13px;
  font-weight: 700;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}

.text-button.danger {
  border-color: transparent;
  color: var(--danger);
}

.toast {
  position: fixed;
  z-index: 100;
  right: 50%;
  bottom: calc(88px + env(safe-area-inset-bottom));
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 14px;
  transform: translateX(50%);
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #2f281a;
  color: #fffdf8;
  font-size: 12px;
  box-shadow: var(--shadow);
}

@media (min-width: 600px) {
  body {
    background: #efe8d9;
  }

  .app-shell {
    border-right: 1px solid var(--border);
    border-left: 1px solid var(--border);
    box-shadow: 0 0 35px rgba(74, 60, 32, 0.08);
  }

  .page {
    padding-right: 22px;
    padding-left: 22px;
  }
}

@media (max-width: 360px) {
  .filter-row {
    grid-template-columns: 1fr;
  }

  .summary-grid {
    gap: 5px;
  }

  .summary-card {
    padding: 11px 8px;
  }
}

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