:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-2: #f2f2f3;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --text: #18181b;
  --muted: #71717a;
  --accent: #18181b;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --focus: #3b82f6;
  --shadow: 0 1px 2px rgba(0,0,0,.04);
  --shadow-lg: 0 20px 50px -12px rgba(0,0,0,.25);

  --radius: 8px;
  --col: 220px;
  --fs: 13px;
  --item-fs: 15px;
  --item-h: 40px;
  --ico: 20px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --dur: .15s;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0c0c0e; --surface: #17171a; --surface-2: #1f1f23; --border: #27272a; --border-strong: #3f3f46;
    --text: #f4f4f5; --muted: #a1a1aa; --accent: #f4f4f5; --accent-text: #18181b;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0c0c0e; --surface: #17171a; --surface-2: #1f1f23; --border: #27272a; --border-strong: #3f3f46;
  --text: #f4f4f5; --muted: #a1a1aa; --accent: #f4f4f5; --accent-text: #18181b;
  --shadow: none; color-scheme: dark;
}

:root[data-font="rounded"] { --font: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", system-ui, sans-serif; }
:root[data-font="serif"]   { --font: ui-serif, "New York", Georgia, "Times New Roman", serif; }
:root[data-font="mono"]    { --font: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
:root[data-size="s"] { --item-fs: 13px; --item-h: 32px; --ico: 16px; }
:root[data-size="l"] { --item-fs: 17px; --item-h: 48px; --ico: 24px; }
:root[data-size="xl"] { --item-fs: 19px; --item-h: 56px; --ico: 28px; }
:root[data-anim="off"] { --dur: 0s; }

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  color: var(--text);
  font: var(--fs)/1.4 var(--font);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.muted { color: var(--muted); }
.small { font-size: .92em; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }

/* Marke */
.brand { display: inline-flex; flex-direction: column; font-weight: 700; font-size: 20px; letter-spacing: -.03em; line-height: 1; user-select: none; }
.brand-bar { display: block; width: 26px; height: 2px; background: currentColor; margin-bottom: 4px; }
.brand-lg { font-size: 34px; }
.brand-lg .brand-bar { width: 42px; height: 3px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 14px;
  border: 1px solid var(--border-strong); border-radius: var(--radius); background: var(--surface);
  cursor: pointer; white-space: nowrap; transition: background var(--dur);
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-primary:hover { background: var(--accent); opacity: .88; }
.btn-danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: var(--radius);
  border: 1px solid transparent; background: transparent; cursor: pointer; color: var(--muted);
  transition: background var(--dur), color var(--dur);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Login */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow-lg);
}
.auth-card p { margin: 0; }
.auth-card .btn { justify-content: center; height: 40px; }

input[type=text], input[type=password], input[type=url], input[type=search], select {
  width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--border-strong);
  border-radius: var(--radius); background: var(--surface);
}
input[type=color] { width: 100%; height: 36px; padding: 2px; border: 1px solid var(--border-strong); border-radius: var(--radius); background: var(--surface); }
input[type=range] { width: 100%; accent-color: var(--accent); }

/* Kopfzeile */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 18px 28px;
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
body.has-bg .topbar { background: transparent; backdrop-filter: none; }
.topbar .actions { margin-left: auto; display: flex; gap: 4px; }
.search {
  flex: 0 1 340px; display: flex; align-items: center; gap: 8px; height: 34px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--muted);
}
.search svg { width: 15px; height: 15px; flex: none; }
.search input { border: 0; background: transparent; height: 100%; padding: 0; outline: none; color: var(--text); }
.search input::-webkit-search-cancel-button { display: none; }

/* Board */
.board {
  display: grid; grid-template-columns: repeat(auto-fit, var(--col)); justify-content: center;
  gap: 30px 18px; padding: 8px 28px 60px; align-items: start;
}
.group { min-width: 0; display: flex; flex-direction: column; gap: 6px; border-radius: var(--radius); }
.group-head {
  display: flex; align-items: center; gap: 4px; min-height: 26px; padding: 0 2px;
  font-weight: 600; font-size: calc(var(--item-fs) * .92); color: var(--text);
}
body.has-bg .group-head { text-shadow: 0 1px 6px color-mix(in srgb, var(--bg) 70%, transparent); }
.group-head .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-head .icon-btn { width: 24px; height: 24px; }
.group-head .icon-btn svg { width: 14px; height: 14px; }
.items { display: flex; flex-direction: column; gap: 6px; min-height: 4px; }

.item {
  position: relative; display: flex; align-items: center; gap: 10px; height: var(--item-h); padding: 0 12px; font-size: var(--item-fs);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  text-decoration: none; color: var(--text); overflow: hidden;
  transition: transform var(--dur), border-color var(--dur), background var(--dur);
}
.item:hover { border-color: var(--border-strong); background: var(--surface-2); }
:root:not([data-anim="off"]) .item:hover { transform: translateY(-1px); }
.item .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fav { width: var(--ico); height: var(--ico); flex: none; border-radius: 4px; object-fit: contain; }
.fav-letter {
  width: var(--ico); height: var(--ico); flex: none; border-radius: 4px; display: grid; place-items: center;
  font-size: calc(var(--ico) * .6); font-weight: 700; color: #fff; text-transform: uppercase;
}
.item.match-first { border-color: var(--text); }
.item.dim { opacity: .18; }
.group.dim { opacity: .3; }

/* Bearbeitungsmodus */
.item .edit-btn { display: none; }
body.editing .item { cursor: grab; padding-right: 30px; }
body.editing .item .edit-btn {
  display: grid; position: absolute; right: 3px; top: 50%; transform: translateY(-50%);
  width: 24px; height: 24px;
}
body.editing .item .edit-btn svg { width: 13px; height: 13px; }
body.editing .group-head { cursor: grab; }
.group-tools { display: none; }
body.editing .group-tools { display: flex; }
.add-item {
  display: none; height: var(--item-h); align-items: center; justify-content: center; gap: 6px;
  border: 1px dashed var(--border-strong); border-radius: var(--radius); background: transparent;
  color: var(--muted); cursor: pointer;
}
.add-item:hover { color: var(--text); border-color: var(--text); }
body.editing .add-item { display: flex; }
.add-group { min-height: 80px; }
.add-group .add-item { height: 80px; }
.dragging { opacity: .35; }
.drop-line { height: 2px; background: var(--focus); border-radius: 2px; margin: -1px 0; }
.group.drop-target { outline: 2px dashed var(--focus); outline-offset: 4px; }
body.editing .board { padding-top: 14px; }

/* Leerer Zustand */
.empty { max-width: 520px; margin: 12vh auto 0; padding: 0 16px; text-align: center; }
.empty h1 { font-size: 26px; letter-spacing: -.02em; margin: 0 0 6px; }
.empty-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 20px; }

/* Dialoge */
.dialog {
  width: min(440px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
}
.dialog-wide { width: min(720px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0,0,0,.35); backdrop-filter: blur(2px); }
.dialog form, .settings { display: flex; flex-direction: column; gap: 14px; padding: 22px; }
.dialog h2 { margin: 0; font-size: 17px; }
.dialog h3 { margin: 18px 0 4px; font-size: 14px; }
.dialog h3:first-child { margin-top: 0; }
.dialog p { margin: 0 0 10px; }
.dialog label { display: flex; flex-direction: column; gap: 5px; font-weight: 500; }
.dialog label output { color: var(--muted); font-weight: 400; margin-left: 4px; }
.dialog label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.dialog-actions { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.spacer { flex: 1; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.grid-form { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.span-2 { grid-column: span 2; }
.file-btn { cursor: pointer; }
.code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; padding: 7px 10px;
  background: var(--surface-2); border-radius: var(--radius); overflow-wrap: anywhere;
}

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin: -6px 0 4px; overflow-x: auto; }
.tabs button {
  border: 0; background: none; padding: 8px 12px; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--text); }
.tab { min-height: 260px; }

.templates { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.tpl {
  text-align: left; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); cursor: pointer; display: flex; flex-direction: column; gap: 4px;
}
.tpl:hover { border-color: var(--text); }
.tpl strong { font-size: 1em; }
.tpl span { color: var(--muted); font-size: .9em; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--accent); color: var(--accent-text); padding: 10px 16px; border-radius: var(--radius);
  transition: opacity .2s, transform .2s; z-index: 50; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 640px) {
  .topbar { padding: 12px 16px; gap: 10px; flex-wrap: wrap; }
  .search { order: 3; flex: 1 1 100%; }
  .board { padding: 8px 16px 40px; grid-template-columns: repeat(auto-fill, minmax(min(var(--col), 150px), 1fr)); justify-content: stretch; }
  .grid-form { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
}

/* Korrekturen */
.dialog form.grid-form { display: grid; padding: 0; }
.dialog .settings:focus { outline: none; }
/* Schwarze Favicons (GitHub, MDN …) bleiben im Dunkelmodus sichtbar */
:root[data-theme="dark"] .fav { background: #fff; padding: 1px; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .fav { background: #fff; padding: 1px; } }

/* Icon-Auswahl im Lesezeichen-Dialog */
.icon-field { display: flex; flex-direction: column; gap: 6px; }
.icon-label { font-weight: 500; }
.icon-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.icon-preview {
  width: 40px; height: 40px; display: grid; place-items: center; flex: none;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2);
}
.icon-preview .fav, .icon-preview .fav-letter { width: 24px; height: 24px; font-size: 14px; }
.icon-field .file-btn { flex-direction: row; font-weight: 400; }
.fav.custom { background: transparent !important; padding: 0 !important; }

/* Suchfeld mittig: dreispaltige Kopfzeile, Mitte unabhängig von Logo- und Button-Breite */
.topbar { display: grid; grid-template-columns: 1fr minmax(0, 440px) 1fr; }
.topbar .brand { justify-self: start; }
.topbar .search { width: 100%; }
.topbar .actions { justify-self: end; margin-left: 0; }
@media (max-width: 640px) {
  .topbar { grid-template-columns: auto 1fr; }
  .topbar .search { grid-column: 1 / -1; grid-row: 2; }
}

/* Beschreibung unter dem Titel */
.item .text { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; line-height: 1.2; }
.item .text .title { flex: none; }
.item .desc {
  font-size: max(10px, calc(var(--item-fs) * .7)); color: var(--muted); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.item.has-desc { height: auto; min-height: var(--item-h); padding-top: 6px; padding-bottom: 6px; }
.dialog label .muted { font-weight: 400; }

/* Beschreibung dezenter, Icon bei zweizeiligen Kacheln oben an der Titelzeile */
.item .desc {
  font-size: max(10px, calc(var(--item-fs) * .66));
  color: color-mix(in srgb, var(--muted) 65%, transparent);
  margin-top: 1px;
}
.item.has-desc { align-items: flex-start; }
.item.has-desc > .fav, .item.has-desc > .fav-letter { margin-top: calc((var(--item-fs) * 1.2 - var(--ico)) / 2); }

/* Löschen-Button im Suchfeld */
.search-clear {
  flex: none; display: grid; place-items: center; width: 22px; height: 22px; margin-right: -4px;
  border: 0; border-radius: 50%; background: var(--surface-2); color: var(--muted); cursor: pointer;
  transition: background var(--dur), color var(--dur);
}
.search-clear:hover { background: var(--border-strong); color: var(--text); }
.search-clear svg { width: 12px; height: 12px; stroke-width: 2.4; }

/* Gleicher Abstand oben/unten wie bei einzeiligen Kacheln: Titelzeile sitzt exakt an derselben Stelle */
.item .text { line-height: 1.2; }
.item.has-desc {
  padding-top: calc((var(--item-h) - var(--item-fs) * 1.2) / 2 - 1px);   /* -1px: Rahmen */
  padding-bottom: calc((var(--item-h) - var(--item-fs) * 1.2) / 2 - 1px);
}

/* Browser-Standardpadding von Buttons entfernen, sonst sitzen Icons nicht exakt mittig */
.search-clear, .icon-btn { padding: 0; -webkit-appearance: none; appearance: none; line-height: 0; }
.search-clear svg, .icon-btn svg { display: block; }
