/* Clortex web UI styles */

:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --sidebar-bg: #1c2333;
  --sidebar-fg: #aeb8cd;
  --sidebar-fg-strong: #e6eaf2;
  --border: #e4e7ee;
  --text: #24292f;
  --muted: #6b7280;
  --accent: #4f6ef7;
  --accent-soft: #eef1fe;
  --mark: #ffe58a;
  --active-bg: #2a3350;
  --green: #2f9e5f;
  --amber: #b7791f;
  --gray: #8a919e;
  --danger: #d64545;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
  --sidebar-width: 300px;
}

* { box-sizing: border-box; }

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

.hidden { display: none !important; }

/* ── Login ─────────────────────────────────────────────── */

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding: max(16px, env(safe-area-inset-top))
           max(16px, env(safe-area-inset-right))
           max(16px, env(safe-area-inset-bottom))
           max(16px, env(safe-area-inset-left));
  background: radial-gradient(1200px 600px at 50% -10%, #2a3350, #161b28);
}

.login-card {
  width: 100%;
  max-width: 320px;
  padding: 40px 32px;
  background: var(--panel);
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
  text-align: center;
}

.login-mark {
  width: 52px;
  height: 52px;
  margin: 0 auto 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border-radius: 14px;
}

.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-sub { margin: 0 0 22px; color: var(--muted); font-size: 13px; }

.login-card input {
  width: 100%;
  padding: 11px 13px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}
.login-card input + input { margin-top: 10px; }
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.login-card button {
  width: 100%;
  margin-top: 12px;
  padding: 11px;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  cursor: pointer;
}
.login-card button:hover { background: #3d5bf0; }

.login-error { margin: 12px 0 0; color: var(--danger); font-size: 13px; }

/* ── Layout ────────────────────────────────────────────── */

.app { display: flex; height: 100vh; height: 100dvh; position: relative; }

.sidebar {
  position: relative;
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  max-width: var(--sidebar-width);
  background: var(--sidebar-bg);
  color: var(--sidebar-fg);
  display: flex;
  flex-direction: column;
  transition: width 0.2s ease, min-width 0.2s ease, max-width 0.2s ease, transform 0.2s ease;
  overflow: hidden;
}

.app.sidebar-collapsed .sidebar {
  width: 0;
  min-width: 0;
  max-width: 0;
}

.app.sidebar-resizing .sidebar {
  transition: none;
}

.sidebar-resize {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
  touch-action: none;
}

.sidebar-resize:hover,
.app.sidebar-resizing .sidebar-resize {
  background: rgba(255, 255, 255, 0.12);
}

.app.sidebar-collapsed .sidebar-resize {
  display: none;
}

.sidebar-backdrop {
  display: none;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px;
  padding-top: max(16px, env(safe-area-inset-top));
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}

.brand-mark {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border-radius: 8px;
  flex-shrink: 0;
}

.brand-name {
  color: var(--sidebar-fg-strong);
  font-weight: 600;
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
}

.sidebar-close { display: none; color: var(--sidebar-fg); }

.status-filters {
  display: flex;
  gap: 6px;
  padding: 10px 12px 0;
  flex-wrap: wrap;
}

.status-chip {
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 600;
  color: var(--sidebar-fg);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 999px;
  cursor: pointer;
}
.status-chip:hover { color: var(--sidebar-fg-strong); background: rgba(255, 255, 255, 0.1); }
.status-chip.active { color: #fff; border-color: transparent; }
.status-chip[data-status="active"].active { background: var(--green); }
.status-chip[data-status="draft"].active { background: var(--amber); }
.status-chip[data-status="archived"].active { background: var(--gray); }

.tree-empty {
  margin: 12px 8px;
  padding: 8px;
  font-size: 12px;
  color: var(--sidebar-fg);
  opacity: 0.8;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { color: var(--text); background: var(--bg); }

.tree { flex: 1; overflow: auto; padding: 10px 8px 20px; font-size: 13px; -webkit-overflow-scrolling: touch; }

/* ── Tree ──────────────────────────────────────────────── */

.tree details > summary {
  list-style: none;
  cursor: pointer;
  padding: 5px 8px;
  border-radius: 6px;
  color: var(--sidebar-fg-strong);
  font-weight: 600;
  user-select: none;
}
.tree details > summary::-webkit-details-marker { display: none; }
.tree details > summary::before {
  content: "▸";
  display: inline-block;
  width: 14px;
  transition: transform 0.12s ease;
}
.tree details[open] > summary::before { transform: rotate(90deg); }
.tree details > summary:hover { background: rgba(255, 255, 255, 0.05); }

.tree-children { padding-left: 14px; border-left: 1px solid rgba(255, 255, 255, 0.08); margin-left: 10px; }

.tree-file {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 8px;
  margin: 1px 0;
  border-radius: 6px;
  color: var(--sidebar-fg);
  text-decoration: none;
  cursor: pointer;
}
.tree-file:hover { background: rgba(255, 255, 255, 0.05); color: var(--sidebar-fg-strong); }
.tree-file.active { background: var(--active-bg); color: #fff; }

.tree-file .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Status dots ───────────────────────────────────────── */

.dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.dot.active { background: var(--green); }
.dot.draft { background: var(--amber); }
.dot.archived { background: var(--gray); }

/* ── Main ──────────────────────────────────────────────── */

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

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.search-wrap { position: relative; flex: 1; max-width: 560px; min-width: 0; }
.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
}
#search {
  width: 100%;
  padding: 9px 12px 9px 34px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
  background: var(--bg);
}
#search:focus { border-color: var(--accent); background: #fff; }

.logout {
  padding: 8px 14px;
  font-size: 13px;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}
.logout:hover { color: var(--text); background: var(--bg); }

.results {
  padding: 12px 20px 0;
  overflow: auto;
  max-height: 45vh;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.result {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
}
.result:hover { background: var(--bg); }
.result .r-title { font-weight: 600; font-size: 14px; }
.result .r-path { font-size: 12px; color: var(--muted); margin: 2px 0 4px; font-family: ui-monospace, monospace; word-break: break-all; }
.result .r-snippet { font-size: 13px; color: var(--muted); }
.result mark { background: var(--mark); color: inherit; padding: 0 1px; border-radius: 2px; }

.content {
  flex: 1;
  overflow: auto;
  padding: 32px 44px 64px;
  max-width: 860px;
  width: 100%;
  -webkit-overflow-scrolling: touch;
}

.empty-state { text-align: center; color: var(--muted); margin-top: 15vh; }
.empty-mark {
  width: 56px; height: 56px; margin: 0 auto 14px;
  display: flex; align-items: center; justify-content: center;
  font-size: 26px; font-weight: 700; color: #fff;
  background: var(--accent); border-radius: 14px; opacity: 0.9;
}

/* ── Markdown rendering ────────────────────────────────── */

.page-head { margin-bottom: 24px; padding-bottom: 18px; border-bottom: 1px solid var(--border); }
.page-title { font-size: 28px; margin: 0 0 10px; line-height: 1.25; }
.page-meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}
.badge.status-active { background: #e6f6ec; color: var(--green); }
.badge.status-draft { background: #fbf1dd; color: var(--amber); }
.badge.status-archived { background: #eef0f3; color: var(--gray); }
.badge.project { background: #f0f0f5; color: #5a5f73; font-family: ui-monospace, monospace; }

.markdown { font-size: 15px; line-height: 1.7; overflow-x: auto; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4 {
  line-height: 1.3; margin: 1.6em 0 0.6em; font-weight: 600;
}
.markdown h1 { font-size: 24px; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.markdown h2 { font-size: 20px; }
.markdown h3 { font-size: 16px; }
.markdown p { margin: 0 0 1em; }
.markdown a { color: var(--accent); text-decoration: none; }
.markdown a:hover { text-decoration: underline; }
.markdown code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
  background: #f0f1f4;
  padding: 2px 5px;
  border-radius: 4px;
}
.markdown pre {
  background: #f6f8fa;
  padding: 14px 16px;
  border-radius: 8px;
  overflow: auto;
  border: 1px solid var(--border);
  max-width: 100%;
}
.markdown pre code { background: transparent; padding: 0; font-size: 13px; }
.markdown blockquote {
  margin: 1em 0;
  padding: 2px 16px;
  border-left: 4px solid var(--accent);
  color: var(--muted);
  background: var(--accent-soft);
  border-radius: 0 6px 6px 0;
}
.markdown table { border-collapse: collapse; margin: 1em 0; width: 100%; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 8px 12px; text-align: left; }
.markdown th { background: var(--bg); font-weight: 600; }
.markdown ul, .markdown ol { padding-left: 1.5em; }
.markdown li { margin: 0.25em 0; }
.markdown hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }
.markdown img { max-width: 100%; border-radius: 8px; }

/* ── Phone / tablet ────────────────────────────────────── */

@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    width: min(var(--sidebar-width), 86vw);
    min-width: min(var(--sidebar-width), 86vw);
    transform: translateX(-105%);
    box-shadow: none;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .app.sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.28);
  }

  .app.sidebar-collapsed .sidebar {
    width: min(var(--sidebar-width), 86vw);
    min-width: min(var(--sidebar-width), 86vw);
  }

  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 30;
    background: rgba(16, 24, 40, 0.45);
  }

  .app.sidebar-open .sidebar-backdrop { display: block; }

  .sidebar-close {
    display: inline-flex;
    border-color: rgba(255, 255, 255, 0.12);
    color: var(--sidebar-fg-strong);
  }
  .sidebar-close:hover { background: rgba(255, 255, 255, 0.08); color: #fff; }

  .sidebar-resize { display: none; }

  .topbar { padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); }
  .search-wrap { max-width: none; }
  .logout { padding: 8px 10px; }
  .results { padding: 10px 12px 0; }
  .content {
    padding: 20px 16px 48px;
    padding-bottom: max(48px, env(safe-area-inset-bottom));
  }
  .page-title { font-size: 22px; }
  .login-card { padding: 32px 22px; }
}

@media (max-width: 420px) {
  .logout { font-size: 12px; padding: 8px 8px; }
}
