:root {
  color-scheme: light;
  --bg: #f7f3ea;
  --surface: #fffdf8;
  --ink: #1b2522;
  --muted: #68736d;
  --line: #ddd6c8;
  --accent: #1f6f68;
  --accent-strong: #18544f;
  --income: #2f8f83;
  --expense: #e4572e;
  --gold: #f2c14e;
  --shadow: 0 16px 40px rgba(31, 43, 37, 0.12);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(31, 111, 104, 0.14), transparent 34%),
    linear-gradient(315deg, rgba(242, 193, 78, 0.18), transparent 30%),
    var(--bg);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 42px;
  border: 0;
  border-radius: 8px;
  background: #ece5d7;
  color: var(--ink);
  cursor: pointer;
}

.app-shell {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: 22px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 2px 20px;
}

.header-tools {
  display: grid;
  grid-template-columns: auto auto minmax(150px, 1fr);
  align-items: end;
  gap: 10px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 5vw, 3.2rem);
}

h2 {
  margin-bottom: 16px;
  font-size: 1.45rem;
}

h3 {
  margin-bottom: 14px;
  font-size: 1rem;
}

.month-picker {
  display: grid;
  gap: 6px;
  min-width: 150px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}

.month-picker input,
label input,
label select {
  width: 100%;
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 12px;
}

.sync-button {
  min-width: 78px;
  padding: 0 14px;
  background: var(--accent);
  color: white;
  font-weight: 900;
}

.sync-button.secondary {
  background: #ece5d7;
  color: var(--ink);
}

.sync-button:disabled {
  cursor: wait;
  opacity: 0.7;
}

.sync-status {
  align-self: center;
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  white-space: nowrap;
}

.sync-status[data-tone="ok"] {
  color: var(--income);
}

.sync-status[data-tone="warn"] {
  color: var(--expense);
}

.tab-bar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 8px;
  margin-bottom: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.9);
  backdrop-filter: blur(16px);
}

.tab-button {
  font-weight: 800;
}

.tab-button.is-active {
  background: var(--accent);
  color: white;
}

.screen-stack {
  overflow: hidden;
}

.screen {
  display: none;
}

.screen.is-active {
  display: block;
  animation: rise 180ms ease-out;
}

.summary-grid,
.chart-layout {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.chart-layout {
  grid-template-columns: minmax(280px, 0.9fr) minmax(320px, 1.1fr);
}

.metric,
.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.92);
  box-shadow: var(--shadow);
}

.metric {
  display: grid;
  gap: 8px;
  padding: 16px;
}

.metric span {
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.metric strong {
  font-size: clamp(1.15rem, 3vw, 1.8rem);
}

.income strong,
.transaction-row .income {
  color: var(--income);
}

.expense strong,
.transaction-row .expense,
.is-negative,
.alert-text.is-danger {
  color: var(--expense);
}

.panel {
  padding: 18px;
}

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

.form-grid h2,
.form-grid .full {
  grid-column: 1 / -1;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.segmented {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented label {
  display: grid;
  min-height: 44px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f2ecdf;
  color: var(--ink);
}

.segmented input:checked + label {
  background: var(--accent);
  color: white;
}

.primary-button {
  background: var(--accent);
  color: white;
  font-weight: 900;
}

.primary-button:hover,
.tab-button.is-active:hover {
  background: var(--accent-strong);
}

.icon-button {
  width: 46px;
  min-width: 46px;
  min-height: 46px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  font-size: 1.7rem;
  font-weight: 700;
  line-height: 1;
}

.ghost-button {
  min-height: 36px;
  padding: 0 12px;
  background: #f2ecdf;
}

.danger-button,
.danger-link {
  color: white;
  background: var(--expense);
  font-weight: 800;
}

.danger-link {
  min-height: 36px;
  padding: 0 12px;
  background: #f7ddd5;
  color: var(--expense);
}

.section-heading {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.section-heading p,
.alert-text {
  margin-bottom: 0;
  color: var(--muted);
  font-weight: 800;
}

.pie-chart {
  position: relative;
  width: min(250px, 72vw);
  aspect-ratio: 1;
  margin: 8px auto 18px;
  border-radius: 50%;
}

.pie-chart::after {
  content: attr(data-label);
  position: absolute;
  inset: 24%;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--surface);
  color: var(--ink);
  font-size: 1.5rem;
  font-weight: 900;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.legend span,
.report-row,
.transaction-row,
.category-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.legend i,
.dot {
  width: 12px;
  height: 12px;
  flex: 0 0 12px;
  border-radius: 50%;
}

.bar-chart {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  align-items: end;
  gap: 10px;
  min-height: 250px;
}

.bar-item {
  display: grid;
  grid-template-rows: 1fr auto auto;
  gap: 6px;
  min-width: 0;
  text-align: center;
}

.bar-track {
  position: relative;
  min-height: 160px;
  border-radius: 8px;
  background: #eee7da;
  overflow: hidden;
}

.bar-track span {
  position: absolute;
  inset: auto 0 0;
  background: linear-gradient(180deg, var(--gold), var(--expense));
}

.bar-item strong,
.bar-item small {
  overflow-wrap: anywhere;
}

.inline-form {
  display: flex;
  align-items: end;
  gap: 12px;
}

.inline-form label {
  flex: 1;
}

.inline-form button {
  min-width: 96px;
  padding: 0 16px;
  background: var(--accent);
  color: white;
  font-weight: 900;
}

.report-list,
.transaction-list,
.category-list {
  display: grid;
  gap: 10px;
}

.report-row,
.transaction-row,
.category-row {
  min-height: 56px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.report-row b,
.transaction-row b {
  margin-left: auto;
  white-space: nowrap;
}

.transaction-row div,
.category-row div {
  min-width: 0;
}

.transaction-row small,
.category-row small,
.report-row small {
  display: block;
  color: var(--muted);
}

.transaction-row button,
.category-row button {
  padding: 0 12px;
  min-height: 36px;
}

.category-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.modal {
  width: min(460px, calc(100vw - 28px));
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
}

.modal::backdrop {
  background: rgba(27, 37, 34, 0.48);
  backdrop-filter: blur(4px);
}

.modal-panel {
  display: grid;
  gap: 14px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.modal-header,
.modal-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-header h2 {
  margin-bottom: 0;
}

.modal-actions {
  justify-content: flex-end;
}

.modal-actions button {
  min-width: 94px;
  padding: 0 16px;
}

.help-text {
  margin-bottom: 0;
  color: var(--expense);
  font-size: 0.9rem;
  font-weight: 800;
}

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

.empty {
  margin: 0;
  color: var(--muted);
}

.fatal-error {
  padding: 24px;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 720px) {
  .app-shell {
    padding: 14px;
  }

  .app-header {
    align-items: stretch;
    flex-direction: column;
  }

  .header-tools {
    grid-template-columns: auto 1fr;
  }

  .month-picker {
    grid-column: 1 / -1;
  }

  .tab-bar {
    gap: 4px;
  }

  .tab-button {
    min-height: 40px;
    font-size: 0.86rem;
  }

  .summary-grid,
  .chart-layout,
  .form-grid {
    grid-template-columns: 1fr;
  }

  .metric {
    padding: 14px;
  }

  .section-heading,
  .inline-form {
    align-items: stretch;
    flex-direction: column;
  }

  .screen[data-screen="categories"] .section-heading {
    align-items: center;
    flex-direction: row;
  }

  .transaction-row {
    display: grid;
    grid-template-columns: 12px 1fr auto;
  }

  .transaction-row button {
    grid-column: 2 / -1;
  }

  .category-row {
    display: grid;
    grid-template-columns: 12px 1fr;
  }

  .category-actions {
    grid-column: 2 / -1;
    margin-left: 0;
  }
}
