:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --text: #2a2622;
  --muted: #8a8178;
  --line: #e6e0d7;
  --accent: #4f7cff;
  --danger: #d9443a;
  --radius: 14px;
  --tab-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}
.hidden { display: none !important; }
h1, h2, h3 { margin: 0; }
a { color: var(--accent); }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  background: #fff;
  font-size: 16px; /* iOS 확대 방지 */
}
textarea { min-height: 80px; resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }

/* ---------- 버튼 ---------- */
.btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.danger { color: var(--danger); border-color: #f1c9c5; }
.btn.ghost { border: none; background: none; }
.btn.block { width: 100%; padding: 12px; }
.btn.icon { width: 44px; padding: 9px 0; flex: none; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; }

/* ---------- 로그인 ---------- */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; text-align: center; }
.login-logo { font-size: 56px; }
.login-card h1 { margin: 8px 0 24px; }
.login-card input { margin-bottom: 10px; }
.error { color: var(--danger); min-height: 1.4em; font-size: 14px; }

/* ---------- 레이아웃 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: rgba(246, 243, 238, .94);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
main { padding: 12px 14px calc(var(--tab-h) + var(--safe-b) + 24px); max-width: 760px; margin: 0 auto; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around; align-items: center;
  height: calc(var(--tab-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: #fff; border-top: 1px solid var(--line);
}
.tabbar button {
  flex: 1; border: none; background: none; color: var(--muted);
  font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.tabbar button span { font-size: 20px; line-height: 1.1; }
.tabbar button.active { color: var(--text); font-weight: 600; }
.tabbar button.add span {
  width: 48px; height: 48px; border-radius: 50%; background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 28px;
  box-shadow: 0 4px 12px rgba(79, 124, 255, .35);
}

/* ---------- 필터 ---------- */
.filters { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.row { display: flex; gap: 8px; }
.row > * { min-width: 0; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: #fff; border-radius: 999px;
  padding: 6px 12px; font-size: 14px; display: inline-flex; align-items: center; gap: 6px;
}
.chip.active { background: var(--text); color: #fff; border-color: var(--text); }
.count { color: var(--muted); font-size: 13px; margin: 4px 2px 8px; }
.legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--muted); align-items: center; }
.legend span { display: inline-flex; align-items: center; gap: 4px; }

/* ---------- 이니셜 배지 ---------- */
.badge {
  --c: #888;
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 22px; height: 22px; padding: 0 5px; border-radius: 11px;
  font-size: 11px; font-weight: 700; line-height: 1;
  background: var(--c); color: #fff; border: 2px solid var(--c);
}
.badge.reading { background: #fff; color: var(--c); }
.badge.wishlist { background: #fff; color: var(--c); border-style: dashed; }
.badge.dropped { background: #bbb; border-color: #bbb; color: #fff; text-decoration: line-through; }
.badge.lg { min-width: 32px; height: 32px; border-radius: 16px; font-size: 14px; }
.badges { display: flex; gap: 3px; flex-wrap: wrap; }

/* ---------- 표지 ---------- */
.cover {
  position: relative; aspect-ratio: 2 / 3; border-radius: 6px; overflow: hidden;
  background: linear-gradient(135deg, #d9cfc1, #b9ab98);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .15);
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover .ph {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: 8px; text-align: center; font-size: 12px; font-weight: 600; color: #4b4136;
  overflow: hidden;
}
.cover .badges { position: absolute; left: 4px; right: 4px; bottom: 4px; }
.cover .fmt { position: absolute; top: 4px; right: 4px; font-size: 12px; background: rgba(255,255,255,.9); border-radius: 6px; padding: 0 4px; }

/* ---------- 책장 ---------- */
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px 12px; }
@media (min-width: 560px) { .grid { grid-template-columns: repeat(5, 1fr); } }
.grid .item { cursor: pointer; }
.grid .item .t { font-size: 13px; font-weight: 600; margin-top: 6px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.grid .item .s { font-size: 11px; color: var(--muted); }

.list .item {
  display: flex; gap: 12px; padding: 10px; background: var(--card); border-radius: var(--radius);
  margin-bottom: 8px; cursor: pointer;
}
.list .item .cover { width: 56px; flex: none; }
.list .item .cover .badges { display: none; }
.list .item .info { min-width: 0; flex: 1; }
.list .item .t { font-weight: 600; }
.list .item .s { font-size: 13px; color: var(--muted); }

/* ---------- 카드 / 피드 ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; }
.feed-item { display: flex; gap: 12px; cursor: pointer; }
.feed-item .cover { width: 64px; flex: none; }
.feed-item .info { flex: 1; min-width: 0; }
.feed-item .who { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.feed-item .t { font-weight: 600; margin-top: 2px; }
.feed-item .comment { margin-top: 4px; font-size: 14px; white-space: pre-wrap; }
.meta { font-size: 12px; color: var(--muted); display: flex; gap: 10px; margin-top: 6px; flex-wrap: wrap; }
.stars { color: #f5a623; letter-spacing: 1px; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }

/* ---------- 상세 페이지 ---------- */
.page { position: fixed; inset: 0; z-index: 30; background: var(--bg); overflow-y: auto; -webkit-overflow-scrolling: touch; }
.page-body { padding: 14px 14px calc(40px + var(--safe-b)); max-width: 760px; margin: 0 auto; }
.book-head { display: flex; gap: 14px; }
.book-head .cover { width: 110px; flex: none; }
.book-head .cover .badges { display: none; }
.book-head h2 { font-size: 19px; }
.book-head .s { color: var(--muted); font-size: 14px; }
.section-title { display: flex; justify-content: space-between; align-items: center; margin: 22px 2px 8px; font-size: 16px; font-weight: 700; }
.desc { font-size: 14px; color: #5a524a; white-space: pre-wrap; max-height: 6.5em; overflow: hidden; position: relative; }
.desc.open { max-height: none; }
.reading-card .head { display: flex; align-items: center; gap: 8px; }
.reading-card .head .name { font-weight: 600; }
.reading-card .head .st { font-size: 12px; color: var(--muted); }
.reading-card .head .edit { margin-left: auto; }
.reading-card .comment { margin: 8px 0 0; white-space: pre-wrap; }
.reactions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.reactions button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 3px 9px; font-size: 14px; }
.reactions button.mine { background: #eef2ff; border-color: #b9c8ff; }
.comments { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 8px; }
.cmt { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; margin-bottom: 6px; }
.cmt .body { flex: 1; white-space: pre-wrap; }
.cmt .del { border: none; background: none; color: var(--muted); font-size: 12px; }
.cmt-form { display: flex; gap: 6px; margin-top: 6px; }
.cmt-form input { padding: 7px 10px; }

/* ---------- 단어 ---------- */
.word-card .top { display: flex; align-items: baseline; gap: 8px; }
.word-card .w { font-size: 18px; font-weight: 700; }
.word-card .pos { font-size: 12px; color: var(--muted); font-style: italic; }
.word-card .owner { margin-left: auto; }
.word-card .def { margin-top: 4px; font-size: 14px; white-space: pre-wrap; }
.word-card .ctx { margin-top: 6px; font-size: 13px; color: #6b6259; font-style: italic; border-left: 3px solid var(--line); padding-left: 8px; }
.word-card .note { margin-top: 6px; font-size: 14px; }
.word-card.learned { opacity: .6; }
.word-card .learn { border: 1px solid var(--line); background: #fff; border-radius: 8px; font-size: 12px; padding: 2px 8px; }
.fab-secondary {
  position: fixed; right: 16px; bottom: calc(var(--tab-h) + var(--safe-b) + 14px); z-index: 15;
  border: none; background: var(--text); color: #fff; border-radius: 999px; padding: 12px 18px;
  font-weight: 600; box-shadow: 0 4px 12px rgba(0, 0, 0, .2);
}
.dict-results { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.dict-entry { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: #fbfaf8; }
.dict-entry .pos { font-size: 12px; color: var(--muted); font-style: italic; }
.dict-entry ol { margin: 4px 0 0; padding-left: 20px; font-size: 14px; }
.dict-entry li { cursor: pointer; padding: 2px 0; }
.dict-entry li.picked { background: #eef2ff; border-radius: 4px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ---------- 바텀 시트 ---------- */
.backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .35); }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-height: 92vh; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 18px 18px 0 0;
  max-width: 760px; margin: 0 auto;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 8px; }
.sheet-head h2 { font-size: 17px; }
.sheet-body { overflow-y: auto; padding: 0 16px calc(24px + var(--safe-b)); -webkit-overflow-scrolling: touch; }
.tabs { display: flex; gap: 4px; background: #ebe5dc; padding: 3px; border-radius: 10px; margin-bottom: 12px; }
.tabs button { flex: 1; border: none; background: none; padding: 8px 4px; border-radius: 8px; font-size: 14px; }
.tabs button.active { background: #fff; font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 7px 12px; font-size: 14px; }
.seg button.active { background: var(--text); color: #fff; border-color: var(--text); }
.star-input { display: flex; gap: 4px; font-size: 28px; }
.star-input button { border: none; background: none; padding: 0 2px; color: #d8d0c4; }
.star-input button.on { color: #f5a623; }
.cand { display: flex; gap: 10px; padding: 8px; background: #fff; border-radius: 10px; margin-bottom: 6px; cursor: pointer; }
.cand .cover { width: 48px; flex: none; }
.cand .t { font-weight: 600; font-size: 14px; }
.cand .s { font-size: 12px; color: var(--muted); }
.preview { display: flex; gap: 12px; align-items: flex-start; background: #fff; padding: 10px; border-radius: 12px; }
.preview .cover { width: 72px; flex: none; }
.actions { display: flex; gap: 8px; margin-top: 18px; }
.actions .btn { flex: 1; padding: 12px; }
.spinner { text-align: center; color: var(--muted); padding: 16px; }

/* ---------- 나 ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px; }
.stat .head { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 6px; }
.stat .num { font-size: 22px; font-weight: 700; }
.stat .sub { font-size: 12px; color: var(--muted); }
.member-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.member-row:last-child { border-bottom: none; }
.member-row .name { flex: 1; }
.member-row .name small { color: var(--muted); }
.color-input { width: 52px; height: 42px; padding: 2px; flex: none; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 60;
  bottom: calc(var(--tab-h) + var(--safe-b) + 20px);
  background: rgba(40, 36, 32, .92); color: #fff; padding: 10px 16px; border-radius: 10px;
  font-size: 14px; max-width: 90vw;
}
