:root {
  --primary: #2f6f4f;
  --primary-dark: #24573f;
  --bg: #f5f6f4;
  --card-bg: #ffffff;
  --border: #dfe3e0;
  --text: #232823;
  --text-muted: #6b756f;
  --danger: #b3392c;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }

/* ---- Auth pages ---- */
.auth-page { display: flex; align-items: center; justify-content: center; height: 100vh; }
.auth-box { background: var(--card-bg); padding: 2.2rem; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,.08); width: 340px; }
.auth-box h1 { margin-top: 0; font-size: 1.4rem; }
.auth-box label { display: block; margin: .8rem 0 .3rem; font-size: .85rem; color: var(--text-muted); }
.auth-box input { width: 100%; padding: .6rem; border: 1px solid var(--border); border-radius: 8px; font-size: 1rem; }
.auth-box button { margin-top: 1.2rem; width: 100%; }
.error { color: var(--danger); font-size: .9rem; }
.success { color: var(--primary); font-size: .9rem; }

/* ---- Buttons ---- */
button, .btn-secondary, .btn-primary, .btn-danger, .btn-link {
  cursor: pointer; border: none; border-radius: 8px; padding: .55rem 1rem;
  font-size: .9rem; font-weight: 500; text-decoration: none; display: inline-block;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: #eceeec; color: var(--text); }
.btn-secondary:hover { background: #dfe3e0; }
.btn-danger { background: #fbe4e1; color: var(--danger); }
.btn-danger:hover { background: #f6cfc9; }
.btn-link { background: transparent; color: var(--text-muted); }
.icon-only { background: transparent; color: var(--text); font-size: 1.3rem; padding: .3rem .5rem; display: none; }
.icon-btn.active { color: var(--primary); }
.pin-btn { font-size: 1rem; }

/* ---- Layout ---- */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: .8rem 1.2rem; background: var(--card-bg); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; cursor: pointer; }
.topbar-actions { display: flex; gap: .5rem; }

#layout { display: flex; height: calc(100vh - 58px); }

#sidebar {
  width: 280px; min-width: 220px; background: var(--card-bg); border-right: 1px solid var(--border);
  overflow-y: auto; padding: .8rem;
}
.sidebar-header { display: flex; justify-content: space-between; align-items: center; font-weight: 600; margin-bottom: .5rem; color: var(--text-muted); font-size: .85rem; text-transform: uppercase; }
.sidebar-header button { background: var(--primary); color: white; width: 26px; height: 26px; border-radius: 50%; padding: 0; line-height: 1; }

#content { flex: 1; overflow-y: auto; padding: 1.4rem; }
.empty-state { color: var(--text-muted); text-align: center; margin-top: 4rem; }
.hidden { display: none !important; }

/* ---- Folder tree ---- */
.folder-node { margin-bottom: 2px; }
.folder-row { display: flex; align-items: center; gap: 4px; padding: .4rem .5rem; border-radius: 6px; cursor: pointer; }
.folder-row:hover { background: #eef2ef; }
.folder-row.active { background: #dcecdf; font-weight: 600; }
.folder-row .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-row .actions { display: none; gap: 2px; }
.folder-row:hover .actions { display: flex; }
.folder-row .actions button { background: none; padding: 2px 5px; font-size: .8rem; color: var(--text-muted); }
.folder-children { margin-left: 16px; border-left: 1px dashed var(--border); padding-left: 6px; }
.folder-checkbox { margin-right: 4px; }

#exportBar { margin-top: 1rem; padding: .8rem; background: #f0f4f1; border-radius: 8px; }
#exportBar p { margin: 0 0 .5rem; font-size: .85rem; color: var(--text-muted); }
#exportBar button { display: block; width: 100%; margin-bottom: .4rem; }

/* ---- Folder view ---- */
.folder-view-header { margin-bottom: 1rem; }
.folder-view-header h2 { margin: 0 0 .6rem; }
.tabs { display: flex; gap: .4rem; border-bottom: 1px solid var(--border); }
.tab { background: none; border-radius: 8px 8px 0 0; color: var(--text-muted); padding: .5rem 1rem; }
.tab.active { background: var(--card-bg); color: var(--primary); font-weight: 600; border: 1px solid var(--border); border-bottom-color: var(--card-bg); margin-bottom: -1px; }
.tab-panel { display: none; padding-top: 1rem; }
.tab-panel.active { display: block; }

.drop-zone {
  border: 2px dashed var(--border); border-radius: 12px; padding: 1.6rem; text-align: center;
  margin-bottom: 1.2rem; background: var(--card-bg);
}
.drop-zone.dragover { border-color: var(--primary); background: #eef7f0; }
.drop-zone p { margin: 0 0 .8rem; color: var(--text-muted); }
.drop-zone button { margin: 0 .3rem; }

.doc-grid, .notes-grid, .todos-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: .8rem; }

.doc-card, .note-card, .todo-card {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px; padding: .8rem;
  display: flex; flex-direction: column; gap: .4rem;
}
.doc-card .icon { font-size: 1.8rem; }
.doc-card .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-card .meta { font-size: .75rem; color: var(--text-muted); }
.doc-card .row { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: auto; }
.doc-card .row button, .doc-card .row a { flex: 1 1 45%; text-align: center; font-size: .72rem; padding: .35rem .3rem; }

.note-card { cursor: pointer; }
.note-card .title { font-weight: 600; }
.note-card .preview { font-size: .8rem; color: var(--text-muted); max-height: 3.2em; overflow: hidden; }
.note-card .meta { font-size: .7rem; color: var(--text-muted); }

.todo-card .todo-header { display: flex; justify-content: space-between; align-items: center; }
.todo-card .todo-header input { border: none; font-weight: 600; font-size: 1rem; background: transparent; width: 70%; }
.todo-item { display: flex; align-items: center; gap: .4rem; padding: .2rem 0; }
.todo-item input[type=text] { border: none; flex: 1; background: transparent; }
.todo-item.done input[type=text] { text-decoration: line-through; color: var(--text-muted); }
.todo-add-row { display: flex; gap: .4rem; margin-top: .5rem; }
.todo-add-row input { flex: 1; border: 1px solid var(--border); border-radius: 6px; padding: .3rem .5rem; }
.icon-btn { background: none; color: var(--text-muted); padding: 2px 6px; }

/* ---- Modals ---- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal-box { background: var(--card-bg); border-radius: 12px; padding: 1.4rem; width: 500px; max-width: 92vw; max-height: 88vh; overflow-y: auto; }
.modal-box.small { width: 360px; }
.modal-box input[type=text], .modal-box textarea { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: .6rem; font-size: 1rem; margin-bottom: .6rem; font-family: inherit; }
.modal-box textarea { min-height: 220px; resize: vertical; }
.modal-actions { display: flex; justify-content: space-between; align-items: center; margin-top: .6rem; }
.modal-actions > div { display: flex; gap: .4rem; }
.hint { font-size: .78rem; color: var(--text-muted); margin-top: .6rem; }

.scan-pages { display: flex; flex-wrap: wrap; gap: .5rem; margin: .8rem 0; }
.scan-pages .page-thumb { position: relative; width: 90px; height: 120px; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.scan-pages .page-thumb img { width: 100%; height: 100%; object-fit: cover; }
.scan-pages .page-thumb .remove { position: absolute; top: 2px; right: 2px; background: rgba(255,255,255,.9); border-radius: 50%; width: 20px; height: 20px; padding: 0; font-size: .7rem; line-height: 1; }

@media (max-width: 780px) {
  #layout { flex-direction: column; height: auto; }
  #sidebar { width: 100%; max-height: 260px; }
  #content { padding: 1rem; }
}

/* ============================================================
   v2 – Dashboard, Unterordner, Teilen, Verschieben, Scan-Editor
   ============================================================ */

.dash-section { margin-bottom: 1.8rem; }
.dash-section:empty { display: none; margin: 0; }
.dash-title { font-weight: 700; font-size: 1rem; margin-bottom: .6rem; color: var(--text); }

.back-link { display: none; margin-bottom: .4rem; padding-left: 0; }

.doc-top, .note-top { display: flex; justify-content: space-between; align-items: flex-start; }
.note-top .title { font-weight: 600; cursor: pointer; }
.note-card .meta { cursor: pointer; }

/* ---- Unterordner-Kacheln ---- */
.subfolder-grid, .move-tree-inline { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.subfolder-tile {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px; padding: .7rem;
  display: flex; align-items: center; gap: .5rem; cursor: pointer;
}
.subfolder-tile:hover { border-color: var(--primary); }
.subfolder-tile .sf-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
#subfolderSection:has(#subfolderList:empty) { display: none; }
.section-label { font-weight: 600; color: var(--text-muted); font-size: .85rem; text-transform: uppercase; }
.panel-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: .6rem; }

/* ---- Notiz: offen im Dashboard ---- */
.note-open-card { background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px; padding: .8rem; margin-bottom: .8rem; }
.note-open-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: .5rem; }
.note-open-header input { flex: 1; border: none; background: transparent; font-weight: 600; font-size: 1rem; }
.note-open-card textarea { width: 100%; min-height: 100px; border: 1px solid var(--border); border-radius: 8px; padding: .5rem; font-family: inherit; resize: vertical; }

/* ---- Teilen ---- */
.share-options { display: flex; flex-direction: column; gap: .5rem; margin: .8rem 0; }
.share-btn { text-align: center; }
.share-link-row { display: flex; gap: .4rem; }
.share-link-row input { flex: 1; border: 1px solid var(--border); border-radius: 8px; padding: .5rem; font-size: .8rem; }

/* ---- Verschieben ---- */
.move-tree { max-height: 300px; overflow-y: auto; }
.move-row { padding: .5rem .6rem; border-radius: 6px; cursor: pointer; }
.move-row:hover { background: #eef2ef; }

/* ---- Scan-Editor (Crop) ---- */
.crop-box { width: 640px; }
.crop-container { width: 100%; height: 340px; background: #111; border-radius: 8px; overflow: hidden; }
.crop-container img { max-width: 100%; display: block; }
.crop-toolbar { display: flex; gap: .5rem; justify-content: center; margin: .8rem 0; }
.page-thumb .edit { position: absolute; bottom: 2px; right: 2px; background: rgba(255,255,255,.9); border-radius: 6px; width: 22px; height: 22px; padding: 0; font-size: .7rem; line-height: 1; }

/* ============================================================
   Mobile-Optimierung
   ============================================================ */

@media (max-width: 780px) {
  .icon-only { display: inline-block; }

  #topbar { padding: .6rem .8rem; }
  .topbar-actions { gap: .3rem; }
  .topbar-actions .btn-secondary, .topbar-actions a { padding: .45rem .6rem; font-size: .78rem; }
  .brand { font-size: 1rem; }

  #layout { position: relative; overflow: hidden; }

  #sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 60; width: 82vw; max-width: 320px;
    transform: translateX(-105%); transition: transform .2s ease; box-shadow: 2px 0 12px rgba(0,0,0,.15);
  }
  #sidebar.open { transform: translateX(0); }
  .sidebar-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 55; }

  #content { padding: .8rem; }

  .back-link { display: inline-block; }

  .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tab { white-space: nowrap; padding: .5rem .8rem; }

  .doc-grid, .notes-grid, .todos-grid, .subfolder-grid { grid-template-columns: 1fr 1fr; gap: .6rem; }

  .modal-box { width: 94vw; padding: 1rem; }
  .crop-box { width: 94vw; }
  .crop-container { height: 260px; }

  .drop-zone { padding: 1.1rem; }
  .drop-zone button { display: block; width: 100%; margin: .4rem 0 0; }

  button, .btn-secondary, .btn-primary, .btn-danger { padding: .6rem .8rem; }
  .doc-card .row, .row { flex-wrap: wrap; }
  .doc-card .row button, .doc-card .row a { flex: 1 1 45%; }
}

@media (max-width: 420px) {
  .doc-grid, .notes-grid, .todos-grid, .subfolder-grid { grid-template-columns: 1fr; }
}

/* ============================================================
   v4 – Dropdown-Menü, einklappbare Ordner
   ============================================================ */

.menu-wrap { position: relative; }
.icon-only-desktop { font-size: 1.1rem; padding: .5rem .7rem; }
.dropdown-menu {
  position: absolute; top: calc(100% + 6px); right: 0; background: var(--card-bg);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.12);
  min-width: 180px; padding: .4rem; display: flex; flex-direction: column; gap: .15rem; z-index: 80;
}
.dropdown-menu button, .dropdown-menu a {
  display: block; text-align: left; background: none; border: none; border-radius: 6px;
  padding: .55rem .7rem; font-size: .88rem; color: var(--text); text-decoration: none; cursor: pointer;
}
.dropdown-menu button:hover, .dropdown-menu a:hover { background: #eef2ef; }

.collapse-toggle {
  display: inline-flex; align-items: center; justify-content: center; width: 18px; height: 18px;
  font-size: .7rem; color: var(--text-muted); cursor: pointer; flex-shrink: 0;
}
.collapse-toggle.spacer { cursor: default; }
.folder-row { display: flex; align-items: center; }
