:root {
  --green: #0b6b3a;
  --green-d: #084d2a;
  --bg: #f4f5f3;
  --card: #ffffff;
  --line: #e2e5e0;
  --text: #1c2420;
  --muted: #79857d;
  --plus: #137a3f;
  --minus: #c0392b;
  --tabbar-h: 60px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Hiragino Kaku Gothic ProN", "Yu Gothic", "Meiryo", sans-serif;
  font-size: 16px;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--green);
  color: #fff;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
}
.hdr-btn { background: rgba(255,255,255,.16); color: #fff; border: none; border-radius: 8px; padding: 7px 10px; font-size: .82rem; cursor: pointer; justify-self: start; }
.hdr-center { text-align: center; font-weight: 700; font-size: 1.05rem; display: flex; gap: 6px; align-items: center; justify-content: center; }
.hdr-title { justify-self: end; font-size: .8rem; opacity: .85; }
.role-badge { font-size: .66rem; font-weight: 700; background: rgba(255,255,255,.22); padding: 2px 7px; border-radius: 99px; }
.role-badge.admin { background: #f4d03f; color: #5a4500; }

#main {
  padding: 14px 14px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 20px);
  max-width: 640px;
  margin: 0 auto;
}

.page { animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.hidden { display: none !important; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.card-title { font-weight: 700; margin-bottom: 12px; font-size: 1.02rem; }

/* ---- score input ---- */
.score-rows { display: flex; flex-direction: column; gap: 10px; }
.score-row {
  display: grid;
  grid-template-columns: 26px 1fr 110px;
  gap: 8px;
  align-items: center;
}
.score-row .seat {
  font-weight: 700; color: var(--green); text-align: center; font-size: 1.1rem;
}
.score-row input {
  width: 100%; padding: 11px 10px; border: 1px solid var(--line);
  border-radius: 10px; font-size: 1rem; background: #fafbfa;
}
.score-row input[type=number] { text-align: right; font-variant-numeric: tabular-nums; }

.sum-line {
  display: flex; gap: 8px; justify-content: flex-end; align-items: baseline;
  margin: 14px 2px 4px; color: var(--muted); font-size: .9rem;
}
.sum-line #score-sum { font-size: 1.1rem; font-weight: 700; color: var(--text); }
.sum-line.bad #score-sum { color: var(--minus); }
.sum-line.ok #score-sum { color: var(--plus); }

.btn-row { display: flex; gap: 10px; margin-top: 14px; align-items: center; }
.btn {
  flex: 1; padding: 13px; border: none; border-radius: 11px;
  font-size: 1rem; font-weight: 700; cursor: pointer;
}
.btn.primary { background: var(--green); color: #fff; }
.btn.primary:active { background: var(--green-d); }
.btn.ghost { background: #eef0ed; color: var(--text); flex: 0 0 90px; }
.btn.danger { background: #fdecea; color: var(--minus); }

/* ---- result ---- */
.result-row {
  display: grid; grid-template-columns: 40px 1fr auto; gap: 10px;
  align-items: center; padding: 11px 4px; border-bottom: 1px solid var(--line);
}
.result-row:last-child { border-bottom: none; }
.rank-badge {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 800; color: #fff; font-size: .95rem;
}
.rank-1 { background: #d4af37; }
.rank-2 { background: #9aa3a8; }
.rank-3 { background: #b07a4f; }
.rank-4 { background: #c2c7c4; color:#5a5a5a; }
.result-name { font-weight: 600; }
.result-sub { font-size: .78rem; color: var(--muted); }
.amount { font-weight: 800; font-variant-numeric: tabular-nums; font-size: 1.05rem; }
.amount.plus { color: var(--plus); }
.amount.minus { color: var(--minus); }
.settle-note { margin-top: 12px; font-size: .82rem; color: var(--muted); line-height: 1.6; }

.setting-summary { text-align: center; color: var(--muted); font-size: .82rem; padding: 4px; }

/* ---- forms / lists ---- */
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; padding: 11px; border: 1px solid var(--line); border-radius: 10px; font-size: 1rem; }
.inline-form .btn { flex: 0 0 80px; }

.list { list-style: none; margin: 14px 0 0; padding: 0; }
.list li {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 6px; border-bottom: 1px solid var(--line);
}
.list li:last-child { border-bottom: none; }
.list .li-main { font-weight: 600; }
.list .li-sub { font-size: .8rem; color: var(--muted); }
.icon-btn { background: none; border: none; color: var(--muted); font-size: 1.1rem; padding: 6px; cursor: pointer; }
.icon-btn.del { color: var(--minus); }

textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 12px;
  font-size: 1rem; font-family: inherit; resize: vertical; background: #fafbfa;
}
.saved-flash { color: var(--plus); font-size: .85rem; opacity: 0; transition: opacity .2s; }
.saved-flash.show { opacity: 1; }

.debt-form { display: flex; flex-direction: column; gap: 10px; }
.debt-form label { display: flex; flex-direction: column; gap: 4px; font-size: .82rem; color: var(--muted); }
.debt-form input, .debt-form select { padding: 11px; border: 1px solid var(--line); border-radius: 10px; font-size: 1rem; background: #fafbfa; }
.debt-from-to { font-weight: 600; }
.debt-amt { font-weight: 800; color: var(--minus); font-variant-numeric: tabular-nums; margin-left: 8px; }

/* 2段階確認の未払いアイテム */
.debt-item { display: block !important; }
.debt-row { display: flex; align-items: flex-start; justify-content: space-between; width: 100%; gap: 8px; }
.debt-right { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }
.debt-item.settled .debt-from-to { text-decoration: line-through; color: var(--muted); }
.debt-item.settled .debt-amt { color: var(--muted); }
.debt-status { font-size: .68rem; font-weight: 700; padding: 3px 8px; border-radius: 99px; white-space: nowrap; }
.st-proposed { background: #fff3d6; color: #8a6d1d; }
.st-active   { background: #fde1de; color: #b3382b; }
.st-paying   { background: #e3eefb; color: #2c5d9e; }
.st-settled  { background: #e6f3ea; color: #137a3f; }
.debt-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.debt-actions .btn.small { flex: 1 1 auto; min-width: 120px; }
.btn.small.approve { background: var(--green); color: #fff; }
.btn.small.done { background: #e6f3ea; color: #137a3f; }
.btn.small.ghost { background: #eef0ed; color: var(--text); }
.btn.small:disabled { opacity: 1; }

/* ---- stats ---- */
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.data-table th, .data-table td { padding: 10px 8px; text-align: center; border-bottom: 1px solid var(--line); }
.data-table th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.data-table td:first-child, .data-table th:first-child { text-align: left; font-weight: 600; }
.data-table .plus { color: var(--plus); font-weight: 700; }
.data-table .minus { color: var(--minus); font-weight: 700; }

.history { display: flex; flex-direction: column; gap: 10px; }
.game-card { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.game-card .g-head { display: flex; justify-content: space-between; font-size: .78rem; color: var(--muted); margin-bottom: 6px; }
.game-card .g-row { display: flex; justify-content: space-between; padding: 3px 0; font-size: .9rem; }
.game-card .g-rank { color: var(--green); font-weight: 700; margin-right: 6px; }
.empty { color: var(--muted); text-align: center; padding: 20px; font-size: .9rem; }

/* ---- options ---- */
.opt-group { display: flex; flex-direction: column; gap: 8px; }
.opt {
  display: flex; align-items: center; gap: 10px; padding: 13px 12px;
  border: 1px solid var(--line); border-radius: 11px; cursor: pointer; background: #fafbfa;
}
.opt.active { border-color: var(--green); background: #eaf6ee; }
.opt .radio { width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--line); flex: 0 0 auto; }
.opt.active .radio { border-color: var(--green); background: radial-gradient(circle, var(--green) 42%, transparent 46%); }
.opt .opt-label { font-weight: 600; }
.opt .opt-desc { font-size: .78rem; color: var(--muted); }

/* ---- tabbar ---- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; background: #fff; border-top: 1px solid var(--line);
}
.tab {
  flex: 1; border: none; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: .68rem; padding: 6px 0;
}
.tab .ico { font-size: 1.25rem; line-height: 1; }
.tab.active { color: var(--green); font-weight: 700; }

/* ---------------- ゲート（ルーム選択） ---------------- */
.gate {
  min-height: 100vh;
  padding: calc(env(safe-area-inset-top) + 24px) 16px 40px;
  background:
    linear-gradient(180deg, rgba(8,4,2,.30) 0%, rgba(8,4,2,.55) 50%, rgba(8,4,2,.85) 100%),
    url("login.png") center top / cover no-repeat;
  background-color: #0a0502;
}
.gate-inner { max-width: 460px; margin: 0 auto; }
.brand { text-align: center; margin-bottom: 20px; color: #fff; }
.brand-ico { font-size: 2.4rem; filter: drop-shadow(0 2px 6px rgba(0,0,0,.6)); }
.brand h1 { margin: 4px 0 0; font-size: 1.4rem; text-shadow: 0 2px 10px rgba(0,0,0,.8); }
.gate .gate-foot { color: #f0e6d2; text-shadow: 0 1px 4px rgba(0,0,0,.8); }
.gate .gate-foot .text-link { color: #ffcf6b; }
.stack-form { display: flex; flex-direction: column; gap: 10px; }
.stack-form input {
  padding: 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 1rem; background: #fafbfa;
}
.btn.small { flex: 0 0 auto; padding: 8px 14px; font-size: .85rem; }

.gate-foot { text-align: center; margin-top: 18px; color: var(--muted); font-size: .9rem; }
.text-link { color: var(--green); font-weight: 700; text-decoration: none; padding: 4px; }
.text-link:active { opacity: .6; }

.search-item { display: block !important; }
.si-head { display: flex; align-items: center; justify-content: space-between; width: 100%; }
.si-pw { display: flex; gap: 8px; margin-top: 10px; }
.si-pw input { flex: 1; padding: 10px; border: 1px solid var(--line); border-radius: 9px; font-size: 1rem; }

/* ---------------- score-row の select ---------------- */
.score-row select {
  width: 100%; padding: 11px 8px; border: 1px solid var(--line);
  border-radius: 10px; font-size: 1rem; background: #fafbfa; appearance: none;
}

/* ---------------- hint / lock ---------------- */
.hint { color: var(--muted); font-size: .8rem; margin: 10px 2px 0; line-height: 1.5; }
.lock-note { background: #fff7e6; border: 1px solid #f0dcae; color: #8a6d1d; padding: 10px 12px; border-radius: 10px; margin-bottom: 12px; }
.opt.locked { opacity: .75; cursor: default; }
.opt.locked:not(.active) { background: #f6f7f5; }
