:root {
  --bg: #0b1021;
  --card: #121a33;
  --muted: #9fb1d4;
  --text: #e8eeff;
  --accent: #5fe3c0;
  --accent-strong: #3ccf9b;
  --border: rgba(255, 255, 255, 0.08);
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
  --radius: 16px;
  --font: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --sidebar-width: 200px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  background: radial-gradient(circle at 20% 20%, rgba(95, 227, 192, 0.08), transparent 25%),
    radial-gradient(circle at 80% 10%, rgba(120, 149, 255, 0.08), transparent 25%),
    var(--bg);
  color: var(--text);
  /* PWA safe areas for iPhone notch */
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}

/* Sidebar */
.app-layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  background: var(--card);
  border-right: 1px solid var(--border);
  padding: 20px 0;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  z-index: 100;
}

.sidebar-brand {
  padding: 0 16px 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}

.sidebar-brand a {
  text-decoration: none;
  color: var(--accent);
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.02em;
}

.sidebar-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 8px;
}

.sidebar-section {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.sidebar-section-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  padding: 0 12px 8px;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--muted);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}

.sidebar-link:hover {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
}

.sidebar-link.active {
  background: rgba(95, 227, 192, 0.12);
  color: var(--accent);
}

.sidebar-icon {
  width: 18px;
  height: 18px;
  opacity: 0.7;
}

.sidebar-link.active .sidebar-icon {
  opacity: 1;
}

.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
}

.sidebar-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 101;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.sidebar-toggle svg {
  width: 20px;
  height: 20px;
}

.page {
  max-width: 1100px;
  padding: 16px 20px 48px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

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

.hero-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.hero h1 {
  margin: 2px 0 6px;
  font-size: 26px;
}

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

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 11px;
  color: var(--accent);
  margin: 0;
}

.pill {
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(95, 227, 192, 0.12);
  color: var(--accent);
  border: 1px solid rgba(95, 227, 192, 0.4);
}

.pill-link {
  text-decoration: none;
  color: var(--accent);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
  flex: 1;
  align-items: stretch;
}

.grid-auto {
  flex: 0;
  align-items: start;
  gap: 10px;
}

.card-compact {
  padding: 14px;
}

.card-compact h2 {
  margin: 0 0 6px;
}

.card-compact .sub {
  margin: 0 0 10px;
}

.card-compact .link-button {
  margin-top: 0;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.card-header h2 {
  margin: 0;
}

.card-header .pill {
  cursor: pointer;
  font-size: 14px;
}

.card-tight {
  padding-top: 10px;
}

.card-tight > h2 {
  margin-top: 0;
  margin-bottom: 6px;
}

.card-tight > .scroll-hint {
  margin-bottom: 0;
  margin-top: 2px;
}

.editor-card {
  display: flex;
  flex-direction: column;
  min-height: 520px;
}

.rules-list {
  margin-top: 16px;
  display: grid;
  gap: 8px;
  overflow: auto;
  padding-right: 4px;
  flex: 1;
  min-height: 0;
  max-height: calc(100vh - 320px);
}

.rules-list li {
  margin: 0;
}

.rules-editor {
  display: grid;
  gap: 12px;
  margin-top: 12px;
}

.rule-header {
  display: grid;
  grid-template-columns: 220px 1fr 1fr 1fr auto;
  gap: 12px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  padding: 0 12px;
}

.rule-row {
  display: grid;
  grid-template-columns: 220px 1fr 1fr 1fr auto;
  gap: 12px;
  align-items: start;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
}

.rule-row textarea {
  min-height: 80px;
  resize: vertical;
}

.rule-delete {
  align-self: stretch;
}

.category-list {
  display: grid;
  gap: 10px;
  margin-top: 12px;
  max-height: 240px;
  overflow: auto;
}

.category-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
}

form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

label {
  display: block;
  font-weight: 600;
  margin-bottom: 6px;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 500;
  margin: 10px 0 16px;
}

.checkbox input {
  width: 18px;
  height: 18px;
  margin: 0;
}

input,
textarea,
select {
  width: 100%;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 16px;
  font-family: var(--font);
  min-width: 0;
}

select {
  color-scheme: dark;
  background-color: #1a2340;
}

select option {
  background-color: #1a2340;
  color: #e8eeff;
}

textarea {
  flex: 1;
  min-height: 320px;
  resize: vertical;
}

input:focus,
textarea:focus {
  outline: 2px solid rgba(95, 227, 192, 0.45);
  border-color: rgba(95, 227, 192, 0.6);
  box-shadow: 0 0 0 3px rgba(95, 227, 192, 0.15);
}

.input-prefix {
  display: flex;
  align-items: center;
  gap: 8px;
}

.input-prefix span {
  padding: 12px 10px 12px 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.input-prefix input {
  flex: 1;
}

.two-up {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

.stack {
  display: grid;
  gap: 4px;
}

.stack-tight {
  gap: 6px;
}

.primary {
  padding: 14px 16px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #022013;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 30px rgba(95, 227, 192, 0.25);
}

.primary:active {
  transform: translateY(0);
}

.link-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-top: 12px;
}

.link-card {
  align-items: stretch;
  text-align: center;
}

.link-card .link-button {
  margin-top: 0;
  width: 100%;
}

.status {
  min-height: 20px;
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.status-inline {
  align-self: center;
}

.amount-positive {
  color: var(--accent);
}

.amount-negative {
  color: #f28c8c;
}

.filters-card {
  min-height: 420px;
}

.list-card {
  min-height: 520px;
  position: relative;
}

.filters-grid {
  display: grid !important;
  grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  gap: 12px 16px;
  width: 100%;
}

.filter-field {
  display: flex;
  flex-direction: column;
  grid-column: span 1;
}

.filter-span {
  grid-column: 1 / -1;
}

.filters-card {
  min-height: auto;
}

.transactions-list {
  display: grid;
  gap: 8px;
  overflow: auto;
  padding-right: 4px;
  flex: 1;
  min-height: 0;
  max-height: calc(100vh - 320px);
}


.totals-card {
  min-height: 220px;
}

.totals-card-tall {
  flex: 1;
  min-height: 400px;
}

.totals-card-tall .totals-list {
  max-height: calc(100vh - 280px);
}

.totals-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.totals-header .pill {
  margin-top: -5px;
  margin-left: 8px;
}

.totals-list {
  display: grid;
  gap: 8px;
  margin-top: 16px;
  overflow: auto;
  flex: 1;
  min-height: 0;
  max-height: 360px;
}

.totals-card {
  position: relative;
}

.scroll-hint {
  margin-top: 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.totals-card::after {
  content: "";
  position: absolute;
  left: 20px;
  right: 20px;
  bottom: 20px;
  height: 24px;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(18, 26, 51, 0), rgba(18, 26, 51, 0.9));
}

.totals-row {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) 80px 120px 140px auto;
  gap: 12px;
  align-items: center;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
}

.totals-row-header {
  background: rgba(255, 255, 255, 0.08);
  font-weight: 600;
}

.transaction-row {
  display: grid;
  grid-template-columns: 90px minmax(120px, 1fr) 70px 140px auto;
  gap: 8px;
  align-items: center;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
}

.transaction-row.header {
  background: rgba(255, 255, 255, 0.08);
  font-weight: 600;
}

.transaction-row .muted {
  color: var(--muted);
  font-size: 13px;
  margin-top: 2px;
}

.muted {
  color: var(--muted);
  font-size: 13px;
}

.transaction-row .amount {
  font-weight: 600;
}

.category-input {
  min-width: 0;
}

.transaction-row input,
.transaction-row select {
  padding: 6px 8px;
  font-size: 14px;
  background-color: #1a2340;
  color: #e8eeff;
}

.transaction-row select option {
  background-color: #1a2340;
  color: #e8eeff;
}

.list-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 10px;
}

.list-footer .status {
  margin: 0;
}

.list-footer-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.budget-list {
  display: grid;
  gap: 10px;
  overflow: auto;
  padding-right: 4px;
  flex: 1;
  min-height: 0;
  max-height: calc(100vh - 320px);
}

.budget-row {
  display: grid;
  grid-template-columns: minmax(140px, 1fr) 90px 120px 50px;
  gap: 8px;
  align-items: center;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
}

.budget-row .category-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.budget-row .category-cell strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.budget-row .category-cell .muted {
  flex-shrink: 0;
  font-size: 11px;
}

.budget-row input[type="number"] {
  padding: 6px 8px;
  font-size: 14px;
}

.budget-row select {
  width: 100%;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 13px;
  font-family: var(--font);
}

.budget-row select option {
  background: var(--card);
  color: var(--text);
}

.budget-row.header {
  background: rgba(255, 255, 255, 0.08);
  font-weight: 600;
  padding: 8px 10px;
}

.btn-sm {
  padding: 6px 10px;
  border: none;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #022013;
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
  transition: transform 100ms ease;
}

.btn-sm:hover {
  transform: translateY(-1px);
}

.btn-sm:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s;
}

.modal-overlay.open {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 90%;
  max-width: 400px;
  box-shadow: var(--shadow);
  transform: scale(0.95);
  transition: transform 0.2s;
}

.modal-overlay.open .modal {
  transform: scale(1);
}

.modal h2 {
  margin-top: 0;
  margin-bottom: 16px;
}

.modal-wide {
  max-width: 960px;
}

.modal .transactions-list {
  max-height: 60vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-header h2 {
  margin: 0;
}

.modal-stats {
  display: flex;
  gap: 24px;
  margin-bottom: 16px;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 8px;
}

.modal-stats .stat {
  display: flex;
  gap: 8px;
}

.budget-summary {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

.summary-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 120px;
}

.summary-label {
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.summary-value {
  font-size: 24px;
  font-weight: 600;
}

.btn-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 24px;
  cursor: pointer;
  padding: 0 8px;
  line-height: 1;
}

.btn-close:hover {
  color: var(--text);
}

.modal-actions {
  display: flex;
  gap: 12px;
  margin-top: 16px;
}

.modal-actions button {
  flex: 1;
}

.btn-secondary {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-weight: 600;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.05);
}

.row-actions {
  display: flex;
  gap: 6px;
}

.btn-icon {
  padding: 6px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-icon svg {
  width: 16px;
  height: 16px;
}

.month-input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.month-input-row input[type="month"] {
  flex: 1;
}

.btn-split {
  background: transparent;
  border: 1px solid var(--border);
}

.btn-split:hover {
  background: rgba(255, 255, 255, 0.05);
}

.btn-split.has-splits {
  background: var(--accent);
  border-color: var(--accent);
  color: #1a1a2e;
}

.btn-danger {
  background: #9b2d30;
  border-color: #9b2d30;
  color: #fff;
}

.btn-danger:hover {
  background: #b33538;
}

.splits-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
}

.split-row {
  display: flex;
  gap: 12px;
  align-items: center;
}

.split-row input {
  flex: 1;
}

.split-row .split-category {
  flex: 2;
}

.split-row .split-amount {
  flex: 1;
  max-width: 120px;
}

.split-row .btn-sm {
  flex-shrink: 0;
}

.chart-canvas {
  width: 100%;
  height: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.02);
}

.chart-scroll {
  max-height: 360px;
  overflow: auto;
  padding-right: 4px;
}

.results h2 {
  margin-top: 0;
}

.result-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.result-line:last-child {
  border-bottom: none;
}

.result-line strong {
  font-size: 20px;
}

.mobile-label {
  display: none;
}

.input-wrap {
  display: contents;
}

@media (max-width: 900px), (pointer: coarse) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-toggle {
    display: block;
  }

  .main-content {
    margin-left: 0;
  }

  .page {
    padding-top: 56px;
  }

  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
    touch-action: manipulation;
  }

  .sidebar-overlay.open {
    display: block;
  }

  .eyebrow,
  .sub {
    display: none;
  }
  .hero {
    flex-direction: column;
    align-items: flex-end;
    text-align: right;
  }

  .hero h1 {
    margin-right: 0;
  }

  .page {
    padding: 5px 5px 5px;
  }

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

  .two-up {
    grid-template-columns: 1fr;
    gap: 10px;
  }

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

  .transaction-row.header {
    display: none;
  }

  .totals-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .totals-row-header {
    display: none;
  }

  .totals-row span,
  .totals-row strong {
    display: flex;
    justify-content: space-between;
  }

  .totals-row span::before,
  .totals-row strong::before {
    content: attr(data-label);
    color: var(--muted);
    font-weight: 500;
    margin-right: 12px;
  }

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

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

  .rule-header {
    display: none;
  }

  .chart-canvas {
    width: 100%;
  }

  .month-card input[type="month"] {
    font-size: 14px;
    padding: 10px;
  }

  .month-card {
    padding-top: 16px;
  }

  input[type="month"] {
    font-size: 14px;
  }

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

  .filter-span {
    grid-column: auto;
  }

  .transaction-row {
    gap: 8px;
  }

  .transaction-row > div {
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: right;
  }

  .transaction-row > div::before {
    content: attr(data-label);
    color: var(--muted);
    font-weight: 500;
    margin-right: 12px;
    flex-shrink: 0;
    text-align: left;
  }

  .transaction-row .input-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
  }

  .transaction-row .input-wrap .mobile-label {
    display: block;
    color: var(--muted);
    font-weight: 500;
    flex-shrink: 0;
  }

  .transaction-row .input-wrap input {
    flex: 1;
    text-align: right;
  }

  .transaction-row > button {
    width: 100%;
  }

  .transaction-row .row-actions {
    display: flex;
    gap: 8px;
  }

  .transaction-row .row-actions button {
    flex: 1;
  }

  .budget-row {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .budget-row .input-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
  }

  .budget-row .input-wrap .mobile-label {
    display: block;
    color: var(--muted);
    font-weight: 500;
    flex-shrink: 0;
  }

  .budget-row .input-wrap input {
    flex: 1;
    text-align: right;
  }

  .budget-row > button {
    width: 100%;
  }
}
