:root {
  --accent: #ef7c30;
  --accent-dark: #c25c17;
  --accent-soft: #fdefe3;
  --bg: #f7f6f4;
  --card: #ffffff;
  --text: #2b2724;
  --muted: #857d76;
  --line: #eae5e0;
  --done: #2f9e6b;
  --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;
}
#setup .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,.18); color: #fff;
  font-size: 19px; line-height: 1; cursor: pointer;
}
.icon-btn:active { background: rgba(255,255,255,.34); }
.icon-btn.spin { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
/* 反対側に同じ幅を置いてタイトルを中央に保つ */
.icon-spacer { width: 36px; flex: none; }

.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; font-family: inherit;
}
/* 下線はタブ幅いっぱいではなく文字の下だけに引く（画面端で切れて見えないように） */
.tab::after {
  content: ''; position: absolute; left: 50%; bottom: 0;
  width: 3.4em; height: 2px; border-radius: 2px;
  transform: translateX(-50%); background: transparent;
}
.tab.on { color: var(--accent-dark); }
.tab.on::after { background: var(--accent); }
.tab .badge {
  display: inline-block; min-width: 17px; margin-left: 4px; padding: 0 4px;
  background: var(--accent); color: #fff; border-radius: 999px;
  font-size: 10px; line-height: 17px; vertical-align: 1px;
}

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

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 14px; box-shadow: 0 1px 2px rgba(60,40,20,.06);
}
.card-title { font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 8px; }
.tab-panel > .card + .card { margin-top: 12px; }
.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:disabled { opacity: .45; cursor: default; transform: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: none; border-color: transparent; color: var(--accent-dark); }
.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: 12px; }

.row-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.row-actions.tight { margin-top: 4px; }
.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(239,124,48,.45); cursor: pointer; z-index: 25;
}

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: #2b2724; 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; }

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

/* ---------- 並べ替え ---------- */
.list-bar { display: flex; justify-content: flex-end; margin-bottom: 8px; }
.mini-btn.on { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
.sort-note { margin: 0 2px 10px; }

.move { flex: none; display: flex; align-items: center; gap: 4px; padding: 8px 6px 8px 8px; }
.move button {
  width: 30px; height: 30px; flex: none;
  border: 1px solid var(--line); border-radius: 8px; background: #fff;
  color: var(--accent-dark); font-size: 14px; line-height: 1;
  cursor: pointer; font-family: inherit;
}
.move button:active:not(:disabled) { background: var(--accent-soft); }
.move button:disabled { opacity: .3; cursor: default; }
/* 見出しの中では余白を詰める */
.group-head .move, .store-head .move { padding: 0; }

/* ---------- グループの絞り込みチップ ---------- */
.filter-chips { margin-bottom: 10px; gap: 6px; }
.filter-chips .chip { padding: 7px 13px; font-size: 13px; }

/* ---------- かいものリスト本体 ---------- */
.group-card {
  background: var(--card); border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(60,40,20,.06); overflow: hidden;
}
.group-head {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 10px 12px 14px; border-bottom: 1px solid var(--line);
  background: var(--accent-soft);
}
.group-head .g-emoji { font-size: 18px; flex: none; }
.group-head .g-name {
  flex: 1; min-width: 0; font-size: 15px; font-weight: 700; color: var(--accent-dark);
  overflow-wrap: anywhere;
}
.group-head .g-count { font-size: 11px; color: var(--muted); flex: none; }
.add-btn {
  flex: none; width: 30px; height: 30px; border-radius: 9px;
  border: 1px solid var(--accent); background: #fff; color: var(--accent-dark);
  font-size: 17px; line-height: 1; cursor: pointer; font-family: inherit;
}
.add-btn:active { background: var(--accent); color: #fff; }

.store-head {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px 8px 14px; background: #fbfaf9;
  border-bottom: 1px solid var(--line);
}
.store-head .s-name {
  flex: 1; min-width: 0; font-size: 13px; font-weight: 700; color: var(--muted);
  overflow-wrap: anywhere;
}
.store-head .add-btn { width: 26px; height: 26px; font-size: 15px; }
.store-head.plain { background: #fff; }

.item {
  display: flex; align-items: stretch; gap: 0;
  border-bottom: 1px solid var(--line);
}
/* カードのいちばん下の線は縁と重なるので消す */
.group-card > .store-block:last-child > .item:last-child { border-bottom: 0; }

.check {
  flex: none; width: 50px; border: 0; background: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.check .box {
  width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid #d8d0c8; background: #fff;
}
.check:active .box { border-color: var(--done); background: #eaf7f0; }

.item-body {
  flex: 1; min-width: 0; text-align: left; border: 0; background: none;
  padding: 11px 12px 11px 0; cursor: pointer; font-family: inherit; color: var(--text);
}
.item-name { font-size: 15px; font-weight: 600; overflow-wrap: anywhere; }
.item-sub { font-size: 11px; color: var(--muted); margin-top: 3px; overflow-wrap: anywhere; }
.item-note {
  display: inline-block; background: var(--accent-soft); color: var(--accent-dark);
  border-radius: 6px; padding: 1px 6px; font-size: 11px; font-weight: 700; margin-top: 4px;
}
.empty-line { padding: 12px 14px; font-size: 12px; color: var(--muted); }

/* ---------- 購入済み ---------- */
.date-head { font-size: 12px; font-weight: 700; color: var(--muted); margin: 8px 2px -2px; }
.done-row {
  display: flex; align-items: center; gap: 8px;
  background: var(--card); border-radius: var(--radius); padding: 10px 12px;
  box-shadow: 0 1px 2px rgba(60,40,20,.06);
}
.done-row .d-info { flex: 1; min-width: 0; }
.done-row .d-name { font-size: 14px; font-weight: 600; color: var(--muted); text-decoration: line-through; overflow-wrap: anywhere; }
.done-row .d-sub { font-size: 11px; color: var(--muted); margin-top: 3px; overflow-wrap: anywhere; }
.mini-btn {
  flex: none; border: 1px solid var(--line); background: #fff; color: var(--muted);
  border-radius: 8px; padding: 6px 9px; font-size: 12px; cursor: pointer; font-family: inherit;
}
.mini-btn.back { color: var(--accent-dark); border-color: #f2d6bd; }

/* ---------- お店タブ ---------- */
.st-card { background: var(--card); border-radius: var(--radius); padding: 12px 14px; box-shadow: 0 1px 2px rgba(60,40,20,.06); }
.st-head { display: flex; align-items: center; gap: 8px; }
.st-head .st-title { flex: 1; font-weight: 700; min-width: 0; overflow-wrap: anywhere; }
.st-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.store-chip {
  border: 1px solid var(--line); background: #fbfaf9; border-radius: 999px;
  padding: 6px 12px; font-size: 13px; cursor: pointer; font-family: inherit; color: var(--text);
  max-width: 100%; overflow-wrap: anywhere;
}
.link-btn { border: 0; background: none; color: var(--accent-dark); font-size: 12px; cursor: pointer; font-family: inherit; padding: 4px; }

/* ---------- 入力シート ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(40,30,20,.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; overflow-x: hidden;
  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; }
.ask-title { display: block; font-size: 16px; margin-bottom: 4px; }

.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 select, #ask-input {
  width: 100%; max-width: 100%; min-width: 0; padding: 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
  color: var(--text); font-family: inherit; -webkit-appearance: none;
}
#ask-input { margin-top: 8px; }
/* iOS Safari は入力欄が自前の最小幅を持っていて縮まないので、明示的に0にする */
.field input, .field select, .field-row input { min-width: 0; max-width: 100%; }
.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;
}
.field-row { display: flex; gap: 8px; min-width: 0; }
.field-row input {
  flex: 1 1 auto; min-width: 0; padding: 11px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; font-family: inherit;
  background: #fff; color: var(--text); -webkit-appearance: none;
}
.field-row .btn { flex: none; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  max-width: 100%; overflow-wrap: anywhere;
  border: 1px solid var(--line); background: #fff; border-radius: 999px;
  padding: 8px 14px; font-size: 15px; cursor: pointer; font-family: inherit; color: var(--muted);
}
.chip.on { border-color: var(--accent); background: var(--accent); color: #fff; font-weight: 700; }

.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;
}
