:root {
  /* Dark theme (default) */
  --bg: #0f1115;
  --surface: #161924;
  --surface-2: #1c2030;
  --input-bg: #0f1115;
  --bg-press: #1a1e2a;
  --border: #232838;
  --border-strong: #2a2f3e;
  --text: #e6e7e9;
  --text-strong: #ffffff;
  --text-2: #d0d2d6;
  --text-3: #b9bcc4;
  --text-muted: #8b8d93;
  --text-faint: #6b6e76;
  --accent: #4f7cff;
  --accent-press: #3f6ae8;
  --warning: #ffd86b;
  --danger: #ff6b6b;
  --success: #6ed28a;
  --overlay: rgba(0, 0, 0, 0.65);
}

[data-theme="light"] {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #ffffff;
  --input-bg: #ffffff;
  --bg-press: #eef1f6;
  --border: #e1e4ec;
  --border-strong: #d6dae3;
  --text: #0f1115;
  --text-strong: #0f1115;
  --text-2: #1f2937;
  --text-3: #374151;
  --text-muted: #6b7280;
  --text-faint: #9ca3af;
  --warning: #b45309;
  --danger: #dc2626;
  --success: #16a34a;
  --overlay: rgba(15, 17, 21, 0.45);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  -webkit-font-smoothing: antialiased;
}

header {
  padding: 20px 20px 12px;
  max-width: 600px;
  margin: 0 auto;
  position: relative;
}

.theme-toggle {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 16px);
  right: 16px;
  display: flex;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 3px;
}

.theme-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 14px;
  line-height: 1;
  padding: 6px 9px;
  border-radius: 5px;
  cursor: pointer;
}

.theme-btn.active {
  background: var(--accent);
  color: #ffffff;
}

.trip-name {
  display: block;
  background: transparent;
  border: none;
  color: var(--text-strong);
  font-family: inherit;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
  cursor: pointer;
  padding: 0;
  text-align: left;
  max-width: calc(100% - 90px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.trip-name:active {
  color: var(--accent);
}

.link-button {
  background: transparent;
  border: none;
  color: var(--accent);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 0;
}

.new-trip-link {
  display: inline-block;
  margin-top: 4px;
  margin-bottom: 14px;
  padding: 4px 0;
}

.tiny-link {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  padding: 4px 6px;
  margin-left: auto;
  text-decoration: underline;
}

.tiny-link:active {
  color: var(--accent);
}

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

.identity-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

.segmented {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
}

.segmented.big {
  width: 100%;
  margin-bottom: 14px;
}

.segmented .seg-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-3);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
}

.segmented.big .seg-btn {
  font-size: 15px;
  padding: 12px;
}

.segmented .seg-btn.active {
  background: var(--accent);
  color: var(--text-strong);
}

main {
  padding: 8px 20px 40px;
  max-width: 600px;
  margin: 0 auto;
}

h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 12px;
  font-weight: 600;
}

.totals-card {
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
}

.totals-group {
  margin-bottom: 14px;
}

.totals-group-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 6px;
}

.totals-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 0;
}

.totals-label {
  font-size: 14px;
  color: var(--text-2);
  font-weight: 500;
}

.totals-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.totals-value.subtle {
  color: var(--text-strong);
}

.settlement {
  font-size: 15px;
  color: var(--text-3);
  text-align: center;
  padding: 12px;
  background: var(--input-bg);
  border-radius: 10px;
  border: 1px solid var(--border);
}

.settlement.settled {
  color: var(--success);
}

.settlement.owes strong {
  color: var(--warning);
}

.primary-button {
  width: 100%;
  padding: 16px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 12px;
  font-family: inherit;
}

.primary-button:active {
  background: var(--accent-press);
  transform: scale(0.99);
}

.primary-button:disabled {
  background: var(--border-strong);
  color: var(--text-faint);
  cursor: not-allowed;
}

.chooser-button {
  display: block;
  width: 100%;
  padding: 16px;
  background: var(--input-bg);
  color: var(--text-strong);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  margin-bottom: 10px;
  font-family: inherit;
}

.chooser-button:active {
  background: var(--bg-press);
  border-color: var(--accent);
}

.secondary-button-wide {
  width: 100%;
  padding: 12px;
  background: transparent;
  color: var(--text-3);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 28px;
  font-family: inherit;
}

.secondary-button-wide:active {
  background: var(--bg-press);
}

.secondary-button {
  padding: 12px 16px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  font-size: 15px;
  cursor: pointer;
  font-family: inherit;
}

/* Webcam modal styling, same as paycheck-tracker */
.webcam-content {
  max-width: 480px;
}

#webcam-video {
  width: 100%;
  border-radius: 10px;
  background: #000;
  margin-bottom: 12px;
  display: block;
}

.webcam-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  align-items: center;
}

.webcam-controls:empty {
  display: none;
}

.ghost-button {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-2);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}

.ghost-button.active {
  background: var(--warning);
  color: #1a1a1a;
  border-color: var(--warning);
}

.thumbnails {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  margin-bottom: 16px;
  min-height: 0;
}

.thumbnails:empty {
  display: none;
}

.thumb {
  position: relative;
  flex: 0 0 auto;
  width: 64px;
  height: 64px;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border-strong);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.thumb .remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.75);
  border: none;
  color: var(--text-strong);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* History list */
.history ul {
  list-style: none;
}

.history li {
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
}

.history li.empty {
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  padding: 28px 16px;
  border: 1px dashed var(--border-strong);
  background: transparent;
  cursor: default;
}

.history li:active {
  background: var(--surface-2);
}

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

.exp-main {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.cat-icon {
  font-size: 22px;
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--input-bg);
  border-radius: 8px;
  border: 1px solid var(--border);
}

.exp-text {
  min-width: 0;
  flex: 1;
}

.merchant {
  font-weight: 600;
  font-size: 14px;
  color: var(--text-strong);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.exp-right {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
}

.amount {
  font-weight: 700;
  font-size: 15px;
  color: var(--text-strong);
  font-variant-numeric: tabular-nums;
}

.delete {
  background: transparent;
  border: none;
  color: var(--text-faint);
  font-size: 22px;
  cursor: pointer;
  padding: 0 6px;
  line-height: 1;
}

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

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
  overflow-y: auto;
}

.modal-content {
  background: var(--surface-2);
  border-radius: 16px;
  padding: 22px;
  width: 100%;
  max-width: 420px;
  border: 1px solid var(--border-strong);
  max-height: 95vh;
  overflow-y: auto;
}

.modal-content h2 {
  color: var(--text-strong);
  font-size: 18px;
  text-transform: none;
  letter-spacing: -0.01em;
  margin-bottom: 8px;
  font-weight: 700;
}

.hint {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 18px;
  line-height: 1.4;
}

.modal-content label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 14px;
  font-weight: 600;
}

.modal-content label.block-label {
  margin-bottom: 8px;
}

.field-row {
  display: flex;
  gap: 10px;
}

.field-row label {
  flex: 1;
}

.field-row label.currency {
  flex: 0 0 90px;
}

.field-row label.grow {
  flex: 1;
}

.modal-content input,
.modal-content select {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  background: var(--input-bg);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  color: var(--text-strong);
  font-size: 16px;
  font-family: inherit;
}

.modal-content input:focus,
.modal-content select:focus {
  outline: none;
  border-color: var(--accent);
}

:root { color-scheme: dark; }
[data-theme="light"] { color-scheme: light; }

.modal-content input[type="date"] {
  cursor: pointer;
  padding-right: 40px;
}

.modal-content input[type="date"]::-webkit-calendar-picker-indicator {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="%234f7cff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>');
  background-position: center;
  background-repeat: no-repeat;
  background-size: 22px 22px;
  width: 28px;
  height: 28px;
  cursor: pointer;
  opacity: 1;
  filter: none;
}

/* Centered labels/groups for Who paid + Split */
.block-label.centered {
  text-align: center;
}

.segmented.big.centered {
  display: flex;
  margin-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 280px;
}

/* Split row: 50/50 + Other */
.split-row {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-bottom: 14px;
  max-width: 280px;
  margin-left: auto;
  margin-right: auto;
}

.split-btn {
  flex: 1;
  padding: 12px 14px;
  background: var(--input-bg);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  color: var(--text-2);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

.split-btn.active {
  background: var(--accent);
  color: var(--text-strong);
  border-color: var(--accent);
}

.custom-split {
  background: var(--input-bg);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 18px;
}

.custom-split-row {
  display: flex;
  gap: 12px;
}

.pct-label {
  flex: 1;
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
  margin-bottom: 0 !important;
  text-align: center;
}

.pct-select {
  width: 100%;
  margin-top: 6px;
  padding: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  color: var(--text-strong);
  font-size: 15px;
  font-family: inherit;
}

.pct-error {
  margin-top: 10px;
  font-size: 12px;
  color: var(--danger);
  text-align: center;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 500;
}

/* Items list */
.items-list {
  margin-bottom: 8px;
}

.item-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

.item-row .item-amount {
  flex: 0 0 110px;
  padding: 10px;
  background: var(--input-bg);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  color: var(--text-strong);
  font-size: 15px;
  font-family: inherit;
}

.item-row .item-category {
  flex: 1;
  padding: 10px;
  background: var(--input-bg);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  color: var(--text-strong);
  font-size: 15px;
  font-family: inherit;
}

.item-row .item-remove {
  flex: 0 0 auto;
  background: transparent;
  border: none;
  color: var(--text-faint);
  font-size: 22px;
  cursor: pointer;
  padding: 0 6px;
  line-height: 1;
}

.add-item-button {
  background: transparent;
  border: 1px dashed var(--border-strong);
  border-radius: 8px;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  padding: 8px;
  width: 100%;
  cursor: pointer;
  margin-bottom: 8px;
}

.add-item-button:active {
  color: var(--text-strong);
  border-color: var(--accent);
}

.items-total {
  text-align: right;
  color: var(--text-3);
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 14px;
  padding: 6px 4px;
}

.modal-buttons {
  display: flex;
  gap: 12px;
  margin-top: 4px;
}

.modal-buttons button {
  flex: 1;
  margin-bottom: 0;
}

.modal-buttons.three {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.modal-buttons.three button {
  margin-bottom: 0;
}

/* Loading overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 200;
  color: var(--text-strong);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.spending-summary {
  margin-top: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}

.spending-summary h2 {
  margin-bottom: 12px;
}

.spending-summary h2.cost-heading {
  margin-top: 20px;
}

.pie-wrap {
  display: flex;
  align-items: center;
  gap: 20px;
}

.pie {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  flex: 0 0 140px;
  background: var(--border-strong);
}

.pie-legend {
  list-style: none;
  flex: 1;
  font-size: 13px;
  margin: 0;
  padding: 0;
  min-width: 0;
}

.pie-legend li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  color: var(--text-2);
}

.pie-legend li span:last-child {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--text-strong);
  text-align: right;
}

.pie-empty {
  color: var(--text-muted);
  font-style: italic;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
  flex: 0 0 12px;
}

.dot.jeff { background: var(--accent); }
.dot.dave { background: #22c55e; }

.pie-total {
  border-top: 1px solid var(--border);
  margin-top: 4px;
  padding-top: 10px !important;
  color: var(--text-strong);
  font-weight: 600;
}

.trip-cost {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.cost-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.cost-name {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-2);
  font-size: 14px;
  font-weight: 600;
}

.cost-amount {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-strong);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.hidden {
  display: none !important;
}

/* Print view (used for Export to PDF) — tight one-page layout */
@media print {
  @page {
    size: letter;
    margin: 0.25in;
  }

  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  body {
    background: #ffffff !important;
    color: #0f1115 !important;
    font-size: 11px;
  }

  /* Hide interactive controls and modals */
  .theme-toggle,
  .new-trip-link,
  .identity-row,
  #add-expense,
  #export-pdf,
  .delete,
  .modal,
  .overlay {
    display: none !important;
  }

  header,
  main {
    max-width: 100% !important;
    padding: 0 !important;
  }

  header {
    margin-bottom: 10px;
  }

  .trip-name {
    color: #0f1115 !important;
    pointer-events: none;
    font-size: 20px;
    max-width: 100% !important;
  }

  /* Two-column header strip: totals card next to spending summary; history full-width */
  main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "totals  summary"
      "history history";
    gap: 10px;
  }

  .totals-card {
    grid-area: totals;
    padding: 10px !important;
    margin: 0 !important;
  }

  .spending-summary {
    grid-area: summary;
    padding: 10px !important;
    margin: 0 !important;
  }

  .history {
    grid-area: history;
    margin-top: 4px;
  }

  /* Card surfaces */
  .totals-card,
  .spending-summary,
  .pie-wrap,
  .history li,
  .cost-row {
    background: #ffffff !important;
    border: 1px solid #d6dae3 !important;
    color: #0f1115 !important;
    box-shadow: none !important;
  }

  /* Compact typography */
  h2 {
    font-size: 10px;
    margin-bottom: 6px;
  }

  .totals-group {
    margin-bottom: 8px;
  }

  .totals-group-label {
    font-size: 9px;
    margin-bottom: 3px;
  }

  .totals-row {
    padding: 3px 0;
  }

  .totals-label {
    font-size: 11px;
  }

  .totals-value {
    font-size: 13px;
  }

  .settlement {
    font-size: 11px;
    padding: 6px 8px;
    margin-top: 4px;
  }

  .pie-wrap {
    padding: 8px !important;
    gap: 10px;
  }

  .pie {
    width: 80px;
    height: 80px;
    flex: 0 0 80px;
  }

  .pie-legend {
    font-size: 10px;
  }

  .pie-legend li {
    padding: 2px 0;
  }

  .spending-summary h2.cost-heading {
    margin-top: 10px;
  }

  .trip-cost {
    gap: 4px;
  }

  .cost-row {
    padding: 5px 8px;
  }

  .cost-name {
    font-size: 11px;
  }

  .cost-amount {
    font-size: 13px;
  }

  /* Expense rows: tight */
  .history li {
    padding: 5px 8px !important;
    margin-bottom: 3px !important;
  }

  .cat-icon {
    width: 26px;
    height: 26px;
    font-size: 14px;
  }

  .merchant {
    font-size: 11px;
  }

  .meta {
    font-size: 10px;
  }

  .amount {
    font-size: 12px;
  }

  /* Force readable colors */
  .totals-group-label,
  .totals-label,
  .meta,
  h2,
  .pie-empty {
    color: #4b5563 !important;
  }

  .totals-value,
  .totals-value.subtle,
  .merchant,
  .amount,
  .cost-amount,
  .pie-legend li span:last-child,
  .pie-total {
    color: #0f1115 !important;
  }

  .totals-value:not(.subtle) {
    color: #1f4ed8 !important;
  }

  .settlement {
    background: #ffffff !important;
    border: 1px solid #d6dae3 !important;
    color: #0f1115 !important;
  }

  .settlement.owes strong {
    color: #b45309 !important;
  }

  /* Page break hygiene */
  .totals-card,
  .spending-summary {
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .history li {
    page-break-inside: avoid;
    break-inside: avoid;
  }
}
