:root {
  --bg: #f6f4ee;
  --panel: #ffffff;
  --ink: #2b2722;
  --muted: #8a8578;
  --line: #e7e3d8;
  --accent: #7a5c43;        /* warm brown */
  --accent-soft: #efe7dc;   /* light brown wash */
  --accent-ink: #5d4632;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(43, 39, 34, 0.05), 0 4px 14px rgba(43, 39, 34, 0.04);
  --shadow-lift: 0 4px 10px rgba(43, 39, 34, 0.10), 0 14px 40px rgba(43, 39, 34, 0.12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  display: flex;
  height: 100vh;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

/* ---- Sidebar ---- */
.sidebar {
  width: 240px;
  flex: 0 0 240px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 20px 16px;
  height: 100vh;
  overflow-y: auto;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 8px; margin-bottom: 22px; }
.brand-mark { letter-spacing: -2px; font-size: 18px; color: var(--accent); }
.brand-name { font-weight: 800; letter-spacing: 1px; font-size: 16px; }

.add-btn {
  display: block;
  width: 100%;
  text-align: center;
  padding: 10px 12px;
  margin-bottom: 8px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.add-btn.secondary { background: var(--accent-soft); color: var(--accent-ink); }
.add-btn:hover { filter: brightness(0.97); }

.cat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 24px 4px 8px;
  font-size: 11px;
  letter-spacing: 1px;
  color: var(--muted);
  font-weight: 700;
}
.cat-header button {
  border: none; background: none; color: var(--muted);
  font-size: 16px; cursor: pointer; line-height: 1;
}

.category-list { display: flex; flex-direction: column; gap: 2px; }
.cat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 7px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: #46413a;
  user-select: none;
  -webkit-user-select: none;
}
/* but the inline rename field must still be selectable/editable */
.cat-edit { user-select: text; -webkit-user-select: text; }
.cat-item:hover { background: #f1eee6; }
.cat-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.cat-item .badge { font-size: 12px; color: var(--muted); }
.cat-item.active .badge { color: var(--accent-ink); }
/* Right-click context menu */
.context-menu {
  position: fixed;
  z-index: 200;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow-lift);
  padding: 5px;
  min-width: 160px;
}
.context-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  font-size: 13px;
  color: var(--ink);
  padding: 8px 10px;
  border-radius: 7px;
  cursor: pointer;
}
.context-item:hover { background: var(--accent-soft); }
.context-item.danger { color: #c0392b; }
.context-item.danger:hover { background: rgba(192,57,43,0.1); }

.cat-edit {
  width: 100%;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 14px;
  font-family: inherit;
  background: #fff;
  color: var(--ink);
  outline: none;
}
.cat-item.draggable { cursor: grab; }
.cat-item.drag-src { opacity: 0.4; }
.cat-item .cat-name { pointer-events: none; }

.hint { margin-top: 22px; font-size: 12px; color: var(--muted); line-height: 1.55; }

/* ---- Main / topbar ---- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,0.7);
  backdrop-filter: blur(6px);
  z-index: 5;
}
.search {
  flex: 1;
  max-width: 460px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 14px;
  background: #fff;
}
.search:focus { outline: none; border-color: var(--accent); }
.toolbar-btn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--accent-ink);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.toolbar-btn:hover { background: var(--accent-soft); }
.count { color: var(--muted); font-size: 13px; margin-left: auto; }

/* ---- Density control ---- */
.density {
  display: flex;
  gap: 2px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 2px;
}
.density-btn {
  border: none; background: none;
  border-radius: 7px;
  padding: 5px 9px;
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
  line-height: 1;
}
.density-btn.active { background: var(--accent-soft); color: var(--accent-ink); }
.density-btn:hover { color: var(--accent-ink); }

/* ---- Masonry grid ---- */
.viewport {
  position: relative;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--bg);
  padding: 22px;
}
.grid {
  position: relative;
  width: 100%;
  margin: 0 auto;
}

/* ---- Groups (labeled areas) ---- */
.group {
  position: absolute;
  z-index: 0;
  background: rgba(122, 92, 67, 0.05);
  border: 1.5px solid rgba(122, 92, 67, 0.25);
  border-radius: 14px;
  cursor: grab;
}
.group.dragging { cursor: grabbing; border-color: rgba(122, 92, 67, 0.5); }
.group-label {
  position: absolute;
  top: -26px; left: 6px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--accent-ink);
  background: var(--bg);
  padding: 3px 8px;
  border-radius: 6px;
  white-space: nowrap;
  cursor: text;
}
.group-del {
  position: absolute;
  top: -26px; right: 6px;
  border: none;
  background: var(--bg);
  color: var(--muted);
  border-radius: 6px;
  font-size: 12px;
  padding: 3px 7px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .12s;
}
.group:hover .group-del { opacity: 1; }
.group-del:hover { color: var(--accent-ink); }
.group-resize {
  position: absolute;
  right: -2px; bottom: -2px;
  width: 18px; height: 18px;
  cursor: nwse-resize;
  border-right: 3px solid rgba(122, 92, 67, 0.45);
  border-bottom: 3px solid rgba(122, 92, 67, 0.45);
  border-bottom-right-radius: 12px;
}

/* ---- Cards ---- */
.card {
  position: absolute;
  top: 0; left: 0;
  z-index: 1;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  transition: transform .28s cubic-bezier(.2, .8, .2, 1), box-shadow .15s;
  will-change: transform;
}
.card.dragging {
  cursor: grabbing;
  box-shadow: var(--shadow-lift);
  z-index: 1000;
  transition: box-shadow .15s;
  opacity: 0.96;
}

/* Reserve height for image cards until their images load (prevents the
   masonry from briefly overlapping during load). */
.card.loading-media { min-height: 260px; }

/* Media area (image / video thumbnail) inside a card. */
.media { position: relative; line-height: 0; }
.media img, .media video {
  width: 100%; display: block;
  border-radius: var(--radius) var(--radius) 0 0;
  pointer-events: none;
}
.card .media { cursor: zoom-in; }
.media-noposter {
  height: 200px;
  background: linear-gradient(135deg, #2b2722, #46413a);
  border-radius: var(--radius) var(--radius) 0 0;
}
.play-badge {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 58px; height: 58px;
  border-radius: 50%;
  background: rgba(20, 16, 12, 0.55);
  backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
  transition: transform .15s, background .15s;
}
.play-badge::after {
  content: "";
  margin-left: 4px;
  border-style: solid;
  border-width: 11px 0 11px 18px;
  border-color: transparent transparent transparent #fff;
}
.card:hover .play-badge { transform: translate(-50%, -50%) scale(1.08); background: rgba(20, 16, 12, 0.7); }

/* ---- Lightbox (foreground media overlay) ---- */
.lightbox {
  position: fixed; inset: 0; z-index: 2000;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.85);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
  opacity: 0;
  transition: opacity .25s ease;
}
.lightbox[hidden] { display: none; }
.lightbox.open { opacity: 1; }
.lb-content {
  transform: scale(.92);
  opacity: .6;
  transition: transform .28s cubic-bezier(.2, .8, .2, 1), opacity .2s ease;
  max-width: 92vw; max-height: 92vh;
  display: flex; align-items: center; justify-content: center;
}
.lightbox.open .lb-content { transform: scale(1); opacity: 1; }
.lb-img {
  max-width: 92vw; max-height: 92vh;
  border-radius: 12px; display: block;
  box-shadow: 0 20px 70px rgba(0, 0, 0, 0.5);
}
.lb-embed {
  width: min(92vw, 1280px);
  aspect-ratio: 16 / 9;
  border-radius: 12px; overflow: hidden;
  box-shadow: 0 20px 70px rgba(0, 0, 0, 0.5);
  background: #000;
}
.lb-embed iframe { width: 100%; height: 100%; border: 0; display: block; }
.lb-video-wrap { position: relative; border-radius: 12px; overflow: hidden; box-shadow: 0 20px 70px rgba(0,0,0,.5); }
.lb-video { max-width: 92vw; max-height: 92vh; display: block; background: #000; }
/* Minimalist custom controls — only show on hover. */
.lb-controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 22px 14px 10px;
  background: linear-gradient(to top, rgba(0,0,0,0.55), transparent);
  opacity: 0; transition: opacity .18s;
}
.lb-video-wrap:hover .lb-controls { opacity: 1; }
.lb-play { background: none; border: none; color: #fff; font-size: 16px; cursor: pointer; width: 22px; }
.lb-seek { flex: 1; height: 4px; border-radius: 3px; background: rgba(255,255,255,.3); cursor: pointer; position: relative; }
.lb-seek-fill { position: absolute; left: 0; top: 0; bottom: 0; background: #fff; border-radius: 3px; width: 0; }
.lb-time { color: #fff; font-size: 12px; font-variant-numeric: tabular-nums; opacity: .85; }
.lb-close {
  position: fixed; top: 16px; right: 20px; z-index: 2001;
  width: 38px; height: 38px; border-radius: 50%;
  border: none; cursor: pointer;
  background: rgba(255,255,255,0.12); color: #fff; font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity .25s, background .15s;
}
.lightbox.open .lb-close { opacity: 1; }
.lb-close:hover { background: rgba(255,255,255,0.25); }

/* Spacer block — a deliberate empty gap that flows with the grid. */
.card.spacer {
  background: repeating-linear-gradient(45deg, #f1ece3, #f1ece3 10px, #ece6da 10px, #ece6da 20px);
  border: 1.5px dashed #cfc7b6;
  box-shadow: none;
  display: flex;
  align-items: center;
  justify-content: center;
}
.spacer-label {
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--muted);
  pointer-events: none;
}
.spacer-resize {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 14px;
  cursor: ns-resize;
}
.spacer-resize::after {
  content: "";
  position: absolute;
  left: 50%; bottom: 3px;
  width: 30px; height: 3px;
  transform: translateX(-50%);
  border-radius: 3px;
  background: #cfc7b6;
}

/* Multi-select */
.card.selected {
  outline: 2.5px solid var(--accent);
  outline-offset: 1px;
  box-shadow: 0 0 0 4px var(--accent-soft), var(--shadow);
}
.card.drag-ghost { opacity: 0.5; }

.marquee {
  position: absolute;
  top: 0; left: 0;
  background: rgba(122, 92, 67, 0.12);
  border: 1.5px solid var(--accent);
  border-radius: 4px;
  z-index: 900;
  pointer-events: none;
}

.selection-bar {
  position: absolute;
  bottom: 18px; left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--accent-ink);
  color: #fff;
  padding: 8px 10px 8px 16px;
  border-radius: 12px;
  box-shadow: var(--shadow-lift);
  z-index: 40;
  font-size: 13px;
}
.selection-bar[hidden] { display: none; }
.selection-bar #sel-count { font-weight: 600; margin-right: 4px; }
.sel-btn {
  border: none;
  background: rgba(255,255,255,0.15);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
}
.sel-btn:hover { background: rgba(255,255,255,0.28); }
.sel-btn.danger:hover { background: #c0392b; }
.sel-btn.ghost { background: transparent; }
.sel-btn.ghost:hover { background: rgba(255,255,255,0.15); }

/* Live landing slot shown while dragging. */
.placeholder {
  position: absolute;
  top: 0; left: 0;
  border: 2px dashed var(--accent);
  background: rgba(122, 92, 67, 0.07);
  border-radius: var(--radius);
  z-index: 2;
  pointer-events: none;
  transition: transform .12s ease;
}
.card img {
  width: 100%; display: block;
  border-radius: var(--radius) var(--radius) 0 0;
  pointer-events: none;
}
.card .text-body {
  padding: 14px 16px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.card .card-title { font-weight: 700; padding: 12px 16px 0; font-size: 15px; }

/* Link cards */
.link-hero {
  width: 100%;
  display: block;
  border-radius: var(--radius) var(--radius) 0 0;
  border-bottom: 1px solid var(--line);
  pointer-events: none;
}
.link-body { padding: 12px 14px; }
.link-head { display: flex; align-items: flex-start; gap: 8px; }
.link-fav {
  width: 18px; height: 18px;
  border-radius: 4px;
  margin-top: 1px;
  flex: 0 0 18px;
  pointer-events: none;
}
.link-title {
  display: block;
  font-weight: 700;
  font-size: 14px;
  line-height: 1.35;
  color: var(--accent-ink);
  text-decoration: none;
  cursor: pointer;
}
.link-title:hover { text-decoration: underline; }
.link-desc {
  margin-top: 5px;
  font-size: 12.5px;
  line-height: 1.45;
  color: #6b6657;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.link-domain {
  display: inline-block;
  margin-top: 8px;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  cursor: pointer;
}
.link-domain:hover { color: var(--accent-ink); }

.inline-link { color: var(--accent-ink); text-decoration: underline; cursor: pointer; word-break: break-all; }

#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: var(--accent-ink);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 10px;
  box-shadow: var(--shadow-lift);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 70;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  align-items: center;
  padding: 8px 12px 10px;
  border-top: 1px solid var(--line);
}
.chip {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #f1eee6;
  color: #6b6657;
}
.chip.cat { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.chip.comments-chip { cursor: pointer; }
.chip.comments-chip:hover { background: #e7e3d8; }

.card-actions {
  position: absolute;
  top: 8px; right: 8px;
  display: flex; gap: 6px;
  opacity: 0;
  transition: opacity .12s;
}
.card:hover .card-actions { opacity: 1; }
.icon-btn {
  width: 28px; height: 28px;
  border: none; border-radius: 7px;
  background: rgba(255,255,255,0.92);
  box-shadow: var(--shadow);
  cursor: pointer; font-size: 13px;
  display: flex; align-items: center; justify-content: center;
  color: #57514a;
}
.icon-btn:hover { background: #fff; }

.resize-handle {
  position: absolute;
  right: -2px; bottom: -2px;
  width: 16px; height: 16px;
  cursor: nwse-resize;
  border-right: 3px solid #cfc9b8;
  border-bottom: 3px solid #cfc9b8;
  border-bottom-right-radius: 10px;
  opacity: 0;
  transition: opacity .12s;
}
.card:hover .resize-handle { opacity: 1; }

/* ---- Comments ---- */
.comments-panel {
  border-top: 1px solid var(--line);
  background: #faf8f3;
  border-radius: 0 0 var(--radius) var(--radius);
  padding: 10px;
  cursor: default;
}
.comment {
  position: relative;
  background: #fff;
  border: 1px solid #eeebe2;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.45;
  margin-bottom: 6px;
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text;
}
.comment-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 5px;
}
.comment-date { font-size: 10px; color: var(--muted); }
.comment-del {
  border: none; background: none; color: transparent;
  cursor: pointer; font-size: 11px; padding: 0;
}
.comment:hover .comment-del { color: var(--muted); }
.comment-del:hover { color: var(--accent-ink) !important; }
.comment-form { display: flex; gap: 6px; }
.comment-input {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
  font-size: 13px;
  font-family: inherit;
  resize: none;
  min-height: 34px;
  background: #fff;
}
.comment-input:focus { outline: none; border-color: var(--accent); }
.comment-add {
  border: none; border-radius: 8px;
  background: var(--accent); color: #fff;
  font-size: 13px; font-weight: 600;
  padding: 0 12px; cursor: pointer;
}
.comment-add:hover { filter: brightness(0.95); }

/* ---- Empty ---- */
.empty {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  text-align: center; color: var(--muted);
  pointer-events: none;
}
.empty[hidden] { display: none; }
.empty h2 { color: var(--ink); margin-bottom: 6px; }
.empty p { max-width: 380px; line-height: 1.5; }

/* ---- Drop overlay ---- */
.drop-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(122, 92, 67, 0.10);
  border: 4px dashed var(--accent);
  display: none;
  align-items: center; justify-content: center;
  font-size: 22px; font-weight: 700; color: var(--accent-ink);
  backdrop-filter: blur(2px);
  pointer-events: none;
}
.drop-overlay.show { display: flex; }

/* ---- Modal ---- */
.modal {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(43, 39, 34, 0.35);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-card {
  width: 100%; max-width: 520px;
  background: #fff; border-radius: 16px; padding: 20px;
  box-shadow: 0 20px 60px rgba(43, 39, 34, 0.25);
}
.modal-title, .modal-body, .modal-category, .modal-tags {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; font-size: 14px; font-family: inherit; margin-bottom: 10px;
}
.modal-title { font-weight: 700; font-size: 16px; }
.modal-heading { margin: 0 0 12px; font-size: 17px; }
.modal-body { min-height: 160px; resize: vertical; line-height: 1.5; }
.modal-row { display: flex; gap: 10px; }
.modal-row > * { flex: 1; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }
.btn {
  padding: 9px 16px; border-radius: 9px; border: none;
  font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: #f1eee6; color: #57514a; }
