:root {
  color-scheme: light;
  --ink: #17243a;
  --muted: #66758a;
  --panel: #ffffff;
  --line: #dbe5f1;
  --wash: #f3f7fc;
  --accent: #1f6fd1;
  --accent-strong: #164f99;
  --orange: #f28c28;
  --orange-soft: #fff3e4;
  --user: #173b70;
  --assistant: #eaf2ff;
  --soft: #f8fbff;
  --danger: #c45124;
  --income: #1f7a59;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--wash);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow: hidden;
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: var(--wash);
  padding: 20px;
}

.auth-gate.hidden {
  display: none;
}

.auth-panel {
  display: grid;
  gap: 12px;
  width: min(100%, 360px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 20px;
}

.auth-panel h1 {
  margin: 0 0 4px;
  font-size: 28px;
}

.auth-panel input {
  height: 46px;
  padding: 0 12px;
}

.auth-panel button {
  height: 46px;
  border: 0;
  border-radius: 8px;
  background: var(--orange);
  color: #fff;
  font-weight: 750;
}

.auth-error {
  color: var(--danger);
  font-size: 13px;
}

button,
textarea,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  min-height: 100dvh;
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
  background: var(--panel);
  padding: calc(env(safe-area-inset-top) + 12px) 12px calc(env(safe-area-inset-bottom) + 10px);
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 2px 2px 12px;
}

.identity {
  flex: 1;
  min-width: 0;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--orange);
  font-size: 12px;
  font-weight: 750;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.08;
}

h2 {
  font-size: 22px;
}

.avatar-control {
  position: relative;
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  width: 50px;
  height: 50px;
  overflow: hidden;
  border: 2px solid rgba(242, 140, 40, 0.46);
  border-radius: 50%;
  background: linear-gradient(135deg, var(--assistant), var(--orange-soft));
  color: var(--accent-strong);
  font-weight: 850;
}

.avatar-control input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.avatar-control img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#avatarFallback {
  font-size: 20px;
}

#displayName {
  width: min(100%, 220px);
  height: 32px;
  border: 0;
  border-radius: 0;
  background: transparent;
  padding: 0;
  color: var(--ink);
  font-size: 26px;
  font-weight: 850;
  line-height: 1.08;
  box-shadow: none;
}

#displayName:focus {
  border-bottom: 2px solid var(--orange);
  box-shadow: none;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  font-size: 22px;
}

.status-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
  padding-bottom: 10px;
}

.status-strip article,
.metric,
.account {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  min-width: 0;
  padding: 9px;
}

.status-strip span,
.metric span,
.account span,
.ledger-item span,
.section-head p {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.status-strip strong {
  display: block;
  margin-top: 4px;
  overflow-wrap: anywhere;
  font-size: 17px;
  line-height: 1.1;
}

.danger {
  color: var(--danger);
}

.view {
  display: none;
  min-height: 0;
  overflow: hidden;
}

#botView.active {
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.chat-tools {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 8px;
  padding-bottom: 9px;
}

.chat-tools label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
}

.chat-tools input {
  height: 38px;
  padding: 0 10px;
}

.chat-tools button {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--accent-strong);
  font-weight: 750;
  padding: 0 12px;
}

#reviewView.active,
#ledgerView.active {
  display: block;
  overflow-y: auto;
  padding-bottom: 10px;
  -webkit-overflow-scrolling: touch;
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 7px;
  overflow-y: auto;
  padding: 4px 0 14px;
  -webkit-overflow-scrolling: touch;
}

.message {
  position: relative;
  width: fit-content;
  max-width: min(78%, 520px);
  padding: 7px 11px;
  border-radius: 17px;
  line-height: 1.38;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: 0 1px 1px rgba(23, 36, 58, 0.04);
}

.message > span {
  display: block;
  max-width: 100%;
}

.message button {
  position: absolute;
  top: -6px;
  right: -6px;
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border: 0;
  border-radius: 50%;
  background: rgba(23, 36, 58, 0.18);
  color: #fff;
  opacity: 0;
  font-size: 13px;
  line-height: 1;
  padding: 0;
}

.message:hover button,
.message:focus-within button {
  opacity: 1;
}

.message.user {
  align-self: flex-end;
  background: var(--user);
  color: #fff;
  border-bottom-right-radius: 5px;
}

.message.assistant {
  align-self: flex-start;
  background: var(--assistant);
  border-bottom-left-radius: 5px;
}

.action-card {
  align-self: flex-start;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  width: min(92%, 420px);
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  background: #fff;
  padding: 11px 12px;
}

.action-card.income {
  border-left-color: var(--income);
}

.action-card div {
  display: grid;
  gap: 4px;
}

.action-card strong {
  font-size: 16px;
}

.action-card span,
.card-actions {
  color: var(--muted);
  font-size: 12px;
}

.card-actions {
  display: flex;
  align-items: start;
  gap: 6px;
  white-space: nowrap;
}

.card-actions button {
  border: 0;
  background: transparent;
  color: var(--accent-strong);
  font-weight: 750;
  padding: 2px 0;
}

.edit-card {
  align-self: flex-start;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  width: min(92%, 520px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  padding: 10px;
}

.edit-card label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
}

.edit-card input,
.edit-card select {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 9px;
}

.edit-actions {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.edit-actions button {
  height: 38px;
  border-radius: 8px;
  font-weight: 750;
}

.edit-actions button:first-child {
  border: 0;
  background: var(--orange);
  color: #fff;
}

.edit-actions button:last-child {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 9px;
  padding-top: 9px;
  padding-bottom: 2px;
  border-top: 1px solid var(--line);
}

textarea,
input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
  color: var(--ink);
  background: #fff;
}

textarea:focus,
input:focus {
  border-color: rgba(31, 111, 209, 0.65);
  box-shadow: 0 0 0 3px rgba(31, 111, 209, 0.13);
}

textarea {
  resize: none;
}

#messageInput {
  max-height: 132px;
  min-height: 46px;
  padding: 12px;
}

#sendButton,
.settings-form button,
.ledger-toolbar button,
.import-button {
  min-width: 74px;
  height: 46px;
  border: 0;
  border-radius: 8px;
  background: var(--orange);
  color: #fff;
  font-weight: 750;
}

.import-button {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0 12px;
}

.import-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.section-head {
  display: grid;
  gap: 5px;
  padding: 2px 0 10px;
}

.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.section-title-row input {
  width: 142px;
  height: 36px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 13px;
}

.metric-grid {
  display: grid;
  gap: 10px;
}

.review-group {
  display: grid;
  gap: 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 11px;
}

.group-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.group-title h3 {
  margin: 0;
  font-size: 16px;
}

.group-title span {
  color: var(--muted);
  font-size: 12px;
}

.hero-metric {
  display: grid;
  gap: 5px;
  border-radius: 8px;
  background: #eaf2ff;
  padding: 13px;
}

.hero-metric.compact {
  background: #fff8ef;
}

.hero-metric span {
  color: var(--muted);
  font-size: 12px;
}

.hero-metric strong {
  overflow-wrap: anywhere;
  font-size: 30px;
  line-height: 1;
}

.hero-metric p {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.mini-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
}

.category-chart {
  display: grid;
  grid-template-columns: 118px 1fr;
  align-items: center;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.donut {
  position: relative;
  display: grid;
  place-items: center;
  width: 118px;
  aspect-ratio: 1;
  border-radius: 50%;
  background: conic-gradient(var(--line) 0 100%);
}

.donut::after {
  content: "";
  position: absolute;
  inset: 24px;
  border-radius: 50%;
  background: #fff;
}

.donut span,
.donut small {
  position: relative;
  z-index: 1;
}

.donut span {
  font-size: 17px;
  font-weight: 850;
}

.donut small {
  color: var(--muted);
  font-size: 11px;
}

.category-chart.empty {
  grid-template-columns: 72px 1fr;
}

.category-chart.empty .donut {
  width: 72px;
}

.category-chart.empty .donut::after {
  inset: 18px;
}

.category-chart.empty p {
  color: var(--muted);
  font-size: 12px;
}

.chart-legend {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.chart-legend article {
  display: grid;
  grid-template-columns: 10px 1fr auto 34px;
  align-items: center;
  gap: 7px;
  min-width: 0;
  color: var(--muted);
  font-size: 12px;
}

.chart-legend i {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.chart-legend span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chart-legend strong {
  color: var(--ink);
  font-size: 12px;
}

.chart-legend em {
  color: var(--muted);
  font-style: normal;
  text-align: right;
}

.metric {
  min-width: 0;
}

.metric strong,
.account strong {
  display: block;
  margin-top: 6px;
  overflow-wrap: anywhere;
  font-size: 17px;
  line-height: 1.15;
}

.metric.positive strong,
.positive {
  color: var(--income);
}

.metric.negative strong,
.negative {
  color: var(--danger);
}

.settings-form {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-top: 10px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.settings-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.settings-form input,
.ledger-toolbar input,
.ledger-toolbar select {
  height: 42px;
  padding: 0 10px;
}

.ledger-toolbar select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

.settings-form button {
  grid-column: 1 / -1;
}

.secondary-action {
  width: 100%;
  height: 42px;
  margin-bottom: 10px;
  border: 1px solid rgba(31, 111, 209, 0.28);
  border-radius: 8px;
  background: var(--assistant);
  color: var(--accent-strong);
  font-weight: 750;
}

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

.account {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.account small {
  display: block;
  margin-top: 4px;
  color: var(--muted);
}

.account-actions {
  display: grid;
  align-content: start;
  gap: 5px;
}

.account-actions button {
  border: 0;
  background: transparent;
  color: var(--accent-strong);
  font-weight: 750;
  padding: 0;
}

.account-editor {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  grid-column: 1 / -1;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.account-editor label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
}

.account-editor input,
.account-editor select {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 9px;
}

.checkbox-row {
  grid-template-columns: auto 1fr;
  align-items: center;
  align-content: center;
}

.checkbox-row input {
  width: 18px;
  height: 18px;
}

.ledger-toolbar {
  display: grid;
  grid-template-columns: minmax(94px, 0.45fr) 1fr auto auto;
  gap: 8px;
  margin: 12px 0;
}

.ledger-list {
  display: grid;
  align-content: start;
  gap: 8px;
  overflow-y: auto;
}

.ledger-item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding: 10px 2px;
}

.ledger-item div {
  display: grid;
  gap: 4px;
}

.ledger-item b {
  white-space: nowrap;
}

.ledger-amount {
  display: grid;
  justify-items: end;
  gap: 4px;
}

.ledger-amount button {
  border: 0;
  background: transparent;
  color: var(--accent-strong);
  font-weight: 750;
  padding: 0;
}

.ledger-item .expense {
  color: var(--danger);
}

.ledger-item .income {
  color: var(--income);
}

.empty-state {
  margin: 30px auto;
  max-width: 310px;
  color: var(--muted);
  text-align: center;
  line-height: 1.55;
}

.bottom-nav {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding-top: 9px;
  border-top: 1px solid var(--line);
}

.bottom-nav button {
  height: 43px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  font-weight: 750;
}

@media (max-width: 430px) {
  .topbar {
    padding-bottom: 10px;
  }

  .avatar-control {
    width: 46px;
    height: 46px;
  }

  #displayName {
    font-size: 24px;
  }

  .status-strip {
    gap: 6px;
  }

  .status-strip article {
    padding: 8px 7px;
  }

  .status-strip span,
  .metric span,
  .account span,
  .ledger-item span,
  .section-head p {
    font-size: 11px;
  }

  .status-strip strong {
    font-size: 15px;
  }

  .section-title-row input {
    width: 132px;
  }

  .mini-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .mini-grid .metric:nth-child(3) {
    grid-column: 1 / -1;
  }

  .hero-metric strong {
    font-size: 28px;
  }

  .category-chart {
    grid-template-columns: 96px 1fr;
    gap: 10px;
  }

  .donut {
    width: 96px;
  }

  .donut::after {
    inset: 20px;
  }

  .donut span {
    font-size: 15px;
  }

  .settings-form,
  .account-list,
  .edit-card,
  .account-editor {
    grid-template-columns: 1fr;
  }

  .ledger-toolbar {
    grid-template-columns: 1fr auto auto;
  }

  .ledger-toolbar select {
    grid-column: 1 / -1;
  }

  .message {
    max-width: 80%;
  }
}

@media (max-width: 360px) {
  .app-shell {
    padding-left: 10px;
    padding-right: 10px;
  }

  .avatar-control {
    width: 42px;
    height: 42px;
  }

  #displayName {
    font-size: 22px;
  }

  .status-strip {
    grid-template-columns: 1fr;
  }

  .status-strip article {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }

  .status-strip strong {
    margin-top: 0;
  }

  .category-chart {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .chart-legend {
    width: 100%;
  }

  .composer {
    grid-template-columns: 1fr;
  }

  #sendButton {
    width: 100%;
  }
}

.bottom-nav button.active {
  border-color: rgba(31, 111, 209, 0.28);
  background: var(--assistant);
  color: var(--accent-strong);
}

@media (min-width: 760px) {
  .app-shell {
    min-height: min(100dvh, 920px);
    margin-top: 20px;
    margin-bottom: 20px;
    border: 1px solid var(--line);
    border-radius: 8px;
  }
}
