:root {
  --bg: #f4f5f2;
  --card: #ffffff;
  --text: #1d2321;
  --muted: #6b7571;
  --line: #e1e4df;
  --accent: #1f6f5c;
  --house: #c9731a;
  --house-bg: #fdf1e3;
  --me: #2b6cb0;
  --me-bg: #e8f0fa;
  --warn-bg: #fff6d9;
  --warn-line: #e9cf7a;
  --bad: #c0392b;
  --good: #1f6f5c;
  --r8: #2f855a;
  --r10: #718096;
  --tab-h: 56px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121514;
    --card: #1d2120;
    --text: #e8ece9;
    --muted: #98a29e;
    --line: #2e3432;
    --accent: #4fb397;
    --house: #f0a050;
    --house-bg: #3a2a18;
    --me: #6aa6e8;
    --me-bg: #1a2a3d;
    --warn-bg: #352e14;
    --warn-line: #6b5a1f;
    --bad: #f07a6c;
    --good: #4fb397;
    --r8: #5cc48c;
    --r10: #a0aec0;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
}
main {
  padding: calc(env(safe-area-inset-top) + 12px) 14px calc(var(--tab-h) + env(safe-area-inset-bottom) + 150px);
  max-width: 640px;
  margin: 0 auto;
}
h1 { font-size: 22px; margin: 6px 0 12px; }
h2 { font-size: 18px; margin: 0 0 10px; }
a { color: var(--accent); }
.hint { color: var(--muted); font-size: 14px; }
small { color: var(--muted); }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  margin: 10px 0;
}
.card.warn { background: var(--warn-bg); border-color: var(--warn-line); font-size: 14px; }
.card.warn ul { margin: 0; padding-left: 18px; }

label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
input, select, textarea {
  display: block;
  width: 100%;
  margin-top: 3px;
  font: inherit;
  font-size: 16px; /* iOSで拡大されない大きさ */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
}
textarea { min-height: 220px; margin: 10px 0; font-family: ui-monospace, Menlo, monospace; font-size: 14px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-items: end; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; align-items: end; }

.btn {
  font: inherit;
  font-size: 16px;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .55; }
.btn.wide { display: block; width: 100%; margin: 8px 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: -2px 0 10px; align-items: center; }
.chip {
  font: inherit;
  font-size: 14px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  margin: 4px 4px 0 0;
}

/* 明細 */
.items { list-style: none; padding: 0; margin: 10px 0; }
.item {
  display: flex;
  align-items: stretch;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  margin-bottom: 8px;
  overflow: hidden;
}
.item.minus .amount { color: var(--bad); }
.item.done { opacity: .82; }
.item-body {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 10px;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
}
.rate { font-size: 11px; font-weight: 700; padding: 2px 5px; border-radius: 5px; color: #fff; flex: none; }
.rate.r8 { background: var(--r8); }
.rate.r10 { background: var(--r10); }
.names { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.name { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.abbr { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.abbr.new { color: var(--house); }
.amount { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; flex: none; }
.qty { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
.sides { display: flex; flex: none; border-left: 1px solid var(--line); }
.side {
  width: 56px;
  border: 0;
  border-left: 1px solid var(--line);
  background: var(--card);
  color: var(--muted);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  position: relative;
}
.side:first-child { border-left: 0; }
.side.sug::after {
  content: "";
  position: absolute;
  inset: 6px;
  border: 2px dashed currentColor;
  border-radius: 8px;
}
.side.me.sug { color: var(--me); }
.side.house.sug { color: var(--house); }
.side.me.on { background: var(--me); color: #fff; }
.side.house.on { background: var(--house); color: #fff; }

/* 下部の合計と保存ボタン */
.footer:empty { display: none; }
.footer {
  position: fixed;
  left: 0; right: 0;
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--line);
  padding: 8px 14px 4px;
}
.footer > * { max-width: 612px; margin-left: auto; margin-right: auto; }
.totals { display: grid; grid-template-columns: 1fr 1fr 1.3fr; gap: 6px; font-variant-numeric: tabular-nums; }
.totals div { display: flex; flex-direction: column; }
.totals b { font-size: 17px; }
.totals b.house { color: var(--house); }
.totals b.bad { color: var(--bad); }
.footer .btn.wide { margin: 6px 0; }

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card);
  border-top: 1px solid var(--line);
}
.tabbar button { border: 0; background: none; font: inherit; font-size: 13px; color: var(--muted); }
.tabbar button.active { color: var(--accent); font-weight: 700; }

/* 月の合計 */
.months { width: 100%; border-collapse: collapse; margin-top: 12px; font-variant-numeric: tabular-nums; background: var(--card); border-radius: 12px; overflow: hidden; }
.months th, .months td { padding: 11px 8px; border-bottom: 1px solid var(--line); text-align: right; }
.months th:first-child, .months td:first-child { text-align: left; }
.months th { font-size: 12px; color: var(--muted); font-weight: 400; }
.months td.house { color: var(--house); font-weight: 700; }
.months tfoot td { font-weight: 700; border-bottom: 0; }
.months tr.detail td { background: var(--bg); text-align: left; font-size: 13px; }
.rline { display: flex; justify-content: space-between; gap: 8px; padding: 3px 0; }

/* 編集パネル（下から出るシート） */
dialog {
  width: 100%;
  max-width: 640px;
  margin: auto auto 0;
  border: 0;
  border-radius: 16px 16px 0 0;
  padding: 0;
  background: var(--card);
  color: var(--text);
  max-height: 92vh;
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.sheet { padding: 16px 14px calc(16px + env(safe-area-inset-bottom)); }
.seg { display: flex; gap: 8px; margin-bottom: 6px; }
.seg label { flex: 1; margin: 0; }
.seg input { display: none; }
.seg span { display: block; text-align: center; padding: 9px; border: 1px solid var(--line); border-radius: 8px; color: var(--text); font-size: 15px; }
.seg input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }

.toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 10px);
  transform: translate(-50%, -150%);
  max-width: 92vw;
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  transition: transform .25s;
  z-index: 10;
}
.toast.show { transform: translate(-50%, 0); }
.toast.bad { background: var(--bad); color: #fff; }
.toast.good { background: var(--good); color: #fff; }
