@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500;600;700&family=Lora:ital,wght@0,400;0,600;1,400&display=swap");

/* === TOKENS === */
:root {
  --bg: #ffffff;
  --bg-card: #fafafa;
  --bg-sidebar: #f4f4f4;
  --border: #e0e0e0;
  --text: #111111;
  --text-muted: #666666;
  --link: #111111;
  --link-h: #444444;
  --code-bg: #f2f2f2;
  --nav-bg: #ffffff;
  --nav-text: #111111;
  --danger-bg: #111111;
  --danger-text: #ffffff;
  --mono: "IBM Plex Mono", "Courier New", monospace;
  --serif: "Lora", Georgia, serif;
  --body-font: var(--mono);
  --shadow: none;
  --card-depth: none;
  --spine-bg: transparent;
  --spine-w: 0px;
}

/* Theme: dark — white on black */
:root[data-theme="dark"] {
  --bg: #0a0a0a;
  --bg-card: #111111;
  --bg-sidebar: #151515;
  --border: #2a2a2a;
  --text: #eeeeee;
  --text-muted: #888888;
  --link: #eeeeee;
  --link-h: #cccccc;
  --code-bg: #1a1a1a;
  --nav-bg: #080808;
  --nav-text: #eeeeee;
  --danger-bg: #eeeeee;
  --danger-text: #0a0a0a;
}

/* Theme: book — parchment, serif, 3D page depth */
:root[data-theme="book"] {
  --bg: #f5f0e8;
  --bg-card: #faf6ed;
  --bg-sidebar: #ede8dc;
  --border: #c8b89a;
  --text: #2c1f0e;
  --text-muted: #7a5c3a;
  --link: #4a2e0a;
  --link-h: #2c1f0e;
  --code-bg: #e8dfc8;
  --nav-bg: #2c1f0e;
  --nav-text: #f5f0e8;
  --danger-bg: #2c1f0e;
  --danger-text: #f5f0e8;
  --body-font: var(--serif);
  --shadow: 4px 4px 0 #c8b89a, 8px 8px 0 #b8a88a;
  --card-depth: 2px 2px 8px rgba(44,31,14,0.18);
  --spine-bg: linear-gradient(to right, #b8a88a, #d4c4a8, #b8a88a);
  --spine-w: 8px;
}

/* Theme: midnight — deep navy-black */
:root[data-theme="midnight"] {
  --bg: #0d0f1a;
  --bg-card: #131626;
  --bg-sidebar: #0f1120;
  --border: #1e2240;
  --text: #c8d0e8;
  --text-muted: #6070a0;
  --link: #c8d0e8;
  --link-h: #ffffff;
  --code-bg: #0a0c18;
  --nav-bg: #080a14;
  --nav-text: #c8d0e8;
  --danger-bg: #c8d0e8;
  --danger-text: #0d0f1a;
}

/* Theme: terminal — green on black */
:root[data-theme="terminal"] {
  --bg: #000000;
  --bg-card: #0a0a00;
  --bg-sidebar: #080800;
  --border: #1a2a00;
  --text: #00dd44;
  --text-muted: #007722;
  --link: #00dd44;
  --link-h: #00ff55;
  --code-bg: #001100;
  --nav-bg: #000000;
  --nav-text: #00dd44;
  --danger-bg: #00dd44;
  --danger-text: #000000;
}

/* === RESET === */
*, *::before, *::after { box-sizing: border-box; }
body, h1, h2, h3, p, ul, ol, figure { margin: 0; }
button, input, textarea, select { font: inherit; }

/* === BASE === */
body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--body-font);
  font-size: 15px;
  line-height: 1.6;
}

a {
  color: var(--link);
  text-decoration: none;
}
a:hover {
  color: var(--link-h);
  text-decoration: underline;
}

code, pre {
  background: var(--code-bg);
  border-radius: 2px;
  font-family: "IBM Plex Mono", "Courier New", monospace;
}
code { padding: 1px 4px; }
pre {
  padding: 14px;
  overflow-x: auto;
  border: 1px solid var(--border);
}

label {
  display: grid;
  gap: 6px;
  color: var(--text-muted);
  font-size: 0.88rem;
}

input, textarea, select {
  width: 100%;
  color: var(--text);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 2px;
  padding: 9px 10px;
}
textarea { resize: vertical; }

input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--text);
  outline-offset: -1px;
  border-color: var(--text);
}

/* === LAYOUT === */
.top-nav {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 20;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 max(18px, calc((100vw - 1100px) / 2));
  background: var(--nav-bg);
  color: var(--nav-text);
  border-bottom: 1px solid var(--border);
}

.top-nav a, .top-nav button { color: var(--nav-text); }

.brand {
  display: inline-flex;
  align-items: baseline;
  gap: 0;
  font-weight: 700;
  letter-spacing: 0.02em;
  font-family: "IBM Plex Mono", monospace;
}
.brand:hover { text-decoration: none; }

.nav-links {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 0.88rem;
}
.nav-links a:hover, .nav-links button:hover { opacity: 0.65; text-decoration: none; }
.nav-links form { margin: 0; }

.theme-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 2px;
  padding: 3px 8px;
  cursor: pointer;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  color: var(--nav-text);
  white-space: nowrap;
}

.nav-links button {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 2px;
  padding: 4px 8px;
  cursor: pointer;
}

.site-main {
  width: min(1100px, calc(100% - 32px));
  margin: 0 auto;
  padding: 84px 0 44px;
}

.site-footer {
  width: min(1100px, calc(100% - 32px));
  margin: 0 auto;
  padding: 24px 0 44px;
  color: var(--text-muted);
  font-size: 0.82rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35em;
}

section + section { margin-top: 34px; }

/* === COMPONENTS === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 8px 18px;
  border-radius: 2px;
  border: 1px solid var(--border);
  font-size: 0.9rem;
  line-height: 1;
  cursor: pointer;
  text-decoration: none;
  transition: opacity 0.12s ease, transform 0.12s ease;
  background: transparent;
  color: var(--text);
  font-family: var(--mono);
}
.btn:hover { text-decoration: none; opacity: 0.75; transform: translateY(-1px); }

.btn.primary {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}
.btn.primary:hover { opacity: 0.82; }

.btn.ghost { background: transparent; }

.btn.danger {
  background: var(--danger-bg);
  color: var(--danger-text);
  border-color: var(--danger-bg);
}

.btn.full { width: 100%; margin-top: 14px; }

.actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}
.actions form { margin: 0; }

.notice {
  padding: 12px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 2px;
  margin-bottom: 16px;
}
.notice.error, .notice.danger {
  border-color: var(--text);
  color: var(--text);
  font-weight: 700;
}

.muted { color: var(--text-muted); }

.section-heading,
.project-title-row,
.file-head,
.project-head,
.editor-toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.section-heading { margin-bottom: 14px; }

/* === PROJECT GRID === */
.project-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 14px;
}

.project-card {
  display: grid;
  gap: 8px;
  min-height: 150px;
  padding: 16px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 2px;
  transition: border-color 0.12s ease;
  box-shadow: var(--card-depth);
  position: relative;
}

/* Book spine effect */
:root[data-theme="book"] .project-card::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: var(--spine-w);
  background: var(--spine-bg);
  border-radius: 2px 0 0 2px;
}

.project-card:hover { border-color: var(--text); }

.project-card-title { color: var(--text); font-weight: 700; }
.project-owner, .meta-row { color: var(--text-muted); font-size: 0.82rem; }
.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: auto;
}

/* === BREADCRUMB === */
.breadcrumb {
  color: var(--text-muted);
  font-size: 0.86rem;
  margin-bottom: 6px;
}
.breadcrumb a { color: var(--text-muted); }
.breadcrumb a:hover { color: var(--link-h); }

/* === PROJECT LAYOUT === */
.project-layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 18px;
  margin-top: 16px;
}

.sidebar {
  align-self: start;
  position: sticky;
  top: 68px;
  max-height: calc(100vh - 96px);
  overflow: auto;
  padding: 14px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 2px;
}
.sidebar-title {
  margin-bottom: 8px;
  color: var(--text-muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.file-tree { font-size: 0.875rem; }
.file-tree ul { list-style: none; padding-left: 12px; margin: 0; }
.file-tree > ul { padding-left: 0; }
.file-tree li + li { margin-top: 2px; }
.file-tree a {
  color: var(--text);
  text-decoration: none;
  display: block;
  padding: 2px 6px;
  border-radius: 2px;
  overflow-wrap: anywhere;
}
.file-tree a:hover { background: var(--border); text-decoration: none; }
.file-tree a.active { background: var(--text); color: var(--bg); }
.file-tree .folder > summary {
  cursor: pointer;
  padding: 2px 6px;
  font-weight: 600;
  list-style: none;
}
.file-tree details[open] > summary::before { content: "▾ "; }
.file-tree details:not([open]) > summary::before { content: "▸ "; }

.tree-toggle { display: none; }
.project-main { min-width: 0; }

/* === BACKLINKS === */
.backlinks {
  margin-top: 20px;
  padding: 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 2px;
}
.backlinks summary { cursor: pointer; font-weight: 700; }
.backlinks ul, .file-list ul, .admin-list {
  margin-top: 10px;
  padding-left: 20px;
}

/* === EMPTY / STATS === */
.empty-state {
  display: grid;
  gap: 14px;
  padding: 24px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 2px;
}

.tabs { display: flex; gap: 8px; margin: 14px 0; }
.tabs a {
  padding: 6px 10px;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 2px;
  font-family: var(--mono);
  font-size: 0.88rem;
}
.tabs a.active { color: var(--bg); background: var(--text); border-color: var(--text); }

.pager { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

.avatar {
  display: grid;
  place-items: center;
  width: 44px; height: 44px;
  background: var(--border);
  color: var(--text);
  border-radius: 2px;
  font-weight: 700;
  font-size: 1.1rem;
}
.avatar.big { width: 64px; height: 64px; font-size: 1.5rem; }

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin: 18px 0 28px;
}
.stats > div {
  padding: 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 2px;
}
.stats strong { display: block; font-size: 1.5rem; }
.stats span { color: var(--text-muted); }

/* === HERO === */
.hero {
  display: grid;
  gap: 18px;
  padding: 56px 0 48px;
  border-bottom: 1px solid var(--border);
}
.hero h1 {
  max-width: 840px;
  font-size: clamp(2rem, 6vw, 4rem);
  line-height: 1.05;
  letter-spacing: -0.01em;
  font-family: var(--body-font);
}
.hero p { max-width: 680px; color: var(--text-muted); font-size: 1rem; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 10px; }

/* === AUTH / FORMS === */
.auth-card, .form-page {
  width: min(460px, 100%);
  margin: 24px auto;
  padding: 24px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 2px;
  box-shadow: var(--card-depth);
  position: relative;
}

/* Book theme: left spine on cards */
:root[data-theme="book"] .auth-card::before,
:root[data-theme="book"] .form-page::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: var(--spine-w);
  background: var(--spine-bg);
  border-radius: 2px 0 0 2px;
}

.auth-card h1, .form-page h1 { margin-bottom: 18px; }
.auth-card form, .form-page form { display: grid; gap: 14px; }

.danger-zone {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}

.profile-head { display: flex; align-items: center; gap: 16px; }

.project-head {
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}
.project-head h1, .file-head h1 { overflow-wrap: anywhere; }
.file-head { margin-bottom: 18px; }

/* === EDITOR === */
.editor-page { width: min(1200px, 100%); }
.editor-form { display: grid; gap: 12px; }

.editor-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-height: 64vh;
  border: 1px solid var(--border);
  border-radius: 2px;
  overflow: hidden;
  background: var(--bg-card);
}

#markdown-input {
  min-height: 64vh;
  border: 0;
  border-right: 1px solid var(--border);
  border-radius: 0;
  padding: 16px;
  background: var(--bg-card);
  line-height: 1.55;
  font-family: "IBM Plex Mono", monospace;
}

.preview-pane {
  min-width: 0;
  max-height: 64vh;
  overflow: auto;
  padding: 16px;
}

.editor-tabs > input, .editor-tabs > label { display: none; }

.check-row {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: start;
  gap: 10px;
  padding: 12px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 2px;
}
.check-row input { width: auto; margin-top: 4px; accent-color: var(--text); }
.check-row small { display: block; color: var(--text-muted); font-size: 0.78rem; }

/* === BADGES === */
.visibility-badge {
  display: inline-flex;
  align-items: center;
  min-height: 18px;
  margin-left: 6px;
  padding: 1px 6px;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  vertical-align: middle;
  font-family: var(--mono);
}

/* === FOOTER NAME === */
.site-name-link {
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 700;
}
.site-name-link:hover { color: var(--text); text-decoration: none; }

/* === COOKIE NOTE === */
.cookie-note {
  position: fixed;
  right: 10px;
  bottom: 10px;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: min(360px, calc(100vw - 20px));
  padding: 6px 7px;
  color: var(--text-muted);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 3px;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.08);
  font-size: 0.68rem;
  line-height: 1.2;
}
.cookie-note[hidden] { display: none; }
.cookie-note button {
  padding: 2px 5px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 2px;
  cursor: pointer;
  font: inherit;
  white-space: nowrap;
}
.cookie-note button:hover {
  background: var(--text);
  color: var(--bg);
}

/* === ADMIN === */
.admin-list { padding-left: 0; list-style: none; }
.admin-list li {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}
.admin-list li:last-child { border-bottom: 0; }

/* === BOOK THEME EXTRAS === */
:root[data-theme="book"] .markdown-body {
  font-family: var(--serif);
  line-height: 1.85;
}
:root[data-theme="book"] .markdown-body h1,
:root[data-theme="book"] .markdown-body h2,
:root[data-theme="book"] .markdown-body h3 {
  font-family: var(--serif);
}
:root[data-theme="book"] body {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4'%3E%3Crect width='4' height='4' fill='%23f5f0e8'/%3E%3Crect width='1' height='1' fill='%23ede8dc' opacity='0.6'/%3E%3C/svg%3E");
}

/* === TERMINAL THEME EXTRAS === */
:root[data-theme="terminal"] .top-nav {
  border-bottom-color: #00441a;
}
:root[data-theme="terminal"] input,
:root[data-theme="terminal"] textarea,
:root[data-theme="terminal"] select {
  caret-color: #00dd44;
}
:root[data-theme="terminal"] a {
  text-decoration: underline;
  text-underline-offset: 2px;
}
:root[data-theme="terminal"] a:hover {
  text-decoration: none;
}

/* === BROKEN LINK === */
.broken-link {
  color: var(--text-muted);
  border-bottom: 1px dashed currentColor;
}

/* === RESPONSIVE === */
@media (max-width: 760px) {
  .top-nav {
    height: auto;
    min-height: 52px;
    padding: 10px 16px;
    align-items: flex-start;
  }
  .nav-links {
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
  }
  .site-main {
    width: min(100% - 20px, 1100px);
    padding-top: 106px;
  }
  .project-head, .file-head, .editor-toolbar { display: grid; }

  .tree-toggle {
    display: inline-flex;
    margin-top: 12px;
    padding: 7px 10px;
    color: var(--text);
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 2px;
    cursor: pointer;
    font-family: var(--mono);
  }
  .project-layout { display: block; }
  .sidebar {
    display: none;
    position: fixed;
    top: 70px; left: 10px; right: 10px;
    z-index: 15;
    max-height: 70vh;
  }
  .sidebar.open { display: block; }
  .project-main { margin-top: 14px; }

  .editor-tabs > label {
    display: inline-flex;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: 2px;
    margin: 4px 4px 8px 0;
    cursor: pointer;
  }
  .editor-tabs > input:checked + label {
    color: var(--bg);
    background: var(--text);
    border-color: var(--text);
  }
  .editor-split { display: block; }
  #markdown-input, .preview-pane {
    display: none;
    min-height: 66vh;
    max-height: none;
    border: 0;
  }
  #tab-edit:checked ~ .editor-split #markdown-input { display: block; }
  #tab-preview:checked ~ .editor-split .preview-pane { display: block; }

  .stats { grid-template-columns: 1fr; }

  .g-recaptcha {
    transform-origin: left top;
    transform: scale(0.88);
    margin-bottom: -14px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
