/* ─────────────────────────── Tokens ─────────────────────────── */
:root {
  --bg:            #0d0f13;
  --bg-elev:       #14171d;
  --bg-elev-2:     #1a1e26;
  --line:          #262b35;
  --line-soft:     #1e222b;
  --text:          #e7e9ee;
  --text-dim:      #9aa3b2;
  --text-faint:    #6b7383;
  --accent:        #6ea8fe;
  --accent-soft:   rgba(110, 168, 254, 0.14);
  --danger:        #ef6d6d;
  --danger-soft:   rgba(239, 109, 109, 0.13);
  --radius:        14px;
  --radius-sm:     10px;
  --shadow:        0 12px 32px rgba(0, 0, 0, 0.45);
  --sidebar-w:     252px;
}

[data-theme="light"] {
  --bg:            #f6f7f9;
  --bg-elev:       #ffffff;
  --bg-elev-2:     #ffffff;
  --line:          #e2e5ea;
  --line-soft:     #edeff3;
  --text:          #191d24;
  --text-dim:      #5b6472;
  --text-faint:    #8b94a3;
  --accent:        #2f6fe0;
  --accent-soft:   rgba(47, 111, 224, 0.09);
  --danger:        #cf3f3f;
  --danger-soft:   rgba(207, 63, 63, 0.09);
  --shadow:        0 12px 32px rgba(20, 26, 40, 0.14);
}

* { box-sizing: border-box; }

/* Our display rules would otherwise beat the hidden attribute. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, -apple-system, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ─────────────────────────── Shell ─────────────────────────── */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100%;
}

/* ─────────────────────────── Sidebar ─────────────────────────── */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 18px 14px 14px;
  background: var(--bg-elev);
  border-right: 1px solid var(--line-soft);
  overflow-y: auto;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 6px 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.brand-mark { color: var(--accent); font-size: 13px; }
.brand-name { font-size: 16px; }

.lists {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-height: 0;
}

.list-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  text-align: left;
  transition: background 0.13s ease, color 0.13s ease;
}

.list-item:hover { background: var(--line-soft); color: var(--text); }

.list-item.active {
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 500;
}

.list-item.active .list-dot { background: var(--accent); }

.list-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
  flex: none;
  transition: background 0.13s ease, opacity 0.13s ease;
}

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

.list-count {
  font-size: 12px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.list-del {
  display: none;
  border: 0;
  background: transparent;
  color: var(--text-faint);
  padding: 0 2px;
  font-size: 13px;
  line-height: 1;
  border-radius: 5px;
}

.list-item:hover .list-del,
.list-item:focus-within .list-del { display: block; }
.list-del:hover { color: var(--danger); }

/* The sidebar grip sits where the status dot is, swapping in on hover. */
.list-item .drag-handle {
  position: absolute;
  left: 6px;
  font-size: 11px;
}

.list-item:hover .drag-handle,
.list-item:focus-within .drag-handle { opacity: 1; }
.list-item:hover .list-dot { opacity: 0; }

.list-item.dragging {
  opacity: 0.5;
  background: var(--line-soft);
}

.add-list-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-top: 8px;
  padding: 9px 10px;
  border: 1px dashed var(--line);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  transition: border-color 0.13s ease, color 0.13s ease;
}

.add-list-btn:hover { border-color: var(--accent); color: var(--text); }

.sidebar-foot {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line-soft);
}

.sync-btn {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sync-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex: none;
  background: var(--text-faint);
}

.sync-dot[data-status="synced"]  { background: #4ac08a; }
.sync-dot[data-status="syncing"] { background: var(--accent); }
.sync-dot[data-status="offline"] { background: #d8a657; }
.sync-dot[data-status="error"]   { background: var(--danger); }

.sync-note {
  margin: 0 0 12px;
  color: var(--text-dim);
  font-size: 13px;
}

.danger-text { color: var(--danger); margin-right: auto; }
.danger-text:hover { color: var(--danger); border-color: var(--danger); }

.foot-row { display: flex; gap: 6px; }
.foot-row .ghost-btn { flex: 1; }

/* ─────────────────────────── Buttons ─────────────────────────── */
.primary-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 9px 15px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #08101f;
  font-weight: 600;
  white-space: nowrap;
  transition: filter 0.13s ease, transform 0.13s ease;
}

[data-theme="light"] .primary-btn { color: #fff; }
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:active { transform: translateY(1px); }

.ghost-btn {
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  font-size: 13px;
  transition: background 0.13s ease, color 0.13s ease, border-color 0.13s ease;
}

.ghost-btn:hover { background: var(--line-soft); color: var(--text); border-color: var(--line); }

.danger-btn {
  padding: 9px 15px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--danger);
  color: #fff;
  font-weight: 600;
}

.danger-btn:hover { filter: brightness(1.08); }

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
}

.icon-btn:hover { color: var(--text); background: var(--line-soft); }
.menu-btn, .sidebar-close { display: none; }
.plus { font-size: 16px; line-height: 1; }

/* ─────────────────────────── Main ─────────────────────────── */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px 28px;
  border-bottom: 1px solid var(--line-soft);
  background: var(--bg);
}

.title-block { flex: 1; min-width: 0; }

.title-block h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 600;
  letter-spacing: -0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.count {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--text-faint);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.view-toggle {
  display: flex;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
}

.view-btn {
  display: grid;
  place-items: center;
  width: 30px;
  height: 28px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-faint);
  transition: background 0.13s ease, color 0.13s ease;
}

.view-btn svg { width: 14px; height: 14px; fill: currentColor; }
.view-btn:hover { color: var(--text); }

.view-btn[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 11px;
  width: 15px;
  height: 15px;
  color: var(--text-faint);
  pointer-events: none;
}

#search {
  width: 240px;
  padding: 9px 12px 9px 33px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  transition: border-color 0.13s ease, width 0.16s ease;
}

#search::placeholder { color: var(--text-faint); }
#search:focus { border-color: var(--accent); outline: none; }
#search::-webkit-search-cancel-button { filter: grayscale(1) opacity(0.5); }

.content {
  flex: 1;
  overflow-y: auto;
  padding: 24px 28px 48px;
}

/* ─────────────────────────── Cards ─────────────────────────── */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(268px, 1fr));
  gap: 12px;
}

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 15px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--bg-elev);
  transition: border-color 0.14s ease, transform 0.14s ease, box-shadow 0.14s ease;
}

.card:hover {
  border-color: var(--line);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

/* ── Drag grip ── */
.drag-handle {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  flex: none;
  border-radius: 5px;
  color: var(--text-faint);
  font-size: 12px;
  line-height: 1;
  cursor: grab;
  opacity: 0;
  transition: opacity 0.13s ease, color 0.13s ease, background 0.13s ease;
  /* Take the gesture on the grip only, so the page still scrolls normally. */
  touch-action: none;
  user-select: none;
}

.drag-handle:hover { color: var(--text); background: var(--line-soft); }
.drag-handle:focus-visible { opacity: 1; }
.drag-handle:active { cursor: grabbing; }

/* In card view the grip tucks into the corner and appears on hover. */
.card .drag-handle {
  position: absolute;
  top: 8px;
  right: 9px;
  z-index: 1;
}

.card:hover .drag-handle,
.card:focus-within .drag-handle { opacity: 1; }

.card.dragging {
  opacity: 0.45;
  cursor: grabbing;
}

/* While a drag is in progress, don't let hover effects chase the pointer,
   and don't select text as it sweeps across rows. */
.reordering { user-select: none; }

.reordering .card:hover {
  transform: none;
  box-shadow: none;
}

.card-top {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.favicon {
  width: 20px;
  height: 20px;
  margin-top: 1px;
  border-radius: 5px;
  flex: none;
  object-fit: contain;
}

/* Shown when a site's /favicon.ico can't be fetched. */
.favicon-fallback {
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  text-transform: uppercase;
}

.card-link {
  flex: 1;
  min-width: 0;
  color: var(--text);
  font-weight: 550;
  text-decoration: none;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-link:hover { color: var(--accent); }

.card-host {
  font-size: 12.5px;
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-note {
  font-size: 13px;
  color: var(--text-dim);
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: auto;
  padding-top: 6px;
}

.card-date { font-size: 12px; color: var(--text-faint); }

.card-tools {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.13s ease;
}

.card:hover .card-tools,
.card:focus-within .card-tools { opacity: 1; }

.tool {
  padding: 4px 8px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--text-faint);
  font-size: 12px;
}

.tool:hover { background: var(--line-soft); color: var(--text); }
.tool.del:hover { background: var(--danger-soft); color: var(--danger); }

/* ── List view ──
   Same card markup, laid out as rows. Each child gets an explicit column so a
   card with no note lines up with one that has one. */
.grid[data-view="list"] {
  grid-template-columns: 1fr;
  gap: 0;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  overflow: hidden;
}

.grid[data-view="list"] .card {
  display: grid;
  grid-template-columns: auto minmax(0, 1.4fr) minmax(0, 0.9fr) minmax(0, 1.3fr) auto;
  align-items: center;
  gap: 16px;
  padding: 11px 15px;
  border: 0;
  border-bottom: 1px solid var(--line-soft);
  border-radius: 0;
  background: transparent;
}

.grid[data-view="list"] .card:last-child { border-bottom: 0; }

.grid[data-view="list"] .card:hover {
  transform: none;
  box-shadow: none;
  background: var(--bg-elev);
}

/* The grip becomes a real column at the start of the row. */
.grid[data-view="list"] .card .drag-handle {
  position: static;
  grid-column: 1;
  opacity: 0.35;
}

.grid[data-view="list"] .card:hover .drag-handle { opacity: 1; }

.grid[data-view="list"] .card-top  { grid-column: 2; }
.grid[data-view="list"] .card-host { grid-column: 3; }
.grid[data-view="list"] .card-note { grid-column: 4; }
.grid[data-view="list"] .card-foot { grid-column: 5; }

/* Everything stays on one line in a row. */
.grid[data-view="list"] .card-link,
.grid[data-view="list"] .card-note {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.grid[data-view="list"] .card-top { align-items: center; gap: 9px; }
.grid[data-view="list"] .favicon { margin-top: 0; }
.grid[data-view="list"] .card-note { margin: 0; }

.grid[data-view="list"] .card-foot {
  margin-top: 0;
  padding-top: 0;
  gap: 14px;
  justify-content: flex-end;
}

.grid[data-view="list"] .card-date { white-space: nowrap; }

/* Drop the least important columns before the row gets cramped. */
@media (max-width: 1100px) {
  .grid[data-view="list"] .card {
    grid-template-columns: auto minmax(0, 1.4fr) minmax(0, 1fr) auto;
  }
  .grid[data-view="list"] .card-note { display: none; }
  .grid[data-view="list"] .card-foot { grid-column: 4; }
}

@media (max-width: 780px) {
  .grid[data-view="list"] .card {
    grid-template-columns: auto minmax(0, 1fr) auto;
  }
  .grid[data-view="list"] .card-host { display: none; }
  .grid[data-view="list"] .card-foot { grid-column: 3; }
  .grid[data-view="list"] .card-date { display: none; }
  /* Touch has no hover, so the grip stays visible. */
  .grid[data-view="list"] .card .drag-handle { opacity: 0.6; }
}

/* ─────────────────────────── Empty state ─────────────────────────── */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 72px 20px;
  text-align: center;
}

.empty-mark { font-size: 34px; opacity: 0.55; }
.empty h2 { margin: 6px 0 0; font-size: 17px; font-weight: 600; }
.empty p { margin: 0 0 14px; color: var(--text-faint); font-size: 14px; }

/* ─────────────────────────── Modals ─────────────────────────── */
.modal-layer {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(4, 6, 10, 0.6);
  backdrop-filter: blur(3px);
  z-index: 40;
  animation: fade 0.14s ease;
}

.modal {
  width: 100%;
  max-width: 440px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-elev);
  box-shadow: var(--shadow);
  animation: rise 0.16s ease;
}

.modal-sm { max-width: 380px; }
.modal-lg { max-width: 500px; }
.modal h2 { margin: 0 0 16px; font-size: 17px; font-weight: 600; }

.modal label {
  display: block;
  margin-bottom: 13px;
}

.modal label > span {
  display: block;
  margin-bottom: 6px;
  font-size: 12.5px;
  color: var(--text-dim);
}

.modal label em {
  font-style: normal;
  color: var(--text-faint);
}

.modal input,
.modal textarea,
.modal select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  transition: border-color 0.13s ease;
  resize: vertical;
}

.modal input:focus,
.modal textarea:focus,
.modal select:focus { border-color: var(--accent); outline: none; }
.modal input::placeholder, .modal textarea::placeholder { color: var(--text-faint); }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

/* ── Import dialog ── */
.import-summary {
  margin: -6px 0 16px;
  color: var(--text-dim);
  font-size: 13.5px;
}

.import-mode {
  margin: 0 0 14px;
  padding: 0;
  border: 0;
}

.import-mode legend {
  padding: 0 0 7px;
  font-size: 12.5px;
  color: var(--text-dim);
}

/* Scoped to .modal so these beat the generic `.modal label` block rule. */
.modal .check,
.modal .radio {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 0;
  padding: 5px 0;
  font-size: 14px;
  cursor: pointer;
}

.modal .check > span,
.modal .radio > span {
  display: inline;
  margin: 0;
  font-size: 14px;
  color: var(--text);
}

.modal .check input,
.modal .radio input {
  width: 15px;
  height: 15px;
  flex: none;
  accent-color: var(--accent);
  margin: 0;
}

.import-folders {
  max-height: 210px;
  margin-bottom: 12px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  overflow-y: auto;
}

.folder-row { padding: 6px 0; }

.folder-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.folder-count {
  font-size: 12px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.confirm-text {
  margin: 0;
  color: var(--text-dim);
  font-size: 14px;
}

.form-error {
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

/* ─────────────────────────── Toast ─────────────────────────── */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-elev-2);
  box-shadow: var(--shadow);
  font-size: 13.5px;
  z-index: 50;
  animation: rise 0.16s ease;
}

.toast button {
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 600;
  font-size: 13px;
  padding: 0;
}

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
  .toast { transform: translateX(-50%); }
}

/* ─────────────────────────── Responsive ─────────────────────────── */
.scrim { display: none; }

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

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar-w);
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.18s ease;
  }

  body.nav-open .sidebar { transform: none; }

  body.nav-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(4, 6, 10, 0.5);
    z-index: 20;
  }

  .menu-btn, .sidebar-close { display: grid; }

  .topbar {
    flex-wrap: wrap;
    padding: 14px 16px;
  }

  .topbar-actions {
    order: 3;
    width: 100%;
  }

  .search-wrap { flex: 1; }
  #search { width: 100%; }
  .content { padding: 16px 16px 40px; }
  .grid { grid-template-columns: 1fr; }
  .card-tools { opacity: 1; }
  .list-del { display: block; }
}
