:root {
  --accent: #5b61d6;
  --accent-dark: #4348b8;
  --accent-soft: #eceefc;
  --bg: #f6f6fa;
  --card: #ffffff;
  --text: #23242b;
  --muted: #7b7d8c;
  --line: #e4e5ee;
  --plus: #2f9e6b;
  --minus: #d4614f;
  --danger: #d4614f;
  --radius: 14px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  -webkit-text-size-adjust: 100%;
}

body { overscroll-behavior-y: none; }

/* ---------- 画面 ---------- */
.screen { min-height: 100svh; display: flex; flex-direction: column; }

/* ヘッダー＋タブをまとめて固定する。個別に sticky にするとタブがヘッダーの裏に隠れる */
.sticky-head { position: sticky; top: 0; z-index: 20; }

.app-bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 4px;
  padding: max(8px, env(safe-area-inset-top)) 10px 8px;
  background: var(--accent); color: #fff;
}
.sticky-head .app-bar { position: static; }
.app-bar h1 {
  flex: 1; margin: 0; padding: 0 6px;
  font-size: 17px; font-weight: 700; text-align: center;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#home .app-bar h1 { text-align: left; padding-left: 6px; }

.icon-btn {
  width: 36px; height: 36px; flex: none;
  border: 0; border-radius: 10px;
  background: rgba(255,255,255,.16); color: #fff;
  font-size: 20px; line-height: 1; cursor: pointer;
}
.icon-btn:active { background: rgba(255,255,255,.3); }

.body {
  flex: 1;
  padding: 12px 12px calc(96px + env(safe-area-inset-bottom));
  max-width: 640px; width: 100%; margin: 0 auto;
}

/* ---------- タブ ---------- */
.tabs {
  display: flex; background: #fff; border-bottom: 1px solid var(--line);
}
.tab {
  position: relative; flex: 1; padding: 12px 4px 11px;
  border: 0; background: none; color: var(--muted);
  font-size: 14px; font-weight: 600; cursor: pointer;
}
/* 下線はタブ幅いっぱいではなく文字の下だけに引く（画面端で切れて見えないように） */
.tab::after {
  content: ''; position: absolute; left: 50%; bottom: 0;
  width: 2.6em; height: 2px; border-radius: 2px;
  transform: translateX(-50%); background: transparent;
}
.tab.on { color: var(--accent); }
.tab.on::after { background: var(--accent); }

/* ---------- パーツ ---------- */
.stack { display: flex; flex-direction: column; gap: 10px; }

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 14px; box-shadow: 0 1px 2px rgba(30,32,60,.06);
}
.card-title { font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 8px; }
.danger-card { border: 1px solid #f0dcd8; }

.note { font-size: 12px; color: var(--muted); line-height: 1.6; margin: 8px 0 0; }
.error { color: var(--danger); font-size: 13px; margin: 6px 0 0; }

.btn {
  border: 1px solid var(--line); border-radius: 10px;
  background: #fff; color: var(--text);
  padding: 10px 14px; font-size: 14px; font-weight: 600;
  cursor: pointer; font-family: inherit;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: none; border-color: transparent; color: var(--accent); }
.btn.danger { background: #fff; border-color: #eccec8; color: var(--danger); }
.btn.big { padding: 14px; font-size: 15px; width: 100%; }
.btn.wide { width: 100%; margin-top: 18px; }

.row-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.row-actions.tight { margin-top: 6px; }
.row-actions.end { justify-content: flex-end; }
.row-actions .btn { flex: 1; }
.row-actions.end .btn, .row-actions.tight .btn { flex: none; }

.home-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }

.fab {
  position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
  width: 58px; height: 58px; border-radius: 50%;
  border: 0; background: var(--accent); color: #fff;
  font-size: 30px; line-height: 1;
  box-shadow: 0 6px 18px rgba(91,97,214,.4); cursor: pointer; z-index: 25;
}

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: #23242b; color: #fff; padding: 10px 16px;
  border-radius: 20px; font-size: 13px; z-index: 60; max-width: 90%;
}

.sync-bar {
  padding: 6px 12px; font-size: 12px; text-align: center;
  background: #fff6e2; color: #8a6a1f; border-bottom: 1px solid #f0e2bf;
}
.sync-bar.err { background: #fdeceb; color: #a4372a; border-bottom-color: #f3d6d2; }

/* ---------- ホームのイベント一覧 ---------- */
.ev-card {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border-radius: var(--radius);
  padding: 14px; text-align: left; width: 100%;
  border: 0; box-shadow: 0 1px 2px rgba(30,32,60,.06); cursor: pointer;
  font-family: inherit;
}
.ev-card .ev-info { flex: 1; min-width: 0; }
.ev-card .ev-name { font-size: 16px; font-weight: 700; }
.ev-card .ev-sub { font-size: 12px; color: var(--muted); margin-top: 3px; }
.ev-card .chev { color: var(--muted); font-size: 20px; }

.empty {
  text-align: center; color: var(--muted); font-size: 13px;
  padding: 32px 12px; line-height: 1.8;
}

/* ---------- サマリー ---------- */
.summary {
  display: flex; gap: 8px; margin-bottom: 12px;
}
.summary .sum-box {
  flex: 1; background: var(--card); border-radius: var(--radius);
  padding: 12px 10px; text-align: center;
  box-shadow: 0 1px 2px rgba(30,32,60,.06);
}
.summary .sum-label { font-size: 11px; color: var(--muted); }
.summary .sum-value { font-size: 17px; font-weight: 700; margin-top: 3px; }

/* ---------- 支出一覧 ---------- */
.ex-row {
  display: block; width: 100%; text-align: left;
  background: var(--card); border: 0; border-radius: var(--radius);
  padding: 12px 14px; cursor: pointer; font-family: inherit;
  box-shadow: 0 1px 2px rgba(30,32,60,.06);
}
.ex-head { display: flex; align-items: baseline; gap: 8px; }
.ex-title { flex: 1; font-size: 15px; font-weight: 700; min-width: 0; overflow-wrap: anywhere; }
.ex-amount { font-size: 16px; font-weight: 700; white-space: nowrap; }
.ex-sub { font-size: 12px; color: var(--muted); margin-top: 5px; line-height: 1.6; }
.ex-foot {
  display: flex; justify-content: space-between; gap: 8px;
  font-size: 11px; color: var(--muted); margin-top: 6px;
}
.date-head {
  font-size: 12px; font-weight: 700; color: var(--muted);
  margin: 6px 2px -2px;
}
.tag {
  display: inline-block; background: var(--accent-soft); color: var(--accent-dark);
  border-radius: 6px; padding: 1px 6px; font-size: 11px; font-weight: 700;
}

/* ---------- 精算 ---------- */
.settle-row {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border-radius: var(--radius); padding: 12px 14px;
  box-shadow: 0 1px 2px rgba(30,32,60,.06);
}
.settle-row .s-name { flex: 1; font-weight: 700; min-width: 0; }
.settle-row .s-name small { display: block; font-weight: 400; color: var(--muted); font-size: 11px; margin-top: 2px; }
.settle-row .s-nums { text-align: right; font-size: 12px; color: var(--muted); }
.settle-row .s-diff { font-size: 15px; font-weight: 700; }
.s-diff.plus { color: var(--plus); }
.s-diff.minus { color: var(--minus); }

.pay-row {
  display: flex; align-items: center; gap: 8px;
  background: var(--card); border-radius: var(--radius); padding: 12px 14px;
  box-shadow: 0 1px 2px rgba(30,32,60,.06); font-size: 14px;
}
.pay-row .arrow { color: var(--accent); font-weight: 700; }
.pay-row .amt { margin-left: auto; font-weight: 700; }
.section-title { font-size: 13px; font-weight: 700; color: var(--muted); margin: 14px 2px 0; }

/* ---------- 参加者 ---------- */
.hh-card { background: var(--card); border-radius: var(--radius); padding: 12px 14px; box-shadow: 0 1px 2px rgba(30,32,60,.06); }
.hh-head { display: flex; align-items: center; gap: 8px; }
.hh-name { flex: 1; font-weight: 700; }
.hh-count { font-size: 11px; color: var(--muted); }
.hh-people { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.person-chip {
  border: 1px solid var(--line); background: #fbfbfe; border-radius: 999px;
  padding: 6px 12px; font-size: 13px; cursor: pointer; font-family: inherit; color: var(--text);
}
.person-chip.me { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-dark); font-weight: 700; }
.link-btn { border: 0; background: none; color: var(--accent); font-size: 12px; cursor: pointer; font-family: inherit; padding: 4px; }

/* ---------- 入力シート ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(20,20,35,.4);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet-inner {
  background: var(--bg); width: 100%; max-width: 640px;
  max-height: 92svh; overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding: 12px 14px calc(24px + env(safe-area-inset-bottom));
}
.sheet-inner.narrow {
  border-radius: 16px; margin: auto 12px; max-width: 420px; padding: 18px;
}
.sheet-bar {
  position: sticky; top: -12px; z-index: 2;
  display: flex; align-items: center; gap: 8px;
  background: var(--bg); padding: 6px 0 12px; margin: 0 0 4px;
}
.sheet-bar strong { flex: 1; text-align: center; font-size: 15px; }
.sheet-bar .btn { flex: none; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.field input[type=text], .field input[type=number], .field input[type=date], .field select, #pr-input {
  width: 100%; padding: 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
  color: var(--text); font-family: inherit;
}
.field select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }
.amount-wrap { display: flex; align-items: center; gap: 8px; }
.amount-wrap input { text-align: right; font-size: 20px; font-weight: 700; }
.amount-wrap em { font-style: normal; color: var(--muted); font-size: 14px; }
.field-row { display: flex; gap: 8px; }
.field-row input { flex: 1; padding: 11px; font-size: 16px; border: 1px solid var(--line); border-radius: 10px; font-family: inherit; }
.field-row .btn { flex: none; }

.seg { display: flex; background: #e9e9f2; border-radius: 10px; padding: 3px; gap: 3px; }
.seg-btn {
  flex: 1; border: 0; background: none; border-radius: 8px;
  padding: 9px 4px; font-size: 14px; font-weight: 600; color: var(--muted);
  cursor: pointer; font-family: inherit;
}
.seg-btn.on { background: #fff; color: var(--accent); box-shadow: 0 1px 2px rgba(30,32,60,.1); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.chip {
  border: 1px solid var(--line); background: #fff; border-radius: 999px;
  padding: 8px 14px; font-size: 14px; cursor: pointer; font-family: inherit; color: var(--muted);
}
.chip.on { border-color: var(--accent); background: var(--accent); color: #fff; font-weight: 700; }
.chip small { display: block; font-size: 10px; font-weight: 400; opacity: .85; }
.chip-btn { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 5px 12px; font-size: 12px; cursor: pointer; font-family: inherit; color: var(--muted); }

.preview {
  margin-top: 10px; background: var(--accent-soft); color: var(--accent-dark);
  border-radius: 10px; padding: 10px 12px; font-size: 13px; font-weight: 600; line-height: 1.6;
}

.code-big {
  font-size: 26px; font-weight: 700; letter-spacing: .12em;
  text-align: center; padding: 12px; background: var(--accent-soft);
  color: var(--accent-dark); border-radius: 10px; user-select: all;
}
