/* ===== Theme: Matrix (black & green) ===== */
.theme-matrix {
	--bg: #080c08;
	--bg-side: #0d140d;
	--bg-list: #0f1a0f;
	--bg-editor: #0a100a;
	--bg-input: rgba(50,255,112,0.06);
	--bg-hover: rgba(50,255,112,0.08);
	--bg-active: rgba(50,255,112,0.16);
	--text: #c8f0c8;
	--text-dim: #6a9a6a;
	--text-heading: #e0ffe0;
	--accent: #32ff70;
	--border: rgba(50,255,112,0.12);
	--border-focus: rgba(50,255,112,0.4);
	--danger: #ff5555;
	--toolbar-bg: #0d140d;
	--scrollbar: rgba(50,255,112,0.15);
	--statusbar-bg: #060a06;
	color-scheme: dark;
}

/* ===== Theme: Dark (blue-gray) ===== */
.theme-dark {
	--bg: #1d2024;
	--bg-side: #263238;
	--bg-list: #2c3540;
	--bg-editor: #1d2024;
	--bg-input: rgba(255,255,255,0.06);
	--bg-hover: rgba(255,255,255,0.06);
	--bg-active: rgba(100,149,237,0.25);
	--text: #cdd8e0;
	--text-dim: #8a9baa;
	--text-heading: #e0e8f0;
	--accent: #6c9eff;
	--border: rgba(255,255,255,0.08);
	--border-focus: rgba(100,149,237,0.5);
	--danger: #ff5555;
	--toolbar-bg: #263238;
	--scrollbar: rgba(255,255,255,0.1);
	--statusbar-bg: #1a1e22;
	color-scheme: dark;
}

/* ===== Theme: Light ===== */
.theme-light {
	--bg: #ffffff;
	--bg-side: #f0f4f8;
	--bg-list: #ffffff;
	--bg-editor: #ffffff;
	--bg-input: rgba(0,0,0,0.04);
	--bg-hover: rgba(0,0,0,0.04);
	--bg-active: rgba(100,149,237,0.15);
	--text: #32363a;
	--text-dim: #6c757d;
	--text-heading: #1d2024;
	--accent: #0066cc;
	--border: rgba(0,0,0,0.09);
	--border-focus: rgba(0,102,204,0.4);
	--danger: #dc3545;
	--toolbar-bg: #f7f7f7;
	--scrollbar: rgba(0,0,0,0.12);
	--statusbar-bg: #e8ecf0;
	color-scheme: light;
}

/* ===== Reset ===== */
*,*::before,*::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
	font-size: 13px;
	line-height: 1.4;
	color: var(--text);
	background: var(--bg);
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }

/* ===== 3-Column App Layout ===== */
.app {
	display: flex;
	height: calc(100vh - 28px);
	overflow: hidden;
}

.mobile-topbar,
.mobile-nav-toggle,
.mobile-nav-backdrop { display: none; }

.col-nav {
	display: flex;
	flex-direction: column;
	width: 320px;
	min-width: 320px;
	background: var(--bg-side);
	border-right: 1px solid var(--border);
	min-width: 0;
	height: 100%;
}

.nav-panel-header {
	display: flex;
	align-items: center;
	gap: 6px;
	padding: 8px 10px;
	border-bottom: 1px solid var(--border);
	background: var(--bg-side);
	flex-shrink: 0;
}

.nav-items {
	flex: 1;
	overflow-y: auto;
	padding: 6px;
}

.nav-folder {
	margin-bottom: 6px;
	border: 1px solid var(--border);
	border-radius: 8px;
	overflow: hidden;
	background: rgba(255,255,255,0.02);
}

.nav-folder.active {
	border-color: var(--border-focus);
	background: var(--bg-active);
}

.nav-folder-row {
	display: flex;
	align-items: center;
	gap: 6px;
	padding: 7px 8px;
	cursor: pointer;
}

.nav-folder-toggle {
	width: 18px;
	height: 18px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	color: var(--text-dim);
	font-size: 11px;
	transition: transform 0.12s;
	flex-shrink: 0;
}

.nav-folder.collapsed .nav-folder-toggle { transform: rotate(-90deg); }

.nav-folder-title {
	flex: 1;
	min-width: 0;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	font-weight: 600;
}

.nav-folder .sidebar-item-icon {
	font-size: 12px;
	width: 14px;
}

.nav-folder-add {
	flex-shrink: 0;
	width: 20px;
	height: 20px;
	font-size: 14px;
}

.nav-folder-notes {
	padding: 0 0 4px;
	border-top: 1px solid var(--border);
}

.nav-folder.collapsed .nav-folder-notes { display: none; }

.nav-folder-notes .notelist-item {
	padding: 7px 10px 7px 34px;
	border-bottom: none;
	background: transparent;
}

.nav-empty {
	padding: 6px 10px 8px 34px;
	font-size: 11px;
}

/* Column 1: Sidebar (notebooks) */
.col-sidebar {
	width: 220px;
	min-width: 220px;
	background: var(--bg-side);
	border-right: 1px solid var(--border);
	display: flex;
	flex-direction: column;
	transition: width 0.2s, min-width 0.2s;
}
.col-sidebar.collapsed {
	width: 42px;
	min-width: 42px;
	overflow: hidden;
}
.col-sidebar.collapsed .col-label,
.col-sidebar.collapsed .col-scroll,
.col-sidebar.collapsed .col-header .btn-icon-sm:last-child {
	display: none;
}

.col-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 10px 12px 6px;
	flex-shrink: 0;
}

.col-label {
	font-size: 11px;
	font-weight: 700;
	letter-spacing: 0.08em;
	color: var(--text-dim);
	text-transform: uppercase;
}

.btn-icon-sm {
	width: 22px;
	height: 22px;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 4px;
	font-size: 16px;
	font-weight: 600;
	color: var(--text-dim);
	transition: background 0.1s;
}
.btn-icon-sm:hover { background: var(--bg-hover); color: var(--text); }

.col-scroll {
	flex: 1;
	overflow-y: auto;
	padding: 4px 6px 8px;
}

/* Sidebar items */
.sidebar-item {
	display: flex;
	align-items: center;
	gap: 6px;
	width: 100%;
	padding: 5px 8px;
	border-radius: 4px;
	text-align: left;
	transition: background 0.1s;
	margin-bottom: 1px;
}
.sidebar-item:hover { background: var(--bg-hover); }
.sidebar-item.active { background: var(--bg-active); }

.sidebar-item-icon {
	font-size: 14px;
	flex-shrink: 0;
	width: 18px;
	text-align: center;
}

.sidebar-item-name {
	flex: 1;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.sidebar-item-count {
	font-size: 11px;
	color: var(--text-dim);
	flex-shrink: 0;
	min-width: 16px;
	text-align: right;
}

/* Column 2: Note list */
.col-notelist {
	width: 280px;
	min-width: 280px;
	background: var(--bg-list);
	border-right: 1px solid var(--border);
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

.notelist-header {
	display: flex;
	align-items: center;
	gap: 6px;
	padding: 8px 10px;
	border-bottom: 1px solid var(--border);
	flex-shrink: 0;
}

.notelist-search {
	flex: 1;
	padding: 5px 8px;
	border: 1px solid var(--border);
	border-radius: 4px;
	background: var(--bg-input);
	outline: none;
	font-size: 12px;
}
.notelist-search:focus { border-color: var(--border-focus); }

.notelist-items {
	flex: 1;
	overflow-y: auto;
}

.notelist-item {
	display: block;
	width: 100%;
	padding: 8px 12px;
	text-align: left;
	border-bottom: 1px solid var(--border);
	transition: background 0.1s;
}
.notelist-item:hover { background: var(--bg-hover); }
.notelist-item.active { background: var(--bg-active); }

.notelist-item-title {
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* Column 3: Editor */
.col-editor {
	flex: 1;
	min-width: 0;
	background: var(--bg-editor);
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

.editor-empty {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100%;
	color: var(--text-dim);
}

.editor-form {
	display: flex;
	flex-direction: column;
	height: 100%;
}

.editor-titlebar {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 8px 16px;
	border-bottom: 1px solid var(--border);
	flex-shrink: 0;
}

.editor-title {
	flex: 1;
	padding: 4px 0;
	border: none;
	background: transparent;
	font-size: 18px;
	font-weight: 700;
	color: var(--text-heading);
	outline: none;
	min-height: 1.4em;
}
.editor-title:empty::before {
	content: attr(data-placeholder);
	color: var(--text-muted, #888);
	pointer-events: none;
}

.editor-folder-select {
	padding: 3px 6px;
	border: 1px solid var(--border);
	border-radius: 4px;
	background: var(--bg-sidebar);
	color: var(--text);
	font-size: 12px;
	max-width: 160px;
	outline: none;
	cursor: pointer;
}
.editor-folder-select:focus { border-color: var(--accent); }
.editor-folder-arrow {
	color: var(--text-dim);
	font-size: 14px;
	margin: 0 2px;
}

/* Toolbar */
.editor-toolbar {
	display: flex;
	align-items: center;
	gap: 1px;
	padding: 4px 12px;
	border-bottom: 1px solid var(--border);
	background: var(--toolbar-bg);
	flex-shrink: 0;
	flex-wrap: wrap;
}

.tb {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	min-width: 28px;
	height: 26px;
	padding: 0 5px;
	border-radius: 3px;
	font-size: 13px;
	color: var(--text-dim);
	transition: background 0.1s, color 0.1s;
}
.tb:hover { background: var(--bg-hover); color: var(--text); }

.tb-div {
	width: 1px;
	height: 16px;
	background: var(--border);
	margin: 0 4px;
}

/* Editor body */
.editor-body {
	flex: 1;
	padding: 16px 20px;
	border: none;
	background: transparent;
	resize: none;
	outline: none;
	font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', Consolas, monospace;
	font-size: 14px;
	line-height: 1.65;
	tab-size: 4;
}

/* Preview pane */
.editor-preview {
	flex: 1;
	padding: 16px 20px;
	overflow-y: auto;
	font-size: 15px;
	line-height: 1.7;
	color: var(--text);
	outline: none;
}
.editor-preview p { margin: 0.2em 0; }
.editor-preview p:first-child { margin-top: 0; }
.editor-preview .md-blank-line { height: 1em; }
.editor-preview[contenteditable="true"] {
	cursor: text;
}
.editor-preview h1, .editor-preview h2, .editor-preview h3,
.editor-preview h4, .editor-preview h5, .editor-preview h6 {
	color: var(--text-heading);
	margin: 1em 0 0.5em;
}
.editor-preview h1 { font-size: 1.8em; }
.editor-preview h2 { font-size: 1.5em; }
.editor-preview h3 { font-size: 1.25em; }
.editor-preview strong { color: var(--text-heading); }
.editor-preview a { color: var(--accent); }
.editor-preview code {
	background: var(--bg-hover);
	padding: 2px 5px;
	border-radius: 3px;
	font-family: 'SF Mono', 'Fira Code', Consolas, monospace;
	font-size: 0.9em;
}
.editor-preview pre {
	background: var(--bg-hover);
	padding: 12px 16px;
	border-radius: 6px;
	overflow-x: auto;
	margin: 1em 0;
}
.editor-preview pre code { background: none; padding: 0; }
.editor-preview blockquote {
	border-left: 3px solid var(--accent);
	margin: 1em 0;
	padding: 4px 16px;
	color: var(--text-dim);
}
.editor-preview hr { border: none; border-top: 1px solid var(--border); margin: 1.5em 0; }
.editor-preview ul { padding-left: 1.5em; margin: 0.5em 0; }
.editor-preview li { margin: 0.25em 0; }
.editor-preview .md-checkbox { margin: 0.25em 0; }
.editor-preview .md-checkbox.checked { text-decoration: line-through; opacity: 0.7; }
.preview-img { max-width: 100%; border-radius: 6px; margin: 8px 0; cursor: default; }
.editor-preview[contenteditable="true"] .preview-img { cursor: nwse-resize; border: 1px solid transparent; }
.editor-preview[contenteditable="true"] .preview-img:hover { border-color: var(--accent); opacity: 0.95; }
.editor-preview[contenteditable="true"] .preview-img:active { border-color: var(--accent); }

/* Autosave */
.autosave-ok { color: var(--accent); font-size: 11px; opacity: 0.8; }
.autosave-edited { color: var(--text-dim); font-size: 11px; opacity: 0.9; }
.autosave-error { color: var(--danger); font-size: 11px; }
.autosave-conflict { display: inline-flex; align-items: center; gap: 6px; }
.htmx-indicator { display: none; color: var(--text-dim); font-size: 11px; }
.htmx-request .htmx-indicator { display: inline; }
.htmx-request #autosave-status { display: none; }

.note-meta {
	color: var(--text-dim);
	font-size: 10px;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	max-width: 260px;
}

/* Status bar */
.app-statusbar {
	height: 28px;
	display: flex;
	align-items: center;
	gap: 10px;
	padding: 0 12px;
	background: var(--statusbar-bg);
	border-top: 1px solid var(--border);
	font-size: 11px;
	color: var(--text-dim);
	flex-shrink: 0;
}

.status-user { }
.status-spacer { flex: 1; }

.theme-picker {
	padding: 2px 4px;
	border: 1px solid var(--border);
	border-radius: 3px;
	background: var(--bg-input);
	font-size: 11px;
	outline: none;
	cursor: pointer;
}

/* Buttons */
.btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	padding: 5px 12px;
	border-radius: 4px;
	border: 1px solid var(--border);
	font-weight: 500;
	font-size: 12px;
	transition: background 0.1s;
	text-decoration: none;
	white-space: nowrap;
}
.btn:hover { background: var(--bg-hover); }

.btn-primary {
	background: var(--accent);
	color: #fff;
	border-color: var(--accent);
}
.btn-primary:hover { filter: brightness(1.1); }

.btn-secondary { background: var(--bg-input); }

.btn-sm { padding: 4px 10px; font-size: 12px; }

.btn-icon {
	width: 28px;
	height: 28px;
	padding: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 4px;
	border: none;
	font-size: 14px;
	color: var(--text-dim);
	transition: background 0.1s;
}
.btn-icon:hover { background: var(--bg-hover); }
.btn-icon.active { color: var(--accent); background: var(--bg-active); }
.btn-danger:hover { color: var(--danger); }

.empty-hint {
	padding: 12px;
	color: var(--text-dim);
	font-size: 12px;
}

/* Login */
.login-page {
	display: flex;
	align-items: center;
	justify-content: center;
	min-height: 100vh;
}

.login-card { text-align: center; max-width: 360px; width: 100%; }

.login-title {
	font-size: 36px;
	font-weight: 800;
	color: var(--accent);
	margin-bottom: 8px;
}

.login-sub {
	color: var(--text-dim);
	margin-bottom: 20px;
}

.login-form {
	display: flex;
	flex-direction: column;
	gap: 10px;
}

.login-input {
	padding: 10px 12px;
	border: 1px solid var(--border);
	border-radius: 4px;
	background: var(--bg-main);
	color: var(--text);
	font-size: 14px;
	outline: none;
}

.login-input:focus {
	border-color: var(--accent);
}

.login-btn {
	padding: 10px 12px;
	font-size: 14px;
	margin-top: 4px;
}

.login-error {
	color: #e74c3c;
	font-size: 13px;
	min-height: 1.2em;
}

.login-password-wrap {
	position: relative;
	display: flex;
}

.login-password-wrap .login-input {
	flex: 1;
	padding-right: 36px;
}

.login-eye {
	position: absolute;
	right: 4px;
	top: 50%;
	transform: translateY(-50%);
	background: none;
	border: none;
	cursor: pointer;
	font-size: 18px;
	padding: 4px;
	line-height: 1;
	opacity: 0.6;
}

.login-eye:hover {
	opacity: 1;
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 3px; }

/* Responsive */
@media (max-width: 768px) {
	body { font-size: 12px; }
	.login-page {
		padding: calc(20px + env(safe-area-inset-top, 0px)) 16px 20px;
		align-items: flex-start;
	}
	.login-card {
		max-width: 320px;
		margin: 0 auto;
	}
	.login-title { font-size: 30px; }
	.login-sub { margin-bottom: 16px; }
	.login-input,
	.login-btn { font-size: 16px; }
	.app { display: block; position: relative; height: calc(100vh - 24px); }
	body.mobile-nav-open { overflow: hidden; }
	.mobile-topbar {
		display: flex;
		align-items: center;
		height: calc(42px + env(safe-area-inset-top, 0px));
		padding: env(safe-area-inset-top, 0px) 8px 0;
		border-bottom: 1px solid var(--border);
		background: var(--bg-side);
	}
	.mobile-nav-toggle {
		display: inline-flex;
		width: 34px;
		height: 34px;
		align-items: center;
		justify-content: center;
		border-radius: 8px;
		background: var(--bg-side);
		border: 1px solid var(--border);
		color: var(--text);
		font-size: 18px;
	}
	.mobile-nav-backdrop {
		display: block;
		position: fixed;
		inset: 0;
		background: rgba(0,0,0,0.45);
		opacity: 0;
		pointer-events: none;
		transition: opacity 0.18s;
		z-index: 18;
	}
	.mobile-nav-backdrop.open {
		opacity: 1;
		pointer-events: auto;
	}
	.col-nav {
		position: fixed;
		left: 0;
		top: 0;
		bottom: 24px;
		width: min(88vw, 360px);
		max-width: 100%;
		flex-direction: column;
		background: var(--bg-side);
		transform: translateX(-100%);
		transition: transform 0.18s ease;
		z-index: 20;
		box-shadow: 10px 0 30px rgba(0,0,0,0.25);
	}
	.col-nav.open { transform: translateX(0); }
	.col-editor { height: 100%; min-height: 0; }
	.col-editor { height: calc(100% - 42px - env(safe-area-inset-top, 0px)); }
	.nav-panel-header { padding: 10px; gap: 5px; }
	.nav-items { padding: 8px; }
	.nav-folder-row { padding: 8px; }
	.nav-folder-notes .notelist-item { padding: 8px 10px 8px 32px; }
	.editor-titlebar {
		padding: 6px 8px;
		gap: 6px;
		flex-wrap: wrap;
	}
	.editor-title {
		order: -1;
		flex: 1 0 100%;
		font-size: 16px;
		line-height: 1.2;
		padding: 0;
		min-height: 1.2em;
	}
	.editor-folder-select {
		max-width: calc(100% - 120px);
		font-size: 11px;
		padding: 2px 5px;
	}
	.editor-folder-arrow { margin: 0; font-size: 12px; }
	#autosave-status,
	#autosave-indicator { margin-left: auto; font-size: 10px; }
	.btn,
	.btn-sm { padding: 4px 8px; font-size: 11px; }
	.btn-icon { width: 26px; height: 26px; font-size: 13px; }
	.editor-toolbar {
		flex-wrap: nowrap;
		overflow-x: auto;
		overflow-y: hidden;
		padding: 3px 6px;
		gap: 2px;
		-webkit-overflow-scrolling: touch;
	}
	.editor-toolbar::-webkit-scrollbar { height: 3px; }
	.editor-toolbar .tb { min-width: 24px; height: 24px; padding: 0 4px; font-size: 12px; flex: 0 0 auto; }
	.editor-toolbar .tb-div { margin: 0 2px; height: 14px; flex: 0 0 auto; }
	.editor-body,
	.editor-preview {
		padding: 10px 12px;
		font-size: 14px;
		line-height: 1.55;
	}
	.editor-preview h1 { font-size: 1.45em; }
	.editor-preview h2 { font-size: 1.28em; }
	.editor-preview h3 { font-size: 1.15em; }
	.app-statusbar {
		height: 24px;
		gap: 6px;
		padding: 0 8px;
		font-size: 10px;
	}
	.status-user {
		max-width: 40vw;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	.theme-picker { padding: 1px 3px; font-size: 10px; }
}
