:root {
  color-scheme: light dark;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-subtle: #f2f4f7;
  --text: #101828;
  --text-muted: #475467;
  --border: #d0d5dd;
  --border-strong: #667085;
  --brand: #17324d;
  --brand-text: #ffffff;
  --primary: #1769aa;
  --primary-hover: #12558a;
  --primary-soft: #e8f2fa;
  --positive: #067647;
  --positive-bg: #ecfdf3;
  --positive-border: #abefc6;
  --negative: #b42318;
  --negative-bg: #fef3f2;
  --negative-border: #fecdca;
  --info: #175cd3;
  --info-bg: #eff8ff;
  --info-border: #b2ddff;
  --neutral-bg: #f2f4f7;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.06), 0 1px 2px rgba(16, 24, 40, 0.04);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 14px;
  --radius-pill: 999px;
  --gutter: clamp(0.75rem, 3.8vw, 1.25rem);
  --card-pad: clamp(0.85rem, 3.2vw, 1.15rem);
  --person-wolfgang: #0e7490;
  --person-anna: #7a3ec8;
  --person-samuel: #b54708;

  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1720;
    --surface: #17212b;
    --surface-subtle: #1f2c3a;
    --text: #e6eaf0;
    --text-muted: #9aa6b5;
    --border: #2b3846;
    --border-strong: #7b8a9b;
    --brand: #12263a;
    --brand-text: #f0f4f8;
    --primary: #4da3e8;
    --primary-hover: #6cb4ee;
    --primary-soft: #18324a;
    --positive: #4ade80;
    --positive-bg: #0f2a1c;
    --positive-border: #1d5437;
    --negative: #f87171;
    --negative-bg: #2d1414;
    --negative-border: #5c2323;
    --info: #60a5fa;
    --info-bg: #132742;
    --info-border: #234775;
    --neutral-bg: #1f2a35;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
    --person-wolfgang: #22d3ee;
    --person-anna: #c084fc;
    --person-samuel: #fb923c;
  }
}

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

html,
body {
  margin: 0;
  max-width: 100%;
  overflow-x: clip;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
}

/* Header & Navigation */
header {
  background: var(--brand);
  color: var(--brand-text);
  padding-block: 0.65rem;
  padding-inline: max(var(--gutter), env(safe-area-inset-left, 0px))
    max(var(--gutter), env(safe-area-inset-right, 0px));
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.header-inner {
  max-width: 850px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem 0.75rem;
  flex-wrap: wrap;
}

.brand {
  color: var(--brand-text);
  text-decoration: none;
  font-weight: 800;
  font-size: 1.1rem;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  min-height: 2.75rem;
}

.brand-mark {
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 6px;
  background: var(--primary);
  color: #ffffff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 0.95rem;
  flex-shrink: 0;
}

.header-user {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.user-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.875rem;
  padding: 0.25rem 0.6rem 0.25rem 0.3rem;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.12);
  color: var(--brand-text);
}

.header-logout {
  margin: 0;
}

.header-logout button {
  width: auto;
  margin: 0;
  min-height: 2.75rem;
  padding: 0.35rem 0.7rem;
  font-size: 0.85rem;
  background: rgba(255, 255, 255, 0.14);
  color: var(--brand-text);
  border: 1px solid rgba(255, 255, 255, 0.28);
}

.header-logout button:hover {
  background: rgba(255, 255, 255, 0.24);
}

/* Avatars */
.avatar {
  width: 1.55rem;
  height: 1.55rem;
  border-radius: var(--radius-pill);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.78rem;
  color: #ffffff;
  background: var(--primary);
  flex-shrink: 0;
}

.avatar-wolfgang {
  background: #0e7490;
}

.avatar-anna {
  background: #7a3ec8;
}

.avatar-samuel {
  background: #b54708;
}

/* Main Layout */
main {
  width: 100%;
  max-width: 850px;
  margin: 0 auto;
  padding-top: 1.15rem;
  padding-bottom: calc(2.5rem + env(safe-area-inset-bottom, 0px));
  padding-inline: max(var(--gutter), env(safe-area-inset-left, 0px))
    max(var(--gutter), env(safe-area-inset-right, 0px));
  flex: 1;
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

h1 {
  font-size: clamp(1.3rem, 4vw, 1.5rem);
  margin: 0 0 1rem;
  letter-spacing: -0.01em;
}

h2 {
  font-size: 1.125rem;
  margin: 0 0 0.65rem;
  letter-spacing: -0.01em;
}

h3 {
  font-size: 0.875rem;
  margin: 1.15rem 0 0.5rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

section {
  margin-bottom: 1.75rem;
}

/* Cards */
.card,
.expense,
.balance,
.hero-balance,
.settlement-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--card-pad);
  box-shadow: var(--shadow);
}

.narrow {
  max-width: 440px;
}

/* Alerts & Feedback */
.alert {
  padding: 0.8rem var(--card-pad);
  border-radius: var(--radius-md);
  margin: 0 0 1rem;
  border: 1px solid var(--negative-border);
  background: var(--negative-bg);
  color: var(--negative);
  font-weight: 600;
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
}

.alert-success {
  border-color: var(--positive-border);
  background: var(--positive-bg);
  color: var(--positive);
}

.alert-info {
  border-color: var(--info-border);
  background: var(--info-bg);
  color: var(--info);
}

.field-error {
  display: block;
  color: var(--negative);
  font-weight: 600;
  font-size: 0.85rem;
  margin-top: 0.35rem;
}

input[aria-invalid="true"],
select[aria-invalid="true"] {
  border-color: var(--negative);
  box-shadow: 0 0 0 1px var(--negative);
}

fieldset[aria-invalid="true"] {
  border: 1px solid var(--negative);
  border-radius: var(--radius-sm);
  padding: 0.65rem;
}

/* Focus styles */
:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}

/* Balances & Hero */
.hero-balance {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.4rem 1rem;
  margin-bottom: 0.65rem;
  padding: 0.8rem var(--card-pad);
  background: var(--surface-subtle);
  flex-wrap: wrap;
}

.hero-balance.positive {
  background: var(--positive-bg);
  border-color: var(--positive-border);
}

.hero-balance.negative {
  background: var(--negative-bg);
  border-color: var(--negative-border);
}

.hero-status {
  font-size: 1rem;
}

.hero-status strong {
  font-variant-numeric: tabular-nums;
}

.trip-totals {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.balances {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.6rem;
}

.balance {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.8rem;
  min-width: 0;
}

.balance-person {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 600;
  font-size: 0.95rem;
  min-width: 0;
}

.balance-state {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.balance strong {
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.balance.positive {
  border-color: var(--positive-border);
}

.balance.positive strong,
.balance.positive .balance-state {
  color: var(--positive);
}

.balance.negative {
  border-color: var(--negative-border);
}

.balance.negative strong,
.balance.negative .balance-state {
  color: var(--negative);
}

.balance.neutral strong {
  color: var(--text-muted);
}

/* Settlements */
.settlements-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.settlement-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem 0.75rem;
  padding: 0.7rem var(--card-pad);
  flex-wrap: wrap;
}

.settlement-item.is-mine {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.settlement-text {
  font-variant-numeric: tabular-nums;
  min-width: 0;
}

.settlement-amount {
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Forms & Controls */
label,
.field-label {
  display: block;
  font-weight: 650;
  margin-bottom: 0.9rem;
  color: var(--text);
  min-width: 0;
  max-width: 100%;
}

.label-hint {
  font-weight: 400;
  font-size: 0.825rem;
  color: var(--text-muted);
  margin-left: 0.2rem;
}

input,
select,
button,
.button {
  font: inherit;
  font-size: 1rem;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: 2.75rem;
  margin-top: 0.35rem;
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
}

input[type="date"] {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  -webkit-appearance: none;
  appearance: none;
  text-align: left;
}

input[type="date"]::-webkit-date-and-time-value {
  text-align: left;
  margin: 0;
}

input[type="file"] {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 0.4rem 0.5rem;
  border: 1px dashed var(--border-strong);
  background: var(--surface-subtle);
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

input[type="file"]::file-selector-button {
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  margin-right: 0.6rem;
  padding: 0.35rem 0.65rem;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.amount-wrap,
.share-wrap {
  position: relative;
  display: flex;
  align-items: center;
  margin-top: 0.35rem;
  min-width: 0;
  max-width: 100%;
}

.amount-wrap input,
.share-wrap input {
  margin-top: 0;
  padding-right: 2rem;
  font-variant-numeric: tabular-nums;
}

.amount-wrap input {
  font-size: 1.15rem;
  font-weight: 700;
}

.unit {
  position: absolute;
  right: 0.7rem;
  color: var(--text-muted);
  font-weight: 700;
  pointer-events: none;
}

.grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr);
  gap: 0.85rem;
}

.grid-equal {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.85rem;
  align-items: start;
}

/* Fieldsets ohne doppelte Verschachtelung */
fieldset {
  border: 0;
  padding: 0;
  margin: 0 0 1rem;
  min-width: 0;
  max-width: 100%;
}

legend {
  display: block;
  width: 100%;
  font-weight: 650;
  padding: 0;
  margin-bottom: 0.35rem;
  color: var(--text);
}

/* Segmented Control (Payer / Login Person) */
.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.45rem;
}

.seg-option {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 2.75rem;
  padding: 0.4rem 0.35rem;
  margin: 0;
  min-width: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
  font-weight: 600;
  font-size: 0.925rem;
  text-align: center;
  user-select: none;
}

.seg-option span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.seg-option input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  margin: 0;
}

.seg-option:has(input[type="radio"]:checked) {
  background: var(--primary-soft);
  border-color: var(--primary);
  box-shadow: inset 0 0 0 1px var(--primary);
}

.seg-option:has(input[type="radio"]:focus-visible) {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}

/* Participant rows & Chips */
.participants-list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  margin-bottom: 0.5rem;
}

.participant {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.5rem;
}

.chip-option {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-height: 2.75rem;
  padding: 0.4rem 0.65rem;
  margin: 0;
  min-width: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
  font-weight: 600;
}

.chip-option input[type="checkbox"] {
  width: 1.15rem;
  height: 1.15rem;
  min-height: auto;
  margin: 0;
  flex-shrink: 0;
  accent-color: var(--primary);
}

.chip-option:has(input[type="checkbox"]:checked) {
  background: var(--primary-soft);
  border-color: var(--primary);
}

.share-wrap {
  display: none;
  width: clamp(6.25rem, 28vw, 8rem);
  margin-top: 0;
}

fieldset:has(input[name="custom"]:checked) .share-wrap,
fieldset.is-custom .share-wrap {
  display: flex;
}

.custom {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.75rem;
  margin: 0.15rem 0;
  cursor: pointer;
  font-weight: 600;
}

.custom input[type="checkbox"] {
  width: 1.15rem;
  height: 1.15rem;
  min-height: auto;
  margin: 0;
  flex-shrink: 0;
  accent-color: var(--primary);
}

.split-hint {
  display: block;
  color: var(--text-muted);
  font-size: 0.825rem;
  line-height: 1.4;
  margin-top: 0.25rem;
  font-variant-numeric: tabular-nums;
}

.split-hint.is-error {
  color: var(--negative);
  font-weight: 600;
}

.split-hint.is-ok {
  color: var(--positive);
  font-weight: 600;
}

/* Receipt controls (ohne doppelte Rahmenbox) */
.receipt-box {
  min-width: 0;
  max-width: 100%;
  margin-bottom: 0.9rem;
}

.receipt-box label {
  margin-bottom: 0.25rem;
}

.receipt-current {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 0.75rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
  font-size: 0.875rem;
}

/* Buttons */
button,
.button {
  background: var(--primary);
  color: #ffffff;
  border: 1px solid transparent;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  transition: background-color 0.15s ease;
}

button:hover,
.button:hover {
  background: var(--primary-hover);
}

button[disabled] {
  opacity: 0.65;
  cursor: wait;
}

.secondary {
  background: var(--surface-subtle);
  color: var(--text);
  border-color: var(--border-strong);
}

.secondary:hover {
  background: var(--border);
}

.danger {
  background: var(--negative);
  color: #ffffff;
}

.danger-outline {
  background: transparent;
  color: var(--negative);
  border-color: var(--negative);
}

.danger-outline:hover {
  background: var(--negative-bg);
}

.btn-sm {
  width: auto;
  min-height: 2.75rem;
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  margin-top: 0;
  white-space: nowrap;
}

/* Expense List */
.section-head,
.actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.65rem;
}

.section-head h2 {
  margin: 0;
}

.section-actions {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.section-head .button,
.actions .button,
.actions button {
  width: auto;
}

.expense {
  margin: 0.6rem 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.85rem;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.expense:target {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary);
}

.expense-main {
  display: flex;
  gap: 0.65rem;
  align-items: flex-start;
  min-width: 0;
  width: 100%;
  flex: 1;
}

.expense-body {
  min-width: 0;
  flex: 1;
}

.expense-title {
  font-weight: 700;
  font-size: 1.02rem;
  line-height: 1.35;
  overflow-wrap: anywhere;
  display: block;
}

.expense-meta {
  display: block;
  color: var(--text-muted);
  font-size: 0.84rem;
  line-height: 1.4;
  margin-top: 0.15rem;
  overflow-wrap: break-word;
}

.expense-splits {
  display: block;
  color: var(--text-muted);
  font-size: 0.82rem;
  line-height: 1.45;
  margin-top: 0.25rem;
  font-variant-numeric: tabular-nums;
}

.split-pair {
  display: inline-block;
  white-space: nowrap;
}

.receipt-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  margin-top: 0.4rem;
  padding: 0.25rem 0.6rem;
  min-height: 2.25rem;
  border-radius: var(--radius-pill);
  background: var(--surface-subtle);
  border: 1px solid var(--border-strong);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  text-decoration: none;
}

.receipt-badge:hover {
  border-color: var(--primary);
}

.expense-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.45rem;
  flex-shrink: 0;
}

.expense-amount {
  font-size: 1.125rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.expense-actions {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  flex-wrap: wrap;
  margin-left: auto;
}

.expense-actions .button,
.expense-actions button {
  width: auto;
  min-height: 2.75rem;
  padding: 0.4rem 0.7rem;
  margin-top: 0;
  font-size: 0.85rem;
}

.expense-actions form {
  margin: 0;
}

.danger-zone {
  margin-top: 1.25rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem 1rem;
  flex-wrap: wrap;
}

.muted {
  color: var(--text-muted);
}

/* Mobile Floating Action Button */
.fab {
  display: none;
  position: fixed;
  right: max(var(--gutter), env(safe-area-inset-right, 0px));
  bottom: calc(0.85rem + env(safe-area-inset-bottom, 0px));
  z-index: 20;
  width: auto;
  min-height: 2.75rem;
  padding: 0.5rem 0.95rem;
  font-size: 0.9rem;
  border-radius: var(--radius-pill);
  background: var(--primary);
  color: #ffffff;
  font-weight: 750;
  text-decoration: none;
  box-shadow: 0 4px 12px rgba(16, 24, 40, 0.28);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.fab.is-hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(0.5rem);
}

/* Footer */
footer {
  text-align: center;
  padding-top: 1.25rem;
  padding-bottom: calc(2rem + env(safe-area-inset-bottom, 0px));
  padding-inline: max(var(--gutter), env(safe-area-inset-left, 0px))
    max(var(--gutter), env(safe-area-inset-right, 0px));
  color: var(--text-muted);
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
}

/* Responsive Breakpoints */
@media (max-width: 640px) {
  main {
    padding-bottom: calc(5rem + env(safe-area-inset-bottom, 0px));
  }

  .grid,
  .grid-equal {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
  }

  .expense {
    flex-direction: column;
    gap: 0.65rem;
  }

  .expense-side {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem 0.75rem;
    padding-top: 0.6rem;
    border-top: 1px solid var(--border);
  }

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

@media (max-width: 560px) {
  .balances {
    grid-template-columns: minmax(0, 1fr);
    gap: 0.45rem;
  }

  .balance {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 0.65rem 0.85rem;
    gap: 0.5rem;
  }

  .balance-state {
    margin-right: auto;
    margin-left: 0.35rem;
  }

  .balance strong {
    font-size: 1.05rem;
  }
}

@media (max-width: 400px) {
  .user-prefix {
    display: none;
  }

  .seg-option .avatar {
    width: 1.25rem;
    height: 1.25rem;
    font-size: 0.7rem;
  }

  .settlement-item .btn-sm {
    width: 100%;
  }

  .actions .button,
  .actions button {
    flex: 1 1 100%;
  }
}

@media (max-width: 340px) {
  .seg-option .avatar {
    display: none;
  }
}
