:root {
  --paper: #F7F5F0;
  --paper-2: #EFECE4;
  --card: #FFFFFF;
  --ink: #1F2833;
  --ink-2: #5B6573;
  --ink-3: #8A93A0;
  --line: #E3DFD5;
  --accent: #2F4A6D;
  --accent-2: #3E5F8A;
  --accent-soft: #E4EAF2;
  --danger: #B3403A;
  --radius: 14px;
  --shadow-sm: 0 1px 2px rgba(31, 40, 51, .06), 0 1px 3px rgba(31, 40, 51, .05);
  --shadow-md: 0 6px 18px rgba(31, 40, 51, .09), 0 2px 6px rgba(31, 40, 51, .05);
  --shadow-page: 0 2px 6px rgba(31, 40, 51, .08), 0 12px 32px rgba(31, 40, 51, .10);
  --ease: cubic-bezier(.2, .7, .2, 1);
  --line-strong: #CFC9BC;
  --hover: rgba(47, 74, 109, .07);
  --topbar: rgba(247, 245, 240, .86);
  --on-accent: #FFFFFF;
  --sheet: #FFFFFF;          /* 페이지 종이 (캔버스가 그려지기 전 자리) */
  font-family: "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  color-scheme: light;
}

/* 어두운 화면: 같은 종이 톤을 어둡게 (페이지 종이는 FORMAT.md v3 의 #1E1E20).
   JS 가 data-theme 를 정하기 전 깜빡임을 막으려고 시스템 설정에도 같은 값을 건다. */
:root[data-theme="dark"] {
  --paper: #161618; --paper-2: #1C1C1F; --card: #222225;
  --ink: #ECE9E2; --ink-2: #B6B2A9; --ink-3: #8B8880;
  --line: #333338; --line-strong: #46464D;
  --accent: #9DB8DC; --accent-2: #B5CBE8; --accent-soft: #2A3443; --on-accent: #12161C;
  --danger: #E07A73;
  --hover: rgba(157, 184, 220, .10);
  --topbar: rgba(22, 22, 24, .86);
  --sheet: #1E1E20;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .35);
  --shadow-md: 0 6px 18px rgba(0, 0, 0, .45), 0 2px 6px rgba(0, 0, 0, .3);
  --shadow-page: 0 2px 6px rgba(0, 0, 0, .4), 0 12px 32px rgba(0, 0, 0, .45);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --paper: #161618; --paper-2: #1C1C1F; --card: #222225;
    --ink: #ECE9E2; --ink-2: #B6B2A9; --ink-3: #8B8880;
    --line: #333338; --line-strong: #46464D;
    --accent: #9DB8DC; --accent-2: #B5CBE8; --accent-soft: #2A3443; --on-accent: #12161C;
    --topbar: rgba(22, 22, 24, .86);
    --sheet: #1E1E20;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--paper);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- 공통 ---------- */
.view { animation: view-in .35s var(--ease) both; }
@keyframes view-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--ink);
  font-size: 14px; font-weight: 500; white-space: nowrap;
  cursor: pointer; user-select: none;
  transition: background .15s, border-color .15s, box-shadow .15s, transform .1s;
}
.btn:hover { border-color: var(--line-strong); box-shadow: var(--shadow-sm); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .card:focus-visible, input:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--hover); border-color: transparent; box-shadow: none; }
.btn-icon { width: 36px; padding: 0; }
.btn svg { width: 18px; height: 18px; flex: none; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 20px;
  background: var(--topbar);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; color: var(--accent); margin-right: 6px; }
.logo { width: 26px; height: 28px; border-radius: 6px; background: var(--accent); position: relative; flex: none; }
.logo::before, .logo::after { content: ""; position: absolute; left: 6px; right: 6px; height: 2px; border-radius: 1px; background: var(--paper); }
.logo::before { top: 9px; box-shadow: 0 5px 0 var(--paper); }
.logo::after { top: 19px; right: 10px; }
.spacer { flex: 1; }

/* ---------- 로그인 ---------- */
.view-login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 380px;
  background: var(--card); border: 1px solid var(--line); border-radius: 20px;
  box-shadow: var(--shadow-md);
  padding: 36px 28px 28px; text-align: center;
}
.login-card .logo { width: 44px; height: 48px; margin: 0 auto 14px; border-radius: 10px; }
.login-card .logo::before { left: 10px; right: 10px; top: 15px; height: 3px; box-shadow: 0 8px 0 var(--paper); }
.login-card .logo::after { left: 10px; right: 18px; top: 31px; height: 3px; }
.login-card h1 { margin: 0 0 6px; font-size: 22px; color: var(--accent); }
.login-card p { margin: 0 0 22px; color: var(--ink-2); font-size: 14px; line-height: 1.5; }
.login-form { display: grid; gap: 10px; text-align: left; }
.login-form label { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.code-input {
  height: 52px; width: 100%;
  border: 1px solid var(--line); border-radius: 12px; background: var(--paper);
  text-align: center; font-size: 24px; letter-spacing: .3em; font-variant-numeric: tabular-nums;
  transition: border-color .15s, background .15s;
}
.code-input:focus { outline: none; border-color: var(--accent-2); background: var(--card); }
.login-form .btn { height: 46px; font-size: 15px; }
.form-error { min-height: 20px; color: var(--danger); font-size: 13px; text-align: center; }
.hint { margin-top: 18px !important; margin-bottom: 0 !important; font-size: 12.5px !important; color: var(--ink-3) !important; }
.hint a { color: var(--accent); }

/* ---------- 불러오는 중 / 오류 ---------- */
.center-state { min-height: 70vh; display: grid; place-items: center; text-align: center; color: var(--ink-2); padding: 24px 16px; }
.center-state .spinner { margin: 0 auto 14px; }
.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid var(--accent-soft); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 노트 목록 ---------- */
.search {
  flex: 1 1 220px; max-width: 420px; position: relative;
}
.search svg { position: absolute; left: 11px; top: 50%; width: 16px; height: 16px; transform: translateY(-50%); color: var(--ink-3); pointer-events: none; }
.search input {
  width: 100%; height: 38px; padding: 0 12px 0 34px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card);
  transition: border-color .15s, box-shadow .15s;
}
.search input:focus { outline: none; border-color: var(--accent-2); box-shadow: 0 0 0 3px var(--accent-soft); }
.library { max-width: 1280px; margin: 0 auto; padding: 18px 20px 48px; }
.lib-meta { color: var(--ink-3); font-size: 13px; margin: 2px 2px 14px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 18px; }
.card {
  display: flex; flex-direction: column;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; text-decoration: none; color: inherit;
  box-shadow: var(--shadow-sm);
  animation: card-in .45s var(--ease) both;
  transition: transform .2s var(--ease), box-shadow .2s var(--ease), border-color .2s;
}
.card:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); border-color: var(--line-strong); }
@keyframes card-in {
  from { opacity: 0; transform: translateY(12px) scale(.985); }
  to { opacity: 1; transform: none; }
}
.thumb {
  height: 190px; background: var(--paper-2);
  display: flex; align-items: center; justify-content: center;
  border-bottom: 1px solid var(--line);
}
.thumb canvas { display: block; box-shadow: 0 1px 4px rgba(31, 40, 51, .14); background: var(--sheet); animation: fade .3s ease both; }
.thumb-ph { width: 46%; height: 70%; background: var(--sheet); opacity: .7; box-shadow: 0 1px 4px rgba(31, 40, 51, .08); }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.card-body { padding: 11px 13px 13px; }
.card-title { font-weight: 600; font-size: 14.5px; line-height: 1.35; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meta { margin-top: 4px; font-size: 12.5px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.badge { display: inline-block; margin-left: 4px; padding: 0 6px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); font-size: 11px; font-weight: 600; line-height: 18px; vertical-align: 1px; }
.empty { text-align: center; color: var(--ink-3); padding: 60px 0; }

/* ---------- 폴더 경로 · 폴더 ---------- */
.crumbs { display: flex; align-items: center; flex-wrap: wrap; gap: 2px 4px; min-height: 36px; margin: 0 0 4px; font-size: 15px; }
.crumb {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 4px 8px; border-radius: 8px;
  color: var(--ink-2); text-decoration: none; font-weight: 500;
  transition: background .15s, color .15s;
}
a.crumb:hover { background: var(--hover); color: var(--ink); }
.crumb-now { color: var(--ink); font-weight: 700; }
.crumb-sep { color: var(--ink-3); padding: 0 1px; }
.crumb-up { width: 32px; height: 32px; margin-right: 2px; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--ink-3); flex: none; }

.section-label { margin: 14px 2px 10px; font-size: 12px; font-weight: 700; letter-spacing: .08em; color: var(--ink-3); }
.folder-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; margin-bottom: 8px; }
/* 폴더 = 왼쪽에 폴더 색 띠 + 오른쪽에 든 노트 수만큼 겹친 종이 옆면 */
.folder {
  --fc: var(--accent);
  position: relative; display: flex; align-items: stretch; min-height: 68px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  overflow: hidden; text-decoration: none; color: inherit;
  box-shadow: var(--shadow-sm);
  animation: card-in .4s var(--ease) both;
  transition: transform .2s var(--ease), box-shadow .2s var(--ease), border-color .2s;
}
@supports (background: color-mix(in srgb, red 10%, white)) {
  .folder { background: linear-gradient(90deg, color-mix(in srgb, var(--fc) 9%, var(--card)), var(--card) 70%); }
}
.folder:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); border-color: var(--line-strong); }
.folder:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }
.folder-band { width: 7px; flex: none; background: var(--fc); }
.folder-body { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 3px; padding: 10px 12px 10px 13px; }
.folder-name { font-weight: 650; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-meta { font-size: 12.5px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.folder-sheets { display: flex; flex-direction: column; justify-content: center; gap: 3px; padding: 0 14px 0 4px; }
.folder-sheets i {
  display: block; width: 22px; height: 3px; border-radius: 2px;
  background: var(--fc); opacity: .55;
  transition: transform .2s var(--ease);
}
.folder-sheets i:nth-child(2) { opacity: .4; width: 19px; }
.folder-sheets i:nth-child(3) { opacity: .3; width: 16px; }
.folder-sheets i:nth-child(4) { opacity: .22; width: 13px; }
.folder:hover .folder-sheets i { transform: translateX(-3px); }
.folder:hover .folder-sheets i:nth-child(2) { transform: translateX(-1px); }

/* 검색 결과에서만: 노트가 있는 폴더 */
.card-where { display: none; margin-top: 3px; font-size: 12px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-where .dot { width: 7px; height: 7px; margin-right: 5px; vertical-align: 1px; }
.grid.searching .card-where { display: block; }

/* ---------- 노트 보기 ---------- */
.note-bar { gap: 8px; }
.note-title { flex: 1 1 160px; min-width: 0; }
.note-title b { display: block; font-size: 15.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-title small { display: block; font-size: 12px; color: var(--ink-3); }
.group { display: inline-flex; align-items: center; gap: 2px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1px; }
.group .btn { height: 32px; border: 0; border-radius: 8px; background: transparent; box-shadow: none; }
.group .btn:hover { background: var(--hover); }
.zoom-label { min-width: 48px; text-align: center; font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.page-ind { font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; padding: 0 4px; min-width: 52px; text-align: center; }

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30;
  min-width: 210px; padding: 6px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow-md);
  opacity: 0; transform: translateY(-4px) scale(.98); transform-origin: top right;
  pointer-events: none; transition: opacity .15s, transform .15s var(--ease);
}
.menu.open .menu-list { opacity: 1; transform: none; pointer-events: auto; }
.menu-item {
  display: flex; width: 100%; align-items: center; gap: 10px;
  padding: 9px 10px; border: 0; border-radius: 8px; background: none;
  text-align: left; font-size: 14px; cursor: pointer;
}
.menu-item:hover { background: var(--accent-soft); }
.menu-item svg { width: 17px; height: 17px; color: var(--accent); flex: none; }
.menu-sep { height: 1px; background: var(--line); margin: 5px 4px; }
.menu-hint { margin: 6px 4px 2px; padding: 8px 6px 2px; border-top: 1px solid var(--line); font-size: 11.5px; line-height: 1.45; color: var(--ink-3); max-width: 240px; }
.export-hint { font-size: 11px; line-height: 1.25; color: var(--ink-3); text-align: right; white-space: nowrap; }
.theme-btn[aria-pressed="true"] svg { transform: rotate(180deg); }
.theme-btn svg { transition: transform .3s var(--ease); }

.pages { overflow-x: auto; padding: 24px 0 64px; }
.pages-inner { width: max-content; min-width: 100%; padding: 0 16px; }
.page-wrap {
  position: relative; margin: 0 auto 26px;
  background: var(--sheet); box-shadow: var(--shadow-page);
  animation: card-in .4s var(--ease) both;
}
.page-wrap canvas { display: block; width: 100%; height: 100%; }
.page-wrap canvas.fresh { animation: fade .25s ease both; }
.page-num { position: absolute; left: 0; right: 0; bottom: -20px; text-align: center; font-size: 11.5px; color: var(--ink-3); font-variant-numeric: tabular-nums; }

/* ---------- 진행 표시 / 알림 ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center; padding: 16px;
  background: rgba(31, 40, 51, .28);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  animation: fade .2s ease both;
}
.overlay-card {
  width: 100%; max-width: 340px; padding: 26px 24px 18px;
  background: var(--card); border-radius: 18px; box-shadow: var(--shadow-md);
  text-align: center; animation: view-in .3s var(--ease) both;
}
.overlay-card .spinner { margin: 0 auto 14px; }
.overlay-title { font-weight: 600; margin-bottom: 14px; }
.bar { height: 6px; border-radius: 3px; background: var(--accent-soft); overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--accent); border-radius: 3px; transition: width .2s ease; }
.overlay-text { margin: 10px 0 12px; font-size: 13px; color: var(--ink-2); min-height: 18px; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 110;
  max-width: calc(100% - 32px);
  padding: 11px 18px; border-radius: 12px;
  background: var(--ink); color: var(--paper); font-size: 14px;
  box-shadow: var(--shadow-md);
  opacity: 0; transform: translate(-50%, 12px); pointer-events: none;
  transition: opacity .2s, transform .25s var(--ease);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

#print-root { display: none; }

/* ---------- 좁은 화면 ---------- */
@media (max-width: 720px) {
  .topbar { padding: 8px 12px; gap: 6px; }
  .library { padding: 14px 12px 40px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
  .thumb { height: 160px; }
  .hide-sm { display: none !important; }
  .btn { padding: 0 10px; }
  .note-title { order: -1; flex-basis: calc(100% - 50px); }
  .pages { padding-top: 16px; }
  .pages-inner { padding: 0 8px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- 인쇄: 미리 그린 페이지 이미지만 ---------- */
@media print {
  @page { margin: 0; }
  html, body { background: #fff !important; margin: 0 !important; padding: 0 !important; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block !important; }
  .print-page {
    overflow: hidden; margin: 0; padding: 0;
    break-after: page; page-break-after: always; break-inside: avoid;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  .print-page:last-child { break-after: auto; page-break-after: auto; }
  .print-page img { display: block; width: 100%; height: 100%; object-fit: cover; object-position: left top; }
}
