:root {
  --bg: #fff8f3;
  --card: #ffffff;
  --text: #2d2a26;
  --sub: #8a817a;
  --line: #f0e6de;
  --primary: #ff8a65;
  --primary-dark: #f4511e;
  --primary-soft: #ffe9e0;
  --sun: #e53935;
  --sat: #1e88e5;
  --radius: 16px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a19;
    --card: #2a2725;
    --text: #f2ede9;
    --sub: #a59d96;
    --line: #3a3532;
    --primary-soft: #4a2f25;
    --sun: #ef7070;
    --sat: #64a8f0;
  }
}

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

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Malgun Gothic", sans-serif;
  font-size: 16px;
}

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

/* ---------- 상단바 / 탭바 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 16px 12px;
  background: var(--bg);
}
.topbar h1 { margin: 0; font-size: 22px; font-weight: 800; }

.icon-btn {
  border: 0; background: transparent; font-size: 22px; padding: 6px; cursor: pointer;
}

main {
  max-width: 640px; margin: 0 auto;
  padding: 0 16px calc(var(--tabbar-h) + var(--safe-b) + 24px);
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--card);
  border-top: 1px solid var(--line);
}
.tabbar button {
  flex: 1; border: 0; background: transparent; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 11px; color: var(--sub);
}
.tabbar button span { font-size: 22px; filter: grayscale(1); opacity: .6; }
.tabbar button { position: relative; }
.tabbar .soon {
  position: absolute; top: 6px; left: 50%; margin-left: 4px;
  font-style: normal; font-size: 9px; font-weight: 700; line-height: 1;
  padding: 3px 5px; border-radius: 999px;
  background: var(--sub); color: var(--card); white-space: nowrap;
}
.tabbar button.active { color: var(--primary-dark); font-weight: 700; }
.tabbar button.active span { filter: none; opacity: 1; }

/* ---------- 공통 ---------- */
.card {
  background: var(--card); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
}
.card h3 { margin: 0 0 10px; font-size: 16px; display: flex; justify-content: space-between; align-items: center; }
.card h3 small { color: var(--sub); font-weight: 400; font-size: 13px; }

.muted { color: var(--sub); font-size: 14px; }
.empty { text-align: center; color: var(--sub); padding: 24px 0; font-size: 14px; }

.btn {
  border: 0; border-radius: 12px; padding: 10px 16px; cursor: pointer;
  background: var(--primary-soft); color: var(--primary-dark); font-weight: 700;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.danger { background: transparent; color: var(--sun); }
.btn.ghost { background: transparent; color: var(--sub); }
.btn.small { padding: 6px 10px; font-size: 13px; }

.fab {
  position: fixed; right: 20px; bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); z-index: 15;
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--primary); color: #fff; font-size: 28px; cursor: pointer;
  box-shadow: 0 4px 14px rgba(244, 81, 30, .35);
}

.row { display: flex; align-items: center; gap: 10px; }
.list-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer;
}
.list-item:last-child { border-bottom: 0; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .title { font-weight: 600; }
.list-item .meta { font-size: 13px; color: var(--sub); margin-top: 2px; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }

/* ---------- 폼 / 모달 ---------- */
dialog {
  border: 0; border-radius: 20px; padding: 0;
  width: min(92vw, 440px); max-height: 88vh;
  background: var(--card); color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form { padding: 20px; }
dialog h2 { margin: 0 0 16px; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
.modal-actions .spacer { flex: 1; }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--sub); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--primary); }
.field textarea { min-height: 70px; resize: vertical; }
.field-row { display: flex; gap: 8px; }
.field-row .field { flex: 1; }
.check-label { display: flex; align-items: center; gap: 6px; font-size: 14px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--bg); border-radius: 999px;
  padding: 6px 12px; font-size: 13px; cursor: pointer;
}
.chip.on { background: var(--primary); color: #fff; border-color: var(--primary); }

.colors { display: flex; gap: 8px; flex-wrap: wrap; }
.color-pick {
  width: 30px; height: 30px; border-radius: 50%; border: 3px solid transparent; cursor: pointer;
}
.color-pick.on { border-color: var(--text); }

/* ---------- 캘린더 ---------- */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.cal-head h2 { margin: 0; font-size: 20px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); }
.cal-dow { text-align: center; font-size: 12px; color: var(--sub); padding: 6px 0; }
.cal-dow:first-child { color: var(--sun); }
.cal-dow:last-child { color: var(--sat); }
.cal-day {
  min-height: 58px; padding: 4px 2px; text-align: center; cursor: pointer;
  border-radius: 10px; display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.cal-day .num {
  font-size: 14px; width: 26px; height: 26px; line-height: 26px; border-radius: 50%;
}
.cal-day.other { opacity: .3; }
.cal-day.sun .num { color: var(--sun); }
.cal-day.sat .num { color: var(--sat); }
.cal-day.today .num { background: var(--primary); color: #fff; font-weight: 700; }
.cal-day.selected { background: var(--primary-soft); }
.cal-day .dots { display: flex; gap: 2px; flex-wrap: wrap; justify-content: center; max-width: 36px; }
.cal-day .dots i { width: 5px; height: 5px; border-radius: 50%; display: block; }

/* ---------- 장보기 ---------- */
.add-bar { display: flex; gap: 8px; margin-bottom: 12px; }
.add-bar input {
  flex: 1; min-width: 0; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); outline: none;
}
.add-bar input:focus { border-color: var(--primary); }
.shop-item { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.shop-item:last-child { border-bottom: 0; }
.shop-item .check {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--line);
  background: transparent; cursor: pointer; flex: none; color: #fff; font-size: 14px;
}
.shop-item.done .check { background: var(--primary); border-color: var(--primary); }
.shop-item .name { flex: 1; }
.shop-item.done .name { text-decoration: line-through; color: var(--sub); }
.shop-item .qty { font-size: 13px; color: var(--sub); }
.shop-item .who { font-size: 12px; color: var(--sub); }
.thumb {
  width: 44px; height: 44px; border-radius: 10px; object-fit: cover; flex: none;
  background: var(--line); cursor: zoom-in; border: 0; padding: 0;
}
.shop-item.done .thumb { opacity: .5; }
.img-drop {
  border: 2px dashed var(--line); border-radius: 12px; min-height: 120px;
  display: flex; align-items: center; justify-content: center; text-align: center;
  color: var(--sub); font-size: 13px; padding: 10px; outline: none; cursor: pointer;
}
.img-drop:focus, .img-drop.drag { border-color: var(--primary); }
.img-drop img { max-width: 100%; max-height: 220px; border-radius: 8px; }
.img-view { width: 100%; border-radius: 12px; display: block; }
.cat-label { font-size: 12px; font-weight: 700; color: var(--primary-dark); margin: 6px 0 0; }

/* ---------- 가족 ---------- */
.member { display: flex; gap: 12px; align-items: center; }
.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex: none;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; color: #fff; font-weight: 700;
}
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.kv:last-child { border-bottom: 0; }
.kv .k { color: var(--sub); flex: none; }
.kv .v { text-align: right; word-break: break-all; }
.member-detail { font-size: 14px; display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin-top: 10px; }
.member-detail dt { color: var(--sub); }
.member-detail dd { margin: 0; }
.member-detail a { color: var(--primary-dark); }

/* ---------- 준비중 ---------- */
.coming {
  text-align: center; padding: 60px 20px;
}
.coming .big { font-size: 64px; }
.coming h2 { margin: 12px 0 6px; }

/* ---------- 홈 ---------- */
.hello { font-size: 15px; color: var(--sub); margin: 0 0 14px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 14px 10px; text-align: center; cursor: pointer; }
.stat b { display: block; font-size: 22px; color: var(--primary-dark); }
.stat span { font-size: 12px; color: var(--sub); }

/* ---------- 로그인 / 동기화 ---------- */
body.locked .tabbar, body.locked #settingsBtn, body.locked #syncStatus { display: none; }
.login { text-align: center; padding: 48px 8px; }
.login img { width: 88px; height: 88px; border-radius: 22px; }
.login h2 { margin: 16px 0 6px; }
.login form { display: flex; flex-direction: column; gap: 10px; margin-top: 24px; }
.login input {
  padding: 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); outline: none; text-align: center; font-size: 17px;
}
.login input:focus { border-color: var(--primary); }
.login .err { color: var(--sun); font-size: 14px; min-height: 20px; }
.sync-status { font-size: 12px; color: var(--sub); }
.sync-status.offline { color: var(--sun); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 90px);
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: rgba(40,36,33,.92); color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; transition: .25s; z-index: 50; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
