/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --color-primary: #22c55e;
  --color-primary-dark: #16a34a;
  --color-primary-light: rgba(34, 197, 94, 0.1);
  --color-success: #4ade80;
  --color-danger: #ef4444;
  --color-bg: #0f1117;
  --color-surface: #1a1d27;
  --color-surface-hover: #242836;
  --color-text: #e4e4e7;
  --color-text-secondary: #9ca3af;
  --color-border: #2e3241;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  min-height: 100vh;
}

/* === Nav === */
.nav {
  background: #161922;
  border-bottom: 1px solid var(--color-border);
  color: var(--color-text);
  display: flex;
  align-items: center;
  padding: 0 1.5rem;
  height: 56px;
  gap: 1.5rem;
}

.nav-brand {
  font-weight: 700;
  font-size: 1.1rem;
  white-space: nowrap;
}

.nav-tabs {
  display: flex;
  gap: 0.25rem;
}

.nav-tab {
  background: transparent;
  color: var(--color-text-secondary);
  border: none;
  padding: 0.5rem 1rem;
  cursor: pointer;
  font-size: 0.95rem;
  border-radius: var(--radius) var(--radius) 0 0;
  transition: color 0.15s, background 0.15s;
}

.nav-tab:hover {
  color: var(--color-text);
  background: var(--color-surface-hover);
}

.nav-tab.active {
  color: var(--color-primary);
  background: var(--color-surface-hover);
  font-weight: 600;
}

.nav-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.9rem;
}

/* === Views === */
.view {
  display: none;
}

.view.active {
  display: block;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  min-height: 44px;
  min-width: 44px;
  transition: background 0.15s, opacity 0.15s;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--color-primary-dark);
}

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

.btn-secondary:hover {
  background: var(--color-surface-hover);
}

.btn-danger {
  background: var(--color-danger);
  color: #fff;
}

.btn-danger:hover {
  opacity: 0.9;
}

.btn-sm {
  padding: 0.35rem 0.75rem;
  font-size: 0.85rem;
  min-height: 36px;
}

.btn-block {
  width: 100%;
}

.btn-outline {
  background: transparent;
  color: var(--color-text-secondary);
  border: 1px solid var(--color-border);
}

.btn-outline:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

/* === Auth === */
.auth-container {
  max-width: 400px;
  margin: 10vh auto 0;
  padding: 0 1rem;
}

.auth-title {
  text-align: center;
  color: var(--color-primary);
  font-size: 1.8rem;
  margin-bottom: 2rem;
}

.auth-form {
  background: var(--color-surface);
  padding: 2rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.auth-form h2 {
  margin-bottom: 1.5rem;
  font-size: 1.25rem;
}

.auth-toggle {
  text-align: center;
  margin-top: 1rem;
  font-size: 0.9rem;
  color: var(--color-text-secondary);
}

.auth-toggle a {
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 500;
}

.auth-toggle a:hover {
  text-decoration: underline;
}

/* === Forms === */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 0.35rem;
  color: var(--color-text-secondary);
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-family: inherit;
  min-height: 44px;
  transition: border-color 0.15s;
  background: var(--color-bg);
  color: var(--color-text);
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

.form-error {
  color: var(--color-danger);
  font-size: 0.85rem;
  min-height: 1.25rem;
  margin-bottom: 0.5rem;
}

/* === Page layout === */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header h2 {
  font-size: 1.4rem;
}

/* === Summary bar === */
.summary-bar {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem 1rem;
}

.summary-cards {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.summary-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.75rem 1.25rem;
  flex: 1;
  min-width: 150px;
}

.summary-label {
  display: block;
  font-size: 0.8rem;
  color: var(--color-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.summary-value {
  font-size: 1.2rem;
  font-weight: 700;
}

/* === Empty state === */
.empty-state {
  text-align: center;
  color: var(--color-text-secondary);
  padding: 3rem 1rem;
  font-size: 1rem;
  grid-column: 1 / -1;
}

.error-message {
  color: var(--color-danger);
  text-align: center;
  padding: 2rem 1rem;
  grid-column: 1 / -1;
}

/* === Envelope grid === */
.envelope-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1rem;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem 2rem;
}

/* === Envelope card === */
.envelope-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border-left: 4px solid var(--color-primary);
  padding: 1.25rem;
  position: relative;
}

.envelope-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 0.75rem;
}

.envelope-card-name {
  font-size: 1.1rem;
  font-weight: 600;
}

.envelope-card-amounts {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  margin-bottom: 0.5rem;
}

.envelope-card-available {
  font-size: 1.3rem;
  font-weight: 700;
  margin-top: 0.5rem;
}

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

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

/* === Progress bar === */
.progress-bar {
  height: 8px;
  background: var(--color-border);
  border-radius: 4px;
  overflow: hidden;
  margin-top: 0.5rem;
}

.progress-fill {
  height: 100%;
  background: var(--color-primary);
  border-radius: 4px;
  transition: width 0.3s ease;
}

.progress-fill.overspent {
  background: var(--color-danger);
}

/* === Color picker === */
.color-trigger {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid var(--color-border);
  transition: transform 0.1s;
}

.color-trigger:hover {
  transform: scale(1.15);
}

.color-swatch {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.1s;
}

.color-swatch:hover {
  transform: scale(1.15);
}

.color-swatch.selected {
  border-color: #fff;
  box-shadow: 0 0 0 2px var(--color-bg), 0 0 0 4px #fff;
}

.color-picker-popup {
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem;
  background: var(--color-surface-hover);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  position: absolute;
  right: 0;
  bottom: calc(100% + 0.5rem);
  z-index: 10;
}

.envelope-card-actions {
  display: flex;
  gap: 0.25rem;
  position: relative;
}

.color-picker-inline {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  padding: 0.25rem 0;
}

/* === Period badge === */
.period-badge {
  display: inline-block;
  font-size: 0.75rem;
  padding: 0.15rem 0.5rem;
  border-radius: 12px;
  background: var(--color-surface-hover);
  color: var(--color-text-secondary);
  margin-top: 0.25rem;
  border: 1px solid var(--color-border);
}

/* === Filter bar === */
.filter-bar {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem 1rem;
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

/* === Envelope form (modal-like) === */
.envelope-form-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.envelope-form {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 90%;
  max-width: 480px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

.envelope-form h3 {
  margin-bottom: 1.25rem;
  font-size: 1.2rem;
}

.envelope-form .form-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  margin-top: 1.5rem;
}

/* === Filter inputs === */
.filter-input {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: 0.9rem;
  min-height: 40px;
}

.filter-input:focus {
  outline: none;
  border-color: var(--color-primary);
}

/* === Transaction table === */
.txn-table-wrap {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem 2rem;
  overflow-x: auto;
}

.txn-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.txn-table thead {
  position: sticky;
  top: 0;
}

.txn-table th {
  text-align: left;
  padding: 0.75rem 0.75rem;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--color-text-secondary);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
}

.txn-table th.sortable {
  cursor: pointer;
  user-select: none;
}

.txn-table th.sortable:hover {
  color: var(--color-primary);
}

.txn-table td {
  padding: 0.7rem 0.75rem;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

.txn-table tbody tr:hover {
  background: var(--color-surface);
}

.txn-desc {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.txn-actions {
  display: flex;
  gap: 0.25rem;
  white-space: nowrap;
}

/* === Category pill === */
.category-pill {
  display: inline-block;
  font-size: 0.75rem;
  padding: 0.2rem 0.6rem;
  border-radius: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.category-income {
  background: var(--color-primary);
  color: #fff;
}

.category-none {
  background: var(--color-surface-hover);
  color: var(--color-text-secondary);
}

/* === Transaction form (modal) === */
.txn-form-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.txn-form {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 90%;
  max-width: 480px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

.txn-form h3 {
  margin-bottom: 1.25rem;
  font-size: 1.2rem;
}

.txn-form .form-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  margin-top: 1.5rem;
}

/* === Toast notifications === */
.toast-container {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
}

.toast {
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  pointer-events: auto;
  animation: toast-in 0.25s ease-out, toast-out 0.25s ease-in 2.75s forwards;
  max-width: 360px;
}

.toast-success {
  background: var(--color-primary);
  color: #fff;
}

.toast-error {
  background: var(--color-danger);
  color: #fff;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-0.5rem); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* === Floating action button (mobile) === */
.fab {
  display: none;
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--color-primary);
  color: #fff;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  z-index: 50;
  transition: transform 0.15s, background 0.15s;
}

.fab:hover {
  background: var(--color-primary-dark);
  transform: scale(1.05);
}

.fab:active {
  transform: scale(0.95);
}

/* === Envelope name link === */
.envelope-card-name-link {
  cursor: pointer;
  transition: color 0.15s;
}

.envelope-card-name-link:hover {
  color: var(--color-primary);
}

/* === Responsive === */
@media (max-width: 768px) {
  .nav {
    padding: 0 0.75rem;
    gap: 0.5rem;
  }

  .nav-brand {
    font-size: 0.9rem;
  }

  .nav-tab {
    padding: 0.4rem 0.5rem;
    font-size: 0.8rem;
  }

  .nav-right {
    gap: 0.5rem;
    font-size: 0.8rem;
  }

  #nav-username {
    display: none;
  }

  .page-header {
    padding: 1rem;
  }

  .page-header h2 {
    font-size: 1.2rem;
  }

  /* Hide desktop add buttons on mobile — FAB replaces them */
  #btn-add-transaction {
    display: none;
  }

  .fab {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Envelope grid — single column */
  .envelope-grid {
    grid-template-columns: 1fr;
    padding: 0 1rem 5rem; /* extra bottom padding for FAB */
  }

  .summary-cards {
    flex-direction: column;
  }

  .summary-bar {
    padding: 0 1rem 1rem;
  }

  /* Filter bar stacks vertically */
  .filter-bar {
    padding: 0 1rem 1rem;
    flex-direction: column;
  }

  .filter-input {
    width: 100%;
  }

  /* Transaction table — horizontally scrollable with sticky description */
  .txn-table-wrap {
    padding: 0 0 5rem; /* extra bottom for FAB */
    margin: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .txn-table {
    font-size: 0.8rem;
    min-width: 600px;
  }

  .txn-table td:nth-child(2),
  .txn-table th:nth-child(2) {
    position: sticky;
    left: 0;
    background: var(--color-bg);
    z-index: 1;
  }

  .txn-table tbody tr:hover td:nth-child(2) {
    background: var(--color-surface);
  }

  .txn-desc {
    max-width: 130px;
  }

  /* Modals full-width on mobile */
  .envelope-form,
  .txn-form {
    width: 95%;
    padding: 1.5rem;
    max-height: 90vh;
    overflow-y: auto;
  }

  .toast-container {
    top: auto;
    bottom: 5rem;
    right: 1rem;
    left: 1rem;
  }

  .toast {
    max-width: 100%;
  }
}
