:root {
  --bg: #f5f7f4;
  --surface: #ffffff;
  --surface-2: #eef3f0;
  --ink: #1b2620;
  --muted: #647168;
  --line: #dbe3de;
  --green: #168255;
  --red: #b64242;
  --blue: #315f9f;
  --amber: #a66d12;
  --shadow: 0 18px 45px rgba(23, 35, 29, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

[hidden] {
  display: none !important;
}

.auth-screen {
  display: grid;
  min-height: 100dvh;
  place-items: center;
  padding: 18px;
  background: var(--bg);
}

.auth-card {
  display: grid;
  width: min(420px, 100%);
  gap: 12px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.auth-card .brand {
  justify-content: center;
  margin-bottom: 6px;
}

.auth-card label {
  display: grid;
  gap: 5px;
}

.auth-card label span,
.remember-row span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.auth-card input,
.control-bar select {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 10px;
  color: var(--ink);
  background: #fff;
}

.remember-row {
  display: flex !important;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 8px !important;
}

.remember-row[hidden] {
  display: none !important;
}

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

.auth-actions:has(button[hidden]) {
  grid-template-columns: 1fr;
}

.auth-actions button,
.control-bar button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #fff;
  font-weight: 800;
  cursor: pointer;
}

.auth-actions button[type="submit"] {
  color: #fff;
  background: var(--green);
  border-color: var(--green);
}

.form-error,
.form-help {
  color: var(--red);
  font-weight: 700;
}

.form-help {
  color: var(--green);
}

.link-button {
  border: 0;
  background: transparent;
  color: var(--blue);
  font-weight: 800;
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100dvh;
  padding: 12px;
  gap: 10px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 46px;
}

.control-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.user-pill {
  max-width: 180px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 10px;
  color: var(--ink);
  background: #fff;
  font-size: 0.82rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.control-bar select {
  width: min(280px, 34vw);
  padding-block: 9px;
}

.control-bar button {
  min-height: 38px;
  padding: 0 10px;
  white-space: nowrap;
}

.workspace {
  display: grid;
  min-height: 0;
}

.sidebar,
.chat-area,
.ledger,
.files-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.sidebar,
.ledger,
.files-panel {
  padding: 16px;
  overflow: hidden;
}

.brand {
  display: flex;
  gap: 10px;
  align-items: center;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  object-fit: cover;
}

.brand strong,
.brand span {
  display: block;
}

.brand span,
.metric span,
.message-time,
.transaction-meta,
.empty {
  color: var(--muted);
  font-size: 0.84rem;
}

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

.panel-hero {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.panel-hero p {
  margin-top: 4px;
  color: var(--muted);
}

.metric {
  min-height: 88px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}

.metric-link {
  cursor: pointer;
}

.metric-link:hover,
.metric-link:focus-visible {
  border-color: rgba(49, 95, 159, 0.45);
  outline: none;
  box-shadow: 0 0 0 3px rgba(49, 95, 159, 0.12);
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: clamp(1rem, 2vw, 1.3rem);
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.metric.result strong {
  color: var(--green);
}

.panel {
  margin-top: 18px;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.ledger .panel-title:not(:first-child) {
  margin-top: 18px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.25rem, 3vw, 1.8rem);
}

h2 {
  font-size: 0.95rem;
}

.chat-area {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.topbar p {
  color: var(--muted);
  margin-top: 4px;
}

.upload-button {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: #f7faf8;
  color: var(--blue);
  font-weight: 700;
  cursor: pointer;
}

.upload-button input {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
}

.file-input {
  position: fixed;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
  pointer-events: none;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
  padding: 16px;
}

.message {
  max-width: min(75ch, 86%);
  border-radius: 8px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  background: #f9fbfa;
}

.message.user {
  align-self: flex-end;
  color: #fff;
  background: var(--blue);
  border-color: var(--blue);
}

.message.assistant {
  align-self: flex-start;
}

.message p {
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.message.user .message-time {
  color: rgba(255, 255, 255, 0.78);
}

.chat-pending-card {
  display: grid;
  gap: 8px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.chat-pending-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px;
  border: 1px solid rgba(38, 80, 58, 0.16);
  border-radius: 8px;
  background: #fff;
}

.chat-pending-row strong,
.chat-pending-row span {
  display: block;
}

.chat-pending-row span,
.chat-pending-extra {
  color: var(--muted);
  font-size: 0.84rem;
}

.chat-pending-actions,
.chat-bulk-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}

.chat-pending-actions button,
.chat-bulk-actions button {
  min-height: 32px;
  padding: 0 10px;
  font-size: 0.8rem;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 14px;
  border-top: 1px solid var(--line);
  background: #fbfcfb;
}

.composer input {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 13px 12px;
  color: var(--ink);
  background: #fff;
}

.composer button {
  border: 0;
  border-radius: 8px;
  padding: 0 18px;
  color: #fff;
  background: var(--green);
  font-weight: 800;
  cursor: pointer;
}

.category-list,
.document-list,
.transaction-list {
  display: grid;
  gap: 8px;
  overflow: auto;
  padding-right: 2px;
}

.category-list {
  max-height: min(42dvh, 380px);
}

.document-list,
.transaction-list {
  max-height: min(48dvh, 520px);
}

.pending-list {
  max-height: min(56dvh, 580px);
}

.panel-screen {
  display: none;
  min-height: 0;
}

.panel-screen.active {
  display: block;
}

.chat-area.panel-screen.active {
  display: grid;
}

.tabbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: var(--shadow);
}

.tabbar button {
  position: relative;
  display: grid;
  min-height: 38px;
  place-items: center;
  border: 0;
  border-radius: 8px;
  color: var(--muted);
  background: transparent;
  font-weight: 800;
  cursor: pointer;
}

.tabbar button.active {
  color: #fff;
  background: var(--blue);
}

.tabbar svg {
  width: 21px;
  height: 21px;
  fill: currentColor;
}

.badge {
  display: inline-grid;
  min-width: 20px;
  height: 20px;
  margin-left: 4px;
  place-items: center;
  border-radius: 999px;
  color: #fff;
  background: var(--red);
  font-size: 0.72rem;
  line-height: 1;
}

.category-row,
.document-row,
.transaction-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #fbfcfb;
}

.confirmed-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: start;
}

.confirmed-row .transaction-meta:last-child {
  display: grid;
  gap: 6px;
  justify-items: end;
}

.danger-button {
  border-color: rgba(176, 54, 54, 0.32);
  color: #9f2f2f;
  background: #fff7f5;
}

.document-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}

.open-file-button {
  display: inline-grid;
  min-height: 34px;
  padding: 0 12px;
  place-items: center;
  border: 1px solid rgba(38, 80, 58, 0.28);
  border-radius: 8px;
  color: var(--green-900);
  background: #f4faf6;
  font-size: 0.86rem;
  font-weight: 800;
  text-decoration: none;
}

.pending-row {
  border-color: rgba(166, 109, 18, 0.42);
  background: #fffaf1;
}

.segmented-control {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}

.segmented-control button {
  min-height: 32px;
  padding: 0 10px;
  border: 0;
  border-radius: 0;
  color: var(--muted);
  background: transparent;
  font-size: 0.8rem;
  font-weight: 800;
}

.segmented-control button.active {
  color: #fff;
  background: var(--green);
}

.category-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  cursor: pointer;
}

.category-row:hover,
.category-row:focus-visible {
  border-color: rgba(49, 95, 159, 0.38);
  outline: none;
  background: #f7fbff;
}

.bar {
  grid-column: 1 / -1;
  height: 7px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--surface-2);
}

.bar span {
  display: block;
  height: 100%;
  background: var(--amber);
}

.category-row.income .bar span {
  background: var(--green);
}

.category-row.expense .bar span {
  background: var(--red);
}

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

.insight-card {
  display: grid;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.insight-title {
  display: flex;
  gap: 6px;
  justify-content: space-between;
  align-items: baseline;
}

.insight-title span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.monthly-chart {
  display: grid;
  grid-template-columns: repeat(6, minmax(28px, 1fr));
  gap: 8px;
  min-height: 146px;
  align-items: end;
}

.month-bars {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.bars {
  display: flex;
  gap: 3px;
  height: 112px;
  align-items: end;
  justify-content: center;
  padding: 7px 4px;
  border-radius: 8px;
  background: var(--surface-2);
}

.bars span {
  width: 9px;
  min-height: 4px;
  border-radius: 999px 999px 2px 2px;
}

.income-bar {
  background: var(--green);
}

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

.month-bars small {
  overflow: hidden;
  color: var(--muted);
  font-size: 0.72rem;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.comparison-list {
  display: grid;
  gap: 8px;
}

.comparison-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 7px;
  border-bottom: 1px solid var(--line);
}

.comparison-row:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.comparison-row .income,
.big-number.income {
  color: var(--green);
}

.comparison-row .expense,
.big-number.expense {
  color: var(--red);
}

.big-number {
  font-size: 1.55rem;
}

.transaction-row strong,
.document-row strong {
  display: block;
  margin-bottom: 4px;
  line-height: 1.25;
}

.transaction-value {
  font-weight: 800;
}

.transaction-value.income {
  color: var(--green);
}

.transaction-value.expense {
  color: var(--red);
}

.review-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.filter-summary {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.82rem;
}

.filter-clear-button {
  min-height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--blue);
  background: #fff;
  font-size: 0.82rem;
  font-weight: 800;
}

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

.ledger-filters label {
  display: grid;
  gap: 4px;
}

.ledger-filters span {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
}

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

.review-actions button {
  min-height: 36px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  font-weight: 800;
  cursor: pointer;
}

.review-actions button[data-action="confirm"] {
  color: #fff;
  background: var(--green);
  border-color: var(--green);
}

.review-actions button[data-action="update"] {
  color: var(--blue);
}

.review-actions button[data-action="dismiss"] {
  color: var(--red);
}

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

.review-form {
  display: grid;
  gap: 9px;
}

.review-form label {
  display: grid;
  gap: 4px;
}

.review-form label span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
}

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

.review-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

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

@media (min-width: 920px) {
  .workspace {
    width: min(1180px, 100%);
    margin: 0 auto;
  }

  .panel-screen.active {
    min-height: calc(100dvh - 184px);
  }

  .app-header {
    width: min(1180px, 100%);
    margin: 0 auto;
  }

  .tabbar {
    width: min(760px, 100%);
    margin: 0 auto;
  }

  .chat-area {
    min-height: calc(100dvh - 184px);
  }

  .messages {
    max-height: calc(100dvh - 320px);
  }

  .ledger,
  .sidebar,
  .files-panel {
    max-height: calc(100dvh - 184px);
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 8px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  .brand-mark {
    width: 34px;
    height: 34px;
  }

  .brand strong {
    font-size: 0.95rem;
  }

  .brand span {
    font-size: 0.78rem;
  }

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

  .control-bar {
    display: grid;
    grid-template-columns: 1fr auto auto auto;
  }

  .user-pill {
    grid-column: 1 / -1;
    max-width: 100%;
    width: 100%;
  }

  .control-bar select {
    width: 100%;
  }

  .control-bar button {
    padding: 0 8px;
    font-size: 0.78rem;
  }

  .auth-actions {
    grid-template-columns: 1fr;
  }

  .chat-area,
  .sidebar,
  .ledger,
  .files-panel {
    min-height: calc(100dvh - 208px);
    max-height: calc(100dvh - 208px);
  }

  .messages {
    max-height: calc(100dvh - 378px);
  }

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

  .panel-hero {
    flex-direction: column;
  }

  .upload-button {
    width: 100%;
    text-align: center;
  }

  .message {
    max-width: 94%;
  }

  .chat-pending-row {
    grid-template-columns: 1fr;
  }

  .chat-pending-actions,
  .chat-bulk-actions {
    justify-content: flex-start;
  }

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

  .composer button {
    min-height: 44px;
  }

  .review-actions,
  .review-grid,
  .optional-grid,
  .ledger-filters {
    grid-template-columns: 1fr;
  }

  .confirmed-row {
    grid-template-columns: 1fr;
  }

  .confirmed-row .transaction-meta:last-child {
    justify-items: start;
  }

  .document-row {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

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

  .document-list,
  .transaction-list,
  .pending-list,
  .category-list {
    max-height: calc(100dvh - 248px);
  }
}
