:root {
  --bg: #f4f5f7; --panel: #fff; --border: #dfe2e8; --text: #1f2430; --muted: #6b7280;
  --accent: #3b6df0; --accent-fg: #fff; --hover: #eef1f7; --sel: #e3ebff;
  --c-yellow: #ffe14d; --c-green: #6fe08a; --c-blue: #6cb8ff; --c-pink: #ff8fc2; --c-orange: #ffb04d;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14161b; --panel: #1d2027; --border: #2f343f; --text: #e6e8ee; --muted: #9aa1b0;
    --accent: #6b92ff; --hover: #272b35; --sel: #28324f; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.5 -apple-system, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  background: var(--bg); color: var(--text); }
button, select, input, textarea { font: inherit; color: inherit; }
button { background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 4px 10px; cursor: pointer; }
button:hover { background: var(--hover); }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.1); }
button.active { background: var(--sel); border-color: var(--accent); }

#app { display: grid; grid-template-columns: 260px minmax(0, 1fr) 340px; height: 100vh; }

/* 라이브러리 */
#library { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; position: relative; min-height: 0; }
.lib-head { padding: 12px; border-bottom: 1px solid var(--border); display: grid; gap: 8px; }
.lib-head h1 { font-size: 16px; margin: 0; }
.lib-head input { width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
.upload-row { display: flex; gap: 6px; }
.upload-row select { flex: 1; min-width: 0; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); padding: 4px; }
#paperList { overflow-y: auto; flex: 1; padding: 6px 0; }
.cat { margin-bottom: 4px; }
.cat-title { padding: 6px 12px; font-weight: 600; cursor: pointer; display: flex; justify-content: space-between; user-select: none; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.cat.collapsed .paper { display: none; }
.paper { padding: 6px 12px 6px 16px; cursor: pointer; display: flex; align-items: center; gap: 6px; border-left: 3px solid transparent; }
.paper:hover { background: var(--hover); }
.paper.current { background: var(--sel); border-left-color: var(--accent); }
.paper .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.paper .del { opacity: 0; border: 0; background: none; padding: 0 4px; color: var(--muted); }
.paper:hover .del { opacity: 1; }
.paper .badge { font-size: 11px; }
#dropHint { display: none; position: absolute; inset: 0; background: color-mix(in srgb, var(--accent) 18%, transparent); border: 3px dashed var(--accent); align-items: center; justify-content: center; font-weight: 600; pointer-events: none; }
#library.dragging #dropHint { display: flex; }

/* 뷰어 */
#viewerWrap { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
#toolbar { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
#paperTitle { flex: 1; min-width: 120px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-group { display: flex; align-items: center; gap: 4px; }
.swatch { width: 24px; height: 24px; border-radius: 50%; padding: 0; border: 2px solid transparent; }
.swatch:hover { transform: scale(1.12); }
#pageInput { width: 54px; padding: 3px 4px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
#viewer { flex: 1; overflow: auto; padding: 16px; background: var(--bg); }
#viewer .empty { margin: 80px auto; text-align: center; color: var(--muted); }
.page { position: relative; margin: 0 auto 16px; background: #fff; box-shadow: 0 1px 6px rgba(0,0,0,.25); --scale-factor: 1; }
.page canvas { display: block; }
.page .textLayer { position: absolute; inset: 0; z-index: 2; }
.page .hlLayer { position: absolute; inset: 0; z-index: 1; pointer-events: none; mix-blend-mode: multiply; }
.hl { position: absolute; border-radius: 2px; opacity: .5; }
.hl.flash { opacity: .95; outline: 2px solid #e0245e; }
.page .memoLayer { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
body.memo-mode .page .memoLayer { pointer-events: auto; cursor: crosshair; }
body.memo-mode .page .textLayer { pointer-events: none; }
.memo { position: absolute; pointer-events: auto; background: #fff8b8; color: #222; border: 1px solid #d8c64a; box-shadow: 0 2px 6px rgba(0,0,0,.25); border-radius: 4px; display: flex; flex-direction: column; min-width: 80px; min-height: 40px; resize: both; overflow: hidden; }
.memo .mh { height: 14px; background: #f0dc66; cursor: move; display: flex; justify-content: flex-end; flex: none; }
.memo .mh span { font-size: 11px; line-height: 14px; padding: 0 5px; cursor: pointer; }
.memo textarea { flex: 1; border: 0; background: transparent; resize: none; padding: 3px 5px; font-size: 13px; outline: none; color: #222; }

#selPopup { position: fixed; z-index: 20; background: var(--panel); border: 1px solid var(--border); border-radius: 20px; padding: 4px 6px; display: flex; gap: 5px; box-shadow: 0 4px 14px rgba(0,0,0,.25); }
#selPopup[hidden] { display: none; }
#selPopup .del-btn { border-radius: 14px; padding: 0 8px; font-size: 12px; }

/* 패널 */
#panel { background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tabs button { flex: 1; border: 0; border-radius: 0; padding: 10px; background: none; border-bottom: 2px solid transparent; }
.tabs button.active { border-bottom-color: var(--accent); font-weight: 600; background: none; }
.tab { display: none; padding: 12px; overflow-y: auto; flex: 1; min-height: 0; }
.tab.active { display: block; }
#tab-notes.active { display: flex; flex-direction: column; gap: 6px; }
#notesArea { flex: 1; width: 100%; resize: none; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); padding: 8px; line-height: 1.6; }
h3 { font-size: 12px; color: var(--muted); margin: 14px 0 4px; text-transform: uppercase; letter-spacing: .04em; }
.box { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 8px; white-space: pre-wrap; word-break: break-word; max-height: 32vh; overflow-y: auto; }
#trTarget { font-size: 15px; }
.row { display: flex; gap: 6px; margin-top: 8px; }
.hint { color: var(--muted); font-size: 12px; }
.hist, .annot { border: 1px solid var(--border); border-radius: 6px; padding: 8px; margin-bottom: 8px; cursor: pointer; font-size: 13px; }
.hist:hover, .annot:hover { background: var(--hover); }
.hist .src, .annot .txt { color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.annot { border-left: 5px solid var(--c); }
.annot .meta { display: flex; justify-content: space-between; align-items: center; font-size: 11px; color: var(--muted); margin-bottom: 4px; gap: 4px; }
.annot .tr { margin-top: 4px; }
.annot input.cm { width: 100%; margin-top: 6px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 4px; background: var(--bg); }
.annot .mini { display: flex; gap: 3px; align-items: center; }
.annot .mini i { width: 12px; height: 12px; border-radius: 50%; display: inline-block; cursor: pointer; }
.annot .mini b { cursor: pointer; padding: 0 4px; font-weight: normal; }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #222; color: #fff; padding: 8px 16px; border-radius: 8px; z-index: 50; }
#toast[hidden] { display: none; }

/* ---- 로그인/폴더 게이트 ---- */
#gate { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 20px; }
#gate[hidden] { display: none; }
#gate .card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 28px; max-width: 460px; width: 100%; display: grid; gap: 12px; }
#gate h2 { margin: 0; font-size: 20px; }
#gate p { margin: 0; color: var(--muted); }
#gate button { padding: 10px 14px; }
#gate .err { color: #d33; }

/* ---- 라이브러리 하단 ---- */
.lib-foot { border-top: 1px solid var(--border); padding: 8px 12px; display: grid; gap: 6px; }
.lib-foot .who { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-foot .btns { display: flex; gap: 6px; }
.lib-foot .btns button { flex: 1; padding: 4px 6px; font-size: 12px; }

/* ---- 대화상자 ---- */
dialog { border: 1px solid var(--border); border-radius: 10px; background: var(--panel); color: var(--text); padding: 20px; max-width: 460px; width: calc(100% - 40px); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin: 0 0 12px; font-size: 17px; }
dialog label { display: grid; gap: 4px; margin-bottom: 10px; font-size: 13px; }
dialog input[type=password], dialog input[type=text], dialog select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
dialog label.chk { display: flex; gap: 6px; align-items: center; }
dialog .row { justify-content: flex-end; }
dialog .menu-list { display: grid; gap: 8px; }
dialog .menu-list button { text-align: left; padding: 8px 10px; }
dialog .danger { color: #d33; border-color: #d33; }
dialog .note { font-size: 12px; color: var(--muted); margin: 0 0 10px; }

dialog { max-height: 90vh; overflow-y: auto; }
dialog details.guide { margin: 0 0 12px; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; background: var(--bg); }
dialog details.guide summary { cursor: pointer; font-weight: 600; font-size: 13px; }
dialog details.guide ol { padding-left: 18px; margin: 8px 0; font-size: 13px; display: grid; gap: 8px; }
dialog details.guide ul { padding-left: 16px; margin: 4px 0 0; }
dialog details.guide code { background: var(--panel); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; user-select: all; white-space: nowrap; }
dialog details.guide code#guideDomain { white-space: normal; word-break: break-all; }
dialog details.guide a { color: var(--accent); }
