:root {
  --bg: #f9f8f7;
  --surface: #ffffff;
  --surface-2: #f9f8f7;
  --surface-3: #f0efed;
  --text: #2c2c2b;
  --muted: #7d7a75;
  --line: #e6e5e3;
  --primary: #2783de;
  --primary-strong: #1f6fc0;
  --primary-soft: #e5f2fc;
  --success: #46a171;
  --success-soft: #e8f1ec;
  --orange: #d5803b;
  --orange-soft: #fbebde;
  --danger: #e56458;
  --danger-strong: #c8483c;
  --danger-soft: #fce9e7;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 12px rgba(0, 0, 0, .04);
  --shadow-lg: 0 18px 48px rgba(35, 40, 45, .16);
  --radius: 12px;
  --radius-sm: 8px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }

/* ===================== App bar ===================== */
.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 12px clamp(16px, 4vw, 32px);
  background: rgba(255, 255, 255, .88);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand__icon {
  display: grid; place-items: center;
  width: 42px; height: 42px;
  border-radius: 11px;
  background: var(--primary-soft);
  font-size: 22px;
  flex: 0 0 auto;
}
.brand__text { min-width: 0; }
.brand__text h1 { font-size: 19px; font-weight: 700; letter-spacing: -.01em; }
.brand__text p { color: var(--muted); font-size: 13px; }

.appbar__trip { display: flex; align-items: flex-end; gap: 10px; }
.trip-select { display: flex; flex-direction: column; gap: 4px; }
.trip-select label {
  font-size: 11px; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--muted); padding-left: 2px;
}
.trip-select select {
  min-width: min(46vw, 260px);
  height: 42px;
  font-weight: 650;
}

/* ===================== Buttons ===================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 650;
  white-space: nowrap;
  transition: background .15s ease, border-color .15s ease, transform .05s ease;
}
.btn:active { transform: scale(.98); }
.btn:disabled { cursor: wait; opacity: .6; }
.btn__plus { font-size: 18px; line-height: 1; margin-top: -1px; }

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

.btn--ghost { background: var(--surface); border-color: var(--line); color: var(--text); }
.btn--ghost:hover { background: var(--surface-2); }

.btn--soft { background: var(--surface-3); color: var(--text); }
.btn--soft:hover { background: #e6e4e1; }
.btn--soft.btn--danger { color: var(--danger-strong); }
.btn--soft.btn--danger:hover { background: var(--danger-soft); }

.btn--sheet { width: 100%; margin-top: 12px; background: var(--success); color: #fff; }
.btn--sheet:hover { background: #3c8c62; }

.link-button {
  padding: 0; border: 0; background: transparent;
  color: var(--primary); font-size: 13px; font-weight: 650;
}
.link-button:hover { text-decoration: underline; }
.link-button--danger { color: var(--danger-strong); }

.icon-button {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--muted);
}
.icon-button:hover { background: var(--surface-3); color: var(--text); }
.icon-button--danger:hover { background: var(--danger-soft); color: var(--danger-strong); }
.icon-button:disabled { cursor: not-allowed; opacity: .3; }

/* ===================== Layout ===================== */
.app {
  width: min(1080px, 100%);
  margin: 0 auto;
  padding: 20px clamp(16px, 4vw, 32px) 96px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 20px;
  min-width: 0;
}

/* ===================== Trip bar ===================== */
.trip-bar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.trip-bar__info { min-width: 0; }
.trip-bar__info h2 { font-size: 22px; font-weight: 700; letter-spacing: -.01em; margin: 8px 0 4px; }
.trip-bar__meta { color: var(--muted); font-size: 13px; }
.trip-bar__aside { display: flex; flex-direction: column; align-items: flex-end; gap: 10px; }
.trip-bar__actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.trip-bar__actions .btn { min-height: 38px; padding: 0 12px; font-size: 13px; }
.save-status { color: var(--muted); font-size: 12px; white-space: nowrap; }

.status-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  font-size: 11px; font-weight: 750; letter-spacing: .04em; text-transform: uppercase;
  background: var(--surface-3); color: var(--muted);
}
.status-chip::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-chip[data-status="active"] { background: var(--success-soft); color: var(--success); }
.status-chip[data-status="planning"] { background: var(--primary-soft); color: var(--primary); }
.status-chip[data-status="settling"] { background: var(--orange-soft); color: var(--orange); }
.status-chip[data-status="closed"] { background: var(--surface-3); color: var(--muted); }

/* ===================== Tabs ===================== */
.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--surface-3);
  border-radius: 12px;
  position: sticky;
  top: 68px;
  z-index: 20;
}
.tab {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
  transition: background .15s ease, color .15s ease;
}
.tab svg { width: 18px; height: 18px; fill: currentColor; flex: 0 0 auto; }
.tab:hover { color: var(--text); }
.tab.is-active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

.panel { display: none; flex-direction: column; gap: 16px; }
.panel.is-active { display: flex; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ===================== Section titles ===================== */
.section-title { margin-bottom: 16px; }
.section-title--row { display: flex; align-items: flex-end; justify-content: space-between; gap: 14px; flex-wrap: wrap; }
.section-title h2 { font-size: 17px; font-weight: 700; }
.section-title p { margin-top: 4px; color: var(--muted); font-size: 13px; }

/* ===================== Stats ===================== */
.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.stat-card {
  padding: 16px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.stat-card span { display: block; margin-bottom: 6px; color: var(--muted); font-size: 13px; }
.stat-card strong { font-size: 22px; font-weight: 750; letter-spacing: -.01em; }
.stat-card--accent { background: var(--primary-soft); border-color: #cfe6fa; }
.stat-card--accent strong { color: var(--primary-strong); }

/* ===================== Forms ===================== */
input, select {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid #d6d4d0;
  border-radius: 10px;
  background: #fff;
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
}
input:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(39, 131, 222, .16);
}
input::placeholder { color: #aba8a3; }

.field { min-width: 0; }
.field label { display: block; margin-bottom: 6px; font-size: 13px; font-weight: 650; }
.field label span, legend span { color: var(--danger); }
.field-hint { display: block; margin-top: 5px; color: var(--muted); font-size: 11px; }
.helper-text { margin-top: 8px; color: var(--muted); font-size: 12px; line-height: 1.5; }

.form-grid { display: grid; gap: 14px; margin-bottom: 4px; }
.form-grid--3 { grid-template-columns: 1.5fr 1fr 1fr; }

.money-input { position: relative; }
.money-input input { padding-right: 34px; text-align: right; font-weight: 700; }
.money-input > span { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); }

.fieldset {
  position: relative;
  min-width: 0;
  margin-top: 16px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-2);
}
.fieldset legend { padding: 0 7px; font-size: 13px; font-weight: 750; }
.fieldset__actions { position: absolute; top: 12px; right: 14px; display: flex; gap: 12px; }

.participant-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 9px; margin-top: 6px; }
.check-card {
  display: flex; align-items: center; gap: 9px;
  min-width: 0; min-height: 44px;
  padding: 8px 11px;
  border: 1px solid var(--line); border-radius: 10px;
  background: #fff; cursor: pointer;
}
.check-card:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.check-card input { width: 18px; height: 18px; min-height: auto; margin: 0; flex: 0 0 auto; accent-color: var(--primary); }
.check-card span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; font-weight: 600; }

.split-mode { display: flex; flex-wrap: wrap; gap: 10px; }
.split-mode label {
  display: flex; align-items: center; gap: 8px;
  min-height: 44px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
  font-size: 14px; font-weight: 600; cursor: pointer;
}
.split-mode label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); color: var(--primary-strong); }
.split-mode input { width: 18px; height: 18px; min-height: auto; accent-color: var(--primary); }

.split-preview { margin-top: 12px; padding: 11px 13px; border-radius: 10px; background: var(--surface-3); color: var(--muted); font-size: 13px; }

.custom-shares { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-top: 12px; }
.custom-share-row label { display: block; margin-bottom: 5px; color: var(--muted); font-size: 12px; }
.custom-share-row input { text-align: right; }
.custom-share-row input::placeholder { color: var(--success); font-size: 12px; font-weight: 600; opacity: 1; }
.custom-share-input--suggested { border-color: #bfe3cf; background: var(--success-soft); }
.custom-share-input--invalid { border-color: #f2b3ad; }
.custom-guide {
  grid-column: 1 / -1; margin: 0; padding: 10px 12px;
  border-radius: 9px; background: var(--success-soft); color: #2f6d4c;
  font-size: 12px; line-height: 1.5;
}
.custom-total { grid-column: 1 / -1; display: flex; justify-content: flex-end; padding-top: 4px; font-size: 13px; font-weight: 700; }
.custom-total--error { color: var(--danger-strong); }

.more-fields {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-2);
}
.more-fields > summary {
  list-style: none; cursor: pointer;
  padding: 13px 16px; font-size: 14px; font-weight: 650; color: var(--primary);
}
.more-fields > summary::-webkit-details-marker { display: none; }
.more-fields > summary::before { content: "+ "; font-weight: 800; }
.more-fields[open] > summary::before { content: "− "; }
.more-fields[open] > summary { border-bottom: 1px solid var(--line); color: var(--text); }
.more-fields .form-grid { margin: 0; padding: 16px; }

.form-error, .field-error { min-height: 18px; margin-top: 10px; color: var(--danger-strong); font-size: 13px; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }
.form-actions .btn { min-width: 130px; }

/* ===================== Members ===================== */
.member-form { display: grid; gap: 10px; }
.member-form__actions { display: flex; gap: 10px; }
.member-form__actions .btn { flex: 1; }
.member-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 14px; }
.member-item {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2);
}
.member-item > div { min-width: 0; }
.member-item strong, .member-item small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-item strong { font-size: 14px; font-weight: 650; }
.member-item small { margin-top: 2px; color: var(--muted); font-size: 12px; }
.member-item small.missing-email { color: var(--orange); }
.member-item--editing { border-color: var(--primary); background: var(--primary-soft); }
.member-item__actions { display: flex; flex: 0 0 auto; }

/* ===================== Tips ===================== */
.tips__grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.tips__grid p { color: var(--muted); font-size: 13px; line-height: 1.5; }
.tips__grid strong { color: var(--text); }

/* ===================== Setup guide ===================== */
.setup-guide {
  margin-bottom: 14px; padding: 12px 14px;
  border: 1px solid #cfe6fa; border-radius: 10px; background: var(--primary-soft);
  font-size: 13px;
}
.setup-guide summary { cursor: pointer; color: var(--primary-strong); font-weight: 700; }
.setup-guide ol { margin: 10px 0 8px; padding-left: 20px; color: #4a4844; line-height: 1.55; }
.setup-guide li + li { margin-top: 6px; }
.setup-guide a { color: var(--primary-strong); font-weight: 650; }
.setup-guide code { font-size: 12px; background: rgba(255,255,255,.6); padding: 1px 5px; border-radius: 4px; }
.setup-guide__generate { margin-top: 4px; }

.export-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.export-grid .btn { width: 100%; }
.file-button { display: inline-flex; align-items: center; }

/* ===================== Tables ===================== */
.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 640px; }
th, td { padding: 12px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { color: var(--muted); background: var(--surface-2); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; font-weight: 700; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
.align-right { text-align: right; }
.amount { font-weight: 750; white-space: nowrap; }
.expense-main { font-weight: 650; }
.expense-sub { margin-top: 3px; color: var(--muted); font-size: 11px; }
.pill-group { display: flex; flex-wrap: wrap; gap: 5px; min-width: 180px; }
.pill { padding: 3px 8px; border-radius: 999px; background: var(--surface-3); color: #5f5c57; font-size: 11px; font-weight: 600; }
.row-actions { display: flex; justify-content: flex-end; gap: 4px; }
.per-person strong { display: block; color: var(--success); }
.per-person small { display: block; margin-top: 3px; color: var(--muted); font-size: 10px; font-weight: 600; }
.balance-positive { color: var(--success); }
.balance-negative { color: var(--danger-strong); }
.payment-out { color: var(--primary-strong); }
.payment-in { color: var(--success); }
.summary-row--settled { background: var(--success-soft); }

.status-badge { display: inline-flex; align-items: center; padding: 4px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; white-space: nowrap; }
.status-badge--receive { background: var(--success-soft); color: var(--success); }
.status-badge--pay { background: var(--danger-soft); color: var(--danger-strong); }
.status-badge--done { background: var(--surface-3); color: var(--muted); }

/* ===================== Settlements ===================== */
.settlement-list { display: grid; gap: 10px; }
.settlement-item {
  display: grid;
  grid-template-columns: 1fr auto 1fr auto;
  align-items: center; gap: 14px;
  padding: 14px 16px;
  border: 1px solid var(--line); border-radius: 12px;
}
.settlement-item--pending { border-color: #f0d5b8; background: var(--orange-soft); }
.settlement-item--paid { border-color: #bfe3cf; background: var(--success-soft); }
.settlement-person { font-weight: 700; }
.settlement-person:nth-child(3) { text-align: right; }
.settlement-person small { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; font-weight: 500; }
.transfer-arrow { display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--muted); font-size: 11px; }
.transfer-arrow strong { color: var(--primary-strong); font-size: 15px; }
.btn--paid { min-height: 40px; padding: 0 14px; border-radius: 10px; background: var(--success); color: #fff; font-size: 13px; font-weight: 650; border: 0; }
.btn--paid:hover { background: #3c8c62; }

.payment-history { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 14px; }
.payment-history > summary { cursor: pointer; font-size: 14px; font-weight: 650; color: var(--muted); list-style: none; }
.payment-history > summary::-webkit-details-marker { display: none; }
.payment-history > summary::before { content: "▸ "; }
.payment-history[open] > summary::before { content: "▾ "; }
.payment-history[open] > summary { margin-bottom: 12px; color: var(--text); }

/* ===================== Misc ===================== */
.search-input { width: min(260px, 100%); min-height: 40px; }
.empty-state { padding: 28px; color: var(--muted); text-align: center; font-size: 14px; }
.empty-state--success { color: var(--success); background: var(--success-soft); border-radius: 10px; }
.hidden { display: none !important; }

.toast {
  position: fixed; left: 50%; bottom: 88px; transform: translate(-50%, 14px);
  z-index: 60; max-width: min(440px, calc(100vw - 32px));
  padding: 12px 16px; border-radius: 11px;
  background: #2c2c2b; color: #fff; font-size: 14px;
  box-shadow: var(--shadow-lg);
  opacity: 0; pointer-events: none; transition: .22s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ===================== Dialog ===================== */
dialog {
  width: min(520px, calc(100% - 24px));
  padding: 0; border: 0; border-radius: 16px;
  box-shadow: var(--shadow-lg); color: var(--text); background: var(--surface);
}
dialog::backdrop { background: rgba(20, 20, 19, .5); backdrop-filter: blur(3px); }
.dialog-form { display: grid; gap: 14px; padding: 22px; }
.dialog-title { display: flex; justify-content: space-between; gap: 16px; }
.dialog-title p { margin-top: 4px; color: var(--muted); font-size: 13px; }
.dialog-title h2 { font-size: 18px; font-weight: 700; }
.dialog-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ===================== Responsive ===================== */
@media (max-width: 860px) {
  .form-grid--3 { grid-template-columns: 1fr 1fr; }
  .form-grid--3 .field:first-child { grid-column: 1 / -1; }
  .tips__grid { grid-template-columns: 1fr; gap: 8px; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 760px) {
  .appbar { top: 0; padding: 10px 16px; }
  .appbar__trip { flex: 1 1 100%; }
  .trip-select { flex: 1; }
  .trip-select select { min-width: 0; width: 100%; }
  .brand__text p { display: none; }

  .app { padding: 16px 16px 92px; gap: 14px; }

  .trip-bar { flex-direction: column; }
  .trip-bar__aside { align-items: flex-start; width: 100%; }
  .trip-bar__actions { width: 100%; }
  .trip-bar__actions .btn { flex: 1; }

  /* Tabs become a bottom navigation bar */
  .tabs {
    position: fixed;
    left: 0; right: 0; bottom: 0; top: auto;
    border-radius: 0;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, .95);
    border-top: 1px solid var(--line);
    backdrop-filter: blur(12px);
    gap: 2px;
    z-index: 40;
  }
  .tab { flex-direction: column; gap: 3px; min-height: 52px; font-size: 11px; padding: 4px; }
  .tab svg { width: 22px; height: 22px; }
  .tab.is-active { box-shadow: none; background: var(--primary-soft); }

  .stats-grid { grid-template-columns: 1fr 1fr; }
  .participant-grid { grid-template-columns: 1fr 1fr; }
  .custom-shares { grid-template-columns: 1fr; }
  .member-list { grid-template-columns: 1fr; }
  .export-grid { grid-template-columns: 1fr; }
  .split-mode { flex-direction: column; }
  .split-mode label { width: 100%; }
  .form-actions .btn { flex: 1; min-width: 0; }

  .section-title--row { flex-direction: column; align-items: stretch; }
  .search-input { width: 100%; }

  .settlement-item { grid-template-columns: 1fr; text-align: center; gap: 8px; }
  .settlement-person:nth-child(3) { text-align: center; }
  .transfer-arrow { flex-direction: row; justify-content: center; }
  .btn--paid { width: 100%; }

  .toast { bottom: 76px; }
  .dialog-grid { grid-template-columns: 1fr; }
}

@media (max-width: 420px) {
  .form-grid--3 { grid-template-columns: 1fr; }
  .participant-grid { grid-template-columns: 1fr; }
}

/* ===================== Dark mode ===================== */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191919;
    --surface: #202020;
    --surface-2: #262625;
    --surface-3: #33322f;
    --text: #ffffff;
    --muted: rgba(255, 255, 255, .62);
    --line: rgba(255, 255, 255, .16);
    --primary: #5e9fe8;
    --primary-strong: #7db4ee;
    --primary-soft: rgba(94, 159, 232, .16);
    --success: #72bc8f;
    --success-soft: rgba(114, 188, 143, .16);
    --orange: #de9255;
    --orange-soft: rgba(222, 146, 85, .16);
    --danger: #e97366;
    --danger-strong: #ef8b80;
    --danger-soft: rgba(233, 115, 102, .16);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 12px rgba(0, 0, 0, .25);
    --shadow-lg: 0 18px 48px rgba(0, 0, 0, .5);
  }
  .appbar { background: rgba(25, 25, 25, .85); }
  .tabs { background: var(--surface-3); }
  input, select { background: var(--surface-2); border-color: var(--line); }
  .check-card, .split-mode label, .stat-card { background: var(--surface-2); }
  .setup-guide code { background: rgba(255,255,255,.1); }
  .toast { background: #383836; }
  .btn--paid, .btn--sheet, .btn--primary { color: #10261c; }
  .btn--primary { color: #10233a; }
  @media (max-width: 760px) { .tabs { background: rgba(25, 25, 25, .95); } }
}

@media (prefers-reduced-motion: reduce) {
  *, .panel.is-active { animation: none !important; transition: none !important; }
}
