:root {
  --red: #da291c;
  --red-dark: #b21f15;
  --red-soft: #fdecea;
  --bg: #f4f5f6;
  --surface: #ffffff;
  --surface-2: #fafbfb;
  --text: #1a1c1e;
  --text-muted: #5d6368;
  --border: #e3e6e8;
  --border-strong: #c9cdd1;
  --shadow: 0 1px 3px rgba(16, 18, 20, .06), 0 6px 18px rgba(16, 18, 20, .05);
  --radius: 16px;
  --radius-sm: 10px;
  --focus: #1769ff;
  --good: #1f8a4c;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

[data-theme="dark"] {
  --red: #ff5345;
  --red-dark: #ff7468;
  --red-soft: #3a1714;
  --bg: #121315;
  --surface: #1c1e21;
  --surface-2: #232629;
  --text: #f0f2f3;
  --text-muted: #a4abb1;
  --border: #2e3236;
  --border-strong: #3c4146;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .35);
  --focus: #6ea8ff;
  --good: #5bd48a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 17px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

body {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 max(16px, var(--safe-l)) calc(24px + var(--safe-b)) max(16px, var(--safe-r));
}

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--red);
  color: #fff;
  padding: 8px 14px;
  border-radius: 0 0 8px 0;
  z-index: 50;
}
.skip-link:focus { left: 0; }

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + var(--safe-t)) 2px 14px;
  position: sticky;
  top: 0;
  background: linear-gradient(var(--bg) 78%, rgba(244,245,246,0));
  z-index: 10;
}
[data-theme="dark"] .app-header {
  background: linear-gradient(var(--bg) 78%, rgba(18,19,21,0));
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  display: inline-flex;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(218,41,28,.28);
}
.brand-text h1 {
  margin: 0;
  font-size: 1.18rem;
  font-weight: 800;
  letter-spacing: -.02em;
  display: flex;
  align-items: baseline;
  gap: 5px;
}
.brand-ch {
  font-size: .62rem;
  font-weight: 800;
  letter-spacing: .08em;
  color: #fff;
  background: var(--red);
  padding: 2px 6px;
  border-radius: 6px;
  transform: translateY(-1px);
}
.tagline { margin: 1px 0 0; font-size: .76rem; color: var(--text-muted); }

.theme-toggle {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.3rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .12s ease, border-color .12s ease;
}
.theme-toggle:hover { border-color: var(--border-strong); }
.theme-toggle:active { transform: scale(.94); }

/* Cards */
.app-main { display: flex; flex-direction: column; gap: 16px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card-title {
  margin: 0 0 12px;
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: -.01em;
}
.card-head .card-title { margin-bottom: 0; }
.card-sub { color: var(--text-muted); font-weight: 500; font-size: .82rem; }

/* Fields */
.field { margin-top: 14px; }
.field:first-of-type { margin-top: 0; }
label {
  display: block;
  font-size: .82rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.hint { margin: 6px 0 0; font-size: .78rem; color: var(--text-muted); }

.amount-input {
  display: flex;
  align-items: center;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  overflow: hidden;
  transition: border-color .12s ease, box-shadow .12s ease;
}
.amount-input:focus-within {
  border-color: var(--red);
  box-shadow: 0 0 0 3px rgba(218,41,28,.15);
}
.amount-input .prefix {
  padding: 0 10px 0 14px;
  font-weight: 700;
  font-size: .9rem;
  color: var(--text-muted);
}
.amount-input input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 1.35rem;
  font-weight: 700;
  padding: 12px 14px 12px 0;
  outline: none;
  font-variant-numeric: tabular-nums;
}

/* Stepper */
.stepper {
  display: flex;
  align-items: stretch;
  gap: 0;
  max-width: 230px;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-2);
}
.stepper:focus-within {
  border-color: var(--red);
  box-shadow: 0 0 0 3px rgba(218,41,28,.15);
}
.step-btn {
  flex: none;
  width: 54px;
  border: 0;
  background: var(--surface);
  color: var(--red);
  font-size: 1.5rem;
  font-weight: 700;
  cursor: pointer;
  transition: background .12s ease;
}
.step-btn:hover { background: var(--red-soft); }
.step-btn:active { background: var(--red); color: #fff; }
.stepper input {
  flex: 1;
  min-width: 0;
  border: 0;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  text-align: center;
  font-size: 1.25rem;
  font-weight: 700;
  outline: none;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* Chips */
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.chip-row:last-of-type { margin-bottom: 0; }
.chip {
  flex: 1 1 auto;
  min-width: 86px;
  padding: 11px 12px;
  border: 1.5px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: .92rem;
  font-weight: 600;
  cursor: pointer;
  transition: all .12s ease;
}
.chip:hover { border-color: var(--red); }
.chip.is-active {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
  box-shadow: 0 3px 10px rgba(218,41,28,.3);
}
.chip-round { font-size: .85rem; }
.tip-custom { margin-top: 14px; }

/* Segmented control */
.seg {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
}
.seg-btn {
  flex: 1;
  padding: 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--text-muted);
  font-size: .92rem;
  font-weight: 700;
  cursor: pointer;
  transition: all .12s ease;
}
.seg-btn.is-active {
  background: var(--surface);
  color: var(--red);
  box-shadow: var(--shadow);
}

/* Custom split */
.custom-wrap { margin-top: 14px; }
.custom-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.custom-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.custom-row .name {
  flex: none;
  width: 92px;
  font-size: .85rem;
  font-weight: 600;
  color: var(--text-muted);
}
.custom-row .amount-input { flex: 1; }
.custom-row .amount-input input { font-size: 1.05rem; padding: 10px 12px 10px 0; }
.custom-sum {
  margin: 10px 0 0;
  font-size: .82rem;
  font-weight: 600;
}
.custom-sum.ok { color: var(--good); }
.custom-sum.warn { color: var(--red); }

/* Result */
.result-card { border-top: 4px solid var(--red); }
.result-grid { display: flex; flex-direction: column; gap: 2px; }
.result-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 8px 0;
  border-bottom: 1px dashed var(--border);
  font-size: .95rem;
  color: var(--text-muted);
}
.result-row .num { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.result-row.total {
  border-bottom: 0;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
  padding-top: 12px;
}
.result-row.total .num { font-size: 1.1rem; }

.per-person {
  margin-top: 14px;
  background: var(--red-soft);
  border: 1px solid color-mix(in srgb, var(--red) 25%, transparent);
  border-radius: var(--radius-sm);
  padding: 16px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.per-person-label { font-size: .9rem; font-weight: 600; color: var(--red-dark); }
[data-theme="dark"] .per-person-label { color: var(--red-dark); }
.per-person-amount {
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--red);
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
}

.per-list { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
.per-list:empty { display: none; }
.per-item {
  display: flex;
  justify-content: space-between;
  padding: 9px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: .92rem;
}
.per-item .pi-name { font-weight: 600; color: var(--text-muted); }
.per-item .pi-amt { font-weight: 700; font-variant-numeric: tabular-nums; }

.twint-hint {
  margin: 14px 0 0;
  font-size: .82rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 8px;
  line-height: 1.4;
}
.twint-badge {
  flex: none;
  font-size: .66rem;
  font-weight: 800;
  letter-spacing: .04em;
  color: #fff;
  background: #000;
  padding: 3px 7px;
  border-radius: 5px;
}
[data-theme="dark"] .twint-badge { background: #fff; color: #000; }

.result-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.btn-secondary, .btn-ghost {
  flex: 1 1 auto;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-size: .9rem;
  font-weight: 700;
  cursor: pointer;
  transition: all .12s ease;
}
.btn-secondary {
  border: 1.5px solid var(--red);
  background: var(--surface);
  color: var(--red);
}
.btn-secondary:hover { background: var(--red-soft); }
.btn-secondary:active { background: var(--red); color: #fff; }
.btn-ghost {
  border: 1.5px solid var(--border-strong);
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover { border-color: var(--text-muted); color: var(--text); }
.btn-ghost.small { flex: none; padding: 7px 12px; font-size: .8rem; }

/* History */
.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-list:empty { display: none; }
.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.hi-main { min-width: 0; }
.hi-top { font-weight: 700; font-size: .95rem; font-variant-numeric: tabular-nums; }
.hi-sub { font-size: .76rem; color: var(--text-muted); margin-top: 2px; }
.hi-del {
  flex: none;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  line-height: 1;
}
.hi-del:hover { border-color: var(--red); color: var(--red); }
.empty { margin: 4px 0 0; font-size: .85rem; color: var(--text-muted); }
.empty[hidden] { display: none; }

/* Footer */
.app-footer {
  margin-top: 22px;
  text-align: center;
}
.app-footer p { margin: 0; font-size: .74rem; color: var(--text-muted); }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-b));
  transform: translateX(-50%) translateY(8px);
  background: var(--text);
  color: var(--bg);
  padding: 11px 18px;
  border-radius: 999px;
  font-size: .88rem;
  font-weight: 600;
  box-shadow: 0 6px 22px rgba(0,0,0,.3);
  opacity: 0;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 100;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast[hidden] { display: none; }

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

@media (max-width: 400px) {
  body { font-size: 16px; }
  .chip { min-width: 70px; padding: 10px 8px; }
  .per-person-amount { font-size: 1.55rem; }
  .custom-row .name { width: 78px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
