@font-face {
  font-family: 'RuneNorse';
  src: url('fonts/Norse.otf') format('opentype');
  font-weight: 400;
}
@font-face {
  font-family: 'RuneNorse';
  src: url('fonts/Norse-Bold.otf') format('opentype');
  font-weight: 700;
}
@font-face {
  font-family: 'RuneScript';
  src: url('fonts/NotoSansRunic-Regular.ttf') format('truetype');
  font-weight: 400;
}
@font-face {
  font-family: 'RuneText';
  src: url('fonts/Lora.ttf') format('truetype');
  font-weight: 400 700;
}

:root {
  --bg: #141021;
  --panel: #1c1630;
  --module: #2a2145;
  --border: #40346a;
  --text: #efeaff;
  --muted: #a396c9;
  --accent: #c084fc;
  --accent-weak: rgba(192, 132, 252, 0.14);
  --danger: #f472b6;
  --danger-weak: rgba(244, 114, 182, 0.12);
  --radius: 8px;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
  --glow: #e879f9;
  --rune-gold: #9c7a3c;
  --rune-gold-bright: #b8860b;
}

* { box-sizing: border-box; }

/* the hidden attribute must win (classes like .modal override the UA default) */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.brand {
  font-family: 'RuneNorse', serif;
  font-size: 18px;
  font-weight: 800;
  color: var(--rune-gold-bright);
  letter-spacing: 1.5px;
  text-shadow: 0 0 14px rgba(184, 134, 11, 0.4);
}
.toolbar { display: flex; gap: 8px; }

/* buttons */
.btn {
  font-family: 'RuneNorse', serif;
  border: 1px solid var(--border);
  background: var(--module);
  color: var(--rune-gold);
  padding: 6px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  letter-spacing: .6px;
  transition: filter .15s, background .15s, color .15s;
}
.btn:hover { filter: brightness(1.15); }
.btn.primary {
  background: linear-gradient(135deg, #c084fc, #8b5cf6);
  border-color: transparent;
  color: #1a1030;
  font-weight: 700;
  box-shadow: 0 0 14px rgba(192, 132, 252, 0.28);
}
.btn.danger { background: linear-gradient(135deg, #f472b6, #ec4899); border-color: transparent; color: #1c0814; font-weight: 700; }
.btn.ghost { background: transparent; }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; filter: none; }

/* main layout: note sidebar | canvas | log sidebar */
main { flex: 1; display: flex; min-height: 0; }
#canvas-wrap { flex: 1; position: relative; overflow: hidden; min-width: 0; background: var(--bg); }
#canvas { width: 100%; height: 100%; display: block; cursor: grab; }
#canvas.dragging { cursor: grabbing; }
.zoom-hint {
  position: absolute;
  top: 10px;
  left: 12px;
  font-size: 11px;
  font-family: "Cascadia Code", Consolas, monospace;
  color: var(--muted);
  background: rgba(28, 22, 48, .85);
  border: 1px solid var(--border);
  padding: 2px 9px;
  border-radius: 10px;
  pointer-events: none;
  z-index: 2;
}

/* sidebars */
.sidebar {
  width: 340px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  z-index: 5;
}
#note-sidebar { border-right: 1px solid var(--border); }
#log-sidebar { width: 380px; border-left: 1px solid var(--border); }
.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 15px;
  font-family: 'RuneNorse', serif;
  letter-spacing: .8px;
  color: var(--rune-gold-bright);
}
.sidebar-head .close { border: none; background: none; font-size: 20px; cursor: pointer; color: var(--muted); line-height: 1; }
.sidebar-head .close:hover { color: var(--danger); }

/* note modules */
.note-modules { flex: 1; overflow: auto; padding: 10px; }
.note-module {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 12px;
  background: var(--module);
}
.module-head { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
.module-title {
  flex: 1;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  background: transparent;
}
.module-title:hover, .module-title:focus { border-color: var(--border); outline: none; background: rgba(255, 255, 255, 0.03); }
.module-del { border: none; background: none; cursor: pointer; color: var(--muted); font-size: 14px; }
.module-del:hover { color: var(--danger); }
.module-tag { font-size: 11px; color: var(--accent); background: rgba(192, 132, 252, 0.12); padding: 2px 8px; border-radius: 10px; }
textarea.module-body {
  width: 100%;
  min-height: 90px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  font-size: 14px;
  font-family: 'RuneText', serif;
  line-height: 1.6;
  resize: vertical;
  color: var(--text);
  background: #1e1833;
}
textarea.module-body:focus { outline: 2px solid var(--accent-weak); border-color: var(--accent); }
.module-tools { display: flex; gap: 6px; margin-top: 8px; }
.sidebar-foot {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.file-link {
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  font-weight: 500;
  white-space: nowrap;
}

/* note preview */
.note-preview {
  flex: 1;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-size: 14px;
  font-family: 'RuneText', serif;
  line-height: 1.7;
  background: #1e1833;
  color: var(--text);
  word-break: break-word;
}
.note-preview img.note-img { max-width: 100%; border-radius: 6px; margin: 6px 0; }
.note-preview h2, .note-preview h3, .note-preview h4 { margin: .6em 0 .3em; color: #f3e8ff; }
.note-preview code { background: rgba(192, 132, 252, 0.15); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.note-preview blockquote {
  border-left: 3px solid var(--accent);
  margin: 6px 0;
  padding: 2px 10px;
  color: var(--muted);
  background: rgba(192, 132, 252, 0.08);
  border-radius: 0 4px 4px 0;
}
.note-preview li { margin-left: 18px; }

/* right-click menu */
.ctx-menu {
  position: fixed;
  z-index: 60;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
  min-width: 200px;
  display: none;
}
.ctx-item {
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}
.ctx-item:hover { background: rgba(192, 132, 252, 0.15); color: #f0abfc; }
.ctx-item.danger:hover { background: var(--danger-weak); color: var(--danger); }
.ctx-sep { height: 1px; background: var(--border); margin: 4px 6px; }

/* modals */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 6, 20, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 20px;
  width: 420px;
  max-width: 92vw;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; font-weight: 600; }
.modal-head .close { border: none; background: none; font-size: 22px; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-body { font-size: 14px; }
.modal-body.danger { color: var(--danger); font-weight: 600; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
  background: var(--module);
  color: var(--text);
}
.field input:focus { outline: 2px solid var(--accent-weak); border-color: var(--accent); }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* log sidebar */
.log-dates {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 8px;
  border-bottom: 1px solid var(--border);
  max-height: 150px;
  overflow: auto;
}
.log-date {
  padding: 4px 10px;
  border-radius: 14px;
  cursor: pointer;
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--border);
  background: var(--module);
}
.log-date:hover { filter: brightness(1.2); }
.log-date.active { background: linear-gradient(135deg, #c084fc, #8b5cf6); color: #1a1030; border-color: transparent; font-weight: 700; }
#log-sidebar .log-content {
  flex: 1;
  border: none;
  border-radius: 0;
  overflow: auto;
  font-family: 'RuneText', serif;
  font-size: 13px;
  background: #1e1833;
  padding: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  color: var(--text);
}

/* in-page toast (native alert may be disabled in webviews) */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #2a2145;
  border: 1px solid var(--border);
  color: #f3e8ff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 100;
  display: none;
  max-width: 80vw;
  text-align: center;
  line-height: 1.5;
}
.toast.show { display: block; }
.toast.error { background: #4c1d3c; border-color: #f472b6; color: #fbcfe8; }

/* --- Logs sidebar: todo pane + rolling 7-day log window --- */
#log-sidebar { display: flex; flex-direction: column; }
.todo-pane { border-bottom: 1px solid var(--border); max-height: 40%; display: flex; flex-direction: column; }
.todo-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; }
.todo-label { font-family: 'RuneNorse', serif; color: var(--rune-gold-bright); font-size: 14px; letter-spacing: .8px; }
.todo-list { overflow: auto; padding: 0 10px 10px; }
.todo-item { display: flex; align-items: center; gap: 8px; padding: 6px 4px; border-radius: 6px; font-size: 13px; }
.todo-item:hover { background: rgba(192,132,252,.08); }
.todo-check { cursor: pointer; font-size: 16px; color: var(--accent); flex: none; }
.todo-text { flex: 1; word-break: break-word; }
.todo-text.done { text-decoration: line-through; color: var(--muted); }
.todo-node { font-size: 11px; color: var(--muted); background: rgba(192,132,252,.12); padding: 1px 7px; border-radius: 9px; flex: none; }
.todo-del { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 13px; flex: none; }
.todo-del:hover { color: var(--danger); }
.log-pane { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.log-window-head { display: flex; align-items: center; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.log-range { flex: 1; text-align: center; font-size: 12px; color: var(--muted); }
.log-days { flex: 1; overflow: auto; padding: 10px 12px; }
.log-day { margin-bottom: 14px; }
.log-day-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.log-day-date { font-family: 'RuneNorse', serif; color: var(--rune-gold-bright); font-size: 13px; letter-spacing: .5px; }
.log-day-add { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 12px; }
.log-day-add:hover { color: var(--accent); }
.log-day-body { font-family: 'RuneText', serif; font-size: 12px; line-height: 1.6; color: var(--text); white-space: pre-wrap; word-break: break-word; }
.log-day-body .empty { color: var(--muted); font-style: italic; }
.log-day-body a.log-node { color: var(--accent); cursor: pointer; text-decoration: underline; }
.log-day-body a.log-node:hover { color: #f0abfc; }

/* --- comment thread (module under the note) --- */
.comment-module .module-tag { color: var(--rune-gold-bright); background: rgba(184,134,11,.12); }
.comment-thread { display: flex; flex-direction: column; gap: 8px; }
.comment-item { border-left: 3px solid #f0abfc; background: rgba(192,132,252,.08); border-radius: 4px; padding: 8px 10px; font-size: 13px; }
.comment-item.reply { border-left-color: var(--accent); background: rgba(192,132,252,.04); margin-left: 14px; }
.comment-time { color: var(--muted); font-size: 11px; display: block; margin-bottom: 3px; }
.comment-content { word-break: break-word; line-height: 1.5; }
.comment-actions { margin-top: 6px; display: flex; gap: 6px; }
.comment-replies { margin-top: 6px; display: flex; flex-direction: column; gap: 6px; }
