:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --surface: #ffffff;
  --ink: #24433b;
  --muted: #526a61;
  --line: #e2efe9;
  --track: #eef7f2;
  --accent: #14806a;
  --accent-ink: #0e6b58;
  --accent-soft: #daf1e7;
  --on-accent: #ffffff;
  --owe: #ad4630;
  --owe-soft: #fbeae3;
  --get: #0f6e5a;
  --get-soft: #d9f2e7;
  --overlay: rgb(15 60 48 / 0.38);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12201c;
    --surface: #1a2b26;
    --ink: #eaf5f0;
    --muted: #a3b9b0;
    --line: #2b3f38;
    --track: #22342e;
    --accent: #5fd0b3;
    --accent-ink: #5fd0b3;
    --accent-soft: #1e3b33;
    --on-accent: #0f1f1a;
    --owe: #f4a08c;
    --owe-soft: #3a2624;
    --get: #74d8b6;
    --get-soft: #1b3a31;
    --overlay: rgb(0 0 0 / 0.55);
  }
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 ui-rounded, 'SF Pro Rounded', 'Nunito', 'Quicksand', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body:has(dialog[open]) {
  overflow: hidden;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

#shell,
.picker {
  max-width: 520px;
  margin: 0 auto;
}

.money {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  white-space: nowrap;
}

/* Ghosts */
.ghost {
  flex: none;
  display: block;
  width: 44px;
  height: 44px;
}
.ghost.small {
  width: 28px;
  height: 28px;
}
.ghost.tiny {
  width: 22px;
  height: 22px;
}
.ghost-front {
  fill: var(--ghost);
}
.ghost-back {
  fill: var(--ghost-shade);
}
.ghost-ink {
  fill: #4a1f4e;
}
.ghost-tongue {
  fill: #e98a7c;
}
.ghost-line {
  fill: none;
  stroke: #4a1f4e;
  stroke-width: 2.800;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ghost-trang-linh { --ghost: #f5b5a1; --ghost-shade: #e39c84; }
.ghost-ha-mu { --ghost: #dcc2f4; --ghost-shade: #c9afe4; }
.ghost-long { --ghost: #b4d6f6; --ghost-shade: #97bee3; }
.ghost-yen { --ghost: #fbdc95; --ghost-shade: #ecc573; }
.ghost-an { --ghost: #b5e6c8; --ghost-shade: #94d0ac; }

/* A huddle of ghosts, big one in the middle */
.crew {
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.crew .ghost {
  width: 76px;
  height: 76px;
  margin-left: -20px;
}
.crew .ghost:first-child {
  margin-left: 0;
}
.crew .ghost:nth-child(1) {
  width: 56px;
  height: 56px;
  transform: rotate(-10deg);
}
.crew .ghost:nth-child(2) {
  transform: rotate(-6deg);
}
.crew .ghost:nth-child(3) {
  position: relative;
  z-index: 1;
  width: 100px;
  height: 100px;
}
.crew .ghost:nth-child(4) {
  transform: rotate(6deg);
}
.crew .ghost:nth-child(5) {
  width: 56px;
  height: 56px;
  transform: rotate(10deg);
}

/* Friendly empty state */
.hero {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 16px 8px;
  text-align: center;
}
.hero p {
  max-width: 300px;
  margin: 0;
  color: var(--muted);
  font-size: 20px;
  line-height: 1.4;
}

/* Who-are-you screen */
.picker {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
  padding: 24px 16px;
}
.picker .crew {
  margin-bottom: 14px;
}
.picker h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 600;
  line-height: 1.25;
  text-align: center;
}
.picker p {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 18px;
  text-align: center;
}
.picker .sheet-error {
  margin: 0;
  color: var(--owe);
  font-size: 14px;
  text-align: left;
}
.picker-option {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  min-height: 64px;
  padding: 8px 14px;
  background: var(--surface);
  border: 1.500px solid var(--line);
  border-radius: 14px;
  font-size: 18px;
  font-weight: 600;
  text-align: left;
}

/* Header */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
}
.topbar h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 600;
  line-height: 1.2;
}
.me-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 4px 14px 4px 8px;
  background: var(--accent-soft);
  border: 0;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
}

/* Main content */
main {
  /* Fills the screen below the header so an empty state can centre itself. */
  display: flex;
  flex-direction: column;
  min-height: calc(100dvh - 66px - env(safe-area-inset-top));
  padding: 4px 16px calc(150px + env(safe-area-inset-bottom));
}
section {
  margin-top: 28px;
}
section:first-child {
  margin-top: 6px;
}
h2 {
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
}
.hint {
  margin: 8px 0 0;
  font-size: 13px;
  color: var(--muted);
}

/* Summary at the top of "Của tôi" */
.summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.stat {
  min-width: 0;
  padding: 16px 14px;
  border-radius: 16px;
}
.stat.owe {
  background: var(--owe-soft);
}
.stat.get {
  background: var(--get-soft);
}
.stat span {
  display: block;
  font-size: 13px;
  color: var(--muted);
}
.stat .money {
  display: block;
  margin-top: 2px;
  font-size: clamp(18px, 5.600vw, 23px);
}
.stat.owe .money {
  color: var(--owe);
}
.stat.get .money {
  color: var(--get);
}

/* Lists */
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 68px;
  padding: 12px 0;
}
.row + .row {
  border-top: 1px solid var(--line);
}
.row-text {
  flex: 1;
  min-width: 0;
}
.row-title {
  font-weight: 600;
  overflow-wrap: anywhere;
}
.row-sub {
  font-size: 13px;
  color: var(--muted);
  overflow-wrap: anywhere;
}
.row-sub .money {
  font-size: 15px;
}
.row-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  flex: none;
  gap: 6px;
  max-width: 46%;
  text-align: right;
}
.owe-text {
  color: var(--owe);
}
.get-text {
  color: var(--get);
}
.empty {
  padding: 14px 0;
  color: var(--muted);
  font-size: 14px;
}
.pair {
  flex: none;
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 13px;
}

/* Buttons: flat on purpose, no shadows */
.btn-small {
  min-height: 38px;
  padding: 4px 12px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.btn-primary {
  width: 100%;
  min-height: 56px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}
.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.btn-plain {
  min-height: 44px;
  padding: 0 4px;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
}

/* Add button + tabs, docked to the bottom */
.dock {
  position: fixed;
  z-index: 6;
  left: 0;
  right: 0;
  bottom: 0;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bg);
}
.dock-inner {
  max-width: 520px;
  margin: 0 auto;
  padding: 10px 16px 0;
}
.add-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.add-button span {
  font-size: 22px;
  font-weight: 400;
  line-height: 1;
}
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
.tab {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-height: 62px;
  padding: 10px 4px 6px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.tab svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.800;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tab[aria-current='page'] {
  color: var(--accent);
}
.badge {
  position: absolute;
  top: 5px;
  left: calc(50% + 6px);
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--owe);
  color: var(--bg);
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

/* Bottom sheet */
.sheet {
  width: 100%;
  max-width: 520px;
  max-height: 92dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 24px 24px 0 0;
  background: var(--surface);
  color: var(--ink);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.sheet::backdrop {
  background: var(--overlay);
}
.sheet-body {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.sheet-head h2 {
  margin: 0;
  font-size: 22px;
  color: var(--ink);
  overflow-wrap: anywhere;
}
.close {
  flex: none;
  width: 44px;
  height: 44px;
  margin-right: -8px;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  font-size: 30px;
  font-weight: 300;
  line-height: 1;
}
fieldset {
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}
.field-label,
legend {
  display: block;
  padding: 0;
  margin-bottom: 6px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}
.label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 30px;
  margin-bottom: 4px;
}
.label-row .field-label {
  margin: 0;
}
.amount-box {
  display: flex;
  align-items: center;
  min-height: 58px;
  padding: 4px 14px;
  border: 1.500px solid var(--line);
  border-radius: 12px;
  font-size: 26px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  cursor: text;
}
.amount-box:focus-within {
  border-color: var(--accent);
}

/* An input as wide as its text: a hidden copy of the value sets the width. */
.k-input {
  position: relative;
  display: block;
  min-width: 0;
  overflow: hidden;
}
.k-input::after {
  content: attr(data-value);
  display: block;
  padding-right: 2px;
  visibility: hidden;
  white-space: pre;
}
.k-input input {
  position: absolute;
  inset: 0;
  width: 100%;
  padding: 0;
  border: 0;
  outline: 0;
  background: none;
  font: inherit;
  font-variant-numeric: inherit;
  text-align: inherit;
}
.unit {
  flex: none;
}
.k-input:has(input:placeholder-shown) + .unit {
  color: var(--muted);
}
.text-input {
  width: 100%;
  height: 50px;
  padding: 0 14px;
  border: 1.500px solid var(--line);
  border-radius: 12px;
  background: none;
  font-size: 16px;
}
.text-input:focus {
  outline: 0;
  border-color: var(--accent);
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.chip input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.chip span {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 14px 0 8px;
  border: 1.500px solid var(--line);
  border-radius: 999px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.chip input:checked + span {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}
.chip input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.preview {
  min-height: 48px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--accent-soft);
  font-size: 14px;
}
.sheet-error {
  margin: 0;
  color: var(--owe);
  font-size: 14px;
}

/* Rows that open a detail sheet */
.row-button {
  width: 100%;
  border: 0;
  background: none;
  text-align: left;
}
.copy-buffer {
  position: fixed;
  opacity: 0;
  width: 1px;
  height: 1px;
}
.row-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  margin: -12px 0;
  padding: 12px 0;
  min-height: 68px;
  border: 0;
  background: none;
  text-align: left;
}
.btn-small.quiet {
  position: relative;
  min-height: 26px;
  padding: 0 10px;
  border: 1.500px solid var(--line);
  border-radius: 8px;
  background: none;
  color: var(--ink);
  font-size: 12px;
  line-height: 1;
}
/* Short enough that the row doesn't change height, with a finger-sized tap area. */
.btn-small.quiet::after {
  content: '';
  position: absolute;
  inset: -9px -6px;
}
.row-side:has(.btn-small.quiet) {
  gap: 1px;
}
.pending-text {
  color: var(--accent);
  font-weight: 600;
}
.warn-text {
  color: var(--owe);
}
.struck {
  text-decoration: line-through;
  color: var(--muted);
}

/* Sheet contents */
.sheet-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.btn-secondary {
  min-height: 52px;
  border: 1.500px solid var(--line);
  border-radius: 12px;
  background: none;
  font-weight: 600;
}
.btn-secondary.danger {
  color: var(--owe);
}
.btn-danger {
  width: 100%;
  min-height: 56px;
  border: 0;
  border-radius: 12px;
  background: var(--owe);
  color: var(--bg);
  font-weight: 600;
}
.lead {
  margin: 0;
  font-size: 16px;
}
.sheet-amount {
  font-size: 28px;
}
.sheet-list {
  border: 1.500px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}
.line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 52px;
  padding: 8px 12px;
  border: 0;
  background: none;
  text-align: left;
}
.line + .line {
  border-top: 1px solid var(--line);
}
.line.total {
  font-weight: 700;
  background: var(--accent-soft);
}
.line-who {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

/* Segmented split mode */
.segments {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  padding: 4px;
  border-radius: 12px;
  background: var(--track);
}
.segment input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.segment span {
  display: grid;
  place-items: center;
  min-height: 42px;
  padding: 0 4px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
}
.segment input:checked + span {
  background: var(--accent);
  color: var(--on-accent);
}
.segment input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.split-area {
  margin-top: 12px;
}
.split-row .row-sub {
  display: block;
}
.stepper {
  flex: none;
  display: flex;
  align-items: center;
  gap: 4px;
}
.stepper button {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-size: 20px;
  line-height: 1;
}
.stepper button:disabled {
  opacity: 0.35;
}
.stepper output {
  width: 26px;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.exact-input {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 124px;
  height: 44px;
  padding: 0 10px;
  border: 1.500px solid var(--line);
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  cursor: text;
}
.exact-input:focus-within {
  border-color: var(--accent);
}

/* Bank details */
.bank-card {
  padding: 12px 14px;
  border: 1.500px solid var(--line);
  border-radius: 14px;
}
.bank-name {
  font-size: 14px;
  font-weight: 600;
}
.bank-number-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  column-gap: 2px;
}
.bank-number {
  font-size: 20px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  user-select: all;
}
.copy-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--muted);
}
.copy-icon svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.800;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.copy-note {
  color: var(--accent-ink);
  font-size: 13px;
  font-weight: 600;
}

/* Toast */
.toast {
  position: fixed;
  z-index: 20;
  left: 50%;
  bottom: calc(148px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14px;
  font-weight: 600;
}
