:root {
  --bg: #f4f5fb;
  --card: #ffffff;
  --text: #1c2033;
  --muted: #6b7190;
  --line: #e3e6f0;
  --primary: #4f6bff;
  --primary-ink: #ffffff;
  --soft: #eef1ff;
  --danger: #e5484d;
  --ok: #1f9d6b;
  --warn: #e39b17;
  --shadow: 0 1px 3px rgba(20, 30, 70, .06), 0 4px 14px rgba(20, 30, 70, .05);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #10121a;
    --card: #1a1d29;
    --text: #e8eaf4;
    --muted: #9aa0bd;
    --line: #2b3042;
    --primary: #7088ff;
    --soft: #23284a;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #10121a; --card: #1a1d29; --text: #e8eaf4; --muted: #9aa0bd;
  --line: #2b3042; --primary: #7088ff; --soft: #23284a; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
a { color: var(--primary); }
h2 { font-size: 16px; margin: 0; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.grow { flex: 1; min-width: 0; }

/* 상단 */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line);
}
.brand-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.brand { font-weight: 800; font-size: 17px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--line); background: var(--card);
  font-size: 18px; line-height: 1; cursor: pointer; display: grid; place-items: center; padding: 0;
}
.icon-btn:active { transform: scale(.94); }
.kids { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; }
.kids::-webkit-scrollbar { display: none; }
.kid {
  flex: none; border: 1.5px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 999px; padding: 6px 14px; font-weight: 600; font-size: 14px;
}
.kid.on { background: var(--kc, var(--primary)); border-color: var(--kc, var(--primary)); color: #fff; }

main { max-width: 680px; margin: 0 auto; padding: 12px 16px; }
.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px; margin-bottom: 12px; border: 1px solid var(--line);
}
.card-h { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 10px; }

/* 통계 */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px; box-shadow: var(--shadow); }
.stat b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }

.chart-box { position: relative; height: 210px; }
.chart-box.small-chart { height: 180px; }
.empty { text-align: center; color: var(--muted); padding: 24px 8px; font-size: 14px; }

/* 버튼 / 입력 */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 10px 14px; border-radius: 10px; font-weight: 600; font-size: 14.5px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 42px;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.btn.wide { width: 100%; }
.btn:active { transform: scale(.98); }
input, select, textarea {
  font: inherit; font-size: 16px; /* iOS 확대 방지 */
  color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 11px; width: 100%; min-height: 42px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 50%, transparent); border-color: var(--primary); }
.row { display: flex; align-items: center; }
.row.gap { gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.row > input { flex: 1; min-width: 0; }
.field { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
.field input, .field select, .field textarea { margin-top: 4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }

/* 세그먼트 */
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button {
  flex: 1; border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 13.5px;
  font-weight: 600; padding: 7px 6px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg.big button { padding: 10px 6px; font-size: 14.5px; }
#fStat button.on[data-v="read"] { background: var(--ok); color: #fff; }
#fStat button.on[data-v="reading"] { background: var(--warn); color: #fff; }

/* 필터 */
.filters { display: grid; gap: 8px; margin-bottom: 12px; }
.filters .row { gap: 8px; }
.filters .row .seg { flex: 1; }
.filters select { width: auto; flex: none; }
.ar-range label { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.ar-range input { width: 84px; }
.ar-range #shelfCount { margin-left: auto; }

/* 리스트 */
.list { display: grid; gap: 8px; }
.item {
  display: flex; gap: 12px; align-items: center; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px; cursor: pointer; text-align: left; width: 100%; color: inherit; font: inherit;
}
.card .item { box-shadow: none; }
.cover {
  width: 46px; height: 64px; object-fit: cover; border-radius: 6px; background: var(--soft);
  flex: none; border: 1px solid var(--line);
}
.cover.big { width: 84px; height: 116px; }
.cover-ph { display: grid; place-items: center; font-size: 20px; }
.item .t { font-weight: 700; font-size: 14.5px; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.item .a { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tags { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 4px; }
.tag { font-size: 11.5px; font-weight: 700; padding: 2px 7px; border-radius: 999px; background: var(--soft); color: var(--muted); }
.tag.ar { background: hsl(var(--h) 80% 92%); color: hsl(var(--h) 60% 30%); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tag.ar { background: hsl(var(--h) 40% 24%); color: hsl(var(--h) 80% 82%); }
}
:root[data-theme="dark"] .tag.ar { background: hsl(var(--h) 40% 24%); color: hsl(var(--h) 80% 82%); }
.tag.read { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.tag.reading { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.quick {
  flex: none; width: 40px; height: 40px; border-radius: 50%; border: 1.5px solid var(--line);
  background: var(--card); font-size: 18px; color: var(--muted); cursor: pointer;
}
.quick.done { background: var(--ok); border-color: var(--ok); color: #fff; }

/* 스캐너 */
.reader { width: 100%; border-radius: 12px; overflow: hidden; background: #000; margin-bottom: 10px; display: none; }
.reader.on { display: block; }
.reader video { width: 100% !important; display: block; }

/* 탭바 */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  border: 0; background: none; color: var(--muted); font: inherit; font-size: 11.5px; font-weight: 600;
  padding: 8px 0 9px; display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
}
.tabbar button span { font-size: 20px; line-height: 1; filter: grayscale(1); opacity: .7; }
.tabbar button.on { color: var(--primary); }
.tabbar button.on span { filter: none; opacity: 1; }

/* 시트 */
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-bg { position: absolute; inset: 0; background: rgba(10, 12, 25, .45); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 92vh; max-height: 92dvh; overflow-y: auto;
  background: var(--card); border-radius: 18px 18px 0 0; padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  max-width: 680px; margin: 0 auto; animation: up .22s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.book-head { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 12px; }
.book-head h2 { font-size: 17px; line-height: 1.3; }
.status-box { background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin-bottom: 12px; display: grid; gap: 8px; }
.status-box input[type="date"] { flex: 1; background: var(--card); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font-size: 12.5px; background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 4px 6px 4px 10px; display: inline-flex; gap: 4px; align-items: center; }
.chip button { border: 0; background: none; color: var(--muted); font-size: 14px; cursor: pointer; padding: 0 4px; }
.stars { display: flex; gap: 2px; }
.stars button { border: 0; background: none; font-size: 24px; padding: 0 2px; cursor: pointer; color: var(--line); }
.stars button.on { color: #f5b301; }
.ar-box { border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px 10px; margin: 0 0 12px; }
.ar-box legend { font-size: 12.5px; font-weight: 700; color: var(--muted); padding: 0 4px; }
.sheet-actions { position: sticky; bottom: calc(-16px - env(safe-area-inset-bottom)); background: var(--card); padding: 10px 0 4px; }

/* 토스트 / 로딩 */
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: #1c2033; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 70; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.loading { position: fixed; inset: 0; z-index: 60; background: rgba(10, 12, 25, .35); display: grid; place-content: center; justify-items: center; gap: 10px; color: #fff; font-weight: 600; }
.loading[hidden] { display: none; }
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 4px solid rgba(255,255,255,.35); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.kid-row { display: flex; gap: 8px; align-items: center; }
.kid-dot { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.kid-row input { flex: 1; }
.kid-row input[type="color"] { flex: none; width: 42px; padding: 2px; }

@media (max-width: 360px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grid3 { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 720px) {
  .stats { grid-template-columns: repeat(6, 1fr); }
}

#kidList { margin-bottom: 10px; }

/* 위시 / 적정 범위 / 단계별 */
.tag.wish { background: color-mix(in srgb, #e5487f 15%, transparent); color: #d63a72; }
.tag.zpd { background: color-mix(in srgb, var(--kid, var(--primary)) 18%, transparent); color: var(--kid, var(--primary)); }
#fStat button.on[data-v="wish"] { background: #e5487f; color: #fff; }
.item.in-zpd { border-color: color-mix(in srgb, var(--kid, var(--primary)) 55%, var(--line)); box-shadow: inset 4px 0 0 var(--kid, var(--primary)); }
.btn.mini { min-height: 36px; padding: 6px 10px; font-size: 13px; flex: none; }
.level { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 4px 12px; margin-bottom: 10px; box-shadow: var(--shadow); }
.level summary { display: flex; align-items: center; gap: 8px; padding: 10px 0 8px; cursor: pointer; list-style: none; font-weight: 700; }
.level summary::-webkit-details-marker { display: none; }
.level summary::after { content: '▾'; color: var(--muted); margin-left: 4px; transition: transform .15s; }
.level:not([open]) summary::after { transform: rotate(-90deg); }
.lv-name { padding: 3px 10px; border-radius: 999px; background: hsl(var(--h) 80% 92%); color: hsl(var(--h) 60% 30%); font-size: 14px; }
:root[data-theme="dark"] .lv-name { background: hsl(var(--h) 40% 24%); color: hsl(var(--h) 80% 82%); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .lv-name { background: hsl(var(--h) 40% 24%); color: hsl(var(--h) 80% 82%); } }
.lv-count { margin-left: auto; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.bar { height: 6px; border-radius: 3px; background: var(--bg); overflow: hidden; margin-bottom: 10px; }
.bar i { display: block; height: 100%; border-radius: 3px; background: hsl(var(--h) 70% 55%); }
.level .list { padding-bottom: 10px; }
.kid-card { display: grid; gap: 6px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.kid-card:last-child { border-bottom: 0; }
.kid-row.zpd span { flex: none; }
.kid-row.zpd input { width: 80px; flex: none; }

/* AR 입력 도우미 / 시리즈 일괄 */
.btn-grid { margin-bottom: 8px; }
.btn-grid .btn { font-size: 13.5px; padding: 10px 8px; }
.paste-box { display: grid; gap: 8px; margin-top: 8px; }
.paste-box textarea { font-size: 14px; }
#btnSeries { margin-top: 2px; }
#seriesSheet h2 { margin: 0 0 6px; }
.check-list { display: grid; gap: 6px; margin: 10px 0 4px; }
.check-item { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); cursor: pointer; }
.check-item input { width: 20px; height: 20px; min-height: 0; flex: none; accent-color: var(--primary); }
.check-item .t { flex: 1; min-width: 0; font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
