:root {
  --bg: #ffffff;
  --bg-secondary: #f0eee6;
  --bg-hover: #e8e5db;
  --bg-active: #ddd9ce;
  --text: #1f1e1d;
  --text-secondary: #6b6863;
  --text-muted: #a39e94;
  --border: #e3dfd4;
  --primary: #d97757;
  --primary-light: #f7ede8;
  --sidebar-width: 280px;
  --danger: #bc4b2b;
  --danger-light: #f7e6df;
  --shadow: 0 1px 3px rgba(60,50,40,0.08);
  --radius: 8px;
}

[data-theme="dark"] {
  --bg: #262624;
  --bg-secondary: #1f1e1d;
  --bg-hover: #30302d;
  --bg-active: #3a3a36;
  --text: #eceae3;
  --text-secondary: #b5b1a6;
  --text-muted: #827e74;
  --border: #3d3d39;
  --primary: #e08060;
  --primary-light: #3a2e28;
  --danger: #f0a48a;
  --danger-light: #3c241c;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

#app { display: flex; height: 100vh; position: relative; }

/* ===== Sidebar ===== */

#sidebar {
  width: var(--sidebar-width);
  min-width: 200px;
  max-width: 500px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
  transition: width 0.18s ease, min-width 0.18s ease;
}

#sidebar.collapsed {
  width: 0 !important;
  min-width: 0;
  border-right: none;
}

#sidebar.collapsed .sidebar-header,
#sidebar.collapsed .sidebar-actions,
#sidebar.collapsed #folder-tree,
#sidebar.collapsed .sidebar-footer {
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1 { font-size: 18px; font-weight: 700; letter-spacing: -0.3px; white-space: nowrap; }

.header-btns { display: flex; align-items: center; gap: 2px; }

#theme-toggle, #sidebar-collapse {
  background: none; border: none; cursor: pointer;
  padding: 4px 6px; border-radius: 6px;
  transition: background 0.15s; color: var(--text-secondary);
}
#theme-toggle { font-size: 18px; }
#sidebar-collapse { font-size: 16px; }
#theme-toggle:hover, #sidebar-collapse:hover { background: var(--bg-hover); }

/* Floating reopen button */
#sidebar-expand {
  position: absolute; top: 14px; left: 14px; z-index: 50;
  width: 34px; height: 34px;
  display: none; align-items: center; justify-content: center;
  font-size: 17px; background: var(--bg-secondary); color: var(--text-secondary);
  border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
  box-shadow: var(--shadow); transition: background 0.15s;
}
#sidebar-expand:hover { background: var(--bg-hover); }
#app.sidebar-hidden #sidebar-expand { display: flex; }
#app.sidebar-hidden .reader-header { padding-left: 60px; }
#app.sidebar-hidden #empty-state { padding-left: 60px; }

.sidebar-actions { padding: 12px 16px; }

#folder-tree { flex: 1; overflow-y: auto; padding: 4px 8px 16px; }

.sidebar-footer {
  padding: 8px 16px; border-top: 1px solid var(--border);
  font-size: 11px; color: var(--text-muted); white-space: nowrap;
}

/* Folder items */
.folder-item { margin-bottom: 2px; }

.folder-item.drag-over-top > .folder-header { box-shadow: inset 0 2px 0 var(--primary); }
.folder-item.drag-over-bottom > .folder-header { box-shadow: inset 0 -2px 0 var(--primary); }
.folder-item.dragging { opacity: 0.4; }

.folder-header {
  display: flex; align-items: center;
  padding: 8px 10px; border-radius: var(--radius);
  cursor: pointer; transition: background 0.1s; gap: 6px; user-select: none;
}
.folder-header:hover { background: var(--bg-hover); }
.folder-header.active { background: var(--primary-light); color: var(--primary); }

.folder-drag-handle {
  font-size: 11px; color: var(--text-muted); cursor: grab;
  flex-shrink: 0; opacity: 0; transition: opacity 0.1s;
}
.folder-header:hover .folder-drag-handle { opacity: 1; }

.folder-chevron {
  font-size: 10px; transition: transform 0.15s; color: var(--text-muted);
  width: 14px; text-align: center; flex-shrink: 0;
}
.folder-chevron.open { transform: rotate(90deg); }

.folder-icon { font-size: 14px; flex-shrink: 0; }
.folder-name {
  font-size: 13px; font-weight: 600; flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.folder-count {
  font-size: 11px; color: var(--text-muted); background: var(--bg-hover);
  padding: 1px 6px; border-radius: 10px; flex-shrink: 0;
}
.folder-actions { display: none; gap: 2px; flex-shrink: 0; }
.folder-header:hover .folder-actions { display: flex; }
.folder-header:hover .folder-count { display: none; }
.folder-action-btn {
  background: none; border: none; font-size: 13px; cursor: pointer;
  padding: 2px 4px; border-radius: 4px; color: var(--text-secondary); transition: background 0.1s;
}
.folder-action-btn:hover { background: var(--bg-active); }
.folder-action-btn.danger:hover { background: var(--danger-light); color: var(--danger); }

/* File list */
.folder-files { padding-left: 18px; overflow: hidden; }
.folder-files.collapsed { display: none; }

.file-item {
  display: flex; align-items: center;
  padding: 6px 10px; border-radius: 6px; cursor: pointer;
  transition: background 0.1s; gap: 6px; font-size: 13px; color: var(--text-secondary);
}
.file-item:hover { background: var(--bg-hover); color: var(--text); }
.file-item.active { background: var(--primary-light); color: var(--primary); font-weight: 500; }
.file-item.dragging { opacity: 0.4; }
.file-item.drag-over-top { box-shadow: inset 0 2px 0 var(--primary); }
.file-item.drag-over-bottom { box-shadow: inset 0 -2px 0 var(--primary); }

.drag-handle {
  font-size: 12px; color: var(--text-muted); cursor: grab;
  flex-shrink: 0; opacity: 0; transition: opacity 0.1s;
}
.file-item:hover .drag-handle { opacity: 1; }
.drag-handle:active { cursor: grabbing; }

.file-badge {
  font-size: 9px; font-weight: 700; letter-spacing: 0.3px;
  padding: 1px 4px; border-radius: 3px; flex-shrink: 0;
  text-transform: uppercase;
}
.file-badge.md { background: #f7ede8; color: #c2410c; }
.file-badge.html { background: #e8e9e2; color: #5f6b52; }
[data-theme="dark"] .file-badge.md { background: #3a2e28; color: #e09b7a; }
[data-theme="dark"] .file-badge.html { background: #2c302a; color: #9fac8c; }

.file-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-rename, .file-delete {
  display: none; background: none; border: none; font-size: 12px; cursor: pointer;
  padding: 2px 4px; border-radius: 4px; color: var(--text-muted); flex-shrink: 0;
}
.file-item:hover .file-rename, .file-item:hover .file-delete { display: block; }
.file-rename:hover { background: var(--bg-hover); color: var(--primary); }
.file-delete:hover { background: var(--danger-light); color: var(--danger); }

/* 공유(내장) 노트 */
.folder-shared {
  font-size: 9px; font-weight: 700; letter-spacing: 0.3px;
  padding: 1px 5px; border-radius: 3px; flex-shrink: 0;
  background: #e8e9e2; color: #5f6b52;
}
[data-theme="dark"] .folder-shared { background: #2c302a; color: #9fac8c; }
.drag-handle.ghost { opacity: 0.18 !important; cursor: default; }
.folder-item.builtin .folder-header { cursor: pointer; }

/* 새로고침 버튼 회전 */
@keyframes nr-spin { to { transform: rotate(360deg); } }
#refresh-notes.spinning { animation: nr-spin 0.6s linear infinite; }

/* 토스트 */
#toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%) translateY(12px);
  background: var(--text, #1a1a1a); color: #fff; font-size: 13px; font-weight: 500;
  padding: 9px 16px; border-radius: 999px; box-shadow: 0 4px 16px rgba(0,0,0,0.22);
  opacity: 0; pointer-events: none; transition: opacity 0.22s, transform 0.22s; z-index: 9999;
  white-space: nowrap;
}
#toast.show { opacity: 0.96; transform: translateX(-50%) translateY(0); }

.folder-upload { margin: 4px 0 8px; padding: 0 10px; }
.upload-btn {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  width: 100%; padding: 6px; border: 1px dashed var(--border); border-radius: 6px;
  background: none; color: var(--text-muted); font-size: 12px; cursor: pointer; transition: all 0.15s;
}
.upload-btn:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }

/* ===== Resize Handle ===== */
#resize-handle { width: 4px; cursor: col-resize; background: transparent; transition: background 0.15s; flex-shrink: 0; }
#resize-handle:hover, #resize-handle.active { background: var(--primary); }

/* ===== Main ===== */
#main { flex: 1; overflow: hidden; position: relative; display: flex; flex-direction: column; }

#empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 100%; color: var(--text-muted); text-align: center; padding: 40px;
}
.empty-icon { font-size: 64px; margin-bottom: 16px; }
#empty-state h2 { font-size: 24px; font-weight: 600; color: var(--text-secondary); margin-bottom: 8px; }
#empty-state p { font-size: 14px; line-height: 1.6; }
#empty-state b { color: var(--primary); }
.hint { margin-top: 4px; font-size: 13px !important; color: var(--text-muted); }

#reader { display: flex; flex-direction: column; height: 100%; }

.reader-header {
  background: var(--bg); padding: 12px 24px 8px; z-index: 10;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
#reader-breadcrumb { font-size: 12px; color: var(--text-muted); }
.reader-progress { height: 2px; background: var(--bg-hover); border-radius: 1px; margin-top: 8px; overflow: hidden; }
#read-progress-bar { height: 100%; background: var(--primary); border-radius: 1px; width: 0%; transition: width 0.1s; }

/* md scroll area */
#md-scroll { flex: 1; overflow-y: auto; }
#md-content { max-width: 860px; margin: 0 auto; padding: 28px 40px 100px; }

/* html iframe */
#html-frame { flex: 1; width: 100%; border: none; background: #fff; }

/* ===== Markdown Styles ===== */
.markdown-body { font-size: 15px; line-height: 1.75; color: var(--text); word-wrap: break-word; }
.markdown-body h1 { font-size: 28px; font-weight: 700; margin: 32px 0 16px; padding-bottom: 8px; border-bottom: 2px solid var(--border); letter-spacing: -0.5px; }
.markdown-body h1:first-child { margin-top: 0; }
.markdown-body h2 { font-size: 22px; font-weight: 700; margin: 28px 0 12px; padding-bottom: 6px; border-bottom: 1px solid var(--border); letter-spacing: -0.3px; }
.markdown-body h3 { font-size: 18px; font-weight: 600; margin: 24px 0 10px; }
.markdown-body h4 { font-size: 16px; font-weight: 600; margin: 20px 0 8px; }
.markdown-body h5, .markdown-body h6 { font-size: 14px; font-weight: 600; margin: 16px 0 8px; color: var(--text-secondary); }
.markdown-body p { margin: 0 0 14px; }
.markdown-body a { color: var(--primary); text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }
.markdown-body strong { font-weight: 600; }
.markdown-body ul, .markdown-body ol { margin: 0 0 14px; padding-left: 24px; }
.markdown-body li { margin-bottom: 4px; }
.markdown-body li > ul, .markdown-body li > ol { margin-top: 4px; margin-bottom: 4px; }
.markdown-body blockquote { margin: 0 0 14px; padding: 8px 16px; border-left: 4px solid var(--primary); background: var(--bg-secondary); border-radius: 0 var(--radius) var(--radius) 0; color: var(--text-secondary); }
.markdown-body blockquote p:last-child { margin-bottom: 0; }
.markdown-body code { font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace; font-size: 13px; background: var(--bg-secondary); padding: 2px 6px; border-radius: 4px; border: 1px solid var(--border); }
.markdown-body pre { margin: 0 0 14px; background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; }
.markdown-body pre code { display: block; padding: 14px 18px; border: none; background: none; font-size: 13px; line-height: 1.6; }
.markdown-body hr { border: none; height: 1px; background: var(--border); margin: 24px 0; }
.markdown-body table { width: 100%; border-collapse: collapse; margin: 0 0 14px; font-size: 14px; }
.markdown-body th, .markdown-body td { padding: 8px 12px; border: 1px solid var(--border); text-align: left; }
.markdown-body th { font-weight: 600; background: var(--bg-secondary); }
.markdown-body tr:nth-child(even) { background: var(--bg-secondary); }
.markdown-body img { max-width: 100%; border-radius: var(--radius); margin: 8px 0; }
.markdown-body .task-list-item { list-style: none; margin-left: -24px; padding-left: 4px; }
.markdown-body .task-list-item input[type="checkbox"] { margin-right: 8px; }

/* ===== Drop Overlay ===== */
#drop-overlay {
  position: fixed; inset: 0; background: rgba(13, 148, 136, 0.08); z-index: 1000;
  display: flex; align-items: center; justify-content: center; backdrop-filter: blur(2px);
}
.drop-message {
  background: var(--bg); border: 2px dashed var(--primary); border-radius: 16px;
  padding: 48px 64px; text-align: center; box-shadow: var(--shadow);
}
.drop-icon { font-size: 48px; margin-bottom: 12px; }
.drop-message p { font-size: 16px; color: var(--primary); font-weight: 500; }

/* ===== Modal ===== */
#modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 2000; display: flex; align-items: center; justify-content: center; }
.modal { background: var(--bg); border-radius: 12px; padding: 24px; width: 360px; box-shadow: 0 8px 32px rgba(0,0,0,0.15); }
.modal h3 { font-size: 16px; font-weight: 600; margin-bottom: 16px; }
.modal input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 14px; background: var(--bg); color: var(--text); outline: none; transition: border-color 0.15s; }
.modal input:focus { border-color: var(--primary); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* ===== Buttons ===== */
.btn-primary { background: var(--primary); color: #fff; border: none; padding: 8px 16px; border-radius: var(--radius); font-size: 13px; font-weight: 500; cursor: pointer; transition: opacity 0.15s; width: 100%; }
.btn-primary:hover { opacity: 0.9; }
.btn-secondary { background: var(--bg-hover); color: var(--text-secondary); border: 1px solid var(--border); padding: 8px 16px; border-radius: var(--radius); font-size: 13px; font-weight: 500; cursor: pointer; transition: background 0.15s; }
.btn-secondary:hover { background: var(--bg-active); }
.modal .btn-primary { width: auto; }

/* ===== Utility ===== */
.hidden { display: none !important; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
