:root {
  --bg: #1a1b26;
  --bg-elev: #20212e;
  --bg-elev2: #262736;
  --border: #353748;
  --text: #c0caf5;
  --text-dim: #7a7fa8;
  --accent: #7aa2f7;
  --accent-dim: #3d5a99;
  --danger: #f7768e;
  --ok: #9ece6a;
  --mono: "SF Mono", "Fira Code", Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: var(--sans);
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }

.screen { height: 100%; }

/* ---- setup screen ---- */
#setup { display: flex; align-items: center; justify-content: center; padding: 20px; }
.setup-box {
  max-width: 420px; width: 100%;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px;
}
.setup-box h1 { margin: 0 0 8px; font-size: 1.6rem; }
.setup-box p { color: var(--text-dim); font-size: 0.9rem; line-height: 1.4; }
.setup-box code { background: var(--bg-elev2); padding: 2px 5px; border-radius: 4px; }
#token-input {
  width: 100%; padding: 12px; margin: 14px 0; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  font-family: var(--mono); font-size: 0.95rem;
}
#token-save {
  width: 100%; padding: 12px; border-radius: 8px; border: none;
  background: var(--accent); color: #0d0e16; font-weight: 600; font-size: 1rem;
  cursor: pointer;
}
#token-save:active { opacity: 0.8; }
.hint { font-size: 0.75rem; margin-top: 10px; }

/* ---- app layout ---- */
#app { display: flex; flex-direction: column; }

#topbar {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--bg-elev); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#current-path {
  font-size: 0.95rem; font-weight: 600; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; max-width: 50vw;
}
.spacer { flex: 1; }
.icon-btn {
  background: none; border: none; color: var(--text); font-size: 1.2rem;
  padding: 6px 10px; border-radius: 8px; cursor: pointer; line-height: 1;
}
.icon-btn:active { background: var(--bg-elev2); }

#layout { flex: 1; display: flex; min-height: 0; position: relative; }

#sidebar {
  width: 260px; flex-shrink: 0; background: var(--bg-elev);
  border-right: 1px solid var(--border); overflow-y: auto;
  display: flex; flex-direction: column;
}
#tree { flex: 1; padding: 8px; }
#sidebar-footer {
  display: flex; flex-direction: column; gap: 6px; padding: 10px;
  border-top: 1px solid var(--border);
}
#sidebar-footer button {
  background: var(--bg-elev2); border: 1px solid var(--border); color: var(--text);
  padding: 9px; border-radius: 8px; font-size: 0.85rem; cursor: pointer;
}
#logout-btn { color: var(--danger); }

.folder-row, .file-row {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 8px; border-radius: 7px; cursor: pointer;
  font-size: 0.88rem; user-select: none;
}
.folder-row { color: var(--text-dim); font-weight: 600; margin-top: 4px; }
.folder-row:hover, .file-row:hover { background: var(--bg-elev2); }
.file-row.active { background: var(--accent-dim); color: #fff; }
.folder-children { margin-left: 14px; }
.tree-icon { opacity: 0.7; font-size: 0.85em; width: 1.1em; text-align: center; }

#sidebar-backdrop {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 5;
}

#content {
  flex: 1; overflow-y: auto; padding: 18px 20px 60px;
  min-width: 0;
}
.empty-state { color: var(--text-dim); text-align: center; margin-top: 60px; }

#editor {
  width: 100%; height: calc(100vh - 150px); min-height: 300px;
  background: var(--bg-elev); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px; font-family: var(--mono); font-size: 0.92rem;
  line-height: 1.5; resize: none;
}

/* ---- markdown render ---- */
.markdown-body { max-width: 760px; margin: 0 auto; line-height: 1.65; }
.markdown-body h1, .markdown-body h2, .markdown-body h3 {
  border-bottom: 1px solid var(--border); padding-bottom: 6px; margin-top: 1.6em;
}
.markdown-body h1 { font-size: 1.6rem; }
.markdown-body h2 { font-size: 1.3rem; }
.markdown-body h3 { font-size: 1.1rem; }
.markdown-body a { color: var(--accent); text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }
.markdown-body code {
  background: var(--bg-elev2); padding: 2px 5px; border-radius: 5px;
  font-family: var(--mono); font-size: 0.88em;
}
.markdown-body pre {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 8px;
  padding: 12px; overflow-x: auto;
}
.markdown-body pre code { background: none; padding: 0; }
.markdown-body blockquote {
  border-left: 3px solid var(--accent-dim); margin: 1em 0; padding: 2px 16px;
  color: var(--text-dim);
}
.markdown-body img { max-width: 100%; border-radius: 10px; margin: 10px 0; }
.markdown-body table { border-collapse: collapse; width: 100%; margin: 1em 0; font-size: 0.9rem; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; }
.markdown-body th { background: var(--bg-elev2); }
.markdown-body ul, .markdown-body ol { padding-left: 1.4em; }
.markdown-body li.task-list-item { list-style: none; margin-left: -1.4em; }
.markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 1.8em 0; }

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--bg-elev2); border: 1px solid var(--border); padding: 10px 18px;
  border-radius: 10px; font-size: 0.88rem; opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s; z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.ok { border-color: var(--ok); }
#toast.err { border-color: var(--danger); }

@media (max-width: 760px) {
  #sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 10;
    width: 78vw; max-width: 300px;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-backdrop.show { display: block; }
  #content { padding: 14px 16px 60px; }
  #current-path { max-width: 40vw; }
}
