:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #d7dbe2;
  --text: #222831;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --danger: #b91c1c;
  --ok: #166534;
  --accent-deep: #1c3f78;
  --accent-light: #93c5fd;
  --accent-header: var(--accent);
  --accent-text: #ffffff;
  --accent-link: var(--accent-dark);
  --accent-check: var(--accent-dark);
  --btn-ok: var(--accent);
  --btn-ok-hover: var(--accent-dark);
  --btn-neutral: #dfe3e7;
  --btn-neutral-border: #c6cdd3;
  --btn-neutral-hover: #d3d9df;
}

body.dark {
  --bg: #0f141a;
  --panel: #171e26;
  --border: #303945;
  --text: #e7ecf3;
  --muted: #93a0af;
  --btn-neutral: #464b52;
  --btn-neutral-border: #5b6169;
  --btn-neutral-hover: #525760;
}
body.dark input, body.dark select, body.dark textarea {
  background: var(--panel);
  color: var(--text);
  border-color: var(--border);
}
body.dark input[type="checkbox"] {
  appearance: none;
  width: 16px;
  height: 16px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  vertical-align: middle;
}
body.dark input[type="checkbox"]:checked {
  background: var(--accent-check) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23fff'%3E%3Cpath d='M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z'/%3E%3C/svg%3E") center / 12px 12px no-repeat;
  border-color: var(--accent-check);
}
a { color: var(--accent-link); }
body.dark nav a { color: var(--accent-light); }
body.dark .sidebar a, body.dark .sidebar-list a { color: var(--accent-light); }
body.dark a { color: var(--accent-light); }
body.dark table.bookmarks a { color: var(--accent-light); }
body.dark .tree .active a, body.dark .tree a:hover { color: var(--accent-light); }
body.dark .badge.public { background: #1c3a2b; color: #86efac; }
body.dark .badge.private { background: #232b35; color: var(--muted); }
body.dark .flash { background: #1e2836; }
body.dark dialog { color: var(--text); }

/* Header buttons share one rule so their height cannot drift apart:
   explicit font-family + line-height remove the browser's UA defaults.
   Scoping to .header-actions keeps them above the generic "body.dark a"
   link rule, which would otherwise recolour the Login text. */
.header-actions .dark-toggle,
.header-actions .logout-link,
.header-actions .login-btn {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 6px;
  padding: 6px 12px;
  font-family: inherit;
  font-size: 0.85rem;
  line-height: 1.2;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
}
.header-actions .dark-toggle:hover,
.header-actions .logout-link:hover,
.header-actions .login-btn:hover { background: rgba(255, 255, 255, 0.25); }
.header-actions a { text-decoration: none; }
.logout-form { margin: 0; }

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--ui-font, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif);
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

header {
  padding: 14px 20px;
  background: linear-gradient(130deg, var(--accent-deep), var(--accent-header));
  color: #fff;
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 12px;
}

header h1 { margin: 0; font-size: 1.25rem; }
header .user { color: rgba(255, 255, 255, 0.85); font-size: 0.9rem; }
.header-inner .stats { color: rgba(255, 255, 255, 0.85); font-size: 0.85rem; margin-left: auto; }
.header-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

nav {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

nav a {
  color: var(--accent-link);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  font-size: 0.9rem;
}
.nav-settings { margin-left: auto; }

nav a:hover { text-decoration: underline; }

main {
  padding: 20px;
}

.flash {
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #eef2ff;
  margin-bottom: 14px;
}

.breadcrumb {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 10px;
}

.columns {
  display: grid;
  grid-template-columns: minmax(260px, max-content) 1fr;
  gap: 20px;
  align-items: stretch;
}

.tree-col, .bookmark-col {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px;
}

/* folder pane grows to fit the longest name, but never past 480px;
   names longer than that get ellipsized instead of wrapping */
.tree-col { max-width: 480px; }

/* Panes are viewport-height boxes: only the long parts (tree, bookmark
   list) scroll inside them, so the header, search and toolbars stay put
   instead of scrolling the whole page. */
.tree-col, .bookmark-col {
  display: flex;
  flex-direction: column;
  max-height: max(340px, calc(100vh - 170px));
  min-height: 0;
}
.tree-col .tree { flex: 1; min-height: 0; overflow-y: auto; }
.bookmark-col form { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.bm-scroll { flex: 1; min-height: 0; overflow-y: auto; }

@media (max-width: 720px) {
  .columns { grid-template-columns: 1fr; }
}

.tree {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
}

.tree ul { list-style: none; padding-left: 14px; margin: 0; }
.tree a {
  color: var(--text);
  text-decoration: none;
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree a:hover { color: var(--accent-link); }
.tree .active a { font-weight: 600; color: var(--accent-link); }
.pane-title {
  margin: 0 0 12px;
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.tree details summary { cursor: pointer; list-style: none; }
/* every folder row gets an arrow, expandable ones flip when opened */
.tree summary::before, .tree .tree-leaf a::before {
  content: "▸";
  color: var(--muted);
  margin-right: 6px;
  font-size: 0.85rem;
}
.tree details[open] > summary::before { content: "▾"; }

/* Tree connector lines: vertical trunk + horizontal tick per item */
.tree li, .sidebar li {
  position: relative;
  padding-left: 20px;
}
.tree > li, .sidebar > li { padding-left: 0; }
.tree > li::before, .tree > li::after,
.sidebar > li::before, .sidebar > li::after { display: none; }

.tree li::before, .sidebar li::before {
  content: "";
  position: absolute;
  left: 6px;
  top: 0;
  height: 100%;
  border-left: 1px solid var(--border);
}
.tree li:last-child::before, .sidebar li:last-child::before { height: 12px; }

.tree li::after, .sidebar li::after {
  content: "";
  position: absolute;
  left: 6px;
  top: 11px;
  width: 9px;
  border-top: 1px solid var(--border);
}

.tree .tree-bm a { color: var(--muted); font-size: 0.85rem; }
.tree .tree-bm a:hover { color: var(--accent-link); }

.search { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.search input[type="text"] { flex: 1 1 100px; min-width: 80px; }
.search button { flex: 0 0 auto; }

.tree-actions {
  display: flex;
  gap: 8px;
  margin: 10px 0;
}
.btnlink {
  background: var(--btn-neutral);
  color: var(--text);
  border: 1px solid var(--btn-neutral-border);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 0.85rem;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}
.btnlink:hover { background: var(--btn-neutral-hover); }

.folder-tools {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.folder-form, .move-form {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin: 8px 0;
}
.folder-form input[type="text"] { flex: 1; min-width: 120px; }
.move-form select { flex: 1; min-width: 120px; max-width: 100%; }

.list-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}
.list-toolbar .breadcrumb { margin: 0; }

table.bookmarks {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
}

table.bookmarks th, table.bookmarks td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

table.bookmarks a { color: var(--accent-link); }
table.bookmarks td.check, table.bookmarks td.icon { width: 36px; }
table.bookmarks td.icon { text-align: center; }
table.bookmarks td.date { width: 110px; }
table.bookmarks td.badge-cell { width: 80px; }
table.bookmarks td.edit { width: 56px; }
.desc { color: var(--muted); font-size: 0.85rem; margin-top: 3px; }
.date { color: var(--muted); font-size: 0.85rem; white-space: nowrap; }
.foldercol { color: var(--muted); font-size: 0.85rem; }

.badge {
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}
.badge.public { background: #dcfce7; color: var(--ok); }
.badge.private { background: #f3f4f6; color: var(--muted); }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: center;
  margin: 14px 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
}
.ctrl-group {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  padding: 6px 8px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.04);
}
body.dark .ctrl-group { background: rgba(255, 255, 255, 0.06); }

.skip-check { width: 16px; height: 16px; cursor: pointer; }
table.bookmarks td.skip { width: 30px; }

.bookmark-form, .panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px;
  margin: 14px 0;
}

.bookmark-form label { display: block; margin-bottom: 8px; }
.form-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.form-actions .form-cancel { margin-left: auto; }
#edit-bookmark-dialog { width: min(1200px, 95vw); max-width: none; }
#edit-folder-dialog { width: min(420px, 92vw); }
#edit-folder-form label { display: block; margin-bottom: 8px; }
#edit-folder-form input[type="text"] {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.bookmark-form input[type="text"], .bookmark-form textarea {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
}

button {
  background: var(--btn-ok);
  color: var(--accent-text);
  border: 1px solid rgba(0, 0, 0, 0.18);
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
}

.icon {
  width: 1em;
  height: 1em;
  fill: currentColor;
  flex: 0 0 auto;
  margin-right: 6px;
}

/* Placeholder for bookmarks without a favicon: outlined globe, dimmed. */
.no-favicon { opacity: 0.45; }
.no-favicon .icon {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  margin-right: 0;
}

button:hover { background: var(--btn-ok-hover); }
button.danger { background: var(--danger); }
button.danger:hover { background: #8f1717; }

input, select {
  padding: 7px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
}

.empty { color: var(--muted); }
.hint { color: var(--muted); font-size: 0.85rem; }
.login .field { display: grid; grid-template-columns: 110px 1fr; gap: 10px; align-items: center; }
.login .login-submit { display: flex; justify-content: flex-end; }
.login-warning {
  background: #fef2f2;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 8px;
  padding: 12px 16px;
  margin: 12px 0 0;
  font-size: 0.95rem;
}
.login-warning .icon { vertical-align: middle; }
body.dark .login-warning { background: rgba(185, 28, 28, 0.18); }

.login {
  max-width: 380px;
  margin: 40px auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
}

.sidebar-list { list-style: none; margin: 0; padding: 0; }
.sidebar-list a { color: var(--accent-link); text-decoration: none; }

.sidebar-page {
  margin: 18px;
  padding: 0 14px 0 62px;
}
.sidebar-tools {
  position: fixed;
  left: 12px;
  top: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
  z-index: 60;
}
.sidebar-toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 0 0 14px;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
}
#sidebar-filter {
  flex: 1;
  min-width: 140px;
  padding: 8px 10px;
  border-radius: 8px;
}
.icon-only {
  width: 40px;
  height: 40px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--btn-neutral);
  border: 1px solid var(--btn-neutral-border);
  border-radius: 8px;
  color: var(--text);
}
.icon-only:hover { background: var(--btn-neutral-hover); }
body.dark .icon-only { color: var(--text); }
.icon-only .icon { margin-right: 0; }
.hidden-item { display: none; }
.sidebar-empty { color: var(--muted); font-size: 0.9rem; margin: 8px 0; }

.sidebar { list-style: none; margin: 0; padding: 0; }
.sidebar ul { list-style: none; margin: 8px 0; padding-left: 14px; }
.sidebar a {
  color: var(--accent-link);
  text-decoration: none;
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar summary { cursor: pointer; font-weight: 600; }
.sidebar .folder-name { font-weight: 600; }
.sidebar-bm img, .sidebar-bm .fav-fetch { margin-right: 6px; vertical-align: middle; }

.settings-page {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.settings-section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
}
.settings-section h2 {
  margin: 0 0 10px;
  font-size: 1.02rem;
  display: inline-flex;
  align-items: center;
}
.settings-section h2:not(:first-child) {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.settings-section label { display: block; margin: 9px 0; }
.settings-section button { margin-top: 12px; }
.settings-section h3 { margin: 16px 0 8px; font-size: 1rem; }

.fav-fetch {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 0.8rem;
}
.fav-fetch:hover { color: var(--accent-link); border-color: var(--accent-link); }
.fav-fetch:disabled { opacity: 0.35; cursor: default; }

/* favicon fetch progress popup */
.fav-progress {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: 320px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
  z-index: 1000;
}
.fav-progress-current {
  font-size: 0.8rem;
  margin-bottom: 8px;
}
.fav-current-folder { color: var(--muted); }
.fav-current-name {
  color: var(--accent-link);
  font-weight: 600;
  max-width: 230px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
  vertical-align: bottom;
}
.fav-progress-label {
  font-size: 0.72rem;
  color: var(--muted);
  margin: 6px 0 3px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.fav-progress-track {
  height: 8px;
  border-radius: 4px;
  background: var(--border);
}
.fav-progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 4px;
  background: var(--accent-link);
  transition: width 0.3s ease;
}
.fav-fill-overall { background: var(--accent-header); }
.fav-cancel {
  margin-top: 10px;
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  cursor: pointer;
}
.fav-cancel:hover { background: rgba(185, 28, 28, 0.12); }

/* unreachable-site marker and results dialog */
.dead-link {
  color: var(--danger);
  display: inline-flex;
  align-items: center;
  margin-right: 6px;
}
.dead-link .icon { width: 14px; height: 14px; }

#link-results {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  max-width: 620px;
  padding: 16px;
}
#link-results h2 { margin: 0 0 6px; font-size: 1.05rem; }
.link-results-note { color: var(--muted); font-size: 0.9rem; margin: 0 0 10px; }
#link-results-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#link-results-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
}
.link-result-main { flex: 1; min-width: 0; }
.link-result-main strong { display: block; }
.link-result-url {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.link-result-reason { font-size: 0.8rem; color: var(--danger); white-space: nowrap; }
.link-results-actions { display: flex; gap: 8px; margin-top: 12px; }
body.dark .fav-fetch:hover { color: var(--accent-light); border-color: var(--accent-light); }

dialog {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  max-width: 440px;
  padding: 16px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin-top: 0; }
dialog .bookmark-form { margin: 10px 0; }

button.ghost, a.ghost {
  background: var(--btn-neutral);
  color: var(--text);
  border: 1px solid var(--btn-neutral-border);
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}
button.ghost:hover, a.ghost:hover { background: var(--btn-neutral-hover); }
a.ghost.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
a.ghost.danger:hover { background: #8f1717; }
.folder-tools a.ghost { margin: 8px 10px 0 0; }
