/* Pixel theme: a warm 8-bit ledger palette with hard edges and chunky controls. */

:root {
  --bg: #f5e9c8;
  --surface: #fff8df;
  --surface2: #ead9ad;
  --text: #202344;
  --muted: #6e6b83;
  --line: #202344;
  --green: #4d9b68;
  --green-soft: #c9e4ae;
  --orange: #e9784e;
  --orange-soft: #ffd0a8;
  --yellow: #e7b84b;
  --shadow: 5px 5px 0 #202344;
  --pixel-blue: #5b74c8;
  --pixel-purple: #8d6bb8;
}

* {
  image-rendering: pixelated;
}

body {
  background-color: var(--bg);
  background-image:
    linear-gradient(rgba(32, 35, 68, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(32, 35, 68, 0.045) 1px, transparent 1px);
  background-size: 16px 16px;
  color: var(--text);
  font-family: "Courier New", "Noto Sans SC", monospace;
  letter-spacing: 0.01em;
}

body.dark {
  --bg: #171a36;
  --surface: #25294c;
  --surface2: #34385d;
  --text: #fff5d6;
  --muted: #b3b4ca;
  --line: #fff5d6;
  --green: #82d39a;
  --green-soft: #385b4b;
  --orange: #ff9b68;
  --orange-soft: #5d3b43;
  --yellow: #f6d36a;
  --shadow: 5px 5px 0 #0d1024;
}

.shell {
  grid-template-columns: 236px minmax(0, 1fr);
}

.sidebar {
  padding: 24px 18px 20px;
  background: var(--surface);
  border-right: 3px solid var(--line);
  box-shadow: 5px 0 0 rgba(32, 35, 68, 0.12);
  backdrop-filter: none;
}

.brand {
  gap: 12px;
  padding: 0 8px 28px;
  font-size: 16px;
  text-transform: uppercase;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border: 3px solid var(--line);
  border-radius: 0;
  background: var(--green);
  color: var(--surface);
  box-shadow: 3px 3px 0 var(--line);
  font-family: "Courier New", monospace;
  font-weight: 700;
}

.sidebar nav {
  gap: 9px;
}

.nav-item {
  min-height: 43px;
  padding: 9px 11px;
  border: 2px solid transparent;
  border-radius: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.nav-item span {
  width: 22px;
  font-size: 18px;
  text-align: center;
}

.nav-item:hover,
.nav-item.active {
  border-color: var(--line);
  background: var(--green-soft);
  color: var(--text);
  box-shadow: 3px 3px 0 var(--line);
}

.privacy {
  padding: 12px;
  border: 2px solid var(--line);
  border-radius: 0;
  background: var(--surface2);
}

.session-card {
  display: grid;
  gap: 4px;
  margin-bottom: 10px;
  padding: 11px;
  border: 2px solid var(--line);
  background: var(--green-soft);
}

.session-card span,
.session-card small {
  color: var(--muted);
  font-size: 8px;
}

.session-card b {
  overflow: hidden;
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-card button {
  justify-self: start;
  padding: 0;
  border: 0;
  color: var(--pixel-blue);
  background: transparent;
  font: inherit;
  font-size: 8px;
  font-weight: 700;
  cursor: pointer;
}

.account-session-panel {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.account-session-panel span,
.account-session-panel p,
.account-session-panel small {
  display: block;
  color: var(--muted);
  font-size: 10px;
}

.account-session-panel h2 {
  margin: 6px 0;
}

.privacy b,
.privacy small {
  font-size: 10px;
}

.text-btn {
  color: var(--pixel-blue);
  font-size: 11px;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 3px;
}

main {
  max-width: 1540px;
  padding: 30px 42px 60px;
}

.topbar,
.section-head {
  margin-bottom: 26px;
}

.eyebrow {
  color: var(--pixel-purple);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
}

h1,
h2,
.section-head h2 {
  font-family: "Courier New", "Noto Sans SC", monospace;
  font-weight: 900;
  letter-spacing: 0.02em;
}

h1 {
  font-size: 23px;
}

h2,
.section-head h2 {
  font-size: 16px;
}

.top-actions {
  gap: 12px;
}

.icon-btn,
.primary-btn,
.ai-btn,
.close-btn,
.calendar-actions button,
.filter-clear,
.recurring-actions button,
.more-btn,
.agent-head-actions > button:first-child,
.agent-quick-actions button {
  border: 2px solid var(--line);
  border-radius: 0;
  font-family: inherit;
  font-weight: 700;
}

.icon-btn,
.primary-btn,
.ai-btn {
  box-shadow: 4px 4px 0 var(--line);
}

.icon-btn:active,
.primary-btn:active,
.ai-btn:active,
.calendar-actions button:active,
.filter-clear:active {
  transform: translate(3px, 3px);
  box-shadow: 1px 1px 0 var(--line);
}

.icon-btn {
  width: 42px;
  height: 42px;
  background: var(--surface);
  color: var(--text);
}

.primary-btn {
  padding: 10px 15px;
  background: var(--green);
  color: #14233a;
  font-size: 12px;
}

.ai-btn {
  padding: 9px 13px;
  background: var(--yellow);
  color: var(--text);
  font-size: 11px;
}

.stats-grid,
.content-grid,
.analysis-grid,
.budget-detail-grid,
.account-grid,
.settings-grid {
  gap: 18px;
}

.stat-card,
.panel {
  border: 3px solid var(--line);
  border-radius: 0;
  box-shadow: var(--shadow);
  background: var(--surface);
}

.stat-card {
  min-height: 160px;
  padding: 19px;
}

.stat-head,
.stat-card small,
.panel-head p,
.section-head p {
  font-size: 11px;
}

.stat-card > strong {
  margin: 18px 0 8px;
  font-size: 24px;
  letter-spacing: 0;
}

.pill,
.round-icon,
.tx-icon,
.legend-key,
.status-dot {
  border-radius: 0;
}

.pill {
  border: 2px solid currentColor;
  padding: 4px 7px;
  font-weight: 700;
}

.round-icon {
  width: 32px;
  height: 32px;
  border: 2px solid currentColor;
}

.panel {
  padding: 20px;
}

.panel-head {
  padding-bottom: 12px;
  border-bottom: 2px dashed var(--line);
}

.transaction {
  padding: 13px 4px;
  border-top: 2px dotted var(--line);
}

.tx-icon {
  width: 38px;
  height: 38px;
  border: 2px solid var(--line);
  background: var(--surface2);
}

.tx-info b,
.tx-amount,
.recurring-item b,
.account-card strong {
  font-weight: 700;
}

.empty {
  border: 2px dashed var(--line);
  padding: 30px;
}

input,
select,
.agent-form textarea {
  border: 2px solid var(--line);
  border-radius: 0;
  background: var(--surface2);
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
}

input:focus,
select:focus,
.agent-form textarea:focus {
  border-color: var(--pixel-blue);
  outline: 3px solid color-mix(in srgb, var(--pixel-blue) 28%, transparent);
}

dialog {
  border: 3px solid var(--line);
  border-radius: 0;
  box-shadow: 8px 8px 0 var(--line);
}

dialog::backdrop {
  background: rgba(19, 21, 46, 0.72);
  backdrop-filter: none;
}

dialog form,
.analysis-modal-inner {
  padding: 24px;
}

.modal-head {
  padding-bottom: 12px;
  border-bottom: 2px dashed var(--line);
}

.close-btn {
  width: 32px;
  height: 32px;
  background: var(--orange-soft);
  color: var(--text);
}

.type-toggle {
  border: 2px solid var(--line);
  border-radius: 0;
  background: var(--surface2);
}

.type-toggle button {
  border-radius: 0;
  font-family: inherit;
  font-weight: 700;
}

.type-toggle button.active {
  border: 2px solid var(--line);
  background: var(--yellow);
  color: var(--text);
  box-shadow: 3px 3px 0 var(--line);
}

.amount-field div {
  border-bottom: 3px solid var(--line);
}

.submit-btn {
  border: 2px solid var(--line);
  box-shadow: 4px 4px 0 var(--line);
}

#toast {
  border: 3px solid var(--line);
  border-radius: 0;
  background: var(--yellow);
  color: var(--text);
  box-shadow: 5px 5px 0 var(--line);
  font-weight: 700;
}

.progress,
.large-progress,
.spend-track,
.today-progress {
  border: 2px solid var(--line);
  border-radius: 0;
  background: var(--surface2);
}

.progress i,
.large-progress i,
.spend-track i,
.today-progress i {
  border-radius: 0;
}

.more-btn {
  padding: 4px 8px;
  background: var(--surface2);
  color: var(--text);
}

.calendar-panel {
  border: 3px solid var(--line);
}

.calendar-head {
  border-bottom: 2px dashed var(--line);
}

.calendar-actions button {
  background: var(--surface2);
  color: var(--text);
}

.calendar-day {
  border: 2px solid var(--line);
  margin: -1px 0 0 -1px;
  background: var(--surface);
}

.calendar-day.today .calendar-number {
  border: 2px solid var(--line);
  border-radius: 0;
  background: var(--orange);
  color: var(--text);
}

.calendar-day.selected,
.calendar-day:hover {
  box-shadow: inset 0 0 0 3px var(--pixel-blue);
  background: var(--green-soft);
}

.day-summary,
.filter-clear,
.recurring-actions button,
.legend-row:hover,
.legend-row.active,
.agent-head-actions > button:first-child,
.agent-quick-actions button {
  border-radius: 0;
}

.day-summary {
  border: 2px solid var(--line);
  background: var(--green-soft);
}

.cycle-total-row {
  border-top: 2px dotted var(--line);
}

.cycle-total-row:hover,
.cycle-total-row.active {
  background: var(--green-soft);
}

.ai-btn:hover,
.primary-btn:hover {
  filter: brightness(1.06) saturate(1.1);
}

.analysis-modal {
  border-color: var(--pixel-purple);
}

.agent-quick-actions button {
  background: var(--surface2);
  color: var(--text);
}

.agent-message > span {
  border: 2px solid var(--line);
  border-radius: 0;
  background: var(--green-soft);
}

.agent-message.user > div {
  border: 2px solid var(--line);
  border-radius: 0;
  background: var(--pixel-blue);
  color: #fff8df;
  box-shadow: 3px 3px 0 var(--line);
}

.agent-error {
  border: 2px solid var(--line);
  border-radius: 0;
  background: var(--orange-soft);
}

@media (max-width: 720px) {
  .sidebar {
    border-top: 3px solid var(--line);
  }

  main {
    padding: 22px 13px 92px;
  }

  .topbar {
    gap: 16px;
  }

  .top-actions {
    gap: 7px;
  }

  .primary-btn,
  .ai-btn {
    padding: 8px 9px;
    font-size: 10px;
  }

  .stat-card,
  .panel {
    box-shadow: 3px 3px 0 var(--line);
  }
}

/* Mobile app layout. Loaded after the page theme so it owns the final small-screen rules. */

@media (max-width: 720px) {
  :root {
    --mobile-nav-height: 66px;
  }

  html,
  body {
    width: 100%;
    max-width: 100%;
    min-height: 100%;
    overflow-x: clip;
    overscroll-behavior-x: none;
  }

  body {
    min-height: 100dvh;
    -webkit-tap-highlight-color: transparent;
  }

  .shell,
  main,
  .page-content,
  .panel,
  .stat-card,
  .topbar,
  .section-head,
  .content-grid,
  .stats-grid {
    min-width: 0;
    max-width: 100%;
  }

  img,
  svg,
  canvas,
  video {
    max-width: 100%;
  }

  button,
  a,
  input,
  select,
  textarea {
    min-width: 0;
  }

  p,
  small,
  strong,
  b,
  code,
  .tx-info,
  .panel-head > div {
    overflow-wrap: anywhere;
  }

  .shell {
    display: block;
    min-height: 100dvh;
  }

  main {
    width: 100%;
    margin: 0;
    padding:
      calc(18px + env(safe-area-inset-top)) 12px
      calc(var(--mobile-nav-height) + 20px + env(safe-area-inset-bottom));
  }

  .sidebar {
    position: fixed;
    z-index: 50;
    inset: auto 0 0;
    width: 100%;
    height: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom));
    padding: 5px 4px calc(5px + env(safe-area-inset-bottom));
    border: 0;
    border-top: 3px solid var(--line);
    background: var(--surface);
    box-shadow: 0 -4px 0 color-mix(in srgb, var(--line) 13%, transparent);
  }

  .brand,
  .sidebar-foot {
    display: none;
  }

  .sidebar nav {
    display: grid;
    grid-template-columns: repeat(8, minmax(0, 1fr));
    gap: 0;
    width: 100%;
    height: 100%;
  }

  .nav-item {
    display: grid;
    grid-template-rows: 22px 12px;
    place-items: center;
    align-content: center;
    gap: 2px;
    min-width: 0;
    min-height: 52px;
    padding: 3px 0;
    border: 0;
    border-top: 2px solid transparent;
    font-size: 0;
    line-height: 1;
  }

  .nav-item span {
    width: auto;
    font-size: 16px;
    line-height: 20px;
  }

  .nav-item::after {
    color: inherit;
    font-size: 8px;
    font-weight: 700;
    line-height: 1;
    white-space: nowrap;
  }

  .nav-item[data-page-link="dashboard"]::after { content: "总览"; }
  .nav-item[data-page-link="transactions"]::after { content: "明细"; }
  .nav-item[data-page-link="budget"]::after { content: "预算"; }
  .nav-item[data-page-link="accounts"]::after { content: "账户"; }
  .nav-item[data-page-link="market"]::after { content: "行情"; }
  .nav-item[data-page-link="fire"]::after { content: "FIRE"; }
  .nav-item[data-page-link="planner"]::after { content: "笔记"; }
  .nav-item[data-page-link="settings"]::after { content: "设置"; }

  .nav-item:hover,
  .nav-item.active {
    border-color: var(--green);
    background: var(--green-soft);
    box-shadow: none;
    transform: none;
  }

  .topbar {
    display: grid;
    align-items: start;
    gap: 14px;
    margin-bottom: 20px;
  }

  .topbar > div:first-child {
    min-width: 0;
  }

  .topbar h1 {
    max-width: none;
    font-size: 20px;
    line-height: 1.35;
  }

  .eyebrow {
    margin-bottom: 6px;
    font-size: 9px;
  }

  .top-actions {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
    gap: 8px;
    width: 100%;
  }

  .top-actions .icon-btn {
    display: none;
  }

  .top-actions .primary-btn,
  .top-actions .ai-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    padding: 8px 10px;
    font-size: 10px;
    line-height: 1.2;
    text-align: center;
    white-space: normal;
  }

  .section-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 16px;
  }

  .section-head > div,
  .section-head p {
    max-width: 100%;
  }

  .section-head > .primary-btn,
  .section-head > button {
    width: 100%;
    min-height: 40px;
  }

  .panel,
  .stat-card {
    min-width: 0;
    padding: 15px;
    box-shadow: 3px 3px 0 var(--line);
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
  }

  .stat-card {
    min-height: 128px;
  }

  .stat-card > strong {
    margin: 14px 0 7px;
    font-size: clamp(17px, 5.4vw, 21px);
    overflow-wrap: anywhere;
  }

  .stat-head,
  .stat-card small,
  .panel-head p,
  .section-head p {
    font-size: 9px;
    line-height: 1.55;
  }

  .content-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .panel-head {
    min-width: 0;
    gap: 10px;
  }

  .filters,
  .form-grid,
  .settings-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 9px;
  }

  .account-session-panel {
    align-items: stretch;
    flex-direction: column;
  }

  .filters input,
  .filters select,
  .filters button,
  .form-grid input,
  .form-grid select {
    width: 100%;
  }

  .transaction {
    grid-template-columns: 34px minmax(0, 1fr) auto;
    gap: 8px;
    padding: 12px 0;
  }

  .tx-icon {
    width: 32px;
    height: 32px;
    font-size: 14px;
  }

  .tx-info {
    min-width: 0;
  }

  .tx-info b,
  .tx-info small {
    white-space: normal;
  }

  .tx-amount {
    max-width: 116px;
    font-size: 11px;
    white-space: nowrap;
  }

  .tx-amount small {
    white-space: normal;
  }

  .calendar-head {
    gap: 8px;
    padding: 12px;
  }

  .calendar-head h2 {
    font-size: 14px;
  }

  .calendar-actions {
    flex: none;
  }

  .calendar-actions button {
    min-width: 28px;
    height: 30px;
    padding: 0 7px;
    font-size: 9px;
  }

  .calendar-weekdays {
    padding-inline: 4px;
  }

  .transaction-calendar {
    padding: 0 4px 8px;
  }

  .calendar-day {
    height: 58px;
    padding: 4px;
  }

  .cycle-totals-panel,
  .fire-table-panel {
    overflow: visible;
  }

  .cycle-totals-panel > .panel-head {
    min-width: 0;
  }

  .cycle-total-head,
  .fire-table-head {
    display: none;
  }

  .cycle-total-row {
    min-width: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 11px 8px;
    margin-top: 10px;
    padding: 12px;
    border: 2px solid var(--line);
    background: var(--surface2);
  }

  .cycle-total-row > span:first-child {
    grid-column: 1 / -1;
  }

  .cycle-total-row strong,
  .cycle-total-row small {
    display: grid;
    gap: 4px;
    text-align: left;
  }

  .cycle-total-row strong::before,
  .cycle-total-row small::before,
  .fire-table-row > span::before,
  .fire-table-row > b::before {
    color: var(--muted);
    font-size: 8px;
    font-weight: 400;
  }

  .cycle-total-row strong:nth-child(2)::before { content: "收入"; }
  .cycle-total-row strong:nth-child(3)::before { content: "支出"; }
  .cycle-total-row strong:nth-child(4)::before { content: "结余"; }
  .cycle-total-row small::before { content: "记录"; }

  .fire-table-row {
    min-width: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px 8px;
    margin-top: 10px;
    padding: 12px;
    border: 1px solid var(--line);
    background: var(--surface2);
  }

  .fire-table-row > span:first-child {
    grid-column: 1 / -1;
    font-weight: 700;
  }

  .fire-table-row > span:nth-child(2),
  .fire-table-row > span:nth-child(3),
  .fire-table-row > b {
    display: grid;
    gap: 4px;
    overflow-wrap: anywhere;
  }

  .fire-table-row > span:nth-child(2)::before { content: "账户余额"; }
  .fire-table-row > span:nth-child(3)::before { content: "计入比例"; }
  .fire-table-row > b::before { content: "计入 FIRE 资产"; }

  .fire-table-row > b {
    grid-column: 1 / -1;
  }

  .fire-table-total {
    min-width: 0;
    align-items: flex-end;
    gap: 10px;
    padding-inline: 0;
  }

  .fire-table-total b {
    max-width: 55%;
    text-align: right;
    overflow-wrap: anywhere;
  }

  .planner-toolbar {
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px;
  }

  .planner-toolbar > span {
    display: none;
  }

  .planner-toolbar .template-btn {
    flex: 1 1 120px;
    max-width: none;
  }

  .planner-toolbar .mode-toggle {
    flex: none;
  }

  #planEditor {
    padding: 16px;
    font-size: 13px;
  }

  .markdown-body {
    padding: 16px;
  }

  .table-scroll {
    max-width: 100%;
    overscroll-behavior-x: contain;
  }

  .fire-settings-form {
    grid-template-columns: 1fr;
  }

  .fire-factor-row {
    grid-template-columns: minmax(0, 1fr) 86px;
  }

  .fire-subhead {
    align-items: flex-start;
    flex-direction: column;
  }

  .recurring-item {
    grid-template-columns: 32px minmax(0, 1fr);
  }

  .recurring-actions {
    grid-column: 2;
    justify-content: flex-start;
  }

  .account-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
  }

  .account-grid,
  .budget-detail-grid {
    grid-template-columns: 1fr;
  }

  .legend-row {
    gap: 7px;
    padding-inline: 6px;
  }

  .legend-name {
    min-width: 0;
  }

  .legend-row > b {
    font-size: 11px;
  }

  .legend-row > small {
    width: 42px;
    font-size: 9px;
  }

  .market-chart-panel,
  .market-card,
  .market-hero,
  .market-note {
    padding: 15px;
  }

  .market-price-row,
  .position-actions,
  .market-card footer {
    flex-wrap: wrap;
  }

  .market-card footer small {
    text-align: left;
  }

  .chart-frame {
    width: 100%;
    height: 210px;
  }

  .allocation-frame {
    height: 190px;
  }

  .market-metrics,
  .portfolio-stats {
    gap: 5px;
  }

  .market-metrics > div,
  .portfolio-stats > div {
    min-width: 0;
    padding: 8px 5px;
  }

  .market-note {
    gap: 12px;
  }

  dialog {
    width: calc(100% - 20px);
    max-width: calc(100% - 20px);
    max-height: calc(100dvh - 24px);
    margin: auto;
  }

  dialog form {
    max-height: calc(100dvh - 24px);
    padding: 18px;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .amount-field input {
    min-width: 0;
    font-size: 30px;
  }

  #toast {
    bottom: calc(var(--mobile-nav-height) + 12px + env(safe-area-inset-bottom));
    max-width: calc(100% - 24px);
    text-align: center;
  }

  .analysis-modal-inner {
    width: 100%;
    max-width: 100%;
    padding: 15px;
  }

  .agent-head {
    gap: 8px;
  }

  .agent-form {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 360px) {
  main {
    padding-inline: 10px;
  }

  .stats-grid,
  .account-summary {
    grid-template-columns: 1fr;
  }

  .nav-item::after {
    font-size: 7px;
  }

  .top-actions {
    grid-template-columns: repeat(auto-fit, minmax(95px, 1fr));
  }

  .trend-stats,
  .portfolio-stats,
  .market-metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .portfolio-stats > div:last-child,
  .market-metrics > div:last-child {
    grid-column: 1 / -1;
  }
}
