:root {
  --bg: #0f141a;
  --panel: #121a22;
  --panel-2: #0b1015;
  --text: #e6edf3;
  --muted: #91a4b7;
  --accent: #4da3ff;
  --danger: #ff6363;
  --border: #2a3a4a;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 10;
}
.app-title { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.sidebar-toggle { background: transparent; border: 1px solid var(--border); color: var(--text); padding: 4px 8px; border-radius: 6px; cursor: pointer; }
.sidebar-toggle:hover { background: #1a2430; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; overflow-x: auto; }
.toolbar .divider { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
button, .btn-like {
  background: #1b2733;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
button:hover, .btn-like:hover { background: #223244; }
button.danger { border-color: #5a2a2a; color: #ffbcbc; }
button.danger:hover { background: #2a1a1a; }

.app-main {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: calc(100vh - 46px);
}
@supports (height: 100dvh) {
  .app-main { height: calc(100dvh - 46px); }
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--panel-2);
  display: flex;
  flex-direction: column;
}
.sidebar-header { padding: 8px; border-bottom: 1px solid var(--border); }
.filter-row { display: flex; gap: 6px; align-items: center; }
.sidebar-header input { width: 100%; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--border); background: #0c1218; color: var(--text); }
/* Ensure filter input and clear button stay within row */
.filter-row input { flex: 1; width: auto; min-width: 0; }
.filter-row button { white-space: nowrap; }
.sidebar-actions { display: flex; gap: 6px; margin-top: 8px; }
.sidebar-actions input { flex: 1; }
.batch-bar { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 8px; padding: 6px; background: #0e1620; border: 1px dashed var(--border); border-radius: 8px; width: 100%; }
.batch-bar #batchCount { color: #c9d7e1; }

.tree { padding: 6px; overflow: auto; height: 100%; }
.tree-topic { display: flex; align-items: center; justify-content: space-between; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.tree-topic:hover { background: #15202b; }
.tree-topic.selected { background: #1f2d3a; border: 1px solid var(--border); }
.tree-topic .left { display: flex; align-items: center; gap: 8px; min-width: 0; }
.tree-topic .right { display: flex; align-items: center; gap: 6px; }
.caret { width: 0; height: 0; border-top: 5px solid transparent; border-bottom: 5px solid transparent; border-left: 6px solid #9db8cf; transition: transform 0.12s ease; }
.caret.expanded { transform: rotate(90deg); }
.tree-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: #1b2a39; color: #c8e6ff; border: 1px solid #27445f; border-radius: 999px; font-size: 11px; padding: 1px 6px; }
.mini-action { background: transparent; border: 1px dashed #2a3a4a; color: #c9d7e1; border-radius: 6px; padding: 2px 6px; cursor: pointer; }
.mini-action:hover { background: #142131; }
.tree-subitem { padding: 4px 28px 4px 28px; border-radius: 6px; cursor: pointer; color: #c9d7e1; display: flex; align-items: center; justify-content: space-between; }
.tree-subitem:hover { background: #15202b; }
.tree-subitem.selected { background: #1f2d3a; border: 1px solid var(--border); }
.tree-item.drag-over, .tree-subitem.drag-over { outline: 2px dashed var(--accent); outline-offset: -2px; }

/* Row actions shown on hover */
.row-actions { display: none; gap: 6px; }
.tree-topic:hover .row-actions, .tree-subitem:hover .row-actions { display: inline-flex; }
.icon-btn { background: transparent; border: 1px solid #2a3a4a; color: #c9d7e1; padding: 2px 6px; border-radius: 6px; cursor: pointer; }
.icon-btn:hover { background: #142131; }
.icon-danger { border-color: #5a2a2a; color: #ffbcbc; }

.content { position: relative; overflow: hidden; }
.empty { padding: 24px; color: var(--muted); }

.drop-hint {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  color: #c8e6ff;
  background: rgba(77, 163, 255, 0.09);
  border: 2px dashed var(--accent);
  pointer-events: none;
}
.drop-hint.show { display: flex; }

.image-grid {
  position: relative;
  height: 100%;
  overflow: auto;
  padding: 12px;
  background: transparent;
  background-image: radial-gradient(circle at 1px 1px, rgba(255,255,255,0.06) 1px, transparent 0);
  background-size: 16px 16px;
}
.canvas-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  pointer-events: none;
  text-align: center;
}
.image-item {
  position: absolute;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #0c1218;
  user-select: none;
}
.image-item.selected { box-shadow: 0 0 0 2px var(--accent) inset; }
.image-item.dragging { opacity: 0.7; }
.image-item img.image-thumb {
  width: 100%;
  height: 100%;
  object-fit: contain;
  cursor: move;
}
.resize-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  right: 4px;
  bottom: 4px;
  border-radius: 3px;
  background: rgba(77,163,255,0.9);
  border: 1px solid #0b2a4d;
  cursor: nwse-resize;
}

/* Notes */
.note-item {
  position: absolute;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #0e1620;
  color: var(--text);
  box-shadow: 0 2px 8px rgba(0,0,0,0.35);
  overflow: hidden;
  z-index: 2;
}
.note-item.selected { box-shadow: 0 0 0 2px var(--accent) inset, 0 2px 8px rgba(0,0,0,0.35); }
.note-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px;
  background: #142131;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: #c9d7e1;
  user-select: none;
}
.note-body {
  padding: 8px;
  width: 100%;
  height: calc(100% - 24px);
  overflow: auto;
  white-space: pre-wrap;
}
.note-body[contenteditable="true"] { outline: none; background: #101a27; }
.note-resize { position: absolute; width: 14px; height: 14px; right: 4px; bottom: 4px; border-radius: 3px; background: rgba(150,200,255,0.9); border: 1px solid #0b2a4d; cursor: nwse-resize; }

.modal { position: fixed; inset: 0; display: none; }
.modal[hidden] { display: none; }
.modal:not([hidden]) { display: block; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.6); }
.modal-content { position: absolute; inset: 5% 10%; background: #0b1015; border: 1px solid var(--border); border-radius: 8px; display: flex; flex-direction: column; }
.modal-close { position: absolute; top: 8px; right: 8px; z-index: 2; }
.modal-toolbar { display: flex; gap: 8px; padding: 8px; border-top: 1px solid var(--border); background: #0c1218; }
.modal-content img { flex: 1; object-fit: contain; width: 100%; height: 100%; background: #000; }

/* Progress */
.progress { position: relative; width: 260px; height: 18px; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: #0c1218; }
.progress-bar { position: absolute; left: 0; top: 0; bottom: 0; width: 0%; background: linear-gradient(90deg, #4da3ff, #6ec3ff); }
.progress-text { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 12px; color: #e6f2ff; text-shadow: 0 1px 2px #000; }

/* Collapsed sidebar */
.sidebar-collapsed .sidebar { display: none; }
.sidebar-collapsed .app-main { grid-template-columns: 0 1fr; }

@media (max-width: 1024px) {
  .app-main { grid-template-columns: 240px 1fr; }
  .modal-content { inset: 5% 6%; }
}
/* Button variants */
.btn-primary, .btn-like.btn-primary {
  background: #173354;
  border-color: #2d5c8e;
}
.btn-primary:hover, .btn-like.btn-primary:hover {
  background: #1d406a;
}
.btn-ghost {
  background: transparent;
  border-color: #223244;
}
.btn-ghost:hover {
  background: #162331;
}

@media (max-width: 768px) {
  .app-header { flex-direction: column; align-items: flex-start; gap: 8px; }
  .app-main { grid-template-columns: 180px 1fr; height: calc(100vh - 72px); }
  @supports (height: 100dvh) { .app-main { height: calc(100dvh - 72px); } }
  button, .btn-like { padding: 6px 8px; }
  .modal-content { inset: 4% 4%; }
}

@media (max-width: 480px) {
  .app-main { grid-template-columns: 160px 1fr; }
  .toolbar { gap: 6px; }
  .tree-subitem { padding-left: 22px; }
}

/* Off-canvas sidebar behavior on small screens */
@media (max-width: 768px) {
  .app-main { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 48px;
    bottom: 0;
    left: 0;
    width: min(82vw, 320px);
    max-width: 90vw;
    transform: translateX(-100%);
    transition: transform 0.2s ease-out;
    z-index: 20;
    box-shadow: 4px 0 12px rgba(0,0,0,0.4);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 15;
  }
}

/* Context menu */
.context-menu {
  position: fixed;
  min-width: 180px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
  padding: 6px;
  z-index: 50;
}
.context-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  color: var(--text);
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.context-menu button:hover { background: #162331; }
.context-menu .danger { color: #ffbcbc; }
