/* Zettel — тёмная тема в стиле Obsidian */

:root {
  --bg: #1e1e2e;
  --bg-sidebar: #181825;
  --bg-surface: #313244;
  --bg-hover: #45475a;
  --text: #cdd6f4;
  --text-dim: #9399b2;
  --accent: #89b4fa;
  --green: #a6e3a1;
  --red: #f38ba8;
  --border: #45475a;
  --radius: 8px;
}

* { box-sizing: border-box; }

/* атрибут hidden всегда побеждает display из других правил */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

/* ---------------- Сайдбар ---------------- */

#sidebar {
  width: 280px;
  min-width: 220px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 10px;
}

.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  font-weight: 700;
  letter-spacing: .5px;
  color: var(--accent);
}

#btn-new {
  background: var(--accent);
  color: #1e1e2e;
  border: none;
  border-radius: var(--radius);
  padding: 7px 12px;
  font-weight: 600;
  cursor: pointer;
}
#btn-new:hover { filter: brightness(1.1); }

#search {
  width: 100%;
  padding: 8px 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text);
  outline: none;
}
#search:focus { border-color: var(--accent); }

.note-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
}

.note-list li {
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-dim);
}
.note-list li:hover { background: var(--bg-hover); }
.note-list li.active {
  background: var(--bg-surface);
  color: var(--text);
  font-weight: 600;
}

#note-list { flex: 1; }

#backlinks-box {
  border-top: 1px solid var(--border);
  padding-top: 8px;
}
#backlinks-box h3 {
  margin: 0 0 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
}
#backlinks li { color: var(--green); }

/* ---------------- Редактор ---------------- */

#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-sidebar);
}

#note-title {
  font-weight: 600;
  flex-shrink: 0;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#save-status {
  flex: 1;
  text-align: right;
  font-size: 13px;
  color: var(--text-dim);
}
#save-status.saving { color: #f9e2af; }
#save-status.saved  { color: var(--green); }
#save-status.error  { color: var(--red); }

#toolbar button {
  background: var(--bg-surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  cursor: pointer;
}
#toolbar button:hover { background: var(--bg-hover); }
#btn-delete:hover { color: var(--red); border-color: var(--red); }

#panes {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* обёртка редактора: занимает всё место под тулбаром */
#editor-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

/* вид карточек */
#home-view {
  flex: 1;
  overflow-y: auto;
}

#preview {
  flex: 1;
  width: 50%;
  overflow-y: auto;
  padding: 20px 28px;
  border-left: 1px dashed var(--border);
  background: var(--bg-sidebar);
}

/* ---------------- Markdown ---------------- */

.markdown-body { line-height: 1.7; word-wrap: break-word; }
.markdown-body h1, .markdown-body h2, .markdown-body h3 { line-height: 1.3; }
.markdown-body h1 { border-bottom: 1px solid var(--border); padding-bottom: .3em; }
.markdown-body code {
  background: var(--bg-surface);
  padding: 2px 6px;
  border-radius: 5px;
  font-family: Consolas, ui-monospace, monospace;
  font-size: .9em;
}
.markdown-body pre {
  background: var(--bg-surface);
  padding: 12px 16px;
  border-radius: var(--radius);
  overflow-x: auto;
}
.markdown-body pre code { background: none; padding: 0; }
.markdown-body blockquote {
  margin-left: 0;
  padding: 2px 16px;
  border-left: 4px solid var(--accent);
  color: var(--text-dim);
}
.markdown-body a { color: var(--accent); }
.markdown-body img { max-width: 100%; }
.markdown-body table { border-collapse: collapse; }
.markdown-body th, .markdown-body td {
  border: 1px solid var(--border);
  padding: 6px 12px;
}
.markdown-body hr { border: none; border-top: 1px solid var(--border); }

/* wiki-ссылки [[...]] */
a.wiki-link {
  color: var(--green);
  text-decoration: none;
  border-bottom: 1px dotted var(--green);
  cursor: pointer;
}
a.wiki-link.missing { color: var(--red); border-color: var(--red); }

/* ---------------- Адаптивность и мобильные ---------------- */

@media (max-width: 800px) {
  #sidebar {
    position: fixed;
    z-index: 10;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    transition: transform .2s ease;
    box-shadow: 0 0 20px rgba(0,0,0,.5);
  }
  body.sidebar-open #sidebar { transform: translateX(0); }
  #panes { flex-direction: column; }
  .editor-stack { width: 100%; flex: none; height: 50%; }
  #preview { width: 100%; border-left: none; border-top: 1px dashed var(--border); }
}

/* ---------------- Вкладки видов ---------------- */

.side-head button.tab.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--bg-surface);
}

.burger {
  background: none;
  border: none;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  padding: 0 6px;
  display: none;
}

/* бургер виден только на мобильных */
@media (max-width: 800px) {
  .burger { display: block; }
}

/* ---------------- Редактор: overlay-подсветка ссылок ---------------- */

.editor-stack {
  position: relative;
  flex: 1;
  width: 50%;
  min-width: 0;
}

#highlight {
  position: absolute;
  inset: 0;
  overflow: hidden;
  padding: 20px 24px;
  font-family: Consolas, "Cascadia Code", ui-monospace, monospace;
  font-size: 15px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
  pointer-events: none;
}

#highlight .hl-wl {
  color: var(--accent);
  font-weight: 600;
  cursor: pointer;
}
#highlight .hl-wl.hl-missing {
  color: var(--red);
  text-decoration: underline dotted;
}

#editor {
  position: relative;
  flex: 1;
  width: 100%;
  height: 100%;
  display: block;
  resize: none;
  border: none;
  outline: none;
  padding: 20px 24px;
  background: transparent;
  color: transparent;          /* текст рисует #highlight */
  caret-color: var(--text);    /* а курсор — настоящий */
  /* метрики ДОЛЖНЫ совпадать с #highlight, иначе слои разъедутся */
  font-family: Consolas, "Cascadia Code", ui-monospace, monospace;
  font-size: 15px;
  line-height: 1.65;
  letter-spacing: normal;
}
#editor::placeholder { color: var(--text-dim); }
/* при выделении текст должен стать видимым (он прозрачный в обычном режиме) */
#editor::selection { background: rgba(137, 180, 250, .45); color: var(--text); }

/* двойной клик по ссылке открывает заметку — подсказка в статусе */

/* ---------------- Модалка «Создать связь» ---------------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(17,17,27,.72);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 10vh;
}
.modal-backdrop[hidden] { display: none; }

.modal-panel {
  width: min(480px, 92vw);
  max-height: 70vh;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0,0,0,.5);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.modal-head button {
  background: var(--bg-surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 28px; height: 28px;
  cursor: pointer;
}
.modal-head button:hover { color: var(--red); border-color: var(--red); }

.modal-body {
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
}
.sel-preview { font-size: 13px; color: var(--text-dim); }
.sel-preview b { color: var(--green); }

#link-search {
  padding: 9px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text);
  outline: none;
}
#link-search:focus { border-color: var(--accent); }

#link-results {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
}
#link-results li {
  padding: 9px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text);
}
#link-results li:hover, #link-results li.active {
  background: var(--accent);
  color: #1e1e2e;
}
#link-results li.create-new { color: var(--green); font-weight: 600; }
#link-results li.create-new:hover, #link-results li.create-new.active {
  background: var(--green);
  color: #1e1e2e;
}
#link-results li.none { color: var(--text-dim); cursor: default; }
#link-results li.none:hover { background: none; }

/* ---------------- Граф связей (полный вид) ---------------- */

#graph-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
#graph-view[hidden] { display: none; }

.graph-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-sidebar);
  font-weight: 600;
}
.graph-head .hint { font-weight: 400; font-size: 12px; color: var(--text-dim); }
.graph-head button {
  background: var(--bg-surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 30px; height: 30px;
  cursor: pointer;
}
.graph-head button:hover { color: var(--accent); border-color: var(--accent); }

#graph-canvas {
  flex: 1;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

/* ---------------- Навигация в сайдбаре ---------------- */

.side-head button {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 5px 9px;
  cursor: pointer;
}
.side-head button:hover { background: var(--bg-hover); }

/* ---------------- Домашняя страница (Google Keep style) ---------------- */

#home-view {
  flex: 1;
  overflow-y: auto;
  padding: 20px 28px;
}

.home-head {
  display: flex;
  align-items: baseline;
  gap: 14px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.home-head h1 { margin: 0; font-size: 22px; }
.home-head .hint { font-size: 12px; color: var(--text-dim); }
.home-head button {
  margin-left: auto;
  background: var(--bg-surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  cursor: pointer;
}
.home-head button:hover { background: var(--bg-hover); }

#home-grid {
  columns: 240px;
  column-gap: 12px;
}

.note-card {
  break-inside: avoid;
  display: inline-block;
  width: 100%;
  margin-bottom: 12px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
  transition: border-color .12s;
}
.note-card:hover { border-color: var(--accent); }
.note-card.dragging { opacity: .35; }
.note-card h4 {
  margin: 0 0 6px;
  font-size: 15px;
  color: var(--accent);
  word-break: break-word;
}
.note-card p {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.45;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 6;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.note-card time {
  font-size: 11px;
  color: var(--text-dim);
  opacity: .7;
}

.empty-hint {
  color: var(--text-dim);
  padding: 40px 0;
  text-align: center;
  grid-column: 1 / -1;
}

/* ---------------- Автодополнение [[ссылок ---------------- */

#panes { position: relative; }

#ac-popup {
  position: absolute;
  z-index: 30;
  min-width: 220px;
  max-width: 320px;
  max-height: 240px;
  overflow-y: auto;
  background: var(--bg-surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: 0 6px 24px rgba(0,0,0,.45);
  font-size: 13px;
}
#ac-popup div {
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#ac-popup div.active, #ac-popup div:hover { background: var(--accent); color: #1e1e2e; }
#ac-popup .ac-empty { color: var(--text-dim); cursor: default; }
#ac-popup .ac-empty.active, #ac-popup .ac-empty:hover { background: none; color: var(--text-dim); }

[hidden] { display: none !important; }
#unlock-panel { min-height: 100dvh; display: grid; place-items: center; padding: 24px; overflow: auto; }
#unlock-form { width: min(100%, 440px); display: grid; gap: 16px; }
#unlock-form h1 { font-size: 32px; }
#unlock-form label { display: grid; gap: 8px; }
#unlock-form input { width: 100%; padding: 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg-sidebar); color: var(--text); }
#unlock-form .check-label { display: flex; align-items: center; }
#unlock-form input[type=checkbox] { width: auto; }
#unlock-form p { color: var(--text-dim); line-height: 1.5; }
#unlock-form #unlock-error { color: var(--red); }
#privacy-bar { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px; background: var(--bg-sidebar); }
#privacy-bar label { font-size: 12px; }
#privacy-bar input { max-width: 160px; }
body:has(#privacy-bar:not([hidden])) #app { height: calc(100dvh - 56px); }
