:root {
  --bg: #ffffff;
  --bg-side: #f4f5f7;
  --bg-soft: #eef0f3;
  --bg-hover: #e6e9ee;
  --text: #1c1f24;
  --muted: #646b76;
  --border: #dde1e6;
  --accent: #2563eb;
  --accent-soft: #dbe7fe;
  --on-accent: #ffffff;
  --danger: #c62828;
  --ok: #1b7f3b;
  --warn: #c76a00;
  --radius: 10px;
  --shadow: 0 8px 30px rgb(0 0 0 / 0.16);
  color-scheme: light dark;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17191d;
    --bg-side: #1f2227;
    --bg-soft: #262a30;
    --bg-hover: #2e333a;
    --text: #e8eaed;
    --muted: #9aa1ab;
    --border: #353a42;
    --accent: #5b8def;
    --accent-soft: #24365a;
    --danger: #ef6b6b;
    --ok: #4cc27a;
    --warn: #f0a33a;
    --shadow: 0 8px 30px rgb(0 0 0 / 0.5);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

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

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 8px;
  padding: 6px 12px;
}

button:hover:not(:disabled) {
  background: var(--bg-hover);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

button.primary:hover:not(:disabled) {
  filter: brightness(1.08);
  background: var(--accent);
}

button.danger {
  color: var(--danger);
}

button.icon {
  border: none;
  background: transparent;
  padding: 4px 8px;
  font-size: 1.15rem;
  line-height: 1;
}

button.icon.small {
  font-size: 0.95rem;
  padding: 2px 6px;
}

button.link {
  border: none;
  background: none;
  color: var(--accent);
  padding: 2px 0;
  text-align: left;
}

input[type="text"],
input[type="password"],
input[type="search"],
input[type="date"],
input[type="datetime-local"],
select,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  background: var(--bg);
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible,
a:focus-visible,
li:focus-visible,
tr:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

a {
  color: var(--accent);
}

pre {
  background: var(--bg-soft);
  padding: 8px 10px;
  border-radius: 8px;
  overflow-x: auto;
}

code {
  font: 0.9em ui-monospace, SFMono-Regular, Consolas, monospace;
}

.hint,
small {
  color: var(--muted);
}

.form-error {
  color: var(--danger);
  white-space: pre-line;
  margin: 0;
}

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* --- Anmeldung --- */

.login-page {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg-side);
}

.login {
  width: min(420px, 100%);
  display: grid;
  gap: 12px;
  background: var(--bg);
  padding: 28px;
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.login h1 {
  margin: 0;
}

.login p {
  margin: 0;
}

/* --- Gerüst --- */

#app {
  height: 100%;
}

.shell {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100%;
  height: 100dvh;
}

.sidebar {
  background: var(--bg-side);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar > header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 12px 8px 16px;
}

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
}

.sidebar-list h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 16px 8px 4px;
}

.sidebar-list .hint {
  font-size: 0.85rem;
  margin: 4px 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  border-radius: 8px;
}

.nav-item a {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  color: inherit;
  text-decoration: none;
  min-width: 0;
}

.nav-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav-icon {
  width: 1.4em;
  text-align: center;
}

.nav-item:hover {
  background: var(--bg-hover);
}

.nav-item.active {
  background: var(--accent-soft);
}

.nav-item.muted .nav-label {
  color: var(--muted);
}

.nav-item button {
  opacity: 0;
}

.nav-item:hover button,
.nav-item.active button,
.nav-item button:focus-visible {
  opacity: 1;
}

.usage {
  padding: 10px 16px 14px;
  font-size: 0.8rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

.usage:empty {
  display: none;
}

.bar {
  margin-top: 5px;
  height: 5px;
  border-radius: 3px;
  background: var(--bg-hover);
  overflow: hidden;
}

.bar > div {
  height: 100%;
  background: var(--accent);
}

.bar.medium > div {
  background: var(--warn);
}

.bar.high > div {
  background: var(--danger);
}

.content {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.content-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  min-height: 56px;
}

.content-header h1 {
  flex: 1;
  font-size: 1.15rem;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.content-header .actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.content-header input[type="search"] {
  width: 200px;
}

.menu-toggle {
  display: none;
}

.scrim {
  display: none;
}

/* --- Menüs, Dialoge, Hinweise --- */

.menu {
  position: fixed;
  z-index: 20;
  min-width: 200px;
  max-width: min(360px, calc(100vw - 16px));
  max-height: 70vh;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 4px;
}

.menu button {
  display: flex;
  gap: 6px;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  padding: 7px 10px;
  border-radius: 6px;
}

.menu .check {
  width: 1em;
  color: var(--accent);
}

.menu hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 4px 0;
}

dialog {
  border: none;
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 0;
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100vh - 48px);
  background: var(--bg);
  color: var(--text);
}

dialog.wide {
  width: min(560px, calc(100vw - 24px));
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.35);
}

dialog form {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 48px);
}

dialog h2 {
  margin: 0;
  padding: 18px 20px 6px;
  font-size: 1.1rem;
}

.dialog-body {
  padding: 8px 20px;
  overflow-y: auto;
  display: grid;
  gap: 12px;
}

.dialog-body h3 {
  margin: 6px 0 0;
  font-size: 0.9rem;
}

dialog .form-error {
  padding: 0 20px;
}

.dialog-buttons {
  display: flex;
  gap: 8px;
  padding: 14px 20px 18px;
  flex-wrap: wrap;
}

.spacer {
  flex: 1;
}

.form {
  display: grid;
  gap: 12px;
}

.field {
  display: grid;
  gap: 4px;
}

.field[hidden] {
  display: none;
}

.field .label {
  font-size: 0.85rem;
  color: var(--muted);
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

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

.icon-preview {
  width: 2em;
  text-align: center;
  font-size: 1.2rem;
}

.field-list,
.backlinks {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.field-list li,
.backlinks li {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 6px 10px;
  border-top: 1px solid var(--border);
}

.field-list li:first-child,
.backlinks li:first-child {
  border-top: none;
}

.field-list span,
.backlinks a {
  flex: 1;
}

.toasts {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  gap: 8px;
  z-index: 30;
  width: min(480px, calc(100vw - 24px));
}

.toast {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: var(--danger);
  color: #fff;
  padding: 10px 12px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  white-space: pre-line;
}

.toast button {
  margin-left: auto;
  border: none;
  background: none;
  color: inherit;
  padding: 0 4px;
  font-size: 1.1rem;
}

/* --- Chat --- */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.intro {
  display: grid;
  gap: 6px;
  justify-items: start;
}

.intro h2 {
  font-size: 1.05rem;
  margin: 0;
}

.intro p {
  margin: 0;
  color: var(--muted);
}

.bubble-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: min(720px, 85%);
}

.bubble-row.user {
  align-self: flex-end;
  align-items: flex-end;
}

.bubble {
  padding: 9px 12px;
  border-radius: 14px;
  background: var(--bg-soft);
  overflow-wrap: anywhere;
}

.bubble-row.user .bubble {
  background: var(--accent-soft);
  white-space: pre-wrap;
}

.markdown > :first-child {
  margin-top: 0;
}

.markdown > :last-child {
  margin-bottom: 0;
}

.markdown p,
.markdown ul,
.markdown ol {
  margin: 0.4em 0;
}

.markdown ul,
.markdown ol {
  padding-left: 1.4em;
}

.markdown h3,
.markdown h4,
.markdown h5,
.markdown h6 {
  margin: 0.6em 0 0.3em;
  font-size: 1em;
}

.action {
  font-size: 0.82rem;
  color: var(--ok);
}

.typing {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}

.composer textarea {
  resize: none;
  max-height: 200px;
  background: var(--bg-soft);
  border-color: transparent;
}

.composer .send {
  border-radius: 50%;
  width: 38px;
  height: 38px;
  padding: 0;
  font-size: 1.2rem;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  flex: none;
}

.composer .mic {
  height: 38px;
}

.mic.listening {
  color: var(--danger);
  animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.4;
  }
}

/* --- Bereiche --- */

.collection-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px 32px;
}

.loading {
  display: grid;
  place-items: center;
  padding: 40px;
}

.description {
  color: var(--muted);
  margin: 0 0 12px;
}

.collection-body h3 {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 18px 0 6px;
}

.quick-add {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.entries {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.entry {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 9px 12px;
  border-top: 1px solid var(--border);
  cursor: pointer;
}

.entry:first-child {
  border-top: none;
}

.entry:hover {
  background: var(--bg-soft);
}

.entry input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin-top: 2px;
  accent-color: var(--accent);
  flex: none;
}

.entry-text {
  flex: 1;
  min-width: 0;
}

.entry.done .entry-title {
  text-decoration: line-through;
  color: var(--muted);
}

.entry-details {
  font-size: 0.82rem;
  color: var(--muted);
}

.entry-long {
  font-size: 0.9rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.entry-long.notes {
  white-space: pre-line;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
}

.entry .delete {
  opacity: 0;
}

.entry:hover .delete,
.entry .delete:focus-visible {
  opacity: 0.7;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

table {
  border-collapse: collapse;
  width: 100%;
}

th,
td {
  text-align: left;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  vertical-align: top;
  max-width: 320px;
  overflow: hidden;
  text-overflow: ellipsis;
}

th {
  border-top: none;
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
  background: var(--bg-side);
  white-space: nowrap;
}

td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

tbody tr {
  cursor: pointer;
}

tbody tr:hover {
  background: var(--bg-soft);
}

/* --- Schmale Bildschirme (Handy) --- */

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

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(300px, 85vw);
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    padding-top: env(safe-area-inset-top);
  }

  .sidebar-open .sidebar {
    transform: none;
    box-shadow: var(--shadow);
  }

  .sidebar-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgb(0 0 0 / 0.3);
    z-index: 9;
  }

  .menu-toggle {
    display: inline-block;
  }

  .content-header {
    padding: calc(8px + env(safe-area-inset-top)) 8px 8px;
    gap: 4px;
    flex-wrap: wrap;
  }

  /* Suche in eine eigene Zeile, damit der Titel Platz hat */
  .content-header .actions {
    display: contents;
  }

  .content-header input[type="search"] {
    order: 5;
    flex: 1 1 100%;
    width: auto;
  }

  .nav-item button,
  .entry .delete {
    opacity: 0.6;
  }

  .bubble-row {
    max-width: 92%;
  }
}

.token-login {
  display: grid;
  gap: 12px;
}

.token-login summary {
  cursor: pointer;
  color: var(--muted);
}
